forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangle.go
1338 lines (1215 loc) · 31.7 KB
/
triangle.go
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
package maths
import (
"errors"
"fmt"
"log"
"sort"
"time"
)
func trace(msg string) func() {
tracer := time.Now()
log.Println(msg, "started at:", tracer)
return func() {
etracer := time.Now()
log.Println(msg, "ElapsedTime in seconds:", etracer.Sub(tracer))
}
}
const adjustBBoxBy = 10
type Triangle [3]Pt
func init() {
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
}
func (t *Triangle) FindEdge(e Line) (idx int, err error) {
switch {
case e[0].IsEqual(t[0]) && e[1].IsEqual(t[1]):
return 0, nil
case e[0].IsEqual(t[1]) && e[1].IsEqual(t[0]):
return 0, nil
case e[0].IsEqual(t[1]) && e[1].IsEqual(t[2]):
return 1, nil
case e[0].IsEqual(t[2]) && e[1].IsEqual(t[1]):
return 1, nil
case e[0].IsEqual(t[2]) && e[1].IsEqual(t[0]):
return 2, nil
case e[0].IsEqual(t[0]) && e[1].IsEqual(t[2]):
return 2, nil
}
return -1, errors.New("Edge not on triangle.")
}
func (t *Triangle) Edge(n int) Line {
if n < 0 || n == 0 {
return Line{t[0], t[1]}
}
if n > 2 || n == 2 {
return Line{t[2], t[0]}
}
return Line{t[1], t[2]}
}
func (t *Triangle) LREdge(n int) Line {
if n < 0 || n == 0 {
return Line{t[0], t[1]}
}
if n > 2 || n == 2 {
return Line{t[0], t[2]}
}
return Line{t[1], t[2]}
}
func (t *Triangle) Edges() [3]Line {
return [3]Line{
{t[0], t[1]},
{t[1], t[2]},
{t[2], t[0]},
}
}
func (t *Triangle) LREdges() [3]Line {
return [3]Line{
{t[0], t[1]},
{t[1], t[2]},
{t[0], t[2]},
}
}
func (t *Triangle) EdgeIdx(pt1, pt2 Pt) int {
if t == nil {
return -1
}
if pt1.IsEqual(t[0]) {
if pt2.IsEqual(t[1]) {
return 0
}
if pt2.IsEqual(t[2]) {
return 2
}
return -1
}
if pt2.IsEqual(t[0]) {
if pt1.IsEqual(t[1]) {
return 0
}
if pt1.IsEqual(t[2]) {
return 2
}
return -1
}
if pt1.IsEqual(t[1]) {
if pt2.IsEqual(t[2]) {
return 1
}
return -1
}
if pt2.IsEqual(t[1]) {
if pt1.IsEqual(t[2]) {
return 1
}
return -1
}
return -1
}
func (t *Triangle) Key() string {
if t == nil {
return ""
}
sort.Sort(t)
return fmt.Sprintf("(%v %v %v)", t[0], t[1], t[2])
}
func (t *Triangle) Points() []Pt { return []Pt{t[0], t[1], t[2]} }
func (t *Triangle) Point(i int) Pt {
switch {
case i <= 0:
return t[0]
case i >= 2:
return t[2]
default:
return t[1]
}
}
func (t *Triangle) Len() int {
if t == nil {
return 0
}
return len(t)
}
// If t is nil, we want to panic, as this this a programming bug.
func (t *Triangle) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t *Triangle) Less(i, j int) bool { return XYOrder(t[i], t[j]) == -1 }
func (t *Triangle) Equal(t1 *Triangle) bool {
if t == nil || t1 == nil {
return t == t1
}
sort.Sort(t)
sort.Sort(t1)
return t[0].IsEqual(t1[0]) && t[1].IsEqual(t1[1]) && t[2].IsEqual(t1[2])
}
func (t *Triangle) EqualAnyPt(pts ...Pt) bool {
if t == nil {
return false
}
for _, pt := range pts {
if pt.IsEqual(t[0]) || pt.IsEqual(t[1]) || pt.IsEqual(t[2]) {
return true
}
}
return false
}
func AreaOfTriangle(v0, v1, v2 Pt) float64 {
d := (v0.X * (v1.Y - v2.Y)) + (v1.X * (v2.Y - v0.Y)) + (v2.X * (v0.Y - v1.Y))
a := d / 2
return a
}
func (t *Triangle) Area() float64 {
if t == nil {
return 0
}
area := AreaOfTriangle(t[0], t[1], t[2])
if area < 0 {
return 0 - area
}
return area
}
func (t *Triangle) Center() Pt {
if t == nil {
return Pt{0, 0}
}
return Pt{
X: (t[0].X + t[1].X + t[2].X) / 3,
Y: (t[0].Y + t[1].Y + t[2].Y) / 3,
}
}
// Will create a new Triangle and sort the points.k
func NewTriangle(pt1, pt2, pt3 Pt) (tri Triangle) {
if pt1 == pt2 || pt1 == pt3 || pt2 == pt3 {
panic(fmt.Sprintf("All three points of a triangle must be different. < %v , %v , %v >", pt1, pt2, pt3))
}
tri = Triangle{pt1, pt2, pt3}
sort.Sort(&tri)
return tri
}
type TriangleEdge struct {
Node *TriangleNode
IsConstrained bool
}
func (te *TriangleEdge) Dump() {
if te == nil || te.Node == nil {
// log.Println("Triangle: nil")
return
}
//log.Println("Triangle: ", te.Node.Key(), "Constrained:", te.IsConstrained)
}
// label is the he label for the triangle. Is in "inside" or "outside".
// TODO: gdey — would be make more sense to just have a bool here? IsInside or somthing like that?
type Label uint8
func (l Label) String() string {
switch l {
case Outside:
return "outside"
case Inside:
return "inside"
default:
return "unknown"
}
}
const (
Unknown Label = iota
Outside
Inside
)
type TriangleNode struct {
Triangle
// Edge 0 has pt's 0 and 1.
// Edge 1 has pt's 1 and 2.
// Edge 2 has pt's 0 and 2.
Neighbors [3]TriangleEdge
Label Label
}
func (tn *TriangleNode) Dump() {
if tn == nil {
//log.Println("Tringle: nil")
return
}
//log.Println("Triangle:", tn.Triangle.Key(), "label:", tn.Label)
for i, val := range tn.Neighbors {
_ = i
if val.Node == nil {
//log.Printf("\t %v : nil", i)
continue
}
//log.Printf("\t %v : %v \t Constrained: %v", i, val.Node.Key(), val.IsConstrained)
}
}
func (tn *TriangleNode) LabelAs(l Label, force bool) (unlabled []*TriangleNode) {
if tn == nil {
return unlabled
}
if !force && tn.Label != Unknown {
//log.Println("Skipping labeling", force, tn.Label)
return unlabled
}
tn.Label = l
//log.Printf("Labeling as %v: Neighbors are %#v", l, tn.Neighbors)
for i := range tn.Neighbors {
//log.Println("Unlabed:", unlabled)
if tn.Neighbors[i].IsConstrained {
// We add this edge to our unlabled array.
unlabled = append(unlabled, tn.Neighbors[i].Node)
continue
}
unlabled = append(unlabled, tn.Neighbors[i].Node.LabelAs(l, false)...)
}
//log.Println("Unlabed:", unlabled)
return unlabled
}
type TriangleGraph struct {
triangles []*TriangleNode
// List of the triangles that are labelled as outside.
outside []int
// List of the triangles that are labelled as inside.
inside []int
bounding []int
}
func (tg *TriangleGraph) Triangles() []*TriangleNode {
return tg.triangles
}
func (tg *TriangleGraph) TrianglesAsMP() (mp [][][]Pt) {
for i := range tg.triangles {
if tg.triangles[i] == nil {
continue
}
mp = append(mp, [][]Pt{tg.triangles[i].Triangle[:]})
}
return mp
}
func (tg *TriangleGraph) Inside() []*TriangleNode {
r := make([]*TriangleNode, 0, len(tg.inside))
for _, i := range tg.inside {
r = append(r, tg.triangles[i])
}
return r
}
func (tg *TriangleGraph) Outside() []*TriangleNode {
r := make([]*TriangleNode, 0, len(tg.outside))
for _, i := range tg.outside {
r = append(r, tg.triangles[i])
}
return r
}
func simplifyNumberOfLines(lines []Line) (sln []Line) {
var m1, m2 float64
var ok1, ok2 bool
if len(lines) <= 2 {
return lines
}
lineToAdd := lines[len(lines)-1]
m1, _, ok1 = lineToAdd.SlopeIntercept()
for i := 0; i < len(lines); i, m1, ok1 = i+1, m2, ok2 {
m2, _, ok2 = lines[i].SlopeIntercept()
if m1 != m2 || ok1 != ok2 {
sln = append(sln, lineToAdd)
lineToAdd = lines[i]
continue
}
switch {
case lineToAdd[0].IsEqual(lines[i][0]):
lineToAdd[0] = lines[i][1]
case lineToAdd[0].IsEqual(lines[i][1]):
lineToAdd[0] = lines[i][0]
case lineToAdd[1].IsEqual(lines[i][0]):
lineToAdd[1] = lines[i][0]
case lineToAdd[1].IsEqual(lines[i][1]):
lineToAdd[1] = lines[i][1]
case lineToAdd[1].IsEqual(lines[i][0]):
default:
sln = append(sln, lineToAdd)
lineToAdd = lines[i]
}
}
sln = append(sln, lineToAdd)
return sln
}
func (tg *TriangleGraph) Rings() (rings [][]Line) {
if tg == nil {
panic("TG nil!")
return rings
}
//log.Println("Starting TriangleGraph Rings")
//defer log.Println("Done with TriangleGraph Rings")
// the key is the index of the trianlgle in the graph array.
seen := make(map[string]struct{})
// We are going to walk all triangles that are labeled as inside nodes, this will generate a set
// of segments. The sements may form one or more close rings. We want to be greedy in the consumption of
// these segments when reassembling the segments into rings.
//log.Println("Going through inside.", tg.inside)
for _, i := range tg.inside {
//log.Printf("Looking at tg.triangle[%v].Key(%v)", i, tg.triangles[i].Key())
if _, ok := seen[tg.triangles[i].Key()]; ok {
//log.Println("Skipping ", i, tg.triangles[i].Key())
continue
}
nodesToProcess := []*TriangleNode{tg.triangles[i]}
var linesToProcess []Line
for offset := 0; offset != len(nodesToProcess); offset++ {
node := nodesToProcess[offset]
if _, ok := seen[node.Key()]; ok {
//log.Println("Skipping Node ", offset, node.Key())
continue
}
seen[node.Key()] = struct{}{}
for j := range node.Neighbors {
if node.Neighbors[j].Node != nil && node.Neighbors[j].Node.Label == node.Label {
nodesToProcess = append(nodesToProcess, node.Neighbors[j].Node)
} else {
//log.Println("Found edge to process: ", node.LREdge(j))
linesToProcess = append(linesToProcess, node.LREdge(j))
}
}
}
// Now need to deal with Lines.
//log.Println("Added lines to ring.", linesToProcess)
if len(linesToProcess) > 0 {
rings = append(rings, linesToProcess)
}
}
return rings
}
func NewTriangleGraph(tri []*TriangleNode, bbox [4]Pt) (tg *TriangleGraph) {
tg = &TriangleGraph{triangles: tri}
for i := range tg.triangles {
switch tg.triangles[i].Label {
case Inside:
tg.inside = append(tg.inside, i)
case Outside:
tg.outside = append(tg.outside, i)
if tg.triangles[i].EqualAnyPt(bbox[0], bbox[1], bbox[2], bbox[3]) {
tg.bounding = append(tg.bounding, i)
}
}
}
return tg
}
type ByXY []Pt
func (t ByXY) Len() int { return len(t) }
func (t ByXY) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t ByXY) Less(i, j int) bool { return XYOrder(t[i], t[j]) == -1 }
type ByXYLine []Line
func (t ByXYLine) Len() int { return len(t) }
func (t ByXYLine) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t ByXYLine) Less(i, j int) bool {
li, lj := t[i].LeftRightMostAsLine(), t[j].LeftRightMostAsLine()
ret := XYOrder(li[0], lj[0])
if ret == 0 {
ret = XYOrder(li[1], lj[1])
}
return ret == -1
}
func PointPairs(pts []Pt) ([][2]Pt, error) {
if len(pts) <= 1 {
return nil, fmt.Errorf("Not enough pts to make pairs.")
}
n := len(pts)
switch n {
case 2:
return [][2]Pt{
{pts[0], pts[1]},
}, nil
case 3:
return [][2]Pt{
{pts[0], pts[1]},
{pts[0], pts[2]},
{pts[1], pts[2]},
}, nil
case 4:
return [][2]Pt{
{pts[0], pts[1]},
{pts[0], pts[2]},
{pts[0], pts[3]},
{pts[1], pts[2]},
{pts[1], pts[3]},
{pts[2], pts[3]},
}, nil
default:
ret := make([][2]Pt, n*(n-1)/2)
c := 0
for i := 0; i < n-1; i++ {
for j := i + 1; j < n; j++ {
ret[c][0], ret[c][1] = pts[i], pts[j]
c++
}
}
return ret, nil
}
}
// insureConnected will add a connecting line as needed to the given polygons. If there is only one line in a polygon, it will be left alone.
func insureConnected(polygons ...[]Line) (ret [][]Line) {
ret = make([][]Line, len(polygons))
for i := range polygons {
ln := len(polygons[i])
if ln == 0 {
continue
}
ret[i] = append(ret[i], polygons[i]...)
if ln == 1 {
continue
}
if !polygons[i][ln-1][1].IsEqual(polygons[i][0][0]) {
ret[i] = append(ret[i], Line{polygons[i][ln-1][1], polygons[i][0][0]})
}
}
return ret
}
// desctucture will split the given polygons into their composite lines, breaking up lines at intersection points. It will remove lines that overlap as well. Polygons need to be fully connected before calling this function.
func destructure(polygons [][]Line) (lines []Line) {
// First we need to combine all the segments.
var segments []Line
{
segs := make(map[Line]struct{})
for i := range polygons {
for _, ln := range polygons[i] {
segs[ln.LeftRightMostAsLine()] = struct{}{}
}
}
for ln := range segs {
segments = append(segments, ln)
}
if len(segments) <= 1 {
return segments
}
sort.Sort(ByXYLine(segments))
}
// linesToSplit holds a list of points for that segment to be split at. This list will have to be
// ordered and deuped.
splitPts := make([][]Pt, len(segments))
FindIntersects(segments, func(src, dest int, ptfn func() Pt) bool {
sline, dline := segments[src], segments[dest]
pt := ptfn() // left most point.
pt.X = float64(int64(pt.X))
pt.Y = float64(int64(pt.Y))
if !(pt.IsEqual(sline[0]) || pt.IsEqual(sline[1])) {
splitPts[src] = append(splitPts[src], pt)
}
if !(pt.IsEqual(dline[0]) || pt.IsEqual(dline[1])) {
splitPts[dest] = append(splitPts[dest], pt)
}
return true
})
for i := range segments {
if splitPts[i] == nil {
lines = append(lines, segments[i].LeftRightMostAsLine())
continue
}
sort.Sort(ByXY(splitPts[i]))
lidx, ridx := Line(segments[i]).XYOrderedPtsIdx()
lpt, rpt := segments[i][lidx], segments[i][ridx]
for j := range splitPts[i] {
if lpt.IsEqual(splitPts[i][j]) {
// Skipp dups.
continue
}
lines = append(lines, Line{lpt, splitPts[i][j]}.LeftRightMostAsLine())
lpt = splitPts[i][j]
}
if !lpt.IsEqual(rpt) {
lines = append(lines, Line{lpt, rpt}.LeftRightMostAsLine())
}
}
sort.Sort(ByXYLine(lines))
return lines
}
// We only care about the first triangle node, as an edge can only contain two triangles.
type aNodeList map[[2]Pt]*TriangleNode
// AddTrinagleForPts will order the points, create a new Triangle and add it to the Node List.
func (nl aNodeList) AddTriangleForPts(pt1, pt2, pt3 Pt, fnIsConstrained func(pt1, pt2 Pt) bool) (tri *TriangleNode, err error) {
var fn = func(pt1, pt2 Pt) bool { return false }
if fnIsConstrained != nil {
fn = fnIsConstrained
}
tri = &TriangleNode{Triangle: NewTriangle(pt1, pt2, pt3)}
for i := range tri.Points() {
j := i + 1
if i == 2 {
j = 0
}
edge := [2]Pt{tri.Point(i), tri.Point(j)}
tri.Neighbors[i].IsConstrained = fn(edge[0], edge[1])
node, ok := nl[edge]
if !ok {
nl[edge] = tri
continue
}
tri.Neighbors[i].Node = node
// Find the point idx so we know which slot we need to add ourself to.
for k, pt := range node.Triangle {
if !pt.IsEqual(edge[1]) {
continue
}
if node.Neighbors[k].Node != nil {
// return an error
//log.Printf("More then two triangles are sharing an edge. \n\t%+v\n\t%v\n\t%+v\n\t %v %v %v", node, k, node.Neighbors[k].Node, pt1, pt2, pt3)
panic("More then two triangles are sharing an edge.")
return nil, fmt.Errorf("More then two triangles are sharing an edge. \n\t%+v\n\t%v\n\t%+v\n\t %v %v %v", node, k, node.Neighbors[k].Node, pt1, pt2, pt3)
}
// Assign ourself to the Neighbor's correct slot.
node.Neighbors[k].Node = tri
}
}
return tri, nil
}
type EdgeMap struct {
Keys []Pt
Map map[Pt]map[Pt]bool
Segments []Line
BBox [4]Pt
destructured []Line
}
func (em *EdgeMap) SubKeys(pt Pt) (skeys []Pt, ok bool) {
sem, ok := em.Map[pt]
if !ok {
return skeys, false
}
for k := range sem {
if k.IsEqual(pt) {
continue
}
skeys = append(skeys, k)
}
sort.Sort(ByXY(skeys))
return skeys, ok
}
func (em *EdgeMap) trianglesForEdge(pt1, pt2 Pt) (*Triangle, *Triangle, error) {
apts, ok := em.SubKeys(pt1)
if !ok {
log.Println("Error 1")
return nil, nil, fmt.Errorf("Point one is not connected to any other points. Invalid edge? (%v %v)", pt1, pt2)
}
bpts, ok := em.SubKeys(pt2)
if !ok {
log.Println("Error 2")
return nil, nil, fmt.Errorf("Point two is not connected to any other points. Invalid edge? (%v %v)", pt1, pt2)
}
// Check to make sure pt1 and pt2 are connected.
if _, ok := em.Map[pt1][pt2]; !ok {
log.Println("Error 3")
return nil, nil, fmt.Errorf("Point one and Point do not form an edge. Invalid edge? (%v %v)", pt1, pt2)
}
// Now we need to look at the both subpts and only keep the points that are common to both lists.
var larea, rarea float64
var triangles [2]*Triangle
NextApts:
for i := range apts {
NextBpts:
for j := range bpts {
if apts[i].IsEqual(bpts[j]) {
tri := NewTriangle(pt1, pt2, apts[i])
area := AreaOfTriangle(pt1, pt2, apts[i])
switch {
case area > 0 && (rarea == 0 || rarea > area):
rarea = area
triangles[1] = &tri
case area < 0 && (larea == 0 || larea < area):
larea = area
triangles[0] = &tri
case area == 0:
// Skip lines with zero area.
continue NextBpts
}
continue NextApts
}
}
}
return triangles[0], triangles[1], nil
}
func generateEdgeMap(destructuredLines []Line) (em EdgeMap) {
em.destructured = destructuredLines
em.Map = make(map[Pt]map[Pt]bool)
em.Segments = make([]Line, 0, len(destructuredLines))
// First we need to combine all the segments.
var minPt, maxPt Pt
for i := range destructuredLines {
seg := destructuredLines[i]
pt1, pt2 := seg[0], seg[1]
if pt1.IsEqual(pt2) {
continue // skip point lines
}
if _, ok := em.Map[pt1]; !ok {
em.Map[pt1] = make(map[Pt]bool)
}
if _, ok := em.Map[pt2]; !ok {
em.Map[pt2] = make(map[Pt]bool)
}
if _, ok := em.Map[pt1][pt2]; ok {
// skip this point as we have already dealt with this point set.
continue
}
em.Segments = append(em.Segments, seg)
em.Map[pt1][pt2] = true
em.Map[pt2][pt1] = true
// Find the min and max points in the segements we where given. This basically creates a bounding box.
// Deal with the min values.
if minPt.X > seg[0].X {
minPt.X = seg[0].X
}
if minPt.X > seg[1].X {
minPt.X = seg[1].X
}
if minPt.Y > seg[0].Y {
minPt.Y = seg[0].Y
}
if minPt.Y > seg[1].Y {
minPt.Y = seg[1].Y
}
// Deal with max values
if maxPt.X < seg[0].X {
maxPt.X = seg[0].X
}
if maxPt.X < seg[1].X {
maxPt.X = seg[1].X
}
if maxPt.Y < seg[0].Y {
maxPt.Y = seg[0].Y
}
if maxPt.Y < seg[1].Y {
maxPt.Y = seg[1].Y
}
}
// Build out the keys:
for k := range em.Map {
em.Keys = append(em.Keys, k)
}
// We are calculating a bound box around all of the given polygons. These external points will
// be used during the labeling phase.
// The adjustBBoxBy is just an arbitrary number, we just care that is out further then the largest and smallest points.
minPt.X, minPt.Y, maxPt.X, maxPt.Y = minPt.X-adjustBBoxBy, minPt.Y-adjustBBoxBy, maxPt.X+adjustBBoxBy, maxPt.Y+adjustBBoxBy
bbv1, bbv2, bbv3, bbv4 := minPt, Pt{maxPt.X, minPt.Y}, maxPt, Pt{minPt.X, maxPt.Y}
// We want our bound box to be in a known position. so that we can able things correctly.
em.Segments = append([]Line{
{bbv1, bbv2}, // Top edge
{bbv2, bbv3}, // right edge
{bbv3, bbv4}, // bottom edge
{bbv4, bbv1}, // left edge
}, em.Segments...)
em.BBox = [4]Pt{bbv1, bbv2, bbv3, bbv4}
em.addLine(false, false, true, em.Segments[0:4]...)
keys := em.Keys
sort.Sort(ByXY(keys))
em.Keys = []Pt{keys[0]}
lk := keys[0]
for _, k := range keys[1:] {
if lk.IsEqual(k) {
continue
}
em.Keys = append(em.Keys, k)
lk = k
}
return em
}
func (em *EdgeMap) addLine(constrained bool, addSegments bool, addKeys bool, lines ...Line) {
if em == nil {
return
}
if em.Map == nil {
em.Map = make(map[Pt]map[Pt]bool)
}
for _, l := range lines {
pt1, pt2 := l[0], l[1]
if _, ok := em.Map[pt1]; !ok {
em.Map[pt1] = make(map[Pt]bool)
}
if _, ok := em.Map[l[1]]; !ok {
em.Map[pt2] = make(map[Pt]bool)
}
if con, ok := em.Map[pt1][pt2]; !ok || !con {
em.Map[pt1][pt2] = constrained
}
if con, ok := em.Map[pt2][pt1]; !ok || !con {
em.Map[pt2][pt1] = constrained
}
if addKeys {
em.Keys = append(em.Keys, pt1, pt2)
}
if addSegments {
em.Segments = append(em.Segments, l)
}
}
}
func (em *EdgeMap) Dump() {
/*
log.Println("Edge Map:")
log.Printf("generateEdgeMap(%#v)", em.destructured)
if em == nil {
log.Println("nil")
}
log.Println("\tKeys:", em.Keys)
log.Println("\tMap:")
var keys []Pt
for k := range em.Map {
keys = append(keys, k)
}
sort.Sort(ByXY(keys))
for _, k := range keys {
log.Println("\t\t", k, ":\t", len(em.Map[k]), em.Map[k])
}
log.Println("\tSegments:")
for _, seg := range em.Segments {
log.Println("\t\t", seg)
}
log.Printf("\tBBox:%v", em.BBox)
*/
}
func (em *EdgeMap) Triangulate1() {
//defer log.Println("Done with Triangulate")
keys := em.Keys
//log.Println("Starting to Triangulate. Keys", len(keys))
// We want to run through all the keys generating possible edges, and then
// collecting the ones that don't intersect with the edges in the map already.
var lines = make([]Line, 0, 2*len(keys))
stime := time.Now()
for i := 0; i < len(keys)-1; i++ {
lookup := em.Map[keys[i]]
//log.Println("Looking at i", i, "Lookup", lookup)
for j := i + 1; j < len(keys); j++ {
if _, ok := lookup[keys[j]]; ok {
// Already have an edge with this point
continue
}
l := Line{keys[i], keys[j]}
lines = append(lines, l)
}
}
etime := time.Now()
log.Println("Finding all lines took: ", etime.Sub(stime))
// Now we need to do a line sweep to see which of the possible edges we want to keep.
offset := len(lines)
lines = append(lines, em.Segments...)
// Assume we are going to keep all the edges we generated.
//skiplines := make([]bool, len(lines))
skiplines := make(map[int]bool, offset)
stime = time.Now()
eq := NewEventQueue(lines)
etime = time.Now()
log.Println("building event queue took: ", etime.Sub(stime))
stime = etime
FindAllIntersectsWithEventQueueWithoutIntersectNotPolygon(eq, lines,
func(src, dest int) bool { return skiplines[src] || skiplines[dest] },
func(src, dest int) {
/*
if src >= offset && dest >= offset {
return
}
//these are two Segments intersecting with each other ignore.
if src < offset && dest < offset {
skiplines[dest] = true
return
}
*/
if dest < offset {
skiplines[dest] = true
return
}
if src < offset {
skiplines[src] = true
return
}
})
etime = time.Now()
log.Println("Find Intersects took: ", etime.Sub(stime))
stime = etime
// Add the remaining possible Edges to the edgeMap.
for i := range lines {
if _, ok := skiplines[i]; ok {
continue
}
// Don't need to add the keys as they are already in the edgeMap, we are just adding additional edges
// between points.
em.addLine(false, true, false, lines[i])
}
}
func (em *EdgeMap) Triangulate() {
//defer log.Println("Done with Triangulate")
keys := em.Keys
lnkeys := len(keys) - 1
//log.Println("Starting to Triangulate. Keys", len(keys))
// We want to run through all the keys generating possible edges, and then
// collecting the ones that don't intersect with the edges in the map already.
for i := 0; i < lnkeys; i++ {
lookup := em.Map[keys[i]]
var possibleEdges []Line
for j := i + 1; j < len(keys); j++ {
if _, ok := lookup[keys[j]]; ok {
// Already have an edge with this point
continue
}
l := Line{keys[i], keys[j]}
possibleEdges = append(possibleEdges, l)
}
// Now we need to do a line sweep to see which of the possible edges we want to keep.
lines := append([]Line{}, possibleEdges...)
offset := len(lines)
lines = append(lines, em.Segments...)
skiplines := make([]bool, offset)
/*
stime := time.Now()
*/
eq := NewEventQueue(lines)
/*
etime := time.Now()
log.Println(i, "of", lnkeys, "building event queue took: ", etime.Sub(stime))
stime = etime
*/
FindAllIntersectsWithEventQueueWithoutIntersectNotPolygon(eq, lines,
func(src, dest int) bool {
if src >= offset && dest >= offset {
return true
}
if src < offset && skiplines[src] {
return true
}
if dest < offset && skiplines[dest] {
return true
}
return false
},
func(src, dest int) {
if src < offset {
// need to remove this possible edge.
if dest >= offset {
skiplines[src] = true
}
return
}
if dest < offset {
// need to remove this possible edge.
if src >= offset {
skiplines[dest] = true
}
return
}
})
/*
etime = time.Now()
log.Println(i, "of", lnkeys, "Find Intersects took: ", etime.Sub(stime))
*/
// Add the remaining possible Edges to the edgeMap.
for i := range possibleEdges {
if skiplines[i] {
continue
}
// Don't need to add the keys as they are already in the edgeMap, we are just adding additional edges
// between points.
em.addLine(false, true, false, possibleEdges[i])
}
}
}
func (em *EdgeMap) FindTriangles() (*TriangleGraph, error) {
//log.Println("Starting FindTriangles")
//defer log.Println("Done with FindTriangles")