計算網格模型各點之間測地線距離
寫在前面
最近白天在做實驗室的事情,晚上學一點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;
總結
具體的解釋都寫在代碼中的注釋里了,這里就不贅述了