2025-02-21 16:34:23 +00:00
|
|
|
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 (
|
2025-02-21 17:29:24 +00:00
|
|
|
MIN_RADIUS = 200
|
|
|
|
|
PADDING = 20
|
|
|
|
|
MIN_SEGMENT_LEN = 10
|
2025-02-21 17:31:43 +00:00
|
|
|
ARC_STEPS = 30
|
2025-02-21 16:34:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) error {
|
|
|
|
|
objects := g.Root.ChildrenArray
|
|
|
|
|
if len(objects) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 16:34:23 +00:00
|
|
|
for _, obj := range g.Objects {
|
|
|
|
|
positionLabelsIcons(obj)
|
|
|
|
|
}
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 17:29:24 +00:00
|
|
|
radius := calculateRadius(objects)
|
|
|
|
|
positionObjects(objects, radius)
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 16:34:23 +00:00
|
|
|
for _, edge := range g.Edges {
|
2025-02-21 19:12:20 +00:00
|
|
|
createCircularArc(edge)
|
2025-02-21 16:34:23 +00:00
|
|
|
}
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 16:34:23 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 17:29:24 +00:00
|
|
|
func calculateRadius(objects []*d2graph.Object) float64 {
|
|
|
|
|
numObjects := float64(len(objects))
|
2025-02-21 16:34:23 +00:00
|
|
|
maxSize := 0.0
|
|
|
|
|
for _, obj := range objects {
|
2025-02-21 17:29:24 +00:00
|
|
|
size := math.Max(obj.Box.Width, obj.Box.Height)
|
2025-02-21 16:34:23 +00:00
|
|
|
maxSize = math.Max(maxSize, size)
|
|
|
|
|
}
|
2025-02-21 17:29:24 +00:00
|
|
|
minRadius := (maxSize/2.0 + PADDING) / math.Sin(math.Pi/numObjects)
|
|
|
|
|
return math.Max(minRadius, MIN_RADIUS)
|
2025-02-21 16:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func positionObjects(objects []*d2graph.Object, radius float64) {
|
|
|
|
|
numObjects := float64(len(objects))
|
2025-02-21 17:05:31 +00:00
|
|
|
angleOffset := -math.Pi / 2
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 16:34:23 +00:00
|
|
|
for i, obj := range objects {
|
2025-02-21 17:31:43 +00:00
|
|
|
angle := angleOffset + (2*math.Pi*float64(i)/numObjects)
|
2025-02-21 16:34:23 +00:00
|
|
|
x := radius * math.Cos(angle)
|
|
|
|
|
y := radius * math.Sin(angle)
|
2025-02-21 17:41:31 +00:00
|
|
|
|
|
|
|
|
obj.TopLeft = geo.NewPoint(
|
|
|
|
|
x-obj.Box.Width/2,
|
|
|
|
|
y-obj.Box.Height/2,
|
|
|
|
|
)
|
2025-02-21 16:34:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:12:20 +00:00
|
|
|
func createCircularArc(edge *d2graph.Edge) {
|
2025-02-21 17:29:24 +00:00
|
|
|
if edge.Src == nil || edge.Dst == nil {
|
2025-02-21 16:34:23 +00:00
|
|
|
return
|
|
|
|
|
}
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 16:34:23 +00:00
|
|
|
srcCenter := edge.Src.Center()
|
|
|
|
|
dstCenter := edge.Dst.Center()
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 17:29:24 +00:00
|
|
|
srcAngle := math.Atan2(srcCenter.Y, srcCenter.X)
|
|
|
|
|
dstAngle := math.Atan2(dstCenter.Y, dstCenter.X)
|
|
|
|
|
if dstAngle < srcAngle {
|
|
|
|
|
dstAngle += 2 * math.Pi
|
2025-02-21 16:34:23 +00:00
|
|
|
}
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 19:12:20 +00:00
|
|
|
arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
|
|
|
|
|
|
2025-02-21 16:34:23 +00:00
|
|
|
path := make([]*geo.Point, 0, ARC_STEPS+1)
|
|
|
|
|
for i := 0; i <= ARC_STEPS; i++ {
|
2025-02-21 17:29:24 +00:00
|
|
|
t := float64(i) / float64(ARC_STEPS)
|
|
|
|
|
angle := srcAngle + t*(dstAngle-srcAngle)
|
|
|
|
|
x := arcRadius * math.Cos(angle)
|
|
|
|
|
y := arcRadius * math.Sin(angle)
|
2025-02-21 16:34:23 +00:00
|
|
|
path = append(path, geo.NewPoint(x, y))
|
|
|
|
|
}
|
2025-02-21 17:29:24 +00:00
|
|
|
path[0] = srcCenter
|
|
|
|
|
path[len(path)-1] = dstCenter
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 17:29:24 +00:00
|
|
|
startIndex, newSrc := clampPointOutsideBox(edge.Src.Box, path, 0)
|
|
|
|
|
endIndex, newDst := clampPointOutsideBoxReverse(edge.Dst.Box, path, len(path)-1)
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 17:29:24 +00:00
|
|
|
path[0] = newSrc
|
|
|
|
|
path[len(path)-1] = newDst
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 17:29:24 +00:00
|
|
|
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]
|
|
|
|
|
}
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 17:29:24 +00:00
|
|
|
for i := startIdx + 1; i < len(path); i++ {
|
|
|
|
|
if boxContains(box, path[i]) {
|
|
|
|
|
continue
|
2025-02-21 16:34:23 +00:00
|
|
|
}
|
2025-02-21 17:41:31 +00:00
|
|
|
seg := geo.NewSegment(path[i-1], path[i])
|
|
|
|
|
inters := boxIntersections(box, *seg)
|
|
|
|
|
if len(inters) > 0 {
|
|
|
|
|
return i, inters[0]
|
|
|
|
|
}
|
|
|
|
|
return i, path[i]
|
2025-02-21 16:34:23 +00:00
|
|
|
}
|
2025-02-21 17:29:24 +00:00
|
|
|
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]
|
|
|
|
|
}
|
2025-02-21 17:41:31 +00:00
|
|
|
|
2025-02-21 17:29:24 +00:00
|
|
|
for j := endIdx - 1; j >= 0; j-- {
|
|
|
|
|
if boxContains(box, path[j]) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2025-02-21 17:41:31 +00:00
|
|
|
seg := geo.NewSegment(path[j], path[j+1])
|
|
|
|
|
inters := boxIntersections(box, *seg)
|
|
|
|
|
if len(inters) > 0 {
|
|
|
|
|
return j, inters[0]
|
|
|
|
|
}
|
|
|
|
|
return j, path[j]
|
2025-02-21 17:29:24 +00:00
|
|
|
}
|
|
|
|
|
return 0, path[0]
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 17:41:31 +00:00
|
|
|
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())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|