-
Notifications
You must be signed in to change notification settings - Fork 414
/
label.go
321 lines (278 loc) · 10.5 KB
/
label.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
package label
import (
"math"
"oss.terrastruct.com/d2/lib/geo"
)
// These are % locations where labels will be placed along the connection
const LEFT_LABEL_POSITION = 1.0 / 4.0
const CENTER_LABEL_POSITION = 2.0 / 4.0
const RIGHT_LABEL_POSITION = 3.0 / 4.0
// This is the space between a node border and its outside label
const PADDING = 5
type Position string
const (
OutsideTopLeft Position = "OUTSIDE_TOP_LEFT"
OutsideTopCenter Position = "OUTSIDE_TOP_CENTER"
OutsideTopRight Position = "OUTSIDE_TOP_RIGHT"
OutsideLeftTop Position = "OUTSIDE_LEFT_TOP"
OutsideLeftMiddle Position = "OUTSIDE_LEFT_MIDDLE"
OutsideLeftBottom Position = "OUTSIDE_LEFT_BOTTOM"
OutsideRightTop Position = "OUTSIDE_RIGHT_TOP"
OutsideRightMiddle Position = "OUTSIDE_RIGHT_MIDDLE"
OutsideRightBottom Position = "OUTSIDE_RIGHT_BOTTOM"
OutsideBottomLeft Position = "OUTSIDE_BOTTOM_LEFT"
OutsideBottomCenter Position = "OUTSIDE_BOTTOM_CENTER"
OutsideBottomRight Position = "OUTSIDE_BOTTOM_RIGHT"
InsideTopLeft Position = "INSIDE_TOP_LEFT"
InsideTopCenter Position = "INSIDE_TOP_CENTER"
InsideTopRight Position = "INSIDE_TOP_RIGHT"
InsideMiddleLeft Position = "INSIDE_MIDDLE_LEFT"
InsideMiddleCenter Position = "INSIDE_MIDDLE_CENTER"
InsideMiddleRight Position = "INSIDE_MIDDLE_RIGHT"
InsideBottomLeft Position = "INSIDE_BOTTOM_LEFT"
InsideBottomCenter Position = "INSIDE_BOTTOM_CENTER"
InsideBottomRight Position = "INSIDE_BOTTOM_RIGHT"
UnlockedTop Position = "UNLOCKED_TOP"
UnlockedMiddle Position = "UNLOCKED_MIDDLE"
UnlockedBottom Position = "UNLOCKED_BOTTOM"
)
func (position Position) IsOutside() bool {
switch position {
case OutsideTopLeft, OutsideTopCenter, OutsideTopRight,
OutsideBottomLeft, OutsideBottomCenter, OutsideBottomRight,
OutsideLeftTop, OutsideLeftMiddle, OutsideLeftBottom,
OutsideRightTop, OutsideRightMiddle, OutsideRightBottom:
return true
default:
return false
}
}
func (position Position) IsUnlocked() bool {
switch position {
case UnlockedTop, UnlockedMiddle, UnlockedBottom:
return true
default:
return false
}
}
func (position Position) IsOnEdge() bool {
switch position {
case InsideMiddleLeft, InsideMiddleCenter, InsideMiddleRight, UnlockedMiddle:
return true
default:
return false
}
}
func (position Position) Mirrored() Position {
switch position {
case OutsideTopLeft:
return OutsideBottomRight
case OutsideTopCenter:
return OutsideBottomCenter
case OutsideTopRight:
return OutsideBottomLeft
case OutsideLeftTop:
return OutsideRightBottom
case OutsideLeftMiddle:
return OutsideRightMiddle
case OutsideLeftBottom:
return OutsideRightTop
case OutsideRightTop:
return OutsideLeftBottom
case OutsideRightMiddle:
return OutsideLeftMiddle
case OutsideRightBottom:
return OutsideLeftTop
case OutsideBottomLeft:
return OutsideTopRight
case OutsideBottomCenter:
return OutsideTopCenter
case OutsideBottomRight:
return OutsideTopLeft
case InsideTopLeft:
return InsideBottomRight
case InsideTopCenter:
return InsideBottomCenter
case InsideTopRight:
return InsideBottomLeft
case InsideMiddleLeft:
return InsideMiddleRight
case InsideMiddleCenter:
return InsideMiddleCenter
case InsideMiddleRight:
return InsideMiddleLeft
case InsideBottomLeft:
return InsideTopRight
case InsideBottomCenter:
return InsideTopCenter
case InsideBottomRight:
return InsideTopLeft
case UnlockedTop:
return UnlockedBottom
case UnlockedBottom:
return UnlockedTop
case UnlockedMiddle:
return UnlockedMiddle
default:
return ""
}
}
func (labelPosition Position) GetPointOnBox(box *geo.Box, padding, width, height float64) *geo.Point {
p := box.TopLeft.Copy()
boxCenter := box.Center()
switch labelPosition {
case OutsideTopLeft:
p.X -= padding
p.Y -= padding + height
case OutsideTopCenter:
p.X = boxCenter.X - width/2
p.Y -= padding + height
case OutsideTopRight:
p.X += box.Width - width - padding
p.Y -= padding + height
case OutsideLeftTop:
p.X -= padding + width
p.Y += padding
case OutsideLeftMiddle:
p.X -= padding + width
p.Y = boxCenter.Y - height/2
case OutsideLeftBottom:
p.X -= padding + width
p.Y += box.Height - height - padding
case OutsideRightTop:
p.X += box.Width + padding
p.Y += padding
case OutsideRightMiddle:
p.X += box.Width + padding
p.Y = boxCenter.Y - height/2
case OutsideRightBottom:
p.X += box.Width + padding
p.Y += box.Height - height - padding
case OutsideBottomLeft:
p.X += padding
p.Y += box.Height + padding
case OutsideBottomCenter:
p.X = boxCenter.X - width/2
p.Y += box.Height + padding
case OutsideBottomRight:
p.X += box.Width - width - padding
p.Y += box.Height + padding
case InsideTopLeft:
p.X += padding
p.Y += padding
case InsideTopCenter:
p.X = boxCenter.X - width/2
p.Y += padding
case InsideTopRight:
p.X += box.Width - width - padding
p.Y += padding
case InsideMiddleLeft:
p.X += padding
p.Y = boxCenter.Y - height/2
case InsideMiddleCenter:
p.X = boxCenter.X - width/2
p.Y = boxCenter.Y - height/2
case InsideMiddleRight:
p.X += box.Width - width - padding
p.Y = boxCenter.Y - height/2
case InsideBottomLeft:
p.X += padding
p.Y += box.Height - height - padding
case InsideBottomCenter:
p.X = boxCenter.X - width/2
p.Y += box.Height - height - padding
case InsideBottomRight:
p.X += box.Width - width - padding
p.Y += box.Height - height - padding
}
return p
}
// return the top left point of a width x height label at the given label position on the route
func (labelPosition Position) GetPointOnRoute(route geo.Route, strokeWidth, labelPercentage, width, height float64) *geo.Point {
totalLength := route.Length()
leftPosition := LEFT_LABEL_POSITION * totalLength
centerPosition := CENTER_LABEL_POSITION * totalLength
rightPosition := RIGHT_LABEL_POSITION * totalLength
unlockedPosition := labelPercentage * totalLength
// outside labels have to be offset in the direction of the edge's normal Vector
// Note: we flip the normal for Top labels but keep it as is for Bottom labels since positive Y is below in SVG
getOffsetLabelPosition := func(basePoint, normStart, normEnd *geo.Point, flip bool) *geo.Point {
// get the normal as a unit Vector so we can multiply to project in its direction
normalX, normalY := geo.GetUnitNormalVector(
normStart.X,
normStart.Y,
normEnd.X,
normEnd.Y,
)
if flip {
normalX *= -1
normalY *= -1
}
// Horizontal Edge with Outside Label | Vertical Edge with Outside Label
// ┌────────────────────┐ ┬ | ┌─┬─┐
// │ │ │ | │ │ │ ┌───────────┬───────────┐
// │ │ │ | │ e │ │ │ │
// ├────label─center────┤ ┬ ┼label height | │ d │ │ label │
// │ │ │ │ | │ g │ │ center │
// │ │ │ │ | │ e │ │ │ │
// └────────────────────┘ │ ┴ ┬ | │ │ │ └───────────┴───────────┘
// │ │ | └─┴─┘ offset
// offset│ │label padding | ├──────────────────┤
// │ │ |
// ┌──────────────────────┐ │ ┬ ┴ | ├───────────┼───────────┤
// │ │ │ │ | ├────┤ label width
// ├─────edge─center──────┤ ┴ ┼stroke width | label padding
// │ │ │ | ├─┼─┤
// └──────────────────────┘ ┴ | stroke width
//
// TODO: get actual edge stroke width on edge
offsetX := strokeWidth/2 + float64(PADDING) + width/2
offsetY := strokeWidth/2 + float64(PADDING) + height/2
return geo.NewPoint(chopPrecision(basePoint.X+normalX*offsetX), chopPrecision(basePoint.Y+normalY*offsetY))
}
var labelCenter *geo.Point
switch labelPosition {
case InsideMiddleLeft:
labelCenter, _ = route.GetPointAtDistance(leftPosition)
case InsideMiddleCenter:
labelCenter, _ = route.GetPointAtDistance(centerPosition)
case InsideMiddleRight:
labelCenter, _ = route.GetPointAtDistance(rightPosition)
case OutsideTopLeft:
basePoint, index := route.GetPointAtDistance(leftPosition)
labelCenter = getOffsetLabelPosition(basePoint, route[index], route[index+1], true)
case OutsideTopCenter:
basePoint, index := route.GetPointAtDistance(centerPosition)
labelCenter = getOffsetLabelPosition(basePoint, route[index], route[index+1], true)
case OutsideTopRight:
basePoint, index := route.GetPointAtDistance(rightPosition)
labelCenter = getOffsetLabelPosition(basePoint, route[index], route[index+1], true)
case OutsideBottomLeft:
basePoint, index := route.GetPointAtDistance(leftPosition)
labelCenter = getOffsetLabelPosition(basePoint, route[index], route[index+1], false)
case OutsideBottomCenter:
basePoint, index := route.GetPointAtDistance(centerPosition)
labelCenter = getOffsetLabelPosition(basePoint, route[index], route[index+1], false)
case OutsideBottomRight:
basePoint, index := route.GetPointAtDistance(rightPosition)
labelCenter = getOffsetLabelPosition(basePoint, route[index], route[index+1], false)
case UnlockedTop:
basePoint, index := route.GetPointAtDistance(unlockedPosition)
labelCenter = getOffsetLabelPosition(basePoint, route[index], route[index+1], true)
case UnlockedMiddle:
labelCenter, _ = route.GetPointAtDistance(unlockedPosition)
case UnlockedBottom:
basePoint, index := route.GetPointAtDistance(unlockedPosition)
labelCenter = getOffsetLabelPosition(basePoint, route[index], route[index+1], false)
default:
return nil
}
// convert from center to top left
labelCenter.X -= chopPrecision(width / 2)
labelCenter.Y -= chopPrecision(height / 2)
return labelCenter
}
// TODO probably use math.Big
func chopPrecision(f float64) float64 {
return math.Round(f*10000) / 10000
}