Skip to content

packages/core/src/features/viz/lib/graph/routes.ts

Read as Markdown

This is the source snapshot used to build these API details. View this revision on GitHub.

Back to reference

1 import { PathGeometry, toSvgPathData } from "@pibbl/core";
2 import type { GraphRoute, Point } from "./types.js";
3 type Endpoints = Readonly<{ sourceAnchor: Point; targetAnchor: Point }>;
4 function route(
5   endpoints: Endpoints,
6   segments: GraphRoute["segments"],
7   arrowLength = 0,
8 ): GraphRoute {
9   const { sourceAnchor: a, targetAnchor: b } = endpoints;
10   if ((!Number.isFinite(a.x) || !Number.isFinite(a.y)) || (!Number.isFinite(b.x) || !Number.isFinite(b.y)))
11         throw new RangeError("Route anchors must be finite.");
12   if (!Number.isFinite(arrowLength) || arrowLength < 0)
13     throw new RangeError("arrowLength must be finite and nonnegative.");
14   const angle = Math.atan2(b.y - a.y, b.x - a.x),
15     c = Math.cos(angle),
16     s = Math.sin(angle);
17   return {
18     ...endpoints,
19     segments,
20     arrowSegments:
21       arrowLength === 0
22         ? null
23         : [
24             { type: "move", x: b.x, y: b.y },
25             {
26               type: "line",
27               x: b.x - arrowLength * c - (arrowLength * s) / 2,
28               y: b.y - arrowLength * s + (arrowLength * c) / 2,
29             },
30             {
31               type: "line",
32               x: b.x - arrowLength * c + (arrowLength * s) / 2,
33               y: b.y - arrowLength * s - (arrowLength * c) / 2,
34             },
35             { type: "close" },
36           ],
37   };
38 }
39 /**
40  * Build an independent straight graph route in local logical units.
41  * @param endpoints - Finite named source and target anchor points. See {@link GraphRoute}.
42  * @param options - Optional nonnegative arrowLength (default zero).
43  * @returns Named anchors and independently editable core path segments. See {@link GraphRoute}.
44  */
45 export function straightGraphRoute(
46   endpoints: Endpoints,
47   options: Readonly<{ arrowLength?: number }> = {},
48 ): GraphRoute {
49   const { sourceAnchor: a, targetAnchor: b } = endpoints;
50   return route(
51     endpoints,
52     [
53       { type: "move", x: a.x, y: a.y },
54       { type: "line", x: b.x, y: b.y },
55     ],
56     options.arrowLength,
57   );
58 }
59 /**
60  * Build an independent horizontal orthogonal graph route in local logical units.
61  * @param endpoints - Finite named source and target anchor points. See {@link GraphRoute}.
62  * @param options - Optional nonnegative arrowLength (default zero).
63  * @returns Named anchors and independently editable core path segments. See {@link GraphRoute}.
64  */
65 export function orthogonalGraphRoute(
66   endpoints: Endpoints,
67   options: Readonly<{ arrowLength?: number }> = {},
68 ): GraphRoute {
69   const { sourceAnchor: a, targetAnchor: b } = endpoints,
70     middle = (a.x + b.x) / 2;
71   const result = route(
72     endpoints,
73     [
74       { type: "move", x: a.x, y: a.y },
75       { type: "line", x: middle, y: a.y },
76       { type: "line", x: middle, y: b.y },
77       { type: "line", x: b.x, y: b.y },
78     ],
79     options.arrowLength,
80   );
81   if (options.arrowLength)
82     return {
83       ...result,
84       arrowSegments: straightGraphRoute(
85         { sourceAnchor: { x: middle, y: b.y }, targetAnchor: b },
86         options,
87       ).arrowSegments,
88     };
89   return result;
90 }
91 /**
92  * Build an independent horizontal cubic graph route in local logical units.
93  * @param endpoints - Finite named source and target anchor points. See {@link GraphRoute}.
94  * @param options - Optional nonnegative arrowLength (default zero); curvature defaults to 0.5 and must be in [0, 1].
95  * @returns Named anchors and independently editable core path segments. See {@link GraphRoute}.
96  */
97 export function curvedGraphRoute(
98   endpoints: Endpoints,
99   options: Readonly<{ curvature?: number; arrowLength?: number }> = {},
100 ): GraphRoute {
101   const { sourceAnchor: a, targetAnchor: b } = endpoints,
102     curvature = options.curvature ?? 0.5;
103   if (!Number.isFinite(curvature) || curvature < 0 || curvature > 1)
104     throw new RangeError("curvature must be between 0 and 1.");
105   const dx = (b.x - a.x) * curvature;
106   const result = route(
107     endpoints,
108     [
109       { type: "move", x: a.x, y: a.y },
110       {
111         type: "cubic",
112         cp1x: a.x + dx,
113         cp1y: a.y,
114         cp2x: b.x - dx,
115         cp2y: b.y,
116         x: b.x,
117         y: b.y,
118       },
119     ],
120     options.arrowLength,
121   );
122   return options.arrowLength && dx !== 0
123     ? {
124         ...result,
125         arrowSegments: straightGraphRoute(
126           { sourceAnchor: { x: b.x - dx, y: b.y }, targetAnchor: b },
127           options,
128         ).arrowSegments,
129       }
130     : result;
131 }
132 export function routePath(segments: GraphRoute["segments"]): string {
133   const path = new PathGeometry();
134   path.spliceSegments(0, 0, segments);
135   return toSvgPathData(path);
136 }
137 

Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.