/ concept-collection / mesh-studio
Sign in
concept-collection / mesh-studio
mesh-studio / src / export / nurbsJson.ts
39 lines · 1.2 KBCodeBlameHistory
2 * The polynomial-native export: dump each face's NURBS surface (degrees, poles,
3 * weights, knots) to JSON. This is the "mesh defined by polynomials on faces"
4 * representation, distinct from any triangulated file.
5 */
6import type { SurfaceModel } from '../model/types'
8export function toNurbsJson(model: SurfaceModel): Uint8Array {
9 const patches = model.patches.map((p) => {
10 if (p.kind === 'nurbs' && p.nurbs) {
11 const n = p.nurbs
12 return {
13 id: p.id,
14 kind: 'nurbs',
15 uDegree: n.uDegree,
16 vDegree: n.vDegree,
17 nu: n.nu,
18 nv: n.nv,
19 rational: n.weights !== null,
20 uKnots: n.uKnots,
21 uMults: n.uMults,
22 vKnots: n.vKnots,
23 vMults: n.vMults,
24 poles: Array.from(n.poles),
25 weights: n.weights ? Array.from(n.weights) : null,
26 }
27 }
28 return { id: p.id, kind: p.kind, nurbs: null }
29 })
30 const doc = {
31 format: 'mesh-studio-nurbs',
32 version: 1,
33 source: model.source.label,
34 patchCount: model.patches.length,
35 polesLayout: 'row-major, pole(i,j) at (i*nv + j)*3',
36 patches,
37 }
38 return new TextEncoder().encode(JSON.stringify(doc, null, 2))
moveopenescclose