From d4e699e9bf3ee7f5bf48aa0bedde7c9bddbc1476 Mon Sep 17 00:00:00 2001 From: Deltakosh Date: Thu, 24 Oct 2024 10:02:39 -0700 Subject: [PATCH 1/4] Add warning when detecting negative genus --- packages/dev/core/src/Meshes/csg2.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/dev/core/src/Meshes/csg2.ts b/packages/dev/core/src/Meshes/csg2.ts index e480448ce83..a3dac59e280 100644 --- a/packages/dev/core/src/Meshes/csg2.ts +++ b/packages/dev/core/src/Meshes/csg2.ts @@ -119,7 +119,7 @@ export class CSG2 implements IDisposable { private _process(operation: "difference" | "intersection" | "union", csg: CSG2) { if (this.numProp !== csg.numProp) { - throw new Error("CSG must have the same number of properties"); + throw new Error("CSG must be used with geometries having the same number of properties"); } return new CSG2(Manifold[operation](this._manifold, csg._manifold), this.numProp, this._vertexStructure); } @@ -180,7 +180,7 @@ export class CSG2 implements IDisposable { for (let i = 0; i < manifoldMesh.triVerts.length; i += 3) { vertexData.indices[i] = manifoldMesh.triVerts[i + 2]; vertexData.indices[i + 1] = manifoldMesh.triVerts[i + 1]; - vertexData.indices[i + 2] = manifoldMesh.triVerts[i]; + vertexData.indices[i + 2] = manifoldMesh.triVerts[i + 0]; } const vertexCount = manifoldMesh.vertProperties.length / manifoldMesh.numProp; @@ -301,22 +301,29 @@ export class CSG2 implements IDisposable { const manifoldMesh = new ManifoldMesh({ numProp: numProp, vertProperties, triVerts, runIndex, runOriginalID }); manifoldMesh.merge(); + let returnValue: CSG2; try { - return new CSG2(new Manifold(manifoldMesh), numProp, structure); + returnValue = new CSG2(new Manifold(manifoldMesh), numProp, structure); } catch (e) { throw new Error("Error while creating the CSG: " + e.message); } + + if (returnValue._manifold.genus() < 0) { + throw new Error("Incorrect volume detected. Make sure you are not using a double sided geometry"); + } + + return returnValue; } - private static _Construct(data: IVertexDataLike, worldMatrix: Nullable, runIndex?: Uint32Array, runOriginalID?: Uint32Array) { + private static _Construct(data: IVertexDataLike, worldMatrix: Nullable, revertIndices: boolean, runIndex?: Uint32Array, runOriginalID?: Uint32Array) { // Create the MeshGL for I/O with Manifold library. const triVerts = new Uint32Array(data.indices!.length); // Revert order for (let i = 0; i < data.indices!.length; i += 3) { - triVerts[i] = data.indices![i + 2]; + triVerts[i] = data.indices![i + (revertIndices ? 2 : 0)]; triVerts[i + 1] = data.indices![i + 1]; - triVerts[i + 2] = data.indices![i]; + triVerts[i + 2] = data.indices![i + (revertIndices ? 0 : 2)]; } const tempVector3 = new Vector3(); @@ -383,7 +390,7 @@ export class CSG2 implements IDisposable { throw new Error("The vertexData must at least have positions and indices"); } - return this._Construct(vertexData, null); + return this._Construct(vertexData, null, false); } /** @@ -434,7 +441,7 @@ export class CSG2 implements IDisposable { uvs5: mesh.getVerticesData(VertexBuffer.UV5Kind), uvs6: mesh.getVerticesData(VertexBuffer.UV6Kind), }; - return this._Construct(data, ignoreWorldMatrix ? null : worldMatrix, runIndex, runOriginalID); + return this._Construct(data, ignoreWorldMatrix ? null : worldMatrix, worldMatrix.determinant() >= 0, runIndex, runOriginalID); } } From 7c01f32f56506afad34c801bdff4ac8346950f10 Mon Sep 17 00:00:00 2001 From: Deltakosh Date: Thu, 24 Oct 2024 10:06:08 -0700 Subject: [PATCH 2/4] revert unwanted changes --- packages/dev/core/src/Meshes/csg2.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/dev/core/src/Meshes/csg2.ts b/packages/dev/core/src/Meshes/csg2.ts index a3dac59e280..90000336acb 100644 --- a/packages/dev/core/src/Meshes/csg2.ts +++ b/packages/dev/core/src/Meshes/csg2.ts @@ -315,15 +315,15 @@ export class CSG2 implements IDisposable { return returnValue; } - private static _Construct(data: IVertexDataLike, worldMatrix: Nullable, revertIndices: boolean, runIndex?: Uint32Array, runOriginalID?: Uint32Array) { + private static _Construct(data: IVertexDataLike, worldMatrix: Nullable, runIndex?: Uint32Array, runOriginalID?: Uint32Array) { // Create the MeshGL for I/O with Manifold library. const triVerts = new Uint32Array(data.indices!.length); // Revert order for (let i = 0; i < data.indices!.length; i += 3) { - triVerts[i] = data.indices![i + (revertIndices ? 2 : 0)]; + triVerts[i] = data.indices![i + 2]; triVerts[i + 1] = data.indices![i + 1]; - triVerts[i + 2] = data.indices![i + (revertIndices ? 0 : 2)]; + triVerts[i + 2] = data.indices![i + 0]; } const tempVector3 = new Vector3(); @@ -390,7 +390,7 @@ export class CSG2 implements IDisposable { throw new Error("The vertexData must at least have positions and indices"); } - return this._Construct(vertexData, null, false); + return this._Construct(vertexData, null); } /** @@ -441,7 +441,7 @@ export class CSG2 implements IDisposable { uvs5: mesh.getVerticesData(VertexBuffer.UV5Kind), uvs6: mesh.getVerticesData(VertexBuffer.UV6Kind), }; - return this._Construct(data, ignoreWorldMatrix ? null : worldMatrix, worldMatrix.determinant() >= 0, runIndex, runOriginalID); + return this._Construct(data, ignoreWorldMatrix ? null : worldMatrix, runIndex, runOriginalID); } } From 681e0b7df305820b537b18ba7e5c6300f09a1aa1 Mon Sep 17 00:00:00 2001 From: Deltakosh Date: Thu, 24 Oct 2024 10:06:45 -0700 Subject: [PATCH 3/4] . --- packages/dev/core/src/Meshes/csg2.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dev/core/src/Meshes/csg2.ts b/packages/dev/core/src/Meshes/csg2.ts index 90000336acb..d8afe8f8b35 100644 --- a/packages/dev/core/src/Meshes/csg2.ts +++ b/packages/dev/core/src/Meshes/csg2.ts @@ -323,7 +323,7 @@ export class CSG2 implements IDisposable { for (let i = 0; i < data.indices!.length; i += 3) { triVerts[i] = data.indices![i + 2]; triVerts[i + 1] = data.indices![i + 1]; - triVerts[i + 2] = data.indices![i + 0]; + triVerts[i + 2] = data.indices![i]; } const tempVector3 = new Vector3(); From e8a22b875ab9eee3af296a743ec2c0a0609173cc Mon Sep 17 00:00:00 2001 From: Deltakosh Date: Thu, 24 Oct 2024 10:07:06 -0700 Subject: [PATCH 4/4] . --- packages/dev/core/src/Meshes/csg2.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dev/core/src/Meshes/csg2.ts b/packages/dev/core/src/Meshes/csg2.ts index d8afe8f8b35..26321bba678 100644 --- a/packages/dev/core/src/Meshes/csg2.ts +++ b/packages/dev/core/src/Meshes/csg2.ts @@ -180,7 +180,7 @@ export class CSG2 implements IDisposable { for (let i = 0; i < manifoldMesh.triVerts.length; i += 3) { vertexData.indices[i] = manifoldMesh.triVerts[i + 2]; vertexData.indices[i + 1] = manifoldMesh.triVerts[i + 1]; - vertexData.indices[i + 2] = manifoldMesh.triVerts[i + 0]; + vertexData.indices[i + 2] = manifoldMesh.triVerts[i]; } const vertexCount = manifoldMesh.vertProperties.length / manifoldMesh.numProp;