-
Notifications
You must be signed in to change notification settings - Fork 2
/
dcel.go
173 lines (153 loc) · 3.86 KB
/
dcel.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
package voronoi
import (
"fmt"
"math"
"sort"
"github.com/quasoft/dcel"
)
// CloseTwins adds a vertex to the specified edges.
func (v *Voronoi) CloseTwins(list []*dcel.HalfEdge, vertex *dcel.Vertex) {
for i := 0; i < len(list); i++ {
he := list[i]
if he.Twin != nil && he.Twin.Target == nil {
he.Twin.Target = vertex
} else if he.Target == nil {
he.Target = vertex
}
}
}
// halfEdgesByCCW implements a slice of half-edges that sort in counter-clockwise order.
type halfEdgesByCCW []*dcel.HalfEdge
func (s halfEdgesByCCW) Len() int {
return len(s)
}
func (s halfEdgesByCCW) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s halfEdgesByCCW) Less(i, j int) bool {
if s[i].Target == nil {
return false
} else if s[j].Target == nil {
return true
}
// Find center of polygon
var sumX int64
var sumY int64
var cnt int
for _, v := range s {
if v.Target != nil {
sumX += int64(v.Target.X)
sumY += int64(v.Target.Y)
cnt++
}
}
centerX := float64(sumX) / float64(cnt)
centerY := float64(sumY) / float64(cnt)
// Sort counter-clockwise
a1 := math.Atan2(float64(s[i].Target.Y)-centerY, float64(s[i].Target.X)-centerX)
a2 := math.Atan2(float64(s[j].Target.Y)-centerY, float64(s[j].Target.X)-centerX)
return a1 >= a2
}
func (s halfEdgesByCCW) UpdateLinks() {
for i := 0; i < len(s); i++ {
if i > 0 {
s[i].Prev, s[i-1].Next = s[i-1], s[i]
}
if i < len(s)-1 {
s[i].Next, s[i+1].Prev = s[i+1], s[i]
}
}
if len(s) == 1 {
s[0].Prev, s[0].Next = nil, nil
} else if len(s) > 1 {
s[0].Prev, s[len(s)-1].Next = s[len(s)-1], s[0]
}
}
// ReorderFaceEdges reorders face half-edges in a clockwise way, while also removing duplicates.
func (v *Voronoi) ReorderFaceEdges(face *dcel.Face) {
var edges []*dcel.HalfEdge
//exists := make(map[string]bool)
edge := face.HalfEdge
for edge != nil {
/*
id := fmt.Sprintf("%v", edge.Target)
if !exists[id] {
exists[id] = true
edges = append(edges, edge)
}
*/
edges = append(edges, edge)
edge = edge.Next
if edge == face.HalfEdge {
break
}
}
sort.Sort(halfEdgesByCCW(edges))
halfEdgesByCCW(edges).UpdateLinks()
}
// GetFaceHalfEdges returns the half-edges that form the boundary of a face (cell).
func (v *Voronoi) GetFaceHalfEdges(face *dcel.Face) []*dcel.HalfEdge {
v.ReorderFaceEdges(face)
var edges []*dcel.HalfEdge
edge := face.HalfEdge
for edge != nil {
edges = append(edges, edge)
edge = edge.Next
if edge == face.HalfEdge {
break
}
}
return edges
}
// verticesByCCW implements a slice of vertices that sort in counter-clockwise order.
type verticesByCCW []*dcel.Vertex
func (s verticesByCCW) Len() int {
return len(s)
}
func (s verticesByCCW) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s verticesByCCW) Less(i, j int) bool {
// Find center of polygon
var sumX float64
var sumY float64
for _, v := range s {
sumX += float64(v.X)
sumY += float64(v.Y)
}
centerX := sumX / float64(len(s))
centerY := sumY / float64(len(s))
// Sort counter-clockwise
a1 := math.Atan2(float64(s[i].Y)-centerY, float64(s[i].X)-centerX)
a2 := math.Atan2(float64(s[j].Y)-centerY, float64(s[j].X)-centerX)
return a1 >= a2
}
// GetFaceVertices returns the vertices that form the boundary of a face (cell),
// sorted in counter-clockwise order.
func (v *Voronoi) GetFaceVertices(face *dcel.Face) []*dcel.Vertex {
var vertices []*dcel.Vertex
exists := make(map[string]bool)
edge := face.HalfEdge
for edge != nil {
if edge.Target != nil {
id := fmt.Sprintf("%v", edge.Target)
if !exists[id] {
exists[id] = true
vertices = append(vertices, edge.Target)
}
}
if edge.Twin != nil && edge.Twin.Target != nil {
id := fmt.Sprintf("%v", edge.Twin.Target)
if !exists[id] {
exists[id] = true
vertices = append(vertices, edge.Twin.Target)
}
}
edge = edge.Next
if edge == face.HalfEdge {
break
}
}
sort.Sort(verticesByCCW(vertices))
return vertices
}