Public: anyone can read this; the crew can change it.

A .obj or .ply mesh opens in the existing 3D viewer, with vertex and face counts beside it #12723

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/12700-a-obj-or-ply-mesh-opens-in-the-existing → main 10 commits fork 4 threads · 4 unresolved

  • Merged. mason/12700-a-obj-or-ply-mesh-opens-in-the-existing was merged into main as e7cfae5.

Review

Sign in to review this change.
13 files changed +559 −48
docs/features/README.md +1
@@ -214,6 +214,7 @@ Read with the guide page [Your apps](../guide-your-apps.md).
214214
215215 ### Files in the browser
216216
217 - [An .obj or .ply file opens in the 3D viewer](mesh-files.md)
217218 - [A .dxf file as a picture](file-dxf.md)
218219 - [A .har file as a request waterfall](file-har.md)
219220 - [A .sarif file opens as its findings](file-sarif.md)
docs/features/mesh-files.md added +23
@@ -0,0 +1,23 @@
1 # Open an .obj or .ply file
2
3 A `.obj` or `.ply` file in the code browser opens in the 3D viewer, the same one an `.stl` uses. Beside the view the
4 facts list **Vertices**, **Faces**, **Triangles** and the **Box** (its size in the file's own units, shown as mm).
5 The first paint carries them: the server reads the file (up to 2 MB) and renders the rows, so they show with
6 JavaScript off. For a larger file the viewer fills them in once it has fetched and read it.
7
8 - **Vertices** are the points the file lists; **Faces** are the polygons, before they are cut into triangles.
9 - **Dropped faces**: a face that names a vertex the file does not have is left out and counted, as
10 `6 (1 face dropped)`.
11 - **Over the cap**: a part of more than 5,000,000 triangles is counted but not drawn. The facts stay, and the view
12 says "This part has more triangles than can be drawn here; download it to open it elsewhere." with a Download button.
13 - **Not readable**: a truncated file, a big-endian PLY, or a header that claims more than the bytes hold is refused
14 with a plain note and Download; nothing is partly drawn. Reading never spends time on a claimed count, only on bytes.
15 - PLY: ASCII and binary little-endian; only `vertex` (x, y, z) and `face` (`vertex_indices`) are read.
16
17 The terminal (both doors): the workspace screen's file view shows the same file as
18 `3D model · OBJ` over `12 triangles · 1 × 1 × 1 mm · 8 vertices · 6 faces`, and `not a readable PLY file` for one it cannot read.
19 The facts come from the same parsers as the page (`src/web/render/model-facts.ts`).
20
21 Try it: commit `part.obj` or `part.ply` to a repository and open it under Code.
22
23 Pinned by `test/render-obj-ply.test.ts`.
src/web/render/kinds.ts +1
@@ -185,6 +185,7 @@ const MEDIA: Readonly<Record<string, Media>> = {
185185 woff: { kind: "font", name: "WOFF", type: "font/woff", magic: ascii("wOFF") },
186186 woff2: { kind: "font", name: "WOFF2", type: "font/woff2", magic: ascii("wOF2") },
187187 "3mf": { kind: "model3d", name: "3MF", type: "model/3mf", magic: bytes(0x50, 0x4b, 0x03, 0x04) },
188 ply: { kind: "model3d", name: "PLY", type: "model/ply", magic: ascii("ply") },
188189 glb: { kind: "model3d", name: "glTF binary", type: "model/gltf-binary", magic: ascii("glTF") },
189190 };
190191
src/web/render/mesh.ts +95 −21
@@ -12,8 +12,21 @@ export interface Mesh {
1212 /** x, y, z for each corner, three corners a triangle. */
1313 readonly positions: Float32Array;
1414 readonly triangles: number;
15 /** Corners read from the file; set by the indexed formats (OBJ, PLY). */
16 readonly vertices?: number;
17 /** Faces kept, before they were cut into triangles. */
18 readonly faces?: number;
19 /** Faces dropped because they name a vertex that is not there. */
20 readonly dropped?: number;
21 /** More triangles than `TRIANGLE_LIMIT`: counted, but `positions` is empty. */
22 readonly overCap?: boolean;
23 /** The box around every vertex, for a mesh whose `positions` were not kept. */
24 readonly box?: Bounds;
1525 }
1626
27 /** Triangles beyond which a part is counted, not drawn. */
28 export const TRIANGLE_LIMIT = 5_000_000;
29
1730 export interface Bounds {
1831 readonly min: readonly [number, number, number];
1932 readonly max: readonly [number, number, number];
@@ -68,25 +81,63 @@ function parseAsciiStl(text: string): Mesh {
6881 return { positions: Float32Array.from(out), triangles: out.length / 9 };
6982 }
7083
84 /**
85 * Indexed vertices and faces into a triangle soup. A face is fanned; one that
86 * names a vertex that is not there is dropped and counted; past
87 * `TRIANGLE_LIMIT` triangles are counted and no longer kept.
88 */
89 export function soupBuilder(vertices: number[], limit = TRIANGLE_LIMIT): {
90 face(indices: readonly number[]): void;
91 done(): Mesh;
92 } {
93 const out: number[] = [];
94 let triangles = 0;
95 let faces = 0;
96 let dropped = 0;
97 const push = (index: number): void => {
98 out.push(vertices[index * 3]!, vertices[index * 3 + 1]!, vertices[index * 3 + 2]!);
99 };
100 return {
101 face(indices) {
102 const count = vertices.length / 3;
103 if (indices.length < 3 || !indices.every((i) => Number.isInteger(i) && i >= 0 && i < count)) {
104 dropped++;
105 return;
106 }
107 faces++;
108 for (let i = 1; i + 1 < indices.length; i++) {
109 triangles++;
110 if (triangles > limit) continue;
111 push(indices[0]!);
112 push(indices[i]!);
113 push(indices[i + 1]!);
114 }
115 },
116 done() {
117 const count = vertices.length / 3;
118 const overCap = triangles > limit;
119 const box = bounds(Float32Array.from(vertices));
120 return {
121 positions: overCap ? new Float32Array(0) : Float32Array.from(out),
122 triangles,
123 vertices: count,
124 faces,
125 dropped,
126 overCap,
127 box,
128 };
129 },
130 };
131 }
132
71133 /**
72134 * Wavefront OBJ: `v` lines and `f` lines; a face of more than three corners
73135 * is a fan. Indices are 1-based, and a negative one counts back from the last
74136 * vertex read. Normals, texture coordinates, groups and materials are skipped.
75137 */
76 export function parseObj(text: string): Mesh {
138 export function parseObj(text: string, limit = TRIANGLE_LIMIT): Mesh {
77139 const vertices: number[] = [];
78 const out: number[] = [];
79 const corner = (token: string): number => {
80 const n = Number.parseInt(token, 10);
81 const count = vertices.length / 3;
82 const index = n < 0 ? count + n : n - 1;
83 if (!Number.isInteger(n) || n === 0 || index < 0 || index >= count)
84 throw new MeshError(`a face names vertex ${token}, which is not there`);
85 return index;
86 };
87 const push = (index: number): void => {
88 out.push(vertices[index * 3]!, vertices[index * 3 + 1]!, vertices[index * 3 + 2]!);
89 };
140 const soup = soupBuilder(vertices, limit);
90141 for (const line of text.split("\n")) {
91142 const parts = line.trim().split(/\s+/);
92143 if (parts[0] === "v") {
@@ -94,16 +145,17 @@ export function parseObj(text: string): Mesh {
94145 if (![x, y, z].every(Number.isFinite)) throw new MeshError("a vertex that is not three numbers");
95146 vertices.push(x, y, z);
96147 } else if (parts[0] === "f") {
97 const corners = parts.slice(1).map((p) => corner(p.split("/")[0] ?? ""));
98 if (corners.length < 3) throw new MeshError("a face with fewer than three corners");
99 for (let i = 1; i + 1 < corners.length; i++) {
100 push(corners[0]!);
101 push(corners[i]!);
102 push(corners[i + 1]!);
103 }
148 const count = vertices.length / 3;
149 // 1-based; a negative index counts back from the last vertex read.
150 soup.face(
151 parts.slice(1).map((p) => {
152 const n = Number((p.split("/")[0] ?? "").trim());
153 return !Number.isInteger(n) || n === 0 ? -1 : n < 0 ? count + n : n - 1;
154 }),
155 );
104156 }
105157 }
106 return { positions: Float32Array.from(out), triangles: out.length / 9 };
158 return soup.done();
107159 }
108160
109161 /** The axis-aligned box around every corner; all zeros for an empty mesh. */
@@ -127,3 +179,25 @@ export function sizeText(size: readonly [number, number, number], unit = "mm"):
127179 const round = (v: number): string => String(Math.round(v * 100) / 100);
128180 return `${size.map(round).join(" × ")} ${unit}`;
129181 }
182
183 /** `1 vertex`, `8 vertices`: the plural is given, never guessed. */
184 function count(v: number, one: string, many: string): string {
185 return `${v.toLocaleString("en")} ${v === 1 ? one : many}`;
186 }
187
188 /**
189 * The words for an indexed mesh's rows, shared by the page's first paint, the viewer and the
190 * terminal.
191 */
192 export function indexedRows(mesh: Mesh): { vertices: string; faces: string; triangles: string; box: string } {
193 const dropped = mesh.dropped ? ` (${count(mesh.dropped, "face", "faces")} dropped)` : "";
194 return {
195 vertices: (mesh.vertices ?? 0).toLocaleString("en"),
196 faces: `${(mesh.faces ?? 0).toLocaleString("en")}${dropped}`,
197 triangles: mesh.triangles.toLocaleString("en"),
198 box: sizeText((mesh.box ?? bounds(mesh.positions)).size),
199 };
200 }
201
202 /** Said beside a part that was counted but not drawn. */
203 export const OVER_CAP_NOTE = "This part has more triangles than can be drawn here; download it to open it elsewhere.";
src/web/render/model-facts.ts +38 −4
@@ -12,7 +12,8 @@
1212
1313 import { inflateRawSync } from "node:zlib";
1414 import { kindByName } from "./kinds.ts";
15 import { bounds, parseObj, parseStl, sizeText } from "./mesh.ts";
15 import { bounds, parseObj, parseStl, sizeText, type Mesh } from "./mesh.ts";
16 import { parsePly } from "./ply.ts";
1617 import { INFLATE_LIMIT, locateModel, parseModelXml } from "./threemf.ts";
1718
1819 /** Over this a model is named and sized, not read. */
@@ -21,6 +22,8 @@ export const FACTS_CAP = 2_000_000;
2122 interface Facts {
2223 readonly triangles: number;
2324 readonly size: readonly [number, number, number];
25 /** For an indexed format: the words about its vertices and faces. */
26 readonly indexed?: string;
2427 }
2528
2629 const extension = (path: string): string => path.slice(path.lastIndexOf(".") + 1).toLowerCase();
@@ -50,7 +53,8 @@ export function modelFacts(path: string, bytes: Uint8Array | null, size: number)
5053 return { title, facts: `not a readable ${format} file` };
5154 }
5255 const count = `${facts.triangles.toLocaleString("en")} triangle${facts.triangles === 1 ? "" : "s"}`;
53 return { title, facts: `${count} · ${sizeText(facts.size)}` };
56 const tail = facts.indexed === undefined ? "" : ` · ${facts.indexed}`;
57 return { title, facts: `${count} · ${sizeText(facts.size)}${tail}` };
5458 }
5559
5660 function read(ext: string, bytes: Uint8Array): Facts {
@@ -61,13 +65,43 @@ function read(ext: string, bytes: Uint8Array): Facts {
6165 const mesh = parseModelXml(new TextDecoder().decode(xml));
6266 return { triangles: mesh.triangles, size: bounds(mesh.positions).size };
6367 }
64 if (ext === "stl" || ext === "obj") {
65 const mesh = ext === "stl" ? parseStl(bytes) : parseObj(new TextDecoder().decode(bytes));
68 if (ext === "stl") {
69 const mesh = parseStl(bytes);
6670 return { triangles: mesh.triangles, size: bounds(mesh.positions).size };
6771 }
72 if (ext === "obj" || ext === "ply") return indexedFacts(ext === "obj" ? parseObj(new TextDecoder().decode(bytes)) : parsePly(bytes));
6873 return gltfFacts(ext === "glb" ? glbJson(bytes) : new TextDecoder().decode(bytes));
6974 }
7075
76 /** Vertices and faces of an OBJ or PLY; a face that named a missing vertex is counted. */
77 export function indexedWords(mesh: Mesh): string {

Principle 8: .replace("vertexs","vertices") patches a bad plural after the fact. Give n the plural.

78 const n = (v: number, one: string, many: string): string => `${v.toLocaleString("en")} ${v === 1 ? one : many}`;
79 const dropped = mesh.dropped ? ` (${n(mesh.dropped, "face", "faces")} dropped)` : "";
80 return `${n(mesh.vertices ?? 0, "vertex", "vertices")} · ${n(mesh.faces ?? 0, "face", "faces")}${dropped}`;
81 }
82
83 /**
84 * An OBJ or PLY read as the viewer reads it, or null for any other file or one that is not
85 * readable or too big to read here.
86 */
87 export function indexedMesh(path: string, bytes: Uint8Array, size: number): Mesh | null {
88 const ext = extension(path);
89 if ((ext !== "obj" && ext !== "ply") || size > FACTS_CAP || bytes.length === 0) return null;
90 try {
91 return ext === "obj" ? parseObj(new TextDecoder().decode(bytes)) : parsePly(bytes);
92 } catch {
93 return null;
94 }
95 }
96
97 function indexedFacts(mesh: Mesh): Facts {
98 return {
99 triangles: mesh.triangles,
100 size: (mesh.box ?? bounds(mesh.positions)).size,
101 indexed: indexedWords(mesh),
102 };
103 }
104
71105 /** A GLB's first chunk, which the spec says is its JSON. */
72106 function glbJson(bytes: Uint8Array): string {
73107 const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
src/web/render/model3d-client.ts +34 −6
@@ -36,7 +36,8 @@ import { OrbitControls } from "three/addons/controls/OrbitControls.js";
3636 import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
3737 import { REFUSED_URI, withinModelDir } from "./gltf-uri.ts";
3838 import { parse3mf } from "./threemf.ts";
39 import { bounds, parseObj, parseStl, sizeText, type Bounds } from "./mesh.ts";
39 import { bounds, indexedRows, OVER_CAP_NOTE, parseObj, parseStl, sizeText, type Bounds, type Mesh as Parsed } from "./mesh.ts";
40 import { parsePly } from "./ply.ts";
4041
4142 /**
4243 * The little of the DOM this touches. The checker runs without the DOM
@@ -71,6 +72,10 @@ interface Loaded {
7172 readonly triangles: number;
7273 /** In the file's own axes and the unit below. */
7374 readonly box: Bounds;
75 /** Set for OBJ and PLY: what the file holds before it is cut into triangles. */
76 readonly rows?: ReturnType<typeof indexedRows> | undefined;
77 /** Too many triangles to draw: the facts show, the viewer does not. */
78 readonly overCap?: boolean | undefined;
7479 }
7580
7681 interface Viewer {
@@ -94,17 +99,25 @@ function soup(positions: Float32Array): Mesh {
9499 async function load(src: string, format: string): Promise<Loaded> {
95100 const response = await fetch(src, { credentials: "same-origin" });
96101 if (!response.ok) throw new Error(`the file answered ${response.status}`);
97 if (format === "stl" || format === "obj" || format === "3mf") {
98 const parsed =
102 if (format === "stl" || format === "obj" || format === "ply" || format === "3mf") {
103 const parsed: Parsed =
99104 format === "obj"
100105 ? parseObj(await response.text())
101106 : format === "3mf"
102107 ? await parse3mf(new Uint8Array(await response.arrayBuffer()))
103 : parseStl(new Uint8Array(await response.arrayBuffer()));
108 : format === "ply"
109 ? parsePly(new Uint8Array(await response.arrayBuffer()))
110 : parseStl(new Uint8Array(await response.arrayBuffer()));
104111 const mesh = soup(parsed.positions);
105112 // A printed part is modelled Z-up; three.js stands Y-up.
106113 if (format === "stl" || format === "3mf") mesh.rotation.x = -Math.PI / 2;
107 return { object: mesh, triangles: parsed.triangles, box: bounds(parsed.positions) };
114 return {
115 object: mesh,
116 triangles: parsed.triangles,
117 box: parsed.box ?? bounds(parsed.positions),
118 rows: parsed.vertices === undefined ? undefined : indexedRows(parsed),
119 overCap: parsed.overCap,
120 };
108121 }
109122 // glTF: binary, or JSON whose buffers sit beside it on the media route.
110123 const data = format === "glb" ? await response.arrayBuffer() : await response.text();
@@ -178,8 +191,23 @@ function mount(figure: Element): void {
178191 };
179192
180193 load(src, format).then(
181 ({ object, triangles, box }) => {
194 ({ object, triangles, box, rows, overCap }) => {
182195 if (!figure.isConnected) return renderer.dispose();
196 if (rows !== undefined) {
197 fact("[data-vertices]", rows.vertices);
198 fact("[data-faces]", rows.faces);
199 }
200 if (overCap) {
201 // Counted, not drawn: the facts stay, the note and Download with them.
202 renderer.dispose();
203 if (rows !== undefined) {
204 fact("[data-triangles]", rows.triangles);
205 fact("[data-box]", rows.box);
206 }
207 if (note !== null) note.hidden = false;
208 stats.textContent = OVER_CAP_NOTE;
209 return;
outdated

Plain words (docs/WRITING.md): 'Too many triangles to draw here.' tells nothing to do. Say it can be downloaded. Also untested.

210 }
183211 const scene = new Scene();
184212 scene.add(object);
185213 // Stand the part on the grid, centred under the camera.
src/web/render/model3d.ts +17 −5
@@ -1,5 +1,5 @@
11 /**
2 * An STL, OBJ or glTF file as a part you can turn: a server-rendered frame,
2 * An STL, OBJ, PLY or glTF file as a part you can turn: a server-rendered frame,
33 * and the viewer (`model3d-client.ts`, with the vendored three.js) loaded
44 * only by the pages that carry one.
55 *
@@ -22,6 +22,8 @@ import { createLog } from "../../kernel/log.ts";
2222 import type { BuiltAsset } from "../asset.ts";
2323 import { html, type Html } from "../html.ts";
2424 import { btn, empty, fileSize } from "../ui.ts";
25 import { indexedRows } from "./mesh.ts";
26 import { indexedMesh } from "./model-facts.ts";
2527 import type { Rendered, RenderInput } from "./renderers.ts";
2628
2729 const log = createLog("web");
@@ -30,7 +32,7 @@ const ENTRY = Bun.fileURLToPath(new URL("./model3d-client.ts", import.meta.url))
3032 const VENDOR = join(import.meta.dir, "vendor", "three");
3133
3234 /** What the viewer reads each extension as. */
33 const FORMATS: Readonly<Record<string, string>> = { stl: "stl", obj: "obj", glb: "glb", gltf: "gltf", "3mf": "3mf" };
35 const FORMATS: Readonly<Record<string, string>> = { stl: "stl", obj: "obj", ply: "ply", glb: "glb", gltf: "gltf", "3mf": "3mf" };
3436
3537 const formatOf = (path: string): string =>
3638 FORMATS[path.slice(path.lastIndexOf(".") + 1).toLowerCase()] ?? "stl";
@@ -39,6 +41,14 @@ export function model3d(input: RenderInput): Rendered {
3941 const name = input.path.slice(input.path.lastIndexOf("/") + 1);
4042 const toggle = (label: string, value: string): Html =>
4143 btn(label, { type: "button", size: "sm", variant: "outline", extra: "js-only", pressed: false, value });
44 // An OBJ or PLY the page read has its counts in the first paint; the viewer
45 // fills the same rows for a file it fetched itself.
46 const mesh = indexedMesh(input.path, input.body, input.size);
47 const rows = mesh === null ? undefined : indexedRows(mesh);
48 const row = (key: string, label: string, value: string | undefined): Html =>
49 value === undefined
50 ? html`<div data-${key} hidden><dt>${label}</dt><dd></dd></div>`
51 : html`<div data-${key}><dt>${label}</dt><dd>${value}</dd></div>`;
4252 return {
4353 lines: 0,
4454 content: html`<figure
@@ -52,8 +62,10 @@ export function model3d(input: RenderInput): Rendered {
5262 <dl class="row-facts">
5363 <div><dt>Format</dt><dd>${formatOf(input.path).toUpperCase()}</dd></div>
5464 <div><dt>File</dt><dd>${fileSize(input.size)}</dd></div>
55 <div data-triangles hidden><dt>Triangles</dt><dd></dd></div>
56 <div data-box hidden><dt>Box</dt><dd></dd></div>
65 ${row("vertices", "Vertices", rows?.vertices)}
66 ${row("faces", "Faces", rows?.faces)}
67 ${row("triangles", "Triangles", rows?.triangles)}
68 ${row("box", "Box", rows?.box)}
5769 </dl>
5870 <span class="muted" data-stats aria-live="polite"></span>
5971 <span class="spacer"></span>
@@ -79,7 +91,7 @@ function sources(): string[] {
7991 readdirSync(dir, { withFileTypes: true })
8092 .flatMap((e) => (e.isDirectory() ? walk(join(dir, e.name)) : [join(dir, e.name)]))
8193 .sort();
82 return [ENTRY, join(import.meta.dir, "mesh.ts"), ...walk(VENDOR)];
94 return [ENTRY, join(import.meta.dir, "mesh.ts"), join(import.meta.dir, "ply.ts"), ...walk(VENDOR)];
8395 }
8496
8597 let path: string | null = null;
src/web/render/ply.ts added +161
@@ -0,0 +1,161 @@
1 /**
2 * Stanford PLY, ASCII and binary little-endian, read into the same triangle
3 * soup as the other meshes (`mesh.ts`). The header names the elements and
4 * their properties; only `vertex` (x, y, z) and `face` (a list of vertex
5 * indices) are kept and every other element is read past. Big-endian is
6 * refused. A file that ends before its header says it should is a
7 * `MeshError`, never a partial mesh; a face that names a missing vertex is
8 * dropped and counted.
9 *
10 * Nothing is allocated from a count in the header, and no time is spent on
11 * one either: an element whose rows could not fit in the bytes left is refused
12 * before its first row, and an element with no properties reads nothing.
13 */
14
15 import { MeshError, soupBuilder, TRIANGLE_LIMIT, type Mesh } from "./mesh.ts";
16
17 interface Property {
18 readonly name: string;
19 readonly type: string;
20 /** For a list: the type of its length. */
21 readonly lengthType?: string;
22 }
23 interface Element {
24 readonly name: string;
25 readonly count: number;
26 readonly properties: Property[];
27 }
28
29 const SIZES: Readonly<Record<string, number>> = {
30 char: 1, int8: 1, uchar: 1, uint8: 1,
31 short: 2, int16: 2, ushort: 2, uint16: 2,
32 int: 4, int32: 4, uint: 4, uint32: 4,
33 float: 4, float32: 4, double: 8, float64: 8,
34 };
35
36 const HEADER_LIMIT = 64 * 1024;
37
38 function isPly(bytes: Uint8Array): boolean {
39 return bytes.length >= 4 && bytes[0] === 0x70 && bytes[1] === 0x6c && bytes[2] === 0x79 && (bytes[3] === 0x0a || bytes[3] === 0x0d);
40 }
41
42 export function parsePly(bytes: Uint8Array, limit = TRIANGLE_LIMIT): Mesh {
43 if (!isPly(bytes)) throw new MeshError("not a PLY file");
44 const head = new TextDecoder("latin1").decode(bytes.subarray(0, HEADER_LIMIT));
45 const end = /\bend_header\r?\n/.exec(head);
46 if (end === null) throw new MeshError("a PLY header that does not end");
47 const { binary, elements } = readHeader(head.slice(0, end.index).split(/\r?\n/));
48 const body = bytes.subarray(end.index + end[0].length);
49 const reader = binary ? binaryReader(body) : asciiReader(new TextDecoder().decode(body));
50
51 const vertices: number[] = [];
52 const soup = soupBuilder(vertices, limit);
53 for (const element of elements) {
54 const per = element.properties.length === 0 ? 0 : reader.rowMin(element.properties);
55 if (element.count * per > reader.left()) throw new MeshError("the PLY ends before its header says it should");
56 const keep = element.name === "vertex" ? "vertex" : element.name === "face" ? "face" : null;
57 if (element.properties.length === 0) {
outdated

Principle 8/robustness: this loop runs element.count times even when element.properties is empty, so a header claiming 4e9 rows of an element with no properties never ends. Bound the row count by remaining bytes.

58 // Rows of nothing: no bytes to read, so no loop to run on a count.
59 if (keep === "vertex" && element.count > 0) throw new MeshError("a vertex without x, y and z");
60 continue;
61 }
62 for (let row = 0; row < element.count; row++) {
63 let xyz: Record<string, number> = {};
64 let list: number[] = [];
65 for (const p of element.properties) {
66 if (p.lengthType !== undefined) {
67 const n = reader.next(p.lengthType);
68 const items: number[] = [];
69 for (let i = 0; i < n; i++) items.push(reader.next(p.type));
70 if (keep === "face" && (p.name === "vertex_indices" || p.name === "vertex_index")) list = items;
71 } else {
72 const v = reader.next(p.type);
73 if (keep === "vertex") xyz = { ...xyz, [p.name]: v };
74 }
75 }
76 if (keep === "vertex") {
77 const { x, y, z } = xyz;
78 if (![x, y, z].every(Number.isFinite)) throw new MeshError("a vertex without x, y and z");
79 vertices.push(x!, y!, z!);
80 } else if (keep === "face") soup.face(list);
81 }
82 }
83 return soup.done();
84 }
85
86 function readHeader(lines: string[]): { binary: boolean; elements: Element[] } {
87 let binary: boolean | null = null;
88 const elements: { name: string; count: number; properties: Property[] }[] = [];
89 for (const line of lines.slice(1)) {
90 const w = line.trim().split(/\s+/);
91 if (w[0] === "format") {
92 if (w[1] === "ascii") binary = false;
93 else if (w[1] === "binary_little_endian") binary = true;
94 else throw new MeshError(`a PLY format that is not read: ${w[1] ?? ""}`);
95 } else if (w[0] === "element") {
96 const count = Number(w[2]);
97 if (w[1] === undefined || !Number.isInteger(count) || count < 0) throw new MeshError("an element without a count");
98 elements.push({ name: w[1], count, properties: [] });
99 } else if (w[0] === "property") {
100 const element = elements[elements.length - 1];
101 if (element === undefined) throw new MeshError("a property before any element");
102 if (w[1] === "list") {
103 if (SIZES[w[2] ?? ""] === undefined || SIZES[w[3] ?? ""] === undefined || w[4] === undefined)
104 throw new MeshError("a list property of an unknown type");
105 element.properties.push({ name: w[4], type: w[3]!, lengthType: w[2]! });
106 } else {
107 if (SIZES[w[1] ?? ""] === undefined || w[2] === undefined) throw new MeshError("a property of an unknown type");
108 element.properties.push({ name: w[2], type: w[1]! });
109 }
110 }
111 }
112 if (binary === null) throw new MeshError("a PLY header with no format");
113 return { binary, elements };
114 }
115
116 interface Reader {
117 next(type: string): number;
118 /** Bytes (binary) or characters (ASCII) not yet read. */
119 left(): number;
120 /** The least one row of these properties can take. */
121 rowMin(properties: readonly Property[]): number;
122 }
123
124 function binaryReader(body: Uint8Array): Reader {
125 const view = new DataView(body.buffer, body.byteOffset, body.byteLength);
126 let at = 0;
127 return {
128 left: () => body.length - at,
129 rowMin: (props) => props.reduce((n, p) => n + SIZES[p.lengthType ?? p.type]!, 0),
130 next(type) {
131 const size = SIZES[type]!;
132 if (at + size > body.length) throw new MeshError("the PLY ends before its header says it should");
133 const o = at;
134 at += size;
135 switch (type) {
136 case "char": case "int8": return view.getInt8(o);
137 case "uchar": case "uint8": return view.getUint8(o);
138 case "short": case "int16": return view.getInt16(o, true);
139 case "ushort": case "uint16": return view.getUint16(o, true);
140 case "int": case "int32": return view.getInt32(o, true);
141 case "uint": case "uint32": return view.getUint32(o, true);
142 case "float": case "float32": return view.getFloat32(o, true);
143 default: return view.getFloat64(o, true);
144 }
145 },
146 };
147 }
148
149 function asciiReader(text: string): Reader {
150 const re = /\S+/g;
151 return {
152 left: () => text.length - re.lastIndex,
153 // A value is a character and the space after it.
154 rowMin: (props) => 2 * props.length - 1,
155 next() {
156 const m = re.exec(text);
157 if (m === null) throw new MeshError("the PLY ends before its header says it should");
158 return Number(m[0]);
159 },
160 };
161 }
src/web/render/threemf.ts +3 −3
@@ -15,14 +15,14 @@
1515 * by entry count and inflated size, so a zip bomb is refused.
1616 */
1717
18 import { MeshError, type Mesh } from "./mesh.ts";
18 import { MeshError, TRIANGLE_LIMIT, type Mesh } from "./mesh.ts";
19
20 export { TRIANGLE_LIMIT };
1921
2022 /** More entries than any slicer writes; a zip with more is refused. */
2123 export const ENTRY_LIMIT = 10_000;
2224 /** The most the model XML may inflate to. */
2325 export const INFLATE_LIMIT = 64 * 1024 * 1024;
24 /** Triangles beyond which a part is refused rather than drawn. */
25 export const TRIANGLE_LIMIT = 5_000_000;
2626 const DEPTH_LIMIT = 16;
2727
2828 /** Millimetres in one unit of each `unit` value (3MF Core §3.4). */
test/model3d.test.ts +2 −2
@@ -57,7 +57,7 @@ describe("the mesh parsers", () => {
5757 });
5858
5959 test("a file that is not the format says so instead of drawing nonsense", () => {
60 expect(() => parseObj("v 0 0 0\nf 1 2 3\n")).toThrow(MeshError);
60 expect(parseObj("v 0 0 0\nf 1 2 3\n").dropped).toBe(1);
6161 expect(() => parseObj("v 0 zero 0\n")).toThrow(MeshError);
6262 expect(() => parseStl(new TextEncoder().encode("hello"))).toThrow(MeshError);
6363 expect(() =>
@@ -164,7 +164,7 @@ describe("a model in words, for the terminal", () => {
164164 expect(modelLine(modelFacts(name, bytes, bytes.length))).toBe("3D model · STL · 12 triangles · 20 × 20 × 20 mm");
165165 }
166166 const obj = await fixture("cube.obj");
167 expect(modelLine(modelFacts("cube.obj", obj, obj.length))).toBe("3D model · OBJ · 12 triangles · 20 × 20 × 20 mm");
167 expect(modelLine(modelFacts("cube.obj", obj, obj.length))).toBe("3D model · OBJ · 12 triangles · 20 × 20 × 20 mm · 8 vertices · 6 faces");
168168 });
169169
170170 test("a GLB is read from its JSON: vertex counts, and position bounds in metres", () => {
test/render-kinds.test.ts +1
@@ -40,6 +40,7 @@ const SAMPLES: Record<string, [Uint8Array, FileKind, string]> = {
4040 stl: [enc("solid cube\n"), "model3d", "model/stl"],
4141 glb: [enc("glTF\x02\0\0\0"), "model3d", "model/gltf-binary"],
4242 "3mf": [enc("PK\x03\x04"), "model3d", "model/3mf"],
43 ply: [enc("ply\nformat ascii 1.0\n"), "model3d", "model/ply"],
4344 db: [enc("SQLite format 3\0"), "sqlite", "application/vnd.sqlite3"],
4445 sqlite: [enc("SQLite format 3\0"), "sqlite", "application/vnd.sqlite3"],
4546 sqlite3: [enc("SQLite format 3\0"), "sqlite", "application/vnd.sqlite3"],
test/render-obj-ply.test.ts added +174
@@ -0,0 +1,174 @@
1 /** OBJ and PLY meshes: a cube in each format, a hostile index, a truncated file, the cap. */
2
3 import { describe, expect, test } from "bun:test";
4 import { kindByName } from "../src/web/render/kinds.ts";
5 import { bounds, indexedRows, OVER_CAP_NOTE, parseObj, TRIANGLE_LIMIT } from "../src/web/render/mesh.ts";
6 import { modelFacts } from "../src/web/render/model-facts.ts";
7 import { MeshError } from "../src/web/render/mesh.ts";
8 import { model3d, model3dAsset } from "../src/web/render/model3d.ts";
9 import { parsePly } from "../src/web/render/ply.ts";
10
11 const enc = (s: string): Uint8Array => new TextEncoder().encode(s);
12
13 const CUBE: number[][] = [
14 [0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0],
15 [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1],
16 ];
17 const QUADS = [[0, 3, 2, 1], [4, 5, 6, 7], [0, 1, 5, 4], [2, 3, 7, 6], [1, 2, 6, 5], [3, 0, 4, 7]];
18
19 const objCube = (): string =>
20 CUBE.map((v) => `v ${v.join(" ")}`).join("\n") + "\n" + QUADS.map((q) => `f ${q.map((i) => i + 1).join(" ")}`).join("\n") + "\n";
21
22 const plyAscii = (): string =>
23 `ply\nformat ascii 1.0\nelement vertex 8\nproperty float x\nproperty float y\nproperty float z\n` +
24 `element face 6\nproperty list uchar int vertex_indices\nend_header\n` +
25 CUBE.map((v) => v.join(" ")).join("\n") + "\n" + QUADS.map((q) => `4 ${q.join(" ")}`).join("\n") + "\n";
26
27 function plyBinary(): Uint8Array {
28 const head = enc(
29 `ply\nformat binary_little_endian 1.0\nelement vertex 8\nproperty float x\nproperty float y\nproperty float z\n` +
30 `property uchar red\nelement face 6\nproperty list uchar int vertex_indices\nend_header\n`,
31 );
32 const body = new DataView(new ArrayBuffer(8 * 13 + 6 * 17));
33 let at = 0;
34 for (const v of CUBE) {
35 for (const c of v) { body.setFloat32(at, c, true); at += 4; }
36 body.setUint8(at++, 255);
37 }
38 for (const q of QUADS) {
39 body.setUint8(at++, 4);
40 for (const i of q) { body.setInt32(at, i, true); at += 4; }
41 }
42 const out = new Uint8Array(head.length + at);
43 out.set(head);
44 out.set(new Uint8Array(body.buffer, 0, at), head.length);
45 return out;
46 }
47
48 describe("a cube in each format", () => {
49 for (const [name, mesh] of [
50 ["OBJ", () => parseObj(objCube())],
51 ["ASCII PLY", () => parsePly(enc(plyAscii()))],
52 ["binary PLY", () => parsePly(plyBinary())],
53 ] as const)
54 test(`${name}: 8 vertices, 6 faces, 12 triangles, a unit box`, () => {
55 const m = mesh();
56 expect([m.vertices, m.faces, m.triangles, m.dropped, m.overCap]).toEqual([8, 6, 12, 0, false]);
57 expect(bounds(m.positions).size).toEqual([1, 1, 1]);
58 });
59
60 test("the facts line says vertices, faces and the box", () => {
61 expect(modelFacts("c.obj", enc(objCube()), 100).facts).toBe("12 triangles · 1 × 1 × 1 mm · 8 vertices · 6 faces");
62 expect(modelFacts("c.ply", plyBinary(), 100).facts).toBe("12 triangles · 1 × 1 × 1 mm · 8 vertices · 6 faces");
63 });
64 });
65
66 describe("what a hostile file cannot do", () => {
67 test("a face naming a missing vertex is dropped and counted, in OBJ and in PLY", () => {
68 const obj = parseObj("v 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\nf 1 2 99\nf -9 1 2\nf 0 1 2\n");
69 expect([obj.faces, obj.dropped, obj.triangles]).toEqual([1, 3, 1]);
70 const ply = parsePly(
71 enc(`ply\nformat ascii 1.0\nelement vertex 3\nproperty float x\nproperty float y\nproperty float z\nelement face 2\nproperty list uchar int vertex_indices\nend_header\n0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n3 0 1 7\n`),
72 );
73 expect([ply.faces, ply.dropped, ply.triangles]).toEqual([1, 1, 1]);
74 expect(modelFacts("x.obj", enc("v 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\nf 1 2 99\n"), 50).facts).toContain("(1 face dropped)");
75 });
76
77 test("negative OBJ indices count back from the last vertex read", () => {
78 expect(parseObj("v 0 0 0\nv 1 0 0\nv 0 2 0\nf -3 -2 -1\n").triangles).toBe(1);
79 });
80
81 test("a truncated PLY is an error, not a partial mesh, and a huge claimed count costs nothing", () => {
82 const whole = plyBinary();
83 expect(() => parsePly(whole.subarray(0, whole.length - 5))).toThrow(MeshError);
84 expect(() => parsePly(enc(plyAscii().slice(0, 150)))).toThrow(MeshError);
85 expect(() => parsePly(enc("ply\nformat ascii 1.0\nelement vertex 4000000000\nproperty float x\nproperty float y\nproperty float z\nend_header\n1 2 3\n"))).toThrow(MeshError);
86 expect(modelFacts("t.ply", whole.subarray(0, 60), 60).facts).toBe("not a readable PLY file");
87 });
88
89 test("big-endian and unknown types are refused", () => {
90 expect(() => parsePly(enc("ply\nformat binary_big_endian 1.0\nend_header\n"))).toThrow(MeshError);
91 expect(() => parsePly(enc("ply\nformat ascii 1.0\nelement vertex 1\nproperty quad x\nend_header\n"))).toThrow(MeshError);
92 expect(() => parsePly(enc("hello"))).toThrow(MeshError);
93 });
94
95 test("past the triangle cap the facts stay and the positions go", () => {
96 const m = parseObj(objCube(), 10);
97 expect([m.overCap, m.triangles, m.faces, m.vertices, m.positions.length]).toEqual([true, 12, 6, 8, 0]);
98 expect(m.box?.size).toEqual([1, 1, 1]);
99 expect(parsePly(plyBinary(), 10).overCap).toBe(true);
100 expect(parseObj(objCube(), 12).overCap).toBe(false);
101 expect(TRIANGLE_LIMIT).toBe(5_000_000);
102 });
103 });
104
105 describe("kinds", () => {
106 test(".obj and .ply open in the 3D viewer", () => {
107 expect(kindByName("a/part.obj")).toBe("model3d");
108 expect(kindByName("a/part.PLY")).toBe("model3d");
109 expect(kindByName("a/part.stp")).not.toBe("model3d");
110 });
111 });
112
113 describe("time is bounded by the bytes, not by a header's count", () => {
114 const head = (format: string, elements: string): Uint8Array => enc(`ply\nformat ${format} 1.0\n${elements}end_header\n`);
115
116 test("an element with no properties and a huge count reads nothing and ends at once", () => {
117 for (const format of ["ascii", "binary_little_endian"]) {
118 const started = performance.now();
119 const m = parsePly(head(format, "element foo 4000000000\n"));
120 expect([m.triangles, m.vertices, m.faces]).toEqual([0, 0, 0]);
121 expect(performance.now() - started).toBeLessThan(500);
122 }
123 });
124
125 test("a vertex element with no properties is still not a vertex", () => {
126 expect(() => parsePly(head("ascii", "element vertex 3\n"))).toThrow(MeshError);
127 });
128
129 test("an element whose rows cannot fit in the bytes left is refused before its first row", () => {
130 const started = performance.now();
131 expect(() => parsePly(head("ascii", "element foo 4000000000\nproperty uchar a\n"))).toThrow(MeshError);
132 expect(() => parsePly(head("binary_little_endian", "element foo 4000000000\nproperty list uchar uchar a\n"))).toThrow(MeshError);
133 expect(performance.now() - started).toBeLessThan(500);
134 });
135
136 test("an element that fits still reads", () => {
137 const m = parsePly(enc("ply\nformat ascii 1.0\nelement extra 2\nproperty uchar a\nelement vertex 3\nproperty float x\nproperty float y\nproperty float z\nelement face 1\nproperty list uchar int vertex_indices\nend_header\n1\n2\n0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n"));
138 expect([m.vertices, m.faces, m.triangles]).toEqual([3, 1, 1]);
139 });
140 });
141
142 describe("the rows beside the viewer", () => {
143 const ply = enc(plyAscii());
144
145 test("the page's first paint has vertices, faces, triangles and box, with no script", () => {
146 const { content } = model3d({ path: "a/cube.ply", size: ply.length, body: ply, media: "/m", download: "/m?download=1" });
147 expect(content.text).toContain("<div data-vertices><dt>Vertices</dt><dd>8</dd></div>");
148 expect(content.text).toContain("<div data-faces><dt>Faces</dt><dd>6</dd></div>");
149 expect(content.text).toContain("<dd>12</dd>");
150 expect(content.text).toContain("<dd>1 × 1 × 1 mm</dd>");
151 expect(content.text).not.toContain("<script");
152 });
153
154 test("a format with no vertex count, or a file not read, leaves the rows for the viewer", () => {
155 const stl = model3d({ path: "a.stl", size: 5, body: enc("solid x"), media: "/m", download: "/m?download=1" });
156 expect(stl.content.text).toContain("<div data-vertices hidden>");
157 const big = model3d({ path: "a.ply", size: 5, body: new Uint8Array(), media: "/m", download: "/m?download=1" });
158 expect(big.content.text).toContain("<div data-faces hidden>");
159 });
160
161 test("the words say a dropped face, and one vertex is not 'vertexs'", () => {
162 const m = parseObj("v 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\nf 1 2 9\n");
163 expect(indexedRows(m).faces).toBe("1 (1 face dropped)");
164 expect(modelFacts("a.obj", enc("v 0 0 0\n"), 8).facts).toContain("1 vertex ·");
165 expect(modelFacts("a.obj", enc(objCube()), 100).facts).toContain("8 vertices · 6 faces");
166 });
167
168 test("over the cap the viewer says, in plain words, what to do; the bundle carries that sentence", async () => {
169 expect(OVER_CAP_NOTE).toBe("This part has more triangles than can be drawn here; download it to open it elsewhere.");
170 const code = (await model3dAsset())!.code;
171 expect(code).toContain(OVER_CAP_NOTE);
172 expect(code).not.toContain("Too many triangles to draw here.");
173 });
174 });
test/render-subtitles-shift.test.ts +9 −7
@@ -268,19 +268,21 @@ describe("the terminal's shift", () => {
268268 test("a writer's shift is the same commit, announced on the bus", async () => {
269269 const { tui, command } = open("alice");
270270 const events: string[] = [];
271 const off = app.bus.subscribe((e) => events.push(e.kind));
271 let announce = () => {};
272 const announced = new Promise<void>((resolve) => (announce = resolve));
273 const off = app.bus.subscribe((e) => {
274 events.push(e.kind);
275 if (e.kind === "push") announce();
276 });
272277 try {
273278 void command[0]!.run();
274279 await settleApp(tui);
275280 await type(tui, "a.srt");
276281 for (let i = 0; i < 3; i++) tui.dispatch({ type: "key", key: "backspace", ctrl: false, alt: false, shift: false });
277282 await type(tui, "2.5");
278 const end = Date.now() + 10_000;
279 let log = await commitLog(repoDir(app.reposRoot, "alice", "subs"), "main", { limit: 1 });
280 while (!(log.ok && log.value[0]?.subject === "Shift subtitles by +2.5 s") && Date.now() < end) {
281 await Bun.sleep(25);
282 log = await commitLog(repoDir(app.reposRoot, "alice", "subs"), "main", { limit: 1 });
283 }
283 // The commit is made before the push is announced: wait for that, then read the log.
284 await Promise.race([announced, new Promise((r) => setTimeout(r, 10_000))]); // real-timer: the deadline if no push is ever announced
285 const log = await commitLog(repoDir(app.reposRoot, "alice", "subs"), "main", { limit: 1 });
284286 expect(log.ok && log.value[0]?.subject).toBe("Shift subtitles by +2.5 s");
285287 expect(events).toContain("push");
286288 } finally {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs