-
Notifications
You must be signed in to change notification settings - Fork 19
/
distmesh_demo.m
316 lines (284 loc) · 10.2 KB
/
distmesh_demo.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
function [ p, t ] = distmesh_demo( cases, do_plot )
% DISTMESH_DEMO Run distmesh example demos.
%
% [ P, T ] = DISTMESH_DEMO( CASES, DO_PLOT ) Run distmesh example
% demos. CASES may be used to specify the distmesh examples to run
% (default 1:10). DO_PLOT specifies whether to plot or not (default
% true).
%
% Example 1: (Uniform mesh on unit circle)
% fd = @(p) sqrt(sum(p.^2,2)) - 1;
% fh = @(p) ones(size(p,1),1);
% [p,t] = distmesh( fd, fh, 0.2, [-1,-1;1,1] );
% patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
%
% Example 2: (Uniform mesh on ellipse)
% fd = @(p) p(:,1).^2/2^2 + p(:,2).^2/1^2 - 1;
% fh = @(p) ones(size(p,1),1);
% [p,t] = distmesh( fd, fh, 0.2, [-2,-1;2,1] );
% patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
%
% Example 3: (Uniform mesh on unit square)
% fd = @(p) -min(min(min(1+p(:,2),1-p(:,2)),1+p(:,1)),1-p(:,1));
% fh = @(p) ones(size(p,1),1);
% [p,t] = distmesh( fd, fh, 0.2, [-1,-1;1,1], [-1,-1;-1,1;1,-1;1,1] );
% patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
%
% Example 4: (Uniform mesh on complex polygon)
% pv = [-0.4 -0.5;0.4 -0.2;0.4 -0.7;1.5 -0.4;0.9 0.1;
% 1.6 0.8;0.5 0.5;0.2 1;0.1 0.4;-0.7 0.7;-0.4 -0.5];
% fd = { 'l_dpolygon', [], pv };
% fh = @(p) ones(size(p,1),1);
% [p,t] = distmesh( fd, fh, 0.1, [-1,-1; 2,1], pv );
% patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
%
% Example 5: (Rectangle with circular hole, refined at circle boundary)
% drectangle = @(p,x1,x2,y1,y2) -min(min(min(-y1+p(:,2),y2-p(:,2)),-x1+p(:,1)),x2-p(:,1));
% fd = @(p) max( drectangle(p,-1,1,-1,1), -(sqrt(sum(p.^2,2))-0.5) );
% fh = @(p) 0.05 + 0.3*(sqrt(sum(p.^2,2))-0.5);
% [p,t] = distmesh( fd, fh, 0.05, [-1,-1;1,1], [-1,-1;-1,1;1,-1;1,1] );
% patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
%
% Example 6: (Square, with size function point and line sources)
% dcircle = @(p,xc,yc,r) sqrt((p(:,1)-xc).^2+(p(:,2)-yc).^2)-r;
% fd = @(p) -min(min(min(p(:,2),1-p(:,2)),p(:,1)),1-p(:,1));
% dpolygon = @(p,v) feval('l_dpolygon',p,v);
% fh = @(p) min(min(0.01+0.3*abs(dcircle(p,0,0,0)), ...
% 0.025+0.3*abs(dpolygon(p,[0.3,0.7;0.7,0.5;0.3,0.7]))),0.15);
% [p,t] = distmesh( fd, fh, 0.01, [0,0;1,1], [0,0;1,0;0,1;1,1] );
% patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
%
% Example 7: (NACA0012 airfoil)
% hlead = 0.01; htrail = 0.04; hmax = 2; circx = 2; circr = 4;
% a = 0.12/0.2*[0.2969,-0.126,-0.3516,0.2843,-0.1036];
% fd = @(p) max( dcircle(p,circx,0,circr), ...
% -((abs(p(:,2))-polyval([a(5:-1:2),0],p(:,1))).^2-a(1)^2*p(:,1)) );
% fh = @(p) min(min(hlead+0.3*dcircle(p,0,0,0),htrail+0.3*dcircle(p,1,0,0)),hmax);
%
% fixx = 1 - htrail*cumsum(1.3.^(0:4)');
% fixy = a(1)*sqrt(fixx) + polyval([a(5:-1:2),0],fixx);
% pfix = [[circx+[-1,1,0,0]*circr; 0,0,circr*[-1,1]]'; 0,0; 1,0; fixx,fixy; fixx,-fixy];
% bbox = [circx-circr,-circr; circx+circr,circr];
% h0 = min([hlead,htrail,hmax]);
% [p,t] = distmesh( fd, fh, h0, bbox, pfix );
% patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
%
% Example 8: (Uniform mesh on unit sphere)
% fd = @(p) sqrt(sum(p.^2,2)) - 1;
% fh = @(p) ones(size(p,1),1);
% [p,t] = distmesh( fd, fh, 0.2, [-1,-1,-1;1,1,1] );
% f = [t(:,[1:3]); t(:,[1,2,4]); t(:,[2,3,4]); t(:,[3,1,4])];
% patch( 'vertices', p, 'faces', f, 'facecolor', [.9, .9, .9] )
%
% Example 9: (Uniform mesh on unit cube)
% fd = @(p) -min(min(min(min(min(p(:,3),1-p(:,3) ),p(:,2)),1-p(:,2)),p(:,1)),1-p(:,1));
% fh = @(p) ones(size(p,1),1);
% pfix = [-1,-1,-1;-1,1,-1;1,-1,-1;1,1,-1; -1,-1,1;-1,1,1;1,-1,1;1,1,1];
% [p,t] = distmesh( fd, fh, 0.2, [-1,-1,-1;1,1,1], pfix );
% f = [t(:,[1:3]); t(:,[1,2,4]); t(:,[2,3,4]); t(:,[3,1,4])];
% patch( 'vertices', p, 'faces', f, 'facecolor', [.9, .9, .9] ), view(3)
%
% Example 10: (Uniform mesh on cylinder)
% fd = @(p) -min(min(p(:,3),4-p(:,3)),1-sqrt(sum(p(:,1:2).^2,2)));
% fh = @(p) ones(size(p,1),1);
% pfix = [-1,-1,-1;-1,1,-1;1,-1,-1;1,1,-1; -1,-1,1;-1,1,1;1,-1,1;1,1,1];
% [p,t] = distmesh( fd, fh, 0.5, [-1,-1,0;1,1,4], [] );
% f = [t(:,[1:3]); t(:,[1,2,4]); t(:,[2,3,4]); t(:,[3,1,4])];
% patch( 'vertices', p, 'faces', f, 'facecolor', [.9, .9, .9] ), view(3)
%
% See also DISTMESH.
% Copyright (C) 2004-2012 Per-Olof Persson, 2018 Precise Simulation Limited.
% See COPYRIGHT.TXT for details.
close all
clc
if( ~nargin || isempty(cases) )
cases = 1:10;
end
if( nargin<2 )
do_plot = true;
end
for i=cases
eval( ['[p,t] = test_case',num2str(i),'(',num2str(do_plot),');'] );
if( do_plot && i~=cases(end) )
pause
end
fprintf('\n\n')
end
if( ~nargout )
clear p t
end
function [p,t] = test_case1( do_plot )
s = 'Example 1: (Uniform mesh on unit circle)';
disp(s)
fd = @(p) sqrt(sum(p.^2,2)) - 1;
fh = @(p) ones(size(p,1),1);
[p,t] = distmesh( fd, fh, 0.2, [-1,-1;1,1] );
if( do_plot )
clf
patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
title(s)
axis tight
axis equal
end
function [p,t] = test_case2( do_plot )
s = 'Example 2: (Uniform mesh on ellipse)';
disp(s)
fd = @(p) p(:,1).^2/2^2 + p(:,2).^2/1^2 - 1;
fh = @(p) ones(size(p,1),1);
[p,t] = distmesh( fd, fh, 0.2, [-2,-1;2,1] );
if( do_plot )
clf
patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
title(s)
axis tight
axis equal
end
function [p,t] = test_case3( do_plot )
s = 'Example 3: (Uniform mesh on unit square)';
disp(s)
fd = @(p) -min(min(min(p(:,2),1-p(:,2)),p(:,1)),1-p(:,1));
fh = @(p) ones(size(p,1),1);
[p,t] = distmesh( fd, fh, 0.2, [-1,-1;1,1], [-1,-1;-1,1;1,-1;1,1] );
if( do_plot )
clf
patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
title(s)
axis tight
axis equal
end
function [p,t] = test_case4( do_plot )
s = 'Example 4: (Uniform mesh on complex polygon)';
disp(s)
pv = [-0.4 -0.5;0.4 -0.2;0.4 -0.7;1.5 -0.4;0.9 0.1;
1.6 0.8;0.5 0.5;0.2 1;0.1 0.4;-0.7 0.7;-0.4 -0.5];
fd = { 'l_dpolygon', [], pv };
fh = @(p) ones(size(p,1),1);
[p,t] = distmesh( fd, fh, 0.1, [-1,-1; 2,1], pv );
if( do_plot )
clf
patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
title(s)
axis tight
axis equal
end
function [p,t] = test_case5( do_plot )
s = 'Example 5: (Rectangle with circular hole, refined at circle boundary)';
disp(s)
drectangle = @(p,x1,x2,y1,y2) -min(min(min(-y1+p(:,2),y2-p(:,2)),-x1+p(:,1)),x2-p(:,1));
fd = @(p) max( drectangle(p,-1,1,-1,1), -(sqrt(sum(p.^2,2))-0.5) );
fh = @(p) 0.05 + 0.3*(sqrt(sum(p.^2,2))-0.5);
[p,t] = distmesh( fd, fh, 0.05, [-1,-1;1,1], [-1,-1;-1,1;1,-1;1,1] );
if( do_plot )
clf
patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
title(s)
axis tight
axis equal
end
function [p,t] = test_case6( do_plot )
s = 'Example 6: (Square, with size function point and line sources)';
disp(s)
dcircle = @(p,xc,yc,r) sqrt((p(:,1)-xc).^2+(p(:,2)-yc).^2)-r;
fd = @(p) -min(min(min(p(:,2),1-p(:,2)),p(:,1)),1-p(:,1));
fh = @(p) min(min(0.01+0.3*abs(dcircle(p,0,0,0)), ...
0.025+0.3*abs(l_dpolygon(p,[0.3,0.7;0.7,0.5;0.3,0.7]))),0.15);
[p,t] = distmesh( fd, fh, 0.01, [0,0;1,1], [0,0;1,0;0,1;1,1] );
if( do_plot )
clf
patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
title(s)
axis tight
axis equal
end
function [p,t] = test_case7( do_plot )
s = 'Example 7: (NACA0012 airfoil)';
disp(s)
hlead = 0.01; htrail = 0.04; hmax = 2; circx = 2; circr = 4;
a = 0.12/0.2*[0.2969,-0.126,-0.3516,0.2843,-0.1036];
dcircle = @(p,xc,yc,r) sqrt((p(:,1)-xc).^2+(p(:,2)-yc).^2)-r;
fd = @(p) max( dcircle(p,circx,0,circr), ...
-((abs(p(:,2))-polyval([a(5:-1:2),0],p(:,1))).^2-a(1)^2*p(:,1)) );
fh = @(p) min(min(hlead+0.3*dcircle(p,0,0,0),htrail+0.3*dcircle(p,1,0,0)),hmax);
fixx = 1 - htrail*cumsum(1.3.^(0:4)');
fixy = a(1)*sqrt(fixx) + polyval([a(5:-1:2),0],fixx);
pfix = [[circx+[-1,1,0,0]*circr; 0,0,circr*[-1,1]]'; 0,0; 1,0; fixx,fixy; fixx,-fixy];
bbox = [circx-circr,-circr; circx+circr,circr];
h0 = min([hlead,htrail,hmax]);
[p,t] = distmesh( fd, fh, h0, bbox, pfix );
if( do_plot )
clf
patch( 'vertices', p, 'faces', t, 'facecolor', [.9, .9, .9] )
title(s)
axis tight
axis equal
end
function [p,t] = test_case8( do_plot )
s = 'Example 8: (Uniform mesh on unit sphere)';
disp(s)
fd = @(p) sqrt(sum(p.^2,2)) - 1;
fh = @(p) ones(size(p,1),1);
[p,t] = distmesh( fd, fh, 0.2, [-1,-1,-1;1,1,1] );
if( do_plot )
clf
f = [t(:,[1:3]); t(:,[1,2,4]); t(:,[2,3,4]); t(:,[3,1,4])];
patch( 'vertices', p, 'faces', f, 'facecolor', [.9, .9, .9] )
title(s)
view(3)
rotate3d('on')
axis tight
axis equal
end
function [p,t] = test_case9( do_plot )
s = 'Example 9: (Uniform mesh on unit cube)';
disp(s)
fd = @(p) -min(min(min(min(min(p(:,3),1-p(:,3) ),p(:,2)),1-p(:,2)),p(:,1)),1-p(:,1));
fh = @(p) ones(size(p,1),1);
pfix = [-1,-1,-1;-1,1,-1;1,-1,-1;1,1,-1; -1,-1,1;-1,1,1;1,-1,1;1,1,1];
[p,t] = distmesh( fd, fh, 0.2, [-1,-1,-1;1,1,1], pfix );
if( do_plot )
clf
f = [t(:,[1:3]); t(:,[1,2,4]); t(:,[2,3,4]); t(:,[3,1,4])];
patch( 'vertices', p, 'faces', f, 'facecolor', [.9 .9 .9] )
title(s)
view(3)
rotate3d('on')
axis tight
axis equal
end
function [p,t] = test_case10( do_plot )
s = 'Example 10: (Uniform mesh on cylinder)';
disp(s)
fd = @(p) -min(min(p(:,3),4-p(:,3)),1-sqrt(sum(p(:,1:2).^2,2)));
fh = @(p) ones(size(p,1),1);
pfix = [-1,-1,-1;-1,1,-1;1,-1,-1;1,1,-1; -1,-1,1;-1,1,1;1,-1,1;1,1,1];
[p,t] = distmesh( fd, fh, 0.5, [-1,-1,0;1,1,4], [] );
if( do_plot )
clf
f = [t(:,[1:3]); t(:,[1,2,4]); t(:,[2,3,4]); t(:,[3,1,4])];
patch( 'vertices', p, 'faces', f, 'facecolor', [.9 .9 .9] )
title(s)
view(3)
rotate3d('on')
axis tight
axis equal
end
%------------------------------------------------------------------------------%
function [ dist ] = l_dpolygon( p, v )
n_p = size(p,1);
n_s = size(v,1)-1;
dist = zeros(n_s,n_p);
for i_s=1:n_s
v_i = v([i_s,i_s+1],:);
n_p = size(p,1);
w = v_i(2,:)-v_i(1,:);
ix1 = ones(n_p,1);
vp = v_i(ix1,:)-p;
w1 = w(ix1,:);
s = dot(w1,vp,2);
u = -s/(w*w.');
u(u<0) = 0;
u(u>1) = 1;
h = w1.*[u,u]+vp;
dist(i_s,:) = sqrt(dot(h,h,2));
end
dist = (-1).^(inpolygon(p(:,1),p(:,2),v(:,1),v(:,2))).*min(dist).';