计算网格模型各点之间测地线距离
写在前面
最近白天在做实验室的事情,晚上学一点iOS基础知识,两个事情进度都挺慢的,心里有点方啊。。。但是方也没办法,只能督促自己提高效率。目前实验室的任务是研究三维序列网格水印,在正式设计自己的代码前,先要实现一篇论文,这篇论文在之前的文章中也有讲过,大意是基于测地线距离的网格序列水印,文章整体思路如下:
整个算法的实现略复杂,由于SDF分割在CGAL库中有实现,所以花了好多时间来配置CGAL环境,但是最终还是不能运行样例代码,所以决定先用k-means分割替代。在这个算法中,需要计算区域内的顶点到区域边界的测地线距离。我们可以先计算网格上任意两点之间的测地线距离,然后根据需要取出某两点之间的测地线距离
代码
% fastmarch Fast marching algorithm for geodesic distance approximation % % Usage: % % D = fastmarch(TRIV, X, Y, Z, [src], [opt]) % D = fastmarch(surface, [src], [opt]) % % Description: % % Computes the geodesic distances on a triangulated surfaces using % the fast marching algorithm. The algorithm may operate in two modes: % single-source and multiple-source. In the single-source mode, a distance % map of every mesh vertex from a single source is computed. The source % may be a single point on the mesh, or any other configuration described % by an initial set of values per mesh vertex. In the multiple-source % mode, a matrix of pair-wise geodesic distances is computed between a % specified set of mesh vertices. % % Input: % % TRIV - ntx3 triangulation matrix with 1-based indices (as the one % returned by the MATLAB function delaunay). % X,Y,Z - vectors with nv vertex coordinates. % surface - alternative way to specify the mesh as a struct, having .TRIV, % .X, .Y, and .Z as its fields. % src - in the multiple-source mode: (default: src = [1:nv]) % list of ns mesh vertex indices to be used as sources. % in the single-source mode: (must be specified) % an nvx1 list of initial values of the distance function on the mesh % (set a vertex to Inf to exclude it from the source set). src % opt - (optional) settings % .mode - Mode (default: 'multiple') % 'multiple' - multiple-source % 'single' - single-source % % Output: % % D - In the multiple-source mode: % nsxns matrix of approximate geodesic distances, where D(i,j) is % the geodesic distance between the i-th and the j-th point, % whose indices are specified by src(i) and src(j), % respectively. % In the single-source mode: % nvx1 vector of approximated geodesic distances, where D(i) is % the geodesic distance from the i-th mesh vertex to the % source. function [D,L] = fastmarch(TRIV, X, Y, Z, src, opt) if nargin < 4, surface = TRIV; TRIV = surface.TRIV; X = surface.X; Y = surface.Y; Z = surface.Z; end mode = 0; if nargin > 5 & isfield(opt, 'mode'), if strcmpi(opt.mode, 'multiple'), mode = 0; elseif strcmpi(opt.mode, 'single'), mode = 1; else error('Invalid mode. Use either "multiple" or "single".'); end end if nargin == 1 | nargin == 4, if mode == 0, src = [1:length(X)]; else error('Source set must be specified in single source mode.'); end end if mode & length(src) ~= length(X(:)), error('src must be nvx1 in the single source mode.'); end % MEX implementation if ~mode, [D] = fastmarch_mex(int32(TRIV-1), int32(src(:)-1), double(X(:)), double(Y(:)), double(Z(:))); else idx = find(src==0); srclabel = zeros(length(src),1); srclabel(idx) = 1:length(idx); [D,L] = fastmarch1_mex(int32(TRIV-1), double([src(:); srclabel(:)]), double(X(:)), double(Y(:)), double(Z(:))); end D(D>=9999999) = Inf;
总结
具体的解释都写在代码中的注释里了,这里就不赘述了