@litfamily/litgravity 0.1.1 → 0.1.2

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 (246) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +17 -3
  3. package/README_ko-KR.md +17 -3
  4. package/package.json +1 -1
  5. package/plugin/hooks/lib/lanes.mjs +65 -0
  6. package/plugin/hooks/lib/motion.mjs +34 -5
  7. package/plugin/hooks/lib/reentry.mjs +7 -0
  8. package/plugin/hooks/lib/rewind/brief.mjs +332 -0
  9. package/plugin/hooks/lib/rewind/collect.mjs +328 -0
  10. package/plugin/hooks/lib/rewind/gauge.mjs +55 -0
  11. package/plugin/hooks/lib/rewind/next-step.mjs +77 -0
  12. package/plugin/hooks/lib/rewind/redact.mjs +60 -0
  13. package/plugin/hooks/lib/rewind/transcripts.mjs +211 -0
  14. package/plugin/hooks/lib/routes.mjs +79 -5
  15. package/plugin/hooks/lib/workflow-stack.mjs +213 -0
  16. package/plugin/hooks/post-tool-use.mjs +37 -1
  17. package/plugin/hooks/pre-invocation.mjs +91 -2
  18. package/plugin/hooks/session-start.mjs +26 -3
  19. package/plugin/hooks/stop.mjs +22 -2
  20. package/plugin/hud/looks.mjs +29 -0
  21. package/plugin/hud/render.mjs +234 -12
  22. package/plugin/plugin.json +1 -1
  23. package/plugin/skills/frontend-ui-ux/SKILL.md +12 -1
  24. package/plugin/skills/frontend-ui-ux/kit/components.css +417 -0
  25. package/plugin/skills/frontend-ui-ux/kit/fonts.json +22 -0
  26. package/plugin/skills/frontend-ui-ux/kit/layouts/landing.html +164 -0
  27. package/plugin/skills/frontend-ui-ux/kit/layouts/settings.html +114 -0
  28. package/plugin/skills/frontend-ui-ux/kit/layouts/workspace.html +115 -0
  29. package/plugin/skills/frontend-ui-ux/kit/styles/computational-brutalism.json +227 -0
  30. package/plugin/skills/frontend-ui-ux/kit/styles/field-notes.json +205 -0
  31. package/plugin/skills/frontend-ui-ux/kit/styles/graphite.json +200 -0
  32. package/plugin/skills/frontend-ui-ux/kit/styles/meadow.json +199 -0
  33. package/plugin/skills/frontend-ui-ux/kit/styles/neo-brutalism.json +222 -0
  34. package/plugin/skills/frontend-ui-ux/kit/ui.js +226 -0
  35. package/plugin/skills/frontend-ui-ux/references/house-styles.md +234 -0
  36. package/plugin/skills/frontend-ui-ux/scripts/design-lock.mjs +400 -0
  37. package/plugin/skills/frontend-ui-ux/scripts/interface-probe-lock.mjs +196 -0
  38. package/plugin/skills/frontend-ui-ux/scripts/interface-probe-page.mjs +30 -9
  39. package/plugin/skills/frontend-ui-ux/scripts/interface-probe-rules.mjs +20 -8
  40. package/plugin/skills/frontend-ui-ux/scripts/interface-probe.mjs +42 -9
  41. package/plugin/skills/frontend-ui-ux/scripts/pixel-art.mjs +60 -0
  42. package/plugin/skills/lit-adhd/SKILL.md +157 -0
  43. package/plugin/skills/lit-adhd/references/card-examples.md +101 -0
  44. package/plugin/skills/lit-adhd/references/status-taxonomy.md +32 -0
  45. package/plugin/skills/lit-adhd/scripts/rewind.mjs +5 -0
  46. package/plugin/skills/lit-diagram-drawer/SKILL.md +5 -3
  47. package/plugin/skills/lit-diagram-drawer/references/drawing-rules.md +1 -1
  48. package/plugin/skills/lit-diagram-drawer/references/style-guide.md +21 -12
  49. package/plugin/skills/lit-diagram-drawer/references/type-architecture.md +3 -3
  50. package/plugin/skills/lit-diagram-drawer/references/type-dependency.md +1 -1
  51. package/plugin/skills/lit-diagram-drawer/references/type-tree.md +3 -3
  52. package/plugin/skills/lit-diagram-drawer/references/type-wardley.md +1 -1
  53. package/plugin/skills/lit-diagram-drawer/scripts/diagram-craft-rules.mjs +1 -2
  54. package/plugin/skills/lit-diagram-drawer/scripts/visual-quality.mjs +1 -2
  55. package/plugin/skills/lit-docx/SKILL.md +3 -3
  56. package/plugin/skills/lit-handoff/SKILL.md +1 -0
  57. package/plugin/skills/lit-hijack/SKILL.md +263 -0
  58. package/plugin/skills/lit-hijack/references/capture-and-provenance.md +100 -0
  59. package/plugin/skills/lit-hijack/references/clean-room-build.md +75 -0
  60. package/plugin/skills/lit-hijack/references/distil-and-licence.md +67 -0
  61. package/plugin/skills/lit-hijack/references/gap-and-port-design.md +100 -0
  62. package/plugin/skills/lit-hijack/references/parity-and-report.md +85 -0
  63. package/plugin/skills/lit-hijack/scripts/hijack.mjs +953 -0
  64. package/plugin/skills/lit-plan/SKILL.md +1 -1
  65. package/plugin/skills/lit-pptx/SKILL.md +8 -8
  66. package/plugin/skills/lit-pptx/fonts/jetbrains-mono/JetBrainsMono-Bold.ttf +0 -0
  67. package/plugin/skills/lit-pptx/fonts/jetbrains-mono/JetBrainsMono-Regular.ttf +0 -0
  68. package/plugin/skills/lit-pptx/fonts/jetbrains-mono/OFL.txt +93 -0
  69. package/plugin/skills/lit-pptx/fonts/provenance.json +32 -1
  70. package/plugin/skills/lit-pptx/references/agents/analyst.md +34 -56
  71. package/plugin/skills/lit-pptx/references/agents/antithesis.md +49 -67
  72. package/plugin/skills/lit-pptx/references/agents/architect.md +38 -51
  73. package/plugin/skills/lit-pptx/references/agents/critic.md +25 -41
  74. package/plugin/skills/lit-pptx/references/agents/synthesizer.md +37 -46
  75. package/plugin/skills/lit-pptx/references/agents/thesis.md +29 -49
  76. package/plugin/skills/lit-pptx/references/anti-slop-checklist.md +119 -145
  77. package/plugin/skills/lit-pptx/references/authoring-guide.md +187 -278
  78. package/plugin/skills/lit-pptx/references/consensus-protocol.md +99 -97
  79. package/plugin/skills/lit-pptx/references/data-display-cookbook.md +36 -137
  80. package/plugin/skills/lit-pptx/references/density-and-fill.md +147 -216
  81. package/plugin/skills/lit-pptx/references/design-system-azure.md +56 -85
  82. package/plugin/skills/lit-pptx/references/direction-step.md +134 -176
  83. package/plugin/skills/lit-pptx/references/examples/01-pitch-signal-ko.md +114 -115
  84. package/plugin/skills/lit-pptx/references/examples/02-pitch-studio-ko.md +66 -66
  85. package/plugin/skills/lit-pptx/references/examples/03-business-review-ledger-ko.md +63 -63
  86. package/plugin/skills/lit-pptx/references/examples/04-business-review-night-en.md +96 -96
  87. package/plugin/skills/lit-pptx/references/examples/05-research-talk-paper-en.md +81 -81
  88. package/plugin/skills/lit-pptx/references/examples/06-lecture-chalk-ko.md +103 -103
  89. package/plugin/skills/lit-pptx/references/examples/07-data-review-night-en.md +100 -98
  90. package/plugin/skills/lit-pptx/references/examples/08-image-led-atlas-ko.md +76 -76
  91. package/plugin/skills/lit-pptx/references/examples/09-briefing-gazette-ko.md +96 -96
  92. package/plugin/skills/lit-pptx/references/examples/10-status-update-ledger-ko.md +80 -80
  93. package/plugin/skills/lit-pptx/references/examples/11-portfolio-studio-en.md +72 -72
  94. package/plugin/skills/lit-pptx/references/examples/12-research-tutorial-chalk-en.md +95 -95
  95. package/plugin/skills/lit-pptx/references/examples/13-engineering-review-computational-brutalism-ko.md +173 -0
  96. package/plugin/skills/lit-pptx/references/examples/14-launch-pitch-neo-brutalism-ko.md +154 -0
  97. package/plugin/skills/lit-pptx/references/examples/15-design-lecture-bauhaus-ko.md +139 -0
  98. package/plugin/skills/lit-pptx/references/examples/16-community-programme-riso-ko.md +158 -0
  99. package/plugin/skills/lit-pptx/references/examples/17-public-data-briefing-newsroom-ko.md +179 -0
  100. package/plugin/skills/lit-pptx/references/interview-protocol.md +83 -102
  101. package/plugin/skills/lit-pptx/references/layout-families.md +161 -265
  102. package/plugin/skills/lit-pptx/references/stages/autopilot.md +30 -70
  103. package/plugin/skills/lit-pptx/references/stages/interview.md +36 -68
  104. package/plugin/skills/lit-pptx/references/stages/plan.md +26 -67
  105. package/plugin/skills/lit-pptx/references/title-treatments.md +93 -145
  106. package/plugin/skills/lit-pptx/references/tonalities/atlas.md +95 -108
  107. package/plugin/skills/lit-pptx/references/tonalities/bauhaus.md +140 -0
  108. package/plugin/skills/lit-pptx/references/tonalities/chalk.md +107 -122
  109. package/plugin/skills/lit-pptx/references/tonalities/computational-brutalism.md +150 -0
  110. package/plugin/skills/lit-pptx/references/tonalities/gazette.md +87 -111
  111. package/plugin/skills/lit-pptx/references/tonalities/ledger.md +96 -125
  112. package/plugin/skills/lit-pptx/references/tonalities/neo-brutalism.md +161 -0
  113. package/plugin/skills/lit-pptx/references/tonalities/newsroom.md +144 -0
  114. package/plugin/skills/lit-pptx/references/tonalities/night.md +104 -131
  115. package/plugin/skills/lit-pptx/references/tonalities/paper.md +114 -130
  116. package/plugin/skills/lit-pptx/references/tonalities/riso.md +137 -0
  117. package/plugin/skills/lit-pptx/references/tonalities/signal.md +96 -103
  118. package/plugin/skills/lit-pptx/references/tonalities/studio.md +81 -112
  119. package/plugin/skills/lit-pptx/scripts/compile-deck.js +4 -6
  120. package/plugin/skills/lit-pptx/scripts/craft_extras.py +135 -91
  121. package/plugin/skills/lit-pptx/scripts/deck_craft.py +1 -1
  122. package/plugin/skills/lit-pptx/scripts/embed_fonts.py +13 -1
  123. package/plugin/skills/lit-pptx/scripts/layout_inventory.py +1 -1
  124. package/plugin/skills/lit-pptx/scripts/learn_template.py +1 -1
  125. package/plugin/skills/lit-pptx/scripts/lib/grid-resolver.js +101 -110
  126. package/plugin/skills/lit-pptx/scripts/lib/layout-resolver.js +9 -15
  127. package/plugin/skills/lit-pptx/scripts/lib/render-pack.js +370 -563
  128. package/plugin/skills/lit-pptx/scripts/lib/slide-grid.js +104 -0
  129. package/plugin/skills/lit-pptx/scripts/lib/template-registry.js +57 -6
  130. package/plugin/skills/lit-pptx/scripts/qa_deck.py +6 -6
  131. package/plugin/skills/lit-pptx/scripts/render_pages.py +31 -0
  132. package/plugin/skills/lit-pptx/scripts/validate_pptx.py +1 -1
  133. package/plugin/skills/lit-pptx/specs/markdown-slide-spec-v1.md +167 -191
  134. package/plugin/skills/lit-pptx/specs/markdown-slide-spec-v2.md +117 -166
  135. package/plugin/skills/lit-pptx/specs/template-enrollment-contract.md +307 -330
  136. package/plugin/skills/lit-pptx/templates/tonalities/atlas/pack.yaml +1 -1
  137. package/plugin/skills/lit-pptx/templates/tonalities/bauhaus/pack.yaml +98 -0
  138. package/plugin/skills/lit-pptx/templates/tonalities/chalk/pack.yaml +1 -1
  139. package/plugin/skills/lit-pptx/templates/tonalities/computational-brutalism/pack.yaml +99 -0
  140. package/plugin/skills/lit-pptx/templates/tonalities/gazette/pack.yaml +1 -1
  141. package/plugin/skills/lit-pptx/templates/tonalities/ledger/pack.yaml +1 -1
  142. package/plugin/skills/lit-pptx/templates/tonalities/neo-brutalism/pack.yaml +107 -0
  143. package/plugin/skills/lit-pptx/templates/tonalities/newsroom/pack.yaml +99 -0
  144. package/plugin/skills/lit-pptx/templates/tonalities/night/pack.yaml +1 -1
  145. package/plugin/skills/lit-pptx/templates/tonalities/paper/pack.yaml +1 -1
  146. package/plugin/skills/lit-pptx/templates/tonalities/riso/pack.yaml +99 -0
  147. package/plugin/skills/lit-pptx/templates/tonalities/signal/pack.yaml +2 -2
  148. package/plugin/skills/lit-pptx/templates/tonalities/studio/pack.yaml +1 -1
  149. package/plugin/skills/lit-schematic/SKILL.md +114 -0
  150. package/plugin/skills/lit-schematic/examples/iso-heat-exchanger/build.mjs +38 -0
  151. package/plugin/skills/lit-schematic/examples/iso-heat-exchanger/scene.mjs +133 -0
  152. package/plugin/skills/lit-schematic/examples/journal-adsorption-rig/build.mjs +87 -0
  153. package/plugin/skills/lit-schematic/examples/pid-dosing-skid/build.mjs +85 -0
  154. package/plugin/skills/lit-schematic/references/cutaway-outputs.md +70 -0
  155. package/plugin/skills/lit-schematic/references/isometric-craft.md +101 -0
  156. package/plugin/skills/lit-schematic/references/journal-figures.md +72 -0
  157. package/plugin/skills/lit-schematic/references/kit-api.md +134 -0
  158. package/plugin/skills/lit-schematic/references/pfd-and-layout.md +53 -0
  159. package/plugin/skills/lit-schematic/references/pid-drafting.md +103 -0
  160. package/plugin/skills/lit-schematic/references/pid-symbols.md +163 -0
  161. package/plugin/skills/lit-schematic/references/verification.md +100 -0
  162. package/plugin/skills/lit-schematic/scripts/doctor.mjs +57 -0
  163. package/plugin/skills/lit-schematic/scripts/lib/figure.mjs +81 -0
  164. package/plugin/skills/lit-schematic/scripts/lib/fonts.mjs +27 -0
  165. package/plugin/skills/lit-schematic/scripts/lib/geom.mjs +113 -0
  166. package/plugin/skills/lit-schematic/scripts/lib/interactive.mjs +292 -0
  167. package/plugin/skills/lit-schematic/scripts/lib/isa.mjs +115 -0
  168. package/plugin/skills/lit-schematic/scripts/lib/iso.mjs +719 -0
  169. package/plugin/skills/lit-schematic/scripts/lib/legend-sheet.mjs +162 -0
  170. package/plugin/skills/lit-schematic/scripts/lib/pid.mjs +937 -0
  171. package/plugin/skills/lit-schematic/scripts/lib/png.mjs +132 -0
  172. package/plugin/skills/lit-schematic/scripts/lib/qa-core.mjs +658 -0
  173. package/plugin/skills/lit-schematic/scripts/lib/route.mjs +218 -0
  174. package/plugin/skills/lit-schematic/scripts/lib/sheet.mjs +890 -0
  175. package/plugin/skills/lit-schematic/scripts/lib/svg.mjs +82 -0
  176. package/plugin/skills/lit-schematic/scripts/lib/theme.mjs +115 -0
  177. package/plugin/skills/lit-schematic/scripts/motion.mjs +129 -0
  178. package/plugin/skills/lit-schematic/scripts/qa.mjs +92 -0
  179. package/plugin/skills/lit-schematic/scripts/render.mjs +233 -0
  180. package/plugin/skills/lit-schematic/scripts/scaffold.mjs +60 -0
  181. package/plugin/skills/lit-scientific-visualization/SKILL.md +17 -6
  182. package/plugin/skills/lit-typographic-motion/SKILL.md +58 -48
  183. package/plugin/skills/lit-typographic-motion/engine/THIRD_PARTY_NOTICES +13 -2
  184. package/plugin/skills/lit-typographic-motion/engine/blender/runner.py +218 -0
  185. package/plugin/skills/lit-typographic-motion/engine/core/presets.mjs +1 -1
  186. package/plugin/skills/lit-typographic-motion/engine/node/analysis.mjs +65 -1
  187. package/plugin/skills/lit-typographic-motion/engine/node/assets.mjs +298 -0
  188. package/plugin/skills/lit-typographic-motion/engine/node/blender.mjs +143 -0
  189. package/plugin/skills/lit-typographic-motion/engine/node/board.mjs +136 -0
  190. package/plugin/skills/lit-typographic-motion/engine/node/brief.mjs +7 -1
  191. package/plugin/skills/lit-typographic-motion/engine/node/chrome.mjs +9 -12
  192. package/plugin/skills/lit-typographic-motion/engine/node/cover-assets.mjs +5 -3
  193. package/plugin/skills/lit-typographic-motion/engine/node/cover-capture.mjs +7 -6
  194. package/plugin/skills/lit-typographic-motion/engine/node/cover-treatment.mjs +37 -4
  195. package/plugin/skills/lit-typographic-motion/engine/node/cover.mjs +19 -8
  196. package/plugin/skills/lit-typographic-motion/engine/node/done.mjs +45 -6
  197. package/plugin/skills/lit-typographic-motion/engine/node/frame-worker.mjs +7 -5
  198. package/plugin/skills/lit-typographic-motion/engine/node/gallery.mjs +121 -0
  199. package/plugin/skills/lit-typographic-motion/engine/node/host.mjs +34 -0
  200. package/plugin/skills/lit-typographic-motion/engine/node/house.mjs +44 -0
  201. package/plugin/skills/lit-typographic-motion/engine/node/look.mjs +34 -20
  202. package/plugin/skills/lit-typographic-motion/engine/node/pipeline.mjs +19 -9
  203. package/plugin/skills/lit-typographic-motion/engine/node/scaffold.mjs +210 -0
  204. package/plugin/skills/lit-typographic-motion/engine/node/score.mjs +639 -0
  205. package/plugin/skills/lit-typographic-motion/engine/node/sound-cli.mjs +21 -4
  206. package/plugin/skills/lit-typographic-motion/engine/node/sound.mjs +127 -29
  207. package/plugin/skills/lit-typographic-motion/engine/node/stage-capture.mjs +7 -6
  208. package/plugin/skills/lit-typographic-motion/engine/node/stage-gate.mjs +59 -5
  209. package/plugin/skills/lit-typographic-motion/engine/node/stage-qa.mjs +141 -9
  210. package/plugin/skills/lit-typographic-motion/engine/node/stage-serve.mjs +28 -5
  211. package/plugin/skills/lit-typographic-motion/engine/node/stage.mjs +82 -14
  212. package/plugin/skills/lit-typographic-motion/engine/node/styles.mjs +213 -0
  213. package/plugin/skills/lit-typographic-motion/engine/node/treatment.mjs +311 -10
  214. package/plugin/skills/lit-typographic-motion/engine/stage/cover-init.js +14 -2
  215. package/plugin/skills/lit-typographic-motion/engine/stage/init.js +11 -1
  216. package/plugin/skills/lit-typographic-motion/engine/stage/qa.js +6 -1
  217. package/plugin/skills/lit-typographic-motion/engine/stage/stage-kit.js +83 -0
  218. package/plugin/skills/lit-typographic-motion/engine/vendor/three/LICENSE +21 -0
  219. package/plugin/skills/lit-typographic-motion/engine/vendor/three/addons/environments/RoomEnvironment.js +144 -0
  220. package/plugin/skills/lit-typographic-motion/engine/vendor/three/addons/geometries/RoundedBoxGeometry.js +155 -0
  221. package/plugin/skills/lit-typographic-motion/engine/vendor/three/three.module.js +54571 -0
  222. package/plugin/skills/lit-typographic-motion/recipes/hero-3d/expected.json +69 -0
  223. package/plugin/skills/lit-typographic-motion/recipes/hero-3d/index.html +139 -0
  224. package/plugin/skills/lit-typographic-motion/recipes/orbit-loop/expected.json +6 -4
  225. package/plugin/skills/lit-typographic-motion/recipes/painted-light/expected.json +4 -2
  226. package/plugin/skills/lit-typographic-motion/recipes/weight-tide/expected.json +7 -5
  227. package/plugin/skills/lit-typographic-motion/references/cover.md +394 -443
  228. package/plugin/skills/lit-typographic-motion/references/craft-loop.md +57 -87
  229. package/plugin/skills/lit-typographic-motion/references/depth.md +122 -0
  230. package/plugin/skills/lit-typographic-motion/references/look.md +94 -129
  231. package/plugin/skills/lit-typographic-motion/references/runtime.md +58 -91
  232. package/plugin/skills/lit-typographic-motion/references/sound.md +130 -99
  233. package/plugin/skills/lit-typographic-motion/references/stage.md +190 -215
  234. package/plugin/skills/lit-typographic-motion/references/studio.md +245 -0
  235. package/plugin/skills/lit-typographic-motion/references/style-bibles.md +108 -97
  236. package/plugin/skills/lit-typographic-motion/references/treatment.md +191 -143
  237. package/plugin/skills/lit-typographic-motion/scripts/motion-doctor.mjs +17 -5
  238. package/plugin/skills/lit-typographic-motion/scripts/motion.mjs +47 -7
  239. package/plugin/skills/lit-typographic-motion/styles/paper.json +41 -0
  240. package/plugin/skills/lit-typographic-motion/styles/swiss-signal.json +41 -0
  241. package/plugin/skills/lit-typographic-motion/styles/terminalcore.json +42 -0
  242. package/plugin/skills/lit-typographic-motion/styles/tidal.json +41 -0
  243. package/plugin/skills/litresearch/SKILL.md +1 -0
  244. package/plugin/skills/readme-studio/SKILL.md +2 -0
  245. package/plugin/skills/review-work/SKILL.md +1 -1
  246. package/plugin/skills/start-work/SKILL.md +1 -1
@@ -0,0 +1,218 @@
1
+ // Orthogonal line routing for P&IDs, PFDs and 2D apparatus panels.
2
+ //
3
+ // Drafting rules it encodes: a line leaves its port straight out along the port's normal
4
+ // before it turns; it never enters a symbol other than at its own ports; it turns as little
5
+ // as possible; it may cross another line only at a right angle on a straight run, never turn
6
+ // on top of another line and never run along one. A failed search throws instead of drawing
7
+ // through a symbol, so the caller moves a symbol or gives waypoints.
8
+
9
+ import { EPS, dist, pointIn, simplify } from "./geom.mjs";
10
+
11
+ const DIRS = [[1, 0], [0, 1], [-1, 0], [0, -1]];
12
+ const dirIndex = ([dx, dy]) => DIRS.findIndex(([x, y]) => Math.abs(x - dx) < EPS && Math.abs(y - dy) < EPS);
13
+
14
+ class Heap {
15
+ constructor() { this.items = []; }
16
+ get size() { return this.items.length; }
17
+ push(item) {
18
+ const items = this.items;
19
+ items.push(item);
20
+ let i = items.length - 1;
21
+ while (i > 0) {
22
+ const parent = (i - 1) >> 1;
23
+ if (less(items[parent], items[i])) break;
24
+ [items[parent], items[i]] = [items[i], items[parent]];
25
+ i = parent;
26
+ }
27
+ }
28
+ pop() {
29
+ const items = this.items;
30
+ const top = items[0];
31
+ const last = items.pop();
32
+ if (items.length) {
33
+ items[0] = last;
34
+ let i = 0;
35
+ for (;;) {
36
+ const l = 2 * i + 1;
37
+ const r = l + 1;
38
+ let m = i;
39
+ if (l < items.length && less(items[l], items[m])) m = l;
40
+ if (r < items.length && less(items[r], items[m])) m = r;
41
+ if (m === i) break;
42
+ [items[m], items[i]] = [items[i], items[m]];
43
+ i = m;
44
+ }
45
+ }
46
+ return top;
47
+ }
48
+ }
49
+ const less = (a, b) => (a.f !== b.f ? a.f < b.f : a.seq < b.seq);
50
+
51
+ function axisValues(lo, hi, step, extra) {
52
+ const values = new Set();
53
+ const first = Math.ceil(lo / step) * step;
54
+ for (let v = first; v <= hi + EPS; v += step) values.add(Math.round(v * 1000) / 1000);
55
+ for (const v of extra) if (v >= lo - EPS && v <= hi + EPS) values.add(Math.round(v * 1000) / 1000);
56
+ return [...values].sort((a, b) => a - b);
57
+ }
58
+
59
+ // Existing lines as axis-aligned runs, so a candidate move can be classified as a clean
60
+ // crossing, an overlap or a turn on a line.
61
+ function occupancy(lines) {
62
+ const horizontal = [];
63
+ const vertical = [];
64
+ for (const points of lines) {
65
+ for (let i = 1; i < points.length; i++) {
66
+ const [a, b] = [points[i - 1], points[i]];
67
+ if (Math.abs(a[1] - b[1]) < EPS) horizontal.push({ y: a[1], x0: Math.min(a[0], b[0]), x1: Math.max(a[0], b[0]) });
68
+ else if (Math.abs(a[0] - b[0]) < EPS) vertical.push({ x: a[0], y0: Math.min(a[1], b[1]), y1: Math.max(a[1], b[1]) });
69
+ }
70
+ }
71
+ return {
72
+ onHorizontal: ([x, y], margin = 0) => horizontal.some((s) => Math.abs(s.y - y) < margin + EPS && x >= s.x0 - EPS && x <= s.x1 + EPS),
73
+ onVertical: ([x, y], margin = 0) => vertical.some((s) => Math.abs(s.x - x) < margin + EPS && y >= s.y0 - EPS && y <= s.y1 + EPS),
74
+ };
75
+ }
76
+
77
+ export function routeOrthogonal({
78
+ from,
79
+ to,
80
+ obstacles = [],
81
+ lines = [],
82
+ bounds,
83
+ grid = 2.5,
84
+ clearance = 2,
85
+ stub = 5,
86
+ bendCost = 9,
87
+ crossCost = 4,
88
+ nearCost = 3,
89
+ exempt = [],
90
+ }) {
91
+ if (!bounds) throw new Error("routeOrthogonal: bounds are required");
92
+ const startDir = dirIndex(from.dir ?? [1, 0]);
93
+ const endDir = dirIndex(to.dir ?? [-1, 0]);
94
+ if (startDir < 0 || endDir < 0) throw new Error("routeOrthogonal: port directions must be axis-aligned unit vectors");
95
+ const inflated = obstacles.map((box) => [box[0] - clearance, box[1] - clearance, box[2] + clearance, box[3] + clearance]);
96
+ const blockedPoint = (p) => inflated.some((box) => pointIn(p, box, EPS));
97
+ // The stub runs out along the port normal until it clears every symbol's clearance zone,
98
+ // its own included: a nozzle on a curved head sits inside its vessel's bounding box.
99
+ const stubEnd = (port, d, label) => {
100
+ for (let k = stub; k <= stub + 80; k += 0.5) {
101
+ const p = [port.x + DIRS[d][0] * k, port.y + DIRS[d][1] * k];
102
+ if (!blockedPoint(p)) return p;
103
+ }
104
+ throw new Error(`routeOrthogonal: the ${label} port at (${port.x}, ${port.y}) faces into another symbol; turn the port or move the symbol`);
105
+ };
106
+ // Two ports that face each other on one axis with nothing between them join straight.
107
+ if ((startDir + 2) % 4 === endDir) {
108
+ const along = [to.x - from.x, to.y - from.y];
109
+ const straight = DIRS[startDir][0] === 0 ? Math.abs(along[0]) < EPS : Math.abs(along[1]) < EPS;
110
+ const ahead = along[0] * DIRS[startDir][0] + along[1] * DIRS[startDir][1] > 0;
111
+ if (straight && ahead) {
112
+ let clear = true;
113
+ const length = Math.hypot(along[0], along[1]);
114
+ for (let k = 0.5; k < length - 0.5 && clear; k += 0.5) {
115
+ const p = [from.x + DIRS[startDir][0] * k, from.y + DIRS[startDir][1] * k];
116
+ if (obstacles.some((box) => !exempt.includes(box) && pointIn(p, box, EPS))) clear = false;
117
+ }
118
+ if (clear) return [[from.x, from.y], [to.x, to.y]];
119
+ }
120
+ }
121
+ const s = stubEnd(from, startDir, "start");
122
+ const t = stubEnd(to, endDir, "end");
123
+ const xs = axisValues(bounds[0], bounds[2], grid, [s[0], t[0], from.x, to.x]);
124
+ const ys = axisValues(bounds[1], bounds[3], grid, [s[1], t[1], from.y, to.y]);
125
+ const xi = new Map(xs.map((v, i) => [v, i]));
126
+ const yi = new Map(ys.map((v, i) => [v, i]));
127
+ const key = (ix, iy) => iy * xs.length + ix;
128
+ const occ = occupancy(lines);
129
+ const sx = xi.get(Math.round(s[0] * 1000) / 1000);
130
+ const sy = yi.get(Math.round(s[1] * 1000) / 1000);
131
+ const tx = xi.get(Math.round(t[0] * 1000) / 1000);
132
+ const ty = yi.get(Math.round(t[1] * 1000) / 1000);
133
+ const arrive = (endDir + 2) % 4;
134
+ const heap = new Heap();
135
+ const best = new Map();
136
+ const came = new Map();
137
+ let seq = 0;
138
+ const h = (ix, iy) => Math.abs(xs[ix] - t[0]) + Math.abs(ys[iy] - t[1]);
139
+ heap.push({ ix: sx, iy: sy, d: startDir, g: 0, f: h(sx, sy), seq: seq++ });
140
+ best.set(`${key(sx, sy)}:${startDir}`, 0);
141
+ let goal = null;
142
+ while (heap.size) {
143
+ const cur = heap.pop();
144
+ const stateKey = `${key(cur.ix, cur.iy)}:${cur.d}`;
145
+ if (cur.g > (best.get(stateKey) ?? Infinity) + EPS) continue;
146
+ if (cur.ix === tx && cur.iy === ty) {
147
+ const finalCost = cur.g + (cur.d === arrive ? 0 : bendCost);
148
+ if (!goal || finalCost < goal.cost) goal = { state: stateKey, cost: finalCost };
149
+ if (cur.d === arrive) break;
150
+ continue;
151
+ }
152
+ const here = [xs[cur.ix], ys[cur.iy]];
153
+ for (let d = 0; d < 4; d++) {
154
+ if (d === (cur.d + 2) % 4) continue;
155
+ const nx = cur.ix + DIRS[d][0];
156
+ const ny = cur.iy + DIRS[d][1];
157
+ if (nx < 0 || ny < 0 || nx >= xs.length || ny >= ys.length) continue;
158
+ const next = [xs[nx], ys[ny]];
159
+ const mid = [(here[0] + next[0]) / 2, (here[1] + next[1]) / 2];
160
+ if (blockedPoint(next) || blockedPoint(mid)) continue;
161
+ const horizontalMove = d % 2 === 0;
162
+ // Running along an existing line, or turning on top of one, is never allowed.
163
+ if (horizontalMove ? occ.onHorizontal(mid) : occ.onVertical(mid)) continue;
164
+ const turning = d !== cur.d;
165
+ const isEndpoint = (cur.ix === sx && cur.iy === sy);
166
+ if (turning && !isEndpoint && (occ.onHorizontal(here) || occ.onVertical(here))) continue;
167
+ let step = dist(here, next);
168
+ if (horizontalMove ? occ.onVertical(next) : occ.onHorizontal(next)) {
169
+ const atGoal = nx === tx && ny === ty;
170
+ if (atGoal) continue;
171
+ step += crossCost;
172
+ }
173
+ if (horizontalMove ? occ.onHorizontal(mid, grid * 0.99) : occ.onVertical(mid, grid * 0.99)) step += nearCost;
174
+ const g = cur.g + step + (turning ? bendCost : 0);
175
+ const nextKey = `${key(nx, ny)}:${d}`;
176
+ if (g + EPS < (best.get(nextKey) ?? Infinity)) {
177
+ best.set(nextKey, g);
178
+ came.set(nextKey, stateKey);
179
+ heap.push({ ix: nx, iy: ny, d, g, f: g + h(nx, ny), seq: seq++ });
180
+ }
181
+ }
182
+ }
183
+ if (!goal) {
184
+ throw new Error(`routeOrthogonal: no clear route from (${from.x}, ${from.y}) to (${to.x}, ${to.y}); move a symbol, widen the gap, or pass waypoints`);
185
+ }
186
+ const cells = [];
187
+ for (let k = goal.state; k; k = came.get(k)) {
188
+ const index = Number(k.split(":")[0]);
189
+ cells.push([xs[index % xs.length], ys[Math.floor(index / xs.length)]]);
190
+ if (!came.has(k)) break;
191
+ }
192
+ cells.reverse();
193
+ return simplify([[from.x, from.y], ...cells, [to.x, to.y]]);
194
+ }
195
+
196
+ // Checks a hand-given polyline: every leg axis-aligned and the first and last legs on the
197
+ // port normals. Returns the simplified corners.
198
+ export function orthogonal(points, { from, to } = {}) {
199
+ const out = simplify(points);
200
+ for (let i = 1; i < out.length; i++) {
201
+ const [a, b] = [out[i - 1], out[i]];
202
+ if (Math.abs(a[0] - b[0]) > EPS && Math.abs(a[1] - b[1]) > EPS) {
203
+ throw new Error(`orthogonal: leg ${i} from (${a}) to (${b}) is diagonal; P&ID lines run horizontally or vertically`);
204
+ }
205
+ }
206
+ const check = (port, a, b, label) => {
207
+ if (!port?.dir) return;
208
+ const d = [Math.sign(b[0] - a[0]), Math.sign(b[1] - a[1])];
209
+ if (d[0] !== Math.sign(port.dir[0]) || d[1] !== Math.sign(port.dir[1])) {
210
+ throw new Error(`orthogonal: the ${label} leg must leave its port along the port's normal`);
211
+ }
212
+ };
213
+ if (out.length >= 2) {
214
+ check(from, out[0], out[1], "first");
215
+ check(to, out.at(-1), out.at(-2), "last");
216
+ }
217
+ return out;
218
+ }