-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcdt.m
More file actions
175 lines (133 loc) · 4 KB
/
cdt.m
File metadata and controls
175 lines (133 loc) · 4 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
function [X,T]=cdt(xb, xi, nTri, keepBoundary, arg)
if nargin<2 || isempty(xi), xi = zeros(0, 2); end
if nargin<4, keepBoundary = false; end
if ~isreal(xb), xb = [real(xb) imag(xb)]; end
assert( size(xb, 2)==2, 'b is not a matrix with 2 columns, transposed?' );
triangle = which('triangle.ex_');
assert(~isempty(triangle), 'Can''t find triangle application');
filename = tempname;
writePoly([filename '.poly'], xb, xi);
if nargin<5
if nargin<3 || nTri<=0
averArea = 0;
else
if ~iscell(xb)
averArea = polyarea(xb(:,1)', xb(:,2)', 2)/nTri;
else
averArea = abs( sum(cellfun(@signedpolyarea, xb))/nTri );
end
end
%%
% -Y Prohibits the insertion of Steiner points on the mesh boundary
% -c including convex hull into the triangulation,
% -q50 for specifying min angle
arg = ' -g ';
if keepBoundary, arg = [arg '-Y ']; end
% use num2str(..., '%f') to fix a bug of not getting target # triangles
if averArea>0, arg = [arg '-a' num2str(averArea, '%f') ' ']; end
evalc(['!"' triangle '" -q30' arg filename '.poly']);
% eval(['!"' triangle '" -c -q50 -g -Y ' filename '.poly']);
else % use input arg
evalc(['!"' triangle '" -g' arg ' ' filename '.poly']);
end
%%
[X, T] = loadOff( [filename '.1.off'] );
X = X(:, 1:2);
eval( ['delete ' filename '.poly'] );
eval( ['delete ' filename '.1.node'] );
eval( ['delete ' filename '.1.ele'] );
eval( ['delete ' filename '.1.poly'] );
eval( ['delete ' filename '.1.off'] );
%% important for some mesh processing. Not clear why Triangle produce these isolated vertices
if ~all( sparse(1, T, 1, 1, size(X,1)) )
nv0 = size(X, 1);
[X, T] = removeIsolatedVerticesInMesh(X, T);
warning('%d isolated vertices produced by Triangle have been removed', nv0-size(X,1));
end
function [X, T] = loadOff(filename)
%%
[fid, errmsg] = fopen(filename,'r');
if fid == -1
warning(errmsg);
return;
end
%%
str = textscan(fid, '%s', 1, 'CommentStyle', '#');
if ~strcmp(str{1}, 'OFF')
return;
end
str = textscan(fid, '%d %d %d', 1, 'CommentStyle', '#');
nv = str{1};
nf = str{2};
%%
X = cell2mat( textscan(fid, '%f %f %f', nv, 'CommentStyle', '#') );
%%
pos = ftell(fid);
tsz = textscan(fid, '%f %*[^\n]', nf, 'CommentStyle', '#');
fseek(fid, pos, 'bof');
%%
if all(tsz{1}==3)
T = cell2mat( textscan(fid, '%*f %f %f %f', nf, 'CommentStyle', '#') ) + 1;
else
T = cell(nf, 1);
for i=1:nf
str = textscan(fid, '%f', 1, 'CommentStyle', '#');
str = textscan(fid, '%f', str{1}, 'CommentStyle', '#');
T{i} = reshape(str{1}, 1, []) + 1;
end
end
%%
fclose(fid);
function writePoly(filename, xb, xi, edges)
if nargin<3
xi = zeros(0, 2);
end
if nargin<4
edges = zeros(0, 2);
end
[fid,errmsg] = fopen(filename,'wt');
if fid == -1
disp(errmsg);
return;
end
if iscell(xb)
holes = xb(2:end);
xb = xb{1};
else
holes = {};
end
nH = numel(holes);
nb = size(xb,1);
nhv = sum( cellfun(@length, holes) );
nv = size(xi,1) + nb + nhv;
%% vertices
fprintf(fid, '%d 2 0 0\n', nv);
fprintf(fid, '%d %.8f %.8f\n', [1:nv; xb(:,1:2)' xi(:,1:2)' cat(1,holes{:})'] );
%% edges
nie = size(edges,1);
fprintf(fid, '%d 0\n', nb+nie+nhv);
fGenPolyEdges = @(n) [1:n; 2:n 1];
e = [fGenPolyEdges(nb) edges'+nb];
for i=1:nH
n = nv - sum( cellfun(@length, holes(i:end)) );
e = [e fGenPolyEdges(size(holes{i}, 1 ))+n];
end
fprintf(fid, '%d %d %d\n', [1:size(e,2); e] );
% fprintf(fid, '%d %d %d\n', [1:nb; 1:nb; [2:nb 1]] );
% fprintf(fid, '%d %d %d\n', [(1:nie)+nb; edges'] );
%% holes
fprintf(fid, '%d\n', nH);
fprintf(fid, '%d %.8f %.8f\n', [1:nH; cell2mat( cellfun( @findPointInPolygon, holes, 'UniformOutput', false ) )'] );
fclose(fid);
function x = findPointInPolygon(p)
poly2area = @(x) sum( x(:,1).*x([2:end 1],2) - x([2:end 1],1).*x(:,2) );
n = size(p,1);
k = nchoosek(1:n, 3);
a = poly2area(p);
for i=1:size(k,1)
tri = p(k(i,:),:);
x = sum(tri)/3;
if poly2area(tri)/a > 1e-4 && inpolygon(x(1), x(2), p(:,1), p(:,2))
break;
end
end