@indispec/fs-compat 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +188 -2
- package/package.json +46 -3
- package/src/engine/assembly.js +481 -0
- package/src/engine/csg.js +580 -0
- package/src/engine/interp.js +1713 -0
- package/src/engine/lex.js +166 -0
- package/src/engine/parse.js +539 -0
- package/src/engine/project.js +409 -0
- package/src/engine/regions.js +923 -0
- package/src/engine/trace.js +211 -0
- package/src/engine/values.js +494 -0
- package/src/index.d.ts +746 -0
- package/src/index.js +108 -0
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
// Assembly document recording.
|
|
2
|
+
//
|
|
3
|
+
// An assembly is a *composition*, not a parametric part: instances of other documents plus
|
|
4
|
+
// their placement. It is still authored as code — the same parser and interpreter run the
|
|
5
|
+
// assembly program, with a different host operation set (`instance`, `part`) and a recorder
|
|
6
|
+
// that collects placements instead of solids (docs/CAD.md §2.2, "one document = one part";
|
|
7
|
+
// assemblies are the second document kind).
|
|
8
|
+
//
|
|
9
|
+
// References are never paths or "latest": an instance names a part through a {@link PartRef}
|
|
10
|
+
// that the document layer resolves from its pinned payload. Source text may *name* a
|
|
11
|
+
// reference; it may not *contain* one.
|
|
12
|
+
//
|
|
13
|
+
// This module is pure (values only, no interpreter import), so the worker and unit tests can
|
|
14
|
+
// use it directly. Placement is restricted to rigid transforms: translation plus rotation
|
|
15
|
+
// about X/Y/Z. Mates are a constraint solve and deliberately do not live in the program.
|
|
16
|
+
|
|
17
|
+
import { Xf } from './values.js';
|
|
18
|
+
|
|
19
|
+
/** A reference to a part document, pinned by content hash when available. */
|
|
20
|
+
export class PartRef {
|
|
21
|
+
/**
|
|
22
|
+
* @param {string} name Logical part name (the document title or a label in the source).
|
|
23
|
+
* @param {{ page?: string|null, hash?: string|null }} [options] Document/page id and content hash.
|
|
24
|
+
*/
|
|
25
|
+
constructor(name, { page = null, hash = null } = {}) {
|
|
26
|
+
/** Logical name used as the BOM line and tree label. */
|
|
27
|
+
this.name = name;
|
|
28
|
+
/** Page/document id the shell resolves to a source, when pinned. */
|
|
29
|
+
this.page = page;
|
|
30
|
+
/** Content hash of the part source at authoring time (pinning / staleness check). */
|
|
31
|
+
this.hash = hash;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** One placed instance in an assembly. */
|
|
36
|
+
export class AssemblyInstance {
|
|
37
|
+
/**
|
|
38
|
+
* @param {object} init Instance fields.
|
|
39
|
+
*/
|
|
40
|
+
constructor(init) {
|
|
41
|
+
/** Execution order (0-based). */
|
|
42
|
+
this.seq = init.seq;
|
|
43
|
+
/** Runtime id path, e.g. `assembly/ring1`. */
|
|
44
|
+
this.id = init.id;
|
|
45
|
+
/** `id` split into segments. */
|
|
46
|
+
this.path = init.path;
|
|
47
|
+
/** Display label. */
|
|
48
|
+
this.name = init.name;
|
|
49
|
+
/** The referenced part. */
|
|
50
|
+
this.part = init.part;
|
|
51
|
+
/** Rigid placement: 3x3 rotation matrix (row-major) plus translation in mm. */
|
|
52
|
+
this.xf = init.xf;
|
|
53
|
+
/** Serialised definition map, for the inspector. */
|
|
54
|
+
this.definition = init.definition;
|
|
55
|
+
/**
|
|
56
|
+
* Optional authored surface material (`{ color: [r,g,b], metallic, roughness }`), or null.
|
|
57
|
+
* Set by the `instanceMaterial` host op and composed by the kernel.
|
|
58
|
+
*/
|
|
59
|
+
this.material = init.material ?? null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The recorded composition for one assembly evaluation. */
|
|
64
|
+
export class AssemblyScene {
|
|
65
|
+
constructor() {
|
|
66
|
+
/** @type {AssemblyInstance[]} */
|
|
67
|
+
this.instances = [];
|
|
68
|
+
/** @type {Array<object>} Inline pipe runs (`pipe(...)` host op), placed in world millimetres. */
|
|
69
|
+
this.pipes = [];
|
|
70
|
+
/** @type {Array<object>} Inline terrain meshes (`terrain(...)` host op), heights in metres. */
|
|
71
|
+
this.terrains = [];
|
|
72
|
+
/** Non-fatal notes raised by the program (`reportFeatureInfo` etc.). */
|
|
73
|
+
this.notes = [];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Add one instance.
|
|
78
|
+
*
|
|
79
|
+
* @param {{ id: unknown, definition: unknown }} init Runtime `Id` and definition map.
|
|
80
|
+
* @returns {AssemblyInstance} The recorded instance.
|
|
81
|
+
*/
|
|
82
|
+
add({ id, definition }) {
|
|
83
|
+
const path = id && typeof id.toString === 'function' ? id.toString().split('/') : [];
|
|
84
|
+
const def = definition instanceof Map ? definition : new Map();
|
|
85
|
+
const part = partRefFromValue(def.get('part')) ?? new PartRef('(unspecified part)');
|
|
86
|
+
const xf = transformFromValue(def.get('transform') ?? def.get('placement'));
|
|
87
|
+
const name = def.get('name') != null ? String(def.get('name')) : part.name;
|
|
88
|
+
const instance = new AssemblyInstance({
|
|
89
|
+
seq: this.instances.length,
|
|
90
|
+
id: path.join('/') || null,
|
|
91
|
+
path,
|
|
92
|
+
name,
|
|
93
|
+
part,
|
|
94
|
+
xf,
|
|
95
|
+
definition: serializeDefinition(def),
|
|
96
|
+
});
|
|
97
|
+
this.instances.push(instance);
|
|
98
|
+
return instance;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Add one inline pipe run.
|
|
103
|
+
*
|
|
104
|
+
* @param {{ id: unknown, definition: unknown }} init Runtime `Id` and definition map.
|
|
105
|
+
* @returns {object} The recorded pipe run.
|
|
106
|
+
*/
|
|
107
|
+
addPipe({ id, definition }) {
|
|
108
|
+
const path = id && typeof id.toString === 'function' ? id.toString().split('/') : [];
|
|
109
|
+
const def = definition instanceof Map ? definition : new Map();
|
|
110
|
+
const pipe = pipeFromValue(def);
|
|
111
|
+
const record = {
|
|
112
|
+
seq: this.pipes.length,
|
|
113
|
+
id: path.join('/') || null,
|
|
114
|
+
path,
|
|
115
|
+
name: pipe.name,
|
|
116
|
+
start: pipe.start,
|
|
117
|
+
end: pipe.end,
|
|
118
|
+
outerRadiusMm: pipe.outerRadiusMm,
|
|
119
|
+
innerRadiusMm: pipe.innerRadiusMm,
|
|
120
|
+
definition: serializeDefinition(def),
|
|
121
|
+
};
|
|
122
|
+
this.pipes.push(record);
|
|
123
|
+
return record;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Live instance count. */
|
|
127
|
+
count() {
|
|
128
|
+
return this.instances.length;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Live inline-pipe count. */
|
|
132
|
+
pipeCount() {
|
|
133
|
+
return this.pipes.length;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Add one inline terrain mesh.
|
|
138
|
+
*
|
|
139
|
+
* @param {{ id: unknown, definition: unknown }} init Runtime `Id` and definition map.
|
|
140
|
+
* @returns {object} The recorded terrain.
|
|
141
|
+
*/
|
|
142
|
+
addTerrain({ id, definition }) {
|
|
143
|
+
const path = id && typeof id.toString === 'function' ? id.toString().split('/') : [];
|
|
144
|
+
const def = definition instanceof Map ? definition : new Map();
|
|
145
|
+
const terrain = terrainFromValue(def);
|
|
146
|
+
const record = {
|
|
147
|
+
seq: this.terrains.length,
|
|
148
|
+
id: path.join('/') || null,
|
|
149
|
+
path,
|
|
150
|
+
name: terrain.name,
|
|
151
|
+
heights: terrain.heights,
|
|
152
|
+
columns: terrain.columns,
|
|
153
|
+
rows: terrain.rows,
|
|
154
|
+
extentXmm: terrain.extentXmm,
|
|
155
|
+
extentYmm: terrain.extentYmm,
|
|
156
|
+
originXmm: terrain.originXmm,
|
|
157
|
+
originYmm: terrain.originYmm,
|
|
158
|
+
originZmm: terrain.originZmm,
|
|
159
|
+
site: terrain.site,
|
|
160
|
+
definition: serializeDefinition(def),
|
|
161
|
+
};
|
|
162
|
+
this.terrains.push(record);
|
|
163
|
+
return record;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Live inline-terrain count. */
|
|
167
|
+
terrainCount() {
|
|
168
|
+
return this.terrains.length;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Set the surface material of one recorded instance, addressed by runtime id path or display
|
|
173
|
+
* name. Returns false when no instance matches (the caller turns that into an error).
|
|
174
|
+
*
|
|
175
|
+
* @param {string} key Instance id path or display name.
|
|
176
|
+
* @param {{ color: number[], metallic: number, roughness: number }} material Authored material.
|
|
177
|
+
* @returns {boolean} True when an instance was updated.
|
|
178
|
+
*/
|
|
179
|
+
setMaterial(key, material) {
|
|
180
|
+
const text = String(key);
|
|
181
|
+
const match = this.instances.find((instance) => instance.id === text || instance.name === text);
|
|
182
|
+
if (!match) return false;
|
|
183
|
+
match.material = material;
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Plain-JSON view for transport to the kernel worker and the UI.
|
|
189
|
+
*
|
|
190
|
+
* @returns {Array<object>} Instances as JSON-safe objects.
|
|
191
|
+
*/
|
|
192
|
+
toJSON() {
|
|
193
|
+
return this.instances.map((instance) => ({
|
|
194
|
+
seq: instance.seq,
|
|
195
|
+
id: instance.id,
|
|
196
|
+
path: instance.path,
|
|
197
|
+
name: instance.name,
|
|
198
|
+
part: { name: instance.part.name, page: instance.part.page, hash: instance.part.hash },
|
|
199
|
+
xf: instance.xf,
|
|
200
|
+
material: instance.material,
|
|
201
|
+
definition: instance.definition,
|
|
202
|
+
}));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Plain-JSON view of the inline pipe runs (world millimetres).
|
|
207
|
+
*
|
|
208
|
+
* @returns {Array<object>} Pipe runs as JSON-safe objects.
|
|
209
|
+
*/
|
|
210
|
+
pipesToJSON() {
|
|
211
|
+
return this.pipes.map((pipe) => ({
|
|
212
|
+
seq: pipe.seq,
|
|
213
|
+
id: pipe.id,
|
|
214
|
+
name: pipe.name,
|
|
215
|
+
start: pipe.start,
|
|
216
|
+
end: pipe.end,
|
|
217
|
+
outerRadiusMm: pipe.outerRadiusMm,
|
|
218
|
+
innerRadiusMm: pipe.innerRadiusMm,
|
|
219
|
+
}));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Plain-JSON view of the inline terrain meshes (heights in metres, extents in millimetres).
|
|
224
|
+
*
|
|
225
|
+
* @returns {Array<object>} Terrain meshes as JSON-safe objects.
|
|
226
|
+
*/
|
|
227
|
+
terrainsToJSON() {
|
|
228
|
+
return this.terrains.map((terrain) => ({
|
|
229
|
+
seq: terrain.seq,
|
|
230
|
+
id: terrain.id,
|
|
231
|
+
name: terrain.name,
|
|
232
|
+
heights: terrain.heights,
|
|
233
|
+
columns: terrain.columns,
|
|
234
|
+
rows: terrain.rows,
|
|
235
|
+
extentXmm: terrain.extentXmm,
|
|
236
|
+
extentYmm: terrain.extentYmm,
|
|
237
|
+
originXmm: terrain.originXmm,
|
|
238
|
+
originYmm: terrain.originYmm,
|
|
239
|
+
originZmm: terrain.originZmm,
|
|
240
|
+
site: terrain.site,
|
|
241
|
+
}));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Identity rotation. */
|
|
246
|
+
const IDENTITY3 = [
|
|
247
|
+
[1, 0, 0],
|
|
248
|
+
[0, 1, 0],
|
|
249
|
+
[0, 0, 1],
|
|
250
|
+
];
|
|
251
|
+
|
|
252
|
+
const ORIGIN3 = [0, 0, 0];
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Row-major 3x3 product.
|
|
256
|
+
*
|
|
257
|
+
* @param {number[][]} a Left matrix.
|
|
258
|
+
* @param {number[][]} b Right matrix.
|
|
259
|
+
* @returns {number[][]} `a · b`.
|
|
260
|
+
*/
|
|
261
|
+
export function multiply3(a, b) {
|
|
262
|
+
const out = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
|
|
263
|
+
for (let i = 0; i < 3; i += 1) {
|
|
264
|
+
for (let j = 0; j < 3; j += 1) {
|
|
265
|
+
out[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Rotation matrix about one axis.
|
|
273
|
+
*
|
|
274
|
+
* @param {'x'|'y'|'z'} axis Rotation axis.
|
|
275
|
+
* @param {number} radians Rotation angle.
|
|
276
|
+
* @returns {number[][]} Row-major 3x3 matrix.
|
|
277
|
+
*/
|
|
278
|
+
export function rotation3(axis, radians) {
|
|
279
|
+
const c = Math.cos(radians);
|
|
280
|
+
const s = Math.sin(radians);
|
|
281
|
+
if (axis === 'x') return [[1, 0, 0], [0, c, -s], [0, s, c]];
|
|
282
|
+
if (axis === 'y') return [[c, 0, s], [0, 1, 0], [-s, 0, c]];
|
|
283
|
+
return [[c, -s, 0], [s, c, 0], [0, 0, 1]];
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Read a number-ish FeatureScript value (quantity, plain number, or numeric string).
|
|
288
|
+
*
|
|
289
|
+
* @param {unknown} value Runtime value.
|
|
290
|
+
* @returns {number} Numeric magnitude, or 0 when unreadable.
|
|
291
|
+
*/
|
|
292
|
+
function numberOf(value) {
|
|
293
|
+
if (value == null) return 0;
|
|
294
|
+
if (typeof value === 'number') return value;
|
|
295
|
+
if (typeof value === 'string') return Number(value) || 0;
|
|
296
|
+
if (typeof value === 'object' && 'v' in value) return Number(value.v) || 0;
|
|
297
|
+
return 0;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Read a 3D vector (metres of translation: millimetres, as the engine's length unit).
|
|
302
|
+
*
|
|
303
|
+
* @param {unknown} value Vector or `[x, y, z]`.
|
|
304
|
+
* @returns {number[]} Three components.
|
|
305
|
+
*/
|
|
306
|
+
function vec3Of(value) {
|
|
307
|
+
if (Array.isArray(value)) return [numberOf(value[0]), numberOf(value[1]), numberOf(value[2])];
|
|
308
|
+
if (value && Array.isArray(value.c)) return value.c.map(numberOf);
|
|
309
|
+
return ORIGIN3;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Turn an `instance` `part` value into a reference.
|
|
314
|
+
*
|
|
315
|
+
* Accepts a plain string/Id name, or a map `{ "name": …, "page": …, "hash": … }` produced by
|
|
316
|
+
* the `part(...)` host helper.
|
|
317
|
+
*
|
|
318
|
+
* @param {unknown} value Runtime value.
|
|
319
|
+
* @returns {PartRef|null} The reference, or null when absent.
|
|
320
|
+
*/
|
|
321
|
+
export function partRefFromValue(value) {
|
|
322
|
+
if (value == null) return null;
|
|
323
|
+
if (typeof value === 'string') return new PartRef(value);
|
|
324
|
+
const name = value?.constructor?.name;
|
|
325
|
+
if (name === 'Id') return new PartRef(value.toString());
|
|
326
|
+
if (value instanceof Map) {
|
|
327
|
+
const rawName = value.get('name') ?? value.get('part') ?? value.get('page');
|
|
328
|
+
if (rawName == null) return null;
|
|
329
|
+
return new PartRef(String(rawName), {
|
|
330
|
+
page: value.get('page') == null ? null : String(value.get('page')),
|
|
331
|
+
hash: value.get('hash') == null ? null : String(value.get('hash')),
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Turn an `instance` `transform` value into a rigid placement.
|
|
339
|
+
*
|
|
340
|
+
* Supported forms: an `Xf`; a translation vector; or a map with `translate` +
|
|
341
|
+
* `rotateX`/`rotateY`/`rotateZ` (degrees or angle quantities). Rotations compose X→Y→Z.
|
|
342
|
+
* Anything else falls back to identity, which the projection reports as unplaced.
|
|
343
|
+
*
|
|
344
|
+
* @param {unknown} value Runtime value.
|
|
345
|
+
* @returns {{ m: number[][], t: number[] }} Rotation matrix plus translation in mm.
|
|
346
|
+
*/
|
|
347
|
+
export function transformFromValue(value) {
|
|
348
|
+
if (value == null) return { m: IDENTITY3.map((row) => [...row]), t: [...ORIGIN3] };
|
|
349
|
+
if (value instanceof Xf) return { m: value.m.map((row) => [...row]), t: [...value.t] };
|
|
350
|
+
if (Array.isArray(value) || (value && Array.isArray(value.c))) {
|
|
351
|
+
return { m: IDENTITY3.map((row) => [...row]), t: vec3Of(value) };
|
|
352
|
+
}
|
|
353
|
+
if (value instanceof Map) {
|
|
354
|
+
const t = vec3Of(value.get('translate') ?? value.get('offset'));
|
|
355
|
+
const rotX = numberOf(value.get('rotateX'));
|
|
356
|
+
const rotY = numberOf(value.get('rotateY'));
|
|
357
|
+
const rotZ = numberOf(value.get('rotateZ'));
|
|
358
|
+
let m = IDENTITY3.map((row) => [...row]);
|
|
359
|
+
if (rotX) m = multiply3(rotation3('x', rotX), m);
|
|
360
|
+
if (rotY) m = multiply3(rotation3('y', rotY), m);
|
|
361
|
+
if (rotZ) m = multiply3(rotation3('z', rotZ), m);
|
|
362
|
+
return { m, t };
|
|
363
|
+
}
|
|
364
|
+
if (value instanceof Xf) return { m: value.m, t: value.t };
|
|
365
|
+
return { m: IDENTITY3.map((row) => [...row]), t: [...ORIGIN3] };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Turn an `instanceMaterial` definition map into a surface material.
|
|
370
|
+
*
|
|
371
|
+
* Accepts `{ "color": vector(r,g,b) | [r,g,b], "metallic": number, "roughness": number }`. Colour
|
|
372
|
+
* components are 0..1; metallic/roughness are 0..1 and default to 0 / 1 (a matte finish).
|
|
373
|
+
*
|
|
374
|
+
* @param {unknown} value Runtime definition map.
|
|
375
|
+
* @returns {{ color: number[], metallic: number, roughness: number }} The material.
|
|
376
|
+
*/
|
|
377
|
+
export function materialFromValue(value) {
|
|
378
|
+
const def = value instanceof Map ? value : new Map();
|
|
379
|
+
const color = vec3Of(def.get('color'));
|
|
380
|
+
const clamp = (v) => Math.max(0, Math.min(1, numberOf(v)));
|
|
381
|
+
return {
|
|
382
|
+
color: [clamp(color[0]), clamp(color[1]), clamp(color[2])],
|
|
383
|
+
metallic: clamp(def.get('metallic')),
|
|
384
|
+
roughness: clamp(def.get('roughness') ?? 1),
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Turn a `pipe` definition map into an inline pipe run.
|
|
390
|
+
*
|
|
391
|
+
* Accepts `{ "start": vector(…), "end": vector(…), "outerRadius": length, "innerRadius"?:
|
|
392
|
+
* length, "name"? }`. Points are in millimetres; radii are length quantities in millimetres.
|
|
393
|
+
*
|
|
394
|
+
* @param {unknown} value Runtime definition map.
|
|
395
|
+
* @returns {{ start: number[], end: number[], outerRadiusMm: number, innerRadiusMm: number, name: string | null }} The pipe run.
|
|
396
|
+
*/
|
|
397
|
+
export function pipeFromValue(value) {
|
|
398
|
+
const def = value instanceof Map ? value : new Map();
|
|
399
|
+
const radius = (key, fallback) => {
|
|
400
|
+
const v = numberOf(def.get(key));
|
|
401
|
+
return v > 0 ? v : fallback;
|
|
402
|
+
};
|
|
403
|
+
return {
|
|
404
|
+
start: vec3Of(def.get('start')),
|
|
405
|
+
end: vec3Of(def.get('end')),
|
|
406
|
+
outerRadiusMm: radius('outerRadius', 0),
|
|
407
|
+
innerRadiusMm: radius('innerRadius', 0),
|
|
408
|
+
name: def.get('name') != null ? String(def.get('name')) : null,
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Turn a `terrain` definition map into an inline terrain mesh.
|
|
414
|
+
*
|
|
415
|
+
* Accepts `{ "heights": number[], "columns": int, "rows": int, "extentXmm": length,
|
|
416
|
+
* "extentYmm": length, "originXmm"?: length, "originYmm"?: length, "originZmm"?: length, "site"?:
|
|
417
|
+
* { "west", "south", "east", "north" }, "name"? }`. Heights are in metres (the Terrarium
|
|
418
|
+
* convention); extents are length quantities in millimetres. The kernel's `buildDemMesh` turns the
|
|
419
|
+
* grid into a closed heightfield.
|
|
420
|
+
*
|
|
421
|
+
* `site` is the grid's geographic footprint (WGS-84 degrees) — the map picker records it so the
|
|
422
|
+
* viewport can drape satellite imagery over the mesh (`terrain-imagery.ts` in the CAD editor). It
|
|
423
|
+
* is optional: an un-georeferenced terrain renders as a plain solid. A half-specified footprint
|
|
424
|
+
* is treated as absent rather than guessed.
|
|
425
|
+
*
|
|
426
|
+
* @param {unknown} value Runtime definition map.
|
|
427
|
+
* @returns {{ heights: number[], columns: number, rows: number, extentXmm: number, extentYmm: number, originXmm: number, originYmm: number, originZmm: number, site: { west: number, south: number, east: number, north: number } | null, name: string | null }} The terrain.
|
|
428
|
+
*/
|
|
429
|
+
export function terrainFromValue(value) {
|
|
430
|
+
const def = value instanceof Map ? value : new Map();
|
|
431
|
+
const raw = def.get('heights');
|
|
432
|
+
const heights = Array.isArray(raw) ? raw.map(numberOf) : [];
|
|
433
|
+
return {
|
|
434
|
+
heights,
|
|
435
|
+
columns: Math.max(2, Math.round(numberOf(def.get('columns')))),
|
|
436
|
+
rows: Math.max(2, Math.round(numberOf(def.get('rows')))),
|
|
437
|
+
extentXmm: numberOf(def.get('extentXmm')),
|
|
438
|
+
extentYmm: numberOf(def.get('extentYmm')),
|
|
439
|
+
originXmm: numberOf(def.get('originXmm') ?? 0),
|
|
440
|
+
originYmm: numberOf(def.get('originYmm') ?? 0),
|
|
441
|
+
originZmm: numberOf(def.get('originZmm') ?? 0),
|
|
442
|
+
site: siteFromValue(def.get('site')),
|
|
443
|
+
name: def.get('name') != null ? String(def.get('name')) : null,
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Read a terrain's geographic footprint from its `site` map.
|
|
449
|
+
*
|
|
450
|
+
* @param {unknown} value The `site` entry of a terrain definition (a FeatureScript map).
|
|
451
|
+
* @returns {{ west: number, south: number, east: number, north: number } | null} Degrees, or null
|
|
452
|
+
* when the map is absent/incomplete or the box is degenerate.
|
|
453
|
+
*/
|
|
454
|
+
function siteFromValue(value) {
|
|
455
|
+
if (!(value instanceof Map)) return null;
|
|
456
|
+
const keys = ['west', 'south', 'east', 'north'];
|
|
457
|
+
const raw = keys.map((key) => value.get(key));
|
|
458
|
+
if (raw.some((entry) => entry == null)) return null;
|
|
459
|
+
const [west, south, east, north] = raw.map(numberOf);
|
|
460
|
+
if (![west, south, east, north].every(Number.isFinite)) return null;
|
|
461
|
+
if (!(east > west) || !(north > south)) return null;
|
|
462
|
+
return { west, south, east, north };
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** Serialise a definition map for display (depth-limited, JSON-safe). */
|
|
466
|
+
function serializeDefinition(definition) {
|
|
467
|
+
/** @type {Record<string, unknown>} */
|
|
468
|
+
const out = {};
|
|
469
|
+
for (const [key, value] of definition.entries()) {
|
|
470
|
+
const name = value?.constructor?.name;
|
|
471
|
+
if (value instanceof PartRef) out[String(key)] = { name: value.name, page: value.page, hash: value.hash };
|
|
472
|
+
else if (name === 'Vec') out[String(key)] = vec3Of(value);
|
|
473
|
+
else if (name === 'Q') out[String(key)] = numberOf(value);
|
|
474
|
+
else if (name === 'Id') out[String(key)] = value.toString();
|
|
475
|
+
else if (value instanceof Map) out[String(key)] = serializeDefinition(value);
|
|
476
|
+
else if (typeof value === 'number' || typeof value === 'string' || typeof value === 'boolean') {
|
|
477
|
+
out[String(key)] = value;
|
|
478
|
+
} else out[String(key)] = value === null || value === undefined ? null : (name ?? typeof value);
|
|
479
|
+
}
|
|
480
|
+
return out;
|
|
481
|
+
}
|