@vielzeug/wire 0.0.0-stage → 26.11.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -2
  3. package/dist/_format.cjs +2 -0
  4. package/dist/_format.cjs.map +1 -0
  5. package/dist/_format.d.ts +3 -0
  6. package/dist/_format.d.ts.map +1 -0
  7. package/dist/_format.js +8 -0
  8. package/dist/_format.js.map +1 -0
  9. package/dist/_heap.cjs +2 -0
  10. package/dist/_heap.cjs.map +1 -0
  11. package/dist/_heap.d.ts +12 -0
  12. package/dist/_heap.d.ts.map +1 -0
  13. package/dist/_heap.js +37 -0
  14. package/dist/_heap.js.map +1 -0
  15. package/dist/_sides.cjs +2 -0
  16. package/dist/_sides.cjs.map +1 -0
  17. package/dist/_sides.d.ts +20 -0
  18. package/dist/_sides.d.ts.map +1 -0
  19. package/dist/_sides.js +55 -0
  20. package/dist/_sides.js.map +1 -0
  21. package/dist/bezier.cjs +2 -0
  22. package/dist/bezier.cjs.map +1 -0
  23. package/dist/bezier.d.ts +24 -0
  24. package/dist/bezier.d.ts.map +1 -0
  25. package/dist/bezier.js +34 -0
  26. package/dist/bezier.js.map +1 -0
  27. package/dist/edit.cjs +2 -0
  28. package/dist/edit.cjs.map +1 -0
  29. package/dist/edit.d.ts +93 -0
  30. package/dist/edit.d.ts.map +1 -0
  31. package/dist/edit.js +170 -0
  32. package/dist/edit.js.map +1 -0
  33. package/dist/errors.cjs +2 -0
  34. package/dist/errors.cjs.map +1 -0
  35. package/dist/errors.d.ts +11 -0
  36. package/dist/errors.d.ts.map +1 -0
  37. package/dist/errors.js +9 -0
  38. package/dist/errors.js.map +1 -0
  39. package/dist/graph.cjs +2 -0
  40. package/dist/graph.cjs.map +1 -0
  41. package/dist/graph.d.ts +21 -0
  42. package/dist/graph.d.ts.map +1 -0
  43. package/dist/graph.js +81 -0
  44. package/dist/graph.js.map +1 -0
  45. package/dist/index.cjs +1 -0
  46. package/dist/index.d.ts +9 -0
  47. package/dist/index.d.ts.map +1 -0
  48. package/dist/index.js +8 -0
  49. package/dist/orthogonal.cjs +2 -0
  50. package/dist/orthogonal.cjs.map +1 -0
  51. package/dist/orthogonal.d.ts +37 -0
  52. package/dist/orthogonal.d.ts.map +1 -0
  53. package/dist/orthogonal.js +122 -0
  54. package/dist/orthogonal.js.map +1 -0
  55. package/dist/path.cjs +2 -0
  56. package/dist/path.cjs.map +1 -0
  57. package/dist/path.d.ts +19 -0
  58. package/dist/path.d.ts.map +1 -0
  59. package/dist/path.js +33 -0
  60. package/dist/path.js.map +1 -0
  61. package/dist/ports.cjs +2 -0
  62. package/dist/ports.cjs.map +1 -0
  63. package/dist/ports.d.ts +21 -0
  64. package/dist/ports.d.ts.map +1 -0
  65. package/dist/ports.js +36 -0
  66. package/dist/ports.js.map +1 -0
  67. package/dist/types.d.ts +31 -0
  68. package/dist/types.d.ts.map +1 -0
  69. package/dist/wire.cjs +2 -0
  70. package/dist/wire.cjs.map +1 -0
  71. package/dist/wire.iife.js +2 -0
  72. package/dist/wire.iife.js.map +1 -0
  73. package/dist/wire.js +543 -0
  74. package/dist/wire.js.map +1 -0
  75. package/package.json +48 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Helmuth Saatkamp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,49 @@
1
- # Temporary Holding Version
1
+ # @vielzeug/wire
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ > Edge routing and graph ordering for node editors
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ pnpm add @vielzeug/wire
9
+ npm install @vielzeug/wire
10
+ yarn add @vielzeug/wire
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ```ts
16
+ import { bezierRoute, bezierToSvgPath, findCyclicGroups, orthogonalRoute, topologicalOrder } from '@vielzeug/wire';
17
+
18
+ const curve = bezierRoute({ side: 'right', x: 100, y: 40 }, { side: 'left', x: 320, y: 120 });
19
+ const d = bezierToSvgPath(curve);
20
+ const points = orthogonalRoute(
21
+ { side: 'right', x: 100, y: 40 },
22
+ { side: 'left', x: 320, y: 120 },
23
+ { obstacles: nodeBounds },
24
+ );
25
+ const order = topologicalOrder(['b', 'a'], [{ from: 'a', to: 'b' }]);
26
+ ```
27
+
28
+ ## Features
29
+
30
+ - `bezierRoute()`: cubic bezier geometry anchored to port sides
31
+ - `orthogonalRoute()`: deterministic A\* Manhattan routing over a padded-obstacle visibility graph with a per-bend penalty and owner-obstacle exemption; `null` when unreachable
32
+ - `portAnchor()`: world position of a port on a node rect, by side and 0..1 offset
33
+ - `bezierToSvgPath()` / `polylineToSvgPath()`: SVG `d` serialization with stable precision
34
+ - `topologicalOrder()`: Kahn ordering with stable input-order tie breaks
35
+ - `findCyclicGroups()`: iterative Tarjan SCC cycle detection (no recursion limits, no exponential enumeration)
36
+ - Typed errors (`WireError`, `WireGraphError`, `WireConfigError`)
37
+ - Zero runtime dependencies, no DOM requirement
38
+
39
+ ## Documentation
40
+
41
+ - [Overview](https://vielzeug.dev/wire/)
42
+ - [Usage Guide](https://vielzeug.dev/wire/usage)
43
+ - [API Reference](https://vielzeug.dev/wire/api)
44
+ - [Examples](https://vielzeug.dev/wire/examples)
45
+ - [Migration Guide](https://vielzeug.dev/wire/migration)
46
+
47
+ ## License
48
+
49
+ MIT © [Helmuth Saatkamp](https://github.com/helmuthdu): part of the [Vielzeug](https://github.com/helmuthdu/vielzeug) monorepo.
@@ -0,0 +1,2 @@
1
+ function fmt(e){let t=Math.round(e*1e6)/1e6;return Object.is(t,-0)?`0`:String(t)}exports.fmt=fmt;
2
+ //# sourceMappingURL=_format.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_format.cjs","names":["rounded","value"],"sources":["../src/_format.ts"],"sourcesContent":["/** Formats a number for SVG path output: rounded to micro-precision, no `-0`, no exponent noise. */\nexport function fmt(value: number): string {\n const rounded = Math.round(value * 1e6) / 1e6;\n return Object.is(rounded, -0) ? '0' : String(rounded);\n}\n"],"mappings":"AACA,SAAgB,IAAI,EAAuB,CACzC,IAAMA,EAAU,KAAK,MAAMC,EAAQ,GAAG,EAAI,IAC1C,OAAO,OAAO,GAAGD,EAAS,EAAE,EAAI,IAAM,OAAOA,CAAO,CACtD,C"}
@@ -0,0 +1,3 @@
1
+ /** Formats a number for SVG path output: rounded to micro-precision, no `-0`, no exponent noise. */
2
+ export declare function fmt(value: number): string;
3
+ //# sourceMappingURL=_format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_format.d.ts","sourceRoot":"","sources":["../src/_format.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,wBAAgB,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAGzC"}
@@ -0,0 +1,8 @@
1
+ // src/_format.ts
2
+ function fmt(e) {
3
+ let t = Math.round(e * 1e6) / 1e6;
4
+ return Object.is(t, -0) ? "0" : String(t);
5
+ }
6
+ export { fmt };
7
+
8
+ //# sourceMappingURL=_format.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_format.js","names":["rounded","value"],"sources":["../src/_format.ts"],"sourcesContent":["/** Formats a number for SVG path output: rounded to micro-precision, no `-0`, no exponent noise. */\nexport function fmt(value: number): string {\n const rounded = Math.round(value * 1e6) / 1e6;\n return Object.is(rounded, -0) ? '0' : String(rounded);\n}\n"],"mappings":";AACA,SAAgB,IAAI,GAAuB;CACzC,IAAMA,IAAU,KAAK,MAAMC,IAAQ,GAAG,IAAI;CAC1C,OAAO,OAAO,GAAGD,GAAS,EAAE,IAAI,MAAM,OAAOA,CAAO;AACtD"}
package/dist/_heap.cjs ADDED
@@ -0,0 +1,2 @@
1
+ var MinHeap=class{items=[];get size(){return this.items.length}push(e,t){let n=this.items;n.push({key:e,value:t});let r=n.length-1;for(;r>0;){let e=r-1>>1,t=n[e],i=n[r];if(t.key<=i.key)break;n[e]=i,n[r]=t,r=e}}pop(){let e=this.items,t=e[0],n=e.pop();if(e.length>0){e[0]=n;let t=0;for(;;){let n=t*2+1,r=n+1,i=t;if(n<e.length&&e[n].key<e[i].key&&(i=n),r<e.length&&e[r].key<e[i].key&&(i=r),i===t)break;let a=e[t],o=e[i];e[t]=o,e[i]=a,t=i}}return t.value}};exports.MinHeap=MinHeap;
2
+ //# sourceMappingURL=_heap.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_heap.cjs","names":["items","i","parent","parentItem","item","top","last","left","right","smallest","swap"],"sources":["../src/_heap.ts"],"sourcesContent":["/**\n * Binary min-heap ordered by a numeric key. Ties between equal keys resolve by heap\n * structure, which is fully determined by the push sequence, so extraction is\n * deterministic for identical input.\n */\nexport class MinHeap {\n private readonly items: { key: number; value: number }[] = [];\n\n get size(): number {\n return this.items.length;\n }\n\n push(key: number, value: number): void {\n const items = this.items;\n items.push({ key, value });\n let i = items.length - 1;\n while (i > 0) {\n const parent = (i - 1) >> 1;\n const parentItem = items[parent]!;\n const item = items[i]!;\n if (parentItem.key <= item.key) break;\n items[parent] = item;\n items[i] = parentItem;\n i = parent;\n }\n }\n\n pop(): number {\n const items = this.items;\n const top = items[0]!;\n const last = items.pop()!;\n if (items.length > 0) {\n items[0] = last;\n let i = 0;\n for (;;) {\n const left = i * 2 + 1;\n const right = left + 1;\n let smallest = i;\n if (left < items.length && items[left]!.key < items[smallest]!.key) smallest = left;\n if (right < items.length && items[right]!.key < items[smallest]!.key) smallest = right;\n if (smallest === i) break;\n const item = items[i]!;\n const swap = items[smallest]!;\n items[i] = swap;\n items[smallest] = item;\n i = smallest;\n }\n }\n return top.value;\n }\n}\n"],"mappings":"AAKA,IAAa,QAAb,KAAqB,CACnB,MAA2D,CAAC,EAE5D,IAAI,MAAe,CACjB,OAAO,KAAK,MAAM,MACpB,CAEA,KAAK,EAAa,EAAqB,CACrC,IAAMA,EAAQ,KAAK,MACnBA,EAAM,KAAK,CAAE,MAAK,OAAM,CAAC,EACzB,IAAIC,EAAID,EAAM,OAAS,EACvB,KAAOC,EAAI,GAAG,CACZ,IAAMC,EAAUD,EAAI,GAAM,EACpBE,EAAaH,EAAME,GACnBE,EAAOJ,EAAMC,GACnB,GAAIE,EAAW,KAAOC,EAAK,IAAK,MAChC,EAAMF,GAAUE,EAChB,EAAMH,GAAKE,EACX,EAAID,CACN,CACF,CAEA,KAAc,CACZ,IAAMF,EAAQ,KAAK,MACbK,EAAML,EAAM,GACZM,EAAON,EAAM,IAAI,EACvB,GAAIA,EAAM,OAAS,EAAG,CACpB,EAAM,GAAKM,EACX,IAAIL,EAAI,EACR,OAAS,CACP,IAAMM,EAAON,EAAI,EAAI,EACfO,EAAQD,EAAO,EACjBE,EAAWR,EAGf,GAFIM,EAAOP,EAAM,QAAUA,EAAMO,EAAK,CAAE,IAAMP,EAAMS,EAAS,CAAE,MAAK,EAAWF,GAC3EC,EAAQR,EAAM,QAAUA,EAAMQ,EAAM,CAAE,IAAMR,EAAMS,EAAS,CAAE,MAAK,EAAWD,GAC7EC,IAAaR,EAAG,MACpB,IAAMG,EAAOJ,EAAMC,GACbS,EAAOV,EAAMS,GACnB,EAAMR,GAAKS,EACX,EAAMD,GAAYL,EAClB,EAAIK,CACN,CACF,CACA,OAAOJ,EAAI,KACb,CACF,E"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Binary min-heap ordered by a numeric key. Ties between equal keys resolve by heap
3
+ * structure, which is fully determined by the push sequence, so extraction is
4
+ * deterministic for identical input.
5
+ */
6
+ export declare class MinHeap {
7
+ private readonly items;
8
+ get size(): number;
9
+ push(key: number, value: number): void;
10
+ pop(): number;
11
+ }
12
+ //# sourceMappingURL=_heap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_heap.d.ts","sourceRoot":"","sources":["../src/_heap.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,qBAAa,OAAO;IAClB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAwC;IAE9D,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAetC,GAAG,IAAI,MAAM;CAuBd"}
package/dist/_heap.js ADDED
@@ -0,0 +1,37 @@
1
+ // src/_heap.ts
2
+ var MinHeap = class {
3
+ items = [];
4
+ get size() {
5
+ return this.items.length;
6
+ }
7
+ push(e, t) {
8
+ let n = this.items;
9
+ n.push({
10
+ key: e,
11
+ value: t
12
+ });
13
+ let r = n.length - 1;
14
+ for (; r > 0;) {
15
+ let e = r - 1 >> 1, t = n[e], i = n[r];
16
+ if (t.key <= i.key) break;
17
+ n[e] = i, n[r] = t, r = e;
18
+ }
19
+ }
20
+ pop() {
21
+ let e = this.items, t = e[0], n = e.pop();
22
+ if (e.length > 0) {
23
+ e[0] = n;
24
+ let t = 0;
25
+ for (;;) {
26
+ let n = t * 2 + 1, r = n + 1, i = t;
27
+ if (n < e.length && e[n].key < e[i].key && (i = n), r < e.length && e[r].key < e[i].key && (i = r), i === t) break;
28
+ let a = e[t], o = e[i];
29
+ e[t] = o, e[i] = a, t = i;
30
+ }
31
+ }
32
+ return t.value;
33
+ }
34
+ };
35
+ export { MinHeap };
36
+
37
+ //# sourceMappingURL=_heap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_heap.js","names":["items","i","parent","parentItem","item","top","last","left","right","smallest","swap"],"sources":["../src/_heap.ts"],"sourcesContent":["/**\n * Binary min-heap ordered by a numeric key. Ties between equal keys resolve by heap\n * structure, which is fully determined by the push sequence, so extraction is\n * deterministic for identical input.\n */\nexport class MinHeap {\n private readonly items: { key: number; value: number }[] = [];\n\n get size(): number {\n return this.items.length;\n }\n\n push(key: number, value: number): void {\n const items = this.items;\n items.push({ key, value });\n let i = items.length - 1;\n while (i > 0) {\n const parent = (i - 1) >> 1;\n const parentItem = items[parent]!;\n const item = items[i]!;\n if (parentItem.key <= item.key) break;\n items[parent] = item;\n items[i] = parentItem;\n i = parent;\n }\n }\n\n pop(): number {\n const items = this.items;\n const top = items[0]!;\n const last = items.pop()!;\n if (items.length > 0) {\n items[0] = last;\n let i = 0;\n for (;;) {\n const left = i * 2 + 1;\n const right = left + 1;\n let smallest = i;\n if (left < items.length && items[left]!.key < items[smallest]!.key) smallest = left;\n if (right < items.length && items[right]!.key < items[smallest]!.key) smallest = right;\n if (smallest === i) break;\n const item = items[i]!;\n const swap = items[smallest]!;\n items[i] = swap;\n items[smallest] = item;\n i = smallest;\n }\n }\n return top.value;\n }\n}\n"],"mappings":";AAKA,IAAa,UAAb,MAAqB;CACnB,QAA2D,CAAC;CAE5D,IAAI,OAAe;EACjB,OAAO,KAAK,MAAM;CACpB;CAEA,KAAK,GAAa,GAAqB;EACrC,IAAMA,IAAQ,KAAK;EACnB,EAAM,KAAK;GAAE;GAAK;EAAM,CAAC;EACzB,IAAIC,IAAID,EAAM,SAAS;EACvB,OAAOC,IAAI,IAAG;GACZ,IAAMC,IAAUD,IAAI,KAAM,GACpBE,IAAaH,EAAME,IACnBE,IAAOJ,EAAMC;GACnB,IAAIE,EAAW,OAAOC,EAAK,KAAK;GAGhC,AAFA,EAAMF,KAAUE,GAChB,EAAMH,KAAKE,GACX,IAAID;EACN;CACF;CAEA,MAAc;EACZ,IAAMF,IAAQ,KAAK,OACbK,IAAML,EAAM,IACZM,IAAON,EAAM,IAAI;EACvB,IAAIA,EAAM,SAAS,GAAG;GACpB,EAAM,KAAKM;GACX,IAAIL,IAAI;GACR,SAAS;IACP,IAAMM,IAAON,IAAI,IAAI,GACfO,IAAQD,IAAO,GACjBE,IAAWR;IAGf,IAFIM,IAAOP,EAAM,UAAUA,EAAMO,EAAK,CAAE,MAAMP,EAAMS,EAAS,CAAE,QAAK,IAAWF,IAC3EC,IAAQR,EAAM,UAAUA,EAAMQ,EAAM,CAAE,MAAMR,EAAMS,EAAS,CAAE,QAAK,IAAWD,IAC7EC,MAAaR,GAAG;IACpB,IAAMG,IAAOJ,EAAMC,IACbS,IAAOV,EAAMS;IAGnB,AAFA,EAAMR,KAAKS,GACX,EAAMD,KAAYL,GAClB,IAAIK;GACN;EACF;EACA,OAAOJ,EAAI;CACb;AACF"}
@@ -0,0 +1,2 @@
1
+ var e={bottom:{x:0,y:1},left:{x:-1,y:0},right:{x:1,y:0},top:{x:0,y:-1}},t={Down:4,Left:1,None:0,Right:2,Up:3},n={[t.Left]:{x:-1,y:0},[t.Right]:{x:1,y:0},[t.Up]:{x:0,y:-1},[t.Down]:{x:0,y:1}},r={bottom:t.Down,left:t.Left,right:t.Right,top:t.Up},i={[t.Down]:t.Up,[t.Left]:t.Right,[t.Right]:t.Left,[t.Up]:t.Down};exports.DELTA=n,exports.Dir=t,exports.OPPOSITE=i,exports.SIDE_DIR=r,exports.SIDE_VECTOR=e;
2
+ //# sourceMappingURL=_sides.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_sides.cjs","names":["SIDE_VECTOR","Dir","DELTA","SIDE_DIR","OPPOSITE"],"sources":["../src/_sides.ts"],"sourcesContent":["import type { Point, PortSide } from './types.js';\n\n/** Outward unit vector for each port side; shared by the routers. */\nexport const SIDE_VECTOR: Readonly<Record<PortSide, Point>> = {\n bottom: { x: 0, y: 1 },\n left: { x: -1, y: 0 },\n right: { x: 1, y: 0 },\n top: { x: 0, y: -1 },\n};\n\n/** Grid movement directions; `None` (0) marks a start state with no incoming direction. */\nexport const Dir = { Down: 4, Left: 1, None: 0, Right: 2, Up: 3 } as const;\nexport type Dir = (typeof Dir)[keyof typeof Dir];\n/** The four real movement directions, excluding the `None` sentinel. */\nexport type Move = Exclude<Dir, typeof Dir.None>;\n\n/** Unit steps per movement direction, in grid order. */\nexport const DELTA: Readonly<Record<Move, Point>> = {\n [Dir.Left]: { x: -1, y: 0 },\n [Dir.Right]: { x: 1, y: 0 },\n [Dir.Up]: { x: 0, y: -1 },\n [Dir.Down]: { x: 0, y: 1 },\n};\n\n/** Outward direction a port on the given side faces. */\nexport const SIDE_DIR: Readonly<Record<PortSide, Move>> = {\n bottom: Dir.Down,\n left: Dir.Left,\n right: Dir.Right,\n top: Dir.Up,\n};\n\nexport const OPPOSITE: Readonly<Record<Move, Move>> = {\n [Dir.Down]: Dir.Up,\n [Dir.Left]: Dir.Right,\n [Dir.Right]: Dir.Left,\n [Dir.Up]: Dir.Down,\n};\n"],"mappings":"AAGA,IAAaA,EAAiD,CAC5D,OAAQ,CAAE,EAAG,EAAG,EAAG,CAAE,EACrB,KAAM,CAAE,EAAG,GAAI,EAAG,CAAE,EACpB,MAAO,CAAE,EAAG,EAAG,EAAG,CAAE,EACpB,IAAK,CAAE,EAAG,EAAG,EAAG,EAAG,CACrB,EAGaC,EAAM,CAAE,KAAM,EAAG,KAAM,EAAG,KAAM,EAAG,MAAO,EAAG,GAAI,CAAE,EAMnDC,EAAuC,EACjDD,EAAI,MAAO,CAAE,EAAG,GAAI,EAAG,CAAE,GACzBA,EAAI,OAAQ,CAAE,EAAG,EAAG,EAAG,CAAE,GACzBA,EAAI,IAAK,CAAE,EAAG,EAAG,EAAG,EAAG,GACvBA,EAAI,MAAO,CAAE,EAAG,EAAG,EAAG,CAAE,CAC3B,EAGaE,EAA6C,CACxD,OAAQF,EAAI,KACZ,KAAMA,EAAI,KACV,MAAOA,EAAI,MACX,IAAKA,EAAI,EACX,EAEaG,EAAyC,EACnDH,EAAI,MAAOA,EAAI,IACfA,EAAI,MAAOA,EAAI,OACfA,EAAI,OAAQA,EAAI,MAChBA,EAAI,IAAKA,EAAI,IAChB,E"}
@@ -0,0 +1,20 @@
1
+ import type { Point, PortSide } from './types.js';
2
+ /** Outward unit vector for each port side; shared by the routers. */
3
+ export declare const SIDE_VECTOR: Readonly<Record<PortSide, Point>>;
4
+ /** Grid movement directions; `None` (0) marks a start state with no incoming direction. */
5
+ export declare const Dir: {
6
+ readonly Down: 4;
7
+ readonly Left: 1;
8
+ readonly None: 0;
9
+ readonly Right: 2;
10
+ readonly Up: 3;
11
+ };
12
+ export type Dir = (typeof Dir)[keyof typeof Dir];
13
+ /** The four real movement directions, excluding the `None` sentinel. */
14
+ export type Move = Exclude<Dir, typeof Dir.None>;
15
+ /** Unit steps per movement direction, in grid order. */
16
+ export declare const DELTA: Readonly<Record<Move, Point>>;
17
+ /** Outward direction a port on the given side faces. */
18
+ export declare const SIDE_DIR: Readonly<Record<PortSide, Move>>;
19
+ export declare const OPPOSITE: Readonly<Record<Move, Move>>;
20
+ //# sourceMappingURL=_sides.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_sides.d.ts","sourceRoot":"","sources":["../src/_sides.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAElD,qEAAqE;AACrE,eAAO,MAAM,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,CAKzD,CAAC;AAEF,2FAA2F;AAC3F,eAAO,MAAM,GAAG;;;;;;CAA0D,CAAC;AAC3E,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,GAAG,CAAC,CAAC,MAAM,OAAO,GAAG,CAAC,CAAC;AACjD,wEAAwE;AACxE,MAAM,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC;AAEjD,wDAAwD;AACxD,eAAO,MAAM,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAK/C,CAAC;AAEF,wDAAwD;AACxD,eAAO,MAAM,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAKrD,CAAC;AAEF,eAAO,MAAM,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAKjD,CAAC"}
package/dist/_sides.js ADDED
@@ -0,0 +1,55 @@
1
+ // src/_sides.ts
2
+ var e = {
3
+ bottom: {
4
+ x: 0,
5
+ y: 1
6
+ },
7
+ left: {
8
+ x: -1,
9
+ y: 0
10
+ },
11
+ right: {
12
+ x: 1,
13
+ y: 0
14
+ },
15
+ top: {
16
+ x: 0,
17
+ y: -1
18
+ }
19
+ }, t = {
20
+ Down: 4,
21
+ Left: 1,
22
+ None: 0,
23
+ Right: 2,
24
+ Up: 3
25
+ }, n = {
26
+ [t.Left]: {
27
+ x: -1,
28
+ y: 0
29
+ },
30
+ [t.Right]: {
31
+ x: 1,
32
+ y: 0
33
+ },
34
+ [t.Up]: {
35
+ x: 0,
36
+ y: -1
37
+ },
38
+ [t.Down]: {
39
+ x: 0,
40
+ y: 1
41
+ }
42
+ }, r = {
43
+ bottom: t.Down,
44
+ left: t.Left,
45
+ right: t.Right,
46
+ top: t.Up
47
+ }, i = {
48
+ [t.Down]: t.Up,
49
+ [t.Left]: t.Right,
50
+ [t.Right]: t.Left,
51
+ [t.Up]: t.Down
52
+ };
53
+ export { n as DELTA, t as Dir, i as OPPOSITE, r as SIDE_DIR, e as SIDE_VECTOR };
54
+
55
+ //# sourceMappingURL=_sides.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_sides.js","names":["SIDE_VECTOR","Dir","DELTA","SIDE_DIR","OPPOSITE"],"sources":["../src/_sides.ts"],"sourcesContent":["import type { Point, PortSide } from './types.js';\n\n/** Outward unit vector for each port side; shared by the routers. */\nexport const SIDE_VECTOR: Readonly<Record<PortSide, Point>> = {\n bottom: { x: 0, y: 1 },\n left: { x: -1, y: 0 },\n right: { x: 1, y: 0 },\n top: { x: 0, y: -1 },\n};\n\n/** Grid movement directions; `None` (0) marks a start state with no incoming direction. */\nexport const Dir = { Down: 4, Left: 1, None: 0, Right: 2, Up: 3 } as const;\nexport type Dir = (typeof Dir)[keyof typeof Dir];\n/** The four real movement directions, excluding the `None` sentinel. */\nexport type Move = Exclude<Dir, typeof Dir.None>;\n\n/** Unit steps per movement direction, in grid order. */\nexport const DELTA: Readonly<Record<Move, Point>> = {\n [Dir.Left]: { x: -1, y: 0 },\n [Dir.Right]: { x: 1, y: 0 },\n [Dir.Up]: { x: 0, y: -1 },\n [Dir.Down]: { x: 0, y: 1 },\n};\n\n/** Outward direction a port on the given side faces. */\nexport const SIDE_DIR: Readonly<Record<PortSide, Move>> = {\n bottom: Dir.Down,\n left: Dir.Left,\n right: Dir.Right,\n top: Dir.Up,\n};\n\nexport const OPPOSITE: Readonly<Record<Move, Move>> = {\n [Dir.Down]: Dir.Up,\n [Dir.Left]: Dir.Right,\n [Dir.Right]: Dir.Left,\n [Dir.Up]: Dir.Down,\n};\n"],"mappings":";AAGA,IAAaA,IAAiD;CAC5D,QAAQ;EAAE,GAAG;EAAG,GAAG;CAAE;CACrB,MAAM;EAAE,GAAG;EAAI,GAAG;CAAE;CACpB,OAAO;EAAE,GAAG;EAAG,GAAG;CAAE;CACpB,KAAK;EAAE,GAAG;EAAG,GAAG;CAAG;AACrB,GAGaC,IAAM;CAAE,MAAM;CAAG,MAAM;CAAG,MAAM;CAAG,OAAO;CAAG,IAAI;AAAE,GAMnDC,IAAuC;EACjDD,EAAI,OAAO;EAAE,GAAG;EAAI,GAAG;CAAE;EACzBA,EAAI,QAAQ;EAAE,GAAG;EAAG,GAAG;CAAE;EACzBA,EAAI,KAAK;EAAE,GAAG;EAAG,GAAG;CAAG;EACvBA,EAAI,OAAO;EAAE,GAAG;EAAG,GAAG;CAAE;AAC3B,GAGaE,IAA6C;CACxD,QAAQF,EAAI;CACZ,MAAMA,EAAI;CACV,OAAOA,EAAI;CACX,KAAKA,EAAI;AACX,GAEaG,IAAyC;EACnDH,EAAI,OAAOA,EAAI;EACfA,EAAI,OAAOA,EAAI;EACfA,EAAI,QAAQA,EAAI;EAChBA,EAAI,KAAKA,EAAI;AAChB"}
@@ -0,0 +1,2 @@
1
+ const e=require("./_sides.cjs"),t=require("./errors.cjs");var n=40,r=1.5;function bezierRoute(i,a,o={}){let s=o.handleLength??n;if(!Number.isFinite(s)||s<0)throw new t.WireConfigError(`Bezier handleLength must be a finite non-negative number, received ${String(s)}.`);let c=e.SIDE_VECTOR[i.side],l=e.SIDE_VECTOR[a.side],u=s;if(o.selfLoop&&i.side===a.side){let e=Math.hypot(a.x-i.x,a.y-i.y);u=Math.max(s,e*r)}return{c1:{x:i.x+c.x*u,y:i.y+c.y*u},c2:{x:a.x+l.x*u,y:a.y+l.y*u},from:{x:i.x,y:i.y},to:{x:a.x,y:a.y}}}exports.bezierRoute=bezierRoute;
2
+ //# sourceMappingURL=bezier.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bezier.cjs","names":["WireConfigError","SIDE_VECTOR","require__sides","require_errors","DEFAULT_HANDLE_LENGTH","SELF_LOOP_HANDLE_RATIO","handleLength","options","fromVector","from","toVector","to","effectiveHandle","spacing"],"sources":["../src/bezier.ts"],"sourcesContent":["import { SIDE_VECTOR } from './_sides.js';\nimport { WireConfigError } from './errors.js';\nimport type { CubicBezier, PortAnchor } from './types.js';\n\n/** Default control-point distance for `bezierRoute()` when `handleLength` is omitted. */\nconst DEFAULT_HANDLE_LENGTH = 40;\n\n/** Configuration for `bezierRoute()`. */\nexport interface BezierRouteOptions {\n /** Distance each control point extends outward from its port along the port side. Defaults to 40. */\n handleLength?: number;\n /**\n * Widen the handles so an edge whose endpoints sit on the same side of one\n * node forms a visible loop instead of collapsing onto that side. When the\n * ports share a side, each handle grows to `1.5 *` the distance between the\n * ports (never smaller than `handleLength`), keeping the loop symmetric and\n * deterministic. Ignored when the ports face different sides. Defaults to false.\n */\n selfLoop?: boolean;\n}\n\n/** Multiplier applied to the port spacing when `selfLoop` widens the handles. */\nconst SELF_LOOP_HANDLE_RATIO = 1.5;\n\n/**\n * Builds a cubic bezier segment between two ports.\n *\n * Control points extend outward from each port along its side, so the curve leaves\n * and enters ports perpendicular to the node edge. With `selfLoop` and both ports\n * on the same side, the handles stretch to `1.5 *` the port spacing so the curve\n * bulges outward into a loop.\n */\nexport function bezierRoute(from: PortAnchor, to: PortAnchor, options: BezierRouteOptions = {}): CubicBezier {\n const handleLength = options.handleLength ?? DEFAULT_HANDLE_LENGTH;\n if (!Number.isFinite(handleLength) || handleLength < 0) {\n throw new WireConfigError(\n `Bezier handleLength must be a finite non-negative number, received ${String(handleLength)}.`,\n );\n }\n\n const fromVector = SIDE_VECTOR[from.side];\n const toVector = SIDE_VECTOR[to.side];\n\n let effectiveHandle = handleLength;\n if (options.selfLoop && from.side === to.side) {\n const spacing = Math.hypot(to.x - from.x, to.y - from.y);\n effectiveHandle = Math.max(handleLength, spacing * SELF_LOOP_HANDLE_RATIO);\n }\n\n return {\n c1: { x: from.x + fromVector.x * effectiveHandle, y: from.y + fromVector.y * effectiveHandle },\n c2: { x: to.x + toVector.x * effectiveHandle, y: to.y + toVector.y * effectiveHandle },\n from: { x: from.x, y: from.y },\n to: { x: to.x, y: to.y },\n };\n}\n"],"mappings":"A,0DAKA,IAAMI,EAAwB,GAiBxBC,EAAyB,IAU/B,SAAgB,YAAY,EAAkB,EAAgB,EAA8B,CAAC,EAAgB,CAC3G,IAAMC,EAAeC,EAAQ,cAAgBH,EAC7C,GAAI,CAAC,OAAO,SAASE,CAAY,GAAKA,EAAe,EACnD,MAAM,IAAIN,EAAAA,gBACR,sEAAsE,OAAOM,CAAY,EAAE,EAC7F,EAGF,IAAME,EAAaP,EAAAA,YAAYQ,EAAK,MAC9BC,EAAWT,EAAAA,YAAYU,EAAG,MAE5BC,EAAkBN,EACtB,GAAIC,EAAQ,UAAYE,EAAK,OAASE,EAAG,KAAM,CAC7C,IAAME,EAAU,KAAK,MAAMF,EAAG,EAAIF,EAAK,EAAGE,EAAG,EAAIF,EAAK,CAAC,EACvD,EAAkB,KAAK,IAAIH,EAAcO,EAAUR,CAAsB,CAC3E,CAEA,MAAO,CACL,GAAI,CAAE,EAAGI,EAAK,EAAID,EAAW,EAAII,EAAiB,EAAGH,EAAK,EAAID,EAAW,EAAII,CAAgB,EAC7F,GAAI,CAAE,EAAGD,EAAG,EAAID,EAAS,EAAIE,EAAiB,EAAGD,EAAG,EAAID,EAAS,EAAIE,CAAgB,EACrF,KAAM,CAAE,EAAGH,EAAK,EAAG,EAAGA,EAAK,CAAE,EAC7B,GAAI,CAAE,EAAGE,EAAG,EAAG,EAAGA,EAAG,CAAE,CACzB,CACF,C"}
@@ -0,0 +1,24 @@
1
+ import type { CubicBezier, PortAnchor } from './types.js';
2
+ /** Configuration for `bezierRoute()`. */
3
+ export interface BezierRouteOptions {
4
+ /** Distance each control point extends outward from its port along the port side. Defaults to 40. */
5
+ handleLength?: number;
6
+ /**
7
+ * Widen the handles so an edge whose endpoints sit on the same side of one
8
+ * node forms a visible loop instead of collapsing onto that side. When the
9
+ * ports share a side, each handle grows to `1.5 *` the distance between the
10
+ * ports (never smaller than `handleLength`), keeping the loop symmetric and
11
+ * deterministic. Ignored when the ports face different sides. Defaults to false.
12
+ */
13
+ selfLoop?: boolean;
14
+ }
15
+ /**
16
+ * Builds a cubic bezier segment between two ports.
17
+ *
18
+ * Control points extend outward from each port along its side, so the curve leaves
19
+ * and enters ports perpendicular to the node edge. With `selfLoop` and both ports
20
+ * on the same side, the handles stretch to `1.5 *` the port spacing so the curve
21
+ * bulges outward into a loop.
22
+ */
23
+ export declare function bezierRoute(from: PortAnchor, to: PortAnchor, options?: BezierRouteOptions): CubicBezier;
24
+ //# sourceMappingURL=bezier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bezier.d.ts","sourceRoot":"","sources":["../src/bezier.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAK1D,yCAAyC;AACzC,MAAM,WAAW,kBAAkB;IACjC,qGAAqG;IACrG,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAKD;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,EAAE,OAAO,GAAE,kBAAuB,GAAG,WAAW,CAuB3G"}
package/dist/bezier.js ADDED
@@ -0,0 +1,34 @@
1
+ import { SIDE_VECTOR as e } from "./_sides.js";
2
+ import { WireConfigError as t } from "./errors.js";
3
+ // src/bezier.ts
4
+ var n = 40, r = 1.5;
5
+ function bezierRoute(i, a, o = {}) {
6
+ let s = o.handleLength ?? n;
7
+ if (!Number.isFinite(s) || s < 0) throw new t(`Bezier handleLength must be a finite non-negative number, received ${String(s)}.`);
8
+ let c = e[i.side], l = e[a.side], u = s;
9
+ if (o.selfLoop && i.side === a.side) {
10
+ let e = Math.hypot(a.x - i.x, a.y - i.y);
11
+ u = Math.max(s, e * r);
12
+ }
13
+ return {
14
+ c1: {
15
+ x: i.x + c.x * u,
16
+ y: i.y + c.y * u
17
+ },
18
+ c2: {
19
+ x: a.x + l.x * u,
20
+ y: a.y + l.y * u
21
+ },
22
+ from: {
23
+ x: i.x,
24
+ y: i.y
25
+ },
26
+ to: {
27
+ x: a.x,
28
+ y: a.y
29
+ }
30
+ };
31
+ }
32
+ export { bezierRoute };
33
+
34
+ //# sourceMappingURL=bezier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bezier.js","names":["DEFAULT_HANDLE_LENGTH","SELF_LOOP_HANDLE_RATIO","handleLength","options","WireConfigError","fromVector","SIDE_VECTOR","from","toVector","to","effectiveHandle","spacing"],"sources":["../src/bezier.ts"],"sourcesContent":["import { SIDE_VECTOR } from './_sides.js';\nimport { WireConfigError } from './errors.js';\nimport type { CubicBezier, PortAnchor } from './types.js';\n\n/** Default control-point distance for `bezierRoute()` when `handleLength` is omitted. */\nconst DEFAULT_HANDLE_LENGTH = 40;\n\n/** Configuration for `bezierRoute()`. */\nexport interface BezierRouteOptions {\n /** Distance each control point extends outward from its port along the port side. Defaults to 40. */\n handleLength?: number;\n /**\n * Widen the handles so an edge whose endpoints sit on the same side of one\n * node forms a visible loop instead of collapsing onto that side. When the\n * ports share a side, each handle grows to `1.5 *` the distance between the\n * ports (never smaller than `handleLength`), keeping the loop symmetric and\n * deterministic. Ignored when the ports face different sides. Defaults to false.\n */\n selfLoop?: boolean;\n}\n\n/** Multiplier applied to the port spacing when `selfLoop` widens the handles. */\nconst SELF_LOOP_HANDLE_RATIO = 1.5;\n\n/**\n * Builds a cubic bezier segment between two ports.\n *\n * Control points extend outward from each port along its side, so the curve leaves\n * and enters ports perpendicular to the node edge. With `selfLoop` and both ports\n * on the same side, the handles stretch to `1.5 *` the port spacing so the curve\n * bulges outward into a loop.\n */\nexport function bezierRoute(from: PortAnchor, to: PortAnchor, options: BezierRouteOptions = {}): CubicBezier {\n const handleLength = options.handleLength ?? DEFAULT_HANDLE_LENGTH;\n if (!Number.isFinite(handleLength) || handleLength < 0) {\n throw new WireConfigError(\n `Bezier handleLength must be a finite non-negative number, received ${String(handleLength)}.`,\n );\n }\n\n const fromVector = SIDE_VECTOR[from.side];\n const toVector = SIDE_VECTOR[to.side];\n\n let effectiveHandle = handleLength;\n if (options.selfLoop && from.side === to.side) {\n const spacing = Math.hypot(to.x - from.x, to.y - from.y);\n effectiveHandle = Math.max(handleLength, spacing * SELF_LOOP_HANDLE_RATIO);\n }\n\n return {\n c1: { x: from.x + fromVector.x * effectiveHandle, y: from.y + fromVector.y * effectiveHandle },\n c2: { x: to.x + toVector.x * effectiveHandle, y: to.y + toVector.y * effectiveHandle },\n from: { x: from.x, y: from.y },\n to: { x: to.x, y: to.y },\n };\n}\n"],"mappings":";;;AAKA,IAAMA,IAAwB,IAiBxBC,IAAyB;AAU/B,SAAgB,YAAY,GAAkB,GAAgB,IAA8B,CAAC,GAAgB;CAC3G,IAAMC,IAAeC,EAAQ,gBAAgBH;CAC7C,IAAI,CAAC,OAAO,SAASE,CAAY,KAAKA,IAAe,GACnD,MAAM,IAAIE,EACR,sEAAsE,OAAOF,CAAY,EAAE,EAC7F;CAGF,IAAMG,IAAaC,EAAYC,EAAK,OAC9BC,IAAWF,EAAYG,EAAG,OAE5BC,IAAkBR;CACtB,IAAIC,EAAQ,YAAYI,EAAK,SAASE,EAAG,MAAM;EAC7C,IAAME,IAAU,KAAK,MAAMF,EAAG,IAAIF,EAAK,GAAGE,EAAG,IAAIF,EAAK,CAAC;EACvD,IAAkB,KAAK,IAAIL,GAAcS,IAAUV,CAAsB;CAC3E;CAEA,OAAO;EACL,IAAI;GAAE,GAAGM,EAAK,IAAIF,EAAW,IAAIK;GAAiB,GAAGH,EAAK,IAAIF,EAAW,IAAIK;EAAgB;EAC7F,IAAI;GAAE,GAAGD,EAAG,IAAID,EAAS,IAAIE;GAAiB,GAAGD,EAAG,IAAID,EAAS,IAAIE;EAAgB;EACrF,MAAM;GAAE,GAAGH,EAAK;GAAG,GAAGA,EAAK;EAAE;EAC7B,IAAI;GAAE,GAAGE,EAAG;GAAG,GAAGA,EAAG;EAAE;CACzB;AACF"}
package/dist/edit.cjs ADDED
@@ -0,0 +1,2 @@
1
+ const e=require("./errors.cjs");var t=10,n=6,r=4,i=64;function cubicAt(e,t){let n=1-t,r=n*n*n,i=3*n*n*t,a=3*n*t*t,o=t*t*t;return{x:r*e.from.x+i*e.c1.x+a*e.c2.x+o*e.to.x,y:r*e.from.y+i*e.c1.y+a*e.c2.y+o*e.to.y}}function requirePoint(t,n){if(!Number.isFinite(t.x)||!Number.isFinite(t.y))throw new e.WireConfigError(`Wire edit ${n} must have finite coordinates, received (${t.x}, ${t.y}).`)}function requirePositive(t,n){if(!Number.isFinite(t)||t<=0)throw new e.WireConfigError(`Wire edit ${n} must be a finite positive number, received ${String(t)}.`)}function requireUnitT(t){if(!Number.isFinite(t)||t<0||t>1)throw new e.WireConfigError(`Wire edit t must be a finite number in [0, 1], received ${String(t)}.`)}function arrowhead(r,i,a={}){requirePoint(r,`arrowhead tip`),requirePoint(i,`arrowhead direction`);let o=Math.hypot(i.x,i.y);if(o===0)throw new e.WireConfigError(`Wire edit arrowhead direction must be non-zero.`);let s=a.length??t,c=a.width??n;requirePositive(s,`arrowhead length`),requirePositive(c,`arrowhead width`);let l=i.x/o,u=i.y/o,d=-u,f=l,p=r.x-l*s,m=r.y-u*s,h=c/2;return[{x:r.x,y:r.y},{x:p+d*h,y:m+f*h},{x:p-d*h,y:m-f*h}]}function polylineLengths(e){let t=Array(e.length);t[0]=0;for(let n=1;n<e.length;n++)t[n]=t[n-1]+Math.hypot(e[n].x-e[n-1].x,e[n].y-e[n-1].y);return t}function pointAtPolylineLength(e,t,n){let r=t[t.length-1];if(r===0)return{x:e[0].x,y:e[0].y};let i=Math.min(Math.max(n,0),r),a=1,o=t.length-1;for(;a<o;){let e=a+o>>1;t[e]<i?a=e+1:o=e}let s=e[a-1],c=e[a],l=t[a]-t[a-1];if(l===0)return{x:s.x,y:s.y};let u=(i-t[a-1])/l;return{x:s.x+(c.x-s.x)*u,y:s.y+(c.y-s.y)*u}}function bezierTable(e){let t=Array(65),n=Array(65);for(let n=0;n<=i;n++)t[n]=cubicAt(e,n/i);n[0]=0;for(let e=1;e<=i;e++)n[e]=n[e-1]+Math.hypot(t[e].x-t[e-1].x,t[e].y-t[e-1].y);return{cumulative:n,samples:t,total:n[i]}}function uAtBezierLength(e,t){let n=Math.min(Math.max(t,0),e.total),r=1,a=i;for(;r<a;){let t=r+a>>1;e.cumulative[t]<n?r=t+1:a=t}let o=e.cumulative[r]-e.cumulative[r-1],s=o===0?0:(n-e.cumulative[r-1])/o;return(r-1+s)/i}function sampledLengthAt(e,t){let n=Math.min(Math.max(t,0),1)*i,r=Math.min(Math.floor(n),63),a=n-r;return e.cumulative[r]+a*(e.cumulative[r+1]-e.cumulative[r])}function requireCurve(e,t){requirePoint(e.from,`${t} from`),requirePoint(e.c1,`${t} c1`),requirePoint(e.c2,`${t} c2`),requirePoint(e.to,`${t} to`)}function pointOnPolyline(t,n){if(t.length<2)throw new e.WireConfigError(`Wire edit pointOnPolyline requires at least 2 points, received ${t.length}.`);for(let e=0;e<t.length;e++)requirePoint(t[e],`pointOnPolyline points[${e}]`);requireUnitT(n);let r=polylineLengths(t);return pointAtPolylineLength(t,r,n*r[r.length-1])}function pointOnBezier(e,t){requireCurve(e,`pointOnBezier`),requireUnitT(t);let n=bezierTable(e);return n.total===0?{x:e.from.x,y:e.from.y}:cubicAt(e,uAtBezierLength(n,t*n.total))}function distanceToSegment(e,t,n){let r=n.x-t.x,i=n.y-t.y,a=r*r+i*i;if(a===0)return{distance:Math.hypot(e.x-t.x,e.y-t.y),ratio:0};let o=Math.min(Math.max(((e.x-t.x)*r+(e.y-t.y)*i)/a,0),1),s=t.x+r*o,c=t.y+i*o;return{distance:Math.hypot(e.x-s,e.y-c),ratio:o}}function hitPolyline(t,n,i={}){if(t.length<2)throw new e.WireConfigError(`Wire edit hitPolyline requires at least 2 points, received ${t.length}.`);for(let e=0;e<t.length;e++)requirePoint(t[e],`hitPolyline points[${e}]`);requirePoint(n,`hitPolyline point`);let a=i.threshold??r;requirePositive(a,`hitPolyline threshold`);let o=polylineLengths(t),s=o[o.length-1],c=1/0,l=0;for(let e=1;e<t.length;e++){let r=distanceToSegment(n,t[e-1],t[e]);r.distance<c&&(c=r.distance,l=o[e-1]+r.ratio*(o[e]-o[e-1]))}return c>a?null:{distance:c,t:s===0?0:l/s}}function hitBezier(e,t,n={}){requireCurve(e,`hitBezier`),requirePoint(t,`hitBezier point`);let a=n.threshold??r;requirePositive(a,`hitBezier threshold`);let o=bezierTable(e);if(o.total===0){let n=Math.hypot(t.x-e.from.x,t.y-e.from.y);return n<=a?{distance:n,t:0}:null}let s=0,c=1/0;for(let e=0;e<=i;e++){let n=Math.hypot(t.x-o.samples[e].x,t.y-o.samples[e].y);n<c&&(c=n,s=e)}let l=Math.max(0,(s-1)/i),u=Math.min(1,(s+1)/i);for(let n=0;n<32;n++){let n=l+(u-l)/3,r=u-(u-l)/3,i=cubicAt(e,n),a=cubicAt(e,r);Math.hypot(t.x-i.x,t.y-i.y)<Math.hypot(t.x-a.x,t.y-a.y)?u=r:l=n}let d=(l+u)/2,f=cubicAt(e,d),p=Math.hypot(t.x-f.x,t.y-f.y);return p>a?null:{distance:p,t:sampledLengthAt(o,d)/o.total}}exports.arrowhead=arrowhead,exports.hitBezier=hitBezier,exports.hitPolyline=hitPolyline,exports.pointOnBezier=pointOnBezier,exports.pointOnPolyline=pointOnPolyline;
2
+ //# sourceMappingURL=edit.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit.cjs","names":["WireConfigError","require_errors","DEFAULT_ARROWHEAD_LENGTH","DEFAULT_ARROWHEAD_WIDTH","DEFAULT_HIT_THRESHOLD","BEZIER_SAMPLES","v","u","a","b","c","d","curve","value","label","tip","direction","magnitude","length","options","width","ux","uy","px","py","baseX","baseY","half","cumulative","points","i","total","clamped","target","low","high","mid","start","end","segmentLength","ratio","samples","table","span","scaled","cell","t","abx","aby","segmentLengthSq","point","projX","projY","threshold","bestDistance","bestLength","hit","distance","bestIndex","lo","hi","m1","m2","p1","p2","closest"],"sources":["../src/edit.ts"],"sourcesContent":["import { WireConfigError } from './errors.js';\nimport type { CubicBezier, Point } from './types.js';\n\nconst DEFAULT_ARROWHEAD_LENGTH = 10;\nconst DEFAULT_ARROWHEAD_WIDTH = 6;\nconst DEFAULT_HIT_THRESHOLD = 4;\nconst BEZIER_SAMPLES = 64;\n\n/** Evaluates the cubic bezier at raw parameter `u` in `[0, 1]`. */\nfunction cubicAt(curve: CubicBezier, u: number): Point {\n const v = 1 - u;\n const a = v * v * v;\n const b = 3 * v * v * u;\n const c = 3 * v * u * u;\n const d = u * u * u;\n return {\n x: a * curve.from.x + b * curve.c1.x + c * curve.c2.x + d * curve.to.x,\n y: a * curve.from.y + b * curve.c1.y + c * curve.c2.y + d * curve.to.y,\n };\n}\n\n/** Options for {@link arrowhead}. */\nexport interface ArrowheadOptions {\n /** Distance from the tip back along the shaft. Default 10. */\n readonly length?: number;\n /** Full width of the base, perpendicular to the shaft. Default 6. */\n readonly width?: number;\n}\n\n/** Options for the hit-test helpers. */\nexport interface HitOptions {\n /** Maximum distance from the shape for a hit. Default 4. */\n readonly threshold?: number;\n}\n\n/** Where a point hit a shape. */\nexport interface HitResult {\n /** Euclidean distance from the query point to the closest point on the shape. */\n readonly distance: number;\n /** Position along the shape, normalized by arc length in `[0, 1]`. */\n readonly t: number;\n}\n\nfunction requirePoint(value: Point, label: string): void {\n if (!Number.isFinite(value.x) || !Number.isFinite(value.y)) {\n throw new WireConfigError(`Wire edit ${label} must have finite coordinates, received (${value.x}, ${value.y}).`);\n }\n}\n\nfunction requirePositive(value: number, label: string): void {\n if (!Number.isFinite(value) || value <= 0) {\n throw new WireConfigError(`Wire edit ${label} must be a finite positive number, received ${String(value)}.`);\n }\n}\n\nfunction requireUnitT(t: number): void {\n if (!Number.isFinite(t) || t < 0 || t > 1) {\n throw new WireConfigError(`Wire edit t must be a finite number in [0, 1], received ${String(t)}.`);\n }\n}\n\n/**\n * Build a filled triangle that caps a line at `tip`, pointing along\n * `direction`. The apex sits at `tip`; the base sits `length` behind it\n * along `-direction`, `width` apart. `direction` may be any non-zero\n * vector and is normalized internally.\n *\n * @param tip - The apex of the arrowhead.\n * @param direction - The direction the line arrives from, pointing toward the tip.\n * @param options - Head `length` (default 10) and `width` (default 6).\n * @returns `[tip, baseLeft, baseRight]` as three points.\n * @throws {@link WireConfigError} when a point is non-finite, `direction` is\n * zero, or `length`/`width` are not finite positive numbers.\n */\nexport function arrowhead(tip: Point, direction: Point, options: ArrowheadOptions = {}): Point[] {\n requirePoint(tip, 'arrowhead tip');\n requirePoint(direction, 'arrowhead direction');\n const magnitude = Math.hypot(direction.x, direction.y);\n if (magnitude === 0) {\n throw new WireConfigError('Wire edit arrowhead direction must be non-zero.');\n }\n const length = options.length ?? DEFAULT_ARROWHEAD_LENGTH;\n const width = options.width ?? DEFAULT_ARROWHEAD_WIDTH;\n requirePositive(length, 'arrowhead length');\n requirePositive(width, 'arrowhead width');\n const ux = direction.x / magnitude;\n const uy = direction.y / magnitude;\n // Perpendicular unit vector for the base offsets.\n const px = -uy;\n const py = ux;\n const baseX = tip.x - ux * length;\n const baseY = tip.y - uy * length;\n const half = width / 2;\n return [\n { x: tip.x, y: tip.y },\n { x: baseX + px * half, y: baseY + py * half },\n { x: baseX - px * half, y: baseY - py * half },\n ];\n}\n\nfunction polylineLengths(points: readonly Point[]): number[] {\n const cumulative = new Array<number>(points.length);\n cumulative[0] = 0;\n for (let i = 1; i < points.length; i++) {\n cumulative[i] = cumulative[i - 1] + Math.hypot(points[i].x - points[i - 1].x, points[i].y - points[i - 1].y);\n }\n return cumulative;\n}\n\nfunction pointAtPolylineLength(points: readonly Point[], cumulative: readonly number[], target: number): Point {\n const total = cumulative[cumulative.length - 1];\n if (total === 0) {\n return { x: points[0].x, y: points[0].y };\n }\n const clamped = Math.min(Math.max(target, 0), total);\n let low = 1;\n let high = cumulative.length - 1;\n while (low < high) {\n const mid = (low + high) >> 1;\n if (cumulative[mid] < clamped) {\n low = mid + 1;\n } else {\n high = mid;\n }\n }\n const start = points[low - 1];\n const end = points[low];\n const segmentLength = cumulative[low] - cumulative[low - 1];\n if (segmentLength === 0) {\n return { x: start.x, y: start.y };\n }\n const ratio = (clamped - cumulative[low - 1]) / segmentLength;\n return { x: start.x + (end.x - start.x) * ratio, y: start.y + (end.y - start.y) * ratio };\n}\n\n/** Sampled arc-length lookup table for one cubic bezier. */\ninterface BezierTable {\n readonly cumulative: number[];\n readonly samples: Point[];\n readonly total: number;\n}\n\nfunction bezierTable(curve: CubicBezier): BezierTable {\n const samples = new Array<Point>(BEZIER_SAMPLES + 1);\n const cumulative = new Array<number>(BEZIER_SAMPLES + 1);\n for (let i = 0; i <= BEZIER_SAMPLES; i++) {\n samples[i] = cubicAt(curve, i / BEZIER_SAMPLES);\n }\n cumulative[0] = 0;\n for (let i = 1; i <= BEZIER_SAMPLES; i++) {\n cumulative[i] = cumulative[i - 1] + Math.hypot(samples[i].x - samples[i - 1].x, samples[i].y - samples[i - 1].y);\n }\n return { cumulative, samples, total: cumulative[BEZIER_SAMPLES] };\n}\n\n/** Raw bezier parameter `u` whose sampled arc length is `target`. */\nfunction uAtBezierLength(table: BezierTable, target: number): number {\n const clamped = Math.min(Math.max(target, 0), table.total);\n let low = 1;\n let high = BEZIER_SAMPLES;\n while (low < high) {\n const mid = (low + high) >> 1;\n if (table.cumulative[mid] < clamped) {\n low = mid + 1;\n } else {\n high = mid;\n }\n }\n const span = table.cumulative[low] - table.cumulative[low - 1];\n const ratio = span === 0 ? 0 : (clamped - table.cumulative[low - 1]) / span;\n return (low - 1 + ratio) / BEZIER_SAMPLES;\n}\n\n/** Sampled arc length up to raw parameter `u`, interpolated within its cell. */\nfunction sampledLengthAt(table: BezierTable, u: number): number {\n const scaled = Math.min(Math.max(u, 0), 1) * BEZIER_SAMPLES;\n const cell = Math.min(Math.floor(scaled), BEZIER_SAMPLES - 1);\n const ratio = scaled - cell;\n return table.cumulative[cell] + ratio * (table.cumulative[cell + 1] - table.cumulative[cell]);\n}\n\nfunction requireCurve(curve: CubicBezier, label: string): void {\n requirePoint(curve.from, `${label} from`);\n requirePoint(curve.c1, `${label} c1`);\n requirePoint(curve.c2, `${label} c2`);\n requirePoint(curve.to, `${label} to`);\n}\n\n/**\n * Point on a polyline at parameter `t`, where `t` is normalized by total\n * arc length (not by segment index): `t = 0.5` is the point halfway along\n * the wire's length. Exact for polylines.\n *\n * @param points - Two or more finite points.\n * @param t - Position in `[0, 1]` along the total length.\n * @returns The point at that length. A degenerate all-duplicate polyline\n * returns its first point for every `t`.\n * @throws {@link WireConfigError} when `points` has fewer than two finite\n * points or `t` is outside `[0, 1]`.\n */\nexport function pointOnPolyline(points: readonly Point[], t: number): Point {\n if (points.length < 2) {\n throw new WireConfigError(`Wire edit pointOnPolyline requires at least 2 points, received ${points.length}.`);\n }\n for (let i = 0; i < points.length; i++) {\n requirePoint(points[i], `pointOnPolyline points[${i}]`);\n }\n requireUnitT(t);\n const cumulative = polylineLengths(points);\n return pointAtPolylineLength(points, cumulative, t * cumulative[cumulative.length - 1]);\n}\n\n/**\n * Point on a cubic bezier at parameter `t`, where `t` is normalized by arc\n * length: `t = 0.5` is the point halfway along the curve's drawn length,\n * not the raw bezier midpoint.\n *\n * The arc-length mapping is approximated with a fixed lookup table of 64\n * samples, so `t` is accurate to within roughly one sample cell of the\n * true length fraction — sub-pixel for typical wire sizes.\n *\n * @param curve - A cubic bezier with finite endpoints and control points.\n * @param t - Position in `[0, 1]` along the curve length.\n * @returns The point at that length. A degenerate zero-length curve returns\n * its start for every `t`.\n * @throws {@link WireConfigError} when any curve point is non-finite or `t`\n * is outside `[0, 1]`.\n */\nexport function pointOnBezier(curve: CubicBezier, t: number): Point {\n requireCurve(curve, 'pointOnBezier');\n requireUnitT(t);\n const table = bezierTable(curve);\n if (table.total === 0) {\n return { x: curve.from.x, y: curve.from.y };\n }\n return cubicAt(curve, uAtBezierLength(table, t * table.total));\n}\n\nfunction distanceToSegment(point: Point, a: Point, b: Point): { distance: number; ratio: number } {\n const abx = b.x - a.x;\n const aby = b.y - a.y;\n const segmentLengthSq = abx * abx + aby * aby;\n if (segmentLengthSq === 0) {\n return { distance: Math.hypot(point.x - a.x, point.y - a.y), ratio: 0 };\n }\n const ratio = Math.min(Math.max(((point.x - a.x) * abx + (point.y - a.y) * aby) / segmentLengthSq, 0), 1);\n const projX = a.x + abx * ratio;\n const projY = a.y + aby * ratio;\n return { distance: Math.hypot(point.x - projX, point.y - projY), ratio };\n}\n\n/**\n * Closest point on a polyline to `point`, parameterized by arc length.\n * Exact: every segment is tested analytically.\n *\n * @param points - Two or more finite points.\n * @param point - The query point.\n * @param options - `threshold` (default 4): the maximum distance for a hit.\n * @returns The length-normalized `t` and `distance` of the closest point, or\n * `null` when the closest point is farther away than `threshold`.\n * @throws {@link WireConfigError} when `points` has fewer than two finite\n * points, `point` is non-finite, or `threshold` is not finite positive.\n */\nexport function hitPolyline(points: readonly Point[], point: Point, options: HitOptions = {}): HitResult | null {\n if (points.length < 2) {\n throw new WireConfigError(`Wire edit hitPolyline requires at least 2 points, received ${points.length}.`);\n }\n for (let i = 0; i < points.length; i++) {\n requirePoint(points[i], `hitPolyline points[${i}]`);\n }\n requirePoint(point, 'hitPolyline point');\n const threshold = options.threshold ?? DEFAULT_HIT_THRESHOLD;\n requirePositive(threshold, 'hitPolyline threshold');\n const cumulative = polylineLengths(points);\n const total = cumulative[cumulative.length - 1];\n let bestDistance = Infinity;\n let bestLength = 0;\n for (let i = 1; i < points.length; i++) {\n const hit = distanceToSegment(point, points[i - 1], points[i]);\n if (hit.distance < bestDistance) {\n bestDistance = hit.distance;\n bestLength = cumulative[i - 1] + hit.ratio * (cumulative[i] - cumulative[i - 1]);\n }\n }\n if (bestDistance > threshold) {\n return null;\n }\n return { distance: bestDistance, t: total === 0 ? 0 : bestLength / total };\n}\n\n/**\n * Closest point on a cubic bezier to `point`, parameterized by arc length.\n * Uses the same 64-sample lookup table as {@link pointOnBezier}: the nearest\n * sample is refined by ternary search on the true curve, so the reported\n * `distance` is accurate; `t` carries the table's arc-length tolerance.\n *\n * @param curve - A cubic bezier with finite endpoints and control points.\n * @param point - The query point.\n * @param options - `threshold` (default 4): the maximum distance for a hit.\n * @returns The length-normalized `t` and `distance` of the closest point, or\n * `null` when the closest point is farther away than `threshold`.\n * @throws {@link WireConfigError} when any curve point is non-finite,\n * `point` is non-finite, or `threshold` is not finite positive.\n */\nexport function hitBezier(curve: CubicBezier, point: Point, options: HitOptions = {}): HitResult | null {\n requireCurve(curve, 'hitBezier');\n requirePoint(point, 'hitBezier point');\n const threshold = options.threshold ?? DEFAULT_HIT_THRESHOLD;\n requirePositive(threshold, 'hitBezier threshold');\n const table = bezierTable(curve);\n if (table.total === 0) {\n const distance = Math.hypot(point.x - curve.from.x, point.y - curve.from.y);\n return distance <= threshold ? { distance, t: 0 } : null;\n }\n let bestIndex = 0;\n let bestDistance = Infinity;\n for (let i = 0; i <= BEZIER_SAMPLES; i++) {\n const distance = Math.hypot(point.x - table.samples[i].x, point.y - table.samples[i].y);\n if (distance < bestDistance) {\n bestDistance = distance;\n bestIndex = i;\n }\n }\n // Refine within the neighbouring samples with ternary search, clamped to the\n // curve so an endpoint sample never extrapolates past t=0 or t=1.\n let lo = Math.max(0, (bestIndex - 1) / BEZIER_SAMPLES);\n let hi = Math.min(1, (bestIndex + 1) / BEZIER_SAMPLES);\n for (let i = 0; i < 32; i++) {\n const m1 = lo + (hi - lo) / 3;\n const m2 = hi - (hi - lo) / 3;\n const p1 = cubicAt(curve, m1);\n const p2 = cubicAt(curve, m2);\n const d1 = Math.hypot(point.x - p1.x, point.y - p1.y);\n const d2 = Math.hypot(point.x - p2.x, point.y - p2.y);\n if (d1 < d2) {\n hi = m2;\n } else {\n lo = m1;\n }\n }\n const u = (lo + hi) / 2;\n const closest = cubicAt(curve, u);\n const distance = Math.hypot(point.x - closest.x, point.y - closest.y);\n if (distance > threshold) {\n return null;\n }\n return { distance, t: sampledLengthAt(table, u) / table.total };\n}\n"],"mappings":"A,gCAGA,IAAME,EAA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KAAK,MAAMb,EAAM,EAAIc,EAAG,EAAGd,EAAM,EAAIc,EAAG,CAC1C,EACP,EAAKF,EAEL,EAAKD,CAET,CACA,IAAMtD,GAAKoD,EAAKC,GAAM,EAChBK,EAAU,QAAQrD,EAAOL,CAAC,EAC1BkD,EAAW,KAAK,MAAMP,EAAM,EAAIe,EAAQ,EAAGf,EAAM,EAAIe,EAAQ,CAAC,EAIpE,OAHIR,EAAWJ,EACN,KAEF,CAAE,WAAU,EAAG,gBAAgBX,EAAOnC,CAAC,EAAImC,EAAM,KAAM,CAChE,C"}
package/dist/edit.d.ts ADDED
@@ -0,0 +1,93 @@
1
+ import type { CubicBezier, Point } from './types.js';
2
+ /** Options for {@link arrowhead}. */
3
+ export interface ArrowheadOptions {
4
+ /** Distance from the tip back along the shaft. Default 10. */
5
+ readonly length?: number;
6
+ /** Full width of the base, perpendicular to the shaft. Default 6. */
7
+ readonly width?: number;
8
+ }
9
+ /** Options for the hit-test helpers. */
10
+ export interface HitOptions {
11
+ /** Maximum distance from the shape for a hit. Default 4. */
12
+ readonly threshold?: number;
13
+ }
14
+ /** Where a point hit a shape. */
15
+ export interface HitResult {
16
+ /** Euclidean distance from the query point to the closest point on the shape. */
17
+ readonly distance: number;
18
+ /** Position along the shape, normalized by arc length in `[0, 1]`. */
19
+ readonly t: number;
20
+ }
21
+ /**
22
+ * Build a filled triangle that caps a line at `tip`, pointing along
23
+ * `direction`. The apex sits at `tip`; the base sits `length` behind it
24
+ * along `-direction`, `width` apart. `direction` may be any non-zero
25
+ * vector and is normalized internally.
26
+ *
27
+ * @param tip - The apex of the arrowhead.
28
+ * @param direction - The direction the line arrives from, pointing toward the tip.
29
+ * @param options - Head `length` (default 10) and `width` (default 6).
30
+ * @returns `[tip, baseLeft, baseRight]` as three points.
31
+ * @throws {@link WireConfigError} when a point is non-finite, `direction` is
32
+ * zero, or `length`/`width` are not finite positive numbers.
33
+ */
34
+ export declare function arrowhead(tip: Point, direction: Point, options?: ArrowheadOptions): Point[];
35
+ /**
36
+ * Point on a polyline at parameter `t`, where `t` is normalized by total
37
+ * arc length (not by segment index): `t = 0.5` is the point halfway along
38
+ * the wire's length. Exact for polylines.
39
+ *
40
+ * @param points - Two or more finite points.
41
+ * @param t - Position in `[0, 1]` along the total length.
42
+ * @returns The point at that length. A degenerate all-duplicate polyline
43
+ * returns its first point for every `t`.
44
+ * @throws {@link WireConfigError} when `points` has fewer than two finite
45
+ * points or `t` is outside `[0, 1]`.
46
+ */
47
+ export declare function pointOnPolyline(points: readonly Point[], t: number): Point;
48
+ /**
49
+ * Point on a cubic bezier at parameter `t`, where `t` is normalized by arc
50
+ * length: `t = 0.5` is the point halfway along the curve's drawn length,
51
+ * not the raw bezier midpoint.
52
+ *
53
+ * The arc-length mapping is approximated with a fixed lookup table of 64
54
+ * samples, so `t` is accurate to within roughly one sample cell of the
55
+ * true length fraction — sub-pixel for typical wire sizes.
56
+ *
57
+ * @param curve - A cubic bezier with finite endpoints and control points.
58
+ * @param t - Position in `[0, 1]` along the curve length.
59
+ * @returns The point at that length. A degenerate zero-length curve returns
60
+ * its start for every `t`.
61
+ * @throws {@link WireConfigError} when any curve point is non-finite or `t`
62
+ * is outside `[0, 1]`.
63
+ */
64
+ export declare function pointOnBezier(curve: CubicBezier, t: number): Point;
65
+ /**
66
+ * Closest point on a polyline to `point`, parameterized by arc length.
67
+ * Exact: every segment is tested analytically.
68
+ *
69
+ * @param points - Two or more finite points.
70
+ * @param point - The query point.
71
+ * @param options - `threshold` (default 4): the maximum distance for a hit.
72
+ * @returns The length-normalized `t` and `distance` of the closest point, or
73
+ * `null` when the closest point is farther away than `threshold`.
74
+ * @throws {@link WireConfigError} when `points` has fewer than two finite
75
+ * points, `point` is non-finite, or `threshold` is not finite positive.
76
+ */
77
+ export declare function hitPolyline(points: readonly Point[], point: Point, options?: HitOptions): HitResult | null;
78
+ /**
79
+ * Closest point on a cubic bezier to `point`, parameterized by arc length.
80
+ * Uses the same 64-sample lookup table as {@link pointOnBezier}: the nearest
81
+ * sample is refined by ternary search on the true curve, so the reported
82
+ * `distance` is accurate; `t` carries the table's arc-length tolerance.
83
+ *
84
+ * @param curve - A cubic bezier with finite endpoints and control points.
85
+ * @param point - The query point.
86
+ * @param options - `threshold` (default 4): the maximum distance for a hit.
87
+ * @returns The length-normalized `t` and `distance` of the closest point, or
88
+ * `null` when the closest point is farther away than `threshold`.
89
+ * @throws {@link WireConfigError} when any curve point is non-finite,
90
+ * `point` is non-finite, or `threshold` is not finite positive.
91
+ */
92
+ export declare function hitBezier(curve: CubicBezier, point: Point, options?: HitOptions): HitResult | null;
93
+ //# sourceMappingURL=edit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit.d.ts","sourceRoot":"","sources":["../src/edit.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAoBrD,qCAAqC;AACrC,MAAM,WAAW,gBAAgB;IAC/B,8DAA8D;IAC9D,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,qEAAqE;IACrE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,wCAAwC;AACxC,MAAM,WAAW,UAAU;IACzB,4DAA4D;IAC5D,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,iCAAiC;AACjC,MAAM,WAAW,SAAS;IACxB,iFAAiF;IACjF,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,sEAAsE;IACtE,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;CACpB;AAoBD;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,GAAE,gBAAqB,GAAG,KAAK,EAAE,CAwB/F;AA0FD;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,EA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