-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiedel.cpp
More file actions
1013 lines (877 loc) · 34.9 KB
/
siedel.cpp
File metadata and controls
1013 lines (877 loc) · 34.9 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
#include "siedel.h"
#include "siedel.private.h"
#include "math.h"
#include <cstring>
#include <cmath>
#include <limits>
#include <sys/time.h>
void SegmentStructure::generateRandomOrdering() {
struct timeval tval;
struct timezone tzone;
unsigned int i;
unsigned int n = seg->size();
unsigned int m, st[n], *p;
chooseIndex = 0;
permute = new std::vector<unsigned int>();
permute->reserve(n);
gettimeofday(&tval, &tzone);
srand48(tval.tv_sec);
for(i = 0u; i <= n; i++) {
st[i] = i;
}
p = st;
for (i = 1; i <= n; i++) {
m = lrand48() % (n + 1 - i) + 1;
permute->push_back(p[m]);
if (m != 1) {
p[m] = p[1];
}
}
}
namespace siedel {
unsigned int mathLogStarN(unsigned int n) {
unsigned int i;
double v = (double)n;
for(i = 0; v >= 1; i++) {
v = std::log2(v);
}
return i - 1;
}
unsigned int mathN(unsigned int n, unsigned int h) {
unsigned int i;
double v;
for (i = 0u, v = n; i < h; i++) {
v = log2(v);
}
return (int) ceil(1.0 * n / v);
}
double dot(const libMesh::Point2 &v0, const libMesh::Point2 &v1) {
return (v0.x * v1.x + v0.y * v1.y);
}
double cross(const libMesh::Point2 &v0, const libMesh::Point2 &v1, const libMesh::Point2 &v2) {
return ((v1.x - v0.x) * (v2.y - v0.y) - (v1.y - v0.y) * (v2.x - v0.x));
}
bool greaterThan(const libMesh::Point2 &v0, const libMesh::Point2 &v1) {
if (libMesh::Math::GreaterThan(v0.y, v1.y)) {
return true;
} else if (libMesh::Math::LessThan(v0.y, v1.y)) {
return false;
} else {
return v0.x > v1.x;
}
}
bool greaterThanEqualTo(const libMesh::Point2 &v0, const libMesh::Point2 &v1) {
if (libMesh::Math::GreaterThan(v0.y, v1.y)) {
return true;
} else if (libMesh::Math::LessThan(v0.y, v1.y)) {
return false;
} else {
return v0.x >= v1.x;
}
}
bool equalTo(const libMesh::Point2 &v0, const libMesh::Point2 &v1)
{
return libMesh::Math::Equals(v0.y, v1.y) && libMesh::Math::Equals(v0.x, v1.x);
}
bool lessThan(const libMesh::Point2 &v0, const libMesh::Point2 &v1)
{
if (libMesh::Math::LessThan(v0.y, v1.y)) {
return true;
} else if (libMesh::Math::GreaterThan(v0.y, v1.y)) {
return false;
} else {
return (v0.x < v1.x);
}
}
bool isLeftOf(const Segment &s, const libMesh::Point2 &v)
{
double area;
if (greaterThan(s.end, s.begin)) {
if (libMesh::Math::Equals(s.end.y, v.y)) {
if (v.x < s.end.x) {
area = 1.0;
} else {
area = -1.0;
}
} else if (libMesh::Math::Equals(s.begin.y, v.y)) {
if (v.x < s.begin.x) {
area = 1.0;
} else {
area = -1.0;
}
} else {
area = cross(s.begin, s.end, v);
}
}
return false;
}
void max(libMesh::Point2 &yValue, const libMesh::Point2 &v0, const libMesh::Point2 &v1) {
if (libMesh::Math::GreaterThan(v0.y, v1.y)) {
yValue = v0;
} else if (libMesh::Math::Equals(v0.y, v1.y)) {
if (libMesh::Math::GreaterThan(v0.x, v1.x)) {
yValue = v0;
} else {
yValue = v1;
}
} else {
yValue = v1;
}
}
void min(libMesh::Point2 &yValue, const libMesh::Point2 &v0, const libMesh::Point2 &v1) {
if (libMesh::Math::LessThan(v0.y, v1.y)) {
yValue = v0;
} else if (libMesh::Math::Equals(v0.y, v1.y)) {
if (v0.x < v1.x) {
yValue = v0;
} else {
yValue = v1;
}
} else {
yValue = v1;
}
}
bool inserted(SegmentStructure &seg, unsigned int segmentIndex, bool mode)
{
if (mode) {
return seg[seg[segmentIndex].prev].isInserted;
} else {
return seg[seg[segmentIndex].next].isInserted;
}
}
unsigned int locateEndpoint(const libMesh::Point2 &v, const libMesh::Point2 &vo, unsigned int root, SegmentStructure &seg, QueryStructure &qs) {
QueryNode *rptr = &qs[root];
switch (rptr->type) {
case NodeType::Sink:
return rptr->trapezoidIndex;
case NodeType::Y:
if (greaterThan(v, rptr->yValue)) { /* above */
return locateEndpoint(v, vo, rptr->right, seg, qs);
} else if (equalTo(v, rptr->yValue)) {
/* the point is already inserted. */
if (greaterThan(vo, rptr->yValue)) { /* above */
return locateEndpoint(v, vo, rptr->right, seg, qs);
} else {
return locateEndpoint(v, vo, rptr->left, seg, qs); /* below */
}
} else {
return locateEndpoint(v, vo, rptr->left, seg, qs); /* below */
}
case NodeType::X:
if (equalTo(v, seg[rptr->segmentIndex].begin) ||
equalTo(v, seg[rptr->segmentIndex].end)) {
if (libMesh::Math::Equals(v.y, vo.y)) { /* horizontal segment */
if (vo.x < v.x) {
return locateEndpoint(v, vo, rptr->left, seg, qs); /* left */
} else {
return locateEndpoint(v, vo, rptr->right, seg, qs); /* right */
}
}
else if (isLeftOf(seg[rptr->segmentIndex], vo)) {
return locateEndpoint(v, vo, rptr->left, seg, qs); /* left */
} else {
return locateEndpoint(v, vo, rptr->right, seg, qs); /* right */
}
} else if (isLeftOf(seg[rptr->segmentIndex], v)) {
return locateEndpoint(v, vo, rptr->left, seg, qs); /* left */
} else {
return locateEndpoint(v, vo, rptr->right, seg, qs); /* right */
}
default:
fprintf(stderr, "Haggu !!!!!\n");
break;
}
return 0u;
}
unsigned int initializeSegments(SegmentStructure &seg, libMesh::PolyLine &polygon, unsigned int *contours, unsigned int nContours) {
unsigned int contourCount = 0;
unsigned int i = 1;
unsigned int nPoints;
while(contourCount < nContours) {
unsigned int j;
unsigned int first, last;
nPoints = contours[contourCount];
first = i;
last = first + nPoints - 1;
// First segment
seg[i].begin = polygon.getPoint(i - 1);
seg[i].next = i + 1;
seg[i].prev = last;
seg[last].end = seg[i].begin;
seg[i].isInserted = false;
i++;
for(j = 1u; j < nPoints - 1; j++, i++) {
seg[i].begin = polygon.getPoint(i - 1);
seg[i].next = i + 1;
seg[i].prev = i - 1;
seg[i - 1].end = seg[i].begin;
seg[i].isInserted = false;
}
// Last segment
seg[i].begin = polygon.getPoint(i - 1);
seg[i].next = first;
seg[i].prev = i - 1;
seg[i - 1].end = seg[i].begin;
seg[i].isInserted = false;
i++;
contourCount++;
}
return i - 1;
}
unsigned int initQueryStructure(SegmentStructure &seg, unsigned int segmentIndex, QueryStructure &qs, TrapezoidStructure &tr) {
Segment &s = seg[segmentIndex];
unsigned int root, i1, i2, i3, i4, i5, i6, i7;
unsigned int qIndex, trIndex = 1;
i1 = qs.nextIndex();
qs[i1].type = NodeType::Y;
max(qs[i1].yValue, s.begin, s.end);
root = i1;
qs[i1].right = i2 = qs.nextIndex();
qs[i2].type = NodeType::Sink;
qs[i2].parent = i1;
qs[i1].left = i3 = qs.nextIndex();
qs[i3].type = NodeType::Y;
min(qs[i3].yValue, s.begin, s.end);
qs[i3].parent = i1;
qs[i3].left = i4 = qs.nextIndex();
qs[i4].type = NodeType::Sink;
qs[i4].parent = i3;
qs[i3].right = i5 = qs.nextIndex();
qs[i5].type = NodeType::X;
qs[i5].segmentIndex = segmentIndex;
qs[i5].parent = i3;
qs[i5].left = i6 = qs.nextIndex();
qs[i6].type = NodeType::Sink;
qs[i6].parent = i5;
qs[i5].right = i7 = qs.nextIndex();
qs[i7].type = NodeType::Sink;
qs[i7].parent = i5;
unsigned int t1, t2, t3, t4;
t1 = tr.nextIndex();
t2 = tr.nextIndex();
t3 = tr.nextIndex();
t4 = tr.nextIndex();
tr[t1].high = tr[t2].high = tr[t4].low = qs[i1].yValue;
tr[t1].low = tr[t2].low = tr[t3].high = qs[i3].yValue;
tr[t4].high.y = std::numeric_limits<float>().infinity();
tr[t4].high.x = std::numeric_limits<float>().infinity();
tr[t3].low.y = -std::numeric_limits<float>().infinity();
tr[t3].low.x = -std::numeric_limits<float>().infinity();
tr[t1].rightSegment = tr[t2].leftSegment = segmentIndex;
tr[t1].up0 = tr[t2].up0 = t4;
tr[t1].down0 = tr[t2].down0 = t3;
tr[t4].down0 = tr[t3].up0 = t1;
tr[t4].down1 = tr[t3].up1 = t2;
tr[t1].sink = i6;
tr[t2].sink = i7;
tr[t3].sink = i4;
tr[t4].sink = i2;
tr[t1].state = tr[t2].state = true;
tr[t3].state = tr[t4].state = true;
qs[i2].trapezoidIndex = t4;
qs[i4].trapezoidIndex = t3;
qs[i6].trapezoidIndex = t1;
qs[i7].trapezoidIndex = t2;
s.isInserted = true;
return 0;
}
void mergeTrapezoids(unsigned int segmentIndex, unsigned int tfirst, unsigned int tlast, Side side, QueryStructure &qs, TrapezoidStructure &tr)
{
int t, tnext, cond;
int ptnext;
/* First merge polys on the LHS */
t = tfirst;
while ((t > 0) && greaterThanEqualTo(tr[t].low, tr[tlast].low)) {
if (side == Side::Left) {
cond = ((((tnext = tr[t].down0) > 0) && (tr[tnext].rightSegment == segmentIndex)) ||
(((tnext = tr[t].down1) > 0) && (tr[tnext].rightSegment == segmentIndex)));
} else {
cond = ((((tnext = tr[t].down0) > 0) && (tr[tnext].leftSegment == segmentIndex)) ||
(((tnext = tr[t].down1) > 0) && (tr[tnext].leftSegment == segmentIndex)));
}
if (cond) {
if ((tr[t].leftSegment == tr[tnext].leftSegment) &&
(tr[t].rightSegment == tr[tnext].rightSegment)) { /* good neighbours */
/* merge them */
/* Use the upper node as the new node i.e. t */
ptnext = qs[tr[tnext].sink].parent;
if (qs[ptnext].left == tr[tnext].sink) {
qs[ptnext].left = tr[t].sink;
} else {
qs[ptnext].right = tr[t].sink; /* redirect parent */
}
/* Change the upper neighbours of the lower trapezoids */
if ((tr[t].down0 = tr[tnext].down0) > 0) {
if (tr[tr[t].down0].up0 == tnext) {
tr[tr[t].down0].up0 = t;
} else if (tr[tr[t].down0].up1 == tnext) {
tr[tr[t].down0].up1 = t;
}
}
if ((tr[t].down1 = tr[tnext].down1) > 0) {
if (tr[tr[t].down1].up0 == tnext) {
tr[tr[t].down1].up0 = t;
} else if (tr[tr[t].down1].up1 == tnext) {
tr[tr[t].down1].up1 = t;
}
}
tr[t].low = tr[tnext].low;
tr[tnext].state = false; /* invalidate the lower */
/* trapezium */
} else { /* not good neighbours */
t = tnext;
}
} else { /* do not satisfy the outer if */
t = tnext;
}
} /* end-while */
}
/* Add in the new segment into the trapezoidation and update Q and T
* structures. First locate the two endpoints of the segment in the
* Q-structure. Then start from the topmost trapezoid and go down to
* the lower trapezoid dividing all the trapezoids in between .
*/
unsigned int addSegment(SegmentStructure &seg, unsigned int segmentIndex, QueryStructure &qs, TrapezoidStructure &tr) {
Segment &s = seg[segmentIndex];
Segment &so = seg[segmentIndex];
unsigned int tu, tl, sk, tfirst, tlast, tnext;
unsigned int tfirstr, tlastr, tfirstl, tlastl;
unsigned int i1, i2, t, t1, t2, tn;
libMesh::Point2 tpt;
int tritop = 0, tribot = 0;
bool isSwapped = false;
int tmptriseg;
if (greaterThan(s.end, s.begin)) {
/* Get higher vertex in begin */
int tmp;
tpt = s.begin;
s.begin = s.end;
s.end = tpt;
tmp = s.root0;
s.root0 = s.root1;
s.root1 = tmp;
isSwapped = true;
}
if (!inserted(seg, segmentIndex, isSwapped)) {
/* insert v0 in the tree */
int tmp_d;
tu = locateEndpoint(s.begin, s.end, s.root0, seg, qs);
tl = tr.nextIndex(); /* tl is the new lower trapezoid */
tr[tl].state = true;
tr[tl] = tr[tu];
tr[tu].low.y = tr[tl].high.y = s.begin.y;
tr[tu].low.x = tr[tl].high.x = s.begin.x;
tr[tu].down0 = tl;
tr[tu].down1 = 0;
tr[tl].up0 = tu;
tr[tl].up1 = 0;
if (((tmp_d = tr[tl].down0) > 0) && (tr[tmp_d].up0 == tu))
tr[tmp_d].up0 = tl;
if (((tmp_d = tr[tl].down0) > 0) && (tr[tmp_d].up1 == tu))
tr[tmp_d].up1 = tl;
if (((tmp_d = tr[tl].down1) > 0) && (tr[tmp_d].up0 == tu))
tr[tmp_d].up0 = tl;
if (((tmp_d = tr[tl].down1) > 0) && (tr[tmp_d].up1 == tu))
tr[tmp_d].up1 = tl;
/* Now update the query structure and obtain the sinks for the */
/* two trapezoids */
i1 = qs.nextIndex(); /* Upper trapezoid sink */
i2 = qs.nextIndex(); /* Lower trapezoid sink */
sk = tr[tu].sink;
qs[sk].type = NodeType::Y;
qs[sk].yValue = s.begin;
qs[sk].segmentIndex = segmentIndex; /* not really reqd ... maybe later */
qs[sk].left = i2;
qs[sk].right = i1;
qs[i1].type = NodeType::Sink;
qs[i1].trapezoidIndex = tu;
qs[i1].parent = sk;
qs[i2].type = NodeType::Sink;
qs[i2].trapezoidIndex = tl;
qs[i2].parent = sk;
tr[tu].sink = i1;
tr[tl].sink = i2;
tfirst = tl;
}
else /* v0 already present */
{ /* Get the topmost intersecting trapezoid */
tfirst = locateEndpoint(s.begin, s.end, s.root0, seg, qs);
tritop = 1;
}
if (!inserted(seg, segmentIndex, isSwapped)) {
/* insert v1 in the tree */
int tmp_d;
tu = locateEndpoint(s.end, s.begin, s.root1, seg, qs);
tl = tr.nextIndex(); /* tl is the new lower trapezoid */
tr[tl].state = true;
tr[tl] = tr[tu];
tr[tu].low.y = tr[tl].high.y = s.end.y;
tr[tu].low.x = tr[tl].high.x = s.end.x;
tr[tu].down0 = tl;
tr[tu].down1 = 0;
tr[tl].up0 = tu;
tr[tl].up1 = 0;
if (((tmp_d = tr[tl].down0) > 0) && (tr[tmp_d].up0 == tu))
tr[tmp_d].up0 = tl;
if (((tmp_d = tr[tl].down0) > 0) && (tr[tmp_d].up1 == tu))
tr[tmp_d].up1 = tl;
if (((tmp_d = tr[tl].down1) > 0) && (tr[tmp_d].up0 == tu))
tr[tmp_d].up0 = tl;
if (((tmp_d = tr[tl].down1) > 0) && (tr[tmp_d].up1 == tu))
tr[tmp_d].up1 = tl;
/* Now update the query structure and obtain the sinks for the */
/* two trapezoids */
i1 = qs.nextIndex(); /* Upper trapezoid sink */
i2 = qs.nextIndex(); /* Lower trapezoid sink */
sk = tr[tu].sink;
qs[sk].type = NodeType::Y;
qs[sk].yValue = s.end;
qs[sk].segmentIndex = segmentIndex; /* not really reqd ... maybe later */
qs[sk].left = i2;
qs[sk].right = i1;
qs[i1].type = NodeType::Sink;
qs[i1].trapezoidIndex = tu;
qs[i1].parent = sk;
qs[i2].type = NodeType::Sink;
qs[i2].trapezoidIndex = tl;
qs[i2].parent = sk;
tr[tu].sink = i1;
tr[tl].sink = i2;
tlast = tu;
}
else /* v1 already present */
{ /* Get the lowermost intersecting trapezoid */
tlast = locateEndpoint(s.end, s.begin, s.root1, seg, qs);
tribot = 1;
}
/* Thread the segment into the query tree creating a new X-node */
/* First, split all the trapezoids which are intersected by s into */
/* two */
t = tfirst; /* topmost trapezoid */
while ((t > 0) &&
greaterThanEqualTo(tr[t].low, tr[tlast].low))
/* traverse from top to bot */
{
int t_sav, tn_sav;
sk = tr[t].sink;
i1 = qs.nextIndex(); /* left trapezoid sink */
i2 = qs.nextIndex(); /* right trapezoid sink */
qs[sk].type = NodeType::Y;
qs[sk].segmentIndex = segmentIndex;
qs[sk].left = i1;
qs[sk].right = i2;
qs[i1].type = NodeType::Sink; /* left trapezoid (use existing one) */
qs[i1].trapezoidIndex = t;
qs[i1].parent = sk;
qs[i2].type = NodeType::Sink; /* right trapezoid (allocate new) */
qs[i2].trapezoidIndex = tn = tr.nextIndex();
tr[tn].state = true;
qs[i2].parent = sk;
if (t == tfirst)
tfirstr = tn;
if (&tr[t].low == &tr[tlast].low)
tlastr = tn;
tr[tn] = tr[t];
tr[t].sink = i1;
tr[tn].sink = i2;
t_sav = t;
tn_sav = tn;
/* error */
if ((tr[t].down0 <= 0) && (tr[t].down1 <= 0)) /* case cannot arise */
{
fprintf(stderr, "add_segment: error\n");
break;
}
/* only one trapezoid below. partition t into two and make the */
/* two resulting trapezoids t and tn as the upper neighbours of */
/* the sole lower trapezoid */
else if ((tr[t].down0 > 0) && (tr[t].down1 <= 0))
{ /* Only one trapezoid below */
if ((tr[t].up0 > 0) && (tr[t].up1 > 0))
{ /* continuation of a chain from abv. */
if (tr[t].up2 > 0) /* three upper neighbours */
{
if (tr[t].up2Side == Side::Left)
{
tr[tn].up0 = tr[t].up1;
tr[t].up1 = -1;
tr[tn].up1 = tr[t].up2;
tr[tr[t].up0].down0 = t;
tr[tr[tn].up0].down0 = tn;
tr[tr[tn].up1].down0 = tn;
}
else /* intersects in the right */
{
tr[tn].up1 = -1;
tr[tn].up0 = tr[t].up1;
tr[t].up1 = tr[t].up0;
tr[t].up0 = tr[t].up2;
tr[tr[t].up0].down0 = t;
tr[tr[t].up1].down0 = t;
tr[tr[tn].up0].down0 = tn;
}
tr[t].up2 = tr[tn].up2 = 0;
}
else /* No usave.... simple case */
{
tr[tn].up0 = tr[t].up1;
tr[t].up1 = tr[tn].up1 = -1;
tr[tr[tn].up0].down0 = tn;
}
}
else
{ /* fresh seg. or upward cusp */
int tmp_u = tr[t].up0;
int td0, td1;
if (((td0 = tr[tmp_u].down0) > 0) &&
((td1 = tr[tmp_u].down1) > 0))
{ /* upward cusp */
if ((tr[td0].rightSegment > 0) &&
!isLeftOf(seg[tr[td0].rightSegment], s.end))
{
tr[t].up0 = tr[t].up1 = tr[tn].up1 = -1;
tr[tr[tn].up0].down1 = tn;
}
else /* cusp going leftwards */
{
tr[tn].up0 = tr[tn].up1 = tr[t].up1 = -1;
tr[tr[t].up0].down0 = t;
}
}
else /* fresh segment */
{
tr[tr[t].up0].down0 = t;
tr[tr[t].up0].down1 = tn;
}
}
if (tr[t].low == tr[tlast].low && tribot) {
/* bottom forms a triangle */
if (isSwapped)
tmptriseg = seg[segmentIndex].prev;
else
tmptriseg = seg[segmentIndex].next;
if ((tmptriseg > 0) && isLeftOf(seg[tmptriseg], s.begin))
{
/* L-R downward cusp */
tr[tr[t].down0].up0 = t;
tr[tn].down0 = tr[tn].down1 = -1;
}
else
{
/* R-L downward cusp */
tr[tr[tn].down0].up1 = tn;
tr[t].down0 = tr[t].down1 = -1;
}
}
else
{
if ((tr[tr[t].down0].up0 > 0) && (tr[tr[t].down0].up1 > 0))
{
if (tr[tr[t].down0].up0 == t) /* passes thru LHS */
{
tr[tr[t].down0].up2 = tr[tr[t].down0].up1;
tr[tr[t].down0].up2Side = Side::Left;
}
else
{
tr[tr[t].down0].up2 = tr[tr[t].down0].up0;
tr[tr[t].down0].up2Side = Side::Right;
}
}
tr[tr[t].down0].up0 = t;
tr[tr[t].down0].up1 = tn;
}
t = tr[t].down0;
}
else if ((tr[t].down0 <= 0) && (tr[t].down1 > 0))
{ /* Only one trapezoid below */
if ((tr[t].up0 > 0) && (tr[t].up1 > 0))
{ /* continuation of a chain from abv. */
if (tr[t].up2 > 0) /* three upper neighbours */
{
if (tr[t].up2Side == Side::Left)
{
tr[tn].up0 = tr[t].up1;
tr[t].up1 = -1;
tr[tn].up1 = tr[t].up2;
tr[tr[t].up0].down0 = t;
tr[tr[tn].up0].down0 = tn;
tr[tr[tn].up1].down0 = tn;
}
else /* intersects in the right */
{
tr[tn].up1 = -1;
tr[tn].up0 = tr[t].up1;
tr[t].up1 = tr[t].up0;
tr[t].up0 = tr[t].up2;
tr[tr[t].up0].down0 = t;
tr[tr[t].up1].down0 = t;
tr[tr[tn].up0].down0 = tn;
}
tr[t].up2 = tr[tn].up2 = 0;
}
else /* No usave.... simple case */
{
tr[tn].up0 = tr[t].up1;
tr[t].up1 = tr[tn].up1 = -1;
tr[tr[tn].up0].down0 = tn;
}
}
else
{ /* fresh seg. or upward cusp */
unsigned int tmp_u = tr[t].up0;
unsigned int td0, td1;
if (((td0 = tr[tmp_u].down0) > 0) &&
((td1 = tr[tmp_u].down1) > 0))
{ /* upward cusp */
if ((tr[td0].rightSegment > 0) &&
!isLeftOf(seg[tr[td0].rightSegment], s.end))
{
tr[t].up0 = tr[t].up1 = tr[tn].up1 = -1;
tr[tr[tn].up0].down1 = tn;
}
else
{
tr[tn].up0 = tr[tn].up1 = tr[t].up1 = -1;
tr[tr[t].up0].down0 = t;
}
}
else /* fresh segment */
{
tr[tr[t].up0].down0 = t;
tr[tr[t].up0].down1 = tn;
}
}
if (tr[t].low == tr[tlast].low && tribot) {
/* bottom forms a triangle */
unsigned int tmpseg;
if (isSwapped)
tmptriseg = seg[segmentIndex].prev;
else
tmptriseg = seg[segmentIndex].next;
if ((tmpseg > 0) && isLeftOf(seg[tmpseg], s.begin))
{
/* L-R downward cusp */
tr[tr[t].down1].up0 = t;
tr[tn].down0 = tr[tn].down1 = -1;
}
else
{
/* R-L downward cusp */
tr[tr[tn].down1].up1 = tn;
tr[t].down0 = tr[t].down1 = -1;
}
}
else
{
if ((tr[tr[t].down1].up0 > 0) && (tr[tr[t].down1].up1 > 0))
{
if (tr[tr[t].down1].up0 == t) /* passes thru LHS */
{
tr[tr[t].down1].up2 = tr[tr[t].down1].up1;
tr[tr[t].down1].up2Side = Side::Left;
}
else
{
tr[tr[t].down1].up2 = tr[tr[t].down1].up0;
tr[tr[t].down1].up2Side = Side::Right;
}
}
tr[tr[t].down1].up0 = t;
tr[tr[t].down1].up1 = tn;
}
t = tr[t].down1;
}
/* two trapezoids below. Find out which one is intersected by */
/* this segment and proceed down that one */
else
{
unsigned int tmpseg = tr[tr[t].down0].rightSegment;
double y0, yt;
libMesh::Point2 tmppt;
unsigned int tnext, i_d0, i_d1;
i_d0 = i_d1 = false;
if (libMesh::Math::Equals(tr[t].low.y, s.begin.y))
{
if (tr[t].low.x > s.begin.x)
i_d0 = true;
else
i_d1 = true;
}
else
{
tmppt.y = y0 = tr[t].low.y;
yt = (y0 - s.begin.y)/(s.end.y - s.begin.y);
tmppt.x = s.begin.x + yt * (s.end.x - s.begin.x);
if (lessThan(tmppt, tr[t].low))
i_d0 = true;
else
i_d1 = true;
}
/* check continuity from the top so that the lower-neighbour */
/* values are properly filled for the upper trapezoid */
if ((tr[t].up0 > 0) && (tr[t].up1 > 0))
{ /* continuation of a chain from abv. */
if (tr[t].up2 > 0) /* three upper neighbours */
{
if (tr[t].up2Side == Side::Left)
{
tr[tn].up0 = tr[t].up1;
tr[t].up1 = -1;
tr[tn].up1 = tr[t].up2;
tr[tr[t].up0].down0 = t;
tr[tr[tn].up0].down0 = tn;
tr[tr[tn].up1].down0 = tn;
}
else /* intersects in the right */
{
tr[tn].up1 = -1;
tr[tn].up0 = tr[t].up1;
tr[t].up1 = tr[t].up0;
tr[t].up0 = tr[t].up2;
tr[tr[t].up0].down0 = t;
tr[tr[t].up1].down0 = t;
tr[tr[tn].up0].down0 = tn;
}
tr[t].up2 = tr[tn].up2 = 0;
}
else /* No usave.... simple case */
{
tr[tn].up0 = tr[t].up1;
tr[tn].up1 = -1;
tr[t].up1 = -1;
tr[tr[tn].up0].down0 = tn;
}
}
else
{ /* fresh seg. or upward cusp */
int tmp_u = tr[t].up0;
int td0, td1;
if (((td0 = tr[tmp_u].down0) > 0) &&
((td1 = tr[tmp_u].down1) > 0))
{ /* upward cusp */
if ((tr[td0].rightSegment > 0) &&
!isLeftOf(seg[tr[td0].rightSegment], s.end))
{
tr[t].up0 = tr[t].up1 = tr[tn].up1 = -1;
tr[tr[tn].up0].down1 = tn;
}
else
{
tr[tn].up0 = tr[tn].up1 = tr[t].up1 = -1;
tr[tr[t].up0].down0 = t;
}
}
else /* fresh segment */
{
tr[tr[t].up0].down0 = t;
tr[tr[t].up0].down1 = tn;
}
}
if (tr[t].low == tr[tlast].low && tribot) {
/* this case arises only at the lowest trapezoid.. i.e.
tlast, if the lower endpoint of the segment is
already inserted in the structure */
tr[tr[t].down0].up0 = t;
tr[tr[t].down0].up1 = -1;
tr[tr[t].down1].up0 = tn;
tr[tr[t].down1].up1 = -1;
tr[tn].down0 = tr[t].down1;
tr[t].down1 = tr[tn].down1 = -1;
tnext = tr[t].down1;
}
else if (i_d0)
/* intersecting d0 */
{
tr[tr[t].down0].up0 = t;
tr[tr[t].down0].up1 = tn;
tr[tr[t].down1].up0 = tn;
tr[tr[t].down1].up1 = -1;
/* new code to determine the bottom neighbours of the */
/* newly partitioned trapezoid */
tr[t].down1 = -1;
tnext = tr[t].down0;
}
else /* intersecting d1 */
{
tr[tr[t].down0].up0 = t;
tr[tr[t].down0].up1 = -1;
tr[tr[t].down1].up0 = t;
tr[tr[t].down1].up1 = tn;
/* new code to determine the bottom neighbours of the */
/* newly partitioned trapezoid */
tr[tn].down0 = tr[t].down1;
tr[tn].down1 = -1;
tnext = tr[t].down1;
}
t = tnext;
}
tr[t_sav].rightSegment = tr[tn_sav].leftSegment = segmentIndex;
} /* end-while */
/* Now combine those trapezoids which share common segments. We can */
/* use the pointers to the parent to connect these together. This */
/* works only because all these new trapezoids have been formed */
/* due to splitting by the segment, and hence have only one parent */
tfirstl = tfirst;
tlastl = tlast;
mergeTrapezoids(segmentIndex, tfirstl, tlastl, Side::Left, qs, tr);
mergeTrapezoids(segmentIndex, tfirstr, tlastr, Side::Right, qs, tr);
seg[segmentIndex].isInserted = true;
return 0;
}
/* Update the roots stored for each of the endpoints of the segment.
* This is done to speed up the location-query for the endpoint when
* the segment is inserted into the trapezoidation subsequently
*/
void findNewRoots(SegmentStructure &seg, unsigned int segmentNumber, QueryStructure &qs, TrapezoidStructure &tr) {
Segment &s = seg[segmentNumber];
if (s.isInserted) {
return;
}
s.root0 = locateEndpoint(s.begin, s.end, s.root0, seg, qs);
s.root0 = tr[s.root0].sink;
s.root1 = locateEndpoint(s.end, s.begin, s.root1, seg, qs);
s.root1 = tr[s.root1].sink;
}
void constructTrapezoids(SegmentStructure &seg, unsigned int numSegments, QueryStructure &qs, TrapezoidStructure &trap) {
unsigned int root = initQueryStructure(seg, seg.chooseSegment(), qs, trap);
unsigned int i;
for(i = 1; i <= numSegments; i++) {
seg[i].root0 = seg[i].root1 = root;
}
for(unsigned int h = 1; h <= mathLogStarN(numSegments); h++) {
for (i = mathN(numSegments, h - 1) + 1; i < mathN(numSegments, h); i++) {
addSegment(seg, seg.chooseSegment(), qs, trap);
}
for (i = 1; i <= numSegments; i++) {
findNewRoots(seg, i, qs, trap);
}
}
for (i = mathN(numSegments, mathLogStarN(numSegments)) + 1; i <= numSegments; i++) {
addSegment(seg, seg.chooseSegment(), qs, trap);
}
}
unsigned int monotonateTrapezoids(TrapezoidStructure &tr, SegmentStructure &seg, unsigned int numSegments) {
return 0u;
}
libMesh::Mesh *earClipping(libMesh::PolyLine &polygon) {
return nullptr;
}
libMesh::Mesh *siedelTriangulation(libMesh::PolyLine &polygon) {
// Using Siedel 1991 algorithm
// See: http://gamma.cs.unc.edu/SEIDEL/
unsigned int nPoints = polygon.count();
SegmentStructure seg = SegmentStructure(nPoints);
QueryStructure qs = QueryStructure(8 * nPoints);
TrapezoidStructure trap = TrapezoidStructure(4 * nPoints);