forked from jontodd/r.refine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtin.c
2032 lines (1749 loc) · 51.9 KB
/
tin.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
/* ************************************************************
*
* MODULE: r.refine
*
* Authors: Jon Todd <[email protected]>, Laura Toma <[email protected]>
* Bowdoin College, USA
*
* Purpose: convert grid data to TIN
*
* COPYRIGHT:
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*
************************************************************ */
/******************************************************************************
*
* tin.c handles all creation,manipulation, and output of TIN and
* TIN_TIlE structure
*
* AUTHOR(S): Jonathan Todd - <[email protected]>
*
* UPDATED: jt 2005-08-11
*
* COMMENTS:
*
*****************************************************************************/
#include "tin.h"
// Debug mode
#define DEBUG if(0)
// dummy pointer for all triangles with maxE < e
R_POINT DONEVar;
R_POINT *DONE = &DONEVar;
//
// add trinagle to a TIN and return pointer to it
//
TRIANGLE* addTri(TIN_TILE *tt, R_POINT *p1, R_POINT *p2, R_POINT *p3,
TRIANGLE *t12, TRIANGLE *t13, TRIANGLE *t23) {
assert(p1 && p2 && p3 && tt && tt->pq);
// Validate that the triangle is not collinear
assert(areaSign(p1,p2,p3));
// Validate that all points should be in this tile
assert(pointInTile(p1,tt) && pointInTile(p2,tt) && pointInTile(p3,tt));
// Allocate space for this triangle and give it values
TRIANGLE *tn;
tn = (TRIANGLE*)malloc(sizeof(TRIANGLE));
assert(tn);
// Assign values to the triangle
tn->maxE = NULL;
tn->points = NULL;
tn->pqIndex = UINT_MAX;
tn->p1=p1;
tn->p2=p2;
tn->p3=p3;
tn->p1p2 = t12;
tn->p1p3 = t13;
tn->p2p3 = t23;
// point neighbor triangles to this triangle if not on boundary
if(!edgeOnBoundary(p1,p2,tt))
pointNeighborTriTo(tn,p1,p2,t12);
if(!edgeOnBoundary(p1,p3,tt))
pointNeighborTriTo(tn,p1,p3,t13);
if(!edgeOnBoundary(p2,p3,tt))
pointNeighborTriTo(tn,p2,p3,t23);
return tn;
}
//
// Given a triangle and 2 pts return the neighbor triangle to that edge
//
TRIANGLE* whichTri(TRIANGLE *tn,R_POINT *pa,R_POINT *pb,TIN_TILE *tt){
assert(pa && pb);
// If we are on the boundary then our neighbor is assumed NULL
if(edgeOnBoundary(pa,pb,tt))
return NULL;
assert(tn);
if( (tn->p1 == pa || tn->p2 == pa) && (tn->p1 == pb || tn->p2 == pb) ){
assert(tn->p1p2 != tn);
return tn->p1p2;
}
if( (tn->p1 == pa || tn->p3 == pa) && (tn->p1 == pb || tn->p3 == pb) ){
assert(tn->p1p3 != tn);
return tn->p1p3;
}
else if ( (tn->p2 == pa || tn->p3 == pa) && (tn->p2 == pb || tn->p3 == pb) ){
assert(tn->p2p3 != tn);
return tn->p2p3;
}
else {
assert(0);
exit(1);
return NULL;
}
}
//
// Which of p1-p3 is same as pa
//
R_POINT* whichPoint(R_POINT *pa, R_POINT *p1, R_POINT *p2, R_POINT *p3){
if(pa->x == p1->x && pa->y == p1->y)
return p1;
else if(pa->x == p2->x && pa->y == p2->y)
return p2;
else{
assert(pa->x == p3->x && pa->y == p3->y);
return p3;
}
}
//
// Find neighbor (t) to tn and point proper edge to t.
//
void pointNeighborTriTo(TRIANGLE *t,R_POINT *pa,R_POINT *pb,TRIANGLE *tn){
assert(t || t == NULL);
assert(t != tn || (t == NULL && tn == NULL));
if(tn != NULL){
if( (tn->p1 == pa || tn->p2 == pa) && (tn->p1 == pb || tn->p2 == pb) )
tn->p1p2 = t;
else if( (tn->p1 == pa || tn->p3 == pa) && (tn->p1 == pb || tn->p3 == pb) )
tn->p1p3 = t;
else if ( (tn->p2 == pa || tn->p3 == pa) && (tn->p2 == pb || tn->p3 == pb))
tn->p2p3 = t;
else{
assert(0);
exit(1);
}
}
}
//
// Print the triangles in a TIN_TILE for debugging
//
void printTinTile(TIN_TILE* tinTile){
TRIANGLE *curT = tinTile->t;
TRIANGLE *prevT = curT;
// Create an edge which
EDGE curE;
curE.type = IN;
curE.t1 = NULL;
curE.t2 = tinTile->t;
curE.p1 = tinTile->e.p1;
curE.p2 = tinTile->e.p2;
printf("\n\n PRINTING TIN_TILE \n\n");
do{
// Print tri if we are on its IN edge
if(curE.type == IN && prevT != NULL){
printTriangle(prevT);
}
// Go to next edge
prevT=curT;
curT = nextEdge(curT,tinTile->v,&curE,tinTile);
assert(curT);
}while((curE.p1 != tinTile->e.p1 || curE.p2 != tinTile->e.p2) &&
(curE.p1 != tinTile->e.p2 || curE.p2 != tinTile->e.p1));
}
//
// Print a tin list for debugging
//
void printTin(TIN *tin){
TIN_TILE *tt;
tt = tin->tt;
// Skip dummy head
tt = tt->next;
while(tt->next != NULL){
printf("\n\n Print TNODE iOffSet: %d jOffSet: %d \n\n",
tt->iOffset,tt->jOffset);
printTinTile(tt);
tt = tt->next;
}
}
//
// Print the points of a triangle and pointers for debugging
//
void printTriangle(TRIANGLE* t){
printf("Tri: t:%p Points: %p(%d,%d) %p(%d,%d) %p(%d,%d) Tris: %p %p %p\n",
t,t->p1,t->p1->x,t->p1->y,t->p2,t->p2->x,t->p2->y,t->p3,t->p3->x,
t->p3->y,t->p1p2,t->p1p3, t->p2p3);
//printPointList(t);
}
//
// Print triangle coordiantes
//
void printTriangleCoords(TRIANGLE* t){
char str[100];
sprintf(str,"Triangle t: Points: (%s,%s,%s) (%s,%s,%s) (%s,%s,%s)\n",
COORD_TYPE_PRINT_CHAR,COORD_TYPE_PRINT_CHAR,ELEV_TYPE_PRINT_CHAR,
COORD_TYPE_PRINT_CHAR,COORD_TYPE_PRINT_CHAR,ELEV_TYPE_PRINT_CHAR,
COORD_TYPE_PRINT_CHAR,COORD_TYPE_PRINT_CHAR,ELEV_TYPE_PRINT_CHAR);
printf(str,t->p1->x,t->p1->y,t->p1->z, t->p2->x,t->p2->y,t->p2->z,
t->p3->x, t->p3->y,t->p3->z);
}
//
// Returns TRUE if is in tile otherwise it returns the direction of
// the tile it is in
//
TIN_TILE *whichTileTri(R_POINT *p1, R_POINT *p2, R_POINT *p3, TIN_TILE *tt){
// If there is a point below tt
if(p1->x < tt->iOffset ||
p2->x < tt->iOffset ||
p3->x < tt->iOffset)
return tt->top;
// If there is a point left of tt
else if( p1->y < tt->jOffset ||
p2->y < tt->jOffset ||
p3->y < tt->jOffset )
return tt->left;
// If there is a point right of tt
else if( p1->y >= (tt->jOffset + tt->ncols) ||
p2->y >= (tt->jOffset + tt->ncols) ||
p3->y >= (tt->jOffset + tt->ncols) )
return tt->right;
// If there is a point above tt
else if( p1->x >= (tt->iOffset + tt->nrows) ||
p2->x >= (tt->iOffset + tt->nrows) ||
p3->x >= (tt->iOffset + tt->nrows) )
return tt->bottom;
else
return tt;
}
//
// Is a point on the boundary of its tile?
//
short pointOnBoundary(R_POINT *p, TIN_TILE *tt){
if(p->x == tt->iOffset ||
p->y == tt->jOffset ||
p->y == (tt->jOffset + tt->ncols-1) ||
p->x == (tt->iOffset + tt->nrows-1) )
return TRUE;
else
return FALSE;
}
//
// Is the point in the given tile?
//
short pointInTile(R_POINT *p, TIN_TILE *tt){
if((p->x >= tt->iOffset && p->x <= (tt->iOffset + tt->nrows-1)) &&
(p->y >= tt->jOffset && p->y <= (tt->jOffset + tt->ncols-1)) )
return TRUE;
else
return FALSE;
}
//
// Is the triangle in the given tile
//
short triangleInTile(TRIANGLE *t, TIN_TILE *tt){
if(pointInTile(t->p1,tt) && pointInTile(t->p2,tt) && pointInTile(t->p3,tt))
return TRUE;
else
return FALSE;
}
//
// Does the triangle have an edge on the boundary of a given tile?
//
short triOnBoundary(R_POINT *p1, R_POINT *p2, R_POINT *p3, TIN_TILE *tt){
short ob1 = pointOnBoundary(p1,tt);
short ob2 = pointOnBoundary(p2,tt);
short ob3 = pointOnBoundary(p3,tt);
if( (ob1 && ob2) || (ob1 && ob3) || (ob2 && ob3) )
return TRUE;
else
return FALSE;
}
//
// Is an edge on the boundary?
//
short edgeOnBoundary(R_POINT *p1, R_POINT *p2, TIN_TILE *tt){
// An edge is on the boundary if both points are on a boundary and
// if both points are on the SAME boundary
// Both Points are on the top boundary
if(p1->x == tt->iOffset && p2->x == tt->iOffset)
return TRUE;
// Both Points are on the bottom boundary
if(p1->x == (tt->iOffset + tt->nrows-1) &&
p2->x == (tt->iOffset + tt->nrows-1))
return TRUE;
// Both Points are on the left boundary
if(p1->y == tt->jOffset && p2->y == tt->jOffset)
return TRUE;
// Both Points are on the right boundary
if( p1->y == (tt->jOffset + tt->ncols-1) &&
p2->y == (tt->jOffset + tt->ncols-1))
return TRUE;
return FALSE;
}
//
// This can be used to traverse the tin. Since this will cross every
// triangle 3 times, it is is important to only use next tri when the
// returned edge is of type IN
//
TRIANGLE *nextEdge(TRIANGLE *t, R_POINT *v, EDGE *edge, TIN_TILE *tt){
assert(t && v && edge && edge->p1 && edge->p2);
EDGE e12, e13, e23;
short inCount,outCount;
int areaOp, areaV;
EDGE *in, *out, *inback, *outback;
in = out = inback = outback = NULL;
inCount = outCount = 1;
//
// Define the three edges of this triangle to work with them. If t1
// is NULL then t1 should point to this triangle since the traversal
// calls for boundary edges to point to the triangle they came
// from. Some boundary points may not be null so we need to check
// for them specifically.
//
if(t->p1p2 == NULL || edgeOnBoundary(t->p1,t->p2,tt) ) //
e12.t1 = t;
else
e12.t1 = t->p1p2;
e12.t2 = t;
e12.p1 = t->p1;
e12.p2 = t->p2;
if(t->p1p3 == NULL || edgeOnBoundary(t->p1,t->p3,tt)) //
e13.t1 = t;
else
e13.t1 = t->p1p3;
e13.t2 = t;
e13.p1 = t->p1;
e13.p2 = t->p3;
if(t->p2p3 == NULL || edgeOnBoundary(t->p2,t->p3,tt) ) //
e23.t1 = t;
else
e23.t1 = t->p2p3;
e23.t2 = t;
e23.p1 = t->p2;
e23.p2 = t->p3;
//
// This code figures out which edges are IN and which are OUT. It
// assumes that if areaV is 0 (collinear) then the edge is IN, we
// will handle this later in the next section of code
//
// Edge p1,p2
areaOp = areaSign(t->p1,t->p2,t->p3);
areaV = areaSign(t->p1,t->p2,v);
if(areaOp > 0 && areaV <= 0)
e12.type = IN;
else if(areaOp < 0 && areaV >= 0 )
e12.type = IN;
else
e12.type = OUT;
// Edge p1,p3
areaOp = areaSign(t->p1,t->p3,t->p2);
areaV = areaSign(t->p1,t->p3,v);
if(areaOp > 0 && areaV <= 0)
e13.type = IN;
else if(areaOp < 0 && areaV >= 0 )
e13.type = IN;
else
e13.type = OUT;
// Edge p2,p3
areaOp = areaSign(t->p2,t->p3,t->p1);
areaV = areaSign(t->p2,t->p3,v);
if(areaOp > 0 && areaV <= 0)
e23.type = IN;
else if(areaOp < 0 && areaV >= 0 )
e23.type = IN;
else
e23.type = OUT;
//
// At this point edges are just marked in or out, we need to handle
// 2 outs or 2 ins. If there are 2 in's mark the left one as the
// real in, change the count and backward link the variables. There
// is also the chance that one of two degenerate cases. The first is
// that v may be one of the point of the trianle which means that we
// are opperating on the lower left most triangle. This is unique
// because there will be two collinear edge with v. The other case
// is where only one of the edges is collinear with v. In the either
// case there is a perscribed solution to the problem in the code
// below.
//
if(e12.type == IN && e13.type == IN){
areaV = areaSign(v,t->p1,t->p2);
areaOp = areaSign(v,t->p1,t->p3);
// e12 is the real in edge and there are no collinears
if( areaV > 0 && areaOp < 0){
inback = &e13;
in = &e12;
out = &e23;
e13.type = INBACK;
inCount++;
}
// e13 is the real in edge and there are no collinears
else if ( areaV < 0 && areaOp > 0){
inback = &e12;
in = &e13;
out = &e23;
e12.type = INBACK;
inCount++;
}
// e13 is collinear and p2 is right of e13
else if ( areaV < 0 && areaOp == 0){
in = &e12;
out = &e13;
outback = &e23;
e13.type = OUT;
e23.type = OUTBACK;
outCount++;
}
// e13 is collinear and p2 is left of e13
else if ( areaV > 0 && areaOp == 0){
inback = &e13;
in = &e12;
out = &e23;
e13.type = INBACK;
inCount++;
}
// e12 is collinear and p3 is right of e12
else if ( areaV == 0 && areaOp < 0){
in = &e13;
out = &e12;
outback = &e23;
e12.type = OUT;
e23.type = OUTBACK;
outCount++;
}
// e12 is collinear and p3 is left of e12
else if ( areaV == 0 && areaOp > 0){
inback = &e12;
in = &e13;
out = &e23;
e12.type = INBACK;
inCount++;
}
// They are both collinear, this must be the lower left corner
else {
assert(areaV == 0 && areaOp == 0);
// Determine which edge is left most
areaV = areaSign(t->p1,t->p2,t->p3);
// areaV should not be 0
assert(areaV);
// if p3 is left of p1p2 then e13 is OUT
if(areaV > 0){
in = &e12;
out = &e13;
outback = &e23;
e13.type = OUT;
e23.type = OUTBACK;
outCount++;
}
// if p3 is right of p1p2 then e12 is OUT
else {
in = &e13;
out = &e12;
outback = &e23;
e12.type = OUT;
e23.type = OUTBACK;
outCount++;
}
}
}
if(e12.type == IN && e23.type == IN){
areaV = areaSign(v,t->p2,t->p1);
areaOp = areaSign(v,t->p2,t->p3);
// e12 is the real in edge and there are no collinears
if( areaV > 0 && areaOp < 0){
inback = &e23;
in = &e12;
out = &e13;
e23.type = INBACK;
inCount++;
}
// e23 is the real in edge and there are no collinears
else if ( areaV < 0 && areaOp > 0){
inback = &e12;
in = &e23;
out = &e13;
e12.type = INBACK;
inCount++;
}
// e23 is collinear and p1 is right of e23
else if ( areaV < 0 && areaOp == 0){
in = &e12;
out = &e23;
outback = &e13;
e23.type = OUT;
e13.type = OUTBACK;
outCount++;
}
// e23 is collinear and p1 is left of e23
else if ( areaV > 0 && areaOp == 0){
inback = &e23;
in = &e12;
out = &e13;
e23.type = INBACK;
inCount++;
}
// e12 is collinear and p3 is right of e12
else if ( areaV == 0 && areaOp < 0){
in = &e23;
out = &e12;
outback = &e13;
e12.type = OUT;
e13.type = OUTBACK;
outCount++;
}
// e12 is collinear and p3 is left of e12
else if ( areaV == 0 && areaOp > 0){
inback = &e12;
in = &e23;
out = &e13;
e12.type = INBACK;
inCount++;
}
// They are both collinear, this must be the lower left corner
else {
assert(areaV == 0 && areaOp == 0);
// Determine which edge is left most
areaV = areaSign(t->p2,t->p1,t->p3);
// areaV should not be 0
assert(areaV);
// if p3 is left of p2p1 then e23 is OUT
if(areaV > 0){
in = &e12;
out = &e23;
outback = &e13;
e23.type = OUT;
e13.type = OUTBACK;
outCount++;
}
// if p3 is right of p2p1 then e12 is OUT
else {
in = &e23;
out = &e12;
outback = &e13;
e12.type = OUT;
e13.type = OUTBACK;
outCount++;
}
}
}
if(e13.type == IN && e23.type == IN){
areaV = areaSign(v,t->p3,t->p1);
areaOp = areaSign(v,t->p3,t->p2);
// e13 is the real in edge and there are no collinears
if( areaV > 0 && areaOp < 0){
inback = &e23;
in = &e13;
out = &e12;
e23.type = INBACK;
inCount++;
}
// e23 is the real in edge and there are no collinears
else if ( areaV < 0 && areaOp > 0){
inback = &e13;
in = &e23;
out = &e12;
e13.type = INBACK;
inCount++;
}
// e23 is collinear and p1 is right of e23
else if ( areaV < 0 && areaOp == 0){
in = &e13;
out = &e23;
outback = &e12;
e23.type = OUT;
e12.type = OUTBACK;
outCount++;
}
// e23 is collinear and p1 is left of e23
else if ( areaV > 0 && areaOp == 0){
inback = &e23;
in = &e13;
out = &e12;
e23.type = INBACK;
inCount++;
}
// e13 is collinear and p2 is right of e13
else if ( areaV == 0 && areaOp < 0){
in = &e23;
out = &e13;
outback = &e12;
e13.type = OUT;
e12.type = OUTBACK;
outCount++;
}
// e13 is collinear and p2 is left of e13
else if ( areaV == 0 && areaOp > 0){
inback = &e13;
in = &e23;
out = &e12;
e13.type = INBACK;
inCount++;
}
// They are both collinear, this must be the lower left corner
else {
assert(areaV == 0 && areaOp == 0);
// Determine which edge is left most
areaV = areaSign(t->p3,t->p1,t->p2);
// areaV should not be 0
assert(areaV);
// if p2 is left of p3p1 then e23 is OUT
if(areaV > 0){
in = &e13;
out = &e23;
outback = &e12;
e23.type = OUT;
e12.type = OUTBACK;
outCount++;
}
// if p2 is right of p3p1 then e13 is OUT
else {
in = &e23;
out = &e13;
outback = &e12;
e13.type = OUT;
e12.type = OUTBACK;
outCount++;
}
}
}
// If there are 2 out's mark the left one as the real out
if(e12.type == OUT && e13.type == OUT){
areaV = areaSign(v,t->p1,t->p2);
outCount++;
if( areaV >= 0 ){
outback = &e13;
out = &e12;
in = &e23;
e13.type = OUTBACK;
}
else{
outback = &e12;
out = &e13;
in = &e23;
e12.type = OUTBACK;
}
}
if(e12.type == OUT && e23.type == OUT){
areaV = areaSign(v,t->p2,t->p1);
outCount++;
if( areaV >= 0 ){
outback = &e23;
out = &e12;
in = &e13;
e23.type = OUTBACK;
}
else{
outback = &e12;
out = &e23;
in = &e13;
e12.type = OUTBACK;
}
}
if(e13.type == OUT && e23.type == OUT){
areaV = areaSign(v,t->p3,t->p1);
outCount++;
if( areaV >= 0 ){
outback = &e23;
out = &e13;
in = &e12;
e23.type = OUTBACK;
}
else{
outback = &e13;
out = &e23;
in = &e12;
e13.type = OUTBACK;
}
}
// Make sure in/out count is not messed up
assert((inCount == 1 && outCount == 2) || (inCount == 2 && outCount == 1));
// Determine the new type of edge
if((t->p1 == edge->p1 || t->p1 == edge->p2) &&
(t->p2 == edge->p1 || t->p2 == edge->p2))
edge->type = e12.type;
if((t->p1 == edge->p1 || t->p1 == edge->p2) &&
(t->p3 == edge->p1 || t->p3 == edge->p2))
edge->type = e13.type;
if((t->p2 == edge->p1 || t->p2 == edge->p2) &&
(t->p3 == edge->p1 || t->p3 == edge->p2))
edge->type = e23.type;
DEBUG{
printf("e12: %d e13: %d e23: %d edge: %d t: %p\n",e12.type,e13.type,
e23.type,edge->type,t);
fflush(stdout);}
//
// Decide which triangle is next
//
// Only one in edge
if(inCount == 1 && edge->type == IN){
// Change e to be the out edge of t but keep t the same so we
// return the next tri
edge->p1 = out->p1;
edge->p2 = out->p2;
DEBUG{printf("Case1 - 1 IN\n");fflush(stdout);}
return out->t1;
}
else if(edge->type == IN){
edge->p1 = out->p1;
edge->p2 = out->p2;
DEBUG{printf("Case2 - 2 IN\n");fflush(stdout);}
return out->t1;
}
else if(edge->type == INBACK){
DEBUG{printf("Case3 - INBACK\n");fflush(stdout);}
return inback->t1;
}
else if(outCount == 2 && edge->type == OUT){
edge->p1 = outback->p1;
edge->p2 = outback->p2;
DEBUG{printf("Case4 - 2 OUT\n");fflush(stdout);}
return outback->t1;
}
else if(outCount == 2 && edge->type == OUTBACK){
edge->p1 = in->p1;
edge->p2 = in->p2;
DEBUG{printf("Case5 - 2 OUT: Outback\n");fflush(stdout);}
return in->t1;
}
// if there is only one out edge
else{
edge->p1 = in->p1;
edge->p2 = in->p2;
DEBUG{printf("Case6 - 1 Out\n");fflush(stdout);}
return in->t1;
}
}
//
// Remove triangle from TIN_TILE and free memory
//
void removeTri(TRIANGLE* t) {
assert(t);
free(t);
}
//
// printPointList - given a triangle,print its point list
//
void printPointList(TRIANGLE* t){
// loop through each node in the list and free the points
if(t != NULL){
if(t->points == NULL)
printf("\t Triangle: %p has no point list to print\n",t);
else {
printf("Point list for: %p \n",t);
QUEUE h;
QNODE *n;
h = t->points;
assert(h);
n = Q_first(h);
while(n){
char str[100];
sprintf(str,"\t x: %s y: %s z: %s ptr:%%p\n",
COORD_TYPE_PRINT_CHAR,COORD_TYPE_PRINT_CHAR,
ELEV_TYPE_PRINT_CHAR);
printf(str,
n->e.x,
n->e.y,
n->e.z,
&n->e);
n = Q_next(h,n);
}
}
}
}
//
// getTileLength finds the dimesions for a sub grid given a memory allocation
//
int getTileLength (double MEM){
// At anyone time in memory one may need:
// - 1 Grid with R points
// - 1 TIN_TILE with R points and 2R Triangles
// - 1 PQ with 2R elements
//
// Max memory for one tile:
// Let R be number of points in tile and sqrt(R) be length of tile side
// Let TL be tile length
// MEM = 2R * (sizeOf(Triangle) + sizeOf(PQelement) + sizeOf(Point))
//
// TL = sqrt(MEM/(2*(sizeOf(Triangle) + sizeOf(PQelement) + sizeOf(Point))))
double TL;
MEM = MEM * 1048576.0; //convert MB into B
TL = sqrt(MEM/
(2*((double)sizeof(TRIANGLE)+
(double)sizeof(PQ_elemType)+
(double)sizeof(R_POINT))));
printf("total size per point=%d\n", sizeof(TRIANGLE) +sizeof(PQ_elemType) + sizeof(R_POINT));
printf("TL = %d\n", (int)TL);
return TL;
}
//
// compute the signed area of a triangle, positive if the
// third point is off to the left of ab
//
int areaSign(R_POINT *a, R_POINT *b, R_POINT *c){
// Rouding error for area computation. areaX should always be an integer
double error = 0.5;
double areaX;
areaX = (b->x-a->x) * (double)(c->y - a->y) -
(c->x-a->x) * (double)(b->y - a->y);
// c is left of ab
if ( areaX > error)
return 1;
// c is right of ab
else if ( areaX < -error)
return -1;
// c is on ab
else
return 0;
}
//
// Is point z in triangle abc?
//
int inTri2D(R_POINT *a, R_POINT *b, R_POINT *c, R_POINT *z){
int area0, area1, area2;
// Assume no collinear triangles
assert(areaSign(a, b, c) != 0);
area0 = areaSign( a, b, z);
area1 = areaSign( b, c, z);
area2 = areaSign( c, a, z);
if ( ((area0 == 0) && (area1 > 0) && (area2 > 0)) ||
((area1 == 0) && (area0 > 0) && (area2 > 0)) ||
((area2 == 0) && (area0 > 0) && (area1 > 0)) )
// on an edge
return 1;
if ( ((area0 == 0) && (area1 < 0) && (area2 < 0)) ||
((area1 == 0) && (area0 < 0) && (area2 < 0)) ||
((area2 == 0) && (area0 < 0) && (area1 < 0)) )
// on an edge
return 1;
if ( ((area0 < 0) && (area1 < 0) && (area2 < 0)) ||
((area1 > 0) && (area0 > 0) && (area2 > 0)) )
// inside tri
return 1;
if ( (area0 == 0) && (area1 == 0) && (area2 == 0)){
// a,b,c,z are collinear
printf("failure");
assert(0);
exit(1);
}
if ( ((area0 == 0) && ( area1 == 0)) ||
((area0 == 0) && ( area2 == 0)) ||
((area2 == 0) && ( area1 == 0)) )
// z is either a, b, or c this should not happen as we remove points from
// point lists when they become part of the triangle.
return 1;
else
return 0;
}
//
// Validate that all points in a triangle's point list are actually in
// that triangle
//
void checkPointList(TRIANGLE* t){
if(t != NULL){
if(t->points == NULL)
printf("triangle %p is DONE\n",t);
else{
QNODE *h, *n;
h = t->points;
assert(h);
n = Q_first(h);
int badpoints=0, npoints=0;
while(n != NULL){
npoints++;
assert(inTri2D(t->p1, t->p2, t->p3, &n->e));
if (!inTri2D(t->p1, t->p2, t->p3, &n->e)) {
badpoints++;
}
//printf("Point %p in triangle %p\n",n->e,t);
n = Q_next(h,n);
}
printf("triangle %p: npoints=%d, badpoints=%d maxE=%p \n",t, npoints,
badpoints,t->maxE);
}
}
}
//
// Find the third point given two known points
//
R_POINT *findThirdPoint(R_POINT *p1, R_POINT *p2, R_POINT *p3, R_POINT *pa,
R_POINT *pb){
if((pa == p1 && pb == p2) ||
(pa == p2 && pb == p1))
return p3;
else if((pa == p1 && pb == p3) ||
(pa == p3 && pb == p1))
return p2;
else
return p1;
}
//
// Is a point an end point of a particular triangle
//
int isEndPoint(TRIANGLE* t, R_POINT *p){
if(t->p1 == p || t->p2 == p || t->p3 == p)
return 1;
else
return 0;
}
//
// Find the edge opposite to a point p
//
EDGE findOpposite(TRIANGLE* t, R_POINT *p){