Skip to content

Commit

Permalink
Add test case for mode=POINTS input to weld()
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Oct 5, 2023
1 parent 566095d commit eb9eb65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
12 changes: 11 additions & 1 deletion packages/functions/src/weld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function weld(_options: WeldOptions = WELD_DEFAULTS): Transform {
for (const prim of mesh.listPrimitives()) {
weldPrimitive(doc, prim, options);

if (prim.getIndices()?.getCount() === 0) prim.dispose();
if (isPrimEmpty(prim)) prim.dispose();
}

if (mesh.listPrimitives().length === 0) mesh.dispose();
Expand Down Expand Up @@ -430,3 +430,13 @@ function expandWeldOptions(_options: WeldOptions): Required<WeldOptions> {

return options;
}

/**
* For purposes of welding, we consider a primitive to be 'empty' or degenerate
* if (1) it has an index, and (2) that index is empty. In some cases
* (mode=POINTS) the index may be missing — this is outside the scope of welding.
*/
function isPrimEmpty(prim: Primitive): boolean {
const indices = prim.getIndices();
return !!indices && indices.getCount() === 0;
}
20 changes: 17 additions & 3 deletions packages/functions/test/weld.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,25 @@ test('modes', async (t) => {
t.deepEqual(
Array.from(primDef.indices),
Array.from(prim.getIndices().getArray()),
`${(i + 1).toString().padStart(2, '0')}: indices ${Array.from(primDef.indices)}`
`${(i + 1).toString().padStart(2, '0')}: indices ${Array.from(primDef.indices)}`,
);
}
});

test('points', async (t) => {
const doc = new Document().setLogger(logger);
const positionArray = new Float32Array([0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, -1]);
const position = doc.createAccessor().setType('VEC3').setArray(positionArray);
const prim = doc.createPrimitive().setAttribute('POSITION', position).setMode(Primitive.Mode.POINTS);
doc.createMesh().addPrimitive(prim);

// points can't be welded, but also shouldn't throw an error.
await doc.transform(weld({ tolerance: 0.001 }));

t.false(prim.isDisposed(), 'prim not disposed');
t.is(prim.getAttribute('POSITION').getArray().length, positionArray.length, 'prim vertices');
});

test('targets', async (t) => {
const document = new Document().setLogger(logger);
// prettier-ignore
Expand Down Expand Up @@ -265,7 +279,7 @@ test('targets', async (t) => {
t.deepEqual(
prim.listTargets()[0].getAttribute('POSITION').getArray(),
positionTargetArray.slice(9, 27),
'target positions'
'target positions',
);
t.is(document.getRoot().listAccessors().length, 3, 'accessor count');
});
Expand Down Expand Up @@ -297,6 +311,6 @@ test('degenerate', async (t) => {
0, 0, 1,
0, 0, -1
],
'vertices on prim'
'vertices on prim',
);
});

0 comments on commit eb9eb65

Please sign in to comment.