Skip to content
Source
Sign inSign up
samwillis/neonbricks
Open History to see the last commit

snapping.ts

117 lines4.4 KB

1import { BY_KEY } from "../shared/catalogue";
2import { bounds, connectorList, enclosing, mating } from "../shared/geometry";
3import type { Part, Position } from "../shared/types";
4import { fitsScene } from "./movement";
5​
6// Points use the database's sixth-stud units; normals are world-space vectors.
7export type SnapSurface = { part?: Part; point: Position; normal: Position };
8export type DragSurface = {
9 surface: SnapSurface;
10 delta: Position;
11 fine: boolean;
12};
13type Options = {
14 excluded?: string[];
15 autoHeight?: boolean;
16 fine?: boolean;
17 rotate?: boolean;
18};
19const distance = (a: Position, b: Position) =>
20 (a.x - b.x) ** 2 + (a.y - b.y) ** 2 + (a.z - b.z) ** 2;
21const translated = (parts: Part[], delta: Position) =>
22 parts.map((p) => ({
23 ...p,
24 position: {
25 x: p.position.x + delta.x,
26 y: p.position.y + delta.y,
27 z: p.position.z + delta.z,
28 },
29 }));
30​
31export function snapToSurface(
32 sources: Part[],
33 surface: SnapSurface,
34 desired: Position,
35 scene: Part[],
36 options: Options = {},
37): { delta: Position; parts: Part[] } {
38 const automatic = options.autoHeight !== false;
39 const box = enclosing(sources, 0);
40 const target = surface.part;
41 const targetBox = target ? bounds(target) : null;
42 const top =
43 surface.normal.y > 0.5 ||
44 (!!targetBox && surface.point.y >= targetBox.maxy - 1);
45 const bottom = surface.normal.y < -0.5;
46 // Catch studs from the upper side of a brick too: users should not have to
47 // aim at a narrow top polygon or adjust a separate build-layer control.
48 const topEdge =
49 automatic && !bottom && !!targetBox && surface.point.y > targetBox.maxy - 3;
50 const fallback = { ...desired };
51 if (automatic && top) fallback.y = (targetBox?.maxy || 0) - box.miny;
52 else if (automatic && bottom && targetBox)
53 fallback.y = targetBox.miny - box.maxy;
54 else if (targetBox) {
55 if (surface.normal.x > 0.5) fallback.x = targetBox.maxx - box.minx;
56 if (surface.normal.x < -0.5) fallback.x = targetBox.minx - box.maxx;
57 if (surface.normal.z > 0.5) fallback.z = targetBox.maxz - box.minz;
58 if (surface.normal.z < -0.5) fallback.z = targetBox.minz - box.maxz;
59 }
60 if (!target) return { delta: fallback, parts: translated(sources, fallback) };
61​
62 const targets = connectorList(target).filter((c) => {
63 if (c.type === "stud") return (top || topEdge) && !options.fine;
64 if (c.type === "socket") return bottom && !options.fine;
65 return distance(c.at, surface.point) <= 8 ** 2;
66 });
67 const variants =
68 options.rotate &&
69 sources.length === 1 &&
70 BY_KEY[sources[0].key].category === "Mechanics"
71 ? [0, 1, 2, 3].map((rotation) => [{ ...sources[0], rotation }])
72 : [sources];
73 const candidates: { delta: Position; sources: Part[]; score: number }[] = [];
74 for (const parts of variants)
75 for (const source of parts)
76 for (const local of connectorList(source))
77 for (const remote of targets) {
78 if (!mating({ ...local, at: remote.at }, remote)) continue;
79 const delta = {
80 x: remote.at.x - local.at.x,
81 y: remote.at.y - local.at.y,
82 z: remote.at.z - local.at.z,
83 };
84 if (!automatic && delta.y !== desired.y) continue;
85 const vertical = local.type === "stud" || local.type === "socket";
86 const preference =
87 local.type === "socket" && topEdge
88 ? { ...desired, y: targetBox!.maxy - box.miny }
89 : vertical
90 ? fallback
91 : desired;
92 const error = distance(delta, preference);
93 // Magnets stay close to the intended pose instead of jumping to a
94 // remote connector on a large part or another member of a group.
95 if (error > (vertical ? 6 : 12) ** 2) continue;
96 candidates.push({
97 delta,
98 sources: parts,
99 score:
100 error +
101 distance(remote.at, surface.point) * 0.05 +
102 (parts[0].rotation === sources[0].rotation ? 0 : 0.01),
103 });
104 }
105 candidates.sort((a, b) => a.score - b.score);
106 const fit = candidates.find((c) =>
107 fitsScene(translated(c.sources, c.delta), scene, options.excluded),
108 );
109 // Keep an occupied attachment red so releasing cannot silently drop beneath it.
110 const candidate = fit || candidates[0];
111 if (candidate)
112 return {
113 delta: candidate.delta,
114 parts: translated(candidate.sources, candidate.delta),
115 };
116 return { delta: fallback, parts: translated(sources, fallback) };