forked from xtaci/navmesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.go
152 lines (132 loc) · 3.69 KB
/
dijkstra.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
package navmesh
import (
"container/heap"
. "github.com/spate/vectormath"
"math"
)
const LARGE_NUMBER = math.MaxInt32
// Triangle Heap
type WeightedTriangle struct {
id int32 // triangle id
weight uint32
}
type TriangleHeap struct {
triangles []WeightedTriangle
indices map[int32]int
}
func NewTriangleHeap() *TriangleHeap {
h := new(TriangleHeap)
h.indices = make(map[int32]int)
return h
}
func (th *TriangleHeap) Len() int {
return len(th.triangles)
}
func (th *TriangleHeap) Less(i, j int) bool {
return th.triangles[i].weight < th.triangles[j].weight
}
func (th *TriangleHeap) Swap(i, j int) {
th.triangles[i], th.triangles[j] = th.triangles[j], th.triangles[i]
th.indices[th.triangles[i].id] = i
th.indices[th.triangles[j].id] = j
}
func (th *TriangleHeap) Push(x interface{}) {
th.triangles = append(th.triangles, x.(WeightedTriangle))
n := len(th.triangles)
th.indices[th.triangles[n-1].id] = n - 1
}
func (th *TriangleHeap) Pop() interface{} {
n := len(th.triangles)
x := th.triangles[n-1]
th.triangles = th.triangles[:n-1]
return x
}
func (th *TriangleHeap) DecreaseKey(id int32, weight uint32) {
if index, ok := th.indices[id]; ok {
th.triangles[index].weight = weight
heap.Fix(th, index)
return
} else {
heap.Push(th, WeightedTriangle{id, weight})
}
}
type Mesh struct {
Vertices []Point3 // vertices
Triangles [][3]int32 // triangles
}
// Dijkstra
type Dijkstra struct {
Matrix map[int32][]WeightedTriangle // all edge for nodes
}
// create neighbour matrix
func (d *Dijkstra) CreateMatrixFromMesh(mesh Mesh) {
d.Matrix = make(map[int32][]WeightedTriangle)
for i := 0; i < len(mesh.Triangles); i++ {
for j := 0; j < len(mesh.Triangles); j++ {
if i == j {
continue
}
if len(intersect(mesh.Triangles[i], mesh.Triangles[j])) == 2 {
x1 := (mesh.Vertices[mesh.Triangles[i][0]].X + mesh.Vertices[mesh.Triangles[i][1]].X + mesh.Vertices[mesh.Triangles[i][2]].X) / 3.0
y1 := (mesh.Vertices[mesh.Triangles[i][0]].Y + mesh.Vertices[mesh.Triangles[i][1]].Y + mesh.Vertices[mesh.Triangles[i][2]].Y) / 3.0
x2 := (mesh.Vertices[mesh.Triangles[j][0]].X + mesh.Vertices[mesh.Triangles[j][1]].X + mesh.Vertices[mesh.Triangles[j][2]].X) / 3.0
y2 := (mesh.Vertices[mesh.Triangles[j][0]].Y + mesh.Vertices[mesh.Triangles[j][1]].Y + mesh.Vertices[mesh.Triangles[j][2]].Y) / 3.0
weight := math.Sqrt(float64((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)))
d.Matrix[int32(i)] = append(d.Matrix[int32(i)], WeightedTriangle{int32(j), uint32(weight)})
}
}
}
}
func intersect(a [3]int32, b [3]int32) []int32 {
var inter []int32
for i := range a {
for j := range b {
if a[i] == b[j] {
inter = append(inter, a[i])
}
}
}
return inter
}
func (d *Dijkstra) Run(src_id int32) []int32 {
// triangle heap
h := NewTriangleHeap()
// min distance records
dist := make([]uint32, len(d.Matrix))
for i := 0; i < len(dist); i++ {
dist[i] = LARGE_NUMBER
}
// previous
prev := make([]int32, len(d.Matrix))
for i := 0; i < len(prev); i++ {
prev[i] = -1
}
// visit map
visited := make([]bool, len(d.Matrix))
// source vertex, the first vertex in Heap
dist[src_id] = 0
heap.Push(h, WeightedTriangle{src_id, 0})
for h.Len() > 0 { // for every un-visited vertex, try relaxing the path
// pop the min element
u := heap.Pop(h).(WeightedTriangle)
if visited[u.id] {
continue
}
// current known shortest distance to u
dist_u := dist[u.id]
// mark the vertex as visited.
visited[u.id] = true
// for each neighbor v of u:
for _, v := range d.Matrix[u.id] {
alt := dist_u + v.weight // from src->u->v
if alt < dist[v.id] {
dist[v.id] = alt
prev[v.id] = u.id
if !visited[v.id] {
h.DecreaseKey(v.id, alt)
}
}
}
}
return prev
}