-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlayout.go
189 lines (156 loc) · 3.98 KB
/
layout.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
package layout
import (
"github.com/loov/layout/internal/hier"
)
const epsilon = 1e-6
func (graph *Graph) AssignMissingValues() {
if graph.FontSize <= 0 {
graph.FontSize = graph.LineHeight * 14 / 16
}
if graph.NodePadding <= 0 {
graph.NodePadding = graph.LineHeight
}
if graph.RowPadding <= 0 {
graph.RowPadding = graph.LineHeight
}
if graph.EdgePadding <= 0 {
graph.EdgePadding = 6 * Point
}
for _, node := range graph.Nodes {
if node.Shape == "" {
node.Shape = graph.Shape
}
if node.Weight < epsilon {
node.Weight = epsilon
}
if node.FontSize <= 0 {
node.FontSize = graph.FontSize
}
if node.Radius.X <= 0 || node.Radius.Y <= 0 {
node.Radius.X = graph.LineHeight
node.Radius.Y = graph.LineHeight
labelRadius := node.approxLabelRadius(graph.LineHeight)
labelRadius.X += node.FontSize * 0.5
labelRadius.Y += node.FontSize * 0.25
if node.Radius.X < labelRadius.X {
node.Radius.X = labelRadius.X
}
if node.Radius.Y < labelRadius.Y {
node.Radius.Y = labelRadius.Y
}
}
}
for _, edge := range graph.Edges {
if edge.Weight < epsilon {
edge.Weight = epsilon
}
}
}
func Hierarchical(graphdef *Graph) {
graphdef.AssignMissingValues()
nodes := map[*Node]hier.ID{}
reverse := map[hier.ID]*Node{}
// construct hierarchical graph
graph := &hier.Graph{}
for _, nodedef := range graphdef.Nodes {
node := graph.AddNode()
nodes[nodedef] = node.ID
reverse[node.ID] = nodedef
node.Label = nodedef.ID
}
for _, edge := range graphdef.Edges {
from, to := nodes[edge.From], nodes[edge.To]
graph.AddEdge(graph.Nodes[from], graph.Nodes[to])
}
// remove cycles
decycledGraph := hier.DefaultDecycle(graph)
// assign nodes to ranks
rankedGraph := hier.DefaultRank(decycledGraph)
// create virtual nodes
filledGraph := hier.DefaultAddVirtuals(rankedGraph)
// order nodes in ranks
orderedGraph := hier.DefaultOrderRanks(filledGraph)
// assign node sizes
for id, node := range orderedGraph.Nodes {
if node.Virtual {
node.Radius.X = float32(graphdef.EdgePadding)
node.Radius.Y = float32(graphdef.EdgePadding)
continue
}
nodedef, ok := reverse[hier.ID(id)]
if !ok {
// TODO: handle missing node
continue
}
node.Radius.X = float32(nodedef.Radius.X + graphdef.NodePadding)
node.Radius.Y = float32(nodedef.Radius.Y + graphdef.RowPadding)
}
// position nodes
positionedGraph := hier.DefaultPosition(orderedGraph)
// assign final positions
for nodedef, id := range nodes {
node := positionedGraph.Nodes[id]
nodedef.Center.X = Length(node.Center.X)
nodedef.Center.Y = Length(node.Center.Y)
}
// calculate edges
edgePaths := map[[2]hier.ID][]Vector{}
for _, source := range positionedGraph.Nodes {
if source.Virtual {
continue
}
sourcedef := reverse[source.ID]
for _, out := range source.Out {
path := []Vector{}
path = append(path, sourcedef.BottomCenter())
target := out
for target != nil && target.Virtual {
if len(target.Out) < 1 { // should never happen
target = nil
break
}
path = append(path, Vector{
Length(target.Center.X),
Length(target.Center.Y),
})
target = target.Out[0]
}
if target == nil {
continue
}
targetdef := reverse[target.ID]
path = append(path, targetdef.TopCenter())
edgePaths[[2]hier.ID{source.ID, target.ID}] = path
}
}
for _, edge := range graphdef.Edges {
sourceid := nodes[edge.From]
targetid := nodes[edge.To]
if sourceid == targetid {
// TODO: improve loops
edge.Path = []Vector{
edge.From.BottomCenter(),
edge.From.TopCenter(),
}
continue
}
path, ok := edgePaths[[2]hier.ID{sourceid, targetid}]
if ok {
edge.Path = path
continue
}
// some paths may have been reversed
revpath, ok := edgePaths[[2]hier.ID{targetid, sourceid}]
if ok {
edge.Path = reversePath(revpath)
continue
}
}
}
func reversePath(path []Vector) []Vector {
rs := make([]Vector, 0, len(path))
for i := len(path) - 1; i >= 0; i-- {
rs = append(rs, path[i])
}
return rs
}