-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadvancingfront.go
131 lines (111 loc) · 2.15 KB
/
advancingfront.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
package poly2tri
type Node struct {
Point *Point
Triangle *Triangle
Next *Node
Prev *Node
Value float64
}
func NewNode(p *Point, t *Triangle) *Node {
return &Node{
Point: p,
Triangle: t,
Next: nil,
Prev: nil,
Value: p.X,
}
}
type AdvancingFront struct {
Head *Node
Tail *Node
SearchNode *Node
}
func NewAdvancingFront(head, tail *Node) *AdvancingFront {
return &AdvancingFront{
Head: head,
Tail: tail,
SearchNode: head,
}
}
func (af *AdvancingFront) GetHead() *Node {
return af.Head
}
func (af *AdvancingFront) SetHead(node *Node) {
af.Head = node
}
func (af *AdvancingFront) GetTail() *Node {
return af.Tail
}
func (af *AdvancingFront) SetTail(node *Node) {
af.Tail = node
}
func (af *AdvancingFront) GetSearch() *Node {
return af.SearchNode
}
func (af *AdvancingFront) SetSearch(node *Node) {
af.SearchNode = node
}
func (af *AdvancingFront) FindSearchNode(x float64) *Node {
return af.GetSearch()
}
func (af *AdvancingFront) LocateNode(x float64) *Node {
node := af.SearchNode
if x < node.Value {
node = node.Prev
for node != nil {
if x >= node.Value {
af.SearchNode = node
return node
}
node = node.Prev
}
} else {
node = node.Next
for node != nil {
if x < node.Value {
af.SearchNode = node.Prev
return node.Prev
}
node = node.Next
}
}
return nil
}
func (af *AdvancingFront) LocatePoint(point *Point) *Node {
px := point.X
node := af.FindSearchNode(px)
nx := node.Point.X
if px == nx {
// Here we are comparing point references, not values
if point != node.Point {
// We might have two nodes with same x value for a short time
if point == node.Prev.Point {
node = node.Prev
} else if point == node.Next.Point {
node = node.Next
} else {
panic("poly2tri Invalid AdvancingFront.locatePoint() call")
}
}
} else if px < nx {
node = node.Prev
for node != nil {
if point == node.Point {
break
}
node = node.Prev
}
} else {
node = node.Next
for node != nil {
if point == node.Point {
break
}
node = node.Next
}
}
if node != nil {
af.SearchNode = node
}
return node
}