topotopo 0.1.0 → 0.3.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.
Files changed (80) hide show
  1. package/README.md +61 -12
  2. package/dist/cli.js +161 -23
  3. package/dist/graph/engine.js +18 -1
  4. package/dist/graph/params.d.ts +7 -1
  5. package/dist/graph/params.js +21 -8
  6. package/dist/graph/types.d.ts +28 -2
  7. package/dist/host/artifacts.d.ts +21 -0
  8. package/dist/host/artifacts.js +58 -0
  9. package/dist/host/export-targets.d.ts +33 -0
  10. package/dist/host/export-targets.js +84 -0
  11. package/dist/host/export_scene.py +200 -0
  12. package/dist/host/node.d.ts +8 -0
  13. package/dist/host/node.js +114 -0
  14. package/dist/host/types.d.ts +5 -0
  15. package/dist/inspect.js +5 -2
  16. package/dist/io/common.d.ts +6 -0
  17. package/dist/io/export.js +7 -4
  18. package/dist/io/gltf.d.ts +1 -1
  19. package/dist/io/gltf.js +167 -32
  20. package/dist/io/heightmap.d.ts +1 -1
  21. package/dist/io/import.d.ts +2 -2
  22. package/dist/io/import.js +10 -3
  23. package/dist/io/png-decode.d.ts +5 -2
  24. package/dist/io/png-decode.js +10 -3
  25. package/dist/io/targets.d.ts +31 -0
  26. package/dist/io/targets.js +75 -0
  27. package/dist/io/textures.d.ts +31 -2
  28. package/dist/io/textures.js +66 -18
  29. package/dist/io/unreal.js +22 -5
  30. package/dist/mcp/docs.d.ts +9 -2
  31. package/dist/mcp/docs.js +126 -2
  32. package/dist/mcp/inputs.d.ts +28 -0
  33. package/dist/mcp/inputs.js +377 -0
  34. package/dist/mcp/server.js +4 -1
  35. package/dist/mcp/tools.d.ts +1 -1
  36. package/dist/mcp/tools.js +118 -51
  37. package/dist/nodes/code.js +34 -6
  38. package/dist/nodes/copy.js +14 -4
  39. package/dist/nodes/flow.js +136 -35
  40. package/dist/nodes/heightfield.js +67 -11
  41. package/dist/nodes/texture.d.ts +1 -1
  42. package/dist/nodes/texture.js +61 -5
  43. package/dist/render/options.js +2 -1
  44. package/dist/render/raster.d.ts +2 -0
  45. package/dist/render/raster.js +3 -3
  46. package/dist/render/texview.d.ts +9 -1
  47. package/dist/render/texview.js +43 -2
  48. package/dist/viewer/app/api.js +64 -0
  49. package/dist/viewer/app/camera.js +250 -0
  50. package/dist/viewer/app/export-popover.js +68 -0
  51. package/dist/viewer/app/grid.js +217 -0
  52. package/dist/viewer/app/guides.js +402 -0
  53. package/dist/viewer/app/image-view.js +114 -0
  54. package/dist/viewer/app/keymap.js +199 -0
  55. package/dist/viewer/app/layers.js +244 -0
  56. package/dist/viewer/app/main.js +646 -0
  57. package/dist/viewer/app/markers.js +206 -0
  58. package/dist/viewer/app/mesh.js +172 -0
  59. package/dist/viewer/app/outliner.js +225 -0
  60. package/dist/viewer/app/outputs.css +73 -0
  61. package/dist/viewer/app/outputs.js +240 -0
  62. package/dist/viewer/app/props.js +432 -0
  63. package/dist/viewer/app/scene.js +529 -0
  64. package/dist/viewer/app/select.js +153 -0
  65. package/dist/viewer/app/selection.js +79 -0
  66. package/dist/viewer/app/status.js +20 -0
  67. package/dist/viewer/app/style.css +173 -0
  68. package/dist/viewer/app/undo.js +49 -0
  69. package/dist/viewer/app/units.js +51 -0
  70. package/dist/viewer/app/vars.js +64 -0
  71. package/dist/viewer/index.html +67 -184
  72. package/dist/viewer/outputs.d.ts +54 -0
  73. package/dist/viewer/outputs.js +436 -0
  74. package/dist/viewer/server.d.ts +47 -1
  75. package/dist/viewer/server.js +480 -17
  76. package/dist/workspace.d.ts +60 -4
  77. package/dist/workspace.js +131 -5
  78. package/package.json +9 -3
  79. package/shelf/brick_wall.json +1 -1
  80. package/shelf/wall_segment.json +2 -2
@@ -0,0 +1,436 @@
1
+ // Output catalogue, previews, downloads and exports. Geometry stays server-side; cards and previews are lazy.
2
+ // A published output exports to its target in the graph (io/targets.ts): its Export button writes it, Export all
3
+ // writes every target, and a first export sets the target in the graph so the next click just writes again.
4
+ import fs from 'node:fs';
5
+ import path from 'node:path';
6
+ import zlib from 'node:zlib';
7
+ import { createHash } from 'node:crypto';
8
+ import { zipSync } from 'fflate';
9
+ import { ArtifactStore, artifactId } from '../host/artifacts.js';
10
+ import { prepareTarget, writeTargets } from '../host/export-targets.js';
11
+ import { Image, referenceableImages, imageToBytes, resizeLayer } from '../core/image.js';
12
+ import { expandPacked } from '../core/packed.js';
13
+ import { withHeightfieldPreview } from '../core/heightfield.js';
14
+ import { withImagePreview } from '../render/texview.js';
15
+ import { renderGeometry } from '../render/raster.js';
16
+ import { encodePNG } from '../render/png.js';
17
+ import { exportGeometryAsync } from '../io/export.js';
18
+ import { FORMAT_LABELS, TARGET_FORMATS, publishedOutputs, targetError, targetExtension } from '../io/targets.js';
19
+ import { summarize } from '../inspect.js';
20
+ const digest = (x) => createHash('sha256').update(x).digest('hex');
21
+ const imageExt = /\.(png|jpe?g|webp)$/i;
22
+ const meshExt = /\.(glb|gltf|obj|stl|ply|geo|bgeo|json|csv)$/i;
23
+ const mime = (p) => ({ '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.glb': 'model/gltf-binary' })[path.extname(p).toLowerCase()] ?? 'application/octet-stream';
24
+ const deflate = (d) => zlib.deflateSync(d, { level: 6 });
25
+ const stamp = (p) => { const s = fs.statSync(p, { throwIfNoEntry: false }); return s ? `${s.size}:${s.mtimeMs}` : 'missing'; };
26
+ function err(status, message) { throw Object.assign(new Error(message), { status }); }
27
+ const idFor = (...parts) => digest(JSON.stringify(parts)).slice(0, 24);
28
+ const inside = (dir, p) => { const rel = path.relative(dir, p); return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel)); };
29
+ /** Predictable ZIP bytes, retaining the bundle directory structure. */
30
+ export function outputZip(files) {
31
+ const entries = Object.create(null);
32
+ for (const f of [...files].sort((a, b) => a.name.localeCompare(b.name, 'en')))
33
+ entries[f.name] = [f.bytes, { mtime: new Date(1980, 0, 1), level: 6 }];
34
+ return zipSync(entries);
35
+ }
36
+ export function createOutputs(ws, changed, opts = {}) {
37
+ const store = new ArtifactStore(ws.exportDir);
38
+ const items = new Map(), previewCache = new Map();
39
+ let queue = Promise.resolve(), closed = false, lastFiles = '', exportQueue = Promise.resolve();
40
+ const cache = async (key, make) => {
41
+ if (previewCache.has(key))
42
+ return previewCache.get(key);
43
+ const bytes = await make();
44
+ previewCache.set(key, bytes);
45
+ // Bound retained image/GLB bytes, independent of the engine's own geometry cache.
46
+ let total = [...previewCache.values()].reduce((n, b) => n + b.length, 0);
47
+ for (const [k, b] of previewCache) {
48
+ if (total <= 64 * 1024 * 1024)
49
+ break;
50
+ previewCache.delete(k);
51
+ total -= b.length;
52
+ }
53
+ return bytes;
54
+ };
55
+ const addImages = (item) => {
56
+ for (const [id, v] of items)
57
+ if (v.owner === item)
58
+ items.delete(id);
59
+ if (!item.geo)
60
+ return;
61
+ for (const [name, img] of referenceableImages(item.geo)) {
62
+ const id = idFor(item.entry.id, name);
63
+ items.set(id, { owner: item, img, entry: { id, origin: item.entry.origin, parent: item.entry.id, graph: item.entry.graph, node: item.entry.node,
64
+ label: name, kind: 'image', generated: true, status: 'ready', revision: item.entry.revision,
65
+ width: img.width, height: img.height, layers: [...img.layers.keys()], layer: img.mainLayer, image: name } });
66
+ }
67
+ };
68
+ const cook = (item) => {
69
+ if (item.pending)
70
+ return item.pending;
71
+ const key = item.key, e = item.entry;
72
+ item.pending = queue = queue.catch(() => { }).then(async () => {
73
+ // Yield between outputs so navigation and metadata requests remain responsive.
74
+ await new Promise((resolve) => setImmediate(resolve));
75
+ if (closed || items.get(e.id) !== item)
76
+ return;
77
+ try {
78
+ const r = item.file
79
+ ? await ws.engine.cook({ nodes: { file: { type: 'file', params: { path: item.file } } }, output: 'file' })
80
+ : await ws.cook(e.graph, e.node);
81
+ if (item.key !== key || items.get(e.id) !== item)
82
+ return;
83
+ if (!r.ok || !r.geo)
84
+ throw new Error(r.errors.map((x) => `${x.node}: ${x.message}`).join('\n') || 'no geometry');
85
+ item.geo = r.geo;
86
+ e.status = 'ready';
87
+ delete e.error;
88
+ const firstImage = referenceableImages(r.geo).values().next().value;
89
+ if (!r.geo.numPoints && !r.geo.numPrims && firstImage) {
90
+ item.img = firstImage;
91
+ e.kind = 'image';
92
+ e.width = firstImage.width;
93
+ e.height = firstImage.height;
94
+ e.layers = [...firstImage.layers.keys()];
95
+ e.layer = firstImage.mainLayer;
96
+ }
97
+ if (!item.file)
98
+ e.revision = digest(`${key}:${r.results.get(r.target)?.sig ?? ''}`);
99
+ e.summary = summarize(r.geo);
100
+ addImages(item);
101
+ }
102
+ catch (error) {
103
+ e.status = 'error';
104
+ e.error = String(error.message);
105
+ // Keep the last valid preview but mark its image children stale as well.
106
+ for (const v of items.values())
107
+ if (v.owner === item) {
108
+ v.entry.status = 'error';
109
+ v.entry.error = e.error;
110
+ }
111
+ }
112
+ finally {
113
+ item.pending = undefined;
114
+ if (!closed)
115
+ changed();
116
+ }
117
+ });
118
+ return item.pending;
119
+ };
120
+ /** The last export of a published output to its current target, from the receipts. */
121
+ const lastSaved = (records, graph, p) => {
122
+ if (!p.target)
123
+ return undefined;
124
+ const dest = path.join(ws.exportDir, ...p.target.path.split('/'));
125
+ let best;
126
+ for (const r of records) {
127
+ if (r.graph !== graph || r.output !== (p.output ?? '') || !r.files.some((f) => inside(dest, f)))
128
+ continue;
129
+ const stats = r.files.map((f) => fs.statSync(f, { throwIfNoEntry: false })).filter((s) => s?.isFile());
130
+ if (!stats.length)
131
+ continue;
132
+ const at = Math.max(...stats.map((s) => s.mtimeMs));
133
+ if (!best || at > best.at)
134
+ best = { at: Math.round(at), size: stats.reduce((n, s) => n + s.size, 0), files: stats.length };
135
+ }
136
+ return best;
137
+ };
138
+ const refresh = () => {
139
+ const alive = new Set();
140
+ const inputStamp = JSON.stringify(ws.listInputs().map((f) => [f.path, f.size, f.mtime]));
141
+ // A referenced graph or a file outside inputs/ can change an otherwise unchanged output.
142
+ const sourceFiles = new Map();
143
+ for (const spec of ws.graphs.values())
144
+ for (const node of Object.values(spec.nodes)) {
145
+ const p = node.params?.path ?? node.params?.file ?? node.params?.filename;
146
+ if (typeof p !== 'string' || !p || p.startsWith('='))
147
+ continue;
148
+ sourceFiles.set(p, ws.engine.host.fileStamp(p));
149
+ try {
150
+ if (ws.engine.host.isDirectory?.(p))
151
+ for (const f of ws.engine.host.listDir?.(p) ?? []) {
152
+ const child = path.join(p, f);
153
+ sourceFiles.set(child, ws.engine.host.fileStamp(child));
154
+ }
155
+ }
156
+ catch { /* a disappearing file is reported by the next cook */ }
157
+ }
158
+ const workspaceRevision = digest(JSON.stringify([...ws.graphs]) + inputStamp + JSON.stringify([...sourceFiles]));
159
+ const records = store.records();
160
+ for (const [graph, spec] of ws.graphs) {
161
+ for (const p of publishedOutputs(graph, spec, ws.engine.findOutput(spec).name)) {
162
+ const id = idFor('graph', graph, p.output === null ? 'default' : 'named', p.output ?? 'main');
163
+ alive.add(id);
164
+ const key = workspaceRevision, old = items.get(id);
165
+ const meta = { label: p.label, output: p.output, target: p.target ? { format: p.target.format, path: p.target.path, options: p.target.options } : null, saved: lastSaved(records, graph, p) };
166
+ if (old?.key === key) {
167
+ Object.assign(old.entry, meta);
168
+ if (!meta.saved)
169
+ delete old.entry.saved;
170
+ continue;
171
+ }
172
+ const item = { geo: old?.geo, key, entry: { id, origin: 'published', graph, node: p.node, kind: 'geometry', generated: true, exportable: true, status: 'loading', revision: key, ...meta } };
173
+ if (!meta.saved)
174
+ delete item.entry.saved;
175
+ // Carry last valid images until the replacement cook completes.
176
+ if (old)
177
+ for (const v of items.values())
178
+ if (v.owner === old) {
179
+ v.owner = item;
180
+ v.entry.status = 'loading';
181
+ }
182
+ items.set(id, item);
183
+ void cook(item);
184
+ }
185
+ }
186
+ const saved = store.list();
187
+ for (const a of saved) {
188
+ const id = 'file-' + a.id;
189
+ alive.add(id);
190
+ const files = a.files.filter((p) => fs.statSync(p, { throwIfNoEntry: false })?.isFile());
191
+ if (!files.length) {
192
+ alive.delete(id);
193
+ continue;
194
+ }
195
+ const revision = digest(files.map((p) => p + stamp(p)).join('|'));
196
+ if (items.get(id)?.entry.revision === revision)
197
+ continue;
198
+ const file = files[0];
199
+ const kind = files.length > 1 ? 'bundle' : imageExt.test(file) ? 'image' : meshExt.test(file) ? 'geometry' : 'file';
200
+ items.set(id, { file, files, entry: { id, origin: 'saved', label: a.label, graph: a.graph, node: a.node, kind, status: 'ready', revision,
201
+ summary: a.format === 'reference' ? 'Reference image' : `Saved ${a.format ?? path.extname(file).slice(1)} output`,
202
+ files: files.map((p) => ({ id: artifactId(p), name: path.relative(path.dirname(file), p).split(path.sep).join('/'), size: fs.statSync(p, { throwIfNoEntry: false })?.size ?? 0 })) } });
203
+ }
204
+ // Bundle contents are individual inspectable outputs as well as downloads.
205
+ for (const item of [...items.values()])
206
+ if (alive.has(item.entry.id) && item.files && item.files.length > 1)
207
+ for (const file of item.files) {
208
+ const stat = fs.statSync(file, { throwIfNoEntry: false });
209
+ if (!stat?.isFile())
210
+ continue;
211
+ const id = item.entry.id + '-' + artifactId(file);
212
+ alive.add(id);
213
+ const revision = digest(stamp(file));
214
+ if (items.get(id)?.entry.revision === revision)
215
+ continue;
216
+ items.set(id, { file, files: [file], entry: { id, origin: item.entry.origin, parent: item.entry.id, graph: item.entry.graph, label: path.basename(file),
217
+ kind: imageExt.test(file) ? 'image' : meshExt.test(file) ? 'geometry' : 'file', status: 'ready', revision,
218
+ files: [{ id: artifactId(file), name: path.basename(file), size: stat.size }] } });
219
+ }
220
+ for (const [id, item] of items)
221
+ if (!alive.has(id) && !item.owner) {
222
+ items.delete(id);
223
+ for (const [child, v] of items)
224
+ if (v.owner === item)
225
+ items.delete(child);
226
+ }
227
+ const signature = JSON.stringify([...items.values()].filter((v) => v.files || v.entry.exportable).map((v) => [v.entry.id, v.entry.revision, v.entry.target, v.entry.saved]));
228
+ if (signature !== lastFiles) {
229
+ lastFiles = signature;
230
+ changed();
231
+ }
232
+ };
233
+ const get = (id) => items.get(id ?? '') ?? err(404, 'output no longer exists');
234
+ const geometry = async (item) => { if (item.owner)
235
+ item = item.owner; if (!item.geo || item.pending)
236
+ await cook(item); return item.geo ?? err(422, item.entry.error ?? 'no geometry'); };
237
+ /** The published output behind a card, as the graph now has it. */
238
+ const published = (entry) => {
239
+ const spec = ws.getGraph(entry.graph);
240
+ return publishedOutputs(entry.graph, spec, ws.engine.findOutput(spec).name).find((p) => p.output === entry.output) ?? err(404, 'output no longer exists');
241
+ };
242
+ /** Cook now and serialize to the target: the export writes the current graph, whatever the preview last showed. */
243
+ const prepare = async (graph, p) => {
244
+ const r = await ws.cook(graph, p.node);
245
+ if (!r.ok || !r.geo)
246
+ err(422, `${p.label} (${graph}): ` + (r.errors.map((e) => `${e.node}: ${e.message}`).join('; ') || 'no geometry'));
247
+ return prepareTarget(r.geo, graph, p, ws.exportDir, ws.engine.host);
248
+ };
249
+ /** One export at a time, so two clicks never interleave their writes. */
250
+ const exclusive = async (work) => {
251
+ let unlock;
252
+ const prior = exportQueue;
253
+ exportQueue = new Promise((r) => { unlock = r; });
254
+ await prior;
255
+ try {
256
+ return await work();
257
+ }
258
+ finally {
259
+ unlock();
260
+ }
261
+ };
262
+ // A file can vanish while a scan stats it. The next tick or request rescans; neither callback may throw (an
263
+ // exception in the timer would end the viewer's process, the MCP server's too when it hosts the viewer).
264
+ const rescan = () => { try {
265
+ refresh();
266
+ }
267
+ catch { /* rescanned on the next tick */ } };
268
+ const listener = () => { rescan(); changed(); };
269
+ ws.listeners.add(listener);
270
+ const timer = setInterval(() => { if (!closed)
271
+ rescan(); }, 1500);
272
+ timer.unref();
273
+ return {
274
+ close() { closed = true; clearInterval(timer); ws.listeners.delete(listener); items.clear(); previewCache.clear(); },
275
+ async handle(req, res, u, readJSON) {
276
+ if (!u.pathname.startsWith('/api/output'))
277
+ return false;
278
+ const send = (body, type, name) => {
279
+ res.writeHead(200, { 'content-type': type, 'cache-control': 'no-store', 'x-content-type-options': 'nosniff', ...(name ? { 'content-disposition': `attachment; filename*=UTF-8''${encodeURIComponent(name)}` } : {}) });
280
+ res.end(body);
281
+ };
282
+ const json = (j) => send(JSON.stringify(j), 'application/json');
283
+ const posts = ['/api/output/export', '/api/output/export-all'];
284
+ try {
285
+ if (posts.includes(u.pathname) ? req.method !== 'POST' : req.method !== 'GET')
286
+ err(405, posts.includes(u.pathname) ? 'POST required' : 'method not allowed');
287
+ refresh();
288
+ if (u.pathname === '/api/outputs') {
289
+ json({ items: [...items.values()].map((v) => v.entry), directory: ws.exportDir,
290
+ formats: TARGET_FORMATS.map((f) => ({ format: f, label: FORMAT_LABELS[f], extension: targetExtension(f) })) });
291
+ return true;
292
+ }
293
+ if (u.pathname === '/api/output/export') {
294
+ // {id} writes the output to its target; {id, export} first sets the target ({id, export: null} only removes it)
295
+ const body = await readJSON(req), item = get(body?.id);
296
+ if (!item.entry.exportable)
297
+ err(400, 'only a published output (a card with an Export button) exports');
298
+ return await exclusive(async () => {
299
+ const graph = item.entry.graph;
300
+ if (body.export !== undefined) {
301
+ if (body.export !== null && targetError(body.export))
302
+ err(400, targetError(body.export));
303
+ try {
304
+ await ws.edit(graph, [{ op: 'export', ...(item.entry.output === null ? {} : { output: item.entry.output }), export: body.export }]);
305
+ }
306
+ catch (e) {
307
+ err(400, e.message);
308
+ }
309
+ opts.persistGraph?.(graph);
310
+ if (body.export === null) {
311
+ rescan();
312
+ changed();
313
+ json({ ok: true, cleared: true });
314
+ return true;
315
+ }
316
+ }
317
+ const p = published(item.entry);
318
+ if (!p.target)
319
+ err(400, 'no export target yet: choose a format');
320
+ const prepared = await prepare(graph, p);
321
+ writeTargets([prepared], ws.exportDir);
322
+ rescan();
323
+ changed();
324
+ json({ ok: true, path: p.target.path, files: prepared.files.length, summary: prepared.summary, warnings: prepared.warnings });
325
+ return true;
326
+ });
327
+ }
328
+ if (u.pathname === '/api/output/export-all') {
329
+ return await exclusive(async () => {
330
+ const targets = [], skipped = [];
331
+ for (const [graph, spec] of ws.graphs)
332
+ for (const p of publishedOutputs(graph, spec, ws.engine.findOutput(spec).name))
333
+ if (p.target)
334
+ targets.push({ graph, p });
335
+ else
336
+ skipped.push({ graph, label: p.label });
337
+ if (!targets.length)
338
+ err(400, 'no output has an export target yet: export one with its Export button first');
339
+ // every target cooks and serializes before any file is written
340
+ const prepared = [];
341
+ for (const t of targets)
342
+ prepared.push(await prepare(t.graph, t.p));
343
+ writeTargets(prepared, ws.exportDir);
344
+ rescan();
345
+ changed();
346
+ json({ ok: true, written: prepared.map((x) => ({ graph: x.graph, label: x.label, path: x.target.path, warnings: x.warnings })), skipped });
347
+ return true;
348
+ });
349
+ }
350
+ const item = get(u.searchParams.get('id'));
351
+ const mode = u.searchParams.get('mode') ?? 'RGB', layer = u.searchParams.get('layer') ?? item.entry.layer ?? '';
352
+ if (u.pathname === '/api/output/file') {
353
+ const p = (item.files ?? []).find((p) => artifactId(p) === u.searchParams.get('file')) ?? (u.searchParams.has('file') ? undefined : item.file);
354
+ if (!p || !fs.statSync(p, { throwIfNoEntry: false })?.isFile())
355
+ err(404, 'file no longer exists');
356
+ send(fs.readFileSync(p), mime(p), u.searchParams.has('download') ? path.basename(p) : undefined);
357
+ return true;
358
+ }
359
+ if (u.pathname === '/api/output/zip') {
360
+ if (!item.files?.length)
361
+ err(400, 'no saved files');
362
+ // names relative to the folder every file shares, so no entry climbs out with "../"
363
+ const common = (a, b) => { while (a !== path.dirname(a) && path.relative(a, b).startsWith('..'))
364
+ a = path.dirname(a); return a; };
365
+ const root = item.files.map((p) => path.dirname(p)).reduce(common);
366
+ send(outputZip(item.files.map((p) => ({ name: path.relative(root, p).split(path.sep).join('/'), bytes: fs.readFileSync(p) }))), 'application/zip', item.entry.label + '.zip');
367
+ return true;
368
+ }
369
+ if (u.pathname === '/api/output/image' || u.pathname === '/api/output/thumb' && item.img) {
370
+ if (!item.img)
371
+ err(400, 'this output is not a generated image');
372
+ let img = item.img;
373
+ if (layer && !img.layers.has(layer))
374
+ err(404, 'image layer no longer exists');
375
+ const thumb = u.pathname.endsWith('/thumb');
376
+ const bytes = await cache(`${item.entry.id}:${item.entry.revision}:${layer}:${mode}:${thumb}`, () => {
377
+ if (thumb) {
378
+ const s = Math.min(1, 160 / Math.max(img.width, img.height)), w = Math.max(1, Math.round(img.width * s)), h = Math.max(1, Math.round(img.height * s));
379
+ const l = layer || img.mainLayer;
380
+ const small = new Image(w, h, img.name);
381
+ small.set(l, resizeLayer(img.layer(l), img.channels(l), img.width, img.height, w, h, true));
382
+ img = small;
383
+ }
384
+ const data = imageToBytes(img, layer, true), rgba = new Uint8Array(img.count * 4);
385
+ for (let i = 0; i < img.count; i++) {
386
+ const rgb = data.channels === 1 ? [data.bytes[i], data.bytes[i], data.bytes[i], 255] : data.bytes.subarray(i * 4, i * 4 + 4);
387
+ const channel = ['R', 'G', 'B', 'A'].indexOf(mode);
388
+ rgba.set(channel >= 0 ? [rgb[channel], rgb[channel], rgb[channel], 255] : [rgb[0], rgb[1], rgb[2], mode === 'RGBA' ? rgb[3] : 255], i * 4);
389
+ }
390
+ return encodePNG(rgba, img.width, img.height, deflate);
391
+ });
392
+ // a texture card's Download: the image as a PNG named after it
393
+ send(bytes, 'image/png', u.searchParams.has('download') ? `${item.entry.label}${layer && layer !== img.mainLayer ? '_' + layer : ''}.png` : undefined);
394
+ return true;
395
+ }
396
+ if (u.pathname === '/api/output/uv') {
397
+ const g = expandPacked(await geometry(item)).geo, uv = g.vertices.get('uv') ?? g.points.get('uv'), segments = [];
398
+ if (uv)
399
+ for (let p = 0; p < g.numPrims; p++) {
400
+ const start = g.primStart[p], end = g.primStart[p + 1];
401
+ for (let v = start; v < end; v++) {
402
+ const next = v + 1 === end ? start : v + 1;
403
+ const a = uv.get(g.vertices.get('uv') ? v : g.vertexPoint[v]), b = uv.get(g.vertices.get('uv') ? next : g.vertexPoint[next]);
404
+ segments.push([a[0], a[1], b[0], b[1]]);
405
+ }
406
+ }
407
+ json({ segments });
408
+ return true;
409
+ }
410
+ if (u.pathname === '/api/output/geo' || u.pathname === '/api/output/thumb') {
411
+ if (item.file && imageExt.test(item.file)) {
412
+ send(fs.readFileSync(item.file), mime(item.file));
413
+ return true;
414
+ }
415
+ const g = await geometry(item), thumb = u.pathname.endsWith('/thumb');
416
+ const bytes = await cache(`${item.entry.id}:${item.entry.revision}:${thumb}`, async () => {
417
+ if (!thumb)
418
+ return exportGeometryAsync(withImagePreview(withHeightfieldPreview(g)), 'glb', { custom: false, splitByName: false, readFile: (p) => ws.engine.host.readFile(p) });
419
+ const r = renderGeometry(g, { size: 160, views: ['persp'], viewLabels: false });
420
+ return encodePNG(r.rgba.subarray(0, r.width * 160 * 4), r.width, 160, deflate);
421
+ });
422
+ send(bytes, thumb ? 'image/png' : 'model/gltf-binary');
423
+ return true;
424
+ }
425
+ err(404, 'unknown output route');
426
+ }
427
+ catch (error) {
428
+ if (error?.name === 'EditRejected')
429
+ error.status = 400;
430
+ res.writeHead(error.status ?? 500, { 'content-type': 'application/json', 'cache-control': 'no-store' });
431
+ res.end(JSON.stringify({ ok: false, error: String(error.message) }));
432
+ }
433
+ return true;
434
+ },
435
+ };
436
+ }
@@ -1,5 +1,51 @@
1
1
  import type { Workspace } from '../workspace.ts';
2
- export declare function startViewer(ws: Workspace, port?: number): Promise<{
2
+ import type { Geometry } from '../core/geometry.ts';
3
+ /** largest file POST /api/inputs accepts */
4
+ export declare const MAX_UPLOAD: number;
5
+ /**
6
+ * A dropped file's name as stored in inputs/: a plain basename with its extension. Path separators and ".." are
7
+ * rejected; characters Windows cannot store become "_".
8
+ */
9
+ export declare function inputFileName(raw: string): string;
10
+ export declare const MARKER_KINDS: readonly ['site', 'path', 'zone', 'volume'];
11
+ export type MarkerKind = (typeof MARKER_KINDS)[number];
12
+ /** One marker, in metres, Y-up, world space. */
13
+ export interface Marker {
14
+ id: string;
15
+ kind: MarkerKind;
16
+ name: string;
17
+ /** site: its point; path: an open polyline; zone: a closed one; volume: two ground corners of an axis-aligned box */
18
+ points: [number, number, number][];
19
+ /** site: radius (pscale), default 5 */
20
+ radius?: number;
21
+ /** volume: extent upward from the lower corner's y, default 10 */
22
+ height?: number;
23
+ }
24
+ /**
25
+ * A marker list (the POST /api/markers body, or the stored list) checked and normalised by the page's own rules
26
+ * (app/markers.js): names trimmed and unique (ignoring case), the defaults filled in, ids given to markers without
27
+ * one. Throws HttpError 400 naming the problem.
28
+ */
29
+ export declare function parseMarkers(body: unknown): Marker[];
30
+ /**
31
+ * The geometry of inputs/markers.json. Sites are points with name, role "site", pscale = radius and orient (identity);
32
+ * paths are open polylines, zones closed ones facing up, volumes closed boxes of 6 quads facing out; those carry prim
33
+ * name and role.
34
+ */
35
+ export declare function markersGeometry(list: Marker[]): Geometry;
36
+ export interface ViewerOptions {
37
+ /**
38
+ * Where the inputs folder is, in words that follow "inputs/" on the page: `topotopo view` passes "beside house.json".
39
+ * Default: "in the workspace" (or the folder that holds it, when the workspace was given another inputs folder).
40
+ */
41
+ inputsWhere?: string;
42
+ /**
43
+ * Called after the page sets an export target in a graph: `topotopo view` writes it back into the graph file, which
44
+ * is the only source there (an MCP workspace stores the graph itself).
45
+ */
46
+ persistGraph?: (graph: string) => void;
47
+ }
48
+ export declare function startViewer(ws: Workspace, port?: number, opts?: ViewerOptions): Promise<{
3
49
  url: string;
4
50
  close: () => void;
5
51
  }>;