try
This commit is contained in:
parent
39d2022956
commit
3c1be1ee2a
5 changed files with 440 additions and 301 deletions
|
|
@ -15,6 +15,7 @@ const (
|
|||
PADDING = 20
|
||||
MIN_SEGMENT_LEN = 10
|
||||
ARC_STEPS = 30
|
||||
EPSILON = 1e-10 // Small value for floating point comparisons
|
||||
)
|
||||
|
||||
func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) error {
|
||||
|
|
@ -31,7 +32,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) e
|
|||
positionObjects(objects, radius)
|
||||
|
||||
for _, edge := range g.Edges {
|
||||
createCircularArc(edge)
|
||||
createPreciseCircularArc(edge)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -64,7 +65,7 @@ func positionObjects(objects []*d2graph.Object, radius float64) {
|
|||
}
|
||||
}
|
||||
|
||||
func createCircularArc(edge *d2graph.Edge) {
|
||||
func createPreciseCircularArc(edge *d2graph.Edge) {
|
||||
if edge.Src == nil || edge.Dst == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -72,6 +73,7 @@ func createCircularArc(edge *d2graph.Edge) {
|
|||
srcCenter := edge.Src.Center()
|
||||
dstCenter := edge.Dst.Center()
|
||||
|
||||
// Calculate angles in the circular layout
|
||||
srcAngle := math.Atan2(srcCenter.Y, srcCenter.X)
|
||||
dstAngle := math.Atan2(dstCenter.Y, dstCenter.X)
|
||||
if dstAngle < srcAngle {
|
||||
|
|
@ -80,6 +82,7 @@ func createCircularArc(edge *d2graph.Edge) {
|
|||
|
||||
arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
|
||||
|
||||
// Generate initial path points
|
||||
path := make([]*geo.Point, 0, ARC_STEPS+1)
|
||||
for i := 0; i <= ARC_STEPS; i++ {
|
||||
t := float64(i) / float64(ARC_STEPS)
|
||||
|
|
@ -88,73 +91,113 @@ func createCircularArc(edge *d2graph.Edge) {
|
|||
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)
|
||||
// Find precise intersection points
|
||||
srcIntersection := findPreciseBoxIntersection(edge.Src.Box, path[0], path[1])
|
||||
dstIntersection := findPreciseBoxIntersection(edge.Dst.Box, path[len(path)-1], path[len(path)-2])
|
||||
|
||||
path[0] = newSrc
|
||||
path[len(path)-1] = newDst
|
||||
// Update path endpoints with precise intersections
|
||||
path[0] = srcIntersection
|
||||
path[len(path)-1] = dstIntersection
|
||||
|
||||
edge.Route = path[startIndex : endIndex+1]
|
||||
// Remove any points that might be inside the boxes
|
||||
startIdx := 0
|
||||
endIdx := len(path) - 1
|
||||
|
||||
for i := 1; i < len(path)-1; i++ {
|
||||
if boxContains(edge.Src.Box, path[i]) {
|
||||
startIdx = i
|
||||
}
|
||||
if boxContains(edge.Dst.Box, path[i]) {
|
||||
endIdx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
edge.Route = path[startIdx:endIdx+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]
|
||||
func findPreciseBoxIntersection(box *geo.Box, p1, p2 *geo.Point) *geo.Point {
|
||||
// Define box edges as line segments
|
||||
edges := []geo.Segment{
|
||||
// Top edge
|
||||
*geo.NewSegment(
|
||||
geo.NewPoint(box.TopLeft.X, box.TopLeft.Y),
|
||||
geo.NewPoint(box.TopLeft.X+box.Width, box.TopLeft.Y),
|
||||
),
|
||||
// Right edge
|
||||
*geo.NewSegment(
|
||||
geo.NewPoint(box.TopLeft.X+box.Width, box.TopLeft.Y),
|
||||
geo.NewPoint(box.TopLeft.X+box.Width, box.TopLeft.Y+box.Height),
|
||||
),
|
||||
// Bottom edge
|
||||
*geo.NewSegment(
|
||||
geo.NewPoint(box.TopLeft.X, box.TopLeft.Y+box.Height),
|
||||
geo.NewPoint(box.TopLeft.X+box.Width, box.TopLeft.Y+box.Height),
|
||||
),
|
||||
// Left edge
|
||||
*geo.NewSegment(
|
||||
geo.NewPoint(box.TopLeft.X, box.TopLeft.Y),
|
||||
geo.NewPoint(box.TopLeft.X, box.TopLeft.Y+box.Height),
|
||||
),
|
||||
}
|
||||
|
||||
for i := startIdx + 1; i < len(path); i++ {
|
||||
if boxContains(box, path[i]) {
|
||||
continue
|
||||
// Line segment from p1 to p2
|
||||
line := *geo.NewSegment(p1, p2)
|
||||
|
||||
// Find the intersection point closest to p1
|
||||
var closestIntersection *geo.Point
|
||||
minDist := math.MaxFloat64
|
||||
|
||||
for _, edge := range edges {
|
||||
if intersection := findSegmentIntersection(line, edge); intersection != nil {
|
||||
dist := math.Hypot(
|
||||
intersection.X-p1.X,
|
||||
intersection.Y-p1.Y,
|
||||
)
|
||||
if dist < minDist {
|
||||
minDist = dist
|
||||
closestIntersection = intersection
|
||||
}
|
||||
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 closestIntersection != nil {
|
||||
return closestIntersection
|
||||
}
|
||||
if !boxContains(box, path[endIdx]) {
|
||||
return endIdx, path[endIdx]
|
||||
return p1
|
||||
}
|
||||
|
||||
for j := endIdx - 1; j >= 0; j-- {
|
||||
if boxContains(box, path[j]) {
|
||||
continue
|
||||
func findSegmentIntersection(s1, s2 geo.Segment) *geo.Point {
|
||||
// Calculate the intersection of two line segments using parametric equations
|
||||
x1, y1 := s1.Start.X, s1.Start.Y
|
||||
x2, y2 := s1.End.X, s1.End.Y
|
||||
x3, y3 := s2.Start.X, s2.Start.Y
|
||||
x4, y4 := s2.End.X, s2.End.Y
|
||||
|
||||
denominator := (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4)
|
||||
if math.Abs(denominator) < EPSILON {
|
||||
return nil
|
||||
}
|
||||
seg := geo.NewSegment(path[j], path[j+1])
|
||||
inters := boxIntersections(box, *seg)
|
||||
if len(inters) > 0 {
|
||||
return j, inters[0]
|
||||
|
||||
t := ((x1-x3)*(y3-y4) - (y1-y3)*(x3-x4)) / denominator
|
||||
u := -((x1-x2)*(y1-y3) - (y1-y2)*(x1-x3)) / denominator
|
||||
|
||||
if t >= 0 && t <= 1 && u >= 0 && u <= 1 {
|
||||
x := x1 + t*(x2-x1)
|
||||
y := y1 + t*(y2-y1)
|
||||
return geo.NewPoint(x, y)
|
||||
}
|
||||
return j, path[j]
|
||||
}
|
||||
return 0, path[0]
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
return p.X >= b.TopLeft.X-EPSILON &&
|
||||
p.X <= b.TopLeft.X+b.Width+EPSILON &&
|
||||
p.Y >= b.TopLeft.Y-EPSILON &&
|
||||
p.Y <= b.TopLeft.Y+b.Height+EPSILON
|
||||
}
|
||||
|
||||
func positionLabelsIcons(obj *d2graph.Object) {
|
||||
|
|
|
|||
48
e2etests/testdata/txtar/cycle-diagram/dagre/board.exp.json
generated
vendored
48
e2etests/testdata/txtar/cycle-diagram/dagre/board.exp.json
generated
vendored
|
|
@ -539,6 +539,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": 20.905000686645508,
|
||||
"y": -198.9040069580078
|
||||
},
|
||||
{
|
||||
"x": 31.285999298095703,
|
||||
"y": -197.53700256347656
|
||||
|
|
@ -634,6 +638,10 @@
|
|||
{
|
||||
"x": 195.62899780273438,
|
||||
"y": -41.582000732421875
|
||||
},
|
||||
{
|
||||
"x": 197.53700256347656,
|
||||
"y": -31.285999298095703
|
||||
}
|
||||
],
|
||||
"isCurve": true,
|
||||
|
|
@ -667,6 +675,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": 197.53700256347656,
|
||||
"y": 31.285999298095703
|
||||
},
|
||||
{
|
||||
"x": 195.62899780273438,
|
||||
"y": 41.582000732421875
|
||||
|
|
@ -762,6 +774,10 @@
|
|||
{
|
||||
"x": 31.285999298095703,
|
||||
"y": 197.53700256347656
|
||||
},
|
||||
{
|
||||
"x": 20.905000686645508,
|
||||
"y": 198.9040069580078
|
||||
}
|
||||
],
|
||||
"isCurve": true,
|
||||
|
|
@ -795,6 +811,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": -20.905000686645508,
|
||||
"y": 198.9040069580078
|
||||
},
|
||||
{
|
||||
"x": -31.285999298095703,
|
||||
"y": 197.53700256347656
|
||||
|
|
@ -890,6 +910,10 @@
|
|||
{
|
||||
"x": -195.62899780273438,
|
||||
"y": 41.582000732421875
|
||||
},
|
||||
{
|
||||
"x": -197.53700256347656,
|
||||
"y": 31.285999298095703
|
||||
}
|
||||
],
|
||||
"isCurve": true,
|
||||
|
|
@ -923,6 +947,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": 526.9509887695312,
|
||||
"y": -149.51199340820312
|
||||
},
|
||||
{
|
||||
"x": 540.833984375,
|
||||
"y": -148.05299377441406
|
||||
|
|
@ -1026,6 +1054,10 @@
|
|||
{
|
||||
"x": 703.2109985351562,
|
||||
"y": 111.8030014038086
|
||||
},
|
||||
{
|
||||
"x": 698.4359741210938,
|
||||
"y": 124.9209976196289
|
||||
}
|
||||
],
|
||||
"isCurve": true,
|
||||
|
|
@ -1059,6 +1091,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": 670.6019897460938,
|
||||
"y": 173.1320037841797
|
||||
},
|
||||
{
|
||||
"x": 661.6279907226562,
|
||||
"y": 183.8260040283203
|
||||
|
|
@ -1158,6 +1194,10 @@
|
|||
{
|
||||
"x": 364.3710021972656,
|
||||
"y": 183.8260040283203
|
||||
},
|
||||
{
|
||||
"x": 355.3970031738281,
|
||||
"y": 173.1320037841797
|
||||
}
|
||||
],
|
||||
"isCurve": true,
|
||||
|
|
@ -1191,6 +1231,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": 992.905029296875,
|
||||
"y": -198.9040069580078
|
||||
},
|
||||
{
|
||||
"x": 1013.5819702148438,
|
||||
"y": -195.62899780273438
|
||||
|
|
@ -1298,6 +1342,10 @@
|
|||
{
|
||||
"x": 1013.5819702148438,
|
||||
"y": 195.62899780273438
|
||||
},
|
||||
{
|
||||
"x": 992.905029296875,
|
||||
"y": 198.9040069580078
|
||||
}
|
||||
],
|
||||
"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 |
48
e2etests/testdata/txtar/cycle-diagram/elk/board.exp.json
generated
vendored
48
e2etests/testdata/txtar/cycle-diagram/elk/board.exp.json
generated
vendored
|
|
@ -539,6 +539,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": 32.904998779296875,
|
||||
"y": -186.9040069580078
|
||||
},
|
||||
{
|
||||
"x": 43.2859992980957,
|
||||
"y": -185.53700256347656
|
||||
|
|
@ -634,6 +638,10 @@
|
|||
{
|
||||
"x": 207.62899780273438,
|
||||
"y": -29.582000732421875
|
||||
},
|
||||
{
|
||||
"x": 209.53700256347656,
|
||||
"y": -19.285999298095703
|
||||
}
|
||||
],
|
||||
"isCurve": true,
|
||||
|
|
@ -667,6 +675,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": 209.53700256347656,
|
||||
"y": 43.2859992980957
|
||||
},
|
||||
{
|
||||
"x": 207.62899780273438,
|
||||
"y": 53.582000732421875
|
||||
|
|
@ -762,6 +774,10 @@
|
|||
{
|
||||
"x": 43.2859992980957,
|
||||
"y": 209.53700256347656
|
||||
},
|
||||
{
|
||||
"x": 32.904998779296875,
|
||||
"y": 210.9040069580078
|
||||
}
|
||||
],
|
||||
"isCurve": true,
|
||||
|
|
@ -795,6 +811,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": -8.904999732971191,
|
||||
"y": 210.9040069580078
|
||||
},
|
||||
{
|
||||
"x": -19.285999298095703,
|
||||
"y": 209.53700256347656
|
||||
|
|
@ -890,6 +910,10 @@
|
|||
{
|
||||
"x": -183.62899780273438,
|
||||
"y": 53.582000732421875
|
||||
},
|
||||
{
|
||||
"x": -185.53700256347656,
|
||||
"y": 43.2859992980957
|
||||
}
|
||||
],
|
||||
"isCurve": true,
|
||||
|
|
@ -923,6 +947,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": 499.45098876953125,
|
||||
"y": -137.51199340820312
|
||||
},
|
||||
{
|
||||
"x": 513.333984375,
|
||||
"y": -136.05299377441406
|
||||
|
|
@ -1026,6 +1054,10 @@
|
|||
{
|
||||
"x": 675.7109985351562,
|
||||
"y": 123.8030014038086
|
||||
},
|
||||
{
|
||||
"x": 670.9359741210938,
|
||||
"y": 136.92100524902344
|
||||
}
|
||||
],
|
||||
"isCurve": true,
|
||||
|
|
@ -1059,6 +1091,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": 643.1019897460938,
|
||||
"y": 185.1320037841797
|
||||
},
|
||||
{
|
||||
"x": 634.1279907226562,
|
||||
"y": 195.8260040283203
|
||||
|
|
@ -1158,6 +1194,10 @@
|
|||
{
|
||||
"x": 336.8710021972656,
|
||||
"y": 195.8260040283203
|
||||
},
|
||||
{
|
||||
"x": 327.8970031738281,
|
||||
"y": 185.1320037841797
|
||||
}
|
||||
],
|
||||
"isCurve": true,
|
||||
|
|
@ -1191,6 +1231,10 @@
|
|||
"labelPercentage": 0,
|
||||
"link": "",
|
||||
"route": [
|
||||
{
|
||||
"x": 925.8150024414062,
|
||||
"y": -186.9040069580078
|
||||
},
|
||||
{
|
||||
"x": 946.4920043945312,
|
||||
"y": -183.62899780273438
|
||||
|
|
@ -1298,6 +1342,10 @@
|
|||
{
|
||||
"x": 946.4920043945312,
|
||||
"y": 207.62899780273438
|
||||
},
|
||||
{
|
||||
"x": 925.8150024414062,
|
||||
"y": 210.9040069580078
|
||||
}
|
||||
],
|
||||
"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 |
Loading…
Reference in a new issue