Debian Codegraph @

debian/external_deps/fetk/gamer/src/biom/SurfSmooth.C

raw
1 /*
2 * ***************************************************************************
3 * GAMER = < Geometry-preserving Adaptive MeshER >
4 * Copyright (C) 1994-- Michael Holst and Zeyun Yu
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * ***************************************************************************
20 */
21
22
23 /* ***************************************************************************
24 * File: SurfSmooth.C
25 *
26 * Author: Zeyun Yu ([email protected])
27 *
28 * Purpose: Smooth and coarsen surface triangular meshes
29 * ****************************************************************************
30 */
31
32 #include <gamer/biom.h>
33 #include "gamercf.h"
34
35 FLTVECT GetPositionSurfaceOnly(float, float, float,int, int, int, SurfaceMesh *);
36 float GetAngleSurfaceOnly(SurfaceMesh *, int, int, int);
37 void MoveVerticesSurfaceOnly(SurfaceMesh *, int);
38 FLTVECT GetNormals(SurfaceMesh *, int);
39 EIGENVECT GetEigenVector(SurfaceMesh *, int, FLTVECT *,float *);
40 void PolygonSubdivision(SurfaceMesh *, NPNT3 *, int *,int *);
41 char CheckFlipAction(SurfaceMesh *, int, int, int, int);
42 void EdgeFlipping(SurfaceMesh *, int);
43 float GetDotProduct(SurfaceMesh *,int, int, int);
44 FLTVECT Rotate(float, float, float,float, float, float);
45 FLTVECT GetCrossProduct(SurfaceMesh *,int, int, int);
46 void NormalSmooth(SurfaceMesh *, int);
47 FLTVECT GetNormals(SurfaceMesh *, int,int,int);
48
49 /*
50 * ***************************************************************************
51 * Routine: GenerateHistogram
52 *
53 * Author: Zeyun Yu ([email protected])
54 *
55 * Purpose: Generate the angle distribution (0 - 180 degrees)
56 * ***************************************************************************
57 */
58 void GenerateHistogram(SurfaceMesh *surfmesh)
59 {
60 int n,m;
61 int a,b,c;
62 float angle;
63 int histogram[18];
64
65 for (m = 0; m < 18; m++)
66 histogram[m] = 0;
67
68 for (n = 0; n < surfmesh->nf; n++) {
69 a = surfmesh->face[n].a;
70 b = surfmesh->face[n].b;
71 c = surfmesh->face[n].c;
72
73 angle = GetAngleSurfaceOnly(surfmesh,a,b,c);
74 for (m=0; m<18; m++) {
75 if (angle >= m*10 && angle < m*10+10)
76 histogram[m] += 1;
77 }
78
79 angle = GetAngleSurfaceOnly(surfmesh,b,a,c);
80 for (m=0; m<18; m++) {
81 if (angle >= m*10 && angle < m*10+10)
82 histogram[m] += 1;
83 }
84
85 angle = GetAngleSurfaceOnly(surfmesh,c,a,b);
86 for (m=0; m<18; m++) {
87 if (angle >= m*10 && angle < m*10+10)
88 histogram[m] += 1;
89 }
90
91 }
92
93 for (m = 0; m < 18; m++) {
94 printf("%f ",100.0*(float)histogram[m]/((float)surfmesh->nf*3.0));
95 }
96 printf("\n\n");
97 }
98
99 /*
100 * ***************************************************************************
101 * Routine: SurfaceMesh_smooth
102 *
103 * Author: Zeyun Yu ([email protected])
104 *
105 * Purpose: Function for surface smoothing
106 * ***************************************************************************
107 */
108 bool SurfaceMesh_smooth(SurfaceMesh *surfmesh,
109 unsigned int max_min_angle,
110 unsigned int min_max_angle,
111 unsigned int max_iter,
112 bool flip_edges)
113 {
114 float min_angle, max_angle;
115 int num_small, num_large;
116 unsigned int i, n;
117 bool smoothed = false;
118
119 // Check if neighborlist is created
120 if (surfmesh->neighbor_list == NULL){
121 SurfaceMesh_createNeighborlist(surfmesh);
122 }
123 i = 0;
124
125 // Print the initial quality only when doing 1 or more iterations
126 if (max_iter > 1) {
127 SurfaceMesh_getMinMaxAngles(surfmesh, &min_angle, &max_angle, &num_small,
128 &num_large, max_min_angle, min_max_angle);
129
130 printf("%2d: min_angle: %f - max_angle: %f - "
131 "smaller-than-%d: %d - larger-than-%d: %d\n",
132 i, min_angle, max_angle, max_min_angle, num_small,
133 min_max_angle, num_large);
134
135 }
136
137 while (!smoothed && i < max_iter)
138 {
139
140 i++;
141
142 // Smooth all vertices
143 if (flip_edges){
144 for (n = 0; n < surfmesh->nv; n++){
145 MoveVerticesSurfaceOnly(surfmesh, n);
146 EdgeFlipping(surfmesh, n);
147 }
148 }
149 else{
150 for (n = 0; n < surfmesh->nv; n++)
151 MoveVerticesSurfaceOnly(surfmesh, n);
152 }
153
154 // Calculate and print quality after surface smooth
155 SurfaceMesh_getMinMaxAngles(surfmesh, &min_angle, &max_angle, &num_small,
156 &num_large, max_min_angle, min_max_angle);
157
158 // Print the iteration number only when doing 1 or more iterations
159 if (max_iter != 1)
160 printf("%2d: min_angle: %f - max_angle: %f - "
161 "smaller-than-%d: %d - larger-than-%d: %d\n",
162 i, min_angle, max_angle, max_min_angle, num_small,
163 min_max_angle, num_large);
164 else
165 printf(" min_angle: %f - max_angle: %f - "
166 "smaller-than-%d: %d - larger-than-%d: %d\n",
167 min_angle, max_angle, max_min_angle, num_small,
168 min_max_angle, num_large);
169
170 // Check if the mesh is smoothed
171 smoothed = min_angle > max_min_angle && max_angle < min_max_angle;
172 }
173
174 return smoothed;
175 }
176
177 /*
178 * ***************************************************************************
179 * Routine: SurfaceMesh_normalSmooth
180 *
181 * Author: Zeyun Yu ([email protected])
182 *
183 * Purpose: Function for surface smoothing using normal smoothing
184 * ***************************************************************************
185 */
186 void SurfaceMesh_normalSmooth(SurfaceMesh *surfmesh)
187 {
188 unsigned int n;
189 float min_angle, max_angle;
190 int num_small, num_large;
191
192 // Check if neighborlist is created
193 if (!surfmesh->neighbor_list)
194 SurfaceMesh_createNeighborlist(surfmesh);
195
196 // Normal smooth all vertices
197 for (n = 0; n < surfmesh->nv; n++)
198 NormalSmooth(surfmesh, n);
199
200 SurfaceMesh_getMinMaxAngles(surfmesh, &min_angle, &max_angle,
201 &num_small, &num_large, 15, 150);
202 printf(" min_angle: %f - max_angle: %f - smaller-than-15: %d "
203 "- larger-than-150: %d\n",
204 min_angle, max_angle, num_small, num_large);
205
206 }
207
208
209 /*
210 * ***************************************************************************
211 * Routine: SurfaceMesh_assignActiveSites
212 *
213 * Author: Zeyun Yu ([email protected]), Johan Hake ([email protected])
214 *
215 * Purpose: Assign markers to the vertices from a list of user-defined spheres.
216 * Note that vertices might be marked twice. Such vertex will then get
217 * the marker from the last sphere.
218 * ***************************************************************************
219 */
220 void SurfaceMesh_assignActiveSites(SurfaceMesh* surfmesh, ATOM* sphere_list,
221 unsigned int num_spheres,
222 unsigned int* sphere_markers)
223 {
224 int i, n;
225 float x,y,z;
226 float center_x,center_y,center_z;
227 float dist, radius;
228
229 // Reset markers to zero
230 SurfaceMesh_resetVertexMarkers(surfmesh);
231
232 // Walk through all spheres and mark the vertices
233 for (i = 0; i < num_spheres; i++) {
234 center_x = sphere_list[i].x;
235 center_y = sphere_list[i].y;
236 center_z = sphere_list[i].z;
237 radius = sphere_list[i].radius;
238
239 for (n = 0; n < surfmesh->nv; n++) {
240 x = surfmesh->vertex[n].x;
241 y = surfmesh->vertex[n].y;
242 z = surfmesh->vertex[n].z;
243 dist = sqrt((x-center_x)*(x-center_x)+
244 (y-center_y)*(y-center_y)+
245 (z-center_z)*(z-center_z));
246
247 // If the vertex n, is within the sphere mark it
248 if (dist < radius){
249 surfmesh->vertex_markers[n] = sphere_markers[i];
250 //printf("vertex: %d marked with %d\n", n, sphere_markers[i]);
251 }
252 }
253 }
254 }
255
256 /*
257 * ***************************************************************************
258 * Routine: SurfaceMesh_coarse
259 *
260 * Author: Zeyun Yu ([email protected])
261 *
262 * Purpose: Coarsen a surface mesh
263 * ***************************************************************************
264 */
265 char SurfaceMesh_coarse(SurfaceMesh* surfmesh,
266 float coarse_rate,
267 float flatness_rate, float denseness_weight,
268 float max_normal_angle)
269 {
270 int m,n,a0,b0;
271 int a,b,c;
272 float x,y,z;
273 NPNT3 *first_ngr, *second_ngr, *tmp_ngr, *tmp_ngr1;
274 int number, neighbor_number, num;
275 float nx,ny,nz;
276 EIGENVECT eigen_vect;
277 FLTVECT eigen_value;
278 float average_len, max_len;
279 float ratio1 = 1.0, ratio2 = 1.0;
280 int face_available_list[64],face_available_index;
281 int neighbor_tmp_list[64];
282 float weight,angle;
283
284 int start_index, *vertex_index, *face_index;
285
286 FLTVECT pos_vect;
287 float w1,w2,w3;
288 char delete_flag;
289 float max_angle;
290
291 char stop;
292 int vertex_num;
293 bool delete_vertex;
294 int* vertex_markers = surfmesh->vertex_markers;
295
296 // Check if neighborlist is created
297 if (!surfmesh->neighbor_list)
298 SurfaceMesh_createNeighborlist(surfmesh);
299
300 NPNT3** neighbor_list = surfmesh->neighbor_list;
301
302 vertex_index = (int *)malloc(sizeof(int)*surfmesh->nv);
303 face_index = (int *)malloc(sizeof(int)*surfmesh->nf);
304
305 vertex_num = surfmesh->nv;
306
307 stop = 0;
308 // If using sparseness weight, calculate the average segment length of the mesh
309 if (denseness_weight > 0.0){
310 average_len = 0;
311 for (n = 0; n < surfmesh->nf; n++) {
312 a = surfmesh->face[n].a;
313 b = surfmesh->face[n].b;
314 c = surfmesh->face[n].c;
315
316 nx = sqrt((surfmesh->vertex[a].x-surfmesh->vertex[b].x)*(surfmesh->vertex[a].x-surfmesh->vertex[b].x)+
317 (surfmesh->vertex[a].y-surfmesh->vertex[b].y)*(surfmesh->vertex[a].y-surfmesh->vertex[b].y)+
318 (surfmesh->vertex[a].z-surfmesh->vertex[b].z)*(surfmesh->vertex[a].z-surfmesh->vertex[b].z));
319 ny = sqrt((surfmesh->vertex[a].x-surfmesh->vertex[c].x)*(surfmesh->vertex[a].x-surfmesh->vertex[c].x)+
320 (surfmesh->vertex[a].y-surfmesh->vertex[c].y)*(surfmesh->vertex[a].y-surfmesh->vertex[c].y)+
321 (surfmesh->vertex[a].z-surfmesh->vertex[c].z)*(surfmesh->vertex[a].z-surfmesh->vertex[c].z));
322 nz = sqrt((surfmesh->vertex[c].x-surfmesh->vertex[b].x)*(surfmesh->vertex[c].x-surfmesh->vertex[b].x)+
323 (surfmesh->vertex[c].y-surfmesh->vertex[b].y)*(surfmesh->vertex[c].y-surfmesh->vertex[b].y)+
324 (surfmesh->vertex[c].z-surfmesh->vertex[b].z)*(surfmesh->vertex[c].z-surfmesh->vertex[b].z));
325 average_len += (nx+ny+nz)/3.0f;
326 }
327 if (surfmesh->nf == 0) {
328 printf("zero degree on a vertex ....\n");
329 exit(0);
330 }
331 else
332 surfmesh->avglen = average_len/(float)(surfmesh->nf);
333 }
334
335
336 // The main loop over all vertices
337 for (n = 0; n < surfmesh->nv; n++) {
338 if (surfmesh->nvm > 0 && vertex_markers[n] > 0){
339 //printf("Do not remove vertex %d\n", n);
340 continue;
341 }
342
343 if (((n+1) % 888) == 0 || (n+1) == surfmesh->nv) {
344 printf("%2.2f%% done (%08d) \r", 100.0*(n+1)/(float)surfmesh->nv, n+1);
345 fflush(stdout);
346 }
347
348 // Check if the vertex has enough neigborgs to be deleted
349 delete_flag = 1;
350 first_ngr = neighbor_list[n];
351 while (first_ngr != NULL) {
352 a = first_ngr->a;
353 number = 0;
354 num = 0;
355 second_ngr = neighbor_list[a];
356 while (second_ngr != NULL) {
357 b = second_ngr->a;
358 tmp_ngr = neighbor_list[n];
359 while (tmp_ngr != NULL) {
360 if (tmp_ngr->a == b)
361 num++;
362 tmp_ngr = tmp_ngr->next;
363 }
364 number++;
365 second_ngr = second_ngr->next;
366 }
367
368 if (number <= 3 || num > 2)
369 delete_flag = 0;
370 first_ngr = first_ngr->next;
371 }
372
373 if (delete_flag) {
374 x = surfmesh->vertex[n].x;
375 y = surfmesh->vertex[n].y;
376 z = surfmesh->vertex[n].z;
377
378 max_len = -1;
379 first_ngr = neighbor_list[n];
380
381 // If using sparseness as a criteria for coarsening
382 // calculate the maximal segment length
383 if (denseness_weight > 0.0){
384 while (first_ngr != NULL) {
385 a = first_ngr->a;
386 b = first_ngr->b;
387
388 nx = sqrt((x-surfmesh->vertex[a].x)*(x-surfmesh->vertex[a].x)+
389 (y-surfmesh->vertex[a].y)*(y-surfmesh->vertex[a].y)+
390 (z-surfmesh->vertex[a].z)*(z-surfmesh->vertex[a].z));
391 ny = sqrt((x-surfmesh->vertex[b].x)*(x-surfmesh->vertex[b].x)+
392 (y-surfmesh->vertex[b].y)*(y-surfmesh->vertex[b].y)+
393 (z-surfmesh->vertex[b].z)*(z-surfmesh->vertex[b].z));
394 if (nx > max_len)
395 max_len = nx;
396 if (ny > max_len)
397 max_len = ny;
398
399 first_ngr = first_ngr->next;
400 }
401 // Max segment length over the average segment length of the mesh
402 ratio2 = max_len/surfmesh->avglen;
403 ratio2 = pow(ratio2, denseness_weight);
404 }
405
406 // If using curvatory as a coarsening criteria
407 // calculate the local structure tensor
408 if (flatness_rate > 0.0) {
409 eigen_vect = GetEigenVector(surfmesh, n, &eigen_value, &max_angle);
410
411 if ((eigen_vect.x1==0 && eigen_vect.y1==0 && eigen_vect.z1==0) ||
412 (eigen_vect.x2==0 && eigen_vect.y2==0 && eigen_vect.z2==0) ||
413 (eigen_vect.x3==0 && eigen_vect.y3==0 && eigen_vect.z3==0))
414 ratio1 = 999999.0f;
415 else {
416 if (eigen_value.x == 0) {
417 printf("max eigen_value is zero.... \n");
418 exit(0);
419 }
420 else {
421 ratio1 = fabs((eigen_value.y)/(eigen_value.x));
422 ratio1 = pow(ratio1, flatness_rate);
423 //ratio1 = (1.0-max_angle)*fabs((eigen_value.y)/(eigen_value.x));
424 }
425 }
426 }
427
428 // Compare the two coarseness criterias against the given coarse_rate
429 delete_vertex = ratio1*ratio2 < coarse_rate;
430
431 // Use maximal angle between vertex normal as a complementary coarse criteria
432 if (max_normal_angle > 0)
433 delete_vertex = delete_vertex && max_angle > max_normal_angle;
434
435 // Deleting a vertex and retrianglulate the hole
436 if (delete_vertex) {
437 vertex_num--;
438 /* delete vertex n */
439 surfmesh->vertex[n].x = -99999;
440 surfmesh->vertex[n].y = -99999;
441 surfmesh->vertex[n].z = -99999;
442
443 neighbor_number = 0;
444 first_ngr = neighbor_list[n];
445 while (first_ngr != NULL) {
446 a = first_ngr->a;
447 c = first_ngr->c;
448 face_available_list[neighbor_number] = c;
449 neighbor_tmp_list[neighbor_number] = a;
450 neighbor_number++;
451
452 /* delete faces associated with vertex n */
453 surfmesh->face[c].a = -1;
454 surfmesh->face[c].b = -1;
455 surfmesh->face[c].c = -1;
456
457 /* delete neighbors associated with vertex n */
458 second_ngr = neighbor_list[a];
459 tmp_ngr = second_ngr;
460 while (second_ngr != NULL) {
461 if (second_ngr->a == n || second_ngr->b == n) {
462 if (second_ngr == neighbor_list[a]) {
463 neighbor_list[a] = second_ngr->next;
464 free(second_ngr);
465 second_ngr = neighbor_list[a];
466 tmp_ngr = second_ngr;
467 }
468 else {
469 tmp_ngr->next = second_ngr->next;
470 free(second_ngr);
471 second_ngr = tmp_ngr->next;
472 }
473 }
474 else {
475 if (second_ngr == neighbor_list[a]) {
476 second_ngr = second_ngr->next;
477 }
478 else {
479 tmp_ngr = second_ngr;
480 second_ngr = second_ngr->next;
481 }
482 }
483 }
484
485 number = 0;
486 second_ngr = neighbor_list[a];
487 while (second_ngr != NULL) {
488 number++;
489 second_ngr = second_ngr->next;
490 }
491 first_ngr->b = number;
492
493 first_ngr = first_ngr->next;
494 }
495
496 first_ngr = neighbor_list[n];
497 while (first_ngr->next != NULL)
498 first_ngr = first_ngr->next;
499 first_ngr->next = neighbor_list[n];
500
501 face_available_index = 0;
502 PolygonSubdivision(surfmesh, neighbor_list[n], face_available_list, &face_available_index);
503
504 /* order the neighbors */
505 for (m = 0; m < neighbor_number; m++) {
506 first_ngr = neighbor_list[neighbor_tmp_list[m]];
507 c = first_ngr->a;
508 while (first_ngr != NULL) {
509 a = first_ngr->a;
510 b = first_ngr->b;
511
512 second_ngr = first_ngr->next;
513 while (second_ngr != NULL) {
514 a0 = second_ngr->a;
515 b0 = second_ngr->b;
516
517 // Assume counter clockwise orientation
518 if (a0==b && b0!=a) {
519 tmp_ngr = first_ngr;
520 while (tmp_ngr != NULL) {
521 if (tmp_ngr->next == second_ngr) {
522 tmp_ngr->next = second_ngr->next;
523 break;
524 }
525 tmp_ngr = tmp_ngr->next;
526 }
527 tmp_ngr = first_ngr->next;
528 first_ngr->next = second_ngr;
529 second_ngr->next = tmp_ngr;
530 break;
531 }
532
533 second_ngr = second_ngr->next;
534 }
535 if (first_ngr->next == NULL) {
536 if (first_ngr->b != c) {
537 printf("some polygons are not closed: %d \n",n);
538 // exit(0);
539 }
540 }
541
542 first_ngr = first_ngr->next;
543 }
544 }
545
546 /* Smooth the neighbors */
547 for (m = 0; m < neighbor_number; m++) {
548 num = neighbor_tmp_list[m];
549 x = surfmesh->vertex[num].x;
550 y = surfmesh->vertex[num].y;
551 z = surfmesh->vertex[num].z;
552 nx = 0;
553 ny = 0;
554 nz = 0;
555
556 weight = 0;
557 first_ngr = neighbor_list[num];
558 while (first_ngr != NULL) {
559 a = first_ngr->a;
560 b = first_ngr->b;
561 second_ngr = first_ngr->next;
562 if (second_ngr == NULL)
563 second_ngr = neighbor_list[num];
564 c = second_ngr->b;
565 pos_vect = GetPositionSurfaceOnly(x,y,z,b,a,c,surfmesh);
566 angle = GetDotProduct(surfmesh,b,a,c);
567 angle += 1.0;
568 nx += angle*pos_vect.x;
569 ny += angle*pos_vect.y;
570 nz += angle*pos_vect.z;
571
572 weight += angle;
573 first_ngr = first_ngr->next;
574 }
575
576 if (weight > 0) {
577 nx /= weight;
578 ny /= weight;
579 nz /= weight;
580
581 eigen_vect = GetEigenVector(surfmesh, num, &eigen_value, &max_angle);
582 if ((eigen_vect.x1==0 && eigen_vect.y1==0 && eigen_vect.z1==0) ||
583 (eigen_vect.x2==0 && eigen_vect.y2==0 && eigen_vect.z2==0) ||
584 (eigen_vect.x3==0 && eigen_vect.y3==0 && eigen_vect.z3==0)) {
585 surfmesh->vertex[num].x = nx;
586 surfmesh->vertex[num].y = ny;
587 surfmesh->vertex[num].z = nz;
588 }
589 else {
590 nx -= x;
591 ny -= y;
592 nz -= z;
593 w1 = (nx*eigen_vect.x1+ny*eigen_vect.y1+nz*eigen_vect.z1)/(1.0+eigen_value.x);
594 w2 = (nx*eigen_vect.x2+ny*eigen_vect.y2+nz*eigen_vect.z2)/(1.0+eigen_value.y);
595 w3 = (nx*eigen_vect.x3+ny*eigen_vect.y3+nz*eigen_vect.z3)/(1.0+eigen_value.z);
596 surfmesh->vertex[num].x = w1*eigen_vect.x1+w2*eigen_vect.x2+w3*eigen_vect.x3 + x;
597 surfmesh->vertex[num].y = w1*eigen_vect.y1+w2*eigen_vect.y2+w3*eigen_vect.y3 + y;
598 surfmesh->vertex[num].z = w1*eigen_vect.z1+w2*eigen_vect.z2+w3*eigen_vect.z3 + z;
599 }
600 }
601 }
602 }
603 }
604 /*
605 if (vertex_num < MeshSizeUpperLimit) {
606 stop = 1;
607 break;
608 }
609 */
610 }
611
612 /* Clean the lists of nodes and faces */
613 start_index = 0;
614 for (n = 0; n < surfmesh->nv; n++) {
615 if (surfmesh->vertex[n].x != -99999 &&
616 surfmesh->vertex[n].y != -99999 &&
617 surfmesh->vertex[n].z != -99999) {
618 if (start_index != n) {
619 surfmesh->vertex[start_index].x = surfmesh->vertex[n].x;
620 surfmesh->vertex[start_index].y = surfmesh->vertex[n].y;
621 surfmesh->vertex[start_index].z = surfmesh->vertex[n].z;
622 neighbor_list[start_index] = neighbor_list[n];
623 }
624 if (surfmesh->nvm > 0)
625 vertex_markers[start_index] = vertex_markers[n];
626 vertex_index[n] = start_index;
627 start_index++;
628 }
629 else {
630 vertex_index[n] = -1;
631 }
632 }
633
634 surfmesh->nv = start_index;
635
636 start_index = 0;
637 for (n = 0; n < surfmesh->nf; n++) {
638 a = surfmesh->face[n].a;
639 b = surfmesh->face[n].b;
640 c = surfmesh->face[n].c;
641 if (a >= 0 && b >= 0 && c >= 0 &&
642 vertex_index[a] >= 0 && vertex_index[b] >= 0 && vertex_index[c] >= 0) {
643 surfmesh->face[start_index].a = vertex_index[a];
644 surfmesh->face[start_index].b = vertex_index[b];
645 surfmesh->face[start_index].c = vertex_index[c];
646 face_index[n] = start_index;
647 start_index++;
648 }
649 else {
650 face_index[n] = -1;
651 }
652 }
653 surfmesh->nf = start_index;
654
655 for (n = 0; n < surfmesh->nv; n++) {
656 first_ngr = neighbor_list[n];
657 while (first_ngr != NULL) {
658 a = first_ngr->a;
659 b = first_ngr->b;
660 c = first_ngr->c;
661 first_ngr->a = vertex_index[a];
662 first_ngr->b = vertex_index[b];
663 first_ngr->c = face_index[c];
664
665 first_ngr = first_ngr->next;
666 }
667 }
668
669 free(vertex_index);
670 free(face_index);
671 return(stop);
672 }
673
674 /*
675 * ***************************************************************************
676 * Routine: PolygonSubdivision
677 *
678 * Author: Zeyun Yu ([email protected])
679 *
680 * Purpose: Recursively re-triangulate the "empty" polygon
681 * ***************************************************************************
682 */
683 void PolygonSubdivision(SurfaceMesh *surfmesh,
684 NPNT3 *start_ngr, int *face_available_list,
685 int *face_available_index)
686 {
687 NPNT3 **neighbor_list = surfmesh->neighbor_list;
688 NPNT3 *first_ngr, *second_ngr;
689 NPNT3 *tmp_ngr, *first_copy_ngr,*second_copy_ngr;
690 int min_num,degree;
691 int face_index,number;
692 int a,b,c;
693
694
695 number = 1;
696 tmp_ngr = start_ngr;
697 while (tmp_ngr->next != start_ngr) {
698 number++;
699 tmp_ngr = tmp_ngr->next;
700 }
701
702 if (number < 3) {
703 printf("error: number of nodes less than 3 \n");
704 exit(0);
705 }
706 if (number == 3) {
707 a = start_ngr->a;
708 tmp_ngr = start_ngr->next;
709 free(start_ngr);
710 start_ngr = tmp_ngr;
711
712 b = start_ngr->a;
713 tmp_ngr = start_ngr->next;
714 free(start_ngr);
715 start_ngr = tmp_ngr;
716
717 c = start_ngr->a;
718 tmp_ngr = start_ngr->next;
719 free(start_ngr);
720 start_ngr = tmp_ngr;
721
722 face_index = face_available_list[*face_available_index];
723 surfmesh->face[face_index].a = a;
724 surfmesh->face[face_index].b = b;
725 surfmesh->face[face_index].c = c;
726 *face_available_index += 1;
727
728 first_ngr = (NPNT3 *)malloc(sizeof(NPNT3));
729 first_ngr->a = b;
730 first_ngr->b = c;
731 first_ngr->c = face_index;
732 first_ngr->next = neighbor_list[a];
733 neighbor_list[a] = first_ngr;
734
735 first_ngr = (NPNT3 *)malloc(sizeof(NPNT3));
736 first_ngr->a = c;
737 first_ngr->b = a;
738 first_ngr->c = face_index;
739 first_ngr->next = neighbor_list[b];
740 neighbor_list[b] = first_ngr;
741
742 first_ngr = (NPNT3 *)malloc(sizeof(NPNT3));
743 first_ngr->a = a;
744 first_ngr->b = b;
745 first_ngr->c = face_index;
746 first_ngr->next = neighbor_list[c];
747 neighbor_list[c] = first_ngr;
748
749 }
750 else {
751 tmp_ngr = start_ngr;
752 min_num = tmp_ngr->b;
753 first_ngr = tmp_ngr;
754 tmp_ngr = tmp_ngr->next;
755 while (tmp_ngr != start_ngr) {
756 degree = tmp_ngr->b;
757 if (degree < min_num) {
758 min_num = degree;
759 first_ngr = tmp_ngr;
760 }
761 tmp_ngr = tmp_ngr->next;
762 }
763
764 min_num = 99999;
765 tmp_ngr = start_ngr;
766 if (tmp_ngr != first_ngr &&
767 tmp_ngr != first_ngr->next &&
768 tmp_ngr->next != first_ngr) {
769 min_num = tmp_ngr->b;
770 second_ngr = tmp_ngr;
771 }
772 tmp_ngr = tmp_ngr->next;
773 while (tmp_ngr != start_ngr) {
774 degree = tmp_ngr->b;
775 if (tmp_ngr != first_ngr &&
776 tmp_ngr != first_ngr->next &&
777 tmp_ngr->next != first_ngr &&
778 degree < min_num) {
779 min_num = degree;
780 second_ngr = tmp_ngr;
781 }
782 tmp_ngr = tmp_ngr->next;
783 }
784
785 first_ngr->b += 1;
786 second_ngr->b += 1;
787 first_copy_ngr = (NPNT3 *)malloc(sizeof(NPNT3));
788 first_copy_ngr->a = first_ngr->a;
789 first_copy_ngr->b = first_ngr->b;
790 second_copy_ngr = (NPNT3 *)malloc(sizeof(NPNT3));
791 second_copy_ngr->a = second_ngr->a;
792 second_copy_ngr->b = second_ngr->b;
793 tmp_ngr = first_ngr;
794 while (tmp_ngr->next != first_ngr)
795 tmp_ngr = tmp_ngr->next;
796 tmp_ngr->next = first_copy_ngr;
797 first_copy_ngr->next = second_copy_ngr;
798 second_copy_ngr->next = second_ngr->next;
799 second_ngr->next = first_ngr;
800
801 PolygonSubdivision(surfmesh, first_ngr, face_available_list,face_available_index);
802 PolygonSubdivision(surfmesh, first_copy_ngr, face_available_list,face_available_index);
803 }
804
805 return;
806 }
807
808
809
810
811 /*
812 * ***************************************************************************
813 * Routine: MoveVerticesSurfaceOnly
814 *
815 * Author: Zeyun Yu ([email protected])
816 *
817 * Purpose: Smooth the surface mesh by moving each of the vertices
818 * using a combination of angle-based method
819 * and local structure tensor
820 * ***************************************************************************
821 */
822 void MoveVerticesSurfaceOnly(SurfaceMesh *surfmesh, int n)
823 {
824 int a,b,c;
825 float x,y,z;
826 FLTVECT pos_vect;
827 NPNT3 **neighbor_list = surfmesh->neighbor_list;
828 NPNT3 *first_ngr, *second_ngr;
829 float weight,angle;
830 float nx,ny,nz;
831 EIGENVECT eigen_vect;
832 FLTVECT eigen_value;
833 float w1,w2,w3;
834 float max_angle;
835
836
837 x = surfmesh->vertex[n].x;
838 y = surfmesh->vertex[n].y;
839 z = surfmesh->vertex[n].z;
840
841 nx = 0;
842 ny = 0;
843 nz = 0;
844
845 weight = 0;
846 first_ngr = neighbor_list[n];
847 while (first_ngr != NULL) {
848 a = first_ngr->a;
849 b = first_ngr->b;
850 second_ngr = first_ngr->next;
851 if (second_ngr == NULL)
852 second_ngr = neighbor_list[n];
853 c = second_ngr->b;
854 pos_vect = GetPositionSurfaceOnly(x,y,z,b,a,c,surfmesh);
855 angle = GetDotProduct(surfmesh,b,a,c);
856 angle += 1.0;
857 nx += angle*pos_vect.x;
858 ny += angle*pos_vect.y;
859 nz += angle*pos_vect.z;
860
861 weight += angle;
862 first_ngr = first_ngr->next;
863 }
864
865 if (weight > 0) {
866 nx /= weight;
867 ny /= weight;
868 nz /= weight;
869
870 eigen_vect = GetEigenVector(surfmesh, n, &eigen_value, &max_angle);
871 if ((eigen_vect.x1==0 && eigen_vect.y1==0 && eigen_vect.z1==0) ||
872 (eigen_vect.x2==0 && eigen_vect.y2==0 && eigen_vect.z2==0) ||
873 (eigen_vect.x3==0 && eigen_vect.y3==0 && eigen_vect.z3==0)) {
874 //printf("old point (%0.2f, %0.2f, %0.2f), new point (%0.2f, %0.2f, %0.2f)\n",
875 // surfmesh->vertex[n].x, surfmesh->vertex[n].y, surfmesh->vertex[n].z,
876 // nx, ny, nz);
877 surfmesh->vertex[n].x = nx;
878 surfmesh->vertex[n].y = ny;
879 surfmesh->vertex[n].z = nz;
880
881 }
882 else {
883 nx -= x;
884 ny -= y;
885 nz -= z;
886 w1 = (nx*eigen_vect.x1+ny*eigen_vect.y1+nz*eigen_vect.z1)/(1.0+eigen_value.x);
887 w2 = (nx*eigen_vect.x2+ny*eigen_vect.y2+nz*eigen_vect.z2)/(1.0+eigen_value.y);
888 w3 = (nx*eigen_vect.x3+ny*eigen_vect.y3+nz*eigen_vect.z3)/(1.0+eigen_value.z);
889 nx = w1*eigen_vect.x1+w2*eigen_vect.x2+w3*eigen_vect.x3 + x;
890 ny = w1*eigen_vect.y1+w2*eigen_vect.y2+w3*eigen_vect.y3 + y;
891 nz = w1*eigen_vect.z1+w2*eigen_vect.z2+w3*eigen_vect.z3 + z;
892
893 //printf("old point (%0.2f, %0.2f, %0.2f), new point (%0.2f, %0.2f, %0.2f)\n",
894 // surfmesh->vertex[n].x, surfmesh->vertex[n].y, surfmesh->vertex[n].z,
895 // nx, ny, nz);
896
897 surfmesh->vertex[n].x = nx;
898 surfmesh->vertex[n].y = ny;
899 surfmesh->vertex[n].z = nz;
900 }
901 }
902 }
903
904
905
906
907 /*
908 * ***************************************************************************
909 * Routine: GetPositionSurfaceOnly
910 *
911 * Author: Zeyun Yu ([email protected])
912 *
913 * Purpose: Move each of the vertices based on the angle-based method
914 * ***************************************************************************
915 */
916 FLTVECT GetPositionSurfaceOnly(float x, float y, float z, int a, int b, int c, SurfaceMesh *surfmesh)
917 {
918 float ax,ay,az;
919 float bx,by,bz;
920 float cx,cy,cz;
921 float xx,yy,zz;
922 float distance;
923 FLTVECT tmp;
924 float tx,ty,tz;
925
926
927 ax = surfmesh->vertex[a].x;
928 ay = surfmesh->vertex[a].y;
929 az = surfmesh->vertex[a].z;
930 bx = surfmesh->vertex[b].x;
931 by = surfmesh->vertex[b].y;
932 bz = surfmesh->vertex[b].z;
933 cx = surfmesh->vertex[c].x;
934 cy = surfmesh->vertex[c].y;
935 cz = surfmesh->vertex[c].z;
936
937 bx -= ax;
938 by -= ay;
939 bz -= az;
940 distance = sqrt(bx*bx+by*by+bz*bz);
941 if (distance > 0) {
942 bx /= distance;
943 by /= distance;
944 bz /= distance;
945 }
946 cx -= ax;
947 cy -= ay;
948 cz -= az;
949 distance = sqrt(cx*cx+cy*cy+cz*cz);
950 if (distance > 0) {
951 cx /= distance;
952 cy /= distance;
953 cz /= distance;
954 }
955 tx = 0.5*(cx+bx);
956 ty = 0.5*(cy+by);
957 tz = 0.5*(cz+bz);
958 distance = sqrt(tx*tx+ty*ty+tz*tz);
959 if (distance > 0) {
960 tx /= distance;
961 ty /= distance;
962 tz /= distance;
963 }
964 xx = by*cz-bz*cy;
965 yy = bz*cx-bx*cz;
966 zz = bx*cy-by*cx;
967 distance = sqrt(xx*xx+yy*yy+zz*zz);
968 if (distance > 0) {
969 xx /= distance;
970 yy /= distance;
971 zz /= distance;
972 }
973 bx = xx;
974 by = yy;
975 bz = zz;
976
977 distance = tx*(x-ax)+ty*(y-ay)+tz*(z-az);
978 xx = distance*tx + ax;
979 yy = distance*ty + ay;
980 zz = distance*tz + az;
981
982 distance = bx*(x-xx)+by*(y-yy)+bz*(z-zz);
983 tmp.x = distance*bx + xx;
984 tmp.y = distance*by + yy;
985 tmp.z = distance*bz + zz;
986
987 return(tmp);
988 }
989
990
991 /*
992 * ***************************************************************************
993 * Routine: SurfaceMesh_getMinMaxAngles
994 *
995 * Author: Zeyun Yu ([email protected])
996 *
997 * Purpose: Calculate the minimum and maximum angles in a surface mesh
998 * ***************************************************************************
999 */
1000 void SurfaceMesh_getMinMaxAngles(SurfaceMesh *surfmesh, float *minangle,
1001 float *maxangle, int *num_small, int *num_large,
1002 int max_min_angle, int min_max_angle)
1003 {
1004 int n,num1,num2;
1005 int a,b,c;
1006 float min_angle, max_angle;
1007 float angle;
1008
1009
1010 min_angle = 99999.0;
1011 max_angle = -99999.0;
1012 num1 = 0;
1013 num2 = 0;
1014 for (n = 0; n < surfmesh->nf; n++) {
1015 a = surfmesh->face[n].a;
1016 b = surfmesh->face[n].b;
1017 c = surfmesh->face[n].c;
1018
1019 angle = GetAngleSurfaceOnly(surfmesh,a,b,c);
1020 if (angle != -999) {
1021 if (angle < min_angle)
1022 min_angle = angle;
1023 if (angle > max_angle)
1024 max_angle = angle;
1025 if (angle < max_min_angle)
1026 num1++;
1027 if (angle > min_max_angle)
1028 num2++;
1029 }
1030 angle = GetAngleSurfaceOnly(surfmesh,b,a,c);
1031 if (angle != -999) {
1032 if (angle < min_angle)
1033 min_angle = angle;
1034 if (angle > max_angle)
1035 max_angle = angle;
1036 if (angle < max_min_angle)
1037 num1++;
1038 if (angle > min_max_angle)
1039 num2++;
1040 }
1041 angle = GetAngleSurfaceOnly(surfmesh,c,a,b);
1042 if (angle != -999) {
1043 if (angle < min_angle)
1044 min_angle = angle;
1045 if (angle > max_angle)
1046 max_angle = angle;
1047 if (angle < max_min_angle)
1048 num1++;
1049 if (angle > min_max_angle)
1050 num2++;
1051 }
1052 }
1053
1054 *minangle = min_angle;
1055 *maxangle = max_angle;
1056 *num_small = num1;
1057 *num_large = num2;
1058 }
1059
1060
1061
1062 /*
1063 * ***************************************************************************
1064 * Routine: GetAngleSurfaceOnly
1065 *
1066 * Author: Zeyun Yu ([email protected])
1067 *
1068 * Purpose: Calculate the angle defined by three vertices
1069 * ***************************************************************************
1070 */
1071 float GetAngleSurfaceOnly(SurfaceMesh *surfmesh, int a, int b, int c)
1072 {
1073 float ax,ay,az;
1074 float bx,by,bz;
1075 float cx,cy,cz;
1076 float length1,length2,length3;
1077 float angle;
1078
1079
1080 ax = surfmesh->vertex[a].x;
1081 ay = surfmesh->vertex[a].y;
1082 az = surfmesh->vertex[a].z;
1083 bx = surfmesh->vertex[b].x;
1084 by = surfmesh->vertex[b].y;
1085 bz = surfmesh->vertex[b].z;
1086 cx = surfmesh->vertex[c].x;
1087 cy = surfmesh->vertex[c].y;
1088 cz = surfmesh->vertex[c].z;
1089
1090
1091 length1 = (ax-bx)*(ax-bx)+(ay-by)*(ay-by)+(az-bz)*(az-bz);
1092 length2 = (ax-cx)*(ax-cx)+(ay-cy)*(ay-cy)+(az-cz)*(az-cz);
1093 length3 = (bx-cx)*(bx-cx)+(by-cy)*(by-cy)+(bz-cz)*(bz-cz);
1094 if (length1 == 0 || length2 == 0)
1095 angle = -999;
1096 else {
1097 angle = 0.5*(length1+length2-length3)/sqrt(length1*length2);
1098 angle = acos(angle)*180.0/PIE;
1099 }
1100
1101 return(angle);
1102 }
1103
1104
1105
1106
1107 /*
1108 * ***************************************************************************
1109 * Routine: GetEigenVector
1110 *
1111 * Author: Zeyun Yu ([email protected])
1112 *
1113 * Purpose: Calculate the eigenvalues and eigenvectors of
1114 * the local structure tensor in a neighborhood of "index0"
1115 * ***************************************************************************
1116 */
1117 EIGENVECT GetEigenVector(SurfaceMesh *surfmesh,
1118 int index0, FLTVECT *eigen_value, float *max_ang)
1119 {
1120 int index,dist;
1121 int n,m;
1122 double x1, x2, x3;
1123 double a,b,Q;
1124 double c0,c1,c2;
1125 double A[3][3];
1126 double B[6];
1127 double theta, p;
1128 double tx,ty,tz;
1129 EIGENVECT tmp;
1130 FLTVECT normal,normal0;
1131
1132 int IndexArray[333];
1133 int DistArray[333];
1134 int start_ptr,end_ptr;
1135 int visited;
1136 NPNT3 **neighbor_list = surfmesh->neighbor_list;
1137 NPNT3 *first_ngr;
1138 float angle, max_angle;
1139
1140
1141 normal = GetNormals(surfmesh, index0);
1142 A[0][0] = normal.x*normal.x;
1143 A[0][1] = normal.x*normal.y;
1144 A[0][2] = normal.x*normal.z;
1145 A[1][1] = normal.y*normal.y;
1146 A[1][2] = normal.y*normal.z;
1147 A[2][2] = normal.z*normal.z;
1148
1149 start_ptr = 0;
1150 end_ptr = 1;
1151 IndexArray[start_ptr] = index0;
1152 DistArray[start_ptr] = 0;
1153
1154 max_angle = 99999.0;
1155 normal0.x = normal.x;
1156 normal0.y = normal.y;
1157 normal0.z = normal.z;
1158 while (start_ptr < end_ptr) {
1159 index = IndexArray[start_ptr];
1160 dist = DistArray[start_ptr];
1161 start_ptr ++;
1162
1163 if (dist < ((DIM_SCALE>2) ? (3):(2))) {
1164 first_ngr = neighbor_list[index];
1165 while (first_ngr != NULL) {
1166 m = first_ngr->a;
1167 visited = 0;
1168 for (n = 0; n < end_ptr; n++) {
1169 if (IndexArray[n] == m) {
1170 visited = 1;
1171 break;
1172 }
1173 }
1174 if (visited == 0) {
1175 normal = GetNormals(surfmesh, m);
1176 angle = normal0.x*normal.x+normal0.y*normal.y+normal0.z*normal.z;
1177 if (angle < 0)
1178 angle = -angle;
1179 if (angle < max_angle)
1180 max_angle = angle;
1181 A[0][0] += normal.x*normal.x;
1182 A[0][1] += normal.x*normal.y;
1183 A[0][2] += normal.x*normal.z;
1184 A[1][1] += normal.y*normal.y;
1185 A[1][2] += normal.y*normal.z;
1186 A[2][2] += normal.z*normal.z;
1187 IndexArray[end_ptr] = m;
1188 DistArray[end_ptr] = dist+1;
1189 end_ptr ++;
1190 }
1191 first_ngr = first_ngr->next;
1192 }
1193 }
1194 }
1195 *max_ang = max_angle;
1196
1197 A[1][0] = A[0][1];
1198 A[2][0] = A[0][2];
1199 A[2][1] = A[1][2];
1200
1201 c0 = A[0][0]*A[1][1]*A[2][2]+2*A[0][1]*A[0][2]*A[1][2]-A[0][0]*A[1][2]*A[1][2]
1202 -A[1][1]*A[0][2]*A[0][2]-A[2][2]*A[0][1]*A[0][1];
1203 c1 = A[0][0]*A[1][1]-A[0][1]*A[0][1]+A[0][0]*A[2][2]-
1204 A[0][2]*A[0][2]+A[1][1]*A[2][2]-A[1][2]*A[1][2];
1205 c2 = A[0][0]+A[1][1]+A[2][2];
1206
1207 a = (3.0*c1-c2*c2)/3.0;
1208 b = (-2.0*c2*c2*c2+9.0*c1*c2-27.0*c0)/27.0;
1209 Q = b*b/4.0+a*a*a/27.0;
1210
1211 theta = atan2(sqrt(-Q),-0.5*b);
1212 p = sqrt(0.25*b*b-Q);
1213 x1 = c2/3.0+2.0*pow(p,1.0/3.0)*cos(theta/3.0);
1214 x2 = c2/3.0-pow(p,1.0/3.0)*(cos(theta/3.0)+sqrt(3.0)*sin(theta/3.0));
1215 x3 = c2/3.0-pow(p,1.0/3.0)*(cos(theta/3.0)-sqrt(3.0)*sin(theta/3.0));
1216
1217 if (isnan(x1) || isnan(x2) || isnan(x3)) {
1218 tmp.x1 = 0;
1219 tmp.y1 = 0;
1220 tmp.z1 = 0;
1221 tmp.x2 = 0;
1222 tmp.y2 = 0;
1223 tmp.z2 = 0;
1224 tmp.x3 = 0;
1225 tmp.y3 = 0;
1226 tmp.z3 = 0;
1227 }
1228 else {
1229 tx = max(x1,max(x2,x3));
1230 if (tx == x1) {
1231 if (x2 >= x3) {
1232 ty = x2;
1233 tz = x3;
1234 }
1235 else {
1236 ty = x3;
1237 tz = x2;
1238 }
1239 }
1240 else if (tx == x2) {
1241 if (x1 >= x3) {
1242 ty = x1;
1243 tz = x3;
1244 }
1245 else {
1246 ty = x3;
1247 tz = x1;
1248 }
1249 }
1250 else if (tx == x3) {
1251 if (x1 >= x2) {
1252 ty = x1;
1253 tz = x2;
1254 }
1255 else {
1256 ty = x2;
1257 tz = x1;
1258 }
1259 }
1260 x1 = tx;
1261 x2 = ty;
1262 x3 = tz;
1263 eigen_value->x = tx;
1264 eigen_value->y = ty;
1265 eigen_value->z = tz;
1266
1267 if (x1 > 99999 || x1 < -99999 ||
1268 x2 > 99999 || x2 < -99999 ||
1269 x3 > 99999 || x3 < -99999) {
1270 printf("dsadsadsad: %f %f %f\n",x1,x2,x3);
1271 exit(0);
1272 }
1273
1274
1275 A[0][0] -= x1;
1276 A[1][1] -= x1;
1277 A[2][2] -= x1;
1278 B[0] = A[1][1]*A[2][2]-A[1][2]*A[1][2];
1279 B[1] = A[0][2]*A[1][2]-A[0][1]*A[2][2];
1280 B[2] = A[0][0]*A[2][2]-A[0][2]*A[0][2];
1281 B[3] = A[0][1]*A[1][2]-A[0][2]*A[1][1];
1282 B[4] = A[0][1]*A[0][2]-A[1][2]*A[0][0];
1283 B[5] = A[0][0]*A[1][1]-A[0][1]*A[0][1];
1284 c0 = B[0]*B[0]+B[1]*B[1]+B[3]*B[3];
1285 c1 = B[1]*B[1]+B[2]*B[2]+B[4]*B[4];
1286 c2 = B[3]*B[3]+B[4]*B[4]+B[5]*B[5];
1287 if (c0 >= c1 && c0 >= c2) {
1288 tx = B[0];
1289 ty = B[1];
1290 tz = B[3];
1291 }
1292 else if (c1 >= c0 && c1 >= c2) {
1293 tx = B[1];
1294 ty = B[2];
1295 tz = B[4];
1296 }
1297 else if (c2 >= c0 && c2 >= c1) {
1298 tx = B[3];
1299 ty = B[4];
1300 tz = B[5];
1301 }
1302 p = sqrt(tx*tx+ty*ty+tz*tz);
1303 if (p > 0) {
1304 tx /= p;
1305 ty /= p;
1306 tz /= p;
1307 }
1308 tmp.x1 = tx;
1309 tmp.y1 = ty;
1310 tmp.z1 = tz;
1311 A[0][0] += x1;
1312 A[1][1] += x1;
1313 A[2][2] += x1;
1314
1315
1316 A[0][0] -= x2;
1317 A[1][1] -= x2;
1318 A[2][2] -= x2;
1319 B[0] = A[1][1]*A[2][2]-A[1][2]*A[1][2];
1320 B[1] = A[0][2]*A[1][2]-A[0][1]*A[2][2];
1321 B[2] = A[0][0]*A[2][2]-A[0][2]*A[0][2];
1322 B[3] = A[0][1]*A[1][2]-A[0][2]*A[1][1];
1323 B[4] = A[0][1]*A[0][2]-A[1][2]*A[0][0];
1324 B[5] = A[0][0]*A[1][1]-A[0][1]*A[0][1];
1325 c0 = B[0]*B[0]+B[1]*B[1]+B[3]*B[3];
1326 c1 = B[1]*B[1]+B[2]*B[2]+B[4]*B[4];
1327 c2 = B[3]*B[3]+B[4]*B[4]+B[5]*B[5];
1328 if (c0 >= c1 && c0 >= c2) {
1329 tx = B[0];
1330 ty = B[1];
1331 tz = B[3];
1332 }
1333 else if (c1 >= c0 && c1 >= c2) {
1334 tx = B[1];
1335 ty = B[2];
1336 tz = B[4];
1337 }
1338 else if (c2 >= c0 && c2 >= c1) {
1339 tx = B[3];
1340 ty = B[4];
1341 tz = B[5];
1342 }
1343 p = sqrt(tx*tx+ty*ty+tz*tz);
1344 if (p > 0) {
1345 tx /= p;
1346 ty /= p;
1347 tz /= p;
1348 }
1349 tmp.x2 = tx;
1350 tmp.y2 = ty;
1351 tmp.z2 = tz;
1352
1353 tmp.x3 = tmp.y1*tz-tmp.z1*ty;
1354 tmp.y3 = tmp.z1*tx-tmp.x1*tz;
1355 tmp.z3 = tmp.x1*ty-tmp.y1*tx;
1356 }
1357
1358 return(tmp);
1359 }
1360
1361
1362
1363
1364 /*
1365 * ***************************************************************************
1366 * Routine: GetNormals
1367 *
1368 * Author: Zeyun Yu ([email protected])
1369 *
1370 * Purpose: Calculate the normal vector at vertex "n"
1371 * ***************************************************************************
1372 */
1373 FLTVECT GetNormals(SurfaceMesh *surfmesh, int n)
1374 {
1375 int a,b;
1376 float x,y,z;
1377 NPNT3 **neighbor_list = surfmesh->neighbor_list;
1378 NPNT3 *first_ngr;
1379 int number;
1380 FLTVECT normal;
1381 float gx,gy,gz;
1382 float ax,ay,az;
1383 float bx,by,bz;
1384 float length;
1385
1386
1387
1388 x = surfmesh->vertex[n].x;
1389 y = surfmesh->vertex[n].y;
1390 z = surfmesh->vertex[n].z;
1391
1392 number = 0;
1393 normal.x = 0;
1394 normal.y = 0;
1395 normal.z = 0;
1396 first_ngr = neighbor_list[n];
1397 while (first_ngr != NULL) {
1398 a = first_ngr->a;
1399 b = first_ngr->b;
1400
1401 ax = surfmesh->vertex[a].x-x;
1402 ay = surfmesh->vertex[a].y-y;
1403 az = surfmesh->vertex[a].z-z;
1404 length = sqrt(ax*ax+ay*ay+az*az);
1405 if (length > 0) {
1406 ax /= length;
1407 ay /= length;
1408 az /= length;
1409 }
1410 bx = surfmesh->vertex[b].x-x;
1411 by = surfmesh->vertex[b].y-y;
1412 bz = surfmesh->vertex[b].z-z;
1413 length = sqrt(bx*bx+by*by+bz*bz);
1414 if (length > 0) {
1415 bx /= length;
1416 by /= length;
1417 bz /= length;
1418 }
1419 gx = ay*bz-az*by;
1420 gy = az*bx-ax*bz;
1421 gz = ax*by-ay*bx;
1422 length = sqrt(gx*gx+gy*gy+gz*gz);
1423 if (length > 0) {
1424 gx /= length;
1425 gy /= length;
1426 gz /= length;
1427 }
1428 length = normal.x*gx+normal.y*gy+normal.z*gz;
1429 if (length < 0) {
1430 gx = -gx;
1431 gy = -gy;
1432 gz = -gz;
1433 }
1434 normal.x += gx;
1435 normal.y += gy;
1436 normal.z += gz;
1437
1438 number ++;
1439 first_ngr = first_ngr->next;
1440 }
1441
1442 if (number > 0) {
1443 normal.x /= (float)number;
1444 normal.y /= (float)number;
1445 normal.z /= (float)number;
1446 length = sqrt(normal.x*normal.x+normal.y*normal.y+normal.z*normal.z);
1447 if (length > 0) {
1448 normal.x /= length;
1449 normal.y /= length;
1450 normal.z /= length;
1451 }
1452 }
1453 else {
1454 normal.x = 0;
1455 normal.y = 0;
1456 normal.z = 0;
1457 }
1458
1459 return(normal);
1460 }
1461
1462
1463
1464
1465 /*
1466 * ***************************************************************************
1467 * Routine: CheckFlipAction
1468 *
1469 * Author: Zeyun Yu ([email protected])
1470 *
1471 * Purpose: Check if the edge flipping is needed or not, by the
1472 * "smaller angle criterion"
1473 * ***************************************************************************
1474 */
1475 char CheckFlipAction(SurfaceMesh *surfmesh,
1476 int a, int b, int c, int d)
1477 {
1478 NPNT3 **neighbor_list = surfmesh->neighbor_list;
1479 float min_angle1,min_angle2,angle;
1480
1481 /* smaller angle criterion */
1482 min_angle1 = -99999;
1483 angle = GetDotProduct(surfmesh, a,b,c);
1484 if (angle > min_angle1)
1485 min_angle1 = angle;
1486 angle = GetDotProduct(surfmesh, a,b,d);
1487 if (angle > min_angle1)
1488 min_angle1 = angle;
1489 angle = GetDotProduct(surfmesh, b,a,c);
1490 if (angle > min_angle1)
1491 min_angle1 = angle;
1492 angle = GetDotProduct(surfmesh, b,a,d);
1493 if (angle > min_angle1)
1494 min_angle1 = angle;
1495
1496 min_angle2 = -99999;
1497 angle = GetDotProduct(surfmesh, c,a,d);
1498 if (angle > min_angle2)
1499 min_angle2 = angle;
1500 angle = GetDotProduct(surfmesh, c,b,d);
1501 if (angle > min_angle2)
1502 min_angle2 = angle;
1503 angle = GetDotProduct(surfmesh, d,a,c);
1504 if (angle > min_angle2)
1505 min_angle2 = angle;
1506 angle = GetDotProduct(surfmesh, d,b,c);
1507 if (angle > min_angle2)
1508 min_angle2 = angle;
1509
1510 if (min_angle1 > min_angle2)
1511 return(1);
1512 else
1513 return(0);
1514
1515 }
1516
1517
1518
1519
1520 /*
1521 * ***************************************************************************
1522 * Routine: GetDotProduct
1523 *
1524 * Author: Zeyun Yu ([email protected])
1525 *
1526 * Purpose: Calculate the dot product of two vectors (b-a) and (c-a)
1527 * ***************************************************************************
1528 */
1529 float GetDotProduct(SurfaceMesh *surfmesh,int a, int b, int c)
1530 {
1531 float cx,cy,cz;
1532 float bx,by,bz;
1533 float length;
1534
1535
1536 bx = surfmesh->vertex[b].x-surfmesh->vertex[a].x;
1537 by = surfmesh->vertex[b].y-surfmesh->vertex[a].y;
1538 bz = surfmesh->vertex[b].z-surfmesh->vertex[a].z;
1539 length = sqrt(bx*bx+by*by+bz*bz);
1540 if (length > 0) {
1541 bx /= length;
1542 by /= length;
1543 bz /= length;
1544 }
1545
1546 cx = surfmesh->vertex[c].x-surfmesh->vertex[a].x;
1547 cy = surfmesh->vertex[c].y-surfmesh->vertex[a].y;
1548 cz = surfmesh->vertex[c].z-surfmesh->vertex[a].z;
1549 length = sqrt(cx*cx+cy*cy+cz*cz);
1550 if (length > 0) {
1551 cx /= length;
1552 cy /= length;
1553 cz /= length;
1554 }
1555
1556 length = bx*cx+by*cy+bz*cz;
1557 return(length);
1558 }
1559
1560
1561
1562
1563
1564 /*
1565 * ***************************************************************************
1566 * Routine: EdgeFlipping
1567 *
1568 * Author: Zeyun Yu ([email protected])
1569 *
1570 * Purpose: Perform the edge flipping
1571 * ***************************************************************************
1572 */
1573 void EdgeFlipping(SurfaceMesh *surfmesh, int n)
1574 {
1575 int a,b,c;
1576 NPNT3 **neighbor_list = surfmesh->neighbor_list;
1577 NPNT3 *first_ngr,*second_ngr;
1578 NPNT3 *tmp_ngr1, *tmp_ngr2, *tmp_ngr;
1579 char flip_flag,flip_check;
1580 int f1,f2, number;
1581 float ax,ay,az;
1582
1583
1584 first_ngr = neighbor_list[n];
1585 while (first_ngr != NULL) {
1586
1587 number = 0;
1588 tmp_ngr = neighbor_list[n];
1589 while (tmp_ngr != NULL) {
1590 number++;
1591 tmp_ngr = tmp_ngr->next;
1592 }
1593 if (number <= 3) {
1594 if (number > 0) {
1595 ax = 0;
1596 ay = 0;
1597 az = 0;
1598 tmp_ngr = neighbor_list[n];
1599 while (tmp_ngr != NULL) {
1600 a = tmp_ngr->a;
1601 ax += surfmesh->vertex[a].x;
1602 ay += surfmesh->vertex[a].y;
1603 az += surfmesh->vertex[a].z;
1604 tmp_ngr = tmp_ngr->next;
1605 }
1606
1607 surfmesh->vertex[n].x = ax/(float)number;
1608 surfmesh->vertex[n].y = ay/(float)number;
1609 surfmesh->vertex[n].z = az/(float)number;
1610 }
1611 return;
1612 }
1613
1614 a = first_ngr->a;
1615 b = first_ngr->b;
1616 second_ngr = first_ngr->next;
1617 if (second_ngr == NULL)
1618 second_ngr = neighbor_list[n];
1619 c = second_ngr->b;
1620
1621 flip_flag = 1;
1622 number = 0;
1623 tmp_ngr = neighbor_list[b];
1624 while (tmp_ngr != NULL) {
1625 number++;
1626 tmp_ngr = tmp_ngr->next;
1627 }
1628 if (number <= 3)
1629 flip_flag = 0;
1630
1631 tmp_ngr = neighbor_list[a];
1632 while (tmp_ngr != NULL) {
1633 if (tmp_ngr->a == c)
1634 flip_flag = 0;
1635 tmp_ngr = tmp_ngr->next;
1636 }
1637 tmp_ngr = neighbor_list[c];
1638 while (tmp_ngr != NULL) {
1639 if (tmp_ngr->a == a)
1640 flip_flag = 0;
1641 tmp_ngr = tmp_ngr->next;
1642 }
1643
1644 if (flip_flag) {
1645
1646 flip_check = CheckFlipAction(surfmesh,n,b,a,c);
1647
1648 if (flip_check) {
1649 f1 = first_ngr->c;
1650 f2 = second_ngr->c;
1651
1652 /* Update face info */
1653 surfmesh->face[f1].a = n;
1654 surfmesh->face[f1].b = a;
1655 surfmesh->face[f1].c = c;
1656 surfmesh->face[f2].a = b;
1657 surfmesh->face[f2].b = c; // Switch a and c here to make the face
1658 surfmesh->face[f2].c = a; // normal point outward
1659
1660 /* Delete the entries in neighbor lists */
1661 first_ngr->b = c;
1662 if (first_ngr->next == NULL)
1663 neighbor_list[n] = neighbor_list[n]->next;
1664 else
1665 first_ngr->next = second_ngr->next;
1666 tmp_ngr1 = second_ngr;
1667
1668 tmp_ngr = neighbor_list[b];
1669 while (tmp_ngr != NULL) {
1670 if (tmp_ngr->b == n)
1671 break;
1672 tmp_ngr = tmp_ngr->next;
1673 }
1674 if (tmp_ngr == NULL)
1675 printf("my god ... %d\n",n);
1676 if (tmp_ngr->a == c) {
1677 tmp_ngr->b = a;
1678 tmp_ngr->c = f2;
1679 if (tmp_ngr->next == NULL) {
1680 second_ngr = neighbor_list[b];
1681 neighbor_list[b] = second_ngr->next;
1682 }
1683 else {
1684 second_ngr = tmp_ngr->next;
1685 tmp_ngr->next = second_ngr->next;
1686 }
1687 tmp_ngr2 = second_ngr;
1688 }
1689 else
1690 {
1691 printf("delete error!!! %d : %d %d %d\n",n,a,b,c);
1692 printf("(%f,%f,%f)\n",
1693 surfmesh->vertex[n].x,
1694 surfmesh->vertex[n].y,
1695 surfmesh->vertex[n].z);
1696 }
1697
1698 /* Add the entries in neighbor lists */
1699 tmp_ngr = neighbor_list[a];
1700 while (tmp_ngr != NULL) {
1701 if ((tmp_ngr->a == n && tmp_ngr->b == b) ||
1702 (tmp_ngr->a == b && tmp_ngr->b == n))
1703 break;
1704 tmp_ngr = tmp_ngr->next;
1705 }
1706
1707 // Assume neigbors are stored counter clockwise
1708 if (tmp_ngr->a == b && tmp_ngr->b == n) {
1709 tmp_ngr->b = c;
1710 tmp_ngr->c = f2;
1711 tmp_ngr1->a = c;
1712 tmp_ngr1->b = n;
1713 tmp_ngr1->c = f1;
1714 tmp_ngr1->next = tmp_ngr->next;
1715 tmp_ngr->next = tmp_ngr1;
1716 }
1717 else
1718 printf("add error 111\n");
1719
1720 tmp_ngr = neighbor_list[c];
1721 while (tmp_ngr != NULL) {
1722 if ((tmp_ngr->a == n && tmp_ngr->b == b) ||
1723 (tmp_ngr->a == b && tmp_ngr->b == n))
1724 break;
1725 tmp_ngr = tmp_ngr->next;
1726 }
1727
1728 // Assume neigbors are stored counter clockwise
1729 if (tmp_ngr->a == n && tmp_ngr->b == b) {
1730 tmp_ngr->b = a;
1731 tmp_ngr->c = f1;
1732 tmp_ngr2->a = a;
1733 tmp_ngr2->b = b;
1734 tmp_ngr2->c = f2;
1735 tmp_ngr2->next = tmp_ngr->next;
1736 tmp_ngr->next = tmp_ngr2;
1737 }
1738 else
1739 printf("add error 222\n");
1740 }
1741 }
1742
1743 first_ngr = first_ngr->next;
1744 }
1745
1746 }
1747
1748
1749
1750
1751 /*
1752 * ***************************************************************************
1753 * Routine: NormalSmooth
1754 *
1755 * Author: Zeyun Yu ([email protected])
1756 *
1757 * Purpose: Smooth the surface mesh by anisotropic normal-based averaging
1758 * ***************************************************************************
1759 */
1760 void NormalSmooth(SurfaceMesh *surfmesh, int n)
1761 {
1762 int a,b,c,d,e;
1763 NPNT3 **neighbor_list = surfmesh->neighbor_list;
1764 NPNT3 *first_ngr,*second_ngr,*third_ngr;
1765 NPNT3 *tmp_ngr;
1766 float bx,by,bz;
1767 float cx,cy,cz;
1768 float dx,dy,dz;
1769 float fx,fy,fz;
1770 float gx,gy,gz;
1771 float pos_x,pos_y,pos_z;
1772 int number,num;
1773 float theta,phi,alpha;
1774 float length;
1775 FLTVECT normal,sv;
1776
1777
1778 number = 0;
1779 pos_x = 0;
1780 pos_y = 0;
1781 pos_z = 0;
1782 first_ngr = neighbor_list[n];
1783 while (first_ngr != NULL) {
1784 a = first_ngr->a;
1785 b = first_ngr->b;
1786 second_ngr = first_ngr->next;
1787 if (second_ngr == NULL)
1788 second_ngr = neighbor_list[n];
1789 c = second_ngr->b;
1790 third_ngr = second_ngr->next;
1791 if (third_ngr == NULL)
1792 third_ngr = neighbor_list[n];
1793 d = third_ngr->b;
1794
1795 tmp_ngr = neighbor_list[b];
1796 while (tmp_ngr != NULL) {
1797 if ((tmp_ngr->a == c && tmp_ngr->b != n) ||
1798 (tmp_ngr->b == c && tmp_ngr->a != n))
1799 break;
1800 tmp_ngr = tmp_ngr->next;
1801 }
1802 if (tmp_ngr->a == c && tmp_ngr->b != n)
1803 e = tmp_ngr->b;
1804 else if (tmp_ngr->b == c && tmp_ngr->a != n)
1805 e = tmp_ngr->a;
1806 else
1807 printf("normal smoothing error...\n");
1808
1809 normal = GetCrossProduct(surfmesh, n, b, c);
1810 gx = normal.x;
1811 gy = normal.y;
1812 gz = normal.z;
1813 dx = 0;
1814 dy = 0;
1815 dz = 0;
1816
1817 num = 0;
1818 normal = GetCrossProduct(surfmesh, n, a, b);
1819 length = normal.x*gx+normal.y*gy+normal.z*gz;
1820 if (length > 0) {
1821 num++;
1822 dx += length*normal.x;
1823 dy += length*normal.y;
1824 dz += length*normal.z;
1825 }
1826 normal = GetCrossProduct(surfmesh, n, c, d);
1827 length = normal.x*gx+normal.y*gy+normal.z*gz;
1828 if (length > 0) {
1829 num++;
1830 dx += length*normal.x;
1831 dy += length*normal.y;
1832 dz += length*normal.z;
1833 }
1834 normal = GetCrossProduct(surfmesh, b, e, c);
1835 length = normal.x*gx+normal.y*gy+normal.z*gz;
1836 if (length > 0) {
1837 num++;
1838 dx += length*normal.x;
1839 dy += length*normal.y;
1840 dz += length*normal.z;
1841 }
1842
1843 length = sqrt(dx*dx+dy*dy+dz*dz);
1844 if (length > 0) {
1845 dx /= length;
1846 dy /= length;
1847 dz /= length;
1848 fx = gy*dz-gz*dy;
1849 fy = gz*dx-gx*dz;
1850 fz = gx*dy-gy*dx;
1851 cx = surfmesh->vertex[c].x;
1852 cy = surfmesh->vertex[c].y;
1853 cz = surfmesh->vertex[c].z;
1854 bx = surfmesh->vertex[b].x;
1855 by = surfmesh->vertex[b].y;
1856 bz = surfmesh->vertex[b].z;
1857 length = fx*(bx-cx)+fy*(by-cy)+fz*(bz-cz);
1858 if (length >= 0) {
1859 theta = (float)atan2(by-cy,bx-cx);
1860 phi = (float)atan2(bz-cz, sqrt((bx-cx)*(bx-cx)+(by-cy)*(by-cy)));
1861 }
1862 else {
1863 theta = (float)atan2(cy-by,cx-bx);
1864 phi = (float)atan2(cz-bz, sqrt((bx-cx)*(bx-cx)+(by-cy)*(by-cy)));
1865 }
1866
1867 alpha = acos(dx*gx+dy*gy+dz*gz)/(float)(4.0-num);
1868 sv = Rotate(surfmesh->vertex[n].x-cx,surfmesh->vertex[n].y-cy,surfmesh->vertex[n].z-cz,theta,phi,alpha);
1869 pos_x += sv.x+cx;
1870 pos_y += sv.y+cy;
1871 pos_z += sv.z+cz;
1872
1873 number++;
1874 }
1875
1876 first_ngr = first_ngr->next;
1877 }
1878
1879 if (number > 0 && !isnan(pos_x) && !isnan(pos_y) && !isnan(pos_z)) {
1880 surfmesh->vertex[n].x = pos_x/(float)number;
1881 surfmesh->vertex[n].y = pos_y/(float)number;
1882 surfmesh->vertex[n].z = pos_z/(float)number;
1883 }
1884
1885 }
1886
1887
1888
1889 /*
1890 * ***************************************************************************
1891 * Routine: GetCrossProduct
1892 *
1893 * Author: Zeyun Yu ([email protected])
1894 *
1895 * Purpose: Calculate the cross product vector between (b-a) and (c-a)
1896 * ***************************************************************************
1897 */
1898 FLTVECT GetCrossProduct(SurfaceMesh *surfmesh,int a, int b, int c)
1899 {
1900 float gx,gy,gz;
1901 float cx,cy,cz;
1902 float bx,by,bz;
1903 float length;
1904 FLTVECT value;
1905
1906
1907 bx = surfmesh->vertex[b].x-surfmesh->vertex[a].x;
1908 by = surfmesh->vertex[b].y-surfmesh->vertex[a].y;
1909 bz = surfmesh->vertex[b].z-surfmesh->vertex[a].z;
1910 length = sqrt(bx*bx+by*by+bz*bz);
1911 if (length > 0) {
1912 bx /= length;
1913 by /= length;
1914 bz /= length;
1915 }
1916 cx = surfmesh->vertex[c].x-surfmesh->vertex[a].x;
1917 cy = surfmesh->vertex[c].y-surfmesh->vertex[a].y;
1918 cz = surfmesh->vertex[c].z-surfmesh->vertex[a].z;
1919 length = sqrt(cx*cx+cy*cy+cz*cz);
1920 if (length > 0) {
1921 cx /= length;
1922 cy /= length;
1923 cz /= length;
1924 }
1925 gx = cy*bz-cz*by;
1926 gy = cz*bx-cx*bz;
1927 gz = cx*by-cy*bx;
1928 length = sqrt(gx*gx+gy*gy+gz*gz);
1929 if (length > 0) {
1930 gx /= length;
1931 gy /= length;
1932 gz /= length;
1933 }
1934
1935 value.x = gx;
1936 value.y = gy;
1937 value.z = gz;
1938
1939 return(value);
1940 }
1941
1942
1943 /*
1944 * ***************************************************************************
1945 * Routine: Rotate
1946 *
1947 * Author: Zeyun Yu ([email protected])
1948 *
1949 * Purpose: Rotate a point "sx,sy,sz" around "theta,phi" axis by "angle"
1950 * ***************************************************************************
1951 */
1952 FLTVECT Rotate(float sx, float sy, float sz,
1953 float theta, float phi, float angle)
1954 {
1955 float x,y,z;
1956 float xx,yy,zz;
1957 float a[3][3],b[3][3];
1958 FLTVECT tmp;
1959
1960
1961 a[0][0] = (float)(cos(0.5*PIE-phi)*cos(theta));
1962 a[0][1] = (float)(cos(0.5*PIE-phi)*sin(theta));
1963 a[0][2] = (float)-sin(0.5*PIE-phi);
1964 a[1][0] = (float)-sin(theta);
1965 a[1][1] = (float)cos(theta);
1966 a[1][2] = 0.f;
1967 a[2][0] = (float)(sin(0.5*PIE-phi)*cos(theta));
1968 a[2][1] = (float)(sin(0.5*PIE-phi)*sin(theta));
1969 a[2][2] = (float)cos(0.5*PIE-phi);
1970
1971 b[0][0] = (float)(cos(0.5*PIE-phi)*cos(theta));
1972 b[0][1] = (float)-sin(theta);
1973 b[0][2] = (float)(sin(0.5*PIE-phi)*cos(theta));
1974 b[1][0] = (float)(cos(0.5*PIE-phi)*sin(theta));
1975 b[1][1] = (float)cos(theta);
1976 b[1][2] = (float)(sin(0.5*PIE-phi)*sin(theta));
1977 b[2][0] = (float)-sin(0.5*PIE-phi);
1978 b[2][1] = 0.f;
1979 b[2][2] = (float)cos(0.5*PIE-phi);
1980
1981
1982 x = a[0][0]*sx+a[0][1]*sy+a[0][2]*sz;
1983 y = a[1][0]*sx+a[1][1]*sy+a[1][2]*sz;
1984 z = a[2][0]*sx+a[2][1]*sy+a[2][2]*sz;
1985
1986 xx = (float)(cos(angle)*x - sin(angle)*y);
1987 yy = (float)(sin(angle)*x + cos(angle)*y);
1988 zz = z;
1989
1990 tmp.x = b[0][0]*xx+b[0][1]*yy+b[0][2]*zz;
1991 tmp.y = b[1][0]*xx+b[1][1]*yy+b[1][2]*zz;
1992 tmp.z = b[2][0]*xx+b[2][1]*yy+b[2][2]*zz;
1993
1994 return(tmp);
1995
1996 }