try
This commit is contained in:
parent
738af0eff0
commit
59f4a1bb90
5 changed files with 259 additions and 189 deletions
|
|
@ -33,12 +33,13 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) e
|
||||||
positionObjects(objects, radius)
|
positionObjects(objects, radius)
|
||||||
|
|
||||||
for _, edge := range g.Edges {
|
for _, edge := range g.Edges {
|
||||||
createCircularArc(edge)
|
createCircularArc(edge, radius)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// calculateRadius determines the radius of the circular layout based on the number and size of objects.
|
||||||
func calculateRadius(objects []*d2graph.Object) float64 {
|
func calculateRadius(objects []*d2graph.Object) float64 {
|
||||||
numObjects := float64(len(objects))
|
numObjects := float64(len(objects))
|
||||||
maxSize := 0.0
|
maxSize := 0.0
|
||||||
|
|
@ -50,9 +51,10 @@ func calculateRadius(objects []*d2graph.Object) float64 {
|
||||||
return math.Max(minRadius, MIN_RADIUS)
|
return math.Max(minRadius, MIN_RADIUS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// positionObjects arranges objects in a circular pattern around the origin.
|
||||||
func positionObjects(objects []*d2graph.Object, radius float64) {
|
func positionObjects(objects []*d2graph.Object, radius float64) {
|
||||||
numObjects := float64(len(objects))
|
numObjects := float64(len(objects))
|
||||||
angleOffset := -math.Pi / 2
|
angleOffset := -math.Pi / 2 // Start at the top of the circle
|
||||||
|
|
||||||
for i, obj := range objects {
|
for i, obj := range objects {
|
||||||
angle := angleOffset + (2*math.Pi*float64(i)/numObjects)
|
angle := angleOffset + (2*math.Pi*float64(i)/numObjects)
|
||||||
|
|
@ -65,7 +67,8 @@ func positionObjects(objects []*d2graph.Object, radius float64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createCircularArc(edge *d2graph.Edge) {
|
// createCircularArc generates a curved edge route with corrected arrow orientation.
|
||||||
|
func createCircularArc(edge *d2graph.Edge, radius float64) {
|
||||||
if edge.Src == nil || edge.Dst == nil {
|
if edge.Src == nil || edge.Dst == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -73,32 +76,26 @@ func createCircularArc(edge *d2graph.Edge) {
|
||||||
srcCenter := edge.Src.Center()
|
srcCenter := edge.Src.Center()
|
||||||
dstCenter := edge.Dst.Center()
|
dstCenter := edge.Dst.Center()
|
||||||
|
|
||||||
srcAngle := math.Atan2(srcCenter.Y, srcCenter.X)
|
// Generate initial arc path from source center to destination center
|
||||||
dstAngle := math.Atan2(dstCenter.Y, dstCenter.X)
|
path := generateArcPoints(srcCenter, dstCenter, radius, ARC_STEPS)
|
||||||
if dstAngle < srcAngle {
|
|
||||||
dstAngle += 2 * math.Pi
|
|
||||||
}
|
|
||||||
|
|
||||||
arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
|
// Clamp endpoints to the boundaries of the source and destination boxes
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
// Clamp endpoints to the boundaries of the source and destination boxes.
|
|
||||||
_, newSrc := clampPointOutsideBox(edge.Src.Box, path, 0)
|
_, newSrc := clampPointOutsideBox(edge.Src.Box, path, 0)
|
||||||
_, newDst := clampPointOutsideBoxReverse(edge.Dst.Box, path, len(path)-1)
|
_, 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
|
||||||
|
|
||||||
// Trim redundant path points that fall inside node boundaries.
|
// Add a point before newDst along the tangent direction to correct arrow orientation
|
||||||
|
if len(path) >= 2 {
|
||||||
|
dstAngle := math.Atan2(newDst.Y, newDst.X)
|
||||||
|
tangent := geo.NewPoint(-math.Sin(dstAngle), math.Cos(dstAngle))
|
||||||
|
ε := 0.01 * radius // Small offset, e.g., 1% of radius
|
||||||
|
preDst := geo.NewPoint(newDst.X-ε*tangent.X, newDst.Y-ε*tangent.Y)
|
||||||
|
// Insert preDst before newDst
|
||||||
|
path = append(path[:len(path)-1], preDst, newDst)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trim redundant path points that fall inside node boundaries
|
||||||
path = trimPathPoints(path, edge.Src.Box)
|
path = trimPathPoints(path, edge.Src.Box)
|
||||||
path = trimPathPoints(path, edge.Dst.Box)
|
path = trimPathPoints(path, edge.Dst.Box)
|
||||||
|
|
||||||
|
|
@ -106,9 +103,34 @@ func createCircularArc(edge *d2graph.Edge) {
|
||||||
edge.IsCurve = true
|
edge.IsCurve = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generateArcPoints creates points along a circular arc from src to dst.
|
||||||
|
func generateArcPoints(src, dst *geo.Point, radius float64, steps int) []*geo.Point {
|
||||||
|
// Calculate angles relative to the center (0,0)
|
||||||
|
srcAngle := math.Atan2(src.Y, src.X)
|
||||||
|
dstAngle := math.Atan2(dst.Y, dst.X)
|
||||||
|
|
||||||
// clampPointOutsideBox walks forward along the path until it finds a point outside the box,
|
// Ensure the arc goes the shorter way
|
||||||
// then replaces the point with a precise intersection.
|
if dstAngle < srcAngle {
|
||||||
|
dstAngle += 2 * math.Pi
|
||||||
|
}
|
||||||
|
angleDiff := dstAngle - srcAngle
|
||||||
|
if angleDiff > math.Pi {
|
||||||
|
dstAngle -= 2 * math.Pi
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate points along the arc
|
||||||
|
path := make([]*geo.Point, 0, steps+1)
|
||||||
|
for i := 0; i <= steps; i++ {
|
||||||
|
t := float64(i) / float64(steps)
|
||||||
|
angle := srcAngle + t*(dstAngle-srcAngle)
|
||||||
|
x := radius * math.Cos(angle)
|
||||||
|
y := radius * math.Sin(angle)
|
||||||
|
path = append(path, geo.NewPoint(x, y))
|
||||||
|
}
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
// clampPointOutsideBox finds the first point outside the box and computes the precise intersection.
|
||||||
func clampPointOutsideBox(box *geo.Box, path []*geo.Point, startIdx int) (int, *geo.Point) {
|
func clampPointOutsideBox(box *geo.Box, path []*geo.Point, startIdx int) (int, *geo.Point) {
|
||||||
if startIdx >= len(path)-1 {
|
if startIdx >= len(path)-1 {
|
||||||
return startIdx, path[startIdx]
|
return startIdx, path[startIdx]
|
||||||
|
|
@ -131,7 +153,7 @@ func clampPointOutsideBox(box *geo.Box, path []*geo.Point, startIdx int) (int, *
|
||||||
return len(path)-1, path[len(path)-1]
|
return len(path)-1, path[len(path)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
// clampPointOutsideBoxReverse works similarly but in reverse order.
|
// clampPointOutsideBoxReverse works similarly but traverses the path in reverse.
|
||||||
func clampPointOutsideBoxReverse(box *geo.Box, path []*geo.Point, endIdx int) (int, *geo.Point) {
|
func clampPointOutsideBoxReverse(box *geo.Box, path []*geo.Point, endIdx int) (int, *geo.Point) {
|
||||||
if endIdx <= 0 {
|
if endIdx <= 0 {
|
||||||
return endIdx, path[endIdx]
|
return endIdx, path[endIdx]
|
||||||
|
|
@ -154,8 +176,7 @@ func clampPointOutsideBoxReverse(box *geo.Box, path []*geo.Point, endIdx int) (i
|
||||||
return 0, path[0]
|
return 0, path[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// findPreciseIntersection calculates intersection points between seg and all four sides of the box,
|
// findPreciseIntersection calculates the closest intersection between a segment and box boundaries.
|
||||||
// then returns the intersection closest to seg.Start.
|
|
||||||
func findPreciseIntersection(box *geo.Box, seg geo.Segment) *geo.Point {
|
func findPreciseIntersection(box *geo.Box, seg geo.Segment) *geo.Point {
|
||||||
intersections := []struct {
|
intersections := []struct {
|
||||||
point *geo.Point
|
point *geo.Point
|
||||||
|
|
@ -170,9 +191,9 @@ func findPreciseIntersection(box *geo.Box, seg geo.Segment) *geo.Point {
|
||||||
dx := seg.End.X - seg.Start.X
|
dx := seg.End.X - seg.Start.X
|
||||||
dy := seg.End.Y - seg.Start.Y
|
dy := seg.End.Y - seg.Start.Y
|
||||||
|
|
||||||
// Check vertical boundaries.
|
// Check vertical boundaries
|
||||||
if dx != 0 {
|
if dx != 0 {
|
||||||
// Left boundary.
|
// Left boundary
|
||||||
t := (left - seg.Start.X) / dx
|
t := (left - seg.Start.X) / dx
|
||||||
if t >= 0 && t <= 1 {
|
if t >= 0 && t <= 1 {
|
||||||
y := seg.Start.Y + t*dy
|
y := seg.Start.Y + t*dy
|
||||||
|
|
@ -183,7 +204,7 @@ func findPreciseIntersection(box *geo.Box, seg geo.Segment) *geo.Point {
|
||||||
}{geo.NewPoint(left, y), t})
|
}{geo.NewPoint(left, y), t})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Right boundary.
|
// Right boundary
|
||||||
t = (right - seg.Start.X) / dx
|
t = (right - seg.Start.X) / dx
|
||||||
if t >= 0 && t <= 1 {
|
if t >= 0 && t <= 1 {
|
||||||
y := seg.Start.Y + t*dy
|
y := seg.Start.Y + t*dy
|
||||||
|
|
@ -196,9 +217,9 @@ func findPreciseIntersection(box *geo.Box, seg geo.Segment) *geo.Point {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check horizontal boundaries.
|
// Check horizontal boundaries
|
||||||
if dy != 0 {
|
if dy != 0 {
|
||||||
// Top boundary.
|
// Top boundary
|
||||||
t := (top - seg.Start.Y) / dy
|
t := (top - seg.Start.Y) / dy
|
||||||
if t >= 0 && t <= 1 {
|
if t >= 0 && t <= 1 {
|
||||||
x := seg.Start.X + t*dx
|
x := seg.Start.X + t*dx
|
||||||
|
|
@ -209,7 +230,7 @@ func findPreciseIntersection(box *geo.Box, seg geo.Segment) *geo.Point {
|
||||||
}{geo.NewPoint(x, top), t})
|
}{geo.NewPoint(x, top), t})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Bottom boundary.
|
// Bottom boundary
|
||||||
t = (bottom - seg.Start.Y) / dy
|
t = (bottom - seg.Start.Y) / dy
|
||||||
if t >= 0 && t <= 1 {
|
if t >= 0 && t <= 1 {
|
||||||
x := seg.Start.X + t*dx
|
x := seg.Start.X + t*dx
|
||||||
|
|
@ -226,14 +247,14 @@ func findPreciseIntersection(box *geo.Box, seg geo.Segment) *geo.Point {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort intersections by t (distance from seg.Start) and return the closest.
|
// Sort intersections by t (distance from seg.Start) and return the closest
|
||||||
sort.Slice(intersections, func(i, j int) bool {
|
sort.Slice(intersections, func(i, j int) bool {
|
||||||
return intersections[i].t < intersections[j].t
|
return intersections[i].t < intersections[j].t
|
||||||
})
|
})
|
||||||
return intersections[0].point
|
return intersections[0].point
|
||||||
}
|
}
|
||||||
|
|
||||||
// trimPathPoints removes intermediate points that fall inside the given box while preserving endpoints.
|
// trimPathPoints removes intermediate points inside the box while retaining endpoints.
|
||||||
func trimPathPoints(path []*geo.Point, box *geo.Box) []*geo.Point {
|
func trimPathPoints(path []*geo.Point, box *geo.Box) []*geo.Point {
|
||||||
if len(path) <= 2 {
|
if len(path) <= 2 {
|
||||||
return path
|
return path
|
||||||
|
|
@ -248,7 +269,7 @@ func trimPathPoints(path []*geo.Point, box *geo.Box) []*geo.Point {
|
||||||
return trimmed
|
return trimmed
|
||||||
}
|
}
|
||||||
|
|
||||||
// boxContains uses strict inequalities so that points exactly on the boundary are considered outside.
|
// boxContains checks if a point is strictly inside the box (boundary points are outside).
|
||||||
func boxContains(b *geo.Box, p *geo.Point) bool {
|
func boxContains(b *geo.Box, p *geo.Point) bool {
|
||||||
return p.X > b.TopLeft.X &&
|
return p.X > b.TopLeft.X &&
|
||||||
p.X < b.TopLeft.X+b.Width &&
|
p.X < b.TopLeft.X+b.Width &&
|
||||||
|
|
@ -256,6 +277,7 @@ func boxContains(b *geo.Box, p *geo.Point) bool {
|
||||||
p.Y < b.TopLeft.Y+b.Height
|
p.Y < b.TopLeft.Y+b.Height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// positionLabelsIcons sets default positions for labels and icons on objects.
|
||||||
func positionLabelsIcons(obj *d2graph.Object) {
|
func positionLabelsIcons(obj *d2graph.Object) {
|
||||||
if obj.Icon != nil && obj.IconPosition == nil {
|
if obj.Icon != nil && obj.IconPosition == nil {
|
||||||
if len(obj.ChildrenArray) > 0 {
|
if len(obj.ChildrenArray) > 0 {
|
||||||
|
|
|
||||||
24
e2etests/testdata/txtar/cycle-diagram/dagre/board.exp.json
generated
vendored
24
e2etests/testdata/txtar/cycle-diagram/dagre/board.exp.json
generated
vendored
|
|
@ -867,6 +867,10 @@
|
||||||
"x": 197.02099609375,
|
"x": 197.02099609375,
|
||||||
"y": -34.3849983215332
|
"y": -34.3849983215332
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": 196.9219970703125,
|
||||||
|
"y": -34.97200012207031
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": 197.2519989013672,
|
"x": 197.2519989013672,
|
||||||
"y": -33
|
"y": -33
|
||||||
|
|
@ -1231,6 +1235,10 @@
|
||||||
"x": 28.18000030517578,
|
"x": 28.18000030517578,
|
||||||
"y": 198.00399780273438
|
"y": 198.00399780273438
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": 28.48200035095215,
|
||||||
|
"y": 197.96499633789062
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": 26.5,
|
"x": 26.5,
|
||||||
"y": 198.22999572753906
|
"y": 198.22999572753906
|
||||||
|
|
@ -1595,6 +1603,10 @@
|
||||||
"x": -197.02099609375,
|
"x": -197.02099609375,
|
||||||
"y": 34.3849983215332
|
"y": 34.3849983215332
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": -196.9219970703125,
|
||||||
|
"y": 34.97200012207031
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": -197.2519989013672,
|
"x": -197.2519989013672,
|
||||||
"y": 33
|
"y": 33
|
||||||
|
|
@ -1975,6 +1987,10 @@
|
||||||
"x": 701.875,
|
"x": 701.875,
|
||||||
"y": 115.77300262451172
|
"y": 115.77300262451172
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": 702.10302734375,
|
||||||
|
"y": 115.11499786376953
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": 701.4329833984375,
|
"x": 701.4329833984375,
|
||||||
"y": 116.9990005493164
|
"y": 116.9990005493164
|
||||||
|
|
@ -2339,6 +2355,10 @@
|
||||||
"x": 364.3710021972656,
|
"x": 364.3710021972656,
|
||||||
"y": 183.8260040283203
|
"y": 183.8260040283203
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": 364.97198486328125,
|
||||||
|
"y": 184.4929962158203
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": 363.6419982910156,
|
"x": 363.6419982910156,
|
||||||
"y": 183
|
"y": 183
|
||||||
|
|
@ -2743,6 +2763,10 @@
|
||||||
"x": 1003.2860107421875,
|
"x": 1003.2860107421875,
|
||||||
"y": 197.53700256347656
|
"y": 197.53700256347656
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": 1000.4819946289062,
|
||||||
|
"y": 197.9530029296875
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": 998.5,
|
"x": 998.5,
|
||||||
"y": 198.21800231933594
|
"y": 198.21800231933594
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 26 KiB |
24
e2etests/testdata/txtar/cycle-diagram/elk/board.exp.json
generated
vendored
24
e2etests/testdata/txtar/cycle-diagram/elk/board.exp.json
generated
vendored
|
|
@ -867,6 +867,10 @@
|
||||||
"x": 209.02099609375,
|
"x": 209.02099609375,
|
||||||
"y": -22.385000228881836
|
"y": -22.385000228881836
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": 208.9219970703125,
|
||||||
|
"y": -22.972000122070312
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": 209.2519989013672,
|
"x": 209.2519989013672,
|
||||||
"y": -21
|
"y": -21
|
||||||
|
|
@ -1231,6 +1235,10 @@
|
||||||
"x": 40.18000030517578,
|
"x": 40.18000030517578,
|
||||||
"y": 210.00399780273438
|
"y": 210.00399780273438
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": 40.481998443603516,
|
||||||
|
"y": 209.96499633789062
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": 38.5,
|
"x": 38.5,
|
||||||
"y": 210.22999572753906
|
"y": 210.22999572753906
|
||||||
|
|
@ -1595,6 +1603,10 @@
|
||||||
"x": -185.02099609375,
|
"x": -185.02099609375,
|
||||||
"y": 46.3849983215332
|
"y": 46.3849983215332
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": -184.9219970703125,
|
||||||
|
"y": 46.97200012207031
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": -185.2519989013672,
|
"x": -185.2519989013672,
|
||||||
"y": 45
|
"y": 45
|
||||||
|
|
@ -1975,6 +1987,10 @@
|
||||||
"x": 674.375,
|
"x": 674.375,
|
||||||
"y": 127.77300262451172
|
"y": 127.77300262451172
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": 674.60302734375,
|
||||||
|
"y": 127.11499786376953
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": 673.9329833984375,
|
"x": 673.9329833984375,
|
||||||
"y": 128.99899291992188
|
"y": 128.99899291992188
|
||||||
|
|
@ -2339,6 +2355,10 @@
|
||||||
"x": 336.8710021972656,
|
"x": 336.8710021972656,
|
||||||
"y": 195.8260040283203
|
"y": 195.8260040283203
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": 337.47198486328125,
|
||||||
|
"y": 196.4929962158203
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": 336.1419982910156,
|
"x": 336.1419982910156,
|
||||||
"y": 195
|
"y": 195
|
||||||
|
|
@ -2743,6 +2763,10 @@
|
||||||
"x": 936.197021484375,
|
"x": 936.197021484375,
|
||||||
"y": 209.53700256347656
|
"y": 209.53700256347656
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"x": 933.3920288085938,
|
||||||
|
"y": 209.9530029296875
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"x": 931.4099731445312,
|
"x": 931.4099731445312,
|
||||||
"y": 210.21800231933594
|
"y": 210.21800231933594
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 26 KiB |
Loading…
Reference in a new issue