-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology.go
230 lines (200 loc) · 5.08 KB
/
topology.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
package raumata
import (
"encoding/json"
"errors"
"fmt"
"github.com/REANNZ/raumata/option"
"github.com/REANNZ/raumata/vec"
)
type NodeId string
type LinkId string
// Represents a node on the map
type Node struct {
Id NodeId `json:"id"`
Pos *[2]int16 `json:"pos,omitempty"`
Label string `json:"label,omitempty"`
LabelAt string `json:"label_at,omitempty"`
Class string `json:"class,omitempty"`
Style *NodeStyle `json:"style,omitempty"`
Extents *NodeExtents `json:"extents,omitempty"`
}
type NodeExtents struct {
Width int16 `json:"width"`
Height int16 `json:"height"`
}
// Link represents a link between two nodes.
//
// While the ends are labeled "to" and "from", links
// are expected to be bi-directional, the naming is
// simply for convenience.
type Link struct {
Id LinkId `json:"id"`
From NodeId `json:"from"`
To NodeId `json:"to"`
Via [][2]int16 `json:"via,omitempty"`
SplitAt *float32 `json:"split_at,omitempty"`
Class string `json:"class,omitempty"`
State string `json:"state,omitempty"`
Style *LinkStyle `json:"style,omitempty"`
Route vec.Polyline `json:"route,omitempty"`
FromData *LinkData `json:"from_data,omitempty"`
ToData *LinkData `json:"to_data,omitempty"`
}
// Data associated with a link
type LinkData struct {
// The "value" of the link, typically link usage as a %
Value option.Float32 `json:"value"`
// The label for the link, typically the amount of traffic
Label string `json:"label"`
}
// A full map topology
type Topology struct {
Nodes map[NodeId]*Node `json:"nodes"`
Links map[LinkId]*Link `json:"links"`
}
func (t *Topology) GetNode(id NodeId) *Node {
return t.Nodes[id]
}
func (t *Topology) GetLink(id LinkId) *Link {
return t.Links[id]
}
func (id NodeId) String() string {
return string(id)
}
func (id LinkId) String() string {
return string(id)
}
// UnmarshalJSON supports a couple different ways of representing
// a Topology
//
// It is always an object with fields: "nodes" and "links",
// However each field can either be an array of nodes/links, or an
// object with { "id": <node/link> } fields.
//
// Node ids are automatically set if the object format used, otherwise
// they must be both present and unique.
//
// Link ids, if not provided, are determined automatically from the
// "from" and "to" fields of the link.
func (t *Topology) UnmarshalJSON(data []byte) error {
var topLevel struct {
Nodes *json.RawMessage
Links *json.RawMessage
}
err := json.Unmarshal(data, &topLevel)
if err != nil {
return err
}
nodeMap := make(map[NodeId]*Node)
if topLevel.Nodes != nil && len(*topLevel.Nodes) > 0 {
rawNodes := *topLevel.Nodes
if rawNodes[0] == '[' {
var array []*Node
err = json.Unmarshal(rawNodes, &array)
if err != nil {
return err
}
for _, n := range array {
if n.Id == "" {
return errors.New("Node must have an id")
}
_, ok := nodeMap[n.Id]
if ok {
return fmt.Errorf("Duplicate node id '%s'", n.Id)
}
nodeMap[n.Id] = n
}
} else if rawNodes[0] == '{' {
err = json.Unmarshal(rawNodes, &nodeMap)
if err != nil {
return err
}
for id, n := range nodeMap {
n.Id = id
}
} else {
return errors.New("\"nodes\" must be an array or object")
}
if t.Nodes == nil {
t.Nodes = nodeMap
} else {
for id, node := range nodeMap {
t.Nodes[id] = node
}
}
}
linkMap := make(map[LinkId]*Link)
if topLevel.Links != nil && len(*topLevel.Links) > 0 {
rawLinks := *topLevel.Links
if rawLinks[0] == '[' {
var array []*Link
err = json.Unmarshal(rawLinks, &array)
if err != nil {
return err
}
for _, l := range array {
id := l.Id
if id == "" {
// Automatically determine an id
id = LinkId(fmt.Sprintf("%s-%s", l.From, l.To))
_, ok := linkMap[id]
n := 2
for ok {
id = LinkId(fmt.Sprintf("%s-%s-%d", l.From, l.To, n))
n += 1
_, ok = linkMap[id]
}
l.Id = id
}
_, ok := linkMap[id]
if ok {
return fmt.Errorf("Duplicate link id '%s'", id)
}
linkMap[id] = l
}
} else if rawLinks[0] == '{' {
err = json.Unmarshal(rawLinks, &linkMap)
if err != nil {
return err
}
for id, link := range linkMap {
link.Id = id
}
} else {
return errors.New("\"links\" must be an array or object")
}
if t.Links == nil {
t.Links = linkMap
} else {
for id, link := range linkMap {
t.Links[id] = link
}
}
}
return nil
}
func (n *Node) IsMultiCell() bool {
if n.Extents == nil {
return false
}
return n.Extents.Height > 1 || n.Extents.Width > 1
}
func (n *Node) GetExtents() (min, max vec.Vec2) {
p := vec.Vec2{
X: float32(n.Pos[0]),
Y: float32(n.Pos[1]),
}
if n.IsMultiCell() {
offset := vec.Vec2{ X: 0.5, Y: 0.5 }
minPos := p.Sub(offset)
minPos.X -= float32(n.Extents.Width/2)
minPos.Y -= float32(n.Extents.Height/2)
maxPos := minPos
maxPos.X += float32(n.Extents.Width)
maxPos.Y += float32(n.Extents.Height)
return minPos, maxPos
} else {
offset := vec.Vec2{ X: 0.5, Y: 0.5 }
return p.Sub(offset), p.Add(offset)
}
}