|
1 | 1 | import { expect, test } from 'bun:test' |
2 | 2 | import { createModelGeometry, modelTransform, multiplyModelMatrices, parseBinaryModelStl, transformModelPoint } from '../src/model-viewer' |
| 3 | +import type { ModelGeometry } from '../src/model-viewer' |
| 4 | + |
| 5 | +test('indexed inline meshes preserve winding and repeated shared vertices without a fetch', () => { |
| 6 | + const geometry: ModelGeometry = { kind: 'mesh', vertices: [[0, 0, 0], [10, 0, 0], [10, 20, 0], [0, 20, 0]], triangles: [[0, 1, 2], [0, 2, 3]] } |
| 7 | + const output = createModelGeometry(geometry) |
| 8 | + expect([...output]).toEqual([0, 0, 0, 10, 0, 0, 10, 20, 0, 0, 0, 0, 10, 20, 0, 0, 20, 0]) |
| 9 | + output[0] = 99 |
| 10 | + expect(geometry.vertices[0]).toEqual([0, 0, 0]) |
| 11 | +}) |
| 12 | + |
| 13 | +test('inline meshes reject malformed, nonfinite, Float32-overflowing and oversized input before delivery', () => { |
| 14 | + const valid: Extract<ModelGeometry, { kind: 'mesh' }> = { kind: 'mesh', vertices: [[0, 0, 0], [1, 0, 0], [0, 1, 0]], triangles: [[0, 1, 2]] } |
| 15 | + const invalid = [ |
| 16 | + { ...valid, vertices: [] }, { ...valid, triangles: [] }, |
| 17 | + { ...valid, vertices: [[Number.NaN, 0, 0], [1, 0, 0], [0, 1, 0]] }, |
| 18 | + { ...valid, vertices: [[1e100, 0, 0], [1, 0, 0], [0, 1, 0]] }, |
| 19 | + { ...valid, vertices: [[0, 0], [1, 0, 0], [0, 1, 0]] }, |
| 20 | + { ...valid, vertices: [[0, , 0], [1, 0, 0], [0, 1, 0]] }, |
| 21 | + { ...valid, triangles: [[0, 1, 3]] }, { ...valid, triangles: [[0, -1, 2]] }, |
| 22 | + { ...valid, triangles: [[0, 1.5, 2]] }, { ...valid, triangles: [[0, 0, 2]] }, |
| 23 | + { ...valid, triangles: [[0, 1]] }, |
| 24 | + { ...valid, triangles: [[0, , 2]] }, |
| 25 | + { ...valid, vertices: Array.from({ length: 500001 }, () => [0, 0, 0]) }, |
| 26 | + { ...valid, triangles: Array.from({ length: 500001 }, () => [0, 1, 2]) }, |
| 27 | + ] |
| 28 | + for (const input of invalid) |
| 29 | + expect(() => createModelGeometry(input as typeof valid)).toThrow() |
| 30 | +}) |
3 | 31 |
|
4 | 32 | test('model transforms preserve CAD millimeters and compose local rotations before world placement', () => { |
5 | 33 | const point = transformModelPoint([70, 155, 6], modelTransform([0, 27, 200], [90, 0, 0])) |
|
0 commit comments