@rulecad/model-link 0.2.1 → 0.2.2

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 (2) hide show
  1. package/model-link.mjs +6 -2
  2. package/package.json +5 -1
package/model-link.mjs CHANGED
@@ -48,6 +48,9 @@ async function pipe(bytes, stream, most = Infinity) {
48
48
  // fromTriangles([[[x,y,z] ×3], …]) -> { q, p, t }: shared corners merged on a grid ~1/5000 of the model's size.
49
49
  /** @param {Triangle[]} tris @returns {{ q: number, p: number[], t: number[] }} */
50
50
  export function fromTriangles(tris) {
51
+ // (triangles: three corners each; a quad or a polygon is split into a fan, not passed through as a broken model)
52
+ if (tris.some((t) => !Array.isArray(t) || t.length < 3)) throw new Error('fromTriangles: each face needs three corners or more');
53
+ if (tris.some((t) => t.length > 3)) tris = tris.flatMap((t) => (t.length === 3 ? [t] : t.slice(1, -1).map((_, i) => [t[0], t[i + 1], t[i + 2]])));
51
54
  const lo = [Infinity, Infinity, Infinity], hi = [-Infinity, -Infinity, -Infinity]; // a loop: spreading big models overflows the stack
52
55
  for (const tri of tris) for (const v of tri) for (let k = 0; k < 3; k++) { if (v[k] < lo[k]) lo[k] = v[k]; if (v[k] > hi[k]) hi[k] = v[k]; }
53
56
  const extent = tris.length ? Math.max(hi[0] - lo[0], hi[1] - lo[1], hi[2] - lo[2]) : 0;
@@ -329,11 +332,12 @@ export function fromSTL(bytes) {
329
332
  if (count > LIMITS.triangles) throw new Error(`${count} triangles: more than a link can hold (${LIMITS.triangles})`);
330
333
  const tris = [];
331
334
  for (let i = 0, o = 84; i < count; i++, o += 50) tris.push([0, 1, 2].map((v) => [0, 1, 2].map((k) => view.getFloat32(o + 12 + v * 12 + k * 4, true))));
335
+ if (tris.some((t) => t.some((p) => p.some((c) => !Number.isFinite(c))))) throw new Error('this STL file is damaged: some of its corners are not numbers');
332
336
  return tris;
333
337
  }
334
338
  const text = new TextDecoder().decode(u8);
335
- if (!/^\s*solid\b/.test(text) || !/\bvertex\b/.test(text)) throw new Error("this isn't an STL file");
336
- const v = [...text.matchAll(/vertex\s+(\S+)\s+(\S+)\s+(\S+)/g)].map((m) => [Number(m[1]), Number(m[2]), Number(m[3])]);
339
+ if (!/^\s*solid\b/i.test(text) || !/\bvertex\b/i.test(text)) throw new Error("this isn't an STL file"); // (keywords in any case: some exporters write SOLID, VERTEX)
340
+ const v = [...text.matchAll(/vertex\s+(\S+)\s+(\S+)\s+(\S+)/gi)].map((m) => [Number(m[1]), Number(m[2]), Number(m[3])]);
337
341
  if (v.length % 3 || v.some((p) => p.some((c) => !Number.isFinite(c)))) throw new Error('this STL file is damaged');
338
342
  if (v.length / 3 > LIMITS.triangles) throw new Error(`${v.length / 3} triangles: more than a link can hold (${LIMITS.triangles})`);
339
343
  return Array.from({ length: v.length / 3 }, (_, i) => v.slice(i * 3, i * 3 + 3));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/model-link",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Share a printable model as a link (the model lives in the URL), and write STL or multi-colour Bambu Studio 3MF files, in the browser or Node.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -31,6 +31,10 @@
31
31
  "url": "git+https://github.com/JoniBach/rulecad.git",
32
32
  "directory": "packages/model-link"
33
33
  },
34
+ "homepage": "https://github.com/JoniBach/rulecad/tree/main/packages/model-link#readme",
35
+ "bugs": {
36
+ "url": "https://github.com/JoniBach/rulecad/issues"
37
+ },
34
38
  "scripts": {
35
39
  "test": "node --test"
36
40
  },