Repository navigation
Expand file tree
/
Copy pathnode.go
More file actions
264 lines (229 loc) · 7.51 KB
/
Copy pathnode.go
File metadata and controls
264 lines (229 loc) · 7.51 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
package layout
import (
"math"
"github.com/loov/layout/internal/draw"
)
// Node is a vertex in a graph. Layout leaves it unchanged; the computed
// position and size are in Layout.Nodes.
type Node struct {
ID string
// Label is the text drawn in the node; when empty, the ID is drawn
// instead, unless NoLabel is set
Label string
NoLabel bool
Tooltip string
FontName string
FontSize Length
FontColor Color
LineWidth Length
LineColor Color
LineStyle LineStyle
// Peripheries is the number of outlines; 0 and 1 draw one
Peripheries int
Shape Shape
FillColor Color
// MinSize is the minimum width and height; the label can grow the
// node unless FixedSize is set
MinSize Vector
FixedSize bool
// Image is a URL drawn inside the node
Image string
// Invisible keeps the node in the layout but leaves it out of the
// drawing, like Graphviz style=invis.
Invisible bool
// Pos pins the center of the node. When every node of the graph has
// a Pos, layout keeps them and only routes the edges that have no
// Pos of their own, like dot -n; otherwise Pos is ignored.
Pos *Vector
}
// NewNode creates a node with the given id and default styling.
func NewNode(id string) *Node {
node := &Node{}
node.ID = id
node.LineWidth = Point
return node
}
// String returns the node id, or its label when there is no id.
func (node *Node) String() string {
if node == nil {
return "?"
}
if node.ID != "" {
return node.ID
}
return node.Label
}
// DefaultLabel returns the label, falling back to the id unless NoLabel
// is set.
func (node *Node) DefaultLabel() string {
if node.Shape == PointShape {
return ""
}
if node.Label != "" || node.NoLabel {
return node.Label
}
return node.ID
}
// textRadius returns the half size of multi-line text, measuring each
// line with graph.MeasureText or the built-in approximation.
func (graph *Graph) textRadius(text string, fontName string, fontSize Length) Vector {
w, h := draw.TextSize(text, float64(graph.LineHeight), float64(fontSize), graph.lineWidth(fontName, fontSize))
return Vector{Length(w) / 2, Length(h) / 2}
}
// lineWidth returns the width of one line of text measured with
// graph.MeasureText, or nil to use the built-in approximation.
func (graph *Graph) lineWidth(fontName string, fontSize Length) func(line string) float64 {
if graph.MeasureText == nil {
return nil
}
return func(line string) float64 { return float64(graph.MeasureText(line, fontName, fontSize)) }
}
// TopLeft returns the top left corner of the node bounds.
func (node *lnode) TopLeft() Vector { return Vector{node.Left(), node.Top()} }
// BottomRight returns the bottom right corner of the node bounds.
func (node *lnode) BottomRight() Vector { return Vector{node.Right(), node.Bottom()} }
// rect returns the rect of the node's box
func (node *lnode) rect() rect { return rect{node.TopLeft(), node.BottomRight()} }
// TopCenter returns the middle of the top edge of the node bounds.
func (node *lnode) TopCenter() Vector { return Vector{node.Center.X, node.Top()} }
// BottomCenter returns the middle of the bottom edge of the node bounds.
func (node *lnode) BottomCenter() Vector { return Vector{node.Center.X, node.Bottom()} }
// Left returns the x coordinate of the left side of the node bounds.
func (node *lnode) Left() Length { return node.Center.X - node.Radius.X }
// Top returns the y coordinate of the top side of the node bounds.
func (node *lnode) Top() Length { return node.Center.Y - node.Radius.Y }
// Right returns the x coordinate of the right side of the node bounds.
func (node *lnode) Right() Length { return node.Center.X + node.Radius.X }
// Bottom returns the y coordinate of the bottom side of the node bounds.
func (node *lnode) Bottom() Length { return node.Center.Y + node.Radius.Y }
// CompassPoint returns the point on the node outline at the compass
// direction, or the center for Center and CompassAuto.
func (node *lnode) CompassPoint(c Compass) Vector {
var dir Vector
switch c {
case North:
dir = Vector{0, -1}
case NorthEast:
dir = Vector{1, -1}
case East:
dir = Vector{1, 0}
case SouthEast:
dir = Vector{1, 1}
case South:
dir = Vector{0, 1}
case SouthWest:
dir = Vector{-1, 1}
case West:
dir = Vector{-1, 0}
case NorthWest:
dir = Vector{-1, -1}
default:
return node.Center
}
return node.Boundary(Vector{node.Center.X + dir.X*node.Radius.X, node.Center.Y + dir.Y*node.Radius.Y})
}
// outlineAlong returns where the line from start, inside the node,
// towards p leaves the node's outline, or the outline towards p from the
// center when start is outside.
func (node *lnode) outlineAlong(start, p Vector) Vector {
inside := func(v Vector) bool {
dx, dy := float64(v.X-node.Center.X), float64(v.Y-node.Center.Y)
rx, ry := float64(node.Radius.X), float64(node.Radius.Y)
switch node.Shape {
case Auto, Box, Square, Record:
return math.Abs(dx) <= rx && math.Abs(dy) <= ry
case Diamond, Octagon:
in := true
sides(node.Shape.polygon(), func(nx, ny, h float64) { in = in && nx*dx/rx+ny*dy/ry <= h })
return in
}
return (dx/rx)*(dx/rx)+(dy/ry)*(dy/ry) <= 1
}
if !inside(start) || inside(p) {
return node.Boundary(p)
}
in, out := start, p
for range 32 {
mid := midpoint(in, out)
if inside(mid) {
in = mid
} else {
out = mid
}
}
return in
}
// Boundary returns the point on the node outline where the ray from the
// center towards p exits the node.
func (node *lnode) Boundary(p Vector) Vector {
dx, dy := float64(p.X-node.Center.X), float64(p.Y-node.Center.Y)
if dx == 0 && dy == 0 {
return node.Center
}
rx, ry := float64(node.Radius.X), float64(node.Radius.Y)
var t float64
switch node.Shape {
case Auto, Box, Square, Record:
t = math.Min(rx/math.Abs(dx), ry/math.Abs(dy)) // ray-rect; Inf for zero component is fine
case Diamond, Octagon:
// the side the ray reaches first
most := 0.0
sides(node.Shape.polygon(), func(nx, ny, h float64) { most = max(most, (nx*dx/rx+ny*dy/ry)/h) })
t = 1 / most
default: // ellipse and circle
t = 1 / math.Hypot(dx/rx, dy/ry)
}
return Vector{node.Center.X + Length(dx*t), node.Center.Y + Length(dy*t)}
}
// halfHeightAt returns how far the outline of node reaches above and below
// its center at x
func (node *lnode) halfHeightAt(x Length) Length {
h := float64(node.Radius.Y)
switch node.Shape {
case Auto, Box, Square, Record:
case Diamond, Octagon:
// the lowest of the sides above at x
u := float64(x-node.Center.X) / float64(node.Radius.X)
top := 1.0
sides(node.Shape.polygon(), func(nx, ny, h float64) {
if ny < 0 {
top = min(top, (h-nx*u)/-ny)
}
})
h *= max(0, top)
default: // ellipse and circle
if rx := float64(node.Radius.X); rx > 0 {
dx := float64(x-node.Center.X) / rx
h *= math.Sqrt(max(0, 1-dx*dx))
}
}
return Length(h)
}
// sideAt returns the point of the outline of node at height y, within it,
// on the side toward x
func (node *lnode) sideAt(y, x Length) Vector {
y = max(node.Center.Y-node.Radius.Y, min(node.Center.Y+node.Radius.Y, y))
w := float64(node.Radius.X)
switch node.Shape {
case Auto, Box, Square, Record:
case Diamond, Octagon:
// the nearest of the sides to the right at y
v := float64(y-node.Center.Y) / float64(node.Radius.Y)
right := 1.0
sides(node.Shape.polygon(), func(nx, ny, h float64) {
if nx > 0 {
right = min(right, (h-ny*v)/nx)
}
})
w *= max(0, right)
default: // ellipse and circle
if ry := float64(node.Radius.Y); ry > 0 {
dy := float64(y-node.Center.Y) / ry
w *= math.Sqrt(max(0, 1-dy*dy))
}
}
if x < node.Center.X {
w = -w
}
return Vector{node.Center.X + Length(w), y}
}