forked from dengwirda/jigsaw-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jitter.m
140 lines (88 loc) · 2.98 KB
/
jitter.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
function [mesh] = jitter(opts,imax,ibad)
%JITTER call JIGSAW iteratively, trying to improve topology.
%
% See also JIGSAW
%-----------------------------------------------------------
% Darren Engwirda
% github.com/dengwirda/jigsaw-matlab
% 03-Sep-2020
%-----------------------------------------------------------
%
mesh = []; OPTS = opts; done = false ;
if (isfield(opts,'init_file'))
%---------------------------------- load IC's if they exist!
mesh = loadmsh(opts.init_file);
end
next = mesh ; best = metric (mesh);
for iter = +1 : imax
if (~isempty(next))
keep = ...
true(size(next.point.coord, 1), 1) ;
%---------------------------------- setup initial conditions
[path,name,fext] = ...
fileparts(opts.mesh_file) ;
if (~isempty(path))
path = [path, '/'] ;
end
OPTS.init_file = ...
[path,name,'-INIT',fext] ;
if (inspect(next,'tria3'))
%---------------------------------- mark any irregular nodes
vdeg = trideg2 ( ...
next.point.coord(:,1:end-1), ...
next.tria3.index(:,1:end-1)) ;
ierr = abs(vdeg - 6) ; % error wrt. topol. degree
ierr(vdeg > 6) = ierr(vdeg > 6) * +2 ;
M = sum(ierr( ...
next.tria3.index(:,1:3)), 2) >= ibad ;
keep(next.tria3.index(M,1:3)) = false;
end
if (inspect(next,'edge2'))
keep(next.edge2.index(:,1:2)) = true ;
end
if (sum(double(keep)) < 8)
%---------------------------------- don't delete everything!
keep = true (size(keep)) ;
end
%---------------------------------- keep nodes far from seam
init.mshID = 'euclidean-mesh';
init.point.coord = ...
next.point.coord(keep,:) ;
done = all(keep);
savemsh(OPTS.init_file,init) ;
end
%---------------------------------- call JIGSAW with new ICs
next = jigsaw(OPTS) ;
cost = metric(next) ;
if (cost > best)
%---------------------------------- keep "best" mesh so far!
mesh = next;
best = cost;
end
if (done), break; end
end
end
function [cnrm] = metric(mesh)
%METRIC assemble a combined "cosst" metric for a given mesh.
cost = [] ; cnrm = +0.0 ;
if (isempty(mesh)), return ; end
if (inspect(mesh,'tria3'))
%--------------------------------------- append TRIA3 scores
COST = triscr2( ...
mesh.point.coord(:,1:end-1), ...
mesh.tria3.index(:,1:end-1)) ;
cost = [cost; COST] ;
end
if (inspect(mesh,'tria4'))
%--------------------------------------- append TRIA4 scores
COST = triscr3( ...
mesh.point.coord(:,1:end-1), ...
mesh.tria4.index(:,1:end-1)) ;
cost = [cost; COST] ;
end
if (isempty(cost)), return ; end
norm = sum ((+1.0 ./ cost) .^ 3) ;
cnrm = ...
nthroot(length(cost) ./ norm, 3) ;
end