forked from mitroadmaps/gomapinfer
/
graph.go
353 lines (311 loc) · 7.5 KB
/
graph.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package common
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
type Node struct {
ID int
Point Point
In []*Edge
Out []*Edge
}
func (node *Node) String() string {
return fmt.Sprintf("Node(%v)", node.Point)
}
func (node *Node) RemoveEdge(edge *Edge) {
filterFunc := func(edges []*Edge) []*Edge {
var n []*Edge
for _, e := range edges {
if e == edge {
continue
}
n = append(n, e)
}
return n
}
node.In = filterFunc(node.In)
node.Out = filterFunc(node.Out)
}
type Edge struct {
ID int
Src *Node
Dst *Node
}
func (edge *Edge) Segment() Segment {
return Segment{edge.Src.Point, edge.Dst.Point}
}
func (edge *Edge) Vector() Point {
return edge.Segment().Vector()
}
func (edge *Edge) AngleTo(other *Edge) float64 {
return edge.Segment().AngleTo(other.Segment())
}
func (edge *Edge) IsAdjacent(other *Edge) bool {
for _, a := range []*Node{edge.Src, edge.Dst} {
for _, b := range []*Node{other.Src, other.Dst} {
if a == b {
return true
}
}
}
return false
}
func (edge *Edge) ClosestPos(point Point) EdgePos {
return EdgePos{
Edge: edge,
Position: edge.Segment().Project(point, false),
}
}
func (edge *Edge) GetOpposite() *Edge {
for _, other := range edge.Dst.Out {
if other.Dst == edge.Src {
return other
}
}
return nil
}
func (edge *Edge) String() string {
return fmt.Sprintf("Edge(%v -> %v)", edge.Src.Point, edge.Dst.Point)
}
type EdgePos struct {
Edge *Edge
Position float64
}
func (ep EdgePos) Point() Point {
return ep.Edge.Segment().PointAtFactor(ep.Position, false)
}
type Graph struct {
Nodes []*Node
Edges []*Edge
}
func (graph *Graph) Bounds() Rectangle {
r := EmptyRectangle
for _, node := range graph.Nodes {
r = r.Extend(node.Point)
}
return r
}
func (graph *Graph) LonLatToMeters(origin Point) {
for _, node := range graph.Nodes {
node.Point = node.Point.LonLatToMeters(origin)
}
}
func (graph *Graph) MetersToLonLat(origin Point) {
for _, node := range graph.Nodes {
node.Point = node.Point.MetersToLonLat(origin)
}
}
func (graph *Graph) AddNode(point Point) *Node {
node := &Node{
ID: len(graph.Nodes),
Point: point,
}
graph.Nodes = append(graph.Nodes, node)
return node
}
func (graph *Graph) FindEdge(src *Node, dst *Node) *Edge {
for _, edge := range src.Out {
if edge.Dst == dst {
return edge
}
}
return nil
}
func (graph *Graph) AddEdge(src *Node, dst *Node) *Edge {
if edge := graph.FindEdge(src, dst); edge != nil {
return edge
}
edge := &Edge{
ID: len(graph.Edges),
Src: src,
Dst: dst,
}
graph.Edges = append(graph.Edges, edge)
edge.Src.Out = append(edge.Src.Out, edge)
edge.Dst.In = append(edge.Dst.In, edge)
return edge
}
func (graph *Graph) AddBidirectionalEdge(v1 *Node, v2 *Node) [2]*Edge {
edge1 := graph.AddEdge(v1, v2)
edge2 := graph.AddEdge(v2, v1)
return [2]*Edge{edge1, edge2}
}
func (graph *Graph) GetSubgraphInRect(r Rectangle) *Graph {
ngraph := &Graph{}
nodeMap := make(map[int]*Node)
for _, node := range graph.Nodes {
if r.Contains(node.Point) {
nodeMap[node.ID] = ngraph.AddNode(node.Point)
}
}
for _, edge := range graph.Edges {
if nodeMap[edge.Src.ID] != nil && nodeMap[edge.Dst.ID] != nil {
ngraph.AddEdge(nodeMap[edge.Src.ID], nodeMap[edge.Dst.ID])
}
}
return ngraph
}
func (graph *Graph) MakeBidirectional() {
for _, edge := range graph.Edges {
graph.AddEdge(edge.Dst, edge.Src)
}
}
func (graph *Graph) Clone() *Graph {
other := &Graph{}
for _, node := range graph.Nodes {
other.AddNode(node.Point)
}
for _, edge := range graph.Edges {
other.AddEdge(other.Nodes[edge.Src.ID], other.Nodes[edge.Dst.ID])
}
return other
}
func (graph *Graph) FilterEdgesWithMaps(badEdges map[int]bool) (*Graph, map[int]*Node, map[int]*Edge) {
other := &Graph{}
nodeMap := make(map[int]*Node)
edgeMap := make(map[int]*Edge)
for _, edge := range graph.Edges {
if badEdges[edge.ID] {
continue
}
for _, node := range []*Node{edge.Src, edge.Dst} {
if nodeMap[node.ID] == nil {
nodeMap[node.ID] = other.AddNode(node.Point)
}
}
nedge := other.AddEdge(nodeMap[edge.Src.ID], nodeMap[edge.Dst.ID])
edgeMap[edge.ID] = nedge
}
return other, nodeMap, edgeMap
}
func (graph *Graph) FilterEdges(badEdges map[int]bool) *Graph {
ngraph, _, _ := graph.FilterEdgesWithMaps(badEdges)
return ngraph
}
func ReadGraph(fname string) (*Graph, error) {
file, err := os.Open(fname)
if err != nil {
return nil, err
}
defer file.Close()
reader := bufio.NewReader(file)
section := "vertices"
var graph Graph
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
} else {
return nil, err
}
}
line = strings.TrimSpace(line)
if section == "vertices" {
if line == "" {
section = "edges"
continue
}
parts := strings.Split(line, " ")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid vertex line: %s", line)
}
x, errx := strconv.ParseFloat(parts[0], 64)
y, erry := strconv.ParseFloat(parts[1], 64)
if errx != nil || erry != nil {
return nil, fmt.Errorf("invalid vertex line: %s", line)
}
graph.AddNode(Point{x, y})
} else if section == "edges" && line != "" {
parts := strings.Split(line, " ")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid edge line: %s", line)
}
src, errsrc := strconv.Atoi(parts[0])
dst, errdst := strconv.Atoi(parts[1])
if errsrc != nil || errdst != nil {
return nil, fmt.Errorf("invalid edge line: %s", line)
}
graph.AddEdge(graph.Nodes[src], graph.Nodes[dst])
}
}
return &graph, nil
}
func (graph *Graph) Write(fname string) error {
file, err := os.Create(fname)
if err != nil {
return err
}
defer file.Close()
// vertices
for _, node := range graph.Nodes {
if _, err := file.Write([]byte(fmt.Sprintf("%f %f\n", node.Point.X, node.Point.Y))); err != nil {
return err
}
}
file.Write([]byte("\n"))
// edges
for _, edge := range graph.Edges {
if _, err := file.Write([]byte(fmt.Sprintf("%d %d\n", edge.Src.ID, edge.Dst.ID))); err != nil {
return err
}
}
return nil
}
func (graph *Graph) SplitEdge(edge *Edge, length float64) *Edge {
point := edge.Segment().PointAtFactor(length, false)
newNode := graph.AddNode(point)
origDst := edge.Dst
oppEdge := edge.GetOpposite()
edge.Dst = newNode
origDst.RemoveEdge(edge)
newNode.In = append(newNode.In, edge)
remainderEdge := graph.AddEdge(newNode, origDst)
if oppEdge != nil {
oppEdge.Src = newNode
origDst.RemoveEdge(oppEdge)
newNode.Out = append(newNode.Out, oppEdge)
graph.AddEdge(origDst, newNode)
}
return remainderEdge
}
func GraphFromEdges(edges []*Edge) *Graph {
g := &Graph{}
nodemap := make(map[int]*Node)
for _, edge := range edges {
if nodemap[edge.Src.ID] == nil {
nodemap[edge.Src.ID] = g.AddNode(edge.Src.Point)
}
if nodemap[edge.Dst.ID] == nil {
nodemap[edge.Dst.ID] = g.AddNode(edge.Dst.Point)
}
g.AddEdge(nodemap[edge.Src.ID], nodemap[edge.Dst.ID])
}
return g
}
func VisualizeGraphs(scale float64, fname string, graphs []*Graph, traces []*Trace) error {
if len(graphs) == 0 {
return fmt.Errorf("at least one graph must be provided")
}
var graphBoundables []Boundable
for _, graph := range graphs[1:] {
graphBoundables = append(graphBoundables, graph)
}
var options SVGOptions
if scale > 0 {
options.Scale = scale
}
var boundables [][]Boundable
boundables = append(boundables, []Boundable{graphs[0]})
if len(graphBoundables) > 0 {
boundables = append(boundables, graphBoundables)
}
if len(traces) > 0 {
boundables = append(boundables, []Boundable{Traces(traces)})
}
return CreateSVG(fname, boundables, options)
}