@floless/app 0.138.2 → 0.139.1
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/autostart.mjs +6 -2
- package/desktop-access-production-manifest.mjs +7 -0
- package/dist/floless-server.cjs +157207 -70887
- package/dist/schemas/model.continuation.v1.schema.json +19 -0
- package/dist/schemas/model.continuation.v2.schema.json +19 -0
- package/dist/schemas/model.query-response.v1.schema.json +95 -0
- package/dist/schemas/model.query-response.v2.schema.json +80 -0
- package/dist/schemas/model.query.v1.schema.json +87 -0
- package/dist/schemas/model.query.v2.schema.json +87 -0
- package/dist/schemas/model.snap-candidate.v1.schema.json +48 -0
- package/dist/schemas/model.snap-constraint.v1.schema.json +18 -0
- package/dist/schemas/model.snap-query.v1.schema.json +24 -0
- package/dist/schemas/model.snap-result.v1.schema.json +33 -0
- package/dist/schemas/model.snapshot.v1.schema.json +136 -0
- package/dist/schemas/revit.model.v1.schema.json +24 -0
- package/dist/schemas/steel.model.v2.schema.json +45 -6
- package/dist/skills/floless-app-approval/SKILL.md +19 -5
- package/dist/skills/floless-app-bridge/SKILL.md +19 -6
- package/dist/skills/floless-app-knowledge/SKILL.md +22 -1
- package/dist/skills/floless-app-new-workflow/SKILL.md +31 -27
- package/dist/skills/floless-app-new-workflow/references/build-and-verify.md +13 -12
- package/dist/skills/floless-app-onboarding/SKILL.md +3 -3
- package/dist/skills/floless-app-onboarding/references/floless-app-tour.md +16 -9
- package/dist/skills/floless-app-steel-model/SKILL.md +11 -2
- package/dist/skills/floless-app-steel-model/references/detail-from-a-screenshot.md +95 -0
- package/dist/skills/floless-app-steel-takeoff/SKILL.md +142 -21
- package/dist/skills/floless-app-steel-takeoff/references/elevation-registration.md +19 -0
- package/dist/skills/floless-app-tweak-contract/SKILL.md +42 -8
- package/dist/skills/floless-app-vectorize/SKILL.md +19 -6
- package/dist/skills/floless-app-workflows/SKILL.md +30 -3
- package/dist/skills/floless-app-workflows/references/workspace-data.md +77 -4
- package/dist/templates/floless-workspace-model-report.flo +77 -0
- package/dist/templates/revit-model.flo +111 -0
- package/dist/web/app.css +415 -177
- package/dist/web/app.js +302 -51
- package/dist/web/aware.js +1357 -1032
- package/dist/web/chat.css +692 -0
- package/dist/web/chat.js +2651 -0
- package/dist/web/commands-revit.js +42 -0
- package/dist/web/commands-shell.js +531 -70
- package/dist/web/commands-steel.js +344 -146
- package/dist/web/commands.js +1 -1
- package/dist/web/index.html +283 -77
- package/dist/web/instructor.js +217 -36
- package/dist/web/left-pane.js +636 -0
- package/dist/web/model-artifact-loader.js +75 -0
- package/dist/web/model-authoring-gestures.js +141 -0
- package/dist/web/model-cad-tools.js +20 -0
- package/dist/web/model-clip-gesture.js +148 -0
- package/dist/web/model-clip-list.js +156 -0
- package/dist/web/model-clip-session.js +274 -0
- package/dist/web/model-clip-three-overlay.js +295 -0
- package/dist/web/model-clip-three-preview.js +59 -0
- package/dist/web/model-editor-contract.js +519 -0
- package/dist/web/model-editor-revit-adapter.js +139 -0
- package/dist/web/model-editor-shell.js +271 -0
- package/dist/web/model-editor-state-recovery.js +44 -0
- package/dist/web/model-editor-steel-adapter.js +415 -0
- package/dist/web/model-editor.css +268 -0
- package/dist/web/model-frame.js +192 -0
- package/dist/web/model-inspector.js +128 -0
- package/dist/web/model-measure.js +390 -0
- package/dist/web/model-navigation-controls.js +246 -0
- package/dist/web/model-orientation-overlay.js +217 -0
- package/dist/web/model-panel-bridge.js +230 -0
- package/dist/web/model-pointer-dock.js +164 -0
- package/dist/web/model-revit-module.js +83 -0
- package/dist/web/model-selection-coordinator.js +151 -0
- package/dist/web/model-selection-tools.js +290 -0
- package/dist/web/model-snap-contract.js +267 -0
- package/dist/web/model-snapping.js +497 -0
- package/dist/web/model-spatial-panel.js +294 -0
- package/dist/web/model-spatial-tools.js +249 -0
- package/dist/web/model-tree.js +47 -0
- package/dist/web/model-viewport-lighting.js +166 -0
- package/dist/web/model-viewport-revit-adapter.js +1224 -0
- package/dist/web/model-viewport-state.js +505 -0
- package/dist/web/model-viewport-steel-adapter.js +150 -0
- package/dist/web/model-viewport-toolbar.js +516 -0
- package/dist/web/model-viewport.js +717 -0
- package/dist/web/panels.js +8 -15
- package/dist/web/renderers.js +3 -0
- package/dist/web/revit-editor.css +35 -0
- package/dist/web/revit-editor.html +74 -0
- package/dist/web/revit-editor.js +845 -0
- package/dist/web/steel-3d-core.js +16 -44
- package/dist/web/steel-3d-view.js +1384 -633
- package/dist/web/steel-canvas-guidance.js +3 -1
- package/dist/web/steel-editor.html +2145 -1255
- package/dist/web/steel-filter.html +5 -1
- package/dist/web/steel-model-v2-commands.js +80 -3
- package/dist/web/steel-split-view.js +5 -39
- package/dist/web/steel-view-generation.js +107 -17
- package/dist/web/vector-3d.html +12 -0
- package/dist/web/workspaces.js +2154 -283
- package/distribution-profile-active.mjs +2 -0
- package/distribution-profile-production.mjs +47 -0
- package/distribution-profile-shared.mjs +8 -0
- package/distribution-profile.mjs +18 -0
- package/distribution-resource-inventory.mjs +81 -0
- package/launch.mjs +18 -10
- package/log.mjs +2 -1
- package/managed-runtime-policy.mjs +104 -0
- package/model-reader-policy-active.mjs +7 -0
- package/network-policy-active.mjs +6 -0
- package/package.json +17 -2
- package/post-update-marker.mjs +3 -1
- package/rvt-production-uat-authority.mjs +60 -0
- package/teardown.mjs +4 -3
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Provider-neutral clip collection and selection semantics for every 3D model viewer.
|
|
3
|
+
*
|
|
4
|
+
* Canonical geometry lives in createModelSpatialTools. This session adds only transient interaction
|
|
5
|
+
* state (selection and range anchor), stable creation names, edit boundaries, and keyboard commands.
|
|
6
|
+
* It deliberately knows nothing about Three.js, Steel, Revit, or any provider transport.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const clone = (value) => globalThis.structuredClone ? globalThis.structuredClone(value) : JSON.parse(JSON.stringify(value));
|
|
10
|
+
const freeze = (value) => {
|
|
11
|
+
if (!value || typeof value !== 'object' || Object.isFrozen(value)) return value;
|
|
12
|
+
for (const item of Object.values(value)) freeze(item);
|
|
13
|
+
return Object.freeze(value);
|
|
14
|
+
};
|
|
15
|
+
const snapshot = (value) => freeze(clone(value));
|
|
16
|
+
const editableTarget = (target) => {
|
|
17
|
+
const tag = String(target?.tagName ?? '').toLowerCase();
|
|
18
|
+
return target?.isContentEditable === true || tag === 'input' || tag === 'textarea' || tag === 'select';
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {{
|
|
23
|
+
* tools:any,
|
|
24
|
+
* onBeginEdit?:(reason:string, state:any)=>void,
|
|
25
|
+
* keyboardTarget?:EventTarget|null,
|
|
26
|
+
* placementOwnsKeyboard?:()=>boolean,
|
|
27
|
+
* dragOwnsKeyboard?:()=>boolean,
|
|
28
|
+
* anotherCommandOwnsKeyboard?:()=>boolean,
|
|
29
|
+
* onArmPlane?:(event:KeyboardEvent)=>boolean|void,
|
|
30
|
+
* onArmBox?:(event:KeyboardEvent)=>boolean|void,
|
|
31
|
+
* }} options
|
|
32
|
+
*/
|
|
33
|
+
export function createModelClipSession({
|
|
34
|
+
tools,
|
|
35
|
+
onBeginEdit = () => {},
|
|
36
|
+
keyboardTarget = null,
|
|
37
|
+
placementOwnsKeyboard = () => false,
|
|
38
|
+
dragOwnsKeyboard = () => false,
|
|
39
|
+
anotherCommandOwnsKeyboard = () => false,
|
|
40
|
+
onArmPlane = null,
|
|
41
|
+
onArmBox = null,
|
|
42
|
+
} = {}) {
|
|
43
|
+
if (!tools?.state || !tools?.subscribe || !tools?.addClipPlane || !tools?.addClipBox || !tools?.updateClip || !tools?.removeClip || !tools?.clearClips) {
|
|
44
|
+
throw new TypeError('Clip session requires createModelSpatialTools APIs.');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const listeners = new Set();
|
|
48
|
+
let selectedIds = new Set();
|
|
49
|
+
let rangeAnchorId = null;
|
|
50
|
+
let sequence = 0;
|
|
51
|
+
let disposed = false;
|
|
52
|
+
let mutationDepth = 0;
|
|
53
|
+
let pendingReason = null;
|
|
54
|
+
|
|
55
|
+
const clips = () => tools.state().clips ?? [];
|
|
56
|
+
const orderedIds = () => clips().map((clip) => clip.id);
|
|
57
|
+
const syncSequence = () => {
|
|
58
|
+
for (const clip of clips()) {
|
|
59
|
+
const match = /^clip-(\d+)$/.exec(String(clip.id));
|
|
60
|
+
if (match) sequence = Math.max(sequence, Number(match[1]));
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const pruneSelection = () => {
|
|
64
|
+
const available = new Set(orderedIds());
|
|
65
|
+
selectedIds = new Set([...selectedIds].filter((id) => available.has(id)));
|
|
66
|
+
if (!available.has(rangeAnchorId)) rangeAnchorId = null;
|
|
67
|
+
};
|
|
68
|
+
const current = () => snapshot({ clips: clips(), selectedIds: [...selectedIds], rangeAnchorId });
|
|
69
|
+
const emit = (reason) => {
|
|
70
|
+
if (disposed) return current();
|
|
71
|
+
pruneSelection();
|
|
72
|
+
const value = current();
|
|
73
|
+
for (const listener of listeners) {
|
|
74
|
+
try { listener(value, reason); } catch (_) { /* subscribers cannot break clip interaction */ }
|
|
75
|
+
}
|
|
76
|
+
return value;
|
|
77
|
+
};
|
|
78
|
+
const publish = (reason) => {
|
|
79
|
+
if (mutationDepth) { pendingReason = reason; return current(); }
|
|
80
|
+
return emit(reason);
|
|
81
|
+
};
|
|
82
|
+
const beginEdit = (reason) => {
|
|
83
|
+
try { onBeginEdit(reason, current()); } catch (_) { /* undo/reporting cannot block the edit */ }
|
|
84
|
+
};
|
|
85
|
+
const transact = (reason, operation) => {
|
|
86
|
+
mutationDepth += 1;
|
|
87
|
+
try { beginEdit(reason); return operation(); }
|
|
88
|
+
finally {
|
|
89
|
+
mutationDepth -= 1;
|
|
90
|
+
if (!mutationDepth) { pendingReason = null; emit(reason); }
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const nextIdentity = (kind) => {
|
|
94
|
+
const usedIds = new Set(orderedIds());
|
|
95
|
+
const usedLabels = new Set(clips().map((clip) => String(clip.label).toLowerCase()));
|
|
96
|
+
let id, label;
|
|
97
|
+
do {
|
|
98
|
+
id = `clip-${++sequence}`;
|
|
99
|
+
label = `${kind === 'plane' ? 'Plane' : 'Box'} ${sequence}`;
|
|
100
|
+
} while (usedIds.has(id) || usedLabels.has(label.toLowerCase()));
|
|
101
|
+
return { id, label };
|
|
102
|
+
};
|
|
103
|
+
const clipById = (id) => clips().find((clip) => clip.id === id) ?? null;
|
|
104
|
+
const labelAvailable = (label, exceptId = null) => {
|
|
105
|
+
const next = String(label ?? '').trim();
|
|
106
|
+
return !!next && !clips().some((clip) => clip.id !== exceptId && String(clip.label).toLowerCase() === next.toLowerCase());
|
|
107
|
+
};
|
|
108
|
+
const checkedPatch = (id, patch) => {
|
|
109
|
+
if (!Object.hasOwn(patch ?? {}, 'label')) return patch;
|
|
110
|
+
const label = String(patch.label ?? '').trim();
|
|
111
|
+
if (!labelAvailable(label, id)) throw new TypeError('Clip names must be unique and non-empty.');
|
|
112
|
+
return { ...patch, label };
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
syncSequence();
|
|
116
|
+
const unsubscribeTools = tools.subscribe((_state, reason) => {
|
|
117
|
+
syncSequence();
|
|
118
|
+
publish(reason === 'replay' ? 'spatial-replay' : reason);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const api = {
|
|
122
|
+
state: current,
|
|
123
|
+
clips: () => snapshot(clips()),
|
|
124
|
+
selected: () => [...selectedIds],
|
|
125
|
+
isSelected: (id) => selectedIds.has(id),
|
|
126
|
+
enabledState() {
|
|
127
|
+
const values = clips(); if (!values.length) return 'off';
|
|
128
|
+
const enabled = values.filter((clip) => clip.enabled !== false).length;
|
|
129
|
+
return enabled === 0 ? 'off' : enabled === values.length ? 'on' : 'mixed';
|
|
130
|
+
},
|
|
131
|
+
subscribe(listener) {
|
|
132
|
+
if (disposed) throw Object.assign(new Error('Clip session is disposed.'), { code: 'E_MODEL_CLIP_SESSION_DISPOSED' });
|
|
133
|
+
if (typeof listener !== 'function') throw new TypeError('Clip subscriber must be a function.');
|
|
134
|
+
listeners.add(listener);
|
|
135
|
+
try { listener(current(), 'replay'); } catch (_) { /* replay is isolated like later publications */ }
|
|
136
|
+
let active = true;
|
|
137
|
+
return () => { if (active) { active = false; listeners.delete(listener); } };
|
|
138
|
+
},
|
|
139
|
+
/** @param {{id?:string|null,label?:string|null,origin:number[],normal:number[],enabled?:boolean}} value */
|
|
140
|
+
addPlane(value = /** @type {any} */ ({})) {
|
|
141
|
+
const { id = null, label = null, origin, normal, enabled = true } = value;
|
|
142
|
+
const suppliedId = String(id ?? '').trim(); const generated = suppliedId ? null : nextIdentity('plane');
|
|
143
|
+
const nextId = suppliedId || generated.id;
|
|
144
|
+
const suppliedLabel = String(label ?? '').trim(); const nextLabel = suppliedLabel || generated?.label || nextId;
|
|
145
|
+
if (!labelAvailable(nextLabel)) throw new TypeError('Clip names must be unique and non-empty.');
|
|
146
|
+
return transact('clip-add', () => {
|
|
147
|
+
tools.addClipPlane({ id: nextId, label: nextLabel, origin, normal, enabled });
|
|
148
|
+
selectedIds = new Set([nextId]); rangeAnchorId = nextId;
|
|
149
|
+
return nextId;
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
/** @param {{id?:string|null,label?:string|null,min:number[],max:number[],enabled?:boolean}} value */
|
|
153
|
+
addBox(value = /** @type {any} */ ({})) {
|
|
154
|
+
const { id = null, label = null, min, max, enabled = true } = value;
|
|
155
|
+
const suppliedId = String(id ?? '').trim(); const generated = suppliedId ? null : nextIdentity('box');
|
|
156
|
+
const nextId = suppliedId || generated.id;
|
|
157
|
+
const suppliedLabel = String(label ?? '').trim(); const nextLabel = suppliedLabel || generated?.label || nextId;
|
|
158
|
+
if (!labelAvailable(nextLabel)) throw new TypeError('Clip names must be unique and non-empty.');
|
|
159
|
+
return transact('clip-add', () => {
|
|
160
|
+
tools.addClipBox({ id: nextId, label: nextLabel, min, max, enabled });
|
|
161
|
+
selectedIds = new Set([nextId]); rangeAnchorId = nextId;
|
|
162
|
+
return nextId;
|
|
163
|
+
});
|
|
164
|
+
},
|
|
165
|
+
select(id, event = {}) {
|
|
166
|
+
const ids = orderedIds(); if (!ids.includes(id)) return current();
|
|
167
|
+
const additive = event.ctrlKey === true || event.metaKey === true;
|
|
168
|
+
if (event.shiftKey === true && rangeAnchorId && ids.includes(rangeAnchorId)) {
|
|
169
|
+
const start = ids.indexOf(rangeAnchorId), end = ids.indexOf(id);
|
|
170
|
+
selectedIds = new Set(ids.slice(Math.min(start, end), Math.max(start, end) + 1));
|
|
171
|
+
} else if (additive) {
|
|
172
|
+
const next = new Set(selectedIds); next.has(id) ? next.delete(id) : next.add(id);
|
|
173
|
+
selectedIds = next; rangeAnchorId = id;
|
|
174
|
+
} else {
|
|
175
|
+
selectedIds = selectedIds.size === 1 && selectedIds.has(id) ? new Set() : new Set([id]);
|
|
176
|
+
rangeAnchorId = id;
|
|
177
|
+
}
|
|
178
|
+
return emit('clip-selection');
|
|
179
|
+
},
|
|
180
|
+
setSelected(ids = [], { anchorId = null } = {}) {
|
|
181
|
+
const available = new Set(orderedIds());
|
|
182
|
+
selectedIds = new Set([...new Set(ids)].filter((id) => available.has(id)));
|
|
183
|
+
rangeAnchorId = available.has(anchorId) ? anchorId : ([...selectedIds].at(-1) ?? null);
|
|
184
|
+
return emit('clip-selection');
|
|
185
|
+
},
|
|
186
|
+
clearSelection() {
|
|
187
|
+
if (!selectedIds.size && rangeAnchorId === null) return current();
|
|
188
|
+
selectedIds.clear(); rangeAnchorId = null; return emit('clip-selection-clear');
|
|
189
|
+
},
|
|
190
|
+
rename(id, name) {
|
|
191
|
+
const clip = clipById(id); if (!clip) return false;
|
|
192
|
+
const next = String(name ?? '').trim();
|
|
193
|
+
if (!labelAvailable(next, id)) return false;
|
|
194
|
+
if (clip.label === next) return true;
|
|
195
|
+
transact('clip-rename', () => tools.updateClip(id, { label: next }));
|
|
196
|
+
return true;
|
|
197
|
+
},
|
|
198
|
+
setEnabled(id, enabled) {
|
|
199
|
+
const clip = clipById(id); if (!clip) return false;
|
|
200
|
+
const next = enabled === true; if (clip.enabled === next) return true;
|
|
201
|
+
transact('clip-enabled', () => tools.updateClip(id, { enabled: next }));
|
|
202
|
+
return true;
|
|
203
|
+
},
|
|
204
|
+
toggle(id) { const clip = clipById(id); return clip ? api.setEnabled(id, clip.enabled === false) : false; },
|
|
205
|
+
setAllEnabled(enabled) {
|
|
206
|
+
const next = enabled === true; const changed = clips().filter((clip) => (clip.enabled !== false) !== next);
|
|
207
|
+
if (!changed.length) return 0;
|
|
208
|
+
transact('clip-enabled-all', () => { for (const clip of changed) tools.updateClip(clip.id, { enabled: next }); });
|
|
209
|
+
return changed.length;
|
|
210
|
+
},
|
|
211
|
+
toggleAllEnabled() { return api.setAllEnabled(api.enabledState() !== 'on'); },
|
|
212
|
+
update(id, patch) {
|
|
213
|
+
if (!clipById(id)) return false;
|
|
214
|
+
transact('clip-update', () => tools.updateClip(id, checkedPatch(id, patch))); return true;
|
|
215
|
+
},
|
|
216
|
+
beginLogicalEdit(reason = 'clip-update') {
|
|
217
|
+
let started = false, closed = false;
|
|
218
|
+
const ensureStarted = () => { if (!started) { started = true; beginEdit(reason); } };
|
|
219
|
+
return Object.freeze({
|
|
220
|
+
update(id, patch) {
|
|
221
|
+
if (closed || !clipById(id)) return false;
|
|
222
|
+
const next = checkedPatch(id, patch); ensureStarted(); tools.updateClip(id, next); return true;
|
|
223
|
+
},
|
|
224
|
+
finish() { closed = true; return started; },
|
|
225
|
+
get started() { return started; },
|
|
226
|
+
});
|
|
227
|
+
},
|
|
228
|
+
remove(id) {
|
|
229
|
+
if (!clipById(id)) return false;
|
|
230
|
+
transact('clip-remove', () => { selectedIds.delete(id); if (rangeAnchorId === id) rangeAnchorId = null; tools.removeClip(id); });
|
|
231
|
+
return true;
|
|
232
|
+
},
|
|
233
|
+
clear() {
|
|
234
|
+
if (!clips().length) { api.clearSelection(); return false; }
|
|
235
|
+
transact('clip-clear', () => { selectedIds.clear(); rangeAnchorId = null; tools.clearClips(); });
|
|
236
|
+
return true;
|
|
237
|
+
},
|
|
238
|
+
deleteSelected() {
|
|
239
|
+
const ids = orderedIds().filter((id) => selectedIds.has(id)); if (!ids.length) return 0;
|
|
240
|
+
transact('clip-delete-selected', () => {
|
|
241
|
+
selectedIds.clear(); rangeAnchorId = null;
|
|
242
|
+
for (const id of ids) tools.removeClip(id);
|
|
243
|
+
});
|
|
244
|
+
return ids.length;
|
|
245
|
+
},
|
|
246
|
+
handleKeydown(event) {
|
|
247
|
+
if (disposed || editableTarget(event?.target)) return false;
|
|
248
|
+
const owned = placementOwnsKeyboard() === true || dragOwnsKeyboard() === true || anotherCommandOwnsKeyboard() === true;
|
|
249
|
+
if (event?.key === 'Escape') {
|
|
250
|
+
if (owned || !selectedIds.size) return false;
|
|
251
|
+
event.preventDefault?.(); event.stopPropagation?.(); api.clearSelection(); return true;
|
|
252
|
+
}
|
|
253
|
+
if ((event?.key === 'Delete' || event?.key === 'Backspace') && !owned && selectedIds.size) {
|
|
254
|
+
event.preventDefault?.(); event.stopPropagation?.(); api.deleteSelected(); return true;
|
|
255
|
+
}
|
|
256
|
+
const key = String(event?.key ?? '').toLowerCase();
|
|
257
|
+
const shortcut = key === 'x' ? onArmPlane : key === 'b' ? onArmBox : null;
|
|
258
|
+
if (shortcut && event?.shiftKey === true && event?.ctrlKey !== true && event?.metaKey !== true && event?.altKey !== true && event?.repeat !== true && !owned) {
|
|
259
|
+
if (shortcut(event) === false) return false;
|
|
260
|
+
event.preventDefault?.(); event.stopPropagation?.(); return true;
|
|
261
|
+
}
|
|
262
|
+
return false;
|
|
263
|
+
},
|
|
264
|
+
serialize: () => tools.serialize(),
|
|
265
|
+
dispose() {
|
|
266
|
+
if (disposed) return; disposed = true; unsubscribeTools(); listeners.clear();
|
|
267
|
+
keyboardTarget?.removeEventListener?.('keydown', keydown);
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const keydown = (event) => api.handleKeydown(event);
|
|
272
|
+
keyboardTarget?.addEventListener?.('keydown', keydown);
|
|
273
|
+
return Object.freeze(api);
|
|
274
|
+
}
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/* Provider-neutral Three.js clipping overlay and handle-drag lifecycle. */
|
|
2
|
+
|
|
3
|
+
import { FT_TO_MM } from './steel-units.js';
|
|
4
|
+
|
|
5
|
+
export const MODEL_CLIP_PLANE_HALF_SIZE_MM = FT_TO_MM;
|
|
6
|
+
export const MODEL_CLIP_HANDLE_HIT_PX = 16;
|
|
7
|
+
export const MODEL_CLIP_MIN_EXTENT_MM = 1;
|
|
8
|
+
|
|
9
|
+
const FACE_AXES = Object.freeze([
|
|
10
|
+
Object.freeze({ axis: 'x', sign: 1 }), Object.freeze({ axis: 'x', sign: -1 }),
|
|
11
|
+
Object.freeze({ axis: 'y', sign: 1 }), Object.freeze({ axis: 'y', sign: -1 }),
|
|
12
|
+
Object.freeze({ axis: 'z', sign: 1 }), Object.freeze({ axis: 'z', sign: -1 }),
|
|
13
|
+
]);
|
|
14
|
+
const AXIS_INDEX = Object.freeze({ x: 0, y: 1, z: 2 });
|
|
15
|
+
|
|
16
|
+
const cloneData = (value) => globalThis.structuredClone
|
|
17
|
+
? globalThis.structuredClone(value)
|
|
18
|
+
: JSON.parse(JSON.stringify(value));
|
|
19
|
+
const finite3 = (value) => Array.isArray(value) && value.length === 3 && value.every(Number.isFinite);
|
|
20
|
+
const addScaled = (point, direction, amount) => point.map((value, axis) => value + direction[axis] * amount);
|
|
21
|
+
const magnitude = (value) => Math.hypot(value[0], value[1], value[2]);
|
|
22
|
+
const normalized = (value) => { const length = magnitude(value); return length > 0 ? value.map((part) => part / length) : null; };
|
|
23
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
24
|
+
const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
25
|
+
const planePointKey = (clip) => finite3(clip?.origin) ? 'origin' : 'point';
|
|
26
|
+
const planePoint = (clip) => clip?.[planePointKey(clip)];
|
|
27
|
+
const planeNormal = (clip) => clip?.normal || clip?.n;
|
|
28
|
+
|
|
29
|
+
export function modelClipPlaneCorners(point, normal, halfSizeMm = MODEL_CLIP_PLANE_HALF_SIZE_MM) {
|
|
30
|
+
const unit = normalized(normal); if (!finite3(point) || !unit) throw new TypeError('Plane corners need a finite point and normal.');
|
|
31
|
+
const reference = Math.abs(unit[2]) < 0.9 ? [0, 0, 1] : [1, 0, 0];
|
|
32
|
+
const u = normalized(cross(reference, unit)); const v = normalized(cross(unit, u));
|
|
33
|
+
return [[1, 1], [-1, 1], [-1, -1], [1, -1]].map(([du, dv]) => point.map((value, axis) => value + halfSizeMm * (du * u[axis] + dv * v[axis])));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function modelClipBoxEdgePoints(box) {
|
|
37
|
+
if (!box || !finite3(box.min) || !finite3(box.max)) throw new TypeError('Box edges need finite minimum and maximum points.');
|
|
38
|
+
const [x0, y0, z0] = box.min; const [x1, y1, z1] = box.max;
|
|
39
|
+
const corners = [[x0,y0,z0],[x1,y0,z0],[x1,y1,z0],[x0,y1,z0],[x0,y0,z1],[x1,y0,z1],[x1,y1,z1],[x0,y1,z1]];
|
|
40
|
+
return [[0,1],[1,2],[2,3],[3,0],[4,5],[5,6],[6,7],[7,4],[0,4],[1,5],[2,6],[3,7]].flatMap(([a, b]) => [corners[a], corners[b]]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Parameter on `linePoint + t * lineDirection` nearest to a ray. */
|
|
44
|
+
export function modelClipClosestLineParameter(linePoint, lineDirection, rayOrigin, rayDirection) {
|
|
45
|
+
const w0 = linePoint.map((value, axis) => value - rayOrigin[axis]);
|
|
46
|
+
const b = dot(lineDirection, rayDirection); const e = dot(rayDirection, w0);
|
|
47
|
+
const d = dot(lineDirection, w0); const cc = dot(rayDirection, rayDirection);
|
|
48
|
+
const denominator = cc - b * b;
|
|
49
|
+
return Math.abs(denominator) < 1e-6 ? d : (b * e - cc * d) / denominator;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Return the canonical clip produced by moving one plane/box-face handle. */
|
|
53
|
+
export function moveModelClipHandle(clip, handle, deltaMm, minimumExtentMm = MODEL_CLIP_MIN_EXTENT_MM) {
|
|
54
|
+
const next = cloneData(clip);
|
|
55
|
+
if (handle.kind === 'plane') {
|
|
56
|
+
const pointKey = planePointKey(next); const point = next[pointKey]; const normal = normalized(planeNormal(next));
|
|
57
|
+
if (!finite3(point) || !normal) throw new TypeError('A clip plane needs a finite point and normal.');
|
|
58
|
+
next[pointKey] = addScaled(point, normal, deltaMm);
|
|
59
|
+
return next;
|
|
60
|
+
}
|
|
61
|
+
const box = next.box || next; const index = AXIS_INDEX[handle.axis];
|
|
62
|
+
if (!box || !finite3(box.min) || !finite3(box.max) || index === undefined || ![-1, 1].includes(handle.sign)) {
|
|
63
|
+
throw new TypeError('A clip box handle needs finite bounds, axis, and sign.');
|
|
64
|
+
}
|
|
65
|
+
if (handle.sign > 0) box.max[index] = Math.max(box.max[index] + deltaMm, box.min[index] + minimumExtentMm);
|
|
66
|
+
else box.min[index] = Math.min(box.min[index] + deltaMm, box.max[index] - minimumExtentMm);
|
|
67
|
+
return next;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Draw and manipulate selected canonical-mm clip planes and boxes in an unclipped Three.js scene.
|
|
72
|
+
* Provider callbacks receive `{ clipId, clip, previous, handle, deltaMm, readout, event, reason? }`.
|
|
73
|
+
*/
|
|
74
|
+
export function createModelClipThreeOverlay({
|
|
75
|
+
THREE, overlayScene, canvas, camera = null, getCamera = () => camera, rayAt = null,
|
|
76
|
+
canonicalToRenderPoint = (point) => point, canonicalToRenderDirection = (direction) => direction,
|
|
77
|
+
renderToCanonicalPoint = (point) => point, getClips, getSelectedClipIds = null, isClipSelected = null,
|
|
78
|
+
preview = () => {}, update = () => {}, commit = () => {}, cancel = () => {}, readout = () => {},
|
|
79
|
+
formatDistance = (valueMm) => String(Math.round(valueMm)), setCursor = (value) => { canvas.style.cursor = value; },
|
|
80
|
+
isPlacementArmed = () => false,
|
|
81
|
+
planeHalfSizeMm = MODEL_CLIP_PLANE_HALF_SIZE_MM, minimumExtentMm = MODEL_CLIP_MIN_EXTENT_MM,
|
|
82
|
+
hitTolerancePx = MODEL_CLIP_HANDLE_HIT_PX, dragThresholdPx = 5,
|
|
83
|
+
planeColor = 0x3b82f6, boxColor = 0x93c5fd, hoverColor = 0xbfdbfe,
|
|
84
|
+
} = {}) {
|
|
85
|
+
if (!THREE || !overlayScene || !canvas || typeof getCamera !== 'function' || typeof getClips !== 'function') {
|
|
86
|
+
throw new TypeError('Clip overlay needs THREE, an overlay scene, a canvas, a camera getter, and clip getters.');
|
|
87
|
+
}
|
|
88
|
+
if (typeof getSelectedClipIds !== 'function' && typeof isClipSelected !== 'function') {
|
|
89
|
+
throw new TypeError('Clip overlay needs selected clip ids or an isClipSelected callback.');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const root = new THREE.Group(); root.name = 'model-clip-overlay'; overlayScene.add(root);
|
|
93
|
+
const hitObjects = []; const overrides = new Map(); let drag = null; let hoveredKey = null; let disposed = false;
|
|
94
|
+
const zAxis = new THREE.Vector3(0, 0, 1); const yAxis = new THREE.Vector3(0, 1, 0);
|
|
95
|
+
const vec = (value) => value?.isVector3 ? value.clone() : new THREE.Vector3(...value);
|
|
96
|
+
const array = (value) => value?.isVector3 ? value.toArray() : [...value];
|
|
97
|
+
const toRenderPoint = (point) => vec(canonicalToRenderPoint([...point]));
|
|
98
|
+
const toRenderDirection = (direction, at) => vec(canonicalToRenderDirection([...direction], at ? [...at] : null)).normalize();
|
|
99
|
+
const toCanonicalPoint = (point) => array(renderToCanonicalPoint(array(point)));
|
|
100
|
+
const selected = (clip) => typeof isClipSelected === 'function'
|
|
101
|
+
? !!isClipSelected(clip)
|
|
102
|
+
: new Set(getSelectedClipIds() || []).has(clip.id);
|
|
103
|
+
const clips = () => Array.from(getClips() || []);
|
|
104
|
+
const currentClip = (id) => overrides.get(id) || clips().find((clip) => clip.id === id) || null;
|
|
105
|
+
const disposeObject = (object) => {
|
|
106
|
+
object.traverse?.((child) => {
|
|
107
|
+
child.geometry?.dispose?.();
|
|
108
|
+
const materials = Array.isArray(child.material) ? child.material : child.material ? [child.material] : [];
|
|
109
|
+
for (const material of materials) material.dispose?.();
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
const clear = () => {
|
|
113
|
+
for (const child of [...root.children]) { root.remove(child); disposeObject(child); }
|
|
114
|
+
hitObjects.length = 0;
|
|
115
|
+
};
|
|
116
|
+
const material = (color, transparent = false, opacity = 1) => {
|
|
117
|
+
const value = new THREE.MeshBasicMaterial({ color, transparent, opacity, side: THREE.DoubleSide, depthTest: false, depthWrite: false });
|
|
118
|
+
return value;
|
|
119
|
+
};
|
|
120
|
+
const lineMaterial = (color) => new THREE.LineBasicMaterial({ color, depthTest: false, depthWrite: false, transparent: true, opacity: 1 });
|
|
121
|
+
const line = (points, color, loop = false) => {
|
|
122
|
+
const object = new (loop ? THREE.LineLoop : THREE.LineSegments)(new THREE.BufferGeometry().setFromPoints(points), lineMaterial(color));
|
|
123
|
+
object.renderOrder = 996; root.add(object); return object;
|
|
124
|
+
};
|
|
125
|
+
const handleKey = (handle) => `${handle.clipId}:${handle.kind}:${handle.axis || ''}:${handle.sign || ''}`;
|
|
126
|
+
const addHandle = (clip, anchorCanonical, normalCanonical, handle, color) => {
|
|
127
|
+
const anchor = toRenderPoint(anchorCanonical); const normal = toRenderDirection(normalCanonical, anchorCanonical);
|
|
128
|
+
const disc = new THREE.Mesh(new THREE.CircleGeometry(1, 32), material(color, true, 0.32));
|
|
129
|
+
disc.position.copy(anchor); disc.quaternion.setFromUnitVectors(zAxis, normal); disc.renderOrder = 998; root.add(disc);
|
|
130
|
+
const stem = new THREE.Line(new THREE.BufferGeometry().setFromPoints([anchor, anchor]), lineMaterial(color));
|
|
131
|
+
stem.renderOrder = 998; root.add(stem);
|
|
132
|
+
const cone = new THREE.Mesh(new THREE.ConeGeometry(0.5, 1.5, 18), material(color));
|
|
133
|
+
cone.quaternion.setFromUnitVectors(yAxis, normal); cone.renderOrder = 999; root.add(cone);
|
|
134
|
+
const descriptor = Object.freeze({ clipId: clip.id, ...handle });
|
|
135
|
+
const data = { descriptor, anchorCanonical: [...anchorCanonical], anchor, normal, disc, stem, cone, color };
|
|
136
|
+
hitObjects.push(data); disc.userData.modelClipHandle = data; cone.userData.modelClipHandle = data;
|
|
137
|
+
};
|
|
138
|
+
const renderBox = (clip, box) => {
|
|
139
|
+
line(modelClipBoxEdgePoints(box).map(toRenderPoint), boxColor);
|
|
140
|
+
const center = [0, 1, 2].map((axis) => (box.min[axis] + box.max[axis]) / 2);
|
|
141
|
+
for (const face of FACE_AXES) {
|
|
142
|
+
const axis = AXIS_INDEX[face.axis]; const anchor = [...center]; anchor[axis] = face.sign > 0 ? box.max[axis] : box.min[axis];
|
|
143
|
+
const normal = [0, 0, 0]; normal[axis] = face.sign;
|
|
144
|
+
addHandle(clip, anchor, normal, { kind: 'box', axis: face.axis, sign: face.sign }, boxColor);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const renderPlane = (clip) => {
|
|
148
|
+
const point = planePoint(clip); const normal = normalized(planeNormal(clip)); if (!finite3(point) || !normal) return;
|
|
149
|
+
line(modelClipPlaneCorners(point, normal, planeHalfSizeMm).map(toRenderPoint), planeColor, true);
|
|
150
|
+
addHandle(clip, point, normal, { kind: 'plane' }, planeColor);
|
|
151
|
+
};
|
|
152
|
+
const worldPerPixel = (point) => {
|
|
153
|
+
const activeCamera = getCamera(); const height = canvas.clientHeight || canvas.getBoundingClientRect?.().height || 1;
|
|
154
|
+
if (activeCamera?.isOrthographicCamera) return ((activeCamera.top - activeCamera.bottom) / (activeCamera.zoom || 1)) / height;
|
|
155
|
+
const distance = activeCamera?.position?.distanceTo?.(point) || 1;
|
|
156
|
+
const fov = Number(activeCamera?.fov) || 50;
|
|
157
|
+
return (2 * Math.tan((fov * Math.PI / 180) / 2) * distance) / height;
|
|
158
|
+
};
|
|
159
|
+
const resize = () => {
|
|
160
|
+
if (disposed) return;
|
|
161
|
+
for (const data of hitObjects) {
|
|
162
|
+
const discScale = worldPerPixel(data.anchor) * 14; data.disc.scale.setScalar(discScale);
|
|
163
|
+
const reach = worldPerPixel(data.anchor) * 34; const arrowPoint = data.anchor.clone().addScaledVector(data.normal, reach);
|
|
164
|
+
data.stem.geometry.setFromPoints([data.anchor, arrowPoint]);
|
|
165
|
+
data.cone.position.copy(arrowPoint); data.cone.scale.setScalar(worldPerPixel(arrowPoint) * 11);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
const render = () => {
|
|
169
|
+
if (disposed) return; const retainedHover = hoveredKey; hoveredKey = null; clear();
|
|
170
|
+
for (const original of clips()) {
|
|
171
|
+
if (!selected(original)) continue;
|
|
172
|
+
const clip = overrides.get(original.id) || original;
|
|
173
|
+
if (clip.kind === 'box') { const box = clip.box || clip; if (finite3(box.min) && finite3(box.max)) renderBox(clip, box); }
|
|
174
|
+
else if (clip.kind === 'plane') renderPlane(clip);
|
|
175
|
+
}
|
|
176
|
+
resize();
|
|
177
|
+
if (retainedHover) paintHover(handlesScreen().find((handle) => handleKey(handle) === retainedHover) || null);
|
|
178
|
+
};
|
|
179
|
+
const handlesScreen = () => {
|
|
180
|
+
if (disposed) return [];
|
|
181
|
+
const activeCamera = getCamera(); const rect = canvas.getBoundingClientRect(); activeCamera?.updateMatrixWorld?.();
|
|
182
|
+
return hitObjects.flatMap((data) => [
|
|
183
|
+
{ ...data.descriptor, part: 'disc', point: data.disc.position },
|
|
184
|
+
{ ...data.descriptor, part: 'arrow', point: data.cone.position },
|
|
185
|
+
]).map(({ point, ...entry }) => {
|
|
186
|
+
const projected = point.clone().project(activeCamera);
|
|
187
|
+
return { ...entry, x: rect.left + (projected.x * .5 + .5) * rect.width, y: rect.top + (-projected.y * .5 + .5) * rect.height, behind: projected.z > 1 };
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
const pickHandle = (clientX, clientY) => {
|
|
191
|
+
let best = null; let bestDistance = hitTolerancePx;
|
|
192
|
+
for (const handle of handlesScreen()) {
|
|
193
|
+
if (handle.behind) continue;
|
|
194
|
+
const distance = Math.hypot(handle.x - clientX, handle.y - clientY);
|
|
195
|
+
if (distance < bestDistance) { best = handle; bestDistance = distance; }
|
|
196
|
+
}
|
|
197
|
+
return best;
|
|
198
|
+
};
|
|
199
|
+
const paintHover = (handle, { releaseCursor = true } = {}) => {
|
|
200
|
+
const nextKey = handle ? handleKey(handle) : null; if (nextKey === hoveredKey) return !!handle;
|
|
201
|
+
hoveredKey = nextKey;
|
|
202
|
+
for (const data of hitObjects) {
|
|
203
|
+
const on = hoveredKey === handleKey(data.descriptor); const color = on ? hoverColor : data.color;
|
|
204
|
+
data.disc.material.color.setHex(color); data.disc.material.opacity = on ? .58 : .32;
|
|
205
|
+
data.stem.material.color.setHex(color); data.cone.material.color.setHex(color);
|
|
206
|
+
}
|
|
207
|
+
if (handle) setCursor('move'); else if (releaseCursor) setCursor('default');
|
|
208
|
+
return !!handle;
|
|
209
|
+
};
|
|
210
|
+
const eventRay = (event) => {
|
|
211
|
+
if (typeof rayAt === 'function') {
|
|
212
|
+
const at = { clientX: Number(event.clientX), clientY: Number(event.clientY) };
|
|
213
|
+
const result = rayAt.length >= 2 ? rayAt(at.clientX, at.clientY, event) : rayAt(at, event);
|
|
214
|
+
return { origin: vec(result.origin), direction: vec(result.direction).normalize() };
|
|
215
|
+
}
|
|
216
|
+
const rect = canvas.getBoundingClientRect(); const caster = new THREE.Raycaster();
|
|
217
|
+
caster.setFromCamera(new THREE.Vector2(((event.clientX - rect.left) / rect.width) * 2 - 1, -((event.clientY - rect.top) / rect.height) * 2 + 1), getCamera());
|
|
218
|
+
return { origin: caster.ray.origin.clone(), direction: caster.ray.direction.clone() };
|
|
219
|
+
};
|
|
220
|
+
const dragDeltaMm = (event) => {
|
|
221
|
+
const ray = eventRay(event); const t = modelClipClosestLineParameter(drag.anchorRender.toArray(), drag.axisRender.toArray(), ray.origin.toArray(), ray.direction.toArray());
|
|
222
|
+
const movedRender = drag.anchorRender.clone().addScaledVector(drag.axisRender, t - drag.t0);
|
|
223
|
+
const movedCanonical = toCanonicalPoint(movedRender);
|
|
224
|
+
return dot(movedCanonical.map((value, axis) => value - drag.anchorCanonical[axis]), drag.axisCanonical);
|
|
225
|
+
};
|
|
226
|
+
const makeReadout = (clip, deltaMm) => {
|
|
227
|
+
if (drag.handle.kind === 'plane') return Object.freeze({ valueMm: deltaMm, text: String(formatDistance(deltaMm, { kind: 'plane-offset', handle: drag.handle })), type: '⟂', deltaMm, extentMm: null });
|
|
228
|
+
const box = clip.box || clip; const extentMm = box.max[AXIS_INDEX[drag.handle.axis]] - box.min[AXIS_INDEX[drag.handle.axis]];
|
|
229
|
+
return Object.freeze({ valueMm: extentMm, text: String(formatDistance(extentMm, { kind: 'box-extent', handle: drag.handle })), type: drag.handle.axis.toUpperCase(), deltaMm, extentMm });
|
|
230
|
+
};
|
|
231
|
+
const payload = (clip, event, reason) => Object.freeze({
|
|
232
|
+
clipId: drag.clipId, clip: cloneData(clip), previous: cloneData(drag.previous), handle: drag.handle,
|
|
233
|
+
deltaMm: drag.deltaMm, readout: drag.lastReadout, event, ...(reason ? { reason } : {}),
|
|
234
|
+
});
|
|
235
|
+
const begin = (event) => {
|
|
236
|
+
if (event.button !== 0 || drag) return false;
|
|
237
|
+
const handle = pickHandle(event.clientX, event.clientY); if (!handle) return false;
|
|
238
|
+
const clip = currentClip(handle.clipId); if (!clip) return false;
|
|
239
|
+
const anchorCanonical = handle.kind === 'plane'
|
|
240
|
+
? [...planePoint(clip)]
|
|
241
|
+
: (() => { const box = clip.box || clip; const center = [0, 1, 2].map((axis) => (box.min[axis] + box.max[axis]) / 2); const i = AXIS_INDEX[handle.axis]; center[i] = handle.sign > 0 ? box.max[i] : box.min[i]; return center; })();
|
|
242
|
+
const axisCanonical = handle.kind === 'plane' ? normalized(planeNormal(clip)) : [handle.axis === 'x' ? 1 : 0, handle.axis === 'y' ? 1 : 0, handle.axis === 'z' ? 1 : 0];
|
|
243
|
+
if (!axisCanonical) return false;
|
|
244
|
+
const anchorRender = toRenderPoint(anchorCanonical); const axisRender = toRenderDirection(axisCanonical, anchorCanonical);
|
|
245
|
+
const ray = eventRay(event); const t0 = modelClipClosestLineParameter(anchorRender.toArray(), axisRender.toArray(), ray.origin.toArray(), ray.direction.toArray());
|
|
246
|
+
drag = { pointerId: event.pointerId, clipId: handle.clipId, handle: Object.freeze({ kind: handle.kind, axis: handle.axis, sign: handle.sign }), previous: cloneData(clip), anchorCanonical, axisCanonical, anchorRender, axisRender, t0, downX: event.clientX, downY: event.clientY, moved: false, deltaMm: 0, lastReadout: null };
|
|
247
|
+
paintHover(null); setCursor('grabbing'); canvas.setPointerCapture?.(event.pointerId); return true;
|
|
248
|
+
};
|
|
249
|
+
const move = (event) => {
|
|
250
|
+
if (!drag || (event.pointerId != null && drag.pointerId != null && event.pointerId !== drag.pointerId)) return false;
|
|
251
|
+
if (event.buttons != null && event.buttons !== 1) return false;
|
|
252
|
+
if (!drag.moved && Math.hypot(event.clientX - drag.downX, event.clientY - drag.downY) <= dragThresholdPx) return true;
|
|
253
|
+
drag.moved = true;
|
|
254
|
+
const deltaMm = dragDeltaMm(event); const next = moveModelClipHandle(drag.previous, drag.handle, deltaMm, minimumExtentMm);
|
|
255
|
+
drag.deltaMm = deltaMm; drag.lastReadout = makeReadout(next, deltaMm); overrides.set(drag.clipId, next);
|
|
256
|
+
const value = payload(next, event); preview(value); update(value); readout(value); render(); return true;
|
|
257
|
+
};
|
|
258
|
+
const finish = (event) => {
|
|
259
|
+
if (!drag || (event.pointerId != null && drag.pointerId != null && event.pointerId !== drag.pointerId)) return false;
|
|
260
|
+
const active = drag; const clip = overrides.get(active.clipId) || active.previous; const value = payload(clip, event);
|
|
261
|
+
canvas.releasePointerCapture?.(active.pointerId); overrides.delete(active.clipId); drag = null;
|
|
262
|
+
if (active.moved) commit(value);
|
|
263
|
+
setCursor('default'); render(); return true;
|
|
264
|
+
};
|
|
265
|
+
const restore = (reason, event = null) => {
|
|
266
|
+
if (!drag) return false;
|
|
267
|
+
const active = drag; active.deltaMm = 0; active.lastReadout = makeReadout(active.previous, 0);
|
|
268
|
+
overrides.set(active.clipId, active.previous); const value = payload(active.previous, event, reason);
|
|
269
|
+
if (active.moved) { preview(value); update(value); }
|
|
270
|
+
cancel(value); readout(Object.freeze({ ...value, hidden: true }));
|
|
271
|
+
canvas.releasePointerCapture?.(active.pointerId); overrides.delete(active.clipId); drag = null; setCursor('default'); render(); return true;
|
|
272
|
+
};
|
|
273
|
+
const inputPlugin = Object.freeze({
|
|
274
|
+
pointerDown(event) {
|
|
275
|
+
if (event.button !== 0) { paintHover(null); return false; }
|
|
276
|
+
return isPlacementArmed() ? false : begin(event);
|
|
277
|
+
},
|
|
278
|
+
pointerMove(event) {
|
|
279
|
+
if (drag || isPlacementArmed()) { paintHover(null, { releaseCursor: false }); return false; }
|
|
280
|
+
if (event.buttons === 2 || event.buttons === 4) { paintHover(null); return false; }
|
|
281
|
+
return paintHover(pickHandle(event.clientX, event.clientY));
|
|
282
|
+
},
|
|
283
|
+
pointerLeave() { paintHover(null); return false; },
|
|
284
|
+
windowPointerMove(event) { return event.buttons === 2 || event.buttons === 4 ? false : move(event); },
|
|
285
|
+
windowPointerUp(event) { return event.button === 1 || event.button === 2 ? false : finish(event); },
|
|
286
|
+
pointerCancel(event) { return drag && event.pointerId !== drag.pointerId ? false : restore('pointer-cancel', event); },
|
|
287
|
+
keyDown(event) { if (!drag || event.key !== 'Escape') return false; restore('escape', event); return true; },
|
|
288
|
+
windowBlur(event) { return restore('blur', event); },
|
|
289
|
+
cancel(reason = 'tool-cancel') { return restore(reason); },
|
|
290
|
+
});
|
|
291
|
+
const dispose = () => { if (disposed) return; restore('dispose'); paintHover(null); clear(); overlayScene.remove(root); disposed = true; };
|
|
292
|
+
|
|
293
|
+
render();
|
|
294
|
+
return Object.freeze({ inputPlugin: () => inputPlugin, render, resize, handlesScreen, dispose });
|
|
295
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/* Shared Three.js creation preview for every model clip provider. */
|
|
2
|
+
import { MODEL_CLIP_PLANE_HALF_SIZE_MM, modelClipBoxEdgePoints, modelClipPlaneCorners } from './model-clip-three-overlay.js';
|
|
3
|
+
|
|
4
|
+
export function createModelClipThreePreview({
|
|
5
|
+
THREE, overlayScene, canvas, camera = null, getCamera = () => camera,
|
|
6
|
+
canonicalToRenderPoint = (point) => point,
|
|
7
|
+
planeHalfSizeMm = MODEL_CLIP_PLANE_HALF_SIZE_MM, color = 0xbfdbfe, pointRadiusPx = 7,
|
|
8
|
+
} = {}) {
|
|
9
|
+
if (!THREE || !overlayScene || !canvas || typeof getCamera !== 'function') {
|
|
10
|
+
throw new TypeError('Clip preview needs THREE, an overlay scene, a canvas, and a camera getter.');
|
|
11
|
+
}
|
|
12
|
+
const root = new THREE.Group(); root.name = 'model-clip-creation-preview'; overlayScene.add(root);
|
|
13
|
+
let pointMarker = null; let shownKind = null; let disposed = false;
|
|
14
|
+
const point = (value) => new THREE.Vector3().fromArray(canonicalToRenderPoint([...value]));
|
|
15
|
+
const meshMaterial = (opacity = 1) => new THREE.MeshBasicMaterial({
|
|
16
|
+
color, transparent: opacity < 1, opacity, side: THREE.DoubleSide, depthTest: false, depthWrite: false, clippingPlanes: [],
|
|
17
|
+
});
|
|
18
|
+
const lineMaterial = () => new THREE.LineBasicMaterial({ color, depthTest: false, depthWrite: false, clippingPlanes: [] });
|
|
19
|
+
const clear = () => {
|
|
20
|
+
for (const child of [...root.children]) {
|
|
21
|
+
root.remove(child); child.traverse?.((item) => {
|
|
22
|
+
item.geometry?.dispose?.();
|
|
23
|
+
const materials = Array.isArray(item.material) ? item.material : item.material ? [item.material] : [];
|
|
24
|
+
for (const material of materials) material.dispose?.();
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
pointMarker = null;
|
|
28
|
+
shownKind = null;
|
|
29
|
+
};
|
|
30
|
+
const worldPerPixel = (at) => {
|
|
31
|
+
const activeCamera = getCamera(); const height = canvas.clientHeight || canvas.getBoundingClientRect?.().height || 1;
|
|
32
|
+
if (activeCamera?.isOrthographicCamera) return ((activeCamera.top - activeCamera.bottom) / (activeCamera.zoom || 1)) / height;
|
|
33
|
+
const distance = activeCamera?.position?.distanceTo?.(at) || 1; const fov = Number(activeCamera?.fov) || 50;
|
|
34
|
+
return (2 * Math.tan((fov * Math.PI / 180) / 2) * distance) / height;
|
|
35
|
+
};
|
|
36
|
+
const resize = () => { if (pointMarker) pointMarker.scale.setScalar(worldPerPixel(pointMarker.position) * pointRadiusPx); };
|
|
37
|
+
const addLine = (points, loop = false) => {
|
|
38
|
+
const value = new (loop ? THREE.LineLoop : THREE.LineSegments)(new THREE.BufferGeometry().setFromPoints(points), lineMaterial());
|
|
39
|
+
value.renderOrder = 1000; root.add(value); return value;
|
|
40
|
+
};
|
|
41
|
+
const show = (value) => {
|
|
42
|
+
if (disposed) return; clear(); if (!value || value.kind === 'clear') return; shownKind = value.kind;
|
|
43
|
+
if (value.kind === 'point' && Array.isArray(value.point)) {
|
|
44
|
+
pointMarker = new THREE.Mesh(new THREE.SphereGeometry(1, 16, 12), meshMaterial());
|
|
45
|
+
pointMarker.position.copy(point(value.point)); pointMarker.renderOrder = 1001; root.add(pointMarker); resize(); return;
|
|
46
|
+
}
|
|
47
|
+
if (value.kind === 'box' && value.box) {
|
|
48
|
+
addLine(modelClipBoxEdgePoints(value.box).map(point)); return;
|
|
49
|
+
}
|
|
50
|
+
if (value.kind === 'plane' && Array.isArray(value.point) && Array.isArray(value.normal)) {
|
|
51
|
+
const corners = modelClipPlaneCorners(value.point, value.normal, planeHalfSizeMm).map(point);
|
|
52
|
+
const geometry = new THREE.BufferGeometry().setFromPoints(corners); geometry.setIndex([0, 1, 2, 0, 2, 3]);
|
|
53
|
+
const fill = new THREE.Mesh(geometry, meshMaterial(.3)); fill.renderOrder = 999; root.add(fill);
|
|
54
|
+
addLine(corners, true);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const dispose = () => { if (disposed) return; clear(); overlayScene.remove(root); disposed = true; };
|
|
58
|
+
return Object.freeze({ show, resize, state: () => Object.freeze({ kind: shownKind }), dispose });
|
|
59
|
+
}
|