-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPolyDetector.cpp
More file actions
2883 lines (2475 loc) · 78.5 KB
/
PolyDetector.cpp
File metadata and controls
2883 lines (2475 loc) · 78.5 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 <assert.h>
#include "PolyDetector.h"
#include <set>
#if 0
#undef logoutf
#define logoutf(...) do {} while(false)
#endif
#define arToStr(arg) #arg
//static const float minPointDiff = .1f;
//static const float minPointDiff = 1e-2f;
//static const float minPointDiff = 1e-3f;
//static const float minPointDiff = 1e-4f;
static const float minPointDiff = 1e-5f;
static const float minPointDiffSq = minPointDiff * minPointDiff;
const char *RmLinesTypeStr(RmLinesType type)
{
switch (type)
{
case RmLinesType::TakenTwice: return "TakenTwice";
case RmLinesType::Collinear: return "Collinear";
case RmLinesType::NoPointNeigh: return "NoPointNeigh";
case RmLinesType::PointConsumed: return "PointConsumed";
}
return "UNKN";
}
// Given three colinear points p, q, r, the function checks if
// point q lies on line segment 'pr'
static bool onSegment(const PointType &p, const PointType &q, const PointType &r)
{
if (q.x <= std::max(p.x, r.x) && q.x >= std::min(p.x, r.x) &&
q.y <= std::max(p.y, r.y) && q.y >= std::min(p.y, r.y))
return true;
return false;
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
static int orientation(const PointType &p, const PointType &q, const PointType &r)
{
// See https://www.geeksforgeeks.org/orientation-3-ordered-points/
// for details of below formula.
double val =
(q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0.0) return 0; // colinear
//if (abs(val) <= 1e-7f) return 0; // colinear
return (val > 0) ? 1: 2; // clock or counterclock wise
}
static bool collinearVecs(const PointType &p, const PointType &q, const PointType &r)
{
return orientation(p, q, r) == 0;
}
// The main function that returns true if line segment 'p1q1'
// and 'p2q2' intersect.
// https://www.cdn.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
static bool doIntersect(const PointType &p1, const PointType &q1, const PointType &p2, const PointType &q2)
{
// Find the four orientations needed for general and
// special cases
int o1 = orientation(p1, q1, p2);
int o2 = orientation(p1, q1, q2);
int o3 = orientation(p2, q2, p1);
int o4 = orientation(p2, q2, q1);
// General case
if (o1 != o2 && o3 != o4)
return true;
// Special Cases
// p1, q1 and p2 are colinear and p2 lies on segment p1q1
if (o1 == 0 && onSegment(p1, p2, q1)) return true;
// p1, q1 and q2 are colinear and q2 lies on segment p1q1
if (o2 == 0 && onSegment(p1, q2, q1)) return true;
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
if (o3 == 0 && onSegment(p2, p1, q2)) return true;
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
if (o4 == 0 && onSegment(p2, q1, q2)) return true;
return false; // Doesn't fall in any of the above cases
}
static bool pointsDiffer(const PointType &a, const PointType &b, bool aprox = true)
{
// max precision is mandatory since this can break convex polys!
if (aprox)
return a.squaredist(b) >= minPointDiffSq;
return a.x != b.x || a.y != b.y;
}
/***
* @return true is this point is betwen a and b
* @note c must be collinear with a and b
* @see O'Rourke, Joseph, "Computational Geometry in C, 2nd Ed.", pp.32
*/
static bool between(const PointType &p, const PointType &a, const PointType &b)
{
// if this point is not collinear with a and b
// then it cannot be between this two points
if (!collinearVecs(p, a, b))
return false;
auto &_x = p.x;
auto &_y = p.y;
return
((a.x <= _x && _x <= b.x) && (a.y <= _y && _y <= b.y)) ||
((b.x <= _x && _x <= a.x) && (b.y <= _y && _y <= a.y));
}
bool PolyLine::contains(const PolyLine &line) const
{
return contains(line.a) && contains(line.b);
}
bool PolyLine::contains(const PointType &point) const
{
return between(point, a, b);
}
bool PolyLine::collinear(const PolyLine &line) const
{
return !doIntersect(a, b, line.a, line.b);
}
//bool PolyLine::intersects(const PolyLine &line) const
//{
// return doIntersect(a, b, line.a, line.b);
//}
bool PolyLine::IntersectionPoint(const PolyLine &line, PointType &pos) const
{
return LineLineIntersectionPoint(line, pos);
}
static bool overlap(const PolyLine &l1, const PolyLine &l2)
{
return (collinearVecs(l1.a, l2.a, l2.b) && collinearVecs(l1.b, l2.a, l2.b)) &&
((l1.contains(l2.a) || l1.contains(l2.b)) ||
(l2.contains(l1.a) || l2.contains(l1.b)));
}
/***
* @return a new simplified line if line_1 and line_2 overlaps, NULL otherwise
*/
static int simplifiedLine(const PolyLine &line_1, const PolyLine &line_2, PolyLine &ret)
{
if (overlap(line_1, line_2))
{
if (line_1.contains(line_2))
{
ret = line_1;
return 1;
}
if (line_2.contains(line_1))
{
ret = line_2;
return 2;
}
PointType new_line_start_point;
PointType new_line_end_point;
// detects which point of <line_1> must be removed
if (between(line_1.a, line_2.a, line_2.b)) {
new_line_start_point = line_1.b;
} else {
new_line_start_point = line_1.a;
}
// detects which point of <line_2> must be removed
if (between(line_2.a, line_1.a, line_1.b)) {
new_line_end_point = line_2.b;
} else {
new_line_end_point = line_2.a;
}
// create a new line
ret = PolyLine(new_line_start_point, new_line_end_point);
return 3;
}
return 0;
}
static int iComparePointOrder(const PointType &p1, const PointType &p2)
{
if (p1.y < p2.y)
return -1;
else if (p1.y == p2.y)
{
if (p1.x < p2.x)
return -1;
else if (p1.x == p2.x)
return 0;
}
// p1 is greater than p2
return 1;
}
static bool bComparePointOrder(const PointType &p1, const PointType &p2)
{
return iComparePointOrder(p1, p2) < 0;
}
bool PolyLine::bCompareLineOrder(const PolyLine &l1, PolyLine &l2)
{
return iCompareLineOrder(l1, l2) < 0;
}
int PolyLine::iCompareLineOrder(const PolyLine &l1, PolyLine &l2)
{
int result = iComparePointOrder(l1.a, l2.a);
if (result == 0)
{
// in case lines share first point
// we must order the lines by its slope
auto dx1 = l1.b.x - l1.a.x;
auto dy1 = l1.b.y - l1.a.y;
auto dx2 = l2.b.x - l2.a.x;
auto dy2 = l2.b.y - l2.a.y;
// by definition of first and last point we are sure that dy > 0
if (dx1>0 && dx2<0)
// line 1 in 1st quadrant, line 2 in 2nd quadrant
// this means line 2 cames first
return 1;
if (dx1<0 && dx2>0)
// line 1 in 2nd quadrant, line 2 in 1st quadrant
// this means line 1 cames first
return -1;
if (dx1 == 0) {
// first line is vertical
if (dx2>0)
// second line in 1st quadrant
// first line is previous
return -1;
if (dx2<0)
// second line in 2nd quadrant
// second line is previous
return 1;
// this should no happen
return 0;
}
if (dx2 == 0) {
// second line is vertical
if (dx1>0)
// first line in 1st quadrant
// second line is previous
return 1;
if (dx1<0)
// first line in 2nd quadrant
// first line is previous
return -1;
// this should not happen
return 0;
}
// calculate the slopes
double m1 = dy1/dx1;
double m2 = dy2/dx2;
// line 1 and line 2 in 2nd quadrant
if (m1 > m2)
return -1;
if (m1 < m2)
return 1;
// in this case we have the same slope in both lines,
// which means that both lines are coincident.
return 0;
}
return result;
}
static float Area(const PointType &a, const PointType &b, const PointType &c)
{
return 0.5 * ((b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y));
}
/*
bool PolyLine::Equals(const PolyLine &line) const
{
return
(!pointsDiffer(line.a, a) || !pointsDiffer(line.a, b)) &&
(!pointsDiffer(line.b, a) || !pointsDiffer(line.b, b));
}
*/
bool PolyLine::HasCommonIdxPoints(const PolyLine &line) const
{
return
aIdx == line.aIdx ||
aIdx == line.bIdx ||
bIdx == line.aIdx ||
bIdx == line.bIdx;
}
/*
bool PolyLine::HasCommonPoints(const PolyLine &line) const
{
return HaveCommonPoints(*this, line);
}
bool PolyLine::HaveCommonPoints(const PolyLine &l1, const PolyLine &l2)
{
return (!pointsDiffer(l1.a, l2.a) || !pointsDiffer(l1.b, l2.b) ||
!pointsDiffer(l1.a, l2.b) || !pointsDiffer(l1.b, l2.a));
}
*/
// https://www.geeksforgeeks.org/program-for-point-of-intersection-of-two-lines/
bool PolyLine::LineLineIntersectionPoint(const PolyLine &line, PointType &pos) const
{
auto &c = line.a;
auto &d = line.b;
// Line AB represented as a1x + b1y = c1
double a1 = b.y - a.y;
double b1 = a.x - b.x;
double c1 = a1*(a.x) + b1*(a.y);
// Line CD represented as a2x + b2y = c2
double a2 = d.y - c.y;
double b2 = c.x - d.x;
double c2 = a2*(c.x)+ b2*(c.y);
double determinant = a1*b2 - a2*b1;
if (determinant == 0)
{
// The lines are parallel. This is simplified
// by returning a pair of FLT_MAX
return false;
}
double x = (b2*c1 - b1*c2)/determinant;
double y = (a1*c2 - a2*c1)/determinant;
pos = PointType(x, y, 0.0f);
return true;
}
void PolyLine::SortIntersectionsList(PolyDetector &pd)
{
std::sort(intersections.begin(), intersections.end(), [&pd, this] (const uint32_t &p1, const uint32_t &p2) {
//return bComparePointOrder(pd.intersectionPoints[p1], pd.intersectionPoints[p2]);
return pd.intersectionPoints[p1].squaredist(a) < pd.intersectionPoints[p2].squaredist(a);
});
}
PolyLine PolyDetector::newLine(uint32_t i, uint32_t j, PolyLine &origLine)
{
PolyLine l;
l.aIdx = origLine.intersections[i];
l.bIdx = origLine.intersections[j];
l.a = intersectionPoints[l.aIdx];
l.b = intersectionPoints[l.bIdx];
l.origLine = origLine.id;
l.lastDissolveStep = origLine.lastDissolveStep;
l.attr0 = origLine.attr0;
l.calcCenter();
return l;
}
bool PolyDetector::CreateLines()
{
//logoutf("Line creation");
uint32_t n = 0;
for (auto &l : origLines)
{
l.id = n++;
}
// prior to removing overlapping, one must
// remove all zero length line, otherwise the results
// will be unpredictable
RemoveZeroLengthLines();
// then we must remove line overlapping
RemoveOverlappings();
// finally we detect intersections between lines
int intersection_count = DetectAllIntersections();
if (intersection_count == 0)
return true;
if (verbose)
{
logoutf("Detected %d intersections", intersection_count);
logoutf("%u lines after intersection detection", uint32_t(origLines.size()));
}
// sweep all lines
lines.clear();
for (auto &line : origLines)
{
if (line.ignore) continue;
// check if current line has intersections
if (line.intersections.size() >= 2)
{
line.SortIntersectionsList(*this);
if (verbose > 1)
logoutf("line #%u nIntersections:%u", line.id, uint32_t(line.intersections.size()));
for (uint32_t i = 1; i < line.intersections.size(); ++i)
{
bool foundDup = false;
auto
aIdx = line.intersections[i - 1],
bIdx = line.intersections[i];
assert(aIdx != bIdx);
auto &p1 = intersectionPoints[aIdx];
auto &p2 = intersectionPoints[bIdx];
if (!pointsDiffer(p1, p2))
{
logoutf("P%u P%u are the same for line #%u", aIdx, bIdx, line.id);
assert(pointsDiffer(p1, p2));
}
assert(p1.squaredist(line.a) <= p2.squaredist(line.a));
for (auto &lDup : lines)
{
if ((lDup.minPid() == std::min(aIdx, bIdx)) &&
lDup.maxPid() == std::max(aIdx, bIdx))
{
//if (verbose)
logoutf("WARN: Identical lines detected! procLine:%u [P%u P%u]. Means origLine:#%u and origLine:#%u are collinear!", lDup.id,
lDup.minPid(), lDup.maxPid(), line.id, lDup.origLine);
//assert(false);
foundDup = true;
}
}
if (foundDup)
{
if (verbose)
logoutf("WARN: line #%u has duplicate procLines! [P%u P%u]", line.id, aIdx, bIdx);
}
else if (!pointsDiffer(p1, p2))
{
if (verbose)
logoutf("WARN: line #%u has zero length! [P%u P%u]", line.id, aIdx, bIdx);
}
else
{
lines.push_back(newLine(i - 1, i, line));
}
if (verbose > 3)
{
logoutf("[L#%u][%f %f, %f %f] -> %u [P%u P%u] [%f %f] [%f %f]",
line.id,
line.a.x, line.a.y, line.b.x, line.b.y,
i,
aIdx < bIdx ? aIdx : bIdx, aIdx < bIdx ? bIdx : aIdx,
p1.x, p1.y,
p2.x, p2.y);
}
}
}
}
#if 1
for (uint32_t i = 0; i < lines.size(); i++)
{
auto &l1 = lines[i];
//if (!pointsDiffer(l1.a, l1.b)) // find a zero length line
// logoutf("line %s points are the same!", l1.toString(*this).c_str());
for (uint32_t j = i + 1; j < lines.size(); j++)
{
auto &l2 = lines[j];
auto aIdx1 = l1.minPid();
auto bIdx1 = l1.maxPid();
auto aIdx2 = l2.minPid();
auto bIdx2 = l2.maxPid();
if (!l1.HasCommonIdxPoints(l2) && overlap(l1, l2))
{
logoutf("%u:%s and %u:%s overlap! olid:#%u olid:#%u", i, l1.toString(*this).c_str(), j, l2.toString(*this).c_str(), l1.origLine, l2.origLine);
}
if (aIdx1 == aIdx2 && bIdx1 == bIdx2)
{
logoutf("%u:%s and %u:%s have same points! [P%u P%u] olid1:#%u olid2:#%u", i, l1.toString(*this).c_str(), j, l2.toString(*this).c_str(), aIdx1, bIdx1, l1.origLine, l2.origLine);
//assert(0);
//dumpLines("commonPts");
}
}
}
#endif
if (verbose)
logoutf("nOrigLines:%u nLines:%u", uint32_t(origLines.size()), uint32_t(lines.size()));
return true;
}
void PolyDetector::dumpLines(const char *msg, bool useIgnore)
{
for (auto &l : lines)
{
if (useIgnore && l.ignore) continue;
logoutf("[%s] %s", msg, l.toString(*this).c_str());
}
}
/***
* @desc removes all lines with zero length
*/
void PolyDetector::RemoveZeroLengthLines(void)
{
for (auto it = origLines.begin(); it != origLines.end(); )
{
if (!pointsDiffer(it->a, it->b)) // find a zero length line
it = origLines.erase(it);
else
++it;
}
}
bool PolyDetector::addPointToLine(uint32_t pid, uint32_t lid)
{
auto &v = pointToLines[pid];
if (std::find(v.begin(), v.end(), lid) == v.end())
{
v.push_back(lid);
return true;
}
return false;
}
/***
* @descr removes line overlappings
* @note must be called before applying Bentley-Ottmann algorithm
*/
void PolyDetector::RemoveOverlappings()
{
uint32_t i, j, count = uint32_t(origLines.size());
//logoutf("RemoveOverlappings");
for (auto &l : origLines)
{
l.calcCenter();
}
PolyLine line;
// lets find overlapping lines
uint32_t countBefore = count;
for (i = 0; i < count; i++)
{
auto &line_i = origLines[i];
for(j = i + 1; j < count; j++)
{
auto &line_j = origLines[j];
if (overlap(line_i, line_j))
{
int ret = simplifiedLine(line_i, line_j, line);
if (verbose > 3)
logoutf("origLine #%u {%f %f %f %f} overlaps with origLine #%u {%f %f %f %f} ret:%d",
line_i.id, line_i.a.x, line_i.a.y, line_i.b.x, line_i.b.y,
line_j.id, line_j.a.x, line_j.a.y, line_j.b.x, line_j.b.y,
ret);
if (ret == 1)
{
// must remove line_j
if (verbose > 1)
logoutf("rm origLine %u", line_j.id);
origLines.erase(origLines.begin() + j);
j--;
count--;
//i = 0; break;
}
else
{
if (ret != 2)
{
//line.id = uint32_t(origLines.size());
line.id = line_i.id;
if (verbose)
logoutf("new origLine %u by merging %u and %u", line.id, line_i.id, line_j.id);
// must remove both line_i and line_j and add a new one
origLines.erase(origLines.begin() + j);
line.calcCenter();
origLines.push_back(std::move(line));
}
// must remove line_i
origLines.erase(origLines.begin() + i);
// update counters
i--;
count--;
// skip inner loop an go to next step of outer loop
break;
}
}
}
}
if (countBefore != count)
logoutf("orig: countBefore:%u count:%u", countBefore, count);
}
uint32_t PolyDetector::DetectAllIntersections()
{
uint32_t ret = 0;
size_t counter = origLines.size();
PointType intersection;
intersectionPoints.clear();
collinearLineMap.clear();
for (auto &l : origLines)
{
l.calcCenter();
}
// sort to always have the same results
std::sort(origLines.begin(), origLines.end(), PolyLine::bCompareLineOrder);
uint32_t n = 0;
for (auto &l : origLines)
{
l.id = n++;
l.intersections.clear();
l.intersectedLines.clear();
}
// intersected lines: remove lines with only one intersection
for (uint32_t i = 0; i < counter; i++)
{
auto &l1 = origLines[i];
for (uint32_t j = i + 1; j < counter; j++)
{
auto &l2 = origLines[j];
if (doIntersect(l1.a, l1.b, l2.a, l2.b))
{
//if (verbose > 1)
// logoutf("line #%u intersects #%u", l1.id, l2.id);
/*
for (auto &l : {&l1, &l2})
{
auto &otherLid = l->id == l1.id ? l2.id : l1.id;
if (l->intersectedLines.find(otherLid) != l->intersectedLines.end())
{
logoutf("WARN: origLine #%u already contains intersected origLine #%u", l->id, otherLid);
}
else
{
l->intersectedLines.insert(otherLid);
}
}
*/
//if (l1.intersectedLines.find(l2.id) != l1.intersectedLines.end())
l1.intersectedLines.insert(l2.id);
l2.intersectedLines.insert(l1.id);
}
}
}
for (uint32_t i = 0; i < counter; i++)
{
auto &l1 = origLines[i];
if (l1.intersectedLines.size() == 1)
{
if (verbose)
logoutf("line #%u has only %u intersectedLines", l1.id, uint32_t(l1.intersectedLines.size()));
for (uint32_t j = 0; j < counter; j++)
{
if (i != j)
{
auto &l2 = origLines[j];
for (auto it = l2.intersectedLines.begin(); it != l2.intersectedLines.end(); )
{
if (*it == l1.id)
{
if (verbose)
logoutf("removed intersectedLine line #%u from line #%u", l1.id, l2.id);
it = l2.intersectedLines.erase(it);
i = 0; // recheck all lines
}
else
++it;
}
}
}
l1.intersectedLines.clear();
}
}
if (verbose > 2)
{
for (auto &l : origLines)
{
//if (!l.intersectedLines.empty())
{
std::string str;
for (auto &n : l.intersectedLines)
str += std::to_string(n) + " ";
if (!str.empty())
str.pop_back();
logoutf("line #%u has %u intersectedLines: [%s]", l.id, uint32_t(l.intersectedLines.size()), str.c_str());
}
}
}
// intersection points
std::set<std::pair<uint32_t, uint32_t>> took;
for (auto &l1 : origLines)
{
for (auto &lid2 : l1.intersectedLines)
{
auto &l2 = origLines[lid2];
if (took.find(std::make_pair(l1.id, l2.id)) == took.end())
{
intersection = vec(0);
if (l1.IntersectionPoint(l2, intersection)) // checks if not parallel
{
uint32_t intersectionIdx = uint32_t(intersectionPoints.size());
// Check if the same intersection point exists
bool dupPoint = false;
for (uint32_t pi = 0; pi < intersectionPoints.size(); ++pi)
{
auto &p = intersectionPoints[pi];
if (!pointsDiffer(p, intersection))
{
if (verbose > 3)
{
logoutf("WARN: origLine #%u intersects origLine #%u exactly in the same point P%u as:", l1.id, l2.id, pi);
for (auto &lDup : origLines)
{
if (lDup.id != l1.id && lDup.id != l2.id)
{
for (auto &inters : lDup.intersections)
{
if (inters == pi)
{
logoutf("WARN: origLine #%u", lDup.id);
}
}
}
}
}
dupPoint = true;
intersectionIdx = pi;
intersection = p;
break;
}
}
if (!dupPoint)
{
intersectionPoints.push_back(intersection);
}
if (verbose > 2)
logoutf("[P%u]: origLine #%u intersects origLine #%u. dup:%d", intersectionIdx, l1.id, l2.id, dupPoint);
for (auto &l : {&l1, &l2})
{
if (std::find(l->intersections.begin(), l->intersections.end(), intersectionIdx) != l->intersections.end())
{
if (verbose > 1)
logoutf("WARN: duplicate intersectionIdx P%u in line #%u", intersectionIdx, l->id);
}
else
{
//auto d = obb::LineSegment(l->a, l->b).Distance(intersection);
//assert(d < .1f);
//auto d1 = obb::LineSegment(l->a, l->b).Distance(intersectionPoints[intersectionIdx]);
//assert(d1 < .1f);
l->intersections.push_back(intersectionIdx);
}
}
//addPointToLine(intersectionIdx, l1.id);
//addPointToLine(intersectionIdx, l2.id);
took.insert(std::make_pair(l1.id, l2.id));
took.insert(std::make_pair(l2.id, l1.id));
ret++;
}
}
}
}
if (verbose)
logoutf("intersectionPoints:%u", uint32_t(intersectionPoints.size()));
//return 0;
if (verbose > 2)
{
for (auto &l : origLines)
{
//if (!l.intersections.empty())
{
std::string str;
for (auto &n : l.intersections)
str += std::to_string(n) + " ";
if (!str.empty())
str.pop_back();
logoutf("line #%u has %u pts: [%s]", l.id, uint32_t(l.intersections.size()), str.c_str());
}
}
}
#if 1
uint32_t nCol = 0;
std::vector<uint32_t> pids;
uint32_t times = 0;
bool ok = false;
do
{
took.clear();
//nCol = 0;
ok = true;
for (auto &l1 : origLines)
{
if (l1.ignore) continue;
if (l1.intersections.size() < 2) continue;
//if (times == 1)
// l1.SortIntersectionsList(*this);
float a, b, c;
vec::line(vec(l1.a.x, l1.a.y), vec(l1.b.x, l1.b.y), a, b, c);
if (a + b == 0.0f)
{
logoutf("l1:#%u l1.a: [%f %f] l1.b: [%f %f]", l1.id, l1.a.x, l1.a.y, l1.b.x, l1.b.y);
assert(0);
continue;
}
//obb::Line line = obb::LineSegment(l1.a, l1.b).ToLine();
for (auto &l2 : origLines)
{
if (l2.ignore) continue;
if (l2.intersections.size() < 2) continue;
//if (took.find(std::make_pair(l1.id, l2.id)) == took.end())
{
//if (k == 1)
// l2.SortIntersectionsList(*this);
took.insert(std::make_pair(l1.id, l2.id));
took.insert(std::make_pair(l2.id, l1.id));
if (l1.id == l2.id) continue;
if (l2.intersections.empty()) continue;
assert(&l1 != &l2);
uint32_t nFound = 0;
float maxLineDist = 0;
pids.clear();
for (auto &pid1 : l1.intersections)
{
for (auto &pid2 : l2.intersections)
{
if (pid1 == pid2)
{
nFound++;
pids.push_back(pid1);
}
//float d = line.Distance(intersectionPoints[pid2]);
float d = vec::lineDist(a, b, c, vec(intersectionPoints[pid2].x, intersectionPoints[pid2].y));
if (d > maxLineDist)
maxLineDist = d;
}
}
if (nFound >= 2)
{
ok = false;
#if 1
if (maxLineDist <= minPointDiff)
{
logoutf("[%u]: line #%u is collinear with line #%u! nFound:%u lineDist:%f merging ...", times, l1.id, l2.id, nFound, maxLineDist);
// TODO: merge point of l2 into l1
uint32_t nMerged = 0;
for (auto &pid : l2.intersections)
{
//logoutf("add P%u to l#%u", pid, l1.id);
if (std::find(l1.intersections.begin(), l1.intersections.end(), pid) == l1.intersections.end())
{
//logoutf("add P%u to l#%u", pid, l1.id);
l1.intersections.push_back(pid);
nMerged++;
}
}
if (nMerged > 0)
{
logoutf("[%u]: merged %u points from line #%u into line #%u! dist:%f", times, nMerged, l2.id, l1.id, maxLineDist);
}
//logoutf("line #%u with %u intersections ignored", l2.id, uint32_t(l2.intersections.size()));
l2.intersections.clear();
l2.a = l2.b = l2.center = vec(0);
l2.ignore = true;
nCol++;
}
else
{
// merge points?
// remove them
uint32_t nRm1 = 0, nRm2 = 0;
for (auto &pid : pids)
{
logoutf("[%u]]: P%u", times, pid);
for (auto it = l1.intersections.begin(); it != l1.intersections.end();)
{
if (*it == pid)
{
it = l1.intersections.erase(it);
nRm1++;
}
else
++it;
}
for (auto it = l2.intersections.begin(); it != l2.intersections.end();)
{
if (*it == pid)
{
it = l2.intersections.erase(it);
nRm2++;
}
else
++it;
}
}
auto keepPid = pids[0];