@bitbybit-dev/jscad 0.20.2 → 0.20.4
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
|
@@ -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;
|
|
@@ -47,6 +47,20 @@ export declare namespace JSCAD {
|
|
|
47
47
|
*/
|
|
48
48
|
right = "right"
|
|
49
49
|
}
|
|
50
|
+
class MeshDto {
|
|
51
|
+
constructor(mesh?: JSCADEntity);
|
|
52
|
+
/**
|
|
53
|
+
* Solid Jscad mesh
|
|
54
|
+
*/
|
|
55
|
+
mesh: JSCADEntity;
|
|
56
|
+
}
|
|
57
|
+
class MeshesDto {
|
|
58
|
+
constructor(meshes?: JSCADEntity[]);
|
|
59
|
+
/**
|
|
60
|
+
* Solid Jscad mesh
|
|
61
|
+
*/
|
|
62
|
+
meshes: JSCADEntity[];
|
|
63
|
+
}
|
|
50
64
|
class DrawSolidMeshDto<T> {
|
|
51
65
|
/**
|
|
52
66
|
* Provide options without default values
|
|
@@ -49,6 +49,22 @@ export var JSCAD;
|
|
|
49
49
|
*/
|
|
50
50
|
jscadTextAlignEnum["right"] = "right";
|
|
51
51
|
})(jscadTextAlignEnum = JSCAD.jscadTextAlignEnum || (JSCAD.jscadTextAlignEnum = {}));
|
|
52
|
+
class MeshDto {
|
|
53
|
+
constructor(mesh) {
|
|
54
|
+
if (mesh !== undefined) {
|
|
55
|
+
this.mesh = mesh;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
JSCAD.MeshDto = MeshDto;
|
|
60
|
+
class MeshesDto {
|
|
61
|
+
constructor(meshes) {
|
|
62
|
+
if (meshes !== undefined) {
|
|
63
|
+
this.meshes = meshes;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
JSCAD.MeshesDto = MeshesDto;
|
|
52
68
|
class DrawSolidMeshDto {
|
|
53
69
|
/**
|
|
54
70
|
* Provide options without default values
|
|
@@ -6,6 +6,7 @@ import { JSCADPolygon } from "./services/jscad-polygon";
|
|
|
6
6
|
import { JSCADShapes } from "./services/jscad-shapes";
|
|
7
7
|
import { JSCADText } from "./services/jscad-text";
|
|
8
8
|
import * as Inputs from "./inputs/jscad-inputs";
|
|
9
|
+
import { Base } from "./inputs/base-inputs";
|
|
9
10
|
import { JSCADHulls } from "./services/jscad-hulls";
|
|
10
11
|
import { JSCADColors } from "./services/jscad-colors";
|
|
11
12
|
import * as JSCAD from "@jscad/modeling";
|
|
@@ -20,14 +21,16 @@ export declare class Jscad {
|
|
|
20
21
|
shapes: JSCADShapes;
|
|
21
22
|
text: JSCADText;
|
|
22
23
|
colors: JSCADColors;
|
|
24
|
+
private point;
|
|
23
25
|
constructor(jscad: typeof JSCAD);
|
|
24
|
-
|
|
26
|
+
toPolygonPoints(inputs: Inputs.JSCAD.MeshDto): Base.Mesh3;
|
|
27
|
+
shapesToMeshes(inputs: Inputs.JSCAD.MeshesDto): {
|
|
25
28
|
positions: number[];
|
|
26
29
|
normals: number[];
|
|
27
30
|
indices: number[];
|
|
28
31
|
transforms: [];
|
|
29
32
|
}[];
|
|
30
|
-
shapeToMesh
|
|
33
|
+
shapeToMesh(inputs: Inputs.JSCAD.MeshDto): {
|
|
31
34
|
positions: number[];
|
|
32
35
|
normals: number[];
|
|
33
36
|
indices: number[];
|
package/lib/api/jscad-service.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GeometryHelper } from "@bitbybit-dev/base";
|
|
1
|
+
import { GeometryHelper, Lists, Point, Transforms, Vector } from "@bitbybit-dev/base";
|
|
2
2
|
import { MathBitByBit } from "@bitbybit-dev/base";
|
|
3
3
|
import { JSCADExpansions } from "./services/jscad-expansions";
|
|
4
4
|
import { JSCADBooleans } from "./services/jscad-booleans";
|
|
@@ -19,6 +19,10 @@ export class Jscad {
|
|
|
19
19
|
};
|
|
20
20
|
const geometryHelper = new GeometryHelper();
|
|
21
21
|
const math = new MathBitByBit();
|
|
22
|
+
const vector = new Vector(math, geometryHelper);
|
|
23
|
+
const transforms = new Transforms(vector, math);
|
|
24
|
+
const lists = new Lists();
|
|
25
|
+
this.point = new Point(geometryHelper, transforms, vector, lists);
|
|
22
26
|
this.booleans = new JSCADBooleans(jscad);
|
|
23
27
|
this.expansions = new JSCADExpansions(jscad);
|
|
24
28
|
this.extrusions = new JSCADExtrusions(jscad, geometryHelper, math);
|
|
@@ -30,6 +34,65 @@ export class Jscad {
|
|
|
30
34
|
this.colors = new JSCADColors(jscad);
|
|
31
35
|
this.jscad = jscad;
|
|
32
36
|
}
|
|
37
|
+
toPolygonPoints(inputs) {
|
|
38
|
+
const meshData = this.shapeToMesh({ mesh: inputs.mesh });
|
|
39
|
+
if (!meshData || !meshData.positions || !meshData.indices) {
|
|
40
|
+
throw new Error("Invalid input: 'data', 'data.positions', and 'data.indices' must be provided.");
|
|
41
|
+
}
|
|
42
|
+
const { positions, indices } = meshData;
|
|
43
|
+
if (positions.length % 3 !== 0) {
|
|
44
|
+
throw new Error(`Invalid input: 'positions' array length (${positions.length}) must be a multiple of 3.`);
|
|
45
|
+
}
|
|
46
|
+
if (indices.length % 3 !== 0) {
|
|
47
|
+
throw new Error(`Invalid input: 'indices' array length (${indices.length}) must be a multiple of 3.`);
|
|
48
|
+
}
|
|
49
|
+
if (positions.length === 0) {
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
if (indices.length === 0) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
const polygons = [];
|
|
56
|
+
const numVertices = positions.length / 3;
|
|
57
|
+
// --- Triangle Reconstruction ---
|
|
58
|
+
for (let i = 0; i < indices.length; i += 3) {
|
|
59
|
+
const index1 = indices[i];
|
|
60
|
+
const index2 = indices[i + 1];
|
|
61
|
+
const index3 = indices[i + 2];
|
|
62
|
+
if (index1 >= numVertices || index2 >= numVertices || index3 >= numVertices ||
|
|
63
|
+
index1 < 0 || index2 < 0 || index3 < 0) {
|
|
64
|
+
console.error(`Invalid vertex index found in 'indices' array at triangle starting at index ${i}. Max vertex index is ${numVertices - 1}. Indices: ${index1}, ${index2}, ${index3}. Skipping triangle.`);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const offset1 = index1 * 3;
|
|
68
|
+
const offset2 = index2 * 3;
|
|
69
|
+
const offset3 = index3 * 3;
|
|
70
|
+
const point1 = [positions[offset1], positions[offset1 + 1], positions[offset1 + 2]];
|
|
71
|
+
const point2 = [positions[offset2], positions[offset2 + 1], positions[offset2 + 2]];
|
|
72
|
+
const point3 = [positions[offset3], positions[offset3 + 1], positions[offset3 + 2]];
|
|
73
|
+
// We must bake the transformations as JSCAD uses those extensively
|
|
74
|
+
const transformation = inputs.mesh.transforms;
|
|
75
|
+
let transformedPoints = [point1, point2, point3];
|
|
76
|
+
if (this.getArrayDepth(transformation) === 2) {
|
|
77
|
+
transformation.forEach(transform => {
|
|
78
|
+
transformedPoints = this.point.transformPoints({ points: transformedPoints, transformation: [transform] });
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
else if (this.getArrayDepth(transformation) === 3) {
|
|
82
|
+
transformation.forEach(transforms => {
|
|
83
|
+
transforms.forEach(mat => {
|
|
84
|
+
transformedPoints = this.point.transformPoints({ points: transformedPoints, transformation: [mat] });
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
transformedPoints = this.point.transformPoints({ points: transformedPoints, transformation: [transformation] });
|
|
90
|
+
}
|
|
91
|
+
const triangle = transformedPoints;
|
|
92
|
+
polygons.push(triangle);
|
|
93
|
+
}
|
|
94
|
+
return polygons;
|
|
95
|
+
}
|
|
33
96
|
shapesToMeshes(inputs) {
|
|
34
97
|
return inputs.meshes.map(mesh => {
|
|
35
98
|
return this.shapeToMesh(Object.assign(Object.assign({}, inputs), { mesh }));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bitbybit-dev/jscad",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.4",
|
|
4
4
|
"description": "Bit By Bit Developers JSCAD based CAD Library to Program Geometry",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"types": "./index.d.ts",
|
|
59
59
|
"type": "module",
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@bitbybit-dev/base": "0.20.
|
|
61
|
+
"@bitbybit-dev/base": "0.20.4",
|
|
62
62
|
"@jscad/io-utils": "2.0.28",
|
|
63
63
|
"@jscad/modeling": "2.12.3",
|
|
64
64
|
"@jscad/stl-serializer": "2.1.18",
|