-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontour.go
147 lines (119 loc) · 2.83 KB
/
contour.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
package martinez_rueda
import (
"github.com/paulmach/orb"
"math"
)
type Contour struct {
points []orb.Point
holes []int
isExternal bool
cc bool
precomputedCC *bool
}
func NewContour(points []orb.Point) Contour {
return Contour{
points: points,
}
}
func (c *Contour) Add(point orb.Point) {
c.points = append(c.points, point)
}
func (c *Contour) erase(index int) {
// unset($this->points[$index]);
c.points = append(c.points[:index], c.points[(index+1):]...)
}
func (c *Contour) clear() {
c.points = []orb.Point{}
c.holes = []int{}
}
func (c *Contour) addHole(index int) {
c.holes = append(c.holes, index)
}
// Get the p-th vertex of the external contour
func (c *Contour) vertex(index int) orb.Point {
return c.points[index]
}
func (c *Contour) segment(index int) Segment {
if index == c.nvertices()-1 {
return NewSegment(c.points[len(c.points)-1], c.points[0])
}
return NewSegment(c.points[index], c.points[index+1])
}
func (c *Contour) hole(index int) int {
return c.holes[index]
}
// Get minimum bounding rectangle
// ['min' => Point, 'max' => Point]
func (c *Contour) getBoundingBox() []orb.Point {
minX := math.Inf(1)
minY := math.Inf(1)
maxX := math.Inf(-1)
maxY := math.Inf(-1)
for idx := 0; idx < len(c.points); idx++ {
point := c.points[idx]
if point.X() < minX {
minX = point.X()
}
if point.X() > maxX {
maxX = point.X()
}
if point.Y() < minY {
minY = point.Y()
}
if point.Y() > maxY {
maxY = point.Y()
}
}
return []orb.Point{orb.Point{minX, minY}, orb.Point{maxX, maxY}}
}
func (c *Contour) counterClockwise() bool {
if c.precomputedCC != nil {
return *c.precomputedCC
}
precomputedCC := true
c.precomputedCC = &precomputedCC
var area float64
for idx := 0; idx < len(c.points)-1; idx++ {
area = area + c.vertex(idx).X()*c.vertex(idx+1).Y() - c.vertex(idx+1).X()*c.vertex(idx).Y()
}
area = area + c.vertex(len(c.points)-1).X()*c.vertex(0).Y() - c.vertex(0).X()*c.vertex(len(c.points)-1).Y()
c.cc = area >= 0.0
return c.cc
}
func (c *Contour) clockwise() bool {
return !c.counterClockwise()
}
func (c *Contour) changeOrientation() {
c.points = segmentsReverse(c.points)
c.cc = !c.cc
}
func (c *Contour) setClockwise() {
if c.counterClockwise() {
c.changeOrientation()
}
}
func (c *Contour) setCounterClockwise() {
if c.clockwise() {
c.changeOrientation()
}
}
func (c *Contour) external() bool {
return c.isExternal
}
func (c *Contour) setExternal(flag bool) {
c.isExternal = flag
}
func (c *Contour) move(x, y float64) {
for idx := 0; idx < len(c.points); idx++ {
c.points[idx] = orb.Point{c.points[idx].X() + x, c.points[idx].Y() + y}
}
}
func (c *Contour) nvertices() int {
return len(c.points)
}
func (c *Contour) GetPoint(index int) orb.Point {
return c.points[index]
}
func (c *Contour) Nvertices() int {
return c.nvertices()
}