iteration 7
This commit is contained in:
parent
9daef7f890
commit
77f88df483
1 changed files with 172 additions and 362 deletions
|
|
@ -1,377 +1,199 @@
|
||||||
// package d2cycle
|
package d2cycle
|
||||||
|
|
||||||
// import (
|
import (
|
||||||
// "context"
|
"context"
|
||||||
// "math"
|
"math"
|
||||||
|
|
||||||
// "oss.terrastruct.com/d2/d2graph"
|
"oss.terrastruct.com/d2/d2graph"
|
||||||
// "oss.terrastruct.com/d2/lib/geo"
|
"oss.terrastruct.com/d2/lib/geo"
|
||||||
// "oss.terrastruct.com/d2/lib/label"
|
"oss.terrastruct.com/d2/lib/label"
|
||||||
// "oss.terrastruct.com/util-go/go2"
|
"oss.terrastruct.com/util-go/go2"
|
||||||
// )
|
)
|
||||||
|
|
||||||
// const (
|
const (
|
||||||
// MIN_RADIUS = 200
|
MIN_RADIUS = 200
|
||||||
// PADDING = 20
|
PADDING = 20
|
||||||
// MIN_SEGMENT_LEN = 10
|
MIN_SEGMENT_LEN = 10
|
||||||
// ARC_STEPS = 30
|
ARC_STEPS = 30
|
||||||
// )
|
)
|
||||||
|
|
||||||
// func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) error {
|
func Layout(ctx context.Context, g *d2graph.Graph, layout d2graph.LayoutGraph) error {
|
||||||
// objects := g.Root.ChildrenArray
|
objects := g.Root.ChildrenArray
|
||||||
// if len(objects) == 0 {
|
if len(objects) == 0 {
|
||||||
// return nil
|
return nil
|
||||||
// }
|
}
|
||||||
|
|
||||||
// for _, obj := range g.Objects {
|
for _, obj := range g.Objects {
|
||||||
// positionLabelsIcons(obj)
|
positionLabelsIcons(obj)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// radius := calculateRadius(objects)
|
radius := calculateRadius(objects)
|
||||||
// positionObjects(objects, radius)
|
positionObjects(objects, radius)
|
||||||
|
|
||||||
// for _, edge := range g.Edges {
|
for _, edge := range g.Edges {
|
||||||
// createCircularArc(edge)
|
createCircularArc(edge)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// return nil
|
return nil
|
||||||
// }
|
}
|
||||||
|
|
||||||
// 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
|
||||||
// for _, obj := range objects {
|
for _, obj := range objects {
|
||||||
// size := math.Max(obj.Box.Width, obj.Box.Height)
|
size := math.Max(obj.Box.Width, obj.Box.Height)
|
||||||
// maxSize = math.Max(maxSize, size)
|
maxSize = math.Max(maxSize, size)
|
||||||
// }
|
}
|
||||||
// minRadius := (maxSize/2.0 + PADDING) / math.Sin(math.Pi/numObjects)
|
minRadius := (maxSize/2.0 + PADDING) / math.Sin(math.Pi/numObjects)
|
||||||
// return math.Max(minRadius, MIN_RADIUS)
|
return math.Max(minRadius, MIN_RADIUS)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
// 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)
|
||||||
// x := radius * math.Cos(angle)
|
x := radius * math.Cos(angle)
|
||||||
// y := radius * math.Sin(angle)
|
y := radius * math.Sin(angle)
|
||||||
|
|
||||||
// obj.TopLeft = geo.NewPoint(
|
obj.TopLeft = geo.NewPoint(
|
||||||
// x-obj.Box.Width/2,
|
x-obj.Box.Width/2,
|
||||||
// y-obj.Box.Height/2,
|
y-obj.Box.Height/2,
|
||||||
// )
|
)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// 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
|
||||||
// }
|
}
|
||||||
|
|
||||||
// srcCenter := edge.Src.Center()
|
srcCenter := edge.Src.Center()
|
||||||
// dstCenter := edge.Dst.Center()
|
dstCenter := edge.Dst.Center()
|
||||||
|
|
||||||
// srcAngle := math.Atan2(srcCenter.Y, srcCenter.X)
|
srcAngle := math.Atan2(srcCenter.Y, srcCenter.X)
|
||||||
// dstAngle := math.Atan2(dstCenter.Y, dstCenter.X)
|
dstAngle := math.Atan2(dstCenter.Y, dstCenter.X)
|
||||||
// if dstAngle < srcAngle {
|
if dstAngle < srcAngle {
|
||||||
// dstAngle += 2 * math.Pi
|
dstAngle += 2 * math.Pi
|
||||||
// }
|
}
|
||||||
|
|
||||||
// arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
|
arcRadius := math.Hypot(srcCenter.X, srcCenter.Y)
|
||||||
|
|
||||||
// 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)
|
||||||
// angle := srcAngle + t*(dstAngle-srcAngle)
|
angle := srcAngle + t*(dstAngle-srcAngle)
|
||||||
// x := arcRadius * math.Cos(angle)
|
x := arcRadius * math.Cos(angle)
|
||||||
// y := arcRadius * math.Sin(angle)
|
y := arcRadius * math.Sin(angle)
|
||||||
// path = append(path, geo.NewPoint(x, y))
|
path = append(path, geo.NewPoint(x, y))
|
||||||
// }
|
}
|
||||||
// path[0] = srcCenter
|
path[0] = srcCenter
|
||||||
// path[len(path)-1] = dstCenter
|
path[len(path)-1] = dstCenter
|
||||||
|
|
||||||
// startIndex, newSrc := clampPointOutsideBox(edge.Src.Box, path, 0)
|
startIndex, newSrc := clampPointOutsideBox(edge.Src.Box, path, 0)
|
||||||
// endIndex, newDst := clampPointOutsideBoxReverse(edge.Dst.Box, path, len(path)-1)
|
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[startIndex : endIndex+1]
|
edge.Route = path[startIndex : endIndex+1]
|
||||||
// edge.IsCurve = true
|
edge.IsCurve = true
|
||||||
// }
|
}
|
||||||
|
|
||||||
// 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]
|
||||||
// }
|
}
|
||||||
// if !boxContains(box, path[startIdx]) {
|
if !boxContains(box, path[startIdx]) {
|
||||||
// return startIdx, path[startIdx]
|
return startIdx, path[startIdx]
|
||||||
// }
|
}
|
||||||
|
|
||||||
// for i := startIdx + 1; i < len(path); i++ {
|
for i := startIdx + 1; i < len(path); i++ {
|
||||||
// if boxContains(box, path[i]) {
|
if boxContains(box, path[i]) {
|
||||||
// continue
|
continue
|
||||||
// }
|
}
|
||||||
// seg := geo.NewSegment(path[i-1], path[i])
|
seg := geo.NewSegment(path[i-1], path[i])
|
||||||
// inters := boxIntersections(box, *seg)
|
inters := boxIntersections(box, *seg)
|
||||||
// if len(inters) > 0 {
|
if len(inters) > 0 {
|
||||||
// return i, inters[0]
|
return i, inters[0]
|
||||||
// }
|
}
|
||||||
// return i, path[i]
|
return i, path[i]
|
||||||
// }
|
}
|
||||||
// last := len(path) - 1
|
last := len(path) - 1
|
||||||
// return last, path[last]
|
return last, path[last]
|
||||||
// }
|
}
|
||||||
|
|
||||||
// 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]
|
||||||
// }
|
}
|
||||||
// if !boxContains(box, path[endIdx]) {
|
if !boxContains(box, path[endIdx]) {
|
||||||
// return endIdx, path[endIdx]
|
return endIdx, path[endIdx]
|
||||||
// }
|
}
|
||||||
|
|
||||||
// for j := endIdx - 1; j >= 0; j-- {
|
for j := endIdx - 1; j >= 0; j-- {
|
||||||
// if boxContains(box, path[j]) {
|
if boxContains(box, path[j]) {
|
||||||
// continue
|
continue
|
||||||
// }
|
}
|
||||||
// seg := geo.NewSegment(path[j], path[j+1])
|
seg := geo.NewSegment(path[j], path[j+1])
|
||||||
// inters := boxIntersections(box, *seg)
|
inters := boxIntersections(box, *seg)
|
||||||
// if len(inters) > 0 {
|
if len(inters) > 0 {
|
||||||
// return j, inters[0]
|
return j, inters[0]
|
||||||
// }
|
}
|
||||||
// return j, path[j]
|
return j, path[j]
|
||||||
// }
|
}
|
||||||
// return 0, path[0]
|
return 0, path[0]
|
||||||
// }
|
}
|
||||||
|
|
||||||
// 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 &&
|
||||||
// p.Y >= b.TopLeft.Y &&
|
p.Y >= b.TopLeft.Y &&
|
||||||
// p.Y <= b.TopLeft.Y+b.Height
|
p.Y <= b.TopLeft.Y+b.Height
|
||||||
// }
|
}
|
||||||
|
|
||||||
// func boxIntersections(b *geo.Box, seg geo.Segment) []*geo.Point {
|
func boxIntersections(b *geo.Box, seg geo.Segment) []*geo.Point {
|
||||||
// return b.Intersections(seg)
|
return b.Intersections(seg)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
// obj.IconPosition = go2.Pointer(label.OutsideTopLeft.String())
|
obj.IconPosition = go2.Pointer(label.OutsideTopLeft.String())
|
||||||
// if obj.LabelPosition == nil {
|
if obj.LabelPosition == nil {
|
||||||
// obj.LabelPosition = go2.Pointer(label.OutsideTopRight.String())
|
obj.LabelPosition = go2.Pointer(label.OutsideTopRight.String())
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
// } else if obj.SQLTable != nil || obj.Class != nil || obj.Language != "" {
|
} else if obj.SQLTable != nil || obj.Class != nil || obj.Language != "" {
|
||||||
// obj.IconPosition = go2.Pointer(label.OutsideTopLeft.String())
|
obj.IconPosition = go2.Pointer(label.OutsideTopLeft.String())
|
||||||
// } else {
|
} else {
|
||||||
// obj.IconPosition = go2.Pointer(label.InsideMiddleCenter.String())
|
obj.IconPosition = go2.Pointer(label.InsideMiddleCenter.String())
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if obj.HasLabel() && obj.LabelPosition == nil {
|
if obj.HasLabel() && obj.LabelPosition == nil {
|
||||||
// if len(obj.ChildrenArray) > 0 {
|
if len(obj.ChildrenArray) > 0 {
|
||||||
// obj.LabelPosition = go2.Pointer(label.OutsideTopCenter.String())
|
obj.LabelPosition = go2.Pointer(label.OutsideTopCenter.String())
|
||||||
// } else if obj.HasOutsideBottomLabel() {
|
} else if obj.HasOutsideBottomLabel() {
|
||||||
// obj.LabelPosition = go2.Pointer(label.OutsideBottomCenter.String())
|
obj.LabelPosition = go2.Pointer(label.OutsideBottomCenter.String())
|
||||||
// } else if obj.Icon != nil {
|
} else if obj.Icon != nil {
|
||||||
// obj.LabelPosition = go2.Pointer(label.InsideTopCenter.String())
|
obj.LabelPosition = go2.Pointer(label.InsideTopCenter.String())
|
||||||
// } else {
|
} else {
|
||||||
// obj.LabelPosition = go2.Pointer(label.InsideMiddleCenter.String())
|
obj.LabelPosition = go2.Pointer(label.InsideMiddleCenter.String())
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if float64(obj.LabelDimensions.Width) > obj.Width ||
|
if float64(obj.LabelDimensions.Width) > obj.Width ||
|
||||||
// float64(obj.LabelDimensions.Height) > obj.Height {
|
float64(obj.LabelDimensions.Height) > obj.Height {
|
||||||
// if len(obj.ChildrenArray) > 0 {
|
if len(obj.ChildrenArray) > 0 {
|
||||||
// obj.LabelPosition = go2.Pointer(label.OutsideTopCenter.String())
|
obj.LabelPosition = go2.Pointer(label.OutsideTopCenter.String())
|
||||||
// } else {
|
} else {
|
||||||
// obj.LabelPosition = go2.Pointer(label.OutsideBottomCenter.String())
|
obj.LabelPosition = go2.Pointer(label.OutsideBottomCenter.String())
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// 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
|
|
||||||
// }
|
|
||||||
// intersection := refineIntersection(box, path[i-1], path[i])
|
|
||||||
// return i, intersection
|
|
||||||
// }
|
|
||||||
// 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
|
|
||||||
// }
|
|
||||||
// intersection := refineIntersection(box, path[j], path[j+1])
|
|
||||||
// return j, intersection
|
|
||||||
// }
|
|
||||||
// return 0, path[0]
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func refineIntersection(box *geo.Box, pInside, pOutside *geo.Point) *geo.Point {
|
|
||||||
// const epsilon = 0.001
|
|
||||||
// a := pInside
|
|
||||||
// b := pOutside
|
|
||||||
// for math.Hypot(b.X-a.X, b.Y-a.Y) > epsilon {
|
|
||||||
// mid := geo.NewPoint((a.X+b.X)/2, (a.Y+b.Y)/2)
|
|
||||||
// if boxContains(box, mid) {
|
|
||||||
// a = mid
|
|
||||||
// } else {
|
|
||||||
// b = mid
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return geo.NewPoint((a.X+b.X)/2, (a.Y+b.Y)/2)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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 (
|
||||||
|
|
@ -470,7 +292,7 @@ func clampPointOutsideBox(box *geo.Box, path []*geo.Point, startIdx int) (int, *
|
||||||
if boxContains(box, path[i]) {
|
if boxContains(box, path[i]) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
intersection := getIntersectionPoint(box, path[i-1], path[i])
|
intersection := refineIntersection(box, path[i-1], path[i])
|
||||||
return i, intersection
|
return i, intersection
|
||||||
}
|
}
|
||||||
last := len(path) - 1
|
last := len(path) - 1
|
||||||
|
|
@ -488,23 +310,14 @@ func clampPointOutsideBoxReverse(box *geo.Box, path []*geo.Point, endIdx int) (i
|
||||||
if boxContains(box, path[j]) {
|
if boxContains(box, path[j]) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
intersection := getIntersectionPoint(box, path[j], path[j+1])
|
intersection := refineIntersection(box, path[j], path[j+1])
|
||||||
return j, intersection
|
return j, intersection
|
||||||
}
|
}
|
||||||
return 0, path[0]
|
return 0, path[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
func getIntersectionPoint(box *geo.Box, pInside, pOutside *geo.Point) *geo.Point {
|
|
||||||
seg := geo.NewSegment(pInside, pOutside)
|
|
||||||
inters := boxIntersections(box, *seg)
|
|
||||||
if len(inters) > 0 {
|
|
||||||
return inters[0]
|
|
||||||
}
|
|
||||||
return refineIntersection(box, pInside, pOutside)
|
|
||||||
}
|
|
||||||
|
|
||||||
func refineIntersection(box *geo.Box, pInside, pOutside *geo.Point) *geo.Point {
|
func refineIntersection(box *geo.Box, pInside, pOutside *geo.Point) *geo.Point {
|
||||||
const epsilon = 1e-6
|
const epsilon = 0.001
|
||||||
a := pInside
|
a := pInside
|
||||||
b := pOutside
|
b := pOutside
|
||||||
for math.Hypot(b.X-a.X, b.Y-a.Y) > epsilon {
|
for math.Hypot(b.X-a.X, b.Y-a.Y) > epsilon {
|
||||||
|
|
@ -519,10 +332,7 @@ func refineIntersection(box *geo.Box, pInside, pOutside *geo.Point) *geo.Point {
|
||||||
}
|
}
|
||||||
|
|
||||||
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.Y >= b.TopLeft.Y && p.Y <= b.TopLeft.Y+b.Height
|
||||||
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 {
|
func boxIntersections(b *geo.Box, seg geo.Segment) []*geo.Point {
|
||||||
|
|
@ -553,8 +363,7 @@ func positionLabelsIcons(obj *d2graph.Object) {
|
||||||
} else {
|
} else {
|
||||||
obj.LabelPosition = go2.Pointer(label.InsideMiddleCenter.String())
|
obj.LabelPosition = go2.Pointer(label.InsideMiddleCenter.String())
|
||||||
}
|
}
|
||||||
if float64(obj.LabelDimensions.Width) > obj.Width ||
|
if float64(obj.LabelDimensions.Width) > obj.Width || float64(obj.LabelDimensions.Height) > obj.Height {
|
||||||
float64(obj.LabelDimensions.Height) > obj.Height {
|
|
||||||
if len(obj.ChildrenArray) > 0 {
|
if len(obj.ChildrenArray) > 0 {
|
||||||
obj.LabelPosition = go2.Pointer(label.OutsideTopCenter.String())
|
obj.LabelPosition = go2.Pointer(label.OutsideTopCenter.String())
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -564,3 +373,4 @@ func positionLabelsIcons(obj *d2graph.Object) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue