forked from SynapseWeb/Reconstruct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboissonnat.cpp
More file actions
executable file
·3045 lines (2789 loc) · 94 KB
/
boissonnat.cpp
File metadata and controls
executable file
·3045 lines (2789 loc) · 94 KB
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
//////////////////////////////////////////////////////////////////////////////////////
// This is a port of the IGL Trace 3D Delaunay triangulation code for Reconstruct.
//
// Copyright (C) 1996-2006 John Fiala (fiala@bu.edu)
//
// This is free software created with funding from the NIH. You may
// redistribute it and/or modify it under the terms of the GNU General
// Public License published by the Free Software Foundation (www.gnu.org).
//
// 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 version 2 for more details.
//
// modified 07/17/03 by JCF
// -+- change: Replaced old contour classes with new contour objects for Reconstruct.
// modified 02/12/04 by JCF
// -+- change: Reduced limit on contour_complete from 2.0 to 0.001.
// -+- change: Wrote but did not install code for adding self-intersections to contours.
// modified 04/22/04 by JCF
// -+- change: Removed the storage of contours in VRMLObject
// modified 03/17/05 by JCF
// -+- change: Modified add-tetra, Next, and BoissonnatSurface to hide lower and upper faces
// only on the first and last section of the object
// modified 4/14/06 by JCF (fiala@bu.edu)
// -+- change: Creation of 3D slabs output. Removed PlanarContourSurface.
// modified 5/4/06 by JCF (fiala@bu.edu)
// -+- change: Added section-by-section offsets using a difference of Z-traces
// modified 6/22/06 by JCF (fiala@bu.edu)
// -+- change: Changed M_PI constants to PI as defined in constants.h
//
#include "reconstruct.h"
// ************************************************************************************
// 2D Delaunay triangulation and voronoi diagrams from contours
//
// Classes defined:
// index_point,index_point_list
// triangle
// edge, edge_list
// triangle_list
// graph_node,graph_edge,graph
// delaunay_triangulation
//
// Copyright (C) 1996,7
// John C. Fiala
//
#define ORIENTED_UNKNOWN 0 // ORable constants for orientations
#define ORIENTED_INTERNAL 1
#define ORIENTED_BOUNDARY 2
#define ORIENTED_EXTERNAL 4
#define DONE_T1 8
#define DONE_T2 16
//#define RAY_XSMALL -2.0e4 // these constants set the limits of how
//#define RAY_YSMALL -1.6e4 // big an image/object can be handled
//#define RAY_XLARGE 2.0e4
//#define RAY_YLARGE 1.6e4
double RAY_XSMALL, RAY_YSMALL, RAY_XLARGE, RAY_YLARGE;
// ************************************** INDEX POINTS
class index_point { // a 2D point with integer index
public:
int index;
double x, y;
index_point *next;
};
class index_point_list { // linked list of indexed points
public:
index_point *head, *tail; // will add items at the tail
int max_index; // so can be traversed in order
index_point_list() { head = NULL;
tail = NULL;
max_index = 0; }
virtual ~index_point_list();
index_point * add( double x, double y );
void remove( index_point *ip ); // remove ip from list and delete
};
index_point_list::~index_point_list() // destructor
{
index_point *tmp;
tmp = head;
while ( tmp != NULL ) {
head = tmp->next;
delete tmp;
tmp = head;
}
} // add a new index point
index_point * index_point_list::add( double x, double y )
{
index_point *tmp = new index_point; // create new item
tmp->next = NULL; // place at end of list
if ( tail != NULL ) tail->next = tmp;
if ( head == NULL ) head = tmp;
tail = tmp;
tmp->x = x;
tmp->y = y;
max_index++; // assign next available index
tmp->index = max_index; // ASSUMES: will not exceed 32-bit capacity
return tmp; // return ptr to item
}
void index_point_list::remove( index_point *ip )
{
index_point *tmp;
if ( ip == head ) head = ip->next;
else {
tmp = head;
while ( tmp->next != ip ) tmp = tmp->next;
tmp->next = ip->next;
if ( ip == tail ) tail = tmp;
}
delete ip;
}
// *****************************************
// ************************************** DIRECTED TRIANGLES
class triangle {
public:
index_point *v1;
index_point *v2;
index_point *v3;
double xc, yc;
double radius;
int edge12;
int edge23;
int edge31;
int oriented;
triangle *prev, *next;
};
// *****************************************
// ************************************** DIRECTED EDGES
class edge { // segment between two indexed pts
public:
index_point *v1;
index_point *v2;
edge *next;
};
class edge_list { // a list of edges
public:
edge *head;
edge_list() { head = NULL; }
virtual ~edge_list();
void add( index_point *p1, index_point *p2 );
void add( triangle *t ); // add the edges of the triangle
void split( edge *e, index_point *m ); // split e, using midpt m
int orientation( triangle *t ); // -1=>opposite, 0=>unknown, 1=>same
void remove_common(); // remove all occurrences of repeated edges
};
edge_list::~edge_list()
{
edge *tmp;
tmp = head;
while ( tmp != NULL ) {
head = tmp->next;
delete tmp;
tmp = head;
}
}
void edge_list::add(index_point *p1, index_point *p2)
{
edge *tmp;
tmp = new edge;
tmp->next = head;
head = tmp;
tmp->v1 = p1; // can assume edge is directed from v1 -> v2
tmp->v2 = p2;
}
void edge_list::add(triangle *t)
{
edge *tmp;
tmp = new edge; // add first edge of triangle
tmp->next = head;
head = tmp;
tmp->v1 = t->v1;
tmp->v2 = t->v2;
tmp = new edge; // add second edge
tmp->next = head;
head = tmp;
tmp->v1 = t->v2;
tmp->v2 = t->v3;
tmp = new edge; // add third edge
tmp->next = head;
head = tmp;
tmp->v1 = t->v3;
tmp->v2 = t->v1;
}
void edge_list::split(edge *e, index_point *m)
{
edge *tmp;
tmp = new edge;
tmp->next = e->next;
tmp->v1 = e->v1; // edge is directed from v1 -> m
tmp->v2 = m;
e->next = tmp;
e->v1 = m; // edge is directed from m -> v2
}
int edge_list::orientation( triangle *t ) // orientation of triangle wrt ordered
{ // edge list; return <0 if external,
edge *e1, *e2; // return >0 if internal,
int i, j, orient; // 0 => not determinable from data
double dvx, dvy, dx1, dx2, dy1, dy2;
double c, s, a1, a2;
bool found;
orient = 0;
for (j=1; j<=3; j++) { // do each vertex of t, sum results
if ( j ==1 ) {
i = t->v1->index; // set vertex & direction of edge
dvx = t->v2->x - t->v1->x;
dvy = t->v2->y - t->v1->y;
}
if ( j == 2 ) {
i = t->v2->index;
dvx = t->v3->x - t->v2->x;
dvy = t->v3->y - t->v2->y;
}
if ( j == 3 ) {
i = t->v3->index;
dvx = t->v1->x - t->v3->x;
dvy = t->v1->y - t->v3->y;
}
found = false; // search for matching contour vertex
e2 = head;
while ( (e2 != NULL) && (!found) ) {
if ( e2->v1->index == i ) found = true;
else e2 = e2->next;
}
if ( found ) { // e2:v2-<-v1 == t:vi == e1:v2-<-v1
e1 = e2->next;
if ( e1 == NULL ) e1 = head; // contour edge list is circular
dx1 = e1->v1->x - e1->v2->x; // | /
dy1 = e1->v1->y - e1->v2->y; // e2| /v
dx2 = e2->v2->x - e2->v1->x; // __e1__|/
dy2 = e2->v2->y - e2->v1->y; //
c = (dx1*dx2 + dy1*dy2); // |e1||e2|cos(a1) = (-e1 . e2)
s = (dx1*dy2 - dx2*dy1); // |e1||e2|sin(a1) = (-e1 x e2)
if ( (s != 0.0) || (c != 0.0) ) a2 = atan2(s,c);
else a2 = 0.0;
if ( a2 < 0.0 ) a2 = 2.0*PI + a2;
c = (dx1*dvx + dy1*dvy); // |e1||v|cos(a2) = (-e1 . v)
s = (dx1*dvy - dvx*dy1); // |e1||v|sin(a2) = (-e1 x v)
if ( (s != 0.0) || (c != 0.0) ) a1 = atan2(s,c);
else a1 = 0.0;
if ( a1 < 0.0 ) a1 = 2.0*PI + a1;
if ( a1 < a2 ) orient += -1; // external (left-handed coord sys)
if ( a1 > a2 ) orient += 1; // internal
}
}
return orient;
}
void edge_list::remove_common()
{
edge *d, *e, *f, *g; // list is ... d -> e -> ... f -> g ...
bool repeated, matched;
// search each list item
d = NULL;
e = head;
while ( e != NULL ) {
repeated = false;
f = e;
g = e->next; // for repetitions in remainder of list
while ( g != NULL ) {
matched = false;
if ( e->v1->index == g->v1->index ) {
if ( e->v2->index == g->v2->index ) matched = true;
}
else if ( e->v2->index == g->v1->index ) {
if ( e->v1->index == g->v2->index ) matched = true;
}
if ( matched ) { // then delete g and will
repeated = true; // need to delete e also
f->next = g->next;
delete g;
g = f->next;
}
else { // otherwise check next list item
f = g;
g = g->next;
}
}
if ( repeated ) { // then delete e and continue
if ( d == NULL ) { // deleting head of list...
head = e->next;
delete e;
e = head;
}
else { // reconnect d to rest of list
d->next = e->next;
delete e;
e = d->next;
}
}
else { // otherwise check next list item
d = e;
e = e->next;
}
// end while ( e != ...
}
}
// ************************************** TRIANGLES (LISTS)
class triangle_item { // a linkable triangle ptr
public:
triangle *tri;
triangle_item *next;
};
class triangle_list { // a list of triangle pointers
public:
triangle_item *head;
triangle_list() { head = NULL; }
virtual ~triangle_list();
void add( triangle *t );
};
triangle_list::~triangle_list()
{
triangle_item *tmp;
tmp = head;
while ( tmp != NULL ) {
head = tmp->next;
delete tmp;
tmp = head;
}
}
void triangle_list::add(triangle *t)
{
triangle_item *tmp;
tmp = new triangle_item;
tmp->next = head;
head = tmp;
tmp->tri = t;
}
// ******************************************
// *************************************** GRAPHS
class graph_node; // declare class name only, will finish declaration below
class graph_edge {
public:
int orientation;
double dx, dy; // direction of edge, dy/dx is slope of line from n1 to n2
double minx, maxx, miny, maxy; // bounding RPP of edge segment
bool n1_dual_12; // n1 duality indicators
bool n1_dual_23;
bool n1_dual_31;
graph_node *n1; // these are the endpoints, if n2==NULL then infinite
graph_node *n2;
bool n2_dual_12; // n2 duality indicators
bool n2_dual_23;
bool n2_dual_31;
graph_edge *next;
};
class graph_edge_ptr { // to make a list of pointers to edges in main list
public:
graph_edge *edge_ptr;
graph_edge_ptr *next;
};
class graph_node_ptr { // to make a list of pointers to nodes in main list
public:
graph_node *node_ptr;
graph_node_ptr *next;
};
class graph_node { // a node of a graph (May not need full graph at all for reconstruction)
public:
index_point *at; // location, NULL if none
triangle *tri; // dual in delaunay triangulation or NULL
graph_edge_ptr *neighbors; // nodes connected to this one by edges
graph_node *next,*prev;
graph_node() { neighbors = NULL;
tri = NULL;
at = NULL;
next = NULL;
prev = NULL;
}
virtual ~graph_node();
void add_neighbor(graph_edge *n);
void remove_neighbor(graph_edge *e);
};
graph_node::~graph_node()
{
graph_edge_ptr *tmp;
tmp = neighbors;
while ( tmp != NULL ) {
neighbors = tmp->next;
delete tmp;
tmp = neighbors;
}
}
void graph_node::add_neighbor(graph_edge *n)
{
graph_edge_ptr *tmp;
tmp = new graph_edge_ptr;
tmp->next = neighbors;
neighbors = tmp;
tmp->edge_ptr = n;
}
void graph_node::remove_neighbor(graph_edge *e) // remove *e from neighbors list
{
graph_edge_ptr *p, *n;
p = NULL;
n = neighbors;
while ( (n != NULL) && (n->edge_ptr != e) ) { // look for match
p = n;
n = n->next;
}
if ( n != NULL ) { // found, so delete it
if ( n == neighbors ) neighbors = n->next;
else p->next = n->next;
delete n;
}
}
class graph { // a generic graph of 2D indexed points
public:
index_point_list *locations;
graph_node *head, *tail;
graph_edge *edges;
graph() { head = NULL;
tail = NULL;
edges = NULL;
locations = new index_point_list;
};
virtual ~graph();
graph_node * add_node();
graph_edge * add_edge(graph_node *n1, graph_node *n2);
};
graph::~graph()
{
graph_node *tmp;
graph_edge *e;
tmp = head;
while ( tmp != NULL ) {
head = tmp->next;
delete tmp;
tmp = head;
}
e = edges;
while ( e != NULL ) {
edges = e->next;
delete e;
e = edges;
}
delete locations;
}
graph_node * graph::add_node()
{
graph_node *tmp;
tmp = new graph_node; // place new node at head
if ( head == NULL ) {
head = tmp;
tail = tmp;
}
else {
tmp->next = head;
head->prev = tmp;
head = tmp;
}
return tmp;
}
graph_edge * graph::add_edge(graph_node *n1, graph_node *n2)
// if edge exists, return pointer to it
{ // otherwise create new item and return
graph_edge *tmp;
tmp = NULL;
if ( n2 != NULL ) { // always create in infinite edge case
tmp = edges;
while ( tmp != NULL ) { // look for match
if ( ((tmp->n1 == n1) && (tmp->n2 == n2))
|| ((tmp->n2 == n1) && (tmp->n1 == n2)) ) break;
tmp = tmp->next;
}
}
if ( tmp == NULL ) { // not found, so create a new one
tmp = new graph_edge;
tmp->next = edges;
edges = tmp;
tmp->n1 = n1; // set endpoint node pointers
tmp->n2 = n2;
tmp->orientation = ORIENTED_UNKNOWN; // set flags
tmp->n1_dual_12 = false;
tmp->n1_dual_23 = false;
tmp->n1_dual_31 = false;
tmp->n2_dual_12 = false;
tmp->n2_dual_23 = false;
tmp->n2_dual_31 = false; // finally set bounding RPP
if ( n2 != NULL ) {
if ( n2->at->x > n1->at->x ) { // use 2nd endpt for max x
tmp->minx = n1->at->x;
tmp->maxx = n2->at->x;
}
else { // otherwise use 1st endpt
tmp->minx = n2->at->x;
tmp->maxx = n1->at->x;
}
if ( n2->at->y > n1->at->y ) { // similarly for y
tmp->miny = n1->at->y;
tmp->maxy = n2->at->y;
}
else {
tmp->miny = n2->at->y;
tmp->maxy = n1->at->y;
}
}
else { // edge is an infinite ray starting a 1st endpt
tmp->minx = n1->at->x; // set correct maximums later...
tmp->maxx = n1->at->x;
tmp->miny = n1->at->y;
tmp->maxy = n1->at->y;
}
}
return tmp; // return pointer
}
// *****************************************
int tris( triangle *head )
{
int i = 0;
while ( head != NULL ) { i++; head = head->next; }
return( i );
}
// ************************************** DELAUNAY TRIANGULATION OF 2D POINTS
class delaunay_triangulation { // contains a dbly-linked list of triangles
index_point *bounding_box_pts; // ptr to first (of 4) box index pts
edge_list *contour_edges; // contour segments in term of DT edges
public:
index_point_list *pts; // where this DT lies in larger vertex lists
graph *voronoi; // ptr to voronoi graph if present
int index_offset; // an offset of indices to other indices
triangle *head, *tail; // constructor needs initial enclosing box
delaunay_triangulation();
virtual ~delaunay_triangulation(); // destructor frees data pts
void add( index_point *v1, index_point *v2, index_point *v3 );
void remove( triangle *t );
void remove_bounding_box(); // remove the initial enclosing box
index_point * insert( double x, double y ); // add (x,y) to DT; return index
void add_contour_intersections(); // add any self-intersections (eg. overlaps)
bool contour_complete(); // make sure that the contour edges are covered
void mark_external(); // label triangles that are outside contour
void triangulate( Contour *acontour, double shiftx = 0.0, double shifty = 0.0 );
void create_voronoi_graph(); // create voronoi graph of triangulation
};
delaunay_triangulation::~delaunay_triangulation()
{
triangle *tmp;
while ( head != NULL ) {
tmp = tail;
tail = tail->prev;
if ( tmp == head ) head = NULL;
delete tmp;
}
delete contour_edges;
delete pts;
if ( voronoi != NULL ) delete voronoi;
}
delaunay_triangulation::delaunay_triangulation()
{
index_point *v1, *v2, *v3, *v4;
head = NULL; // initialize data structures
tail = NULL;
voronoi = NULL; // no voronoi graph initially
pts = new index_point_list;
contour_edges = new edge_list;
// use large bounding box so that can be
// removed later without affecting DT
v1 = pts->add(RAY_XSMALL,RAY_YSMALL);
bounding_box_pts = v1;
v2 = pts->add(RAY_XLARGE,RAY_YSMALL);
v3 = pts->add(RAY_XLARGE,RAY_YLARGE);
v4 = pts->add(RAY_XSMALL,RAY_YLARGE);
// construct enclosing complex
add( v1, v2, v3 );
add( v3, v4, v1 );
}
void delaunay_triangulation::add( index_point *v1, index_point *v2, index_point *v3 )
{
triangle *tmp;
double dx21, dy21, dx31, dy31, p21, p31, d;
// compute difference vectors
dx21 = v2->x - v1->x;
dy21 = v2->y - v1->y;
dx31 = v3->x - v1->x;
dy31 = v3->y - v1->y;
d = dx21*dy31 - dx31*dy21; // cross product gives area
if ( d == 0.0 ) { // don't add flat triangle
return;
}
tmp = new triangle; // create triangle item
tmp->prev = tail;
tmp->next = NULL;
if ( head == NULL ) head = tmp;
else tail->next = tmp;
tail = tmp;
if ( d < 0.0 ) { // add pts to triangle
tmp->v1 = v1; // in clockwise order
tmp->v2 = v3;
tmp->v3 = v2;
}
else {
tmp->v1 = v1;
tmp->v2 = v2;
tmp->v3 = v3;
}
tmp->oriented = ORIENTED_UNKNOWN;
tmp->edge12 = ORIENTED_UNKNOWN;
tmp->edge23 = ORIENTED_UNKNOWN;
tmp->edge31 = ORIENTED_UNKNOWN;
p21 = dx21*(v2->x+v1->x)/2.0 + dy21*(v2->y+v1->y)/2.0;
p31 = dx31*(v3->x+v1->x)/2.0 + dy31*(v3->y+v1->y)/2.0;
tmp->xc = ( p21*dy31 - p31*dy21 )/d;
tmp->yc = ( dx21*p31 - dx31*p21 )/d;
tmp->radius = sqrt( (v1->x-tmp->xc)*(v1->x-tmp->xc) + (v1->y-tmp->yc)*(v1->y-tmp->yc) );
}
void delaunay_triangulation::remove(triangle *t)
{
if ( t == head )
head = t->next;
else t->prev->next = t->next;
if ( t == tail )
tail = t->prev;
else t->next->prev = t->prev;
delete t;
}
void delaunay_triangulation::remove_bounding_box() // call this once after all
{ // contour points are inserted
int i, idx;
triangle *t;
index_point *tmp;
triangle_item *it;
triangle_list *withvertex;
for (i=1; i<=4; i++) { // remove triangles assoc. w/1st 4 index points
if ( bounding_box_pts != NULL ) {
withvertex = new triangle_list;
idx = bounding_box_pts->index;
t = head; // search tri list for this index value
while ( t != NULL ) {
if ( (t->v1->index == idx) || (t->v2->index == idx) || (t->v3->index == idx) )
withvertex->add( t ); // add it to list to remove
t = t->next;
}
it = withvertex->head; // remove triangles from triangulation
while ( it != NULL ) {
remove( it->tri );
it = it->next;
}
delete withvertex; // delete list of old triangles
tmp = bounding_box_pts;
bounding_box_pts = bounding_box_pts->next; // do next index point
pts->remove( tmp );
}
} // end for (i...
}
index_point * delaunay_triangulation::insert(double x, double y)
{
edge *e; // insert (x,y) into DT and
triangle *t; // return ptr to its indexed point;
triangle_item *it; // if (x,y) already in DT,
index_point *p; // just return ptr to indexed point
double d;
//int i ,j;
//char txt[80];
//i = 0; j = 0;
p = pts->head; // search pts list for this point...
while ( p != NULL ) {
d = fabs(p->x-x) + fabs(p->y-y);
if ( d == 0.0 ) { // JCF 7/17/03 not all contour pts are integers!
break;
}
else p = p->next;
}
if ( p == NULL ) { // not found!
p = pts->add(x,y); // create and insert new index point
triangle_list *intersected = new triangle_list;
edge_list *edges = new edge_list;
t = head; // search for circum circles containing p
while ( t != NULL ) {
d = sqrt( (p->x-t->xc)*(p->x-t->xc) + (p->y-t->yc)*(p->y-t->yc) );
if ( t->radius > d ) {
intersected->add( t ); // add triangles intersected to list
edges->add( t ); // add edges of triangle to edge list
}
t = t->next;
}
edges->remove_common(); // remove edges which are repeated
it = intersected->head; // remove old triangles from triangulation
while ( it != NULL ) {
remove( it->tri );
it = it->next;
//j++;
}
e = edges->head; // add new triangles made by p and edges
while ( e != NULL ) {
add( e->v1, e->v2, p );
e = e->next;
//i++;
}
delete intersected; // cleanup temp space
delete edges;
}
return( p ); // return ptr to index pt for (x,y)
}
void delaunay_triangulation::add_contour_intersections()
{
bool found, in_e, in_f;
double d, x, y, edx, fdx, edy, fdy, eminx, emaxx, eminy, emaxy, fminx, fmaxx, fminy, fmaxy;
index_point *p;
edge *e, *f;
// split edges which intersect
e = contour_edges->head;
while ( e != NULL )
{
eminx = e->v1->x;
emaxx = e->v2->x;
if ( emaxx < eminx ) { x = eminx; eminx = emaxx; emaxx = x; }
eminy = e->v1->y;
emaxy = e->v2->y;
if ( emaxy < eminy ) { y = eminy; eminy = emaxy; emaxy = y; }
edx = e->v2->x - e->v1->x;
edy = e->v2->y - e->v1->y;
f = contour_edges->head;
while ( f != NULL )
{
fminx = f->v1->x;
fmaxx = f->v2->x;
if ( fmaxx < fminx ) { x = fminx; fminx = fmaxx; fmaxx = x; }
fminy = f->v1->y;
fmaxy = f->v2->y;
if ( fmaxy < fminy ) { y = fminy; fminy = fmaxy; fmaxy = y; }
found = false;
if ( (e != f) && (fminx < emaxx) && (fmaxx > eminx) && (fminy < emaxy) && (fmaxy > eminy) )
{
fdx = f->v2->x - f->v1->x; // RPPs overlap, check for detailed intersection
fdy = f->v2->y - f->v1->y;
if ( (edx != 0.0) && (fdx != 0.0) )
{
d = fdy/fdx - edy/edx; // y - y1 = (dy1/dx1)(x - x1)
if ( d != 0.0 ) // y - y2 = (dy2/dx2)(x - x2)
{
x = (f->v1->x*fdy/fdx - e->v1->x*edy/edx + e->v1->y - f->v1->y)/d;
y = e->v1->y + (x-e->v1->x)*edy/edx;
found = true;
}
}
else if ( (edx == 0.0) && (fdx != 0.0) ) // e is vertical
{
x = e->v1->x; // x is constant on e
y = fdy*(x - f->v1->x)/fdx + f->v1->y;
found = true;
}
else if ( (edx != 0.0) && (fdx == 0.0) ) // f is vertical
{
x = f->v1->x; // x if constant on f
y = edy*(x - e->v1->x)/edx + e->v1->y;
found = true;
}
if ( found )
{
in_e = false; // check if (x,y) is inside e's endpoints
if ( edx == 0.0 )
{
if ( (y>eminy) && (y<emaxy) ) in_e = true;
}
else if ( edy == 0.0 )
{
if ( (x>eminx) && (x<emaxx) ) in_e = true;
}
else if ( (x<emaxx) && (x>eminx) && (y<emaxy) && (y>eminy) ) in_e = true;
in_f = false; // check if (x,y) is inside e's endpoints
if ( fdx == 0.0 )
{
if ( (y>fminy) && (y<fmaxy) ) in_f = true;
}
else if ( fdy == 0.0 )
{
if ( (x>fminx) && (x<fmaxx) ) in_f = true;
}
else if ( (x<fmaxx) && (x>fminx) && (y<fmaxy) && (y>fminy) ) in_f = true;
if ( in_e && in_f )
{
p = insert(x,y); // add intersection point to triangulation
contour_edges->split(e,p); // split contour segment e
contour_edges->split(f,p); // split contour segment f
}
}
}
f = f->next; // check rest of edges against this e
}
e = e->next; // test next e edge in list
}
}
bool delaunay_triangulation::contour_complete()
{
bool found, changed;
double epsilon = 0.001; // JCF 2/12/04 changed to reflect
double mx, my;
index_point *p;
edge *e;
triangle *t;
changed = false; // split edges which are not in triangulation
e = contour_edges->head; // ASSUMES: contours are all clockwise!!!
while ( e != NULL ) {
t = head; // search for this edge in triangulation
found = false;
while ( (t != NULL ) ) {
if ( e->v1->index == t->v1->index ) {
if ( e->v2->index == t->v2->index ) {
found = true;
t->edge12 |= (ORIENTED_BOUNDARY | ORIENTED_INTERNAL);
}
else if ( e->v2->index == t->v3->index ) {
found = true;
t->edge31 |= (ORIENTED_BOUNDARY | ORIENTED_EXTERNAL);
}
}
else if ( e->v1->index == t->v2->index ) {
if ( e->v2->index == t->v3->index ) {
found = true;
t->edge23 |= (ORIENTED_BOUNDARY | ORIENTED_INTERNAL);
}
else if ( e->v2->index == t->v1->index ) {
found = true;
t->edge12 |= (ORIENTED_BOUNDARY | ORIENTED_EXTERNAL);
}
}
else if ( e->v1->index == t->v3->index ) {
if ( e->v2->index == t->v1->index ) {
found = true;
t->edge31 |= (ORIENTED_BOUNDARY | ORIENTED_INTERNAL);
}
else if ( e->v2->index == t->v2->index ) {
found = true;
t->edge23 |= (ORIENTED_BOUNDARY | ORIENTED_EXTERNAL);
}
}
t = t->next;
}
// split non-Delaunay edges
if ( !found ) {
if ( (fabs(e->v1->x-e->v2->x) > epsilon) || (fabs(e->v1->y-e->v2->y) > epsilon) ) {
mx = (e->v1->x + e->v2->x)/2.0; // compute mid-point
my = (e->v1->y + e->v2->y)/2.0;
p = insert(mx,my); // midpt insertion into DT
contour_edges->split(e,p); // split contour segment
changed = true;
e = contour_edges->head; // now start over to find new problems
}
else {
e = e->next; // give up if segments get too small
}
}
else e = e->next; // go to next edge in list
}
return changed;
}
void delaunay_triangulation::mark_external()
{ // indicate which triangles are outside contour
int i;
triangle *t;
t = head; // mark each triangle internal (external) if contains
while ( t != NULL ) { // an unambiguously internal (external) edge
if ( (t->edge12 & ORIENTED_INTERNAL) && !(t->edge12 & ORIENTED_EXTERNAL) )
t->oriented = ORIENTED_INTERNAL;
else
if ( (t->edge23 & ORIENTED_INTERNAL) && !(t->edge23 & ORIENTED_EXTERNAL) )
t->oriented = ORIENTED_INTERNAL;
else
if ( (t->edge31 & ORIENTED_INTERNAL) && !(t->edge31 & ORIENTED_EXTERNAL) )
t->oriented = ORIENTED_INTERNAL;
if ( (t->edge12 & ORIENTED_EXTERNAL) && !(t->edge12 & ORIENTED_INTERNAL) )
t->oriented = ORIENTED_EXTERNAL;
else
if ( (t->edge23 & ORIENTED_EXTERNAL) && !(t->edge23 & ORIENTED_INTERNAL) )
t->oriented = ORIENTED_EXTERNAL;
else
if ( (t->edge31 & ORIENTED_EXTERNAL) && !(t->edge31 & ORIENTED_INTERNAL) )
t->oriented = ORIENTED_EXTERNAL;
if ( t->oriented == ORIENTED_UNKNOWN ) { // only vertices on contour, no edges
i = contour_edges->orientation( t );
if ( i < 0 ) {
t->oriented = ORIENTED_EXTERNAL;
t->edge12 = ORIENTED_EXTERNAL;
t->edge23 = ORIENTED_EXTERNAL;
t->edge31 = ORIENTED_EXTERNAL;
}
else if (i > 0 ) {
t->oriented = ORIENTED_INTERNAL;
t->edge12 = ORIENTED_INTERNAL;
t->edge23 = ORIENTED_INTERNAL;
t->edge31 = ORIENTED_INTERNAL;
}
} // use oriented to classify unknown edges
if ( t->edge12 == ORIENTED_UNKNOWN ) t->edge12 = t->oriented;