@johnoscott/isometric-engine 0.0.0-stage → 0.9.0
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.
- package/EDITOR-GUIDE.md +317 -0
- package/LICENSE +21 -0
- package/LICENSES/ajv.txt +22 -0
- package/LICENSES/lucide-static.txt +43 -0
- package/LICENSES/yaml.txt +13 -0
- package/README.md +62 -2
- package/assets/app.css +265 -0
- package/assets/default-theme.yaml +99 -0
- package/assets/icons.svg +1 -0
- package/assets/marks.css +35 -0
- package/assets/skin.css +181 -0
- package/iso-editor.d.ts +4 -0
- package/iso-editor.js +550 -0
- package/iso-render.d.ts +3 -0
- package/iso-render.js +388 -0
- package/iso-systems.d.ts +4 -0
- package/iso-systems.js +389 -0
- package/js/iso-core/compile-schema.js +22 -0
- package/js/iso-core/depth.js +55 -0
- package/js/iso-core/errors.js +65 -0
- package/js/iso-core/glyphs.js +100 -0
- package/js/iso-core/index.js +55 -0
- package/js/iso-core/kernel.js +158 -0
- package/js/iso-core/metrics.json +1 -0
- package/js/iso-core/package.json +6 -0
- package/js/iso-core/parse.js +79 -0
- package/js/iso-core/schema-errors.js +170 -0
- package/js/iso-core/shims/crypto.js +125 -0
- package/js/iso-core/shims/path.js +49 -0
- package/js/iso-core/svg-dom.js +144 -0
- package/js/iso-core/theme.js +48 -0
- package/js/iso-core/vdom.js +48 -0
- package/js/iso-editor/adapter.js +0 -0
- package/js/iso-editor/clipboard.js +152 -0
- package/js/iso-editor/commands.js +173 -0
- package/js/iso-editor/complete.js +55 -0
- package/js/iso-editor/doc.js +182 -0
- package/js/iso-editor/index.js +105 -0
- package/js/iso-editor/model.js +210 -0
- package/js/iso-editor/outline.js +111 -0
- package/js/iso-editor/package.json +6 -0
- package/js/iso-editor/path.js +19 -0
- package/js/iso-editor/providers.js +96 -0
- package/js/iso-editor/schema-nav.js +117 -0
- package/js/iso-editor/splice.js +441 -0
- package/js/iso-editor/storage-keys.js +6 -0
- package/js/iso-editor/ui/actions.js +116 -0
- package/js/iso-editor/ui/canvas-layer.js +0 -0
- package/js/iso-editor/ui/canvas.js +1205 -0
- package/js/iso-editor/ui/clip.js +133 -0
- package/js/iso-editor/ui/dialog.js +26 -0
- package/js/iso-editor/ui/flat-canvas.js +54 -0
- package/js/iso-editor/ui/icons.js +15 -0
- package/js/iso-editor/ui/keys.js +26 -0
- package/js/iso-editor/ui/later.js +28 -0
- package/js/iso-editor/ui/layer.js +11 -0
- package/js/iso-editor/ui/menu.js +105 -0
- package/js/iso-editor/ui/monaco-entry.js +2 -0
- package/js/iso-editor/ui/mount-kit.js +0 -0
- package/js/iso-editor/ui/mount.js +767 -0
- package/js/iso-editor/ui/pane.js +0 -0
- package/js/iso-editor/ui/part.js +0 -0
- package/js/iso-editor/ui/plain-icons.js +34 -0
- package/js/iso-editor/ui/props.js +314 -0
- package/js/iso-editor/ui/scope-css.js +81 -0
- package/js/iso-editor/ui/setup.js +145 -0
- package/js/iso-editor/ui/text.js +323 -0
- package/js/iso-editor/ui/tree.js +728 -0
- package/js/iso-editor/validate.js +33 -0
- package/js/iso-editor/window/protocol.js +8 -0
- package/js/iso-objects/assemble.js +43 -0
- package/js/iso-objects/assets.js +88 -0
- package/js/iso-objects/board.js +162 -0
- package/js/iso-objects/compile.js +60 -0
- package/js/iso-objects/css.js +154 -0
- package/js/iso-objects/flat.js +696 -0
- package/js/iso-objects/focus.js +16 -0
- package/js/iso-objects/geometry.js +76 -0
- package/js/iso-objects/index.js +93 -0
- package/js/iso-objects/kinds.js +12 -0
- package/js/iso-objects/labels.js +325 -0
- package/js/iso-objects/package.json +6 -0
- package/js/iso-objects/place.js +347 -0
- package/js/iso-objects/route.js +461 -0
- package/js/iso-objects/scene.js +350 -0
- package/js/iso-objects/steps.js +246 -0
- package/js/iso-objects/types.js +0 -0
- package/js/iso-systems/catalog.js +42 -0
- package/js/iso-systems/compile.js +34 -0
- package/js/iso-systems/css.js +8 -0
- package/js/iso-systems/index.js +16 -0
- package/js/iso-systems/nodes.js +124 -0
- package/js/iso-systems/package.json +6 -0
- package/js/iso-systems/schema.json +311 -0
- package/js/iso-systems/validate.js +35 -0
- package/js/iso-viewer/bundle.js +20 -0
- package/js/iso-viewer/card-motion.js +228 -0
- package/js/iso-viewer/dom.js +14 -0
- package/js/iso-viewer/dual-view.js +212 -0
- package/js/iso-viewer/flat-view.js +124 -0
- package/js/iso-viewer/history.js +990 -0
- package/js/iso-viewer/index.js +98 -0
- package/js/iso-viewer/morph.js +192 -0
- package/js/iso-viewer/overlay.js +199 -0
- package/js/iso-viewer/package.json +6 -0
- package/js/iso-viewer/page.js +138 -0
- package/js/iso-viewer/player.js +409 -0
- package/js/iso-viewer/still.js +81 -0
- package/js/iso-viewer/timeline-contract.js +14 -0
- package/js/iso-viewer/timeline-engine.js +475 -0
- package/js/iso-viewer/timeline-export.js +36 -0
- package/js/iso-viewer/timeline-scale.js +116 -0
- package/js/iso-viewer/timeline-scope.js +58 -0
- package/js/iso-viewer/timeline-search.js +216 -0
- package/js/iso-viewer/timeline-time.js +55 -0
- package/js/iso-viewer/view-css.js +68 -0
- package/js/iso-viewer/viewport.js +177 -0
- package/package.json +502 -4
- package/src/iso-core/compile-schema.ts +23 -0
- package/src/iso-core/depth.ts +56 -0
- package/src/iso-core/errors.ts +73 -0
- package/src/iso-core/glyphs.ts +108 -0
- package/src/iso-core/index.ts +27 -0
- package/src/iso-core/kernel.ts +163 -0
- package/src/iso-core/metrics.json +1 -0
- package/src/iso-core/package.json +6 -0
- package/src/iso-core/parse.ts +91 -0
- package/src/iso-core/schema-errors.ts +163 -0
- package/src/iso-core/shims/crypto.ts +37 -0
- package/src/iso-core/shims/path.ts +24 -0
- package/src/iso-core/svg-dom.ts +103 -0
- package/src/iso-core/theme.ts +64 -0
- package/src/iso-core/vdom.ts +41 -0
- package/src/iso-editor/adapter.ts +128 -0
- package/src/iso-editor/clipboard.ts +163 -0
- package/src/iso-editor/commands.ts +191 -0
- package/src/iso-editor/complete.ts +73 -0
- package/src/iso-editor/doc.ts +207 -0
- package/src/iso-editor/index.ts +67 -0
- package/src/iso-editor/model.ts +234 -0
- package/src/iso-editor/outline.ts +126 -0
- package/src/iso-editor/package.json +6 -0
- package/src/iso-editor/path.ts +18 -0
- package/src/iso-editor/providers.ts +113 -0
- package/src/iso-editor/schema-nav.ts +129 -0
- package/src/iso-editor/splice.ts +472 -0
- package/src/iso-editor/storage-keys.ts +6 -0
- package/src/iso-editor/ui/actions.ts +119 -0
- package/src/iso-editor/ui/app.css +230 -0
- package/src/iso-editor/ui/canvas-layer.ts +56 -0
- package/src/iso-editor/ui/canvas.ts +1071 -0
- package/src/iso-editor/ui/clip.ts +97 -0
- package/src/iso-editor/ui/dialog.ts +25 -0
- package/src/iso-editor/ui/flat-canvas.ts +51 -0
- package/src/iso-editor/ui/icons.ts +21 -0
- package/src/iso-editor/ui/keys.ts +30 -0
- package/src/iso-editor/ui/later.ts +22 -0
- package/src/iso-editor/ui/layer.ts +7 -0
- package/src/iso-editor/ui/marks.css +35 -0
- package/src/iso-editor/ui/menu.ts +67 -0
- package/src/iso-editor/ui/monaco-entry.ts +6 -0
- package/src/iso-editor/ui/mount-kit.ts +35 -0
- package/src/iso-editor/ui/mount.ts +684 -0
- package/src/iso-editor/ui/pane.ts +45 -0
- package/src/iso-editor/ui/part.ts +45 -0
- package/src/iso-editor/ui/plain-icons.ts +83 -0
- package/src/iso-editor/ui/props.ts +347 -0
- package/src/iso-editor/ui/scope-css.ts +65 -0
- package/src/iso-editor/ui/setup.ts +144 -0
- package/src/iso-editor/ui/skin.css +181 -0
- package/src/iso-editor/ui/text.ts +262 -0
- package/src/iso-editor/ui/tree.ts +657 -0
- package/src/iso-editor/validate.ts +62 -0
- package/src/iso-editor/window/protocol.ts +33 -0
- package/src/iso-objects/assemble.ts +57 -0
- package/src/iso-objects/assets.ts +122 -0
- package/src/iso-objects/board.ts +169 -0
- package/src/iso-objects/compile.ts +76 -0
- package/src/iso-objects/css.ts +163 -0
- package/src/iso-objects/flat.ts +653 -0
- package/src/iso-objects/focus.ts +14 -0
- package/src/iso-objects/geometry.ts +86 -0
- package/src/iso-objects/index.ts +33 -0
- package/src/iso-objects/kinds.ts +56 -0
- package/src/iso-objects/labels.ts +310 -0
- package/src/iso-objects/package.json +6 -0
- package/src/iso-objects/place.ts +318 -0
- package/src/iso-objects/route.ts +444 -0
- package/src/iso-objects/scene.ts +376 -0
- package/src/iso-objects/steps.ts +234 -0
- package/src/iso-objects/types.ts +68 -0
- package/src/iso-systems/catalog.ts +31 -0
- package/src/iso-systems/compile.ts +39 -0
- package/src/iso-systems/css.ts +9 -0
- package/src/iso-systems/index.ts +12 -0
- package/src/iso-systems/nodes.ts +126 -0
- package/src/iso-systems/package.json +6 -0
- package/src/iso-systems/schema.json +311 -0
- package/src/iso-systems/validate.ts +34 -0
- package/src/iso-viewer/bundle.ts +15 -0
- package/src/iso-viewer/card-motion.ts +237 -0
- package/src/iso-viewer/dom.ts +13 -0
- package/src/iso-viewer/dual-view.ts +212 -0
- package/src/iso-viewer/flat-view.ts +127 -0
- package/src/iso-viewer/history.ts +781 -0
- package/src/iso-viewer/index.ts +40 -0
- package/src/iso-viewer/morph.ts +191 -0
- package/src/iso-viewer/overlay.ts +212 -0
- package/src/iso-viewer/package.json +6 -0
- package/src/iso-viewer/page.ts +150 -0
- package/src/iso-viewer/player.ts +321 -0
- package/src/iso-viewer/still.ts +74 -0
- package/src/iso-viewer/timeline-contract.ts +342 -0
- package/src/iso-viewer/timeline-engine.ts +420 -0
- package/src/iso-viewer/timeline-export.ts +31 -0
- package/src/iso-viewer/timeline-scale.ts +129 -0
- package/src/iso-viewer/timeline-scope.ts +56 -0
- package/src/iso-viewer/timeline-search.ts +198 -0
- package/src/iso-viewer/timeline-time.ts +54 -0
- package/src/iso-viewer/view-css.ts +59 -0
- package/src/iso-viewer/viewport.ts +142 -0
- package/types/iso-core/compile-schema.d.ts +11 -0
- package/types/iso-core/depth.d.ts +6 -0
- package/types/iso-core/errors.d.ts +24 -0
- package/types/iso-core/glyphs.d.ts +19 -0
- package/types/iso-core/index.d.ts +17 -0
- package/types/iso-core/kernel.d.ts +130 -0
- package/types/iso-core/metrics.json +1 -0
- package/types/iso-core/parse.d.ts +18 -0
- package/types/iso-core/schema-errors.d.ts +14 -0
- package/types/iso-core/shims/crypto.d.ts +9 -0
- package/types/iso-core/svg-dom.d.ts +15 -0
- package/types/iso-core/theme.d.ts +38 -0
- package/types/iso-core/vdom.d.ts +26 -0
- package/types/iso-editor/adapter.d.ts +145 -0
- package/types/iso-editor/clipboard.d.ts +23 -0
- package/types/iso-editor/commands.d.ts +32 -0
- package/types/iso-editor/complete.d.ts +25 -0
- package/types/iso-editor/doc.d.ts +50 -0
- package/types/iso-editor/index.d.ts +57 -0
- package/types/iso-editor/model.d.ts +96 -0
- package/types/iso-editor/outline.d.ts +28 -0
- package/types/iso-editor/path.d.ts +7 -0
- package/types/iso-editor/providers.d.ts +43 -0
- package/types/iso-editor/schema-nav.d.ts +34 -0
- package/types/iso-editor/splice.d.ts +53 -0
- package/types/iso-editor/storage-keys.d.ts +5 -0
- package/types/iso-editor/ui/actions.d.ts +23 -0
- package/types/iso-editor/ui/canvas-layer.d.ts +65 -0
- package/types/iso-editor/ui/canvas.d.ts +279 -0
- package/types/iso-editor/ui/clip.d.ts +14 -0
- package/types/iso-editor/ui/dialog.d.ts +7 -0
- package/types/iso-editor/ui/flat-canvas.d.ts +2 -0
- package/types/iso-editor/ui/icons.d.ts +5 -0
- package/types/iso-editor/ui/keys.d.ts +7 -0
- package/types/iso-editor/ui/later.d.ts +12 -0
- package/types/iso-editor/ui/layer.d.ts +2 -0
- package/types/iso-editor/ui/menu.d.ts +21 -0
- package/types/iso-editor/ui/mount-kit.d.ts +31 -0
- package/types/iso-editor/ui/mount.d.ts +146 -0
- package/types/iso-editor/ui/pane.d.ts +60 -0
- package/types/iso-editor/ui/part.d.ts +40 -0
- package/types/iso-editor/ui/plain-icons.d.ts +10 -0
- package/types/iso-editor/ui/props.d.ts +57 -0
- package/types/iso-editor/ui/scope-css.d.ts +5 -0
- package/types/iso-editor/ui/setup.d.ts +40 -0
- package/types/iso-editor/ui/text.d.ts +19 -0
- package/types/iso-editor/ui/tree.d.ts +100 -0
- package/types/iso-editor/validate.d.ts +36 -0
- package/types/iso-editor/window/protocol.d.ts +60 -0
- package/types/iso-objects/assemble.d.ts +35 -0
- package/types/iso-objects/assets.d.ts +49 -0
- package/types/iso-objects/board.d.ts +36 -0
- package/types/iso-objects/compile.d.ts +35 -0
- package/types/iso-objects/css.d.ts +16 -0
- package/types/iso-objects/flat.d.ts +130 -0
- package/types/iso-objects/focus.d.ts +1 -0
- package/types/iso-objects/geometry.d.ts +59 -0
- package/types/iso-objects/index.d.ts +26 -0
- package/types/iso-objects/kinds.d.ts +53 -0
- package/types/iso-objects/labels.d.ts +92 -0
- package/types/iso-objects/place.d.ts +25 -0
- package/types/iso-objects/route.d.ts +67 -0
- package/types/iso-objects/scene.d.ts +63 -0
- package/types/iso-objects/steps.d.ts +90 -0
- package/types/iso-objects/types.d.ts +140 -0
- package/types/iso-systems/catalog.d.ts +4 -0
- package/types/iso-systems/compile.d.ts +6 -0
- package/types/iso-systems/css.d.ts +3 -0
- package/types/iso-systems/index.d.ts +5 -0
- package/types/iso-systems/nodes.d.ts +3 -0
- package/types/iso-viewer/card-motion.d.ts +56 -0
- package/types/iso-viewer/dom.d.ts +1 -0
- package/types/iso-viewer/dual-view.d.ts +44 -0
- package/types/iso-viewer/flat-view.d.ts +25 -0
- package/types/iso-viewer/history.d.ts +91 -0
- package/types/iso-viewer/index.d.ts +31 -0
- package/types/iso-viewer/morph.d.ts +28 -0
- package/types/iso-viewer/overlay.d.ts +135 -0
- package/types/iso-viewer/page.d.ts +13 -0
- package/types/iso-viewer/still.d.ts +26 -0
- package/types/iso-viewer/timeline-contract.d.ts +345 -0
- package/types/iso-viewer/timeline-engine.d.ts +20 -0
- package/types/iso-viewer/timeline-export.d.ts +5 -0
- package/types/iso-viewer/timeline-scale.d.ts +44 -0
- package/types/iso-viewer/timeline-scope.d.ts +33 -0
- package/types/iso-viewer/timeline-search.d.ts +52 -0
- package/types/iso-viewer/timeline-time.d.ts +8 -0
- package/types/iso-viewer/view-css.d.ts +7 -0
- package/types/iso-viewer/viewport.d.ts +50 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
function focusView(full, box) {
|
|
2
|
+
if (!box) return full;
|
|
3
|
+
const [x0, y0, x1, y1] = box, ar = full[2] / full[3];
|
|
4
|
+
let w = x1 - x0, h = y1 - y0;
|
|
5
|
+
if (w / h < ar) w = h * ar;
|
|
6
|
+
else h = w / ar;
|
|
7
|
+
w = Math.min(w, full[2]);
|
|
8
|
+
h = Math.min(h, full[3]);
|
|
9
|
+
const cx = (x0 + x1) / 2, cy = (y0 + y1) / 2;
|
|
10
|
+
const x = Math.max(full[0], Math.min(full[0] + full[2] - w, cx - w / 2));
|
|
11
|
+
const y = Math.max(full[1], Math.min(full[1] + full[3] - h, cy - h / 2));
|
|
12
|
+
return [x, y, w, h];
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
focusView
|
|
16
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { C, S, iso } from "../iso-core/kernel.js";
|
|
2
|
+
import { regionKind } from "./kinds.js";
|
|
3
|
+
function screenToGrid(dx, dy) {
|
|
4
|
+
const a = dx / (C * S), b = 2 * dy / S;
|
|
5
|
+
return [(a + b) / 2, (b - a) / 2];
|
|
6
|
+
}
|
|
7
|
+
function localAt(sc, id) {
|
|
8
|
+
const p = sc.layout.byId.get(id);
|
|
9
|
+
if (!p) return void 0;
|
|
10
|
+
const o = p.parent ? [p.parent.box.x0, p.parent.box.y0] : [0, 0];
|
|
11
|
+
return [p.box.x0 - o[0], p.box.y0 - o[1]];
|
|
12
|
+
}
|
|
13
|
+
function boxSilhouette(b) {
|
|
14
|
+
const pts = [];
|
|
15
|
+
for (const x of [b.x0, b.x1]) for (const y of [b.y0, b.y1]) for (const z of [b.z0, b.z1]) pts.push(iso(x, y, z));
|
|
16
|
+
return hull(pts);
|
|
17
|
+
}
|
|
18
|
+
function floorOf(b) {
|
|
19
|
+
return [[b.x0, b.y0], [b.x1, b.y0], [b.x1, b.y1], [b.x0, b.y1]].map(([x, y]) => iso(x, y, b.z0));
|
|
20
|
+
}
|
|
21
|
+
function hull(p) {
|
|
22
|
+
const pts = [...new Map(p.map((q) => [`${q[0].toFixed(3)},${q[1].toFixed(3)}`, q])).values()].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
23
|
+
if (pts.length < 3) return pts;
|
|
24
|
+
const cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
|
|
25
|
+
const lo = [], hi = [];
|
|
26
|
+
for (const q of pts) {
|
|
27
|
+
while (lo.length >= 2 && cross(lo[lo.length - 2], lo[lo.length - 1], q) <= 1e-9) lo.pop();
|
|
28
|
+
lo.push(q);
|
|
29
|
+
}
|
|
30
|
+
for (const q of [...pts].reverse()) {
|
|
31
|
+
while (hi.length >= 2 && cross(hi[hi.length - 2], hi[hi.length - 1], q) <= 1e-9) hi.pop();
|
|
32
|
+
hi.push(q);
|
|
33
|
+
}
|
|
34
|
+
return [...lo.slice(0, -1), ...hi.slice(0, -1)];
|
|
35
|
+
}
|
|
36
|
+
const paddingOf = (cat, region) => region?.padding ?? regionKind(cat, region?.kind)?.pad ?? 0;
|
|
37
|
+
function snapMove(start, d, step, size, parent) {
|
|
38
|
+
const snap = (v) => Math.round(v / step) * step;
|
|
39
|
+
let x = snap(start[0] + d[0]), y = snap(start[1] + d[1]);
|
|
40
|
+
const clamped = { left: false, right: false, top: false, bottom: false };
|
|
41
|
+
if (parent) {
|
|
42
|
+
const lo = Math.ceil(parent.pad / step - 1e-9) * step;
|
|
43
|
+
const hiX = Math.floor((parent.w - parent.pad - size[0]) / step + 1e-9) * step, hiY = Math.floor((parent.d - parent.pad - size[1]) / step + 1e-9) * step;
|
|
44
|
+
if (x < lo) {
|
|
45
|
+
x = lo;
|
|
46
|
+
clamped.left = true;
|
|
47
|
+
}
|
|
48
|
+
if (hiX >= lo && x > hiX) {
|
|
49
|
+
x = hiX;
|
|
50
|
+
clamped.right = true;
|
|
51
|
+
}
|
|
52
|
+
if (y < lo) {
|
|
53
|
+
y = lo;
|
|
54
|
+
clamped.top = true;
|
|
55
|
+
}
|
|
56
|
+
if (hiY >= lo && y > hiY) {
|
|
57
|
+
y = hiY;
|
|
58
|
+
clamped.bottom = true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const r = (v) => Math.round(v * 1e3) / 1e3;
|
|
62
|
+
return { at: [r(x), r(y)], delta: [r(x - start[0]), r(y - start[1])], clamped };
|
|
63
|
+
}
|
|
64
|
+
function overlaps(a, others) {
|
|
65
|
+
const e = 1e-6;
|
|
66
|
+
return others.some((b) => a.x0 < b.x1 - e && a.x1 > b.x0 + e && a.y0 < b.y1 - e && a.y1 > b.y0 + e);
|
|
67
|
+
}
|
|
68
|
+
export {
|
|
69
|
+
boxSilhouette,
|
|
70
|
+
floorOf,
|
|
71
|
+
localAt,
|
|
72
|
+
overlaps,
|
|
73
|
+
paddingOf,
|
|
74
|
+
screenToGrid,
|
|
75
|
+
snapMove
|
|
76
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { regionKind, nodeKind, nodeKindOr, wallHeight, kindsCss } from "./kinds.js";
|
|
2
|
+
import { drawDecos, boxAsset, plate, cloudPath, drawCloud, defaultLabel, registerMarks, markCss, iconPainter, facePainter, placeholderIcon } from "./assets.js";
|
|
3
|
+
import { WALL_T, place, gateBox, overlap2 } from "./place.js";
|
|
4
|
+
import { parseEnd, floorAt, ROUTE_BUDGET, routeAll, openList, simplify } from "./route.js";
|
|
5
|
+
import { silhouette, polyBounds, polyRectArea, rectArea, lineRectArea, Obstacles, labelRect, placeNodeLabel, calloutRect, placeCallout, inConvex, convexOverlap } from "./labels.js";
|
|
6
|
+
import { offscreen, assembly } from "./assemble.js";
|
|
7
|
+
import { durationFor, resolveSteps, midpoint } from "./steps.js";
|
|
8
|
+
import { focusView } from "./focus.js";
|
|
9
|
+
import { regionRole, renderScene, wallsOf, LABEL_BUDGET } from "./scene.js";
|
|
10
|
+
import { PAGE_CSS, drawingCss, baseCss, MARKS_CSS, PLAYER_UI_CSS, GRID_CSS, PLAYER_CSS } from "./css.js";
|
|
11
|
+
import { noSource, compileRaw } from "./compile.js";
|
|
12
|
+
import { screenToGrid, localAt, boxSilhouette, floorOf, paddingOf, snapMove, overlaps } from "./geometry.js";
|
|
13
|
+
import { FLAT_DEFAULTS, FLAT_FONTS, metricsMeasure, boardErrors, placeBoard, layoutFlat, flatHit } from "./flat.js";
|
|
14
|
+
import { boardOf, BOARD_3D, BOARD_CSS, cardAsset, boardCatalog, boardDiagram } from "./board.js";
|
|
15
|
+
export {
|
|
16
|
+
BOARD_3D,
|
|
17
|
+
BOARD_CSS,
|
|
18
|
+
FLAT_DEFAULTS,
|
|
19
|
+
FLAT_FONTS,
|
|
20
|
+
GRID_CSS,
|
|
21
|
+
LABEL_BUDGET,
|
|
22
|
+
MARKS_CSS,
|
|
23
|
+
Obstacles,
|
|
24
|
+
PAGE_CSS,
|
|
25
|
+
PLAYER_CSS,
|
|
26
|
+
PLAYER_UI_CSS,
|
|
27
|
+
ROUTE_BUDGET,
|
|
28
|
+
WALL_T,
|
|
29
|
+
assembly,
|
|
30
|
+
baseCss,
|
|
31
|
+
boardCatalog,
|
|
32
|
+
boardDiagram,
|
|
33
|
+
boardErrors,
|
|
34
|
+
boardOf,
|
|
35
|
+
boxAsset,
|
|
36
|
+
boxSilhouette,
|
|
37
|
+
calloutRect,
|
|
38
|
+
cardAsset,
|
|
39
|
+
cloudPath,
|
|
40
|
+
compileRaw,
|
|
41
|
+
convexOverlap,
|
|
42
|
+
defaultLabel,
|
|
43
|
+
drawCloud,
|
|
44
|
+
drawDecos,
|
|
45
|
+
drawingCss,
|
|
46
|
+
durationFor,
|
|
47
|
+
facePainter,
|
|
48
|
+
flatHit,
|
|
49
|
+
floorAt,
|
|
50
|
+
floorOf,
|
|
51
|
+
focusView,
|
|
52
|
+
gateBox,
|
|
53
|
+
iconPainter,
|
|
54
|
+
inConvex,
|
|
55
|
+
kindsCss,
|
|
56
|
+
labelRect,
|
|
57
|
+
layoutFlat,
|
|
58
|
+
lineRectArea,
|
|
59
|
+
localAt,
|
|
60
|
+
markCss,
|
|
61
|
+
metricsMeasure,
|
|
62
|
+
midpoint,
|
|
63
|
+
noSource,
|
|
64
|
+
nodeKind,
|
|
65
|
+
nodeKindOr,
|
|
66
|
+
offscreen,
|
|
67
|
+
openList,
|
|
68
|
+
overlap2,
|
|
69
|
+
overlaps,
|
|
70
|
+
paddingOf,
|
|
71
|
+
parseEnd,
|
|
72
|
+
place,
|
|
73
|
+
placeBoard,
|
|
74
|
+
placeCallout,
|
|
75
|
+
placeNodeLabel,
|
|
76
|
+
placeholderIcon,
|
|
77
|
+
plate,
|
|
78
|
+
polyBounds,
|
|
79
|
+
polyRectArea,
|
|
80
|
+
rectArea,
|
|
81
|
+
regionKind,
|
|
82
|
+
regionRole,
|
|
83
|
+
registerMarks,
|
|
84
|
+
renderScene,
|
|
85
|
+
resolveSteps,
|
|
86
|
+
routeAll,
|
|
87
|
+
screenToGrid,
|
|
88
|
+
silhouette,
|
|
89
|
+
simplify,
|
|
90
|
+
snapMove,
|
|
91
|
+
wallHeight,
|
|
92
|
+
wallsOf
|
|
93
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const regionKind = (cat, name) => name !== void 0 && Object.hasOwn(cat.regions, name) ? cat.regions[name] : void 0;
|
|
2
|
+
const nodeKind = (cat, name) => cat.nodes[name ?? cat.defaultNode];
|
|
3
|
+
const nodeKindOr = (cat, name) => nodeKind(cat, name) ?? cat.nodes[cat.defaultNode];
|
|
4
|
+
const wallHeight = (cat) => Object.values(cat.regions).find((r) => r.shape === "walled")?.z[1] ?? 0.8;
|
|
5
|
+
const kindsCss = (cat) => Object.values(cat.regions).map((r) => r.css ?? "").join("");
|
|
6
|
+
export {
|
|
7
|
+
kindsCss,
|
|
8
|
+
nodeKind,
|
|
9
|
+
nodeKindOr,
|
|
10
|
+
regionKind,
|
|
11
|
+
wallHeight
|
|
12
|
+
};
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { iso, textWidth, FONTS } from "../iso-core/kernel.js";
|
|
2
|
+
function silhouette(b) {
|
|
3
|
+
return [iso(b.x0, b.y0, b.z1), iso(b.x1, b.y0, b.z1), iso(b.x1, b.y0, b.z0), iso(b.x1, b.y1, b.z0), iso(b.x0, b.y1, b.z0), iso(b.x0, b.y1, b.z1)];
|
|
4
|
+
}
|
|
5
|
+
function polyBounds(p) {
|
|
6
|
+
const xs = p.map((q) => q[0]), ys = p.map((q) => q[1]);
|
|
7
|
+
const x = Math.min(...xs), y = Math.min(...ys);
|
|
8
|
+
return { x, y, w: Math.max(...xs) - x, h: Math.max(...ys) - y };
|
|
9
|
+
}
|
|
10
|
+
function polyRectArea(poly, r) {
|
|
11
|
+
let pts = poly;
|
|
12
|
+
const clip = (inside, cross) => {
|
|
13
|
+
const out = [];
|
|
14
|
+
for (let i = 0; i < pts.length; i++) {
|
|
15
|
+
const a2 = pts[i], b = pts[(i + 1) % pts.length];
|
|
16
|
+
const ia = inside(a2), ib = inside(b);
|
|
17
|
+
if (ia) out.push(a2);
|
|
18
|
+
if (ia !== ib) out.push(cross(a2, b));
|
|
19
|
+
}
|
|
20
|
+
pts = out;
|
|
21
|
+
};
|
|
22
|
+
const atX = (x) => (a2, b) => [x, a2[1] + (b[1] - a2[1]) * (x - a2[0]) / (b[0] - a2[0])];
|
|
23
|
+
const atY = (y) => (a2, b) => [a2[0] + (b[0] - a2[0]) * (y - a2[1]) / (b[1] - a2[1]), y];
|
|
24
|
+
clip((p) => p[0] >= r.x, atX(r.x));
|
|
25
|
+
if (!pts.length) return 0;
|
|
26
|
+
clip((p) => p[0] <= r.x + r.w, atX(r.x + r.w));
|
|
27
|
+
if (!pts.length) return 0;
|
|
28
|
+
clip((p) => p[1] >= r.y, atY(r.y));
|
|
29
|
+
if (!pts.length) return 0;
|
|
30
|
+
clip((p) => p[1] <= r.y + r.h, atY(r.y + r.h));
|
|
31
|
+
if (!pts.length) return 0;
|
|
32
|
+
let a = 0;
|
|
33
|
+
for (let i = 0; i < pts.length; i++) {
|
|
34
|
+
const p = pts[i], q = pts[(i + 1) % pts.length];
|
|
35
|
+
a += p[0] * q[1] - q[0] * p[1];
|
|
36
|
+
}
|
|
37
|
+
return Math.abs(a) / 2;
|
|
38
|
+
}
|
|
39
|
+
function rectArea(a, b) {
|
|
40
|
+
const w = Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x), h = Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y);
|
|
41
|
+
return w > 0 && h > 0 ? w * h : 0;
|
|
42
|
+
}
|
|
43
|
+
function lineRectArea(pts, r) {
|
|
44
|
+
let a = 0;
|
|
45
|
+
for (let i = 0; i + 1 < pts.length; i++) {
|
|
46
|
+
const p = pts[i], q = pts[i + 1], len = Math.hypot(q[0] - p[0], q[1] - p[1]), n = Math.max(1, Math.ceil(len / 4));
|
|
47
|
+
for (let j = 0; j < n; j++) {
|
|
48
|
+
const x = p[0] + (q[0] - p[0]) * (j + 0.5) / n, y = p[1] + (q[1] - p[1]) * (j + 0.5) / n;
|
|
49
|
+
if (x >= r.x && x <= r.x + r.w && y >= r.y && y <= r.y + r.h) a += len / n * 4;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return a;
|
|
53
|
+
}
|
|
54
|
+
const CELL = 64;
|
|
55
|
+
class Buckets {
|
|
56
|
+
cells = /* @__PURE__ */ new Map();
|
|
57
|
+
n = 0;
|
|
58
|
+
add(id, x0, y0, x1, y1) {
|
|
59
|
+
for (let cx = Math.floor(x0 / CELL); cx <= Math.floor(x1 / CELL); cx++) for (let cy = Math.floor(y0 / CELL); cy <= Math.floor(y1 / CELL); cy++) {
|
|
60
|
+
const k = cx * 1048576 + cy;
|
|
61
|
+
let list = this.cells.get(k);
|
|
62
|
+
if (!list) this.cells.set(k, list = []);
|
|
63
|
+
if (list[list.length - 1] !== id) list.push(id);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** the ids whose buckets meet the box, ascending, each once */
|
|
67
|
+
query(x0, y0, x1, y1) {
|
|
68
|
+
const out = /* @__PURE__ */ new Set();
|
|
69
|
+
for (let cx = Math.floor(x0 / CELL); cx <= Math.floor(x1 / CELL); cx++) for (let cy = Math.floor(y0 / CELL); cy <= Math.floor(y1 / CELL); cy++) {
|
|
70
|
+
const list = this.cells.get(cx * 1048576 + cy);
|
|
71
|
+
if (list) for (const id of list) out.add(id);
|
|
72
|
+
}
|
|
73
|
+
return [...out].sort((p, q) => p - q);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const boundsOf = (p) => {
|
|
77
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
78
|
+
for (const [x, y] of p) {
|
|
79
|
+
if (x < x0) x0 = x;
|
|
80
|
+
if (x > x1) x1 = x;
|
|
81
|
+
if (y < y0) y0 = y;
|
|
82
|
+
if (y > y1) y1 = y;
|
|
83
|
+
}
|
|
84
|
+
return [x0, y0, x1, y1];
|
|
85
|
+
};
|
|
86
|
+
const indexes = /* @__PURE__ */ new WeakMap();
|
|
87
|
+
function indexOf(list, box) {
|
|
88
|
+
let ix = indexes.get(list);
|
|
89
|
+
if (!ix || ix.n > list.length) indexes.set(list, ix = new Buckets());
|
|
90
|
+
for (; ix.n < list.length; ix.n++) {
|
|
91
|
+
const [x0, y0, x1, y1] = box(list[ix.n]);
|
|
92
|
+
ix.add(ix.n, x0, y0, x1, y1);
|
|
93
|
+
}
|
|
94
|
+
return ix;
|
|
95
|
+
}
|
|
96
|
+
function segmentsOf(lines) {
|
|
97
|
+
let ix = indexes.get(lines);
|
|
98
|
+
if (!ix || ix.n > lines.length) indexes.set(lines, ix = new Buckets());
|
|
99
|
+
for (; ix.n < lines.length; ix.n++) {
|
|
100
|
+
const pts = lines[ix.n].pts;
|
|
101
|
+
for (let i = 0; i + 1 < pts.length; i++) {
|
|
102
|
+
const a = pts[i], b = pts[i + 1], id = ix.n * 65536 + i;
|
|
103
|
+
const k = Math.max(1, Math.ceil(Math.hypot(b[0] - a[0], b[1] - a[1]) / (CELL / 2)));
|
|
104
|
+
for (let m = 0; m < k; m++) {
|
|
105
|
+
const u0 = m / k, u1 = (m + 1) / k;
|
|
106
|
+
const x0 = a[0] + (b[0] - a[0]) * u0, y0 = a[1] + (b[1] - a[1]) * u0, x1 = a[0] + (b[0] - a[0]) * u1, y1 = a[1] + (b[1] - a[1]) * u1;
|
|
107
|
+
ix.add(id, Math.min(x0, x1) - 1, Math.min(y0, y1) - 1, Math.max(x0, x1) + 1, Math.max(y0, y1) + 1);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return ix;
|
|
112
|
+
}
|
|
113
|
+
const near = (ix, x0, y0, x1, y1) => ix.query(x0, y0, x1, y1);
|
|
114
|
+
const polyBox = (p) => boundsOf(p);
|
|
115
|
+
const rectBox = (r) => [r.x, r.y, r.x + r.w, r.y + r.h];
|
|
116
|
+
function sampleRange(a, b, n, [x0, y0, x1, y1]) {
|
|
117
|
+
let lo = 0, hi = n - 1;
|
|
118
|
+
for (const k of [0, 1]) {
|
|
119
|
+
const d = b[k] - a[k], min = k ? y0 : x0, max = k ? y1 : x1;
|
|
120
|
+
if (Math.abs(d) < 1e-12) {
|
|
121
|
+
if (a[k] < min || a[k] > max) return [1, 0];
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
let t0 = (min - a[k]) / d * n - 0.5, t1 = (max - a[k]) / d * n - 0.5;
|
|
125
|
+
if (t0 > t1) [t0, t1] = [t1, t0];
|
|
126
|
+
lo = Math.max(lo, Math.floor(t0) - 1);
|
|
127
|
+
hi = Math.min(hi, Math.ceil(t1) + 1);
|
|
128
|
+
}
|
|
129
|
+
return [lo, hi];
|
|
130
|
+
}
|
|
131
|
+
class Obstacles {
|
|
132
|
+
polys = [];
|
|
133
|
+
rects = [];
|
|
134
|
+
lines = [];
|
|
135
|
+
/** everything drawn over the floor (nodes, walls, gates): floor text under them is hidden */
|
|
136
|
+
solids = [];
|
|
137
|
+
/** text lying in a plane (floor and face labels), as parallelograms */
|
|
138
|
+
slants = [];
|
|
139
|
+
/** label spots the scene may still weigh (see LABEL_BUDGET in src/iso-objects/scene.ts); shared with forks */
|
|
140
|
+
budget = { spots: Infinity, over: false, told: false };
|
|
141
|
+
/** px² of a rectangle hidden under solids */
|
|
142
|
+
hidden(r) {
|
|
143
|
+
let c = 0;
|
|
144
|
+
for (const i of near(indexOf(this.solids, polyBox), ...rectBox(r))) c += polyRectArea(this.solids[i], r);
|
|
145
|
+
return c;
|
|
146
|
+
}
|
|
147
|
+
/** px² of a convex polygon (slanted floor text) hidden under solids */
|
|
148
|
+
hiddenPoly(q) {
|
|
149
|
+
let c = 0;
|
|
150
|
+
for (const i of near(indexOf(this.solids, polyBox), ...boundsOf(q))) c += convexOverlap(q, this.solids[i]);
|
|
151
|
+
return c;
|
|
152
|
+
}
|
|
153
|
+
/** like cost(), for a convex polygon */
|
|
154
|
+
costPoly(q, limit = Infinity) {
|
|
155
|
+
const qb = boundsOf(q);
|
|
156
|
+
let c = 0;
|
|
157
|
+
for (const i of near(indexOf(this.polys, (p) => polyBox(p.poly)), ...qb)) c += convexOverlap(q, this.polys[i].poly);
|
|
158
|
+
for (const i of near(indexOf(this.rects, (x) => rectBox(x.r)), ...qb)) c += polyRectArea(q, this.rects[i].r) * 2;
|
|
159
|
+
for (const i of near(indexOf(this.slants, polyBox), ...qb)) c += convexOverlap(q, this.slants[i]) * 2;
|
|
160
|
+
if (c >= limit) return c;
|
|
161
|
+
for (const id of near(segmentsOf(this.lines), ...qb)) {
|
|
162
|
+
if (c >= limit) return c;
|
|
163
|
+
const pts = this.lines[Math.floor(id / 65536)].pts, i = id % 65536;
|
|
164
|
+
const a = pts[i], b = pts[i + 1];
|
|
165
|
+
const len = Math.hypot(b[0] - a[0], b[1] - a[1]), n = Math.max(1, Math.ceil(len / 4));
|
|
166
|
+
const [j0, j1] = sampleRange(a, b, n, qb);
|
|
167
|
+
for (let j = j0; j <= j1; j++) if (inConvex(q, [a[0] + (b[0] - a[0]) * (j + 0.5) / n, a[1] + (b[1] - a[1]) * (j + 0.5) / n])) c += len / n * 4 * 0.6;
|
|
168
|
+
}
|
|
169
|
+
return c;
|
|
170
|
+
}
|
|
171
|
+
/** px² a rectangle shares with nodes, labels and (weighted by lineW) route lines */
|
|
172
|
+
cost(r, owner, lineW = 0.6) {
|
|
173
|
+
const rb = rectBox(r);
|
|
174
|
+
let c = 0;
|
|
175
|
+
for (const i of near(indexOf(this.polys, (p) => polyBox(p.poly)), ...rb)) {
|
|
176
|
+
const p = this.polys[i];
|
|
177
|
+
c += polyRectArea(p.poly, r) * (p.owner === owner ? 1.5 : 1);
|
|
178
|
+
}
|
|
179
|
+
for (const i of near(indexOf(this.rects, (x) => rectBox(x.r)), ...rb)) {
|
|
180
|
+
const q = this.rects[i];
|
|
181
|
+
if (q.owner !== owner || !owner) c += rectArea(q.r, r) * 2;
|
|
182
|
+
}
|
|
183
|
+
for (const i of near(indexOf(this.slants, polyBox), ...rb)) c += polyRectArea(this.slants[i], r) * 2;
|
|
184
|
+
if (lineW) {
|
|
185
|
+
let line = -1, a = 0;
|
|
186
|
+
for (const id of near(segmentsOf(this.lines), ...rb)) {
|
|
187
|
+
const li = Math.floor(id / 65536);
|
|
188
|
+
if (li !== line) {
|
|
189
|
+
if (line >= 0) c += a * lineW;
|
|
190
|
+
line = li;
|
|
191
|
+
a = 0;
|
|
192
|
+
}
|
|
193
|
+
const pts = this.lines[li].pts, i = id % 65536, p = pts[i], q = pts[i + 1];
|
|
194
|
+
const len = Math.hypot(q[0] - p[0], q[1] - p[1]), n = Math.max(1, Math.ceil(len / 4));
|
|
195
|
+
const [j0, j1] = sampleRange(p, q, n, rb);
|
|
196
|
+
for (let j = j0; j <= j1; j++) {
|
|
197
|
+
const x = p[0] + (q[0] - p[0]) * (j + 0.5) / n, y = p[1] + (q[1] - p[1]) * (j + 0.5) / n;
|
|
198
|
+
if (x >= r.x && x <= r.x + r.w && y >= r.y && y <= r.y + r.h) a += len / n * 4;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (line >= 0) c += a * lineW;
|
|
202
|
+
}
|
|
203
|
+
return c;
|
|
204
|
+
}
|
|
205
|
+
/** a copy whose added rectangles do not reach this one (callouts of one step) */
|
|
206
|
+
fork() {
|
|
207
|
+
const o = new Obstacles();
|
|
208
|
+
o.budget = this.budget;
|
|
209
|
+
o.polys = this.polys;
|
|
210
|
+
o.lines = this.lines;
|
|
211
|
+
o.solids = this.solids;
|
|
212
|
+
o.rects = [...this.rects];
|
|
213
|
+
o.slants = this.slants;
|
|
214
|
+
return o;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const LINE = 14;
|
|
218
|
+
function labelRect(t, x, y, anchor) {
|
|
219
|
+
const w = Math.max(t.name ? textWidth(t.name, FONTS.lbl) : 0, t.sub ? textWidth(t.sub, FONTS.sub) : 0) + 4;
|
|
220
|
+
const lines = (t.name ? 1 : 0) + (t.sub ? 1 : 0);
|
|
221
|
+
const top = y - (t.name ? 11 : 9) - 1;
|
|
222
|
+
const h = (lines - 1) * LINE + (t.name ? 11 : 9) + 4;
|
|
223
|
+
const left = anchor === "start" ? x - 2 : anchor === "end" ? x - w + 2 : x - w / 2;
|
|
224
|
+
return { x: left, y: top, w, h };
|
|
225
|
+
}
|
|
226
|
+
function placeNodeLabel(t, box, own, obs, owner, preferred) {
|
|
227
|
+
const sb = polyBounds(own);
|
|
228
|
+
const cx = iso((box.x0 + box.x1) / 2, (box.y0 + box.y1) / 2, box.z1)[0];
|
|
229
|
+
const lines = (t.name ? 1 : 0) + (t.sub ? 1 : 0);
|
|
230
|
+
const blockBelowBaseline = (lines - 1) * LINE + 4;
|
|
231
|
+
const aboveY = sb.y - 6 - blockBelowBaseline;
|
|
232
|
+
const midY = sb.y + Math.min(sb.h, 60) * 0.45;
|
|
233
|
+
const cands = [];
|
|
234
|
+
if (preferred) cands.push({ ...preferred, pen: 0 });
|
|
235
|
+
cands.push({ x: cx, y: aboveY, anchor: "middle", pen: preferred ? 20 : 0 });
|
|
236
|
+
for (const up of [12, 24, 40]) cands.push({ x: cx, y: aboveY - up, anchor: "middle", pen: 20 + up });
|
|
237
|
+
for (const dx of [-30, 30, -60, 60]) cands.push({ x: cx + dx, y: aboveY - 4, anchor: "middle", pen: 30 + Math.abs(dx) });
|
|
238
|
+
cands.push({ x: sb.x + sb.w + 8, y: midY, anchor: "start", pen: 120 });
|
|
239
|
+
cands.push({ x: sb.x - 8, y: midY, anchor: "end", pen: 120 });
|
|
240
|
+
cands.push({ x: cx, y: sb.y + sb.h + 14, anchor: "middle", pen: 300 });
|
|
241
|
+
let best, bestCost = Infinity;
|
|
242
|
+
for (const c of cands) {
|
|
243
|
+
const rect = labelRect(t, c.x, c.y, c.anchor);
|
|
244
|
+
const cost = obs.cost(rect, owner, 0.15) * 2 + c.pen;
|
|
245
|
+
if (cost < bestCost) {
|
|
246
|
+
bestCost = cost;
|
|
247
|
+
best = { x: c.x, y: c.y, anchor: c.anchor, rect };
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return best;
|
|
251
|
+
}
|
|
252
|
+
function calloutRect(w, x, y, anchor) {
|
|
253
|
+
const left = anchor === "start" ? x : anchor === "end" ? x - w : x - w / 2;
|
|
254
|
+
return { x: left - 2, y: y - 14, w: w + 4, h: 28 };
|
|
255
|
+
}
|
|
256
|
+
function placeCallout(w, ax, ay, preferred, obs) {
|
|
257
|
+
const cands = [{ ...preferred, pen: 0 }];
|
|
258
|
+
for (const dy of [-46, -66, -86, -106, -126, -146]) for (const dx of [0, -70, 70, -140, 140]) {
|
|
259
|
+
cands.push({ x: ax + dx, y: ay + dy, anchor: "middle", pen: 10 + (-dy - 46) * 0.5 + Math.abs(dx) * 0.4 });
|
|
260
|
+
}
|
|
261
|
+
for (const dy of [34, 60, 86]) cands.push({ x: ax, y: ay + dy, anchor: "middle", pen: 60 + dy * 0.3 });
|
|
262
|
+
const pdx = preferred.x - ax, pdy = preferred.y - ay;
|
|
263
|
+
for (const k of [-1, 1]) for (const d of [16, 32, 48]) cands.push({ ...preferred, y: ay + pdy + k * d, x: ax + pdx, pen: 5 + d });
|
|
264
|
+
for (const k of [-1, 1]) for (const d of [30, 60, 90]) for (const e of [0, 16, -16]) cands.push({ ...preferred, x: ax + pdx + k * d, y: ay + pdy + e, pen: 8 + d * 0.6 + Math.abs(e) });
|
|
265
|
+
let best, bestCost = Infinity;
|
|
266
|
+
for (const c of cands) {
|
|
267
|
+
const rect = calloutRect(w, c.x, c.y, c.anchor);
|
|
268
|
+
const cost = obs.cost(rect, void 0, 0.05) * 3 + c.pen;
|
|
269
|
+
if (cost < bestCost) {
|
|
270
|
+
bestCost = cost;
|
|
271
|
+
best = { x: c.x, y: c.y, anchor: c.anchor, rect };
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return best;
|
|
275
|
+
}
|
|
276
|
+
const area = (p) => {
|
|
277
|
+
let a = 0;
|
|
278
|
+
for (let i = 0; i < p.length; i++) {
|
|
279
|
+
const u = p[i], v = p[(i + 1) % p.length];
|
|
280
|
+
a += u[0] * v[1] - v[0] * u[1];
|
|
281
|
+
}
|
|
282
|
+
return a / 2;
|
|
283
|
+
};
|
|
284
|
+
const ccw = (p) => area(p) < 0 ? [...p].reverse() : p;
|
|
285
|
+
function inConvex(poly, q) {
|
|
286
|
+
const p = ccw(poly);
|
|
287
|
+
for (let i = 0; i < p.length; i++) {
|
|
288
|
+
const a = p[i], b = p[(i + 1) % p.length];
|
|
289
|
+
if ((b[0] - a[0]) * (q[1] - a[1]) - (b[1] - a[1]) * (q[0] - a[0]) < 0) return false;
|
|
290
|
+
}
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
function convexOverlap(a, b) {
|
|
294
|
+
let out = ccw(a);
|
|
295
|
+
const c = ccw(b);
|
|
296
|
+
for (let i = 0; i < c.length && out.length; i++) {
|
|
297
|
+
const e0 = c[i], e1 = c[(i + 1) % c.length];
|
|
298
|
+
const side = (p) => (e1[0] - e0[0]) * (p[1] - e0[1]) - (e1[1] - e0[1]) * (p[0] - e0[0]);
|
|
299
|
+
const inp = out;
|
|
300
|
+
out = [];
|
|
301
|
+
for (let j = 0; j < inp.length; j++) {
|
|
302
|
+
const p = inp[j], q = inp[(j + 1) % inp.length], sp = side(p), sq = side(q);
|
|
303
|
+
if (sp >= 0) out.push(p);
|
|
304
|
+
if (sp >= 0 !== sq >= 0) {
|
|
305
|
+
const t = sp / (sp - sq);
|
|
306
|
+
out.push([p[0] + (q[0] - p[0]) * t, p[1] + (q[1] - p[1]) * t]);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return out.length >= 3 ? Math.abs(area(out)) : 0;
|
|
311
|
+
}
|
|
312
|
+
export {
|
|
313
|
+
Obstacles,
|
|
314
|
+
calloutRect,
|
|
315
|
+
convexOverlap,
|
|
316
|
+
inConvex,
|
|
317
|
+
labelRect,
|
|
318
|
+
lineRectArea,
|
|
319
|
+
placeCallout,
|
|
320
|
+
placeNodeLabel,
|
|
321
|
+
polyBounds,
|
|
322
|
+
polyRectArea,
|
|
323
|
+
rectArea,
|
|
324
|
+
silhouette
|
|
325
|
+
};
|