@bitbybit-dev/manifold 0.20.2 → 0.20.3

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c)2025 Bit By Bit Developers
3
+ Copyright (c) 2025 Bit By Bit Developers
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -17,6 +17,14 @@ export declare namespace Base {
17
17
  origin: Base.Point2;
18
18
  direction: Base.Vector2;
19
19
  };
20
+ type Segment2 = [Point2, Point2];
21
+ type Segment3 = [Point3, Point3];
22
+ type TrianglePlane3 = {
23
+ normal: Vector3;
24
+ d: number;
25
+ };
26
+ type Triangle3 = [Base.Point3, Base.Point3, Base.Point3];
27
+ type Mesh3 = Triangle3[];
20
28
  type Plane3 = {
21
29
  origin: Base.Point3;
22
30
  normal: Base.Vector3;
@@ -148,6 +148,13 @@ export declare namespace Manifold {
148
148
  */
149
149
  mesh: DecomposedManifoldMeshDto;
150
150
  }
151
+ class FromPolygonPointsDto {
152
+ constructor(polygonPoints?: Base.Point3[][]);
153
+ /**
154
+ * Points describing polygons
155
+ */
156
+ polygonPoints?: Base.Point3[][];
157
+ }
151
158
  class CubeDto {
152
159
  constructor(center?: boolean, size?: number);
153
160
  /**
@@ -161,6 +161,14 @@ export var Manifold;
161
161
  }
162
162
  }
163
163
  Manifold.CreateFromMeshDto = CreateFromMeshDto;
164
+ class FromPolygonPointsDto {
165
+ constructor(polygonPoints) {
166
+ if (polygonPoints !== undefined) {
167
+ this.polygonPoints = polygonPoints;
168
+ }
169
+ }
170
+ }
171
+ Manifold.FromPolygonPointsDto = FromPolygonPointsDto;
164
172
  class CubeDto {
165
173
  constructor(center, size) {
166
174
  /**
@@ -9,6 +9,7 @@ export declare class ManifoldService {
9
9
  manifold: Manifold;
10
10
  mesh: Mesh;
11
11
  constructor(wasm: Manifold3D.ManifoldToplevel);
12
+ toPolygonPoints(inputs: Inputs.Manifold.ManifoldDto<Manifold3D.Manifold>): Inputs.Base.Mesh3;
12
13
  decomposeManifoldOrCrossSection(inputs: Inputs.Manifold.DecomposeManifoldOrCrossSectionDto<Manifold3D.Manifold | Manifold3D.CrossSection>): Manifold3D.Mesh | Manifold3D.SimplePolygon[];
13
14
  decomposeManifoldsOrCrossSections(inputs: Inputs.Manifold.DecomposeManifoldsOrCrossSectionsDto<Manifold3D.Manifold | Manifold3D.CrossSection>): (Manifold3D.Mesh | Manifold3D.SimplePolygon[])[];
14
15
  }
@@ -8,6 +8,53 @@ export class ManifoldService {
8
8
  this.manifold = new Manifold(wasm);
9
9
  this.mesh = new Mesh(wasm);
10
10
  }
11
+ toPolygonPoints(inputs) {
12
+ // Ensure the manifold is decomposed to access the mesh data.
13
+ // The getMesh() method provides the necessary structure.
14
+ if (inputs.manifold.getMesh) {
15
+ const mesh = inputs.manifold.getMesh();
16
+ if (!mesh || !mesh.vertProperties || !mesh.triVerts || mesh.vertProperties.length === 0 || mesh.triVerts.length === 0) {
17
+ console.warn("Manifold does not contain valid mesh data or is empty.");
18
+ return [];
19
+ }
20
+ const vertProperties = mesh.vertProperties; // Float32Array [x1, y1, z1, [prop4...], x2, y2, z2, [prop4...]...]
21
+ const triVerts = mesh.triVerts; // Uint32Array [idx1, idx2, idx3, idx4, idx5, idx6, ...]
22
+ const numProp = mesh.numProp; // Number of properties per vertex
23
+ // We need at least x, y, z data.
24
+ if (numProp < 3) {
25
+ throw new Error(`Cannot convert to PolygonPoints: Expected numProp >= 3 (for x, y, z), but found ${numProp}.`);
26
+ }
27
+ if (triVerts.length % 3 !== 0) {
28
+ throw new Error(`Mesh data corruption: triVerts length (${triVerts.length}) is not a multiple of 3.`);
29
+ }
30
+ const polygons = [];
31
+ const numVertices = vertProperties.length / numProp;
32
+ for (let i = 0; i < triVerts.length; i += 3) {
33
+ const index1 = triVerts[i];
34
+ const index2 = triVerts[i + 1];
35
+ const index3 = triVerts[i + 2];
36
+ if (index1 >= numVertices || index2 >= numVertices || index3 >= numVertices) {
37
+ console.error(`Invalid vertex index found in triVerts at offset ${i}. Max index should be ${numVertices - 1}. Indices: ${index1}, ${index2}, ${index3}`);
38
+ continue; // Skip this triangle
39
+ }
40
+ // Calculate the starting position of each vertex's data in vertProperties.
41
+ // This calculation works regardless of numProp.
42
+ const vert1Offset = index1 * numProp;
43
+ const vert2Offset = index2 * numProp;
44
+ const vert3Offset = index3 * numProp;
45
+ // Extract only the first 3 properties (x, y, z) starting from the offset for each vertex.
46
+ const point1 = [vertProperties[vert1Offset], vertProperties[vert1Offset + 1], vertProperties[vert1Offset + 2]];
47
+ const point2 = [vertProperties[vert2Offset], vertProperties[vert2Offset + 1], vertProperties[vert2Offset + 2]];
48
+ const point3 = [vertProperties[vert3Offset], vertProperties[vert3Offset + 1], vertProperties[vert3Offset + 2]];
49
+ const triangle = [point1, point2, point3];
50
+ polygons.push(triangle);
51
+ }
52
+ return polygons;
53
+ }
54
+ else {
55
+ return undefined;
56
+ }
57
+ }
11
58
  decomposeManifoldOrCrossSection(inputs) {
12
59
  if (inputs.manifoldOrCrossSection.getMesh) {
13
60
  return inputs.manifoldOrCrossSection.getMesh(inputs.normalIdx);
@@ -4,6 +4,7 @@ export declare class ManifoldShapes {
4
4
  private manifold;
5
5
  constructor(wasm: Manifold3D.ManifoldToplevel);
6
6
  manifoldFromMesh(inputs: Inputs.Manifold.CreateFromMeshDto): Manifold3D.Manifold;
7
+ fromPolygonPoints(inputs: Inputs.Manifold.FromPolygonPointsDto): Manifold3D.Manifold;
7
8
  cube(inputs: Inputs.Manifold.CubeDto): Manifold3D.Manifold;
8
9
  sphere(inputs: Inputs.Manifold.SphereDto): Manifold3D.Manifold;
9
10
  tetrahedron(): Manifold3D.Manifold;
@@ -1,3 +1,4 @@
1
+ import * as Inputs from "../../inputs/manifold-inputs";
1
2
  export class ManifoldShapes {
2
3
  constructor(wasm) {
3
4
  this.manifold = wasm;
@@ -6,6 +7,64 @@ export class ManifoldShapes {
6
7
  const { Manifold } = this.manifold;
7
8
  return new Manifold(inputs.mesh);
8
9
  }
10
+ fromPolygonPoints(inputs) {
11
+ const { Manifold } = this.manifold;
12
+ const polygons = inputs.polygonPoints;
13
+ // Map to store unique vertices and their assigned index.
14
+ // Key: string representation "x,y,z"
15
+ // Value: index in the unique vertex list
16
+ const vertexMap = new Map();
17
+ const uniqueVertexCoords = [];
18
+ const triangleIndices = [];
19
+ let vertexIndexCounter = 0;
20
+ // --- Iterate, Deduplicate, and Store Unique Vertices/Indices ---
21
+ for (const triangle of polygons) {
22
+ if (!triangle || triangle.length !== 3) {
23
+ console.warn(`Skipping invalid polygon data (expected 3 vertices): ${JSON.stringify(triangle)}`);
24
+ continue; // Skip malformed triangles
25
+ }
26
+ for (const point of triangle) {
27
+ if (!point || point.length !== 3 || point.some(isNaN)) {
28
+ // Handle potential malformed point data more robustly
29
+ console.warn(`Skipping invalid point data in triangle: ${JSON.stringify(point)}`);
30
+ // Let's throw an error for clearer failure:
31
+ throw new Error(`Invalid point data encountered: ${JSON.stringify(point)} in triangle ${JSON.stringify(triangle)}`);
32
+ }
33
+ // Create a unique key for the vertex based on its coordinates
34
+ // Using string concatenation is simple for exact matches.
35
+ // Be aware of potential floating-point precision issues if vertices
36
+ // might be *very* slightly different but should be treated as the same.
37
+ const vertexKey = `${point[0]},${point[1]},${point[2]}`;
38
+ let index;
39
+ // Check if this vertex has already been seen
40
+ if (vertexMap.has(vertexKey)) {
41
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
42
+ index = vertexMap.get(vertexKey);
43
+ }
44
+ else {
45
+ // It's a new unique vertex
46
+ index = vertexIndexCounter;
47
+ vertexMap.set(vertexKey, index);
48
+ uniqueVertexCoords.push(point[0], point[1], point[2]);
49
+ vertexIndexCounter++;
50
+ }
51
+ // Add the index (either existing or new) to the triangle indices list
52
+ triangleIndices.push(index);
53
+ }
54
+ }
55
+ // --- Create TypedArrays ---
56
+ // Number of properties per vertex (x, y, z)
57
+ // If we had normals, UVs etc., this would be higher, we're not dealing with that here when making meshes from simple polygon points.
58
+ const numProp = 3;
59
+ const vertProperties = Float32Array.from(uniqueVertexCoords);
60
+ const triVerts = Uint32Array.from(triangleIndices);
61
+ // --- Populate the DTO ---
62
+ const meshDto = new Inputs.Manifold.DecomposedManifoldMeshDto();
63
+ meshDto.numProp = numProp;
64
+ meshDto.vertProperties = vertProperties;
65
+ meshDto.triVerts = triVerts;
66
+ return new Manifold(meshDto);
67
+ }
9
68
  cube(inputs) {
10
69
  const { Manifold } = this.manifold;
11
70
  const { cube } = Manifold;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bitbybit-dev/manifold",
3
- "version": "0.20.2",
3
+ "version": "0.20.3",
4
4
  "description": "Bit By Bit Developers Manifold based CAD Library to Program Geometry",
5
5
  "main": "index.js",
6
6
  "repository": {