-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubdivideSphericalMesh.m
77 lines (63 loc) · 1.89 KB
/
SubdivideSphericalMesh.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function [TR,G]=SubdivideSphericalMesh(TR,k,G,W)
% Subdivide triangular (or quadrilateral) surface mesh, representing
% a zero-centered unit sphere, k times using triangular (or quadrilateral)
% quadrisection. See function 'TriQuad' (or 'QuadQuad') for more info.
%
% INPUT:
% - TR : surface mesh of a unit sphere represented as an object of
% 'TriRep' class, 'triangulation' class, or a cell such that
% TR={F,X}, where F is an M-by-3 or M-by-4 array of faces,
% and X is an N-by-3 array of vertex coordinates.
% - k : desired number of subdivisions. k=1 is default.
% - G : optional; scalar or vector field defined at the vertices of TR.
% - W : optional; positive weights associated with vertices of TR.
% See function 'TriQuad' (or 'QuadQuad') for more info.
%
% OUTPUT:
% - TR : subdivided mesh. Same format as input mesh.
%
% AUTHOR: Anton Semechko (a.semechko@gmail.com)
%
if nargin<2 || isempty(k), k=1; end
if nargin<3, G=[]; end
if nargin<4, W=[]; end
% Get the data structure
[F,X,fmt]=GetMeshData(TR);
if fmt==1 && size(F,2)==4
error('Tet meshes cannot be processed by this function')
end
% Make sure vertices of the input mesh lie on a unit sphere
X=ProjectOnSn(X);
% Return mesh as is if k<1
k=round(k(1));
if k<1
TR=MeshOut(F,X,fmt);
return
end
% Spherical subdivision
for i=1:k
% Subdivide mesh
if size(F,2)==3
[TR,W,G]=TriQuad({F X},W,G);
else
[TR,W,G]=QuadQuad({F X},W,G);
end
N1=size(X,1);
[F,X]=deal(TR{1},TR{2});
% Project vertices onto unit sphere
N1=N1+1;
N2=size(X,1);
X(N1:N2,:)=ProjectOnSn(X(N1:N2,:));
end
TR=MeshOut(F,X,fmt);
function TR=MeshOut(F,X,fmt)
switch fmt
case 1
TR=triangulation(F,X);
case 2
TR=TriRep(F,X); %#ok<*DTRIREP>
case 3
TR={F X};
case 4
TR=struct('faces',F,'vertices',X);
end