This commit is contained in:
Mayank Mohapatra 2025-02-21 19:24:57 +00:00
parent f6c3bdcae6
commit dd28b2cd1b
5 changed files with 207 additions and 673 deletions

View file

@ -1,198 +1,3 @@
// package d2cycle
// import (
// "context"
// "math"
// "oss.terrastruct.com/d2/d2graph"
// "oss.terrastruct.com/d2/lib/geo"
// "oss.terrastruct.com/d2/lib/label"
// "oss.terrastruct.com/util-go/go2"
// )
// const (
// MIN_RADIUS = 200
// PADDING = 20
// MIN_SEGMENT_LEN = 10
// ARC_STEPS = 30
// )
// func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) error {
// objects := g.Root.ChildrenArray
// if len(objects) == 0 {
// return nil
// }
// for _, obj := range g.Objects {
// positionLabelsIcons(obj)
// }
// radius := calculateRadius(objects)
// positionObjects(objects, radius)
// for _, edge := range g.Edges {
// createCircularArc(edge)
// }
// return nil
// }
// func calculateRadius(objects []*d2graph.Object) float64 {
// numObjects := float64(len(objects))
// maxSize := 0.0
// for _, obj := range objects {
// size := math.Max(obj.Box.Width, obj.Box.Height)
// maxSize = math.Max(maxSize, size)
// }
// minRadius := (maxSize/2.0 + PADDING) / math.Sin(math.Pi/numObjects)
// return math.Max(minRadius, MIN_RADIUS)
// }
// func positionObjects(objects []*d2graph.Object, radius float64) {
// numObjects := float64(len(objects))
// angleOffset := -math.Pi / 2
// for i, obj := range objects {
// angle := angleOffset + (2*math.Pi*float64(i)/numObjects)
// x := radius * math.Cos(angle)
// y := radius * math.Sin(angle)
// obj.TopLeft = geo.NewPoint(
// x-obj.Box.Width/2,
// y-obj.Box.Height/2,
// )
// }
// }
// func createCircularArc(edge *d2graph.Edge) {
// if edge.Src == nil || edge.Dst == nil {
// return
// }
// srcCenter := edge.Src.Center()
// dstCenter := edge.Dst.Center()
// srcAngle := math.Atan2(srcCenter.Y, srcCenter.X)
// dstAngle := math.Atan2(dstCenter.Y, dstCenter.X)
// if dstAngle < srcAngle {
// dstAngle += 2 * math.Pi
// }
// arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
// path := make([]*geo.Point, 0, ARC_STEPS+1)
// for i := 0; i <= ARC_STEPS; i++ {
// t := float64(i) / float64(ARC_STEPS)
// angle := srcAngle + t*(dstAngle-srcAngle)
// x := arcRadius * math.Cos(angle)
// y := arcRadius * math.Sin(angle)
// path = append(path, geo.NewPoint(x, y))
// }
// path[0] = srcCenter
// path[len(path)-1] = dstCenter
// startIndex, newSrc := clampPointOutsideBox(edge.Src.Box, path, 0)
// endIndex, newDst := clampPointOutsideBoxReverse(edge.Dst.Box, path, len(path)-1)
// path[0] = newSrc
// path[len(path)-1] = newDst
// edge.Route = path[startIndex : endIndex+1]
// edge.IsCurve = true
// }
// func clampPointOutsideBox(box *geo.Box, path []*geo.Point, startIdx int) (int, *geo.Point) {
// if startIdx >= len(path)-1 {
// return startIdx, path[startIdx]
// }
// if !boxContains(box, path[startIdx]) {
// return startIdx, path[startIdx]
// }
// for i := startIdx + 1; i < len(path); i++ {
// if boxContains(box, path[i]) {
// continue
// }
// seg := geo.NewSegment(path[i-1], path[i])
// inters := boxIntersections(box, *seg)
// if len(inters) > 0 {
// return i, inters[0]
// }
// return i, path[i]
// }
// last := len(path) - 1
// return last, path[last]
// }
// func clampPointOutsideBoxReverse(box *geo.Box, path []*geo.Point, endIdx int) (int, *geo.Point) {
// if endIdx <= 0 {
// return endIdx, path[endIdx]
// }
// if !boxContains(box, path[endIdx]) {
// return endIdx, path[endIdx]
// }
// for j := endIdx - 1; j >= 0; j-- {
// if boxContains(box, path[j]) {
// continue
// }
// seg := geo.NewSegment(path[j], path[j+1])
// inters := boxIntersections(box, *seg)
// if len(inters) > 0 {
// return j, inters[0]
// }
// return j, path[j]
// }
// return 0, path[0]
// }
// func boxContains(b *geo.Box, p *geo.Point) bool {
// return p.X >= b.TopLeft.X &&
// p.X <= b.TopLeft.X+b.Width &&
// p.Y >= b.TopLeft.Y &&
// p.Y <= b.TopLeft.Y+b.Height
// }
// func boxIntersections(b *geo.Box, seg geo.Segment) []*geo.Point {
// return b.Intersections(seg)
// }
// func positionLabelsIcons(obj *d2graph.Object) {
// if obj.Icon != nil && obj.IconPosition == nil {
// if len(obj.ChildrenArray) > 0 {
// obj.IconPosition = go2.Pointer(label.OutsideTopLeft.String())
// if obj.LabelPosition == nil {
// obj.LabelPosition = go2.Pointer(label.OutsideTopRight.String())
// return
// }
// } else if obj.SQLTable != nil || obj.Class != nil || obj.Language != "" {
// obj.IconPosition = go2.Pointer(label.OutsideTopLeft.String())
// } else {
// obj.IconPosition = go2.Pointer(label.InsideMiddleCenter.String())
// }
// }
// if obj.HasLabel() && obj.LabelPosition == nil {
// if len(obj.ChildrenArray) > 0 {
// obj.LabelPosition = go2.Pointer(label.OutsideTopCenter.String())
// } else if obj.HasOutsideBottomLabel() {
// obj.LabelPosition = go2.Pointer(label.OutsideBottomCenter.String())
// } else if obj.Icon != nil {
// obj.LabelPosition = go2.Pointer(label.InsideTopCenter.String())
// } else {
// obj.LabelPosition = go2.Pointer(label.InsideMiddleCenter.String())
// }
// if float64(obj.LabelDimensions.Width) > obj.Width ||
// float64(obj.LabelDimensions.Height) > obj.Height {
// if len(obj.ChildrenArray) > 0 {
// obj.LabelPosition = go2.Pointer(label.OutsideTopCenter.String())
// } else {
// obj.LabelPosition = go2.Pointer(label.OutsideBottomCenter.String())
// }
// }
// }
// }
package d2cycle package d2cycle
import ( import (
@ -259,9 +64,6 @@ func positionObjects(objects []*d2graph.Object, radius float64) {
} }
} }
// createCircularArc computes an arc along a circle from the source to destination,
// then computes the exact intersection points between a ray from the object's center
// and its bounding box. This ensures that the arrow precisely touches the boundary.
func createCircularArc(edge *d2graph.Edge) { func createCircularArc(edge *d2graph.Edge) {
if edge.Src == nil || edge.Dst == nil { if edge.Src == nil || edge.Dst == nil {
return return
@ -276,10 +78,8 @@ func createCircularArc(edge *d2graph.Edge) {
dstAngle += 2 * math.Pi dstAngle += 2 * math.Pi
} }
// Here we use the source center's distance as the arc radius.
arcRadius := math.Hypot(srcCenter.X, srcCenter.Y) arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
// Sample points along the circular arc.
path := make([]*geo.Point, 0, ARC_STEPS+1) path := make([]*geo.Point, 0, ARC_STEPS+1)
for i := 0; i <= ARC_STEPS; i++ { for i := 0; i <= ARC_STEPS; i++ {
t := float64(i) / float64(ARC_STEPS) t := float64(i) / float64(ARC_STEPS)
@ -288,51 +88,73 @@ func createCircularArc(edge *d2graph.Edge) {
y := arcRadius * math.Sin(angle) y := arcRadius * math.Sin(angle)
path = append(path, geo.NewPoint(x, y)) path = append(path, geo.NewPoint(x, y))
} }
// Instead of relying on iterative clamping, we compute exact intersection points. path[0] = srcCenter
// For the source, the ray is from the center toward the second sample point. path[len(path)-1] = dstCenter
newSrc := rayRectangleIntersection(srcCenter, edge.Src.Box, path[1].X-srcCenter.X, path[1].Y-srcCenter.Y)
// For the destination, the ray is from the center in the direction opposite to the second-to-last sample. startIndex, newSrc := clampPointOutsideBox(edge.Src.Box, path, 0)
newDst := rayRectangleIntersection(dstCenter, edge.Dst.Box, dstCenter.X-path[len(path)-2].X, dstCenter.Y-path[len(path)-2].Y) endIndex, newDst := clampPointOutsideBoxReverse(edge.Dst.Box, path, len(path)-1)
path[0] = newSrc path[0] = newSrc
path[len(path)-1] = newDst path[len(path)-1] = newDst
edge.Route = path edge.Route = path[startIndex : endIndex+1]
edge.IsCurve = true edge.IsCurve = true
} }
// rayRectangleIntersection computes the exact intersection point of a ray starting at 'center' func clampPointOutsideBox(box *geo.Box, path []*geo.Point, startIdx int) (int, *geo.Point) {
// with direction (dx, dy) and the boundary of an axis-aligned rectangle 'box'. if startIdx >= len(path)-1 {
// The rectangle is defined by its TopLeft corner, width, and height. return startIdx, path[startIdx]
func rayRectangleIntersection(center *geo.Point, box *geo.Box, dx, dy float64) *geo.Point { }
L := box.TopLeft.X if !boxContains(box, path[startIdx]) {
R := box.TopLeft.X + box.Width return startIdx, path[startIdx]
T := box.TopLeft.Y
B := box.TopLeft.Y + box.Height
var tX, tY float64
if dx > 0 {
tX = (R - center.X) / dx
} else if dx < 0 {
tX = (L - center.X) / dx
} else {
tX = math.Inf(1)
} }
if dy > 0 { for i := startIdx + 1; i < len(path); i++ {
tY = (B - center.Y) / dy if boxContains(box, path[i]) {
} else if dy < 0 { continue
tY = (T - center.Y) / dy }
} else { seg := geo.NewSegment(path[i-1], path[i])
tY = math.Inf(1) inters := boxIntersections(box, *seg)
if len(inters) > 0 {
return i, inters[0]
}
return i, path[i]
}
last := len(path) - 1
return last, path[last]
}
func clampPointOutsideBoxReverse(box *geo.Box, path []*geo.Point, endIdx int) (int, *geo.Point) {
if endIdx <= 0 {
return endIdx, path[endIdx]
}
if !boxContains(box, path[endIdx]) {
return endIdx, path[endIdx]
} }
t := tX for j := endIdx - 1; j >= 0; j-- {
if tY < t { if boxContains(box, path[j]) {
t = tY continue
}
seg := geo.NewSegment(path[j], path[j+1])
inters := boxIntersections(box, *seg)
if len(inters) > 0 {
return j, inters[0]
}
return j, path[j]
} }
return 0, path[0]
}
return geo.NewPoint(center.X+dx*t, center.Y+dy*t) func boxContains(b *geo.Box, p *geo.Point) bool {
return p.X >= b.TopLeft.X &&
p.X <= b.TopLeft.X+b.Width &&
p.Y >= b.TopLeft.Y &&
p.Y <= b.TopLeft.Y+b.Height
}
func boxIntersections(b *geo.Box, seg geo.Segment) []*geo.Point {
return b.Intersections(seg)
} }
func positionLabelsIcons(obj *d2graph.Object) { func positionLabelsIcons(obj *d2graph.Object) {
@ -370,4 +192,4 @@ func positionLabelsIcons(obj *d2graph.Object) {
} }
} }
} }
} }

View file

@ -539,18 +539,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": 26.5,
"y": -199.30599975585938
},
{
"x": 10.467000007629395,
"y": -199.72500610351562
},
{
"x": 20.905000686645508,
"y": -198.9040069580078
},
{ {
"x": 31.285999298095703, "x": 31.285999298095703,
"y": -197.53700256347656 "y": -197.53700256347656
@ -646,22 +634,6 @@
{ {
"x": 195.62899780273438, "x": 195.62899780273438,
"y": -41.582000732421875 "y": -41.582000732421875
},
{
"x": 197.53700256347656,
"y": -31.285999298095703
},
{
"x": 198.9040069580078,
"y": -20.905000686645508
},
{
"x": 199.72500610351562,
"y": -10.467000007629395
},
{
"x": 200.86399841308594,
"y": 33
} }
], ],
"isCurve": true, "isCurve": true,
@ -695,22 +667,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": 199.13499450683594,
"y": 33
},
{
"x": 199.72500610351562,
"y": 10.467000007629395
},
{
"x": 198.9040069580078,
"y": 20.905000686645508
},
{
"x": 197.53700256347656,
"y": 31.285999298095703
},
{ {
"x": 195.62899780273438, "x": 195.62899780273438,
"y": 41.582000732421875 "y": 41.582000732421875
@ -806,18 +762,6 @@
{ {
"x": 31.285999298095703, "x": 31.285999298095703,
"y": 197.53700256347656 "y": 197.53700256347656
},
{
"x": 20.905000686645508,
"y": 198.9040069580078
},
{
"x": 10.467000007629395,
"y": 199.72500610351562
},
{
"x": -26.499000549316406,
"y": 200.6929931640625
} }
], ],
"isCurve": true, "isCurve": true,
@ -851,18 +795,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": -26.499000549316406,
"y": 199.30599975585938
},
{
"x": -10.467000007629395,
"y": 199.72500610351562
},
{
"x": -20.905000686645508,
"y": 198.9040069580078
},
{ {
"x": -31.285999298095703, "x": -31.285999298095703,
"y": 197.53700256347656 "y": 197.53700256347656
@ -958,22 +890,6 @@
{ {
"x": -195.62899780273438, "x": -195.62899780273438,
"y": 41.582000732421875 "y": 41.582000732421875
},
{
"x": -197.53700256347656,
"y": 31.285999298095703
},
{
"x": -198.9040069580078,
"y": 20.905000686645508
},
{
"x": -199.72500610351562,
"y": 10.467000007629395
},
{
"x": -200.86399841308594,
"y": -32.999000549316406
} }
], ],
"isCurve": true, "isCurve": true,
@ -1007,14 +923,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": 539.5,
"y": -149.07400512695312
},
{
"x": 526.9509887695312,
"y": -149.51199340820312
},
{ {
"x": 540.833984375, "x": 540.833984375,
"y": -148.05299377441406 "y": -148.05299377441406
@ -1118,18 +1026,6 @@
{ {
"x": 703.2109985351562, "x": 703.2109985351562,
"y": 111.8030014038086 "y": 111.8030014038086
},
{
"x": 698.4359741210938,
"y": 124.9209976196289
},
{
"x": 692.7579956054688,
"y": 137.6739959716797
},
{
"x": 668.6580200195312,
"y": 182.99899291992188
} }
], ],
"isCurve": true, "isCurve": true,
@ -1163,18 +1059,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": 665.583984375,
"y": 182.99899291992188
},
{
"x": 678.8070068359375,
"y": 161.83799743652344
},
{
"x": 670.6019897460938,
"y": 173.1320037841797
},
{ {
"x": 661.6279907226562, "x": 661.6279907226562,
"y": 183.8260040283203 "y": 183.8260040283203
@ -1274,18 +1158,6 @@
{ {
"x": 364.3710021972656, "x": 364.3710021972656,
"y": 183.8260040283203 "y": 183.8260040283203
},
{
"x": 355.3970031738281,
"y": 173.1320037841797
},
{
"x": 347.1919860839844,
"y": 161.83799743652344
},
{
"x": 319.17401123046875,
"y": 117
} }
], ],
"isCurve": true, "isCurve": true,
@ -1319,14 +1191,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": 998.5,
"y": -198.61099243164062
},
{
"x": 992.905029296875,
"y": -198.9040069580078
},
{ {
"x": 1013.5819702148438, "x": 1013.5819702148438,
"y": -195.62899780273438 "y": -195.62899780273438
@ -1434,14 +1298,6 @@
{ {
"x": 1013.5819702148438, "x": 1013.5819702148438,
"y": 195.62899780273438 "y": 195.62899780273438
},
{
"x": 992.905029296875,
"y": 198.9040069580078
},
{
"x": 945.5,
"y": 201.38800048828125
} }
], ],
"isCurve": true, "isCurve": true,

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

@ -539,18 +539,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": 38.5,
"y": -187.30599975585938
},
{
"x": 22.466999053955078,
"y": -187.72500610351562
},
{
"x": 32.904998779296875,
"y": -186.9040069580078
},
{ {
"x": 43.2859992980957, "x": 43.2859992980957,
"y": -185.53700256347656 "y": -185.53700256347656
@ -646,22 +634,6 @@
{ {
"x": 207.62899780273438, "x": 207.62899780273438,
"y": -29.582000732421875 "y": -29.582000732421875
},
{
"x": 209.53700256347656,
"y": -19.285999298095703
},
{
"x": 210.9040069580078,
"y": -8.904999732971191
},
{
"x": 211.72500610351562,
"y": 1.531999945640564
},
{
"x": 212.86399841308594,
"y": 45
} }
], ],
"isCurve": true, "isCurve": true,
@ -695,22 +667,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": 211.13499450683594,
"y": 45
},
{
"x": 211.72500610351562,
"y": 22.466999053955078
},
{
"x": 210.9040069580078,
"y": 32.904998779296875
},
{
"x": 209.53700256347656,
"y": 43.2859992980957
},
{ {
"x": 207.62899780273438, "x": 207.62899780273438,
"y": 53.582000732421875 "y": 53.582000732421875
@ -806,18 +762,6 @@
{ {
"x": 43.2859992980957, "x": 43.2859992980957,
"y": 209.53700256347656 "y": 209.53700256347656
},
{
"x": 32.904998779296875,
"y": 210.9040069580078
},
{
"x": 22.466999053955078,
"y": 211.72500610351562
},
{
"x": -14.49899959564209,
"y": 212.6929931640625
} }
], ],
"isCurve": true, "isCurve": true,
@ -851,18 +795,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": -14.49899959564209,
"y": 211.30599975585938
},
{
"x": 1.531999945640564,
"y": 211.72500610351562
},
{
"x": -8.904999732971191,
"y": 210.9040069580078
},
{ {
"x": -19.285999298095703, "x": -19.285999298095703,
"y": 209.53700256347656 "y": 209.53700256347656
@ -958,22 +890,6 @@
{ {
"x": -183.62899780273438, "x": -183.62899780273438,
"y": 53.582000732421875 "y": 53.582000732421875
},
{
"x": -185.53700256347656,
"y": 43.2859992980957
},
{
"x": -186.9040069580078,
"y": 32.904998779296875
},
{
"x": -187.72500610351562,
"y": 22.466999053955078
},
{
"x": -188.86399841308594,
"y": -20.999000549316406
} }
], ],
"isCurve": true, "isCurve": true,
@ -1007,14 +923,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": 512,
"y": -137.07400512695312
},
{
"x": 499.45098876953125,
"y": -137.51199340820312
},
{ {
"x": 513.333984375, "x": 513.333984375,
"y": -136.05299377441406 "y": -136.05299377441406
@ -1118,18 +1026,6 @@
{ {
"x": 675.7109985351562, "x": 675.7109985351562,
"y": 123.8030014038086 "y": 123.8030014038086
},
{
"x": 670.9359741210938,
"y": 136.92100524902344
},
{
"x": 665.2579956054688,
"y": 149.6739959716797
},
{
"x": 641.1580200195312,
"y": 194.99899291992188
} }
], ],
"isCurve": true, "isCurve": true,
@ -1163,18 +1059,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": 638.083984375,
"y": 194.99899291992188
},
{
"x": 651.3070068359375,
"y": 173.83799743652344
},
{
"x": 643.1019897460938,
"y": 185.1320037841797
},
{ {
"x": 634.1279907226562, "x": 634.1279907226562,
"y": 195.8260040283203 "y": 195.8260040283203
@ -1274,18 +1158,6 @@
{ {
"x": 336.8710021972656, "x": 336.8710021972656,
"y": 195.8260040283203 "y": 195.8260040283203
},
{
"x": 327.8970031738281,
"y": 185.1320037841797
},
{
"x": 319.6919860839844,
"y": 173.83799743652344
},
{
"x": 291.67401123046875,
"y": 129
} }
], ],
"isCurve": true, "isCurve": true,
@ -1319,14 +1191,6 @@
"labelPercentage": 0, "labelPercentage": 0,
"link": "", "link": "",
"route": [ "route": [
{
"x": 931.4099731445312,
"y": -186.61099243164062
},
{
"x": 925.8150024414062,
"y": -186.9040069580078
},
{ {
"x": 946.4920043945312, "x": 946.4920043945312,
"y": -183.62899780273438 "y": -183.62899780273438
@ -1434,14 +1298,6 @@
{ {
"x": 946.4920043945312, "x": 946.4920043945312,
"y": 207.62899780273438 "y": 207.62899780273438
},
{
"x": 925.8150024414062,
"y": 210.9040069580078
},
{
"x": 878.4099731445312,
"y": 213.38800048828125
} }
], ],
"isCurve": true, "isCurve": true,

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB