-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmoothutil.c
585 lines (490 loc) · 20.1 KB
/
smoothutil.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*************************************************************
*
* smoothutil.c - Utility subroutines for rocksmooth
*
* Mark J. Stock, [email protected]
*
*
* rocktools - Tools for creating and manipulating triangular meshes
* Copyright (C) 1999,2004,14 Mark J. Stock
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
********************************************************** */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "structs.h"
int three_d_laplace(tri_pointer,int);
int three_d_surface_tension(tri_pointer,double);
int compute_normals_2(tri_pointer,int);
int grow_surface_along_normal(tri_pointer,double);
int define_sharp_edges(tri_pointer,double);
/*
* Run a laplacian-like function over the entire surface to smooth the nodes
*/
int three_d_laplace(tri_pointer tri_head,int num_cycles) {
int i,j;//,index;
//int num = 0;
VEC sum;//,tri_norm;
node_ptr curr_node = node_head;
//node_ptr test_node;
//tri_pointer test_tri;
/* Loop this routine a number of times */
if (num_cycles > 0) fprintf(stderr,"Smoothing surface");
for (i=0; i<num_cycles; i++) {
fprintf(stderr,".");
fflush(stderr);
/* save all current node locations in the temp_node_record */
curr_node = node_head;
while (curr_node) {
curr_node->temp_loc.x = curr_node->loc.x;
curr_node->temp_loc.y = curr_node->loc.y;
curr_node->temp_loc.z = curr_node->loc.z;
curr_node = curr_node->next_node;
}
/* move all nodes a short distance, based on the location
* of its neighbor nodes */
curr_node = node_head;
while (curr_node) {
/* Any algorithm for this needs to use the same data that finding the
* surface normal would give. Is it pointless to just take a blind
* average of adjoining nodes? Probably not. */
// fprintf(stderr," node z=%g",curr_node->loc.z); fflush(stderr);
/* Until then, let's just use a basic Laplace-like operation */
sum.x = 0.0;
sum.y = 0.0;
sum.z = 0.0;
for (j=0; j<curr_node->num_adj_nodes; j++) {
sum.x += curr_node->adj_node[j]->loc.x;
sum.y += curr_node->adj_node[j]->loc.y;
sum.z += curr_node->adj_node[j]->loc.z;
}
// fprintf(stderr," node x=%g",curr_node->loc.x); fflush(stderr);
curr_node->temp_loc.x = (curr_node->temp_loc.x + 0.1*sum.x/curr_node->num_adj_nodes) / 1.1;
curr_node->temp_loc.y = (curr_node->temp_loc.y + 0.1*sum.y/curr_node->num_adj_nodes) / 1.1;
curr_node->temp_loc.z = (curr_node->temp_loc.z + 0.1*sum.z/curr_node->num_adj_nodes) / 1.1;
// fprintf(stderr," to x=%g\n",curr_node->loc.x); fflush(stderr);
curr_node = curr_node->next_node;
}
/* apply the new locations to the nodes */
curr_node = node_head;
while (curr_node) {
curr_node->loc.x = curr_node->temp_loc.x;
curr_node->loc.y = curr_node->temp_loc.y;
curr_node->loc.z = curr_node->temp_loc.z;
curr_node = curr_node->next_node;
}
}
if (num_cycles > 0) fprintf(stderr,"\n");
/* return some metric */
return(0);
}
/*
* Run a surface-tension-like function over the entire surface to
* smooth the nodes; ref Tryggvason, JCP 169 (2) p708.
*
* apply one second's worth of surface tension with the given coefficient
*/
int three_d_surface_tension(tri_pointer tri_head,double coeff) {
int i,j;//,index;
//int num = 0;
int num_cycles = 10;
VEC force,tri_norm,oneedge;
node_ptr curr_node;
node_ptr startn,endn;
tri_pointer curr_tri;
// if coefficient is too high, increase the number of cycles
// if (coeff > 0.1) num_cycles = 2+(int)(coeff*10.);
/* Loop this routine a number of times */
if (num_cycles > 0) fprintf(stderr,"Smoothing surface");
for (i=0; i<num_cycles; i++) {
fprintf(stderr,".");
fflush(stderr);
/* save all current node locations in the temp_node_record */
curr_node = node_head;
while (curr_node) {
curr_node->temp_loc.x = curr_node->loc.x;
curr_node->temp_loc.y = curr_node->loc.y;
curr_node->temp_loc.z = curr_node->loc.z;
curr_node = curr_node->next_node;
}
/* move all nodes a short distance, based on the location
* of its neighbor nodes */
curr_tri = tri_head;
while (curr_tri) {
// the triangle norm (make it the unit-length negative norm!)
tri_norm = norm(cross(from(curr_tri->node[0]->loc,curr_tri->node[1]->loc),from(curr_tri->node[2]->loc,curr_tri->node[1]->loc)));
// tri_norm = cross(from(curr_tri->node[0]->loc,curr_tri->node[1]->loc),from(curr_tri->node[2]->loc,curr_tri->node[1]->loc));
// for each edge
for (j=0;j<3;j++) {
startn = curr_tri->node[j];
endn = curr_tri->node[(j+1)%3];
oneedge = from(startn->loc,endn->loc);
// oneedge = norm(from(startn->loc,endn->loc));
// force is coefficient times edge cross normal
force = vscale(0.5*coeff/num_cycles,cross(oneedge,tri_norm));
// half of the force goes on each end node
// fprintf(stderr," node x=%g",curr_node->loc.x); fflush(stderr);
startn->temp_loc.x += force.x;
startn->temp_loc.y += force.y;
startn->temp_loc.z += force.z;
endn->temp_loc.x += force.x;
endn->temp_loc.y += force.y;
endn->temp_loc.z += force.z;
// fprintf(stderr," to x=%g\n",curr_node->loc.x); fflush(stderr);
}
curr_tri = curr_tri->next_tri;
}
/* apply the new locations to the nodes */
curr_node = node_head;
while (curr_node) {
curr_node->loc.x = curr_node->temp_loc.x;
curr_node->loc.y = curr_node->temp_loc.y;
curr_node->loc.z = curr_node->temp_loc.z;
curr_node = curr_node->next_node;
}
}
if (num_cycles > 0) fprintf(stderr,"\n");
/* return some metric */
return(0);
}
/*
* Find the normal vectors of the nodes by averaging the normals
* of ALL connecting triangles.
*
* method 1 uses the mean of the normals of all adjacent elements
* method 2 uses the area-weighted normals of adj elems
* method 3 uses the angle-weighted normals of adj elems
*/
int compute_normals_2 (tri_pointer tri_head, int method) {
int index,j;
int num_norms = 0;
double weight;
VEC sum,tri_norm;
VEC e1,e2;
node_ptr curr_node;
tri_pointer test_tri;
fprintf(stderr,"Computing normal vectors.");
fflush(stderr);
// first, remove all old normals - this is dangerous and ugly
// smart pointers would be better
norm_ptr curr_norm = norm_head;
while (curr_norm) {
norm_ptr temp_norm = curr_norm->next_norm;
free(curr_norm);
curr_norm = temp_norm;
}
norm_head = NULL;
test_tri = tri_head;
while (test_tri) {
for (int i=0; i<3; i++) test_tri->norm[i] = NULL;
test_tri = test_tri->next_tri;
}
// now, compute new normals
NBIN normbin;
(void) prepare_norm_bin (&normbin);
curr_node = node_head;
while (curr_node) {
//fprintf(stderr,"\nnode %d\n",curr_node->index); fflush(stderr);
//fprintf(stderr," num_conn %d\n",curr_node->num_conn); fflush(stderr);
//for (j=0; j<curr_node->num_conn; j++) {
// fprintf(stderr," tri %d\n",curr_node->conn_tri[j]->index); fflush(stderr);
// for (i=0; i<3; i++) fprintf(stderr," node %d\n",curr_node->conn_tri[j]->node[i]->index); fflush(stderr);
//}
// loop over all connected triangles, find normal as average
sum.x = 0;
sum.y = 0;
sum.z = 0;
weight = 0.;
for (j=0; j<curr_node->num_conn; j++) {
test_tri = curr_node->conn_tri[j];
//fprintf(stderr," tri %d\n",test_tri->index); fflush(stderr);
//fprintf(stderr," nodes %d %d %d\n",test_tri->node[0]->index,test_tri->node[1]->index,test_tri->node[2]->index); fflush(stderr);
//for (i=0; i<3; i++) fprintf(stderr," %d %g %g %g\n",i,test_tri->node[i]->loc.x,test_tri->node[i]->loc.y,test_tri->node[i]->loc.z); fflush(stderr);
if (method == 2) {
// norm can use any nodes
e1 = from(test_tri->node[0]->loc,test_tri->node[1]->loc);
e2 = from(test_tri->node[0]->loc,test_tri->node[2]->loc);
tri_norm = cross(e1,e2);
// weight based on area of tri
weight = 0.5*length(tri_norm);
// must normalize *after* finding area
tri_norm = norm(tri_norm);
} else if (method == 3) {
// norm must use vectors from key node
index = curr_node->conn_tri_node[j];
e1 = from(test_tri->node[index]->loc,test_tri->node[(index+1)%3]->loc);
e2 = from(test_tri->node[index]->loc,test_tri->node[(index+2)%3]->loc);
tri_norm = cross(e1,e2);
tri_norm = norm(tri_norm);
// weight based on angle swept by tri from given node
weight = acos(dot(e1,e2)/sqrt(lengthsq(e1)*lengthsq(e2)));
//fprintf(stderr," %d wt %g e1,e2 %g %g norm %g %g %g\n",j,weight,lengthsq(e1),lengthsq(e2),tri_norm.x,tri_norm.y,tri_norm.z);
} else {
// all tris weighted evenly
weight = 1.;
// must normalize normal vector nonetheless
e1 = from(test_tri->node[0]->loc,test_tri->node[1]->loc);
e2 = from(test_tri->node[0]->loc,test_tri->node[2]->loc);
tri_norm = cross(e1,e2);
tri_norm = norm(tri_norm);
}
if (!isnan(tri_norm.x)) {
if (weight == 0.) weight = 1.e-6;
sum.x += tri_norm.x*weight;
sum.y += tri_norm.y*weight;
sum.z += tri_norm.z*weight;
}
}
sum = norm(sum);
if (isnan(sum.x) || fabs(sum.x+sum.y+sum.z) < 1.e-6) {
//fprintf(stderr,"node %d\n",curr_node->index);
//fprintf(stderr," weight %g\n",weight);
//fprintf(stderr," norm %g %g %g\n",sum.x,sum.y,sum.z);
sum.x = 0.;
sum.y = 0.;
sum.z = 1.;
}
// make a new normal object
norm_ptr new_norm = add_to_norms_list(&num_norms,&sum,&normbin);
// loop over all connected triangles, save normal as norm[]
for (j=0; j<curr_node->num_conn; j++) {
test_tri = curr_node->conn_tri[j];
index = curr_node->conn_tri_node[j];
test_tri->norm[index] = new_norm;
}
curr_node = curr_node->next_node;
}
fprintf(stderr,"\n");
return(0);
}
/*
* Find the normal vectors on each tri's nodes by averaging the normals
* of any connecting triangles that are adjacent and not pointing too
* sharply away.
*
* method 1 uses the mean of the normals of all adjacent elements
* method 2 uses the area-weighted normals of adj elems
* method 3 uses the angle-weighted normals of adj elems
*
* this method is a copy of compute_normals_2
*/
int compute_normals_3 (tri_pointer tri_head, int method, int use_sharp, double sharp_thresh) {
int index,i,j,icnt,newj;
double weight,thresh;
VEC sum,tri_norm,this_norm;
VEC e1,e2;
node_ptr curr_node;
tri_pointer curr_tri,test_tri,last_tri;
// before messing with anything, remove all old normals - this is dangerous and ugly
norm_ptr curr_norm = norm_head;
while (curr_norm) {
norm_ptr temp_norm = curr_norm->next_norm;
free(curr_norm);
curr_norm = temp_norm;
}
norm_head = NULL;
test_tri = tri_head;
while (test_tri) {
for (int i=0; i<3; i++) test_tri->norm[i] = NULL;
test_tri = test_tri->next_tri;
}
// ----------------------------------------------------------------
// first step, make sure all triangles have real area,
// and not "nan" for a normal!
fprintf(stderr,"Checking for degenerate tris.");
fflush(stderr);
icnt = 0;
last_tri = NULL;
curr_tri = tri_head;
while (curr_tri) {
// find the normal of this tri
e1 = from(curr_tri->node[0]->loc,curr_tri->node[1]->loc);
e2 = from(curr_tri->node[0]->loc,curr_tri->node[2]->loc);
this_norm = cross(e1,e2);
this_norm = norm(this_norm);
// if the normal for this tri is bad, then throw away the triangle!
// Whoah, that would affect connectivity! Shit.
// But we have to, or else this bad tri will mess up other tris!
if (this_norm.x < 2. && this_norm.x > -2) {
// if true, then this is a good tri! go on to the next.
last_tri = curr_tri;
curr_tri = curr_tri->next_tri;
} else {
// it's a bad tri, remove it CAREFULLY!
//fprintf(stderr,"\ntri %d IS BAD\n",curr_tri->index);
//fprintf(stderr," norm %g %g %g\n",this_norm.x,this_norm.y,this_norm.z);
//fprintf(stderr," nodes %d %d %d\n",curr_tri->node[0]->index,curr_tri->node[1]->index,curr_tri->node[2]->index);
// remove the tri from the connectivity list for each node
for (i=0; i<3; i++) {
curr_node = curr_tri->node[i];
//fprintf(stderr," node %d has %d connecting tris\n",i,curr_node->num_conn);
// loop through the list, compacting it as you go
newj = 0;
for (j=0; j<curr_node->num_conn; j++) {
if (curr_node->conn_tri[j] != curr_tri) {
curr_node->conn_tri[newj] = curr_node->conn_tri[j];
curr_node->conn_tri_node[newj] = curr_node->conn_tri_node[j];
newj++;
}
}
// change num_conn as well
curr_node->num_conn = newj;
//fprintf(stderr," now has %d\n",curr_node->num_conn);
}
// now, remove the triangle from the linked list!
test_tri = curr_tri;
curr_tri = curr_tri->next_tri;
free(test_tri);
last_tri->next_tri = curr_tri;
// keep last_tri unchanged
}
if (icnt/DOTPER == (icnt+DPMO)/DOTPER) fprintf(stderr,".");
icnt++;
}
fprintf(stderr,"\n");
// ----------------------------------------------------------------
fprintf(stderr,"Computing normal vectors.");
fflush(stderr);
// prepare node binning structure
NBIN normbin;
(void) prepare_norm_bin (&normbin);
int num_norms = 0;
// convert threshhold in degrees to a dot product threshhold
thresh = cos(sharp_thresh*M_PI/180.);
icnt = 0;
curr_tri = tri_head;
while (curr_tri) {
// find the normal of this tri
e1 = from(curr_tri->node[0]->loc,curr_tri->node[1]->loc);
e2 = from(curr_tri->node[0]->loc,curr_tri->node[2]->loc);
this_norm = cross(e1,e2);
this_norm = norm(this_norm);
// loop over each node in this tri
for (i=0; i<3; i++) {
curr_node = curr_tri->node[i];
//fprintf(stderr,"\nnode %d\n",curr_node->index);
//fprintf(stderr," norm %g %g %g\n",this_norm.x,this_norm.y,this_norm.z);
//fprintf(stderr," node %g %g %g\n",curr_node->loc.x,curr_node->loc.y,curr_node->loc.z);
//fprintf(stderr," num_conn %d\n",curr_node->num_conn); fflush(stderr);
//for (j=0; j<curr_node->num_conn; j++) {
// fprintf(stderr," tri %d\n",curr_node->conn_tri[j]->index); fflush(stderr);
// for (i=0; i<3; i++) fprintf(stderr," node %d\n",curr_node->conn_tri[j]->node[i]->index); fflush(stderr);
//}
// loop over all connected triangles, find normal as average
sum.x = 0;
sum.y = 0;
sum.z = 0;
weight = 0.;
for (j=0; j<curr_node->num_conn; j++) {
test_tri = curr_node->conn_tri[j];
if (method == 2) {
// norm can use any nodes
e1 = from(test_tri->node[0]->loc,test_tri->node[1]->loc);
e2 = from(test_tri->node[0]->loc,test_tri->node[2]->loc);
tri_norm = cross(e1,e2);
// weight based on area of tri
weight = 0.5*length(tri_norm);
// must normalize *after* finding area
tri_norm = norm(tri_norm);
} else if (method == 3) {
// norm must use vectors from key node
index = curr_node->conn_tri_node[j];
//fprintf(stderr," node %d\n",test_tri->node[index]->index);
//if (curr_node != test_tri->node[index]) fprintf(stderr," whoops");
e1 = from(test_tri->node[index]->loc,test_tri->node[(index+1)%3]->loc);
e2 = from(test_tri->node[index]->loc,test_tri->node[(index+2)%3]->loc);
tri_norm = cross(e1,e2);
tri_norm = norm(tri_norm);
// weight based on angle swept by tri from given node
weight = acos(dot(e1,e2)/sqrt(lengthsq(e1)*lengthsq(e2)));
//if (weight < 0. || weight > 3.15) {
//fprintf(stderr," %d wt %g norm %g %g %g\n",j,weight,tri_norm.x,tri_norm.y,tri_norm.z);
//}
} else {
// all tris weighted evenly
weight = 1.;
// must normalize normal vector nonetheless
e1 = from(test_tri->node[0]->loc,test_tri->node[1]->loc);
e2 = from(test_tri->node[0]->loc,test_tri->node[2]->loc);
tri_norm = cross(e1,e2);
tri_norm = norm(tri_norm);
}
if (use_sharp) {
// check vs threshhold
// is the normal of the test tri significantly different from curr_tri?
if (dot(this_norm,tri_norm) < thresh) weight = 0.;
//fprintf(stderr," dotp %g thresh %g\n",dot(this_norm,tri_norm),thresh);
}
sum.x += tri_norm.x*weight;
sum.y += tri_norm.y*weight;
sum.z += tri_norm.z*weight;
}
sum = norm(sum);
// check for inconclusive normals and assign triangle normal
if (!(sum.x < 2. && sum.x > -2)) {
fprintf(stderr," normal is bad! %g %g %g\n",sum.x,sum.y,sum.z);
sum.x = this_norm.x;
sum.y = this_norm.y;
sum.z = this_norm.z;
}
//if (!(sum.x < 2. && sum.x > -2)) {
// fprintf(stderr," normal is bad again! %g %g %g\n",sum.x,sum.y,sum.z);
// fprintf(stderr," %g %g %g\n",curr_tri->node[0]->loc.x,curr_tri->node[0]->loc.y,curr_tri->node[0]->loc.z);
// fprintf(stderr," %g %g %g\n",curr_tri->node[1]->loc.x,curr_tri->node[1]->loc.y,curr_tri->node[1]->loc.z);
// fprintf(stderr," %g %g %g\n",curr_tri->node[2]->loc.x,curr_tri->node[2]->loc.y,curr_tri->node[2]->loc.z);
//}
// is this normal very different from the tri's normal?
//if (dot(this_norm,sum) < thresh) {
// fprintf(stderr," tri norm %g %g %g\n",this_norm.x,this_norm.y,this_norm.z);
// fprintf(stderr," node norm %g %g %g\n",sum.x,sum.y,sum.z);
// fprintf(stderr," dot prod %g\n",dot(this_norm,sum));
//}
//fprintf(stderr," final norm %g %g %g\n",sum.x,sum.y,sum.z);
// make a new normal object
norm_ptr new_norm = add_to_norms_list(&num_norms,&sum,&normbin);
// save normal
curr_tri->norm[i] = new_norm;
}
curr_tri = curr_tri->next_tri;
if (icnt/DOTPER == (icnt+DPMO)/DOTPER) fprintf(stderr,".");
icnt++;
}
fprintf(stderr,"\n");
return(0);
}
/*
* Grow a surface a fixed distance along each nodes' normal
*/
int grow_surface_along_normal(tri_pointer tri_head,double grow_distance) {
int corner;
node_ptr curr_node;
tri_pointer test_tri;
fprintf(stderr,"Growing boundary...");
fflush(stderr);
curr_node = node_head;
while (curr_node) {
// find the normal
test_tri = curr_node->conn_tri[0];
corner = curr_node->conn_tri_node[0];
curr_node->loc.x += grow_distance*test_tri->norm[corner]->norm.x;
curr_node->loc.y += grow_distance*test_tri->norm[corner]->norm.y;
curr_node->loc.z += grow_distance*test_tri->norm[corner]->norm.z;
curr_node = curr_node->next_node;
}
fprintf(stderr,"\n");
return(0);
}