-
Notifications
You must be signed in to change notification settings - Fork 3
/
connector.go
88 lines (69 loc) · 1.75 KB
/
connector.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
package martinez_rueda
import (
"github.com/paulmach/orb"
)
type Connector struct {
openPolygons []*PointChain
closedPolygons []*PointChain
closed bool
}
func NewConnector() Connector {
return Connector{
openPolygons: []*PointChain{},
closedPolygons: []*PointChain{},
closed: false,
}
}
func (c *Connector) isClosed() bool {
return c.closed
}
func (c *Connector) add(segment Segment) {
size := len(c.openPolygons)
for jdx := 0; jdx < size; jdx++ {
chain := c.openPolygons[jdx]
isLinkSegment := chain.linkSegment(segment)
if !isLinkSegment {
continue
}
if chain.closed {
if len(chain.segments) == 2 {
chain.closed = false
return
}
c.closedPolygons = append(c.closedPolygons, c.openPolygons[jdx])
//append(s[:index], s[index+1:]...)
c.openPolygons = append(c.openPolygons[:jdx], c.openPolygons[(jdx+1):]...)
return
}
// if chain not closed
k := len(c.openPolygons)
for idx := jdx + 1; idx < k; idx++ {
v := c.openPolygons[idx]
if chain.linkChain(v) {
//append(s[:index], s[index+1:]...)
c.openPolygons = append(c.openPolygons[:idx], c.openPolygons[(idx+1):]...)
return
}
}
return
}
newChain := NewPointChain(segment)
c.openPolygons = append(c.openPolygons, newChain)
}
func (c *Connector) toPolygon() *Polygon {
contours := []Contour{}
for _, cp := range c.closedPolygons {
contourPoints := []orb.Point{}
for _, point := range cp.segments {
contourPoints = append(contourPoints, point)
}
// close contour
first := contourPoints[0]
last := contourPoints[len(contourPoints)-1]
if first.Equal(last) == false {
contourPoints = append(contourPoints, first)
}
contours = append(contours, NewContour(contourPoints))
}
return NewPolygon(contours)
}