d2/d2layouts/d2sequence/layout.go

284 lines
8.4 KiB
Go
Raw Normal View History

package d2sequence
import (
"context"
"fmt"
"math"
2022-11-29 03:20:53 +00:00
"sort"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/go2"
"oss.terrastruct.com/d2/lib/label"
2022-11-29 18:11:42 +00:00
"oss.terrastruct.com/d2/lib/shape"
)
func Layout(ctx context.Context, g *d2graph.Graph) (err error) {
sd := &sequenceDiagram{
graph: g,
2022-11-29 01:06:37 +00:00
objectRank: make(map[*d2graph.Object]int),
2022-11-29 23:40:43 +00:00
objectLevel: make(map[*d2graph.Object]int),
2022-11-29 01:06:37 +00:00
minEdgeRank: make(map[*d2graph.Object]int),
maxEdgeRank: make(map[*d2graph.Object]int),
edgeYStep: MIN_EDGE_DISTANCE,
actorXStep: MIN_ACTOR_DISTANCE,
maxActorHeight: 0.,
}
2022-11-29 01:06:37 +00:00
sd.init()
sd.placeActors()
2022-11-29 23:40:43 +00:00
sd.placeSpans()
2022-11-29 01:06:37 +00:00
sd.routeEdges()
2022-11-29 02:23:51 +00:00
sd.addLifelineEdges()
return nil
}
type sequenceDiagram struct {
graph *d2graph.Graph
2022-11-29 23:17:47 +00:00
edges []*d2graph.Edge
actors []*d2graph.Object
spans []*d2graph.Object
2022-11-29 01:06:37 +00:00
2022-11-29 23:40:43 +00:00
// can be either actors or spans
// rank: left to right position of actors/spans (spans have the same rank as their parents)
2022-11-29 23:14:36 +00:00
objectRank map[*d2graph.Object]int
2022-11-29 23:40:43 +00:00
// similar to d2graph.Object.Level() just don't make the recursive calls
objectLevel map[*d2graph.Object]int
2022-11-29 01:06:37 +00:00
// keep track of the first and last edge of a given actor
2022-11-29 23:14:36 +00:00
// the edge rank is the order in which it appears from top to bottom
2022-11-29 01:06:37 +00:00
minEdgeRank map[*d2graph.Object]int
maxEdgeRank map[*d2graph.Object]int
edgeYStep float64
actorXStep float64
maxActorHeight float64
}
2022-11-29 01:06:37 +00:00
func (sd *sequenceDiagram) init() {
sd.edges = make([]*d2graph.Edge, len(sd.graph.Edges))
copy(sd.edges, sd.graph.Edges)
2022-11-29 03:20:53 +00:00
queue := make([]*d2graph.Object, len(sd.graph.Root.ChildrenArray))
copy(queue, sd.graph.Root.ChildrenArray)
for len(queue) > 0 {
obj := queue[0]
queue = queue[1:]
2022-11-29 18:11:42 +00:00
if sd.isActor(obj) {
2022-11-29 01:06:37 +00:00
sd.actors = append(sd.actors, obj)
2022-11-29 03:20:53 +00:00
sd.objectRank[obj] = len(sd.actors)
2022-11-29 23:40:43 +00:00
sd.objectLevel[obj] = 0
2022-11-29 23:14:36 +00:00
sd.maxActorHeight = math.Max(sd.maxActorHeight, obj.Height)
2022-11-29 18:11:42 +00:00
} else {
2022-11-29 23:40:43 +00:00
// spans are always rectangles and have no labels
2022-11-29 03:20:53 +00:00
obj.Attributes.Label = d2graph.Scalar{Value: ""}
2022-11-29 18:11:42 +00:00
obj.Attributes.Shape = d2graph.Scalar{Value: shape.SQUARE_TYPE}
2022-11-29 23:17:47 +00:00
sd.spans = append(sd.spans, obj)
2022-11-29 03:20:53 +00:00
sd.objectRank[obj] = sd.objectRank[obj.Parent]
2022-11-29 23:40:43 +00:00
sd.objectLevel[obj] = sd.objectLevel[obj.Parent] + 1
2022-11-29 01:06:37 +00:00
}
2022-11-29 03:20:53 +00:00
queue = append(queue, obj.ChildrenArray...)
2022-11-29 01:06:37 +00:00
}
2022-11-29 03:20:53 +00:00
2022-11-29 01:06:37 +00:00
for rank, edge := range sd.edges {
2022-11-29 23:14:36 +00:00
sd.edgeYStep = math.Max(sd.edgeYStep, float64(edge.LabelDimensions.Height))
2022-11-29 01:06:37 +00:00
sd.setMinMaxEdgeRank(edge.Src, rank)
sd.setMinMaxEdgeRank(edge.Dst, rank)
// ensures that long labels, spanning over multiple actors, don't make for large gaps between actors
// by distributing the label length across the actors rank difference
rankDiff := math.Abs(float64(sd.objectRank[edge.Src]) - float64(sd.objectRank[edge.Dst]))
distributedLabelWidth := float64(edge.LabelDimensions.Width) / rankDiff
sd.actorXStep = math.Max(sd.actorXStep, distributedLabelWidth+HORIZONTAL_PAD)
}
2022-11-29 23:14:36 +00:00
sd.maxActorHeight += VERTICAL_PAD
sd.edgeYStep += VERTICAL_PAD
2022-11-29 01:06:37 +00:00
}
func (sd *sequenceDiagram) setMinMaxEdgeRank(actor *d2graph.Object, rank int) {
if minRank, exists := sd.minEdgeRank[actor]; exists {
2022-11-29 18:11:42 +00:00
sd.minEdgeRank[actor] = go2.IntMin(minRank, rank)
2022-11-29 01:06:37 +00:00
} else {
sd.minEdgeRank[actor] = rank
}
2022-11-29 18:11:42 +00:00
sd.maxEdgeRank[actor] = go2.IntMax(sd.maxEdgeRank[actor], rank)
2022-11-29 01:06:37 +00:00
}
2022-11-28 19:11:38 +00:00
// placeActors places actors bottom aligned, side by side
func (sd *sequenceDiagram) placeActors() {
x := 0.
2022-11-29 01:06:37 +00:00
for _, actors := range sd.actors {
yOffset := sd.maxActorHeight - actors.Height
2022-11-28 19:11:38 +00:00
actors.TopLeft = geo.NewPoint(x, yOffset)
x += actors.Width + sd.actorXStep
2022-11-28 19:11:38 +00:00
actors.LabelPosition = go2.Pointer(string(label.InsideMiddleCenter))
}
}
2022-11-29 18:08:11 +00:00
// addLifelineEdges adds a new edge for each actor in the graph that represents the its lifeline
2022-11-26 06:41:11 +00:00
// ┌──────────────┐
2022-11-28 19:11:38 +00:00
// │ actor │
2022-11-26 06:41:11 +00:00
// └──────┬───────┘
// │
// │ lifeline
// │
// │
func (sd *sequenceDiagram) addLifelineEdges() {
2022-11-29 01:06:37 +00:00
endY := sd.getEdgeY(len(sd.edges))
for _, actor := range sd.actors {
2022-11-28 19:11:38 +00:00
actorBottom := actor.Center()
actorBottom.Y = actor.TopLeft.Y + actor.Height
actorLifelineEnd := actor.Center()
actorLifelineEnd.Y = endY
sd.graph.Edges = append(sd.graph.Edges, &d2graph.Edge{
Attributes: d2graph.Attributes{
Style: d2graph.Style{
2022-11-28 19:32:56 +00:00
StrokeDash: &d2graph.Scalar{Value: "10"},
2022-11-28 19:11:38 +00:00
Stroke: actor.Attributes.Style.Stroke,
StrokeWidth: actor.Attributes.Style.StrokeWidth,
},
},
2022-11-28 19:11:38 +00:00
Src: actor,
SrcArrow: false,
Dst: &d2graph.Object{
2022-11-28 19:11:38 +00:00
ID: actor.ID + fmt.Sprintf("-lifeline-end-%d", go2.StringToIntHash(actor.ID+"-lifeline-end")),
},
DstArrow: false,
2022-11-28 19:11:38 +00:00
Route: []*geo.Point{actorBottom, actorLifelineEnd},
})
}
}
2022-11-29 01:06:37 +00:00
2022-11-29 23:40:43 +00:00
// placeSpans places spans over the object lifeline
2022-11-29 02:23:51 +00:00
// ┌──────────┐
// │ actor │
// └────┬─────┘
// ┌─┴──┐
// │ │
2022-11-29 23:17:47 +00:00
// |span|
2022-11-29 02:23:51 +00:00
// │ │
// └─┬──┘
// │
// lifeline
// │
2022-11-29 23:40:43 +00:00
func (sd *sequenceDiagram) placeSpans() {
// quickly find the span center X
2022-11-29 01:06:37 +00:00
rankToX := make(map[int]float64)
for _, actor := range sd.actors {
rankToX[sd.objectRank[actor]] = actor.Center().X
}
2022-11-29 23:40:43 +00:00
// places spans from most to least nested
// the order is important because the only way a child span exists is if there'e an edge to it
2022-11-29 23:17:47 +00:00
// however, the parent span might not have an edge to it and then its position is based on the child position
2022-11-29 18:29:15 +00:00
// or, there can be edge to it, but it comes after the child one meaning the top left position is still based on the child
// and not on its own edge
2022-11-29 23:17:47 +00:00
spanFromMostNested := make([]*d2graph.Object, len(sd.spans))
copy(spanFromMostNested, sd.spans)
sort.SliceStable(spanFromMostNested, func(i, j int) bool {
2022-11-29 23:40:43 +00:00
return sd.objectLevel[spanFromMostNested[i]] > sd.objectLevel[spanFromMostNested[j]]
2022-11-29 03:20:53 +00:00
})
2022-11-29 23:17:47 +00:00
for _, span := range spanFromMostNested {
2022-11-29 18:29:15 +00:00
// finds the position based on children
2022-11-29 03:20:53 +00:00
minChildY := math.Inf(1)
maxChildY := math.Inf(-1)
2022-11-29 23:17:47 +00:00
for _, child := range span.ChildrenArray {
2022-11-29 03:20:53 +00:00
minChildY = math.Min(minChildY, child.TopLeft.Y)
maxChildY = math.Max(maxChildY, child.TopLeft.Y+child.Height)
}
2022-11-29 23:40:43 +00:00
// finds the position if there are edges to this span
2022-11-29 03:20:53 +00:00
minEdgeY := math.Inf(1)
2022-11-29 23:17:47 +00:00
if minRank, exists := sd.minEdgeRank[span]; exists {
2022-11-29 03:20:53 +00:00
minEdgeY = sd.getEdgeY(minRank)
}
maxEdgeY := math.Inf(-1)
2022-11-29 23:17:47 +00:00
if maxRank, exists := sd.maxEdgeRank[span]; exists {
2022-11-29 03:20:53 +00:00
maxEdgeY = sd.getEdgeY(maxRank)
}
2022-11-29 18:29:15 +00:00
// if it is the same as the child top left, add some padding
2022-11-29 03:20:53 +00:00
minY := math.Min(minEdgeY, minChildY)
if minY == minChildY {
2022-11-30 00:16:41 +00:00
minY -= SPAN_DEPTH_GROW_FACTOR
2022-11-29 20:57:07 +00:00
} else {
2022-11-29 23:40:43 +00:00
minY -= SPAN_EDGE_PAD
2022-11-29 03:20:53 +00:00
}
maxY := math.Max(maxEdgeY, maxChildY)
if maxY == maxChildY {
2022-11-30 00:16:41 +00:00
maxY += SPAN_DEPTH_GROW_FACTOR
2022-11-29 20:57:07 +00:00
} else {
2022-11-29 23:40:43 +00:00
maxY += SPAN_EDGE_PAD
2022-11-29 03:20:53 +00:00
}
2022-11-29 23:40:43 +00:00
height := math.Max(maxY-minY, MIN_SPAN_HEIGHT)
2022-11-30 00:16:41 +00:00
width := SPAN_WIDTH + (float64(sd.objectLevel[span]-1) * SPAN_DEPTH_GROW_FACTOR)
2022-11-29 23:17:47 +00:00
x := rankToX[sd.objectRank[span]] - (width / 2.)
span.Box = geo.NewBox(geo.NewPoint(x, minY), width, height)
2022-11-29 01:06:37 +00:00
}
}
// routeEdges routes horizontal edges from Src to Dst
func (sd *sequenceDiagram) routeEdges() {
for rank, edge := range sd.edges {
2022-11-29 02:23:51 +00:00
isLeftToRight := edge.Src.TopLeft.X < edge.Dst.TopLeft.X
2022-11-29 18:29:15 +00:00
// finds the proper anchor point based on the edge direction
2022-11-29 02:23:51 +00:00
var startX, endX float64
if sd.isActor(edge.Src) {
startX = edge.Src.Center().X
} else if isLeftToRight {
startX = edge.Src.TopLeft.X + edge.Src.Width
} else {
startX = edge.Src.TopLeft.X
}
if sd.isActor(edge.Dst) {
endX = edge.Dst.Center().X
} else if isLeftToRight {
endX = edge.Dst.TopLeft.X
} else {
endX = edge.Dst.TopLeft.X + edge.Dst.Width
}
2022-11-29 19:53:30 +00:00
if isLeftToRight {
2022-11-29 23:40:43 +00:00
startX += SPAN_EDGE_PAD
endX -= SPAN_EDGE_PAD
2022-11-29 19:53:30 +00:00
} else {
2022-11-29 23:40:43 +00:00
startX -= SPAN_EDGE_PAD
endX += SPAN_EDGE_PAD
2022-11-29 19:53:30 +00:00
}
2022-11-29 02:23:51 +00:00
edgeY := sd.getEdgeY(rank)
edge.Route = []*geo.Point{
geo.NewPoint(startX, edgeY),
geo.NewPoint(endX, edgeY),
}
2022-11-29 01:06:37 +00:00
if edge.Attributes.Label.Value != "" {
if isLeftToRight {
edge.LabelPosition = go2.Pointer(string(label.OutsideTopCenter))
} else {
2022-11-29 18:29:15 +00:00
// the label will be placed above the edge because the orientation is based on the edge normal vector
2022-11-29 01:06:37 +00:00
edge.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter))
}
}
}
}
func (sd *sequenceDiagram) getEdgeY(rank int) float64 {
2022-11-29 02:23:51 +00:00
// +1 so that the first edge has the top padding for its label
2022-11-29 01:06:37 +00:00
return ((float64(rank) + 1.) * sd.edgeYStep) + sd.maxActorHeight
}
2022-11-29 02:23:51 +00:00
func (sd *sequenceDiagram) isActor(obj *d2graph.Object) bool {
return obj.Parent == sd.graph.Root
}