Repository navigation
Expand file tree
/
Copy pathwork.go
More file actions
324 lines (285 loc) · 8.06 KB
/
Copy pathwork.go
File metadata and controls
324 lines (285 loc) · 8.06 KB
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
package layout
import (
"slices"
)
// lgraph is the working copy of a Graph that layout computes on, so that
// the caller's graph is never modified. The embedded Graph holds a copy
// of the settings; its node, edge and cluster slices are nil, and the
// fields below hold copies of them instead.
type lgraph struct {
Graph
def *Graph
Nodes []*lnode
Edges []*ledge
Clusters []*lcluster
SameRank [][]*lnode
MinRank []*lnode
MaxRank []*lnode
// ForText prepares the layout for format/text, see Options.ForText
ForText bool
// Pinned means every node has a Pos; layout keeps them
Pinned bool
// merged holds the group ids of the edge ends that merge, at the
// start and the end, see mergeEdges; the components of a graph
// share it
merged map[*ledge][2]int
// ortho is routing's buffers, see orthoScratch
ortho orthoScratch
}
// lnode is the working copy of a Node.
type lnode struct {
Node
def *Node
Center Vector
// Radius is half the size
Radius Vector
// pad is the padding layout added to Radius, for extra peripheries
// and packed edge ends
pad Vector
}
// reserveHalfHeight grows the node to a half height of at least h, as
// padding
func (node *lnode) reserveHalfHeight(h Length) {
if grow := h - node.Radius.Y; grow > 0 {
node.Radius.Y += grow
node.pad.Y += grow
}
}
// ledge is the working copy of an Edge, between working nodes.
type ledge struct {
Edge
def *Edge
From, To *lnode
Path Path
LabelPos Vector // center of the label, when Label is set
LabelRadius Vector // half size of the label
// the labels at the ends, when HeadLabel and TailLabel are set
head, tail endLabel
// flat is set for an edge whose ends are on one rank, loops too, see
// hierComponent.rank
flat bool
}
// labelRect returns the rect of the edge's label
func (edge *ledge) labelRect() rect {
return centeredRect(edge.LabelPos, edge.LabelRadius)
}
// loop reports whether the edge leaves and reaches the same node
func (edge *ledge) loop() bool { return edge.From == edge.To }
// ported reports whether either end of the edge has a compass port
func (edge *ledge) ported() bool { return edge.FromPort != CompassAuto || edge.ToPort != CompassAuto }
// arcShaped reports whether the edge runs along a rank as an arc of
// three segments, see the flat edges in layout.go
func (edge *ledge) arcShaped() bool { return !edge.loop() && edge.flat && len(edge.Path) == 4 }
// setEnd sets the first point of the edge's path for the start, and the
// last for the end
func (edge *ledge) setEnd(start bool, p Vector) {
if start {
edge.Path[0] = p
} else {
edge.Path[len(edge.Path)-1] = p
}
}
// endLabel is a label at an end of an edge: its center and half size
type endLabel struct{ pos, radius Vector }
// mergeGroup returns the group that the start or the end of edge merges
// in, 0 when it doesn't
func (graph *lgraph) mergeGroup(edge *ledge, start bool) int {
if start {
return graph.merged[edge][0]
}
return graph.merged[edge][1]
}
// lcluster is the working copy of a Cluster, of working nodes.
type lcluster struct {
Cluster
def *Cluster
Nodes []*lnode
Parent *lcluster
TopLeft, BottomRight Vector
}
// rect returns the cluster's box
func (cluster *lcluster) rect() rect { return rect{cluster.TopLeft, cluster.BottomRight} }
// newWorkGraph copies a validated graph for layout. Nodes, edges and
// clusters listed more than once get one copy each.
func newWorkGraph(def *Graph) *lgraph {
g := &lgraph{Graph: *def, def: def}
g.Graph.NodeByID = nil
g.Graph.Nodes = nil
g.Graph.Edges = nil
g.Graph.Clusters = nil
g.Graph.SameRank = nil
g.Graph.MinRank = nil
g.Graph.MaxRank = nil
nodes := make(map[*Node]*lnode, len(def.Nodes))
node := func(n *Node) *lnode {
w := nodes[n]
if w == nil {
w = &lnode{Node: *n, def: n, Radius: n.MinSize.Scale(0.5)}
if n.Pos != nil {
w.Center = *n.Pos
}
nodes[n] = w
}
return w
}
group := func(defs []*Node) []*lnode {
var out []*lnode
for _, n := range defs {
out = append(out, node(n))
}
return out
}
g.Nodes = group(def.Nodes)
edges := make(map[*Edge]*ledge, len(def.Edges))
for _, e := range def.Edges {
w := edges[e]
if w == nil {
w = &ledge{Edge: *e, def: e, From: node(e.From), To: node(e.To), Path: slices.Clone(e.Pos)}
if e.LabelPos != nil {
w.LabelPos = *e.LabelPos
}
edges[e] = w
}
g.Edges = append(g.Edges, w)
}
clusters := make(map[*Cluster]*lcluster, len(def.Clusters))
for _, c := range def.Clusters {
if clusters[c] == nil {
clusters[c] = &lcluster{Cluster: *c, def: c}
}
g.Clusters = append(g.Clusters, clusters[c])
}
for c, w := range clusters {
w.Nodes = group(c.Nodes)
if c.Parent != nil {
w.Parent = clusters[c.Parent]
}
}
for _, same := range def.SameRank {
g.SameRank = append(g.SameRank, group(same))
}
g.MinRank = group(def.MinRank)
g.MaxRank = group(def.MaxRank)
g.Pinned = pinned(def)
return g
}
// pinned reports whether every node of the graph has a Pos.
func pinned(graph *Graph) bool {
for _, node := range graph.Nodes {
if node.Pos == nil {
return false
}
}
return len(graph.Nodes) > 0
}
// result returns the computed layout, index-aligned with the caller's
// graph.
func (graph *lgraph) result() *Layout {
settings := graph.Graph
def := graph.def
settings.NodeByID = def.NodeByID
settings.Nodes = def.Nodes
settings.Edges = def.Edges
settings.Clusters = def.Clusters
settings.SameRank = def.SameRank
settings.MinRank = def.MinRank
settings.MaxRank = def.MaxRank
l := &Layout{
Graph: &settings,
nodeIndex: indexOf(def.Nodes),
edgeIndex: indexOf(def.Edges),
clusterIndex: indexOf(def.Clusters),
}
for _, n := range graph.Nodes {
l.Nodes = append(l.Nodes, NodeBox{
Center: n.Center,
Size: n.Radius.Scale(2),
Shape: n.Shape,
FontSize: n.FontSize,
Label: n.DefaultLabel(),
})
}
for _, e := range graph.Edges {
path := EdgePath{Path: e.Path, FontSize: e.FontSize, Merged: MergeGroups{Start: graph.merged[e][0], End: graph.merged[e][1]}}
if e.Label != "" {
path.LabelCenter = e.LabelPos
path.LabelSize = e.LabelRadius.Scale(2)
}
if e.HeadLabel != "" {
path.HeadLabelCenter, path.HeadLabelSize = e.head.pos, e.head.radius.Scale(2)
}
if e.TailLabel != "" {
path.TailLabelCenter, path.TailLabelSize = e.tail.pos, e.tail.radius.Scale(2)
}
l.Edges = append(l.Edges, path)
}
for _, c := range graph.Clusters {
l.Clusters = append(l.Clusters, ClusterBox{TopLeft: c.TopLeft, BottomRight: c.BottomRight})
}
return l
}
// work returns the working copy of a computed layout, for measuring it.
func (l *Layout) work() *lgraph {
g := newWorkGraph(l.Graph)
for i, n := range g.Nodes {
box := l.Nodes[i]
n.Center = box.Center
n.Radius = box.Size.Scale(0.5)
n.Shape = box.Shape
n.FontSize = box.FontSize
}
for i, e := range g.Edges {
path := l.Edges[i]
e.Path = path.Path
e.LabelPos = path.LabelCenter
e.LabelRadius = path.LabelSize.Scale(0.5)
e.head = endLabel{path.HeadLabelCenter, path.HeadLabelSize.Scale(0.5)}
e.tail = endLabel{path.TailLabelCenter, path.TailLabelSize.Scale(0.5)}
e.FontSize = path.FontSize
if !path.Merged.IsZero() {
if g.merged == nil {
g.merged = map[*ledge][2]int{}
}
g.merged[e] = [2]int{path.Merged.Start, path.Merged.End}
}
}
for i, c := range g.Clusters {
c.TopLeft, c.BottomRight = l.Clusters[i].TopLeft, l.Clusters[i].BottomRight
}
return g
}
// Bounds returns the bounding box of all nodes and edge paths.
func (graph *lgraph) Bounds() (min, max Vector) {
b := emptyRect()
for _, node := range graph.Nodes {
b.add(node.rect())
}
for _, cluster := range graph.Clusters {
b.add(cluster.rect())
}
for _, edge := range graph.Edges {
for _, p := range edge.Path {
b.point(p)
}
if edge.Label != "" {
b.add(edge.labelRect())
}
for _, end := range []endLabel{edge.head, edge.tail} {
if !end.radius.IsZero() {
b.add(centeredRect(end.pos, end.radius))
}
}
}
min, max = b.Min, b.Max
if b.empty() { // nothing to bound
return Vector{}, Vector{}
}
return
}
func (cluster *lcluster) depth() int {
d := 0
for c := cluster; c != nil; c = c.Parent {
d++
}
return d
}