-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetailutil.c
2517 lines (2122 loc) · 96.5 KB
/
detailutil.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************
*
* detailutil.c - Utility subroutines for use primarily
* with rockdetail
*
* Mark J. Stock, [email protected]
*
*
* rocktools - Tools for creating and manipulating triangular meshes
* Copyright (C) 1999,2002-4,6,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 <string.h>
#include <math.h>
#include "structs.h"
tri_pointer split_tri(int,tri_pointer);
tri_pointer split_tri_hex(int,tri_pointer);
tri_pointer split_tri_5(int,tri_pointer);
int make_sphere(tri_pointer);
int find_adjacent_child(tri_pointer,tri_pointer,node_ptr,node_ptr);
node_ptr create_midpoint(node_ptr,node_ptr);
node_ptr create_midpoint_2(int,node_ptr,node_ptr);
node_ptr create_midpoint_3(int,node_ptr,node_ptr,node_ptr);
node_ptr create_midpoint_4(int,node_ptr,node_ptr,node_ptr,node_ptr);
node_ptr create_midpoint_5(int,node_ptr,node_ptr,node_ptr,node_ptr);
void move_existing_node_5( int, node_ptr);
void perturb_node_5 (VEC*, int, VEC);
node_ptr create_midpoint_spline(node_ptr,node_ptr,int*);
node_ptr create_center_point(int,node_ptr,node_ptr,node_ptr);
// from smoothutil.c
extern void compute_normals_2 (tri_pointer,int);
extern double normal_shake;
extern double normal_exponent;
extern double normal_bias;
extern double base_shake;
extern double base_exponent;
extern int use_spline;
extern int use_gaussian_random;
extern int clamp_edges;
extern int use_thresh;
extern double area_thresh;
extern int use_dist;
extern double distance_thresh;
extern VEC viewp;
extern int force_sphere;
extern double sphere_rad;
extern double find_tri_dist(tri_pointer,VEC);
/*
* split_tri_hex takes a linked list of triangles and splits each
* triangle into 6/4/2/0 new triangles. It adds 1 new node to
* the node list, and places it close to, but not exactly on, the
* the center of the original triangle.
*
* This routine mimics the hexagonal subdivision scheme originally
* proposed by Mandelbrot, and should reduce the effect of edging
* over successive generations.
*/
tri_pointer split_tri_hex(int depth,tri_pointer tri_head) {
int i,j,k;
int adj_side = -1;
int local_side[6];
//int tindex = 0;
//int nindex = 100;
double temp_area;
tri_pointer this_tri;
tri_pointer new_tri_head = NULL;
tri_pointer new_tri[2];
tri_pointer local_tri[6];
node_ptr new_node,new_node2;
fprintf(stderr,"Method 2, depth = %d\n",depth); fflush(stderr);
/* for each triangle in the old list */
this_tri = tri_head;
while (this_tri) {
// fprintf(stderr,"Checking tri %d\n",this_tri->index);
// fprintf(stdout," this tri has nodes %d %d %d\n",this_tri->node[0]->index,this_tri->node[1]->index,this_tri->node[2]->index);
// if the tri is too small, do not split it, just copy it to the new list
// For first take, split all tris
if (!this_tri->splittable) {
new_tri[0] = alloc_new_tri();
new_tri[0]->splittable = FALSE;
new_tri[0]->node[0] = this_tri->node[0];
new_tri[0]->node[1] = this_tri->node[1];
new_tri[0]->node[2] = this_tri->node[2];
new_tri[0]->adjacent[0] = this_tri->adjacent[0];
new_tri[0]->adjacent[1] = this_tri->adjacent[1];
new_tri[0]->adjacent[2] = this_tri->adjacent[2];
new_tri[0]->next_tri = new_tri_head;
new_tri_head = new_tri[0];
this_tri = this_tri->next_tri;
continue;
}
// choose the one new node location
new_node = create_center_point(depth,this_tri->node[0],this_tri->node[1],this_tri->node[2]);
// for each side, check to see if two triangles were already made
for (i=0; i<3; i++) {
j = (i+1)%3;
// fprintf(stdout,"adjacent tri %d is %d\n",i,this_tri->adjacent[i]->index);
// fprintf(stdout," has nodes %d %d %d\n",this_tri->adjacent[i]->node[0]->index,this_tri->adjacent[i]->node[1]->index,this_tri->adjacent[i]->node[2]->index);
// fflush(stdout);
// has side i been split?
if (this_tri->midpoint[i]) {
// if so, tell this side's two tris what their new node pointer is
// which of the neighbor's children need this info?
// find the index of the shared side from the adjacent tri's entry
for (k=0; k<3; k++) {
if (this_tri->node[i] == this_tri->adjacent[i]->node[k]) {
if (this_tri->node[j] == this_tri->adjacent[i]->node[(k+2)%3]) {
// both tris are oriented the same
// fprintf(stdout," side has been split, k=%d, tris are oriented\n",k);
// fflush(stdout);
adj_side = mod(k+2,3);
local_tri[i*2] = this_tri->adjacent[i]->adjacent[adj_side]->next_tri;
local_side[i*2] = 0;
local_tri[i*2+1] = this_tri->adjacent[i]->adjacent[adj_side];
local_side[i*2+1] = 2;
break;
} else {
// tris are oriented opposite
// fprintf(stdout," side has been split, k=%d, tris are opposite\n",k);
// fflush(stdout);
adj_side = k;
local_tri[i*2] = this_tri->adjacent[i]->adjacent[adj_side];
local_side[i*2] = 2;
local_tri[i*2+1] = this_tri->adjacent[i]->adjacent[adj_side]->next_tri;
local_side[i*2+1] = 0;
break;
}
}
}
// fprintf(stdout," index of the shared side %d\n",adj_side);
// fflush(stdout);
// set the existing side tris' empty node to new_node
this_tri->adjacent[i]->adjacent[adj_side]->node[0] = new_node;
this_tri->adjacent[i]->adjacent[adj_side]->next_tri->node[0] = new_node;
// let the parent point to the first of the two children
// on this side, regardless of the orientation
this_tri->adjacent[i] = this_tri->adjacent[i]->adjacent[adj_side];
// calculate triangle's area, if below either threshhold, set it up so it will not split
for (k=0; k<2; k++) local_tri[i*2+k]->splittable = TRUE;
if (use_thresh || use_dist) {
for (k=0; k<2; k++) {
temp_area = find_area(local_tri[i*2+k]);
if (use_thresh) {
if (temp_area < area_thresh) {
// this is a flag to the splitter, do not split further
local_tri[i*2+k]->splittable = FALSE;
}
}
if (use_dist) {
// if (sqrt(temp_area)/length(from(viewp,local_tri[i*2+k]->node[0]->loc)) < distance_thresh) {
if (sqrt(temp_area)/find_tri_dist(local_tri[i*2+k],viewp) < distance_thresh) {
// this is a flag to the splitter, do not split further
local_tri[i*2+k]->splittable = FALSE;
}
}
}
}
} else { // if side i has not been split
// if not, create two new elements, fill in as much data as possible
// fprintf(stdout," side has not been split\n");
// fflush(stdout);
// initialize two new elements
new_tri[0] = alloc_new_tri();
new_tri[1] = alloc_new_tri();
// define two new elements
new_tri[0]->node[0] = NULL;
new_tri[0]->node[1] = new_node;
new_tri[0]->node[2] = this_tri->node[i];
new_tri[0]->adjacent[0] = new_tri[1];
new_tri[0]->adjacent[1] = NULL;
new_tri[0]->adjacent[2] = NULL;
new_tri[1]->node[0] = NULL;
new_tri[1]->node[1] = this_tri->node[j];
new_tri[1]->node[2] = new_node;
new_tri[1]->adjacent[0] = NULL;
new_tri[1]->adjacent[1] = NULL;
new_tri[1]->adjacent[2] = new_tri[0];
// new tri's midpoints will not be set this recursion level, set to NULL
for (k=0; k<2; k++) for (j=0; j<3; j++) new_tri[k]->midpoint[j] = NULL;
j = (i+1)%3;
local_tri[i*2] = new_tri[0];
local_side[i*2] = 1;
local_tri[i*2+1] = new_tri[1];
local_side[i*2+1] = 1;
if (this_tri->adjacent[i]) {
// if there exists a neighboring parent, tell it that we've
// got a new node over here
// unless, of course, that element is to never be split again
if (!this_tri->adjacent[i]->splittable) {
// in that case, just split the edge at the midpoint
// and never deal with it again.
j = (i+1)%3;
new_node2 = create_midpoint(this_tri->node[i],this_tri->node[j]);
new_tri[0]->node[0] = new_node2;
new_tri[1]->node[0] = new_node2;
// now, must check the triangles created here for smallness
// calculate triangle's area, if below either threshhold, set it up so it will not split
for (k=0; k<2; k++) new_tri[k]->splittable = TRUE;
if (use_thresh || use_dist) {
for (k=0; k<2; k++) {
temp_area = find_area(new_tri[k]);
if (use_thresh) {
if (temp_area < area_thresh) {
// this is a flag to the splitter, do not split further
new_tri[k]->splittable = FALSE;
}
}
if (use_dist) {
// if (sqrt(temp_area)/length(from(viewp,new_tri[k]->node[0]->loc)) < distance_thresh) {
if (sqrt(temp_area)/find_tri_dist(new_tri[k],viewp) < distance_thresh) {
// this is a flag to the splitter, do not split further
new_tri[k]->splittable = FALSE;
}
}
}
}
} else {
// the neighboring parent exists and isn't too small
// find the index of the shared side from the adjacent tri's entry
for (k=0; k<3; k++) {
// fprintf(stdout," a neighbor parent exists %d\n",k);
// fflush(stdout);
if (this_tri->node[i] == this_tri->adjacent[i]->node[k]) {
if (this_tri->node[j] == this_tri->adjacent[i]->node[(k+2)%3]) {
// both tris are oriented the same
adj_side = (k+2)%3;
break;
} else {
// tris are oriented opposite
adj_side = k;
break;
}
}
}
}
// fprintf(stdout," index of the shared side %d\n",adj_side);
// fflush(stdout);
this_tri->adjacent[i]->midpoint[adj_side] = new_node;
} else {
// if there doesn't even *exist* an adjacent triangle, set a new edge point
// ALWAYS use the true midpoint, because there may be
// a neighbor triangle that is too small to split
// Well, not any more, with the if above
// use the better method for determining a new midpoint
k = (j+1)%3;
if (clamp_edges) {
// clamp the edge, use the mathematical midpoint
new_node2 = create_midpoint(this_tri->node[i],this_tri->node[j]);
} else {
// let the edge get jagged, use the current tri's 3 points
new_node2 = create_midpoint_3(depth,this_tri->node[i],
this_tri->node[j],
this_tri->node[k]);
}
// and set the appropriate node pointers
new_tri[0]->node[0] = new_node2;
new_tri[1]->node[0] = new_node2;
// new_tri->adjacents already point to NULL
// calculate triangle's area, if below either threshhold, set it up so it will not split
for (k=0; k<2; k++) new_tri[k]->splittable = TRUE;
if (use_thresh || use_dist) {
for (k=0; k<2; k++) {
temp_area = find_area(new_tri[k]);
if (use_thresh) {
if (temp_area < area_thresh) {
// this is a flag to the splitter, do not split further
new_tri[k]->splittable = FALSE;
}
}
if (use_dist) {
// if (sqrt(temp_area)/length(from(viewp,new_tri[k]->node[0]->loc)) < distance_thresh) {
if (sqrt(temp_area)/find_tri_dist(new_tri[k],viewp) < distance_thresh) {
// this is a flag to the splitter, do not split further
new_tri[k]->splittable = FALSE;
}
}
}
}
}
// let the parent point to the first of the two children
this_tri->adjacent[i] = new_tri[0];
// add the 2 new tris to the new list
new_tri[1]->next_tri = new_tri_head;
new_tri[0]->next_tri = new_tri[1];
new_tri_head = new_tri[0];
} // end if side has been split
} // end loop for i=0 to 2
// now that each of the three sides has been created/updated, we need to
// update the adjacent pointers within the parent tri
/*
for (i=0; i<6; i++) fprintf(stdout," local_side[%d] = %d\n",i,local_side[i]);
for (i=0; i<6; i++) {
if (local_tri[i]) {
fprintf(stdout," local_tri[%d] exists\n",i);
fflush(stdout);
}
}
*/
local_tri[0]->adjacent[local_side[0]] = local_tri[5];
local_tri[5]->adjacent[local_side[5]] = local_tri[0];
local_tri[2]->adjacent[local_side[2]] = local_tri[1];
local_tri[1]->adjacent[local_side[1]] = local_tri[2];
local_tri[4]->adjacent[local_side[4]] = local_tri[3];
local_tri[3]->adjacent[local_side[3]] = local_tri[4];
// for my own amusement, see if all of this parent->adjacent
// i.e. children, have a next_tri
/*
for (i=0; i<3; i++) {
if (this_tri->adjacent[i]->next_tri) {
fprintf(stdout," this_tri->adjacent[%d]->next_tri exists\n",i);
fflush(stdout);
} else {
fprintf(stdout," this_tri->adjacent[%d]->next_tri DOES NOT exist\n",i);
fflush(stdout);
}
}
*/
// we are now done with this parent tri, we may reference it later, though
this_tri = this_tri->next_tri;
}
// now, call the sphericalizing routine
if (force_sphere) make_sphere(new_tri_head);
/* replace the old list with the new list */
return new_tri_head;
}
/*
* split_tri takes a linked list of triangles and splits each
* triangle into 4 new triangles. It adds 3 new nodes to the node
* list, and places them close to, but not exactly on, the
* the midpoint between any two existing nodes.
*/
tri_pointer split_tri(int depth,tri_pointer tri_head) {
int h,i,j,k;
int adj_side = -1;
int far_corner = -1;
int num_new_tri;
int tri_cnt = 0;
int has_adjacent_been_split[3];
double temp_area;
tri_pointer this_tri;
tri_pointer new_tri_head = NULL;
tri_pointer new_tri[4];
node_ptr new_node[3];
fprintf(stderr,"Method 1, depth = %d\n",depth);
j = -1; k = -1;
// for each triangle in the old list
this_tri = tri_head;
while (this_tri) {
//fprintf(stderr,"Checking tri...\n");
// if the tri is too small, do not split it, just copy it to the new list
// new logic: if the tri is flagged to not split, split it minimally to
// account for neighboring splittable triangles!
if (!this_tri->splittable) {
//fprintf(stderr,"Unsplittable tri %d\n",this_tri->index); fflush(stderr);
// first, see how many neighbors will be split
num_new_tri = 1;
for (i=0; i<3; i++) new_node[i] = NULL;
// NOTE: to make it work, i<3 on next line!
for (i=0; i<3; i++) {
// check to see if a midpoint split already exists, use it if it does
if (this_tri->midpoint[i]) {
// that edge has already been split, use the point
has_adjacent_been_split[i] = TRUE;
new_node[i] = this_tri->midpoint[i];
num_new_tri++;
// if (this_tri->adjacent[i]->splittable) {
// fprintf(stderr," corresponding to splittable tri %d\n",this_tri->adjacent[i]->index); fflush(stderr);
// } else {
// fprintf(stderr," corresponding to unsplittable tri %d\n",this_tri->adjacent[i]->index); fflush(stderr);
// }
} else {
// if it doesn't, see if it eventually will
has_adjacent_been_split[i] = FALSE;
j = mod(i+1,3);
k = mod(j+1,3);
// fprintf(stderr," creating midpoint for side %d\n",i); fflush(stderr);
if (!this_tri->adjacent[i]) {
// there is no adjacent triangle, don't split this edge
} else if (this_tri->adjacent[i]->splittable) {
// fprintf(stderr," corresponding to splittable tri %d\n",this_tri->adjacent[i]->index); fflush(stderr);
// there is an adjacent triangle that can be split
// find the index of the adjacent tri's farthest node
for (h=0; h<3; h++) {
if (this_tri->node[i] == this_tri->adjacent[i]->node[h]) {
far_corner = mod(h+1,3);
break;
}
}
new_node[i] = create_midpoint_5(depth,this_tri->node[i],
this_tri->node[j],
this_tri->node[k],
this_tri->adjacent[i]->node[far_corner]);
// find the index of the shared side from the adjacent tri's entry
for (j=0; j<3; j++) {
if (this_tri->node[i] == this_tri->adjacent[i]->node[j]) {
adj_side = mod(j+2,3);
break;
}
}
j = mod(adj_side+1,3);
this_tri->adjacent[i]->midpoint[adj_side] = new_node[i];
// fprintf(stderr," and told adjacent tri %d that its side %d has a midpoint already\n",i,adj_side);
num_new_tri++;
} else {
// fprintf(stderr," corresponding to unsplittable tri %d\n",this_tri->adjacent[i]->index); fflush(stderr);
// there is an adjacent triangle, but it's flagged to not split,
// so don't split this side at all
}
}
}
// now, based on how many tris we have, create them
// fprintf(stderr," splitting into %d tris\n",num_new_tri); fflush(stderr);
// create the four new triangles
for (i=0; i<num_new_tri; i++) {
new_tri[i] = alloc_new_tri();
new_tri[i]->splittable = FALSE;
}
if (num_new_tri == 4) {
// treat it just like a regular split element, but flag all 4
// children as non-splittable
// no change to old method
// by default, set all external adjacent pointers to NULL, fix later
new_tri[0]->node[0] = this_tri->node[0];
new_tri[0]->node[1] = new_node[0];
new_tri[0]->node[2] = new_node[2];
new_tri[0]->adjacent[0] = NULL;
new_tri[0]->adjacent[1] = new_tri[3];
new_tri[0]->adjacent[2] = NULL;
new_tri[0]->index = tri_cnt++;
new_tri[1]->node[0] = new_node[0];
new_tri[1]->node[1] = this_tri->node[1];
new_tri[1]->node[2] = new_node[1];
new_tri[1]->adjacent[0] = NULL;
new_tri[1]->adjacent[1] = NULL;
new_tri[1]->adjacent[2] = new_tri[3];
new_tri[1]->index = tri_cnt++;
new_tri[2]->node[0] = new_node[2];
new_tri[2]->node[1] = new_node[1];
new_tri[2]->node[2] = this_tri->node[2];
new_tri[2]->adjacent[0] = new_tri[3];
new_tri[2]->adjacent[1] = NULL;
new_tri[2]->adjacent[2] = NULL;
new_tri[2]->index = tri_cnt++;
new_tri[3]->node[0] = new_node[1];
new_tri[3]->node[1] = new_node[2];
new_tri[3]->node[2] = new_node[0];
new_tri[3]->adjacent[0] = new_tri[2];
new_tri[3]->adjacent[1] = new_tri[0];
new_tri[3]->adjacent[2] = new_tri[1];
new_tri[3]->index = tri_cnt++;
// If parent had no adjacent tri, children on that edge have none, either.
// BUT, if the parent DID have an adjacent tri, children will also.
// So, if the parent had an adjacent tri, and it has been split, find the
// children's adjacent tris among the parent's adjacent's children
if (this_tri->adjacent[0] && has_adjacent_been_split[0]) {
// then use the child tri pointed to by this_tri->adjacent[0]->adjacent[0]
// to begin searching for the tris adjacent to the 2 new ones on this side
if (!find_adjacent_child(this_tri->adjacent[0]->adjacent[0],
new_tri[0],this_tri->node[0],new_node[0]) )
fprintf(stderr,"Could not find adjacent child 1.\n");
if (!find_adjacent_child(this_tri->adjacent[0]->adjacent[0],
new_tri[1],new_node[0],this_tri->node[1]) )
fprintf(stderr,"Could not find adjacent child 2.\n");
}
// do the same for parent's side 1
if (this_tri->adjacent[1] && has_adjacent_been_split[1]) {
if (!find_adjacent_child(this_tri->adjacent[1]->adjacent[0],
new_tri[1],this_tri->node[1],new_node[1]) )
fprintf(stderr,"Could not find adjacent child 3.\n");
if (!find_adjacent_child(this_tri->adjacent[1]->adjacent[0],
new_tri[2],new_node[1],this_tri->node[2]) )
fprintf(stderr,"Could not find adjacent child 4.\n");
}
// and for parent's side 2
if (this_tri->adjacent[2] && has_adjacent_been_split[2]) {
if (!find_adjacent_child(this_tri->adjacent[2]->adjacent[0],
new_tri[2],this_tri->node[2],new_node[2]) )
fprintf(stderr,"Could not find adjacent child 5.\n");
if (!find_adjacent_child(this_tri->adjacent[2]->adjacent[0],
new_tri[0],new_node[2],this_tri->node[0]) )
fprintf(stderr,"Could not find adjacent child 6.\n");
}
// new tri's midpoints will not be set this recursion level, set to NULL
for (i=0; i<4; i++) for (j=0; j<3; j++) new_tri[i]->midpoint[j] = NULL;
// Important: set this now-split parent triangle's first adjacent pointer to the
// first of the 4 new triangles created, this information will be useful later
this_tri->adjacent[0] = new_tri[0];
} else if (num_new_tri == 3) {
// first, which edge doesn't have a new_node?
for (h=0; h<3; h++) if (!new_node[h]) {
i = (h+1)%3;
j = (h+2)%3;
k = h;
break;
}
// geometry of the three triangles
new_tri[0]->node[0] = this_tri->node[i];
new_tri[0]->node[1] = new_node[i];
new_tri[0]->node[2] = this_tri->node[k];
new_tri[0]->adjacent[0] = NULL;
new_tri[0]->adjacent[1] = new_tri[2];
new_tri[0]->adjacent[2] = NULL;
new_tri[0]->index = tri_cnt++;
new_tri[1]->node[0] = new_node[i];
new_tri[1]->node[1] = this_tri->node[j];
new_tri[1]->node[2] = new_node[j];
new_tri[1]->adjacent[0] = NULL;
new_tri[1]->adjacent[1] = NULL;
new_tri[1]->adjacent[2] = new_tri[2];
new_tri[1]->index = tri_cnt++;
new_tri[2]->node[0] = new_node[i];
new_tri[2]->node[1] = new_node[j];
new_tri[2]->node[2] = this_tri->node[k];
new_tri[2]->adjacent[0] = new_tri[1];
new_tri[2]->adjacent[1] = NULL;
new_tri[2]->adjacent[2] = new_tri[0];
new_tri[2]->index = tri_cnt++;
// now, set those adjacent pointers
if (this_tri->adjacent[i] && has_adjacent_been_split[i]) {
if (!find_adjacent_child(this_tri->adjacent[i]->adjacent[0],
new_tri[0],this_tri->node[i],new_node[i]) )
fprintf(stderr,"Could not find adjacent child 1.\n");
if (!find_adjacent_child(this_tri->adjacent[i]->adjacent[0],
new_tri[1],new_node[i],this_tri->node[j]) )
fprintf(stderr,"Could not find adjacent child 2.\n");
}
if (this_tri->adjacent[j] && has_adjacent_been_split[j]) {
if (!find_adjacent_child(this_tri->adjacent[j]->adjacent[0],
new_tri[1],this_tri->node[j],new_node[j]) )
fprintf(stderr,"Could not find adjacent child 3.\n");
if (!find_adjacent_child(this_tri->adjacent[j]->adjacent[0],
new_tri[2],new_node[j],this_tri->node[k]) )
fprintf(stderr,"Could not find adjacent child 4.\n");
}
if (this_tri->adjacent[k] && has_adjacent_been_split[k]) {
fprintf(stderr,"Side k should not have been split!\n");
}
// set that other stuff
for (i=0; i<3; i++) for (j=0; j<3; j++) new_tri[i]->midpoint[j] = NULL;
this_tri->adjacent[0] = new_tri[0];
} else if (num_new_tri == 2) {
// first, which edge has the new_node?
for (h=0; h<3; h++) if (new_node[h]) {
i = h;
j = (h+1)%3;
k = (h+2)%3;
break;
}
// geometry of the two triangles
new_tri[0]->node[0] = this_tri->node[i];
new_tri[0]->node[1] = new_node[i];
new_tri[0]->node[2] = this_tri->node[k];
new_tri[0]->adjacent[0] = NULL;
new_tri[0]->adjacent[1] = new_tri[1];
new_tri[0]->adjacent[2] = NULL;
new_tri[0]->index = tri_cnt++;
new_tri[1]->node[0] = new_node[i];
new_tri[1]->node[1] = this_tri->node[j];
new_tri[1]->node[2] = this_tri->node[k];
new_tri[1]->adjacent[0] = NULL;
new_tri[1]->adjacent[1] = NULL;
new_tri[1]->adjacent[2] = new_tri[0];
new_tri[1]->index = tri_cnt++;
// now, set those adjacent pointers
if (this_tri->adjacent[i] && has_adjacent_been_split[i]) {
if (!find_adjacent_child(this_tri->adjacent[i]->adjacent[0],
new_tri[0],this_tri->node[i],new_node[i]) )
fprintf(stderr,"Could not find adjacent child 1.\n");
if (!find_adjacent_child(this_tri->adjacent[i]->adjacent[0],
new_tri[1],new_node[i],this_tri->node[j]) )
fprintf(stderr,"Could not find adjacent child 2.\n");
}
if (this_tri->adjacent[j] && has_adjacent_been_split[j]) {
fprintf(stderr,"Side j should not have been split!\n");
}
if (this_tri->adjacent[k] && has_adjacent_been_split[k]) {
fprintf(stderr,"Side k should not have been split!\n");
}
// set that other stuff
for (i=0; i<2; i++) for (j=0; j<3; j++) new_tri[i]->midpoint[j] = NULL;
this_tri->adjacent[0] = new_tri[0];
} else { // if num_new_tri==1
// then just pass this tri on to the next stage
// new_tri[0] = alloc_new_tri();
// new_tri[0]->splittable = FALSE;
new_tri[0]->node[0] = this_tri->node[0];
new_tri[0]->node[1] = this_tri->node[1];
new_tri[0]->node[2] = this_tri->node[2];
new_tri[0]->adjacent[0] = NULL;
new_tri[0]->adjacent[1] = NULL;
new_tri[0]->adjacent[2] = NULL;
new_tri[0]->index = tri_cnt++;
// new_tri[0]->next_tri = new_tri_head;
// new_tri_head = new_tri[0];
// this_tri = this_tri->next_tri;
// continue;
}
// add all of the tris to the list
for (i=num_new_tri-1; i>-1; i--) {
new_tri[i]->next_tri = new_tri_head;
new_tri_head = new_tri[i];
}
// and jump to the next parent tri
this_tri = this_tri->next_tri;
continue;
}
//--------------------------------------------------------------------------
// if this is a normal, splittable triangle, create 4 child triangles
//fprintf(stderr,"Splittable tri %d\n",this_tri->index); fflush(stderr);
// choose the three edge split points
for (i=0; i<3; i++) new_node[i] = NULL;
for (i=0; i<3; i++) {
// check to see if a midpoint split already exists, use it if it does
if (this_tri->midpoint[i]) {
has_adjacent_been_split[i] = TRUE;
//fprintf(stderr," already have midpoint on side %d\n",i); fflush(stderr);
new_node[i] = this_tri->midpoint[i];
// if (this_tri->adjacent[i]->splittable) {
// fprintf(stderr," corresponding to splittable tri %d\n",this_tri->adjacent[i]->index); fflush(stderr);
// } else {
// fprintf(stderr," corresponding to unsplittable tri %d\n",this_tri->adjacent[i]->index); fflush(stderr);
// }
} else {
/* if it doesn't, choose one and send the information to the adjacent tri, if there is one */
has_adjacent_been_split[i] = FALSE;
j = mod(i+1,3);
//fprintf(stderr," creating midpoint for side %d\n",i); fflush(stderr);
/* use the better method for determining a new midpoint */
k = mod(j+1,3);
if (!this_tri->adjacent[i]) {
//fprintf(stderr," there is no adjacent tri %d\n"); fflush(stderr);
/* there is no adjacent triangle */
if (clamp_edges) {
/* clamp the edge, use the mathematical midpoint */
new_node[i] = create_midpoint(this_tri->node[i],this_tri->node[j]);
} else {
/* let the edge get jagged, use the current tri's 3 points */
new_node[i] = create_midpoint_3(depth,this_tri->node[i],
this_tri->node[j],
this_tri->node[k]);
}
} else if (this_tri->adjacent[i]->splittable) {
//fprintf(stderr," corresponding to splittable tri %d\n",this_tri->adjacent[i]->index); fflush(stderr);
// there is an adjacent triangle that can be split
// find the index of the adjacent tri's farthest node
for (h=0; h<3; h++) {
if (this_tri->node[i] == this_tri->adjacent[i]->node[h]) {
far_corner = mod(h+1,3);
break;
}
}
new_node[i] = create_midpoint_5(depth,this_tri->node[i],
this_tri->node[j],
this_tri->node[k],
this_tri->adjacent[i]->node[far_corner]);
// find the index of the shared side from the adjacent tri's entry
for (j=0; j<3; j++) {
if (this_tri->node[i] == this_tri->adjacent[i]->node[j]) {
adj_side = mod(j+2,3);
break;
}
}
j = mod(adj_side+1,3);
this_tri->adjacent[i]->midpoint[adj_side] = new_node[i];
//fprintf(stderr," and told adjacent tri %d that its side %d has a midpoint already\n",i,adj_side); fflush(stderr);
} else {
//fprintf(stderr," corresponding to unsplittable tri %d\n",this_tri->adjacent[i]->index); fflush(stderr);
// there is an adjacent triangle, but it's set to not split;
// clamp the edge, use the mathematical midpoint
// new_node[i] = create_midpoint(this_tri->node[i],this_tri->node[j]);
// new method: split that edge normally, anyways! AND tell the
// adjacent tri that we split
// find the index of the adjacent tri's farthest node
for (h=0; h<3; h++) {
if (this_tri->node[i] == this_tri->adjacent[i]->node[h]) {
far_corner = mod(h+1,3);
break;
}
}
new_node[i] = create_midpoint_5(depth,this_tri->node[i],
this_tri->node[j],
this_tri->node[k],
this_tri->adjacent[i]->node[far_corner]);
// find the index of the shared side from the adjacent tri's entry
for (j=0; j<3; j++) {
if (this_tri->node[i] == this_tri->adjacent[i]->node[j]) {
adj_side = mod(j+2,3);
break;
}
}
j = mod(adj_side+1,3);
this_tri->adjacent[i]->midpoint[adj_side] = new_node[i];
}
}
}
// create the four new triangles
for (i=0; i<4; i++) new_tri[i] = alloc_new_tri();
// no change to old method
// by default, set all external adjacent pointers to NULL, fix later
new_tri[0]->node[0] = this_tri->node[0];
new_tri[0]->node[1] = new_node[0];
new_tri[0]->node[2] = new_node[2];
new_tri[0]->adjacent[0] = NULL;
new_tri[0]->adjacent[1] = new_tri[3];
new_tri[0]->adjacent[2] = NULL;
new_tri[0]->index = tri_cnt++;
new_tri[1]->node[0] = new_node[0];
new_tri[1]->node[1] = this_tri->node[1];
new_tri[1]->node[2] = new_node[1];
new_tri[1]->adjacent[0] = NULL;
new_tri[1]->adjacent[1] = NULL;
new_tri[1]->adjacent[2] = new_tri[3];
new_tri[1]->index = tri_cnt++;
new_tri[2]->node[0] = new_node[2];
new_tri[2]->node[1] = new_node[1];
new_tri[2]->node[2] = this_tri->node[2];
new_tri[2]->adjacent[0] = new_tri[3];
new_tri[2]->adjacent[1] = NULL;
new_tri[2]->adjacent[2] = NULL;
new_tri[2]->index = tri_cnt++;
new_tri[3]->node[0] = new_node[1];
new_tri[3]->node[1] = new_node[2];
new_tri[3]->node[2] = new_node[0];
new_tri[3]->adjacent[0] = new_tri[2];
new_tri[3]->adjacent[1] = new_tri[0];
new_tri[3]->adjacent[2] = new_tri[1];
new_tri[3]->index = tri_cnt++;
/* If parent had no adjacent tri, children on that edge have none, either.
* BUT, if the parent DID have an adjacent tri, children will also.
* So, if the parent had an adjacent tri, and it has been split, find the children's
* adjacent tris among the parent's adjacent's children */
/* but what about the actual node locations? NO, this is just setup for the next step */
if (this_tri->adjacent[0] && has_adjacent_been_split[0]) {
/* then use the child tri pointed to by this_tri->adjacent[0]->adjacent[0]
* to begin searching for the triangles adjacent to the 2 new ones on this side */
if (!find_adjacent_child(this_tri->adjacent[0]->adjacent[0],new_tri[0],this_tri->node[0],new_node[0]) )
fprintf(stderr,"Could not find adjacent child 1.\n");
if (!find_adjacent_child(this_tri->adjacent[0]->adjacent[0],new_tri[1],new_node[0],this_tri->node[1]) )
fprintf(stderr,"Could not find adjacent child 2.\n");
}
/* do the same for parent's side 1 */
if (this_tri->adjacent[1] && has_adjacent_been_split[1]) {
if (!find_adjacent_child(this_tri->adjacent[1]->adjacent[0],new_tri[1],this_tri->node[1],new_node[1]) )
fprintf(stderr,"Could not find adjacent child 3.\n");
if (!find_adjacent_child(this_tri->adjacent[1]->adjacent[0],new_tri[2],new_node[1],this_tri->node[2]) )
fprintf(stderr,"Could not find adjacent child 4.\n");
}
/* and for parent's side 2 */
if (this_tri->adjacent[2] && has_adjacent_been_split[2]) {
if (!find_adjacent_child(this_tri->adjacent[2]->adjacent[0],new_tri[2],this_tri->node[2],new_node[2]) )
fprintf(stderr,"Could not find adjacent child 5.\n");
if (!find_adjacent_child(this_tri->adjacent[2]->adjacent[0],new_tri[0],new_node[2],this_tri->node[0]) )
fprintf(stderr,"Could not find adjacent child 6.\n");
}
/* new tri's midpoints will not be set this recursion level, set to NULL */
for (i=0; i<4; i++) for (j=0; j<3; j++) new_tri[i]->midpoint[j] = NULL;
/* calculate triangle's area, if below either threshhold, set it up so it will not split */
for (i=0; i<4; i++) new_tri[i]->splittable = TRUE;
if (use_thresh || use_dist) {
for (i=0; i<4; i++) {
temp_area = find_area(new_tri[i]);
if (use_thresh) {
if (temp_area < area_thresh) {
/* this is a flag to the splitter, do not split further */
new_tri[i]->splittable = FALSE;
}
}
if (use_dist) {
// if (sqrt(temp_area)/length(from(viewp,new_tri[i]->node[0]->loc)) < distance_thresh) {
if (sqrt(temp_area)/find_tri_dist(new_tri[i],viewp) < distance_thresh) {
/* this is a flag to the splitter, do not split further */
new_tri[i]->splittable = FALSE;
}
}
}
}
/* add the 4 new tris to the new list */
new_tri[3]->next_tri = new_tri_head;
new_tri[2]->next_tri = new_tri[3];
new_tri[1]->next_tri = new_tri[2];
new_tri[0]->next_tri = new_tri[1];
new_tri_head = new_tri[0];
/* Important: set this now-split parent triangle's first adjacent pointer to the
* first of the 4 new triangles created, this information will be useful later */
this_tri->adjacent[0] = new_tri[0];
/* we are now done with this parent tri, we may reference it later, though */
this_tri = this_tri->next_tri;
}
// finally, call the sphericalizing routine
if (force_sphere) make_sphere(new_tri_head);
return(new_tri_head);
}
/*
* split_tri_5
*
* Takes a linked list of triangles and splits any number
* of triangles into 4 new triangles.
* It only adds one new node for each edge, regardless of how many triangles
* share that edge.
* All new nodes are placed either at the midpoint of their edges, or
* at a location corresponding to the spline surface through the close
* nodes.
* Only after the new topology is created are the nodes moved from their
* initial positions.
*/
tri_pointer split_tri_5 (int depth, tri_pointer tri_head) {
int h,i,j,k;
int adj_side = -1;
int num_new_tri;
int tri_cnt = 0;
int node_cnt = 0;
int has_adjacent_been_split[3];
double temp_area;
tri_pointer this_tri;
tri_pointer new_tri_head = NULL;
tri_pointer new_tri[4];
node_ptr this_node;
node_ptr new_node[3];
fprintf(stderr,"Method 3, depth = %d\n",depth);
j = -1; k = -1;
// count the nodes (we need this to set the index)
node_cnt = count_nodes();
// first, compute normals for all triangles (use best method)
if (depth == 0) (void) compute_normals_2 (tri_head,3);
// for each triangle in the old list
this_tri = tri_head;
while (this_tri) {
//fprintf(stderr,"Checking tri...%d\n",this_tri->index);