@rulecad/model-link 0.2.1 → 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.
- package/model-link.d.mts +10 -1
- package/model-link.mjs +16 -5
- package/package.json +5 -1
package/model-link.d.mts
CHANGED
|
@@ -29,7 +29,16 @@ export function parseHash(hash: string): {
|
|
|
29
29
|
export function fromGist(id: string): Promise<string>;
|
|
30
30
|
/** A link token back to a model, checked: damaged, newer or oversized links are refused. @param {string} token @returns {Promise<Model>} */
|
|
31
31
|
export function decode(token: string): Promise<Model>;
|
|
32
|
-
|
|
32
|
+
/**
|
|
33
|
+
* What's wrong with a model's data, as findings (the kit's one result shape): [{ level: 'fail', text, code: 'link.data' }],
|
|
34
|
+
* empty when it's sound.
|
|
35
|
+
* @param {any} m @returns {{ level: 'fail', text: string, code: string }[]}
|
|
36
|
+
*/
|
|
37
|
+
export function check(m: any): {
|
|
38
|
+
level: "fail";
|
|
39
|
+
text: string;
|
|
40
|
+
code: string;
|
|
41
|
+
}[];
|
|
33
42
|
export function expand(m: any): any;
|
|
34
43
|
export function fromShapes(list: any): {
|
|
35
44
|
shapes: {
|
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;
|
|
@@ -110,12 +113,12 @@ export async function decode(token) {
|
|
|
110
113
|
try { model = JSON.parse(new TextDecoder().decode(await pipe(unb64(m[2]), new DecompressionStream('deflate-raw'), LIMITS.bytes))); }
|
|
111
114
|
catch (e) { if (e?.tooBig) throw e; throw new Error('This link is damaged: it may have been cut short when it was shared. Copy the whole link again.'); }
|
|
112
115
|
if (model && Array.isArray(model.shapes)) model = expand(model);
|
|
113
|
-
const errors =
|
|
116
|
+
const errors = problems(model);
|
|
114
117
|
if (errors.length) throw new Error(errors[0]);
|
|
115
118
|
return model;
|
|
116
119
|
}
|
|
117
120
|
|
|
118
|
-
|
|
121
|
+
function problems(m) {
|
|
119
122
|
if (!m || typeof m !== 'object' || !Array.isArray(m.p) || !Array.isArray(m.t)) return ['no model in the link'];
|
|
120
123
|
if (typeof m.q !== 'number' || !Number.isFinite(m.q) || !(m.q > 0)) return ['bad grid size'];
|
|
121
124
|
if (m.p.length % 3 || m.t.length % 3) return ['broken model data'];
|
|
@@ -133,6 +136,13 @@ export function check(m) {
|
|
|
133
136
|
return [];
|
|
134
137
|
}
|
|
135
138
|
|
|
139
|
+
/**
|
|
140
|
+
* What's wrong with a model's data, as findings (the kit's one result shape): [{ level: 'fail', text, code: 'link.data' }],
|
|
141
|
+
* empty when it's sound.
|
|
142
|
+
* @param {any} m @returns {{ level: 'fail', text: string, code: string }[]}
|
|
143
|
+
*/
|
|
144
|
+
export function check(m) { return problems(m).map((text) => ({ level: 'fail', text, code: 'link.data' })); }
|
|
145
|
+
|
|
136
146
|
// A link's motion, as data: numbers where numbers go, items and joints that exist, nothing that could run
|
|
137
147
|
const nums = (a, n) => Array.isArray(a) && (n == null || a.length === n) && a.every((x) => typeof x === 'number' && Number.isFinite(x));
|
|
138
148
|
function motionOk(mo, places) {
|
|
@@ -154,7 +164,7 @@ function motionOk(mo, places) {
|
|
|
154
164
|
export function expand(m) {
|
|
155
165
|
const { shapes, place, ...meta } = m;
|
|
156
166
|
if (!Array.isArray(place) || shapes.length > LIMITS.shapes || place.length > LIMITS.places) throw new Error('model too big for a link');
|
|
157
|
-
for (const sh of shapes) { const e =
|
|
167
|
+
for (const sh of shapes) { const e = problems(sh); if (e.length) throw new Error(e[0]); }
|
|
158
168
|
// (counted before any copy is made: a few placements of a big shape would otherwise be built in full, then refused)
|
|
159
169
|
let count = 0;
|
|
160
170
|
for (const [i] of place) { count += (shapes[i]?.t.length || 0) / 3; if (count > LIMITS.triangles) throw new Error('model too big for a link'); }
|
|
@@ -329,11 +339,12 @@ export function fromSTL(bytes) {
|
|
|
329
339
|
if (count > LIMITS.triangles) throw new Error(`${count} triangles: more than a link can hold (${LIMITS.triangles})`);
|
|
330
340
|
const tris = [];
|
|
331
341
|
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))));
|
|
342
|
+
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
343
|
return tris;
|
|
333
344
|
}
|
|
334
345
|
const text = new TextDecoder().decode(u8);
|
|
335
|
-
if (!/^\s*solid\b
|
|
336
|
-
const v = [...text.matchAll(/vertex\s+(\S+)\s+(\S+)\s+(\S+)/
|
|
346
|
+
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)
|
|
347
|
+
const v = [...text.matchAll(/vertex\s+(\S+)\s+(\S+)\s+(\S+)/gi)].map((m) => [Number(m[1]), Number(m[2]), Number(m[3])]);
|
|
337
348
|
if (v.length % 3 || v.some((p) => p.some((c) => !Number.isFinite(c)))) throw new Error('this STL file is damaged');
|
|
338
349
|
if (v.length / 3 > LIMITS.triangles) throw new Error(`${v.length / 3} triangles: more than a link can hold (${LIMITS.triangles})`);
|
|
339
350
|
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.
|
|
3
|
+
"version": "0.3.0",
|
|
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
|
},
|