@bitbybit-dev/base 1.4.1 → 1.5.0
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/lib/api/GlobalCDNProvider.js +1 -1
- package/lib/api/color-space.d.ts +14 -0
- package/lib/api/color-space.js +24 -0
- package/lib/api/index.d.ts +2 -0
- package/lib/api/index.js +2 -0
- package/lib/api/inputs/base-inputs.d.ts +382 -5
- package/lib/api/inputs/base-inputs.js +0 -3
- package/lib/api/inputs/color-inputs.d.ts +1 -1
- package/lib/api/inputs/color-inputs.js +0 -1
- package/lib/api/inputs/dates-inputs.js +0 -1
- package/lib/api/inputs/frame-inputs.d.ts +1 -1
- package/lib/api/inputs/inputs.js +0 -1
- package/lib/api/inputs/io-inputs.d.ts +1 -1
- package/lib/api/inputs/line-inputs.d.ts +1 -1
- package/lib/api/inputs/lists-inputs.js +0 -1
- package/lib/api/inputs/logic-inputs.d.ts +1 -1
- package/lib/api/inputs/logic-inputs.js +0 -1
- package/lib/api/inputs/math-inputs.js +0 -1
- package/lib/api/inputs/mesh-inputs.d.ts +29 -1
- package/lib/api/inputs/mesh-inputs.js +25 -1
- package/lib/api/inputs/point-inputs.d.ts +30 -1
- package/lib/api/inputs/point-inputs.js +28 -0
- package/lib/api/inputs/polyline-inputs.d.ts +1 -1
- package/lib/api/inputs/transforms-inputs.d.ts +1 -1
- package/lib/api/inputs/vector-inputs.d.ts +1 -1
- package/lib/api/kernel-calls/describe-failure.d.ts +3 -2
- package/lib/api/kernel-calls/describe-failure.js +9 -17
- package/lib/api/kernel-calls/failure-message.d.ts +1 -1
- package/lib/api/kernel-calls/hashing.d.ts +14 -0
- package/lib/api/kernel-calls/hashing.js +47 -0
- package/lib/api/kernel-calls/index.d.ts +3 -0
- package/lib/api/kernel-calls/index.js +3 -0
- package/lib/api/kernel-calls/input-rules.d.ts +1 -1
- package/lib/api/kernel-calls/input-rules.js +10 -5
- package/lib/api/kernel-calls/input-validation.d.ts +3 -3
- package/lib/api/kernel-calls/input-validation.js +24 -14
- package/lib/api/kernel-calls/kernel-steps.d.ts +19 -0
- package/lib/api/kernel-calls/kernel-steps.js +19 -0
- package/lib/api/kernel-calls/resolve-dto.d.ts +5 -3
- package/lib/api/kernel-calls/resolve-dto.js +9 -4
- package/lib/api/kernel-calls/unknown-values.d.ts +12 -0
- package/lib/api/kernel-calls/unknown-values.js +16 -0
- package/lib/api/kernel-calls/with-defaults.d.ts +1 -1
- package/lib/api/models/point/hex-grid-data.d.ts +1 -1
- package/lib/api/models/text/vector-char-data.d.ts +1 -1
- package/lib/api/models/text/vector-text-data.d.ts +1 -1
- package/lib/api/recipes/checker-types.d.ts +14 -0
- package/lib/api/recipes/checker-types.js +1 -0
- package/lib/api/recipes/index.d.ts +4 -0
- package/lib/api/recipes/index.js +3 -0
- package/lib/api/recipes/limits.constants.d.ts +23 -0
- package/lib/api/recipes/limits.constants.js +23 -0
- package/lib/api/recipes/recipe-types.d.ts +48 -0
- package/lib/api/recipes/recipe-types.js +1 -0
- package/lib/api/recipes/sources.d.ts +16 -0
- package/lib/api/recipes/sources.js +23 -0
- package/lib/api/recipes/surface-mesh.d.ts +16 -0
- package/lib/api/recipes/surface-mesh.js +86 -0
- package/lib/api/recipes/validate.d.ts +36 -0
- package/lib/api/recipes/validate.js +427 -0
- package/lib/api/resolved-inputs/index.d.ts +3 -0
- package/lib/api/services/color.d.ts +1 -1
- package/lib/api/services/frame.d.ts +3 -95
- package/lib/api/services/frame.js +24 -135
- package/lib/api/services/geometry-helper.d.ts +1 -1
- package/lib/api/services/helpers/arcs.d.ts +6 -0
- package/lib/api/services/helpers/arcs.js +30 -0
- package/lib/api/services/helpers/crc32.d.ts +1 -0
- package/lib/api/services/helpers/crc32.js +21 -0
- package/lib/api/services/helpers/dxf/dxf-generator.d.ts +0 -83
- package/lib/api/services/helpers/dxf/dxf-generator.js +5 -86
- package/lib/api/services/helpers/frame-axes.d.ts +9 -1
- package/lib/api/services/helpers/frame-axes.js +34 -0
- package/lib/api/services/helpers/helper-types.d.ts +34 -0
- package/lib/api/services/helpers/helper-types.js +1 -0
- package/lib/api/services/helpers/inflate.constants.d.ts +41 -0
- package/lib/api/services/helpers/inflate.constants.js +41 -0
- package/lib/api/services/helpers/inflate.d.ts +3 -0
- package/lib/api/services/helpers/inflate.js +228 -0
- package/lib/api/services/helpers/matrices.constants.d.ts +2 -0
- package/lib/api/services/helpers/matrices.constants.js +2 -0
- package/lib/api/services/helpers/matrices.d.ts +9 -1
- package/lib/api/services/helpers/matrices.js +18 -0
- package/lib/api/services/helpers/mesh-measures.d.ts +1 -0
- package/lib/api/services/helpers/mesh-measures.js +28 -0
- package/lib/api/services/helpers/mesh-normals.d.ts +2 -0
- package/lib/api/services/helpers/mesh-normals.js +118 -26
- package/lib/api/services/helpers/mesh-primitives.constants.d.ts +2 -0
- package/lib/api/services/helpers/mesh-primitives.constants.js +12 -0
- package/lib/api/services/helpers/mesh-primitives.d.ts +3 -0
- package/lib/api/services/helpers/mesh-primitives.js +46 -0
- package/lib/api/services/helpers/nurbs.d.ts +7 -0
- package/lib/api/services/helpers/nurbs.js +50 -0
- package/lib/api/services/helpers/paths.d.ts +8 -0
- package/lib/api/services/helpers/paths.js +96 -0
- package/lib/api/services/helpers/polygons.d.ts +8 -0
- package/lib/api/services/helpers/polygons.js +59 -0
- package/lib/api/services/helpers/surface-triangulation.d.ts +6 -0
- package/lib/api/services/helpers/surface-triangulation.js +298 -0
- package/lib/api/services/helpers/triangulation.d.ts +2 -0
- package/lib/api/services/helpers/triangulation.js +295 -0
- package/lib/api/services/helpers/vectors.d.ts +18 -0
- package/lib/api/services/helpers/vectors.js +17 -0
- package/lib/api/services/helpers/zip.constants.d.ts +34 -0
- package/lib/api/services/helpers/zip.constants.js +34 -0
- package/lib/api/services/helpers/zip.d.ts +4 -0
- package/lib/api/services/helpers/zip.js +89 -0
- package/lib/api/services/line.d.ts +3 -3
- package/lib/api/services/mesh.d.ts +37 -2
- package/lib/api/services/mesh.js +60 -3
- package/lib/api/services/point.d.ts +22 -10
- package/lib/api/services/point.js +42 -13
- package/lib/api/services/polyline.d.ts +39 -4
- package/lib/api/services/polyline.js +56 -6
- package/lib/api/services/text.d.ts +2 -2
- package/lib/api/services/text.js +10 -5
- package/lib/api/services/transforms.d.ts +3 -9
- package/lib/api/services/transforms.js +0 -6
- package/lib/api/services/vector.d.ts +2 -2
- package/lib/api/services/vector.js +2 -5
- package/package.json +1 -1
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import * as Inputs from "../../inputs";
|
|
1
|
+
import type * as Inputs from "../../inputs";
|
|
2
2
|
type Vec3 = Inputs.Base.Vector3;
|
|
3
3
|
/** Whether a value is one column-major 4 x 4 matrix: sixteen finite numbers. */
|
|
4
4
|
export declare const isTransformMatrix: (value: unknown) => value is Inputs.Base.TransformMatrix;
|
|
5
5
|
/** The product of two column-major 4 x 4 matrices: `first` applied, then `second`. */
|
|
6
6
|
export declare const followedBy: (first: readonly number[], second: readonly number[]) => Inputs.Base.TransformMatrix;
|
|
7
|
+
/** The determinant of a column-major 4 x 4 matrix's rotation and scale part: negative when it mirrors, near zero when it flattens. */
|
|
8
|
+
export declare const linearDeterminant: (matrix: readonly number[]) => number;
|
|
9
|
+
/**
|
|
10
|
+
* Whether a column-major 4 x 4 matrix only turns and moves: its rotation and scale part has columns
|
|
11
|
+
* of length 1 and at right angles, within `tolerance` on their squared lengths and dot products, and
|
|
12
|
+
* keeps its handedness.
|
|
13
|
+
*/
|
|
14
|
+
export declare const isRigidMatrix: (matrix: readonly number[], tolerance?: number) => boolean;
|
|
7
15
|
/** One column-major 4 x 4 matrix that applies a list of them first to last; the identity for an empty list. */
|
|
8
16
|
export declare const composed: (matrices: readonly (readonly number[])[]) => Inputs.Base.TransformMatrix;
|
|
9
17
|
/**
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { LINEAR_COLUMN_STARTS, RIGID_TOLERANCE } from "./matrices.constants";
|
|
2
|
+
import { dot3 } from "./vectors";
|
|
1
3
|
/** Whether a value is one column-major 4 x 4 matrix: sixteen finite numbers. */
|
|
2
4
|
export const isTransformMatrix = (value) => Array.isArray(value) && value.length === 16 && value.every(n => typeof n === "number" && Number.isFinite(n));
|
|
3
5
|
/** The product of two column-major 4 x 4 matrices: `first` applied, then `second`. */
|
|
@@ -10,6 +12,22 @@ export const followedBy = (first, second) => {
|
|
|
10
12
|
});
|
|
11
13
|
return product;
|
|
12
14
|
};
|
|
15
|
+
/** The determinant of a column-major 4 x 4 matrix's rotation and scale part: negative when it mirrors, near zero when it flattens. */
|
|
16
|
+
export const linearDeterminant = (matrix) => {
|
|
17
|
+
const [a, b, c, , d, e, f, , g, h, i] = matrix;
|
|
18
|
+
return a * (e * i - f * h) - d * (b * i - c * h) + g * (b * f - c * e);
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Whether a column-major 4 x 4 matrix only turns and moves: its rotation and scale part has columns
|
|
22
|
+
* of length 1 and at right angles, within `tolerance` on their squared lengths and dot products, and
|
|
23
|
+
* keeps its handedness.
|
|
24
|
+
*/
|
|
25
|
+
export const isRigidMatrix = (matrix, tolerance = RIGID_TOLERANCE) => {
|
|
26
|
+
const [x, y, z] = LINEAR_COLUMN_STARTS.map((start) => [matrix[start], matrix[start + 1], matrix[start + 2]]);
|
|
27
|
+
return [x, y, z].every((column) => Math.abs(dot3(column, column) - 1) <= tolerance)
|
|
28
|
+
&& Math.abs(dot3(x, y)) <= tolerance && Math.abs(dot3(x, z)) <= tolerance && Math.abs(dot3(y, z)) <= tolerance
|
|
29
|
+
&& linearDeterminant(matrix) > 0;
|
|
30
|
+
};
|
|
13
31
|
/** One column-major 4 x 4 matrix that applies a list of them first to last; the identity for an empty list. */
|
|
14
32
|
export const composed = (matrices) => matrices.reduce((total, matrix) => followedBy(total, matrix), [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
|
|
15
33
|
/**
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const signedVolumeOf: (positions: ArrayLike<number>, indices: ArrayLike<number>, start?: number, end?: number) => number;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const CORNERS = 3;
|
|
2
|
+
const TETRAHEDRA_PER_PARALLELEPIPED = 6;
|
|
3
|
+
export const signedVolumeOf = (positions, indices, start = 0, end = indices.length) => {
|
|
4
|
+
if (end <= start) {
|
|
5
|
+
return 0;
|
|
6
|
+
}
|
|
7
|
+
const origin = indices[start] * CORNERS;
|
|
8
|
+
const ox = positions[origin];
|
|
9
|
+
const oy = positions[origin + 1];
|
|
10
|
+
const oz = positions[origin + 2];
|
|
11
|
+
let sixfold = 0;
|
|
12
|
+
for (let at = start; at + 2 < end; at += CORNERS) {
|
|
13
|
+
const a = indices[at] * CORNERS;
|
|
14
|
+
const b = indices[at + 1] * CORNERS;
|
|
15
|
+
const c = indices[at + 2] * CORNERS;
|
|
16
|
+
const ax = positions[a] - ox;
|
|
17
|
+
const ay = positions[a + 1] - oy;
|
|
18
|
+
const az = positions[a + 2] - oz;
|
|
19
|
+
const bx = positions[b] - ox;
|
|
20
|
+
const by = positions[b + 1] - oy;
|
|
21
|
+
const bz = positions[b + 2] - oz;
|
|
22
|
+
const cx = positions[c] - ox;
|
|
23
|
+
const cy = positions[c + 1] - oy;
|
|
24
|
+
const cz = positions[c + 2] - oz;
|
|
25
|
+
sixfold += ax * (by * cz - bz * cy) - ay * (bx * cz - bz * cx) + az * (bx * cy - by * cx);
|
|
26
|
+
}
|
|
27
|
+
return sixfold / TETRAHEDRA_PER_PARALLELEPIPED;
|
|
28
|
+
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CreasedMesh } from "./helper-types";
|
|
1
2
|
/**
|
|
2
3
|
* Compute smooth vertex normals for a mesh that arrives without them.
|
|
3
4
|
* Accumulates each triangle's cross product onto its three vertices, then normalizes, so a vertex
|
|
@@ -7,3 +8,4 @@
|
|
|
7
8
|
* @returns Flat array of normals [nx,ny,nz,nx,ny,nz,...]
|
|
8
9
|
*/
|
|
9
10
|
export declare function computeVertexNormals(positions: number[], indices: number[]): number[];
|
|
11
|
+
export declare const creasedMesh: (positions: ArrayLike<number>, indices: ArrayLike<number>, minSharpAngle: number) => CreasedMesh;
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
const CORNERS = 3;
|
|
2
|
+
const COORDINATES = 3;
|
|
3
|
+
const X = 0;
|
|
4
|
+
const Y = 1;
|
|
5
|
+
const Z = 2;
|
|
6
|
+
const HALF_TURN_DEGREES = 180;
|
|
7
|
+
const DEGREES_TO_RADIANS = Math.PI / HALF_TURN_DEGREES;
|
|
8
|
+
const ANGLE_SLACK = 1e-9;
|
|
1
9
|
/**
|
|
2
10
|
* Compute smooth vertex normals for a mesh that arrives without them.
|
|
3
11
|
* Accumulates each triangle's cross product onto its three vertices, then normalizes, so a vertex
|
|
@@ -7,21 +15,21 @@
|
|
|
7
15
|
* @returns Flat array of normals [nx,ny,nz,nx,ny,nz,...]
|
|
8
16
|
*/
|
|
9
17
|
export function computeVertexNormals(positions, indices) {
|
|
10
|
-
const numVertices = positions.length /
|
|
18
|
+
const numVertices = positions.length / COORDINATES;
|
|
11
19
|
const normals = new Float32Array(positions.length);
|
|
12
|
-
for (let i = 0; i < indices.length; i +=
|
|
20
|
+
for (let i = 0; i < indices.length; i += CORNERS) {
|
|
13
21
|
const i0 = indices[i];
|
|
14
22
|
const i1 = indices[i + 1];
|
|
15
23
|
const i2 = indices[i + 2];
|
|
16
|
-
const v0x = positions[i0 *
|
|
17
|
-
const v0y = positions[i0 *
|
|
18
|
-
const v0z = positions[i0 *
|
|
19
|
-
const v1x = positions[i1 *
|
|
20
|
-
const v1y = positions[i1 *
|
|
21
|
-
const v1z = positions[i1 *
|
|
22
|
-
const v2x = positions[i2 *
|
|
23
|
-
const v2y = positions[i2 *
|
|
24
|
-
const v2z = positions[i2 *
|
|
24
|
+
const v0x = positions[i0 * COORDINATES + X];
|
|
25
|
+
const v0y = positions[i0 * COORDINATES + Y];
|
|
26
|
+
const v0z = positions[i0 * COORDINATES + Z];
|
|
27
|
+
const v1x = positions[i1 * COORDINATES + X];
|
|
28
|
+
const v1y = positions[i1 * COORDINATES + Y];
|
|
29
|
+
const v1z = positions[i1 * COORDINATES + Z];
|
|
30
|
+
const v2x = positions[i2 * COORDINATES + X];
|
|
31
|
+
const v2y = positions[i2 * COORDINATES + Y];
|
|
32
|
+
const v2z = positions[i2 * COORDINATES + Z];
|
|
25
33
|
const e1x = v1x - v0x;
|
|
26
34
|
const e1y = v1y - v0y;
|
|
27
35
|
const e1z = v1z - v0z;
|
|
@@ -31,26 +39,110 @@ export function computeVertexNormals(positions, indices) {
|
|
|
31
39
|
const nx = e1y * e2z - e1z * e2y;
|
|
32
40
|
const ny = e1z * e2x - e1x * e2z;
|
|
33
41
|
const nz = e1x * e2y - e1y * e2x;
|
|
34
|
-
normals[i0 *
|
|
35
|
-
normals[i0 *
|
|
36
|
-
normals[i0 *
|
|
37
|
-
normals[i1 *
|
|
38
|
-
normals[i1 *
|
|
39
|
-
normals[i1 *
|
|
40
|
-
normals[i2 *
|
|
41
|
-
normals[i2 *
|
|
42
|
-
normals[i2 *
|
|
42
|
+
normals[i0 * COORDINATES + X] += nx;
|
|
43
|
+
normals[i0 * COORDINATES + Y] += ny;
|
|
44
|
+
normals[i0 * COORDINATES + Z] += nz;
|
|
45
|
+
normals[i1 * COORDINATES + X] += nx;
|
|
46
|
+
normals[i1 * COORDINATES + Y] += ny;
|
|
47
|
+
normals[i1 * COORDINATES + Z] += nz;
|
|
48
|
+
normals[i2 * COORDINATES + X] += nx;
|
|
49
|
+
normals[i2 * COORDINATES + Y] += ny;
|
|
50
|
+
normals[i2 * COORDINATES + Z] += nz;
|
|
43
51
|
}
|
|
44
52
|
for (let i = 0; i < numVertices; i++) {
|
|
45
|
-
const x = normals[i *
|
|
46
|
-
const y = normals[i *
|
|
47
|
-
const z = normals[i *
|
|
53
|
+
const x = normals[i * COORDINATES + X];
|
|
54
|
+
const y = normals[i * COORDINATES + Y];
|
|
55
|
+
const z = normals[i * COORDINATES + Z];
|
|
48
56
|
const len = Math.sqrt(x * x + y * y + z * z);
|
|
49
57
|
if (len > 0) {
|
|
50
|
-
normals[i *
|
|
51
|
-
normals[i *
|
|
52
|
-
normals[i *
|
|
58
|
+
normals[i * COORDINATES + X] = x / len;
|
|
59
|
+
normals[i * COORDINATES + Y] = y / len;
|
|
60
|
+
normals[i * COORDINATES + Z] = z / len;
|
|
53
61
|
}
|
|
54
62
|
}
|
|
55
63
|
return Array.from(normals);
|
|
56
64
|
}
|
|
65
|
+
const faceNormalsOf = (positions, indices) => {
|
|
66
|
+
const normals = new Float64Array(indices.length);
|
|
67
|
+
for (let at = 0; at + CORNERS - 1 < indices.length; at += CORNERS) {
|
|
68
|
+
const a = indices[at] * COORDINATES;
|
|
69
|
+
const b = indices[at + 1] * COORDINATES;
|
|
70
|
+
const c = indices[at + 2] * COORDINATES;
|
|
71
|
+
const ux = positions[b] - positions[a];
|
|
72
|
+
const uy = positions[b + Y] - positions[a + Y];
|
|
73
|
+
const uz = positions[b + Z] - positions[a + Z];
|
|
74
|
+
const vx = positions[c] - positions[a];
|
|
75
|
+
const vy = positions[c + Y] - positions[a + Y];
|
|
76
|
+
const vz = positions[c + Z] - positions[a + Z];
|
|
77
|
+
normals[at] = uy * vz - uz * vy;
|
|
78
|
+
normals[at + Y] = uz * vx - ux * vz;
|
|
79
|
+
normals[at + Z] = ux * vy - uy * vx;
|
|
80
|
+
}
|
|
81
|
+
return normals;
|
|
82
|
+
};
|
|
83
|
+
const facesAround = (indices, vertexCount) => {
|
|
84
|
+
const starts = new Int32Array(vertexCount + 1);
|
|
85
|
+
for (let at = 0; at < indices.length; at++) {
|
|
86
|
+
starts[indices[at] + 1]++;
|
|
87
|
+
}
|
|
88
|
+
for (let vertex = 0; vertex < vertexCount; vertex++) {
|
|
89
|
+
starts[vertex + 1] += starts[vertex];
|
|
90
|
+
}
|
|
91
|
+
const filled = starts.slice(0, vertexCount);
|
|
92
|
+
const faces = new Int32Array(indices.length);
|
|
93
|
+
for (let at = 0; at < indices.length; at++) {
|
|
94
|
+
faces[filled[indices[at]]++] = Math.floor(at / CORNERS);
|
|
95
|
+
}
|
|
96
|
+
return [starts, faces];
|
|
97
|
+
};
|
|
98
|
+
export const creasedMesh = (positions, indices, minSharpAngle) => {
|
|
99
|
+
const vertexCount = Math.floor(positions.length / COORDINATES);
|
|
100
|
+
const triangleCount = Math.floor(indices.length / CORNERS);
|
|
101
|
+
const weighted = faceNormalsOf(positions, indices);
|
|
102
|
+
const lengths = new Float64Array(triangleCount);
|
|
103
|
+
for (let face = 0; face < triangleCount; face++) {
|
|
104
|
+
lengths[face] = Math.hypot(weighted[face * COORDINATES], weighted[face * COORDINATES + Y], weighted[face * COORDINATES + Z]);
|
|
105
|
+
}
|
|
106
|
+
const [starts, faces] = facesAround(indices, vertexCount);
|
|
107
|
+
const cosine = Math.cos(minSharpAngle * DEGREES_TO_RADIANS) - ANGLE_SLACK;
|
|
108
|
+
const outPositions = [];
|
|
109
|
+
const outNormals = [];
|
|
110
|
+
const outIndices = new Uint32Array(triangleCount * CORNERS);
|
|
111
|
+
const made = Array.from({ length: vertexCount }, () => []);
|
|
112
|
+
for (let corner = 0; corner < triangleCount * CORNERS; corner++) {
|
|
113
|
+
const face = Math.floor(corner / CORNERS);
|
|
114
|
+
const vertex = indices[corner];
|
|
115
|
+
let nx = 0;
|
|
116
|
+
let ny = 0;
|
|
117
|
+
let nz = 0;
|
|
118
|
+
for (let around = starts[vertex]; around < starts[vertex + 1]; around++) {
|
|
119
|
+
const other = faces[around];
|
|
120
|
+
const product = lengths[face] > 0 && lengths[other] > 0
|
|
121
|
+
? (weighted[face * COORDINATES] * weighted[other * COORDINATES] + weighted[face * COORDINATES + Y] * weighted[other * COORDINATES + Y] + weighted[face * COORDINATES + Z] * weighted[other * COORDINATES + Z]) / (lengths[face] * lengths[other])
|
|
122
|
+
: other === face ? 1 : 0;
|
|
123
|
+
if (product >= cosine) {
|
|
124
|
+
nx += weighted[other * COORDINATES];
|
|
125
|
+
ny += weighted[other * COORDINATES + Y];
|
|
126
|
+
nz += weighted[other * COORDINATES + Z];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const length = Math.hypot(nx, ny, nz);
|
|
130
|
+
if (length > 0) {
|
|
131
|
+
nx /= length;
|
|
132
|
+
ny /= length;
|
|
133
|
+
nz /= length;
|
|
134
|
+
}
|
|
135
|
+
const reuse = made[vertex].find((index) => outNormals[index * COORDINATES] === nx && outNormals[index * COORDINATES + Y] === ny && outNormals[index * COORDINATES + Z] === nz);
|
|
136
|
+
if (reuse === undefined) {
|
|
137
|
+
const index = outPositions.length / COORDINATES;
|
|
138
|
+
outPositions.push(positions[vertex * COORDINATES], positions[vertex * COORDINATES + Y], positions[vertex * COORDINATES + Z]);
|
|
139
|
+
outNormals.push(nx, ny, nz);
|
|
140
|
+
made[vertex].push(index);
|
|
141
|
+
outIndices[corner] = index;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
outIndices[corner] = reuse;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return { positions: Float32Array.from(outPositions), normals: Float32Array.from(outNormals), indices: outIndices };
|
|
148
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const GOLDEN = (1 + Math.sqrt(5)) / 2;
|
|
2
|
+
export const ICOSAHEDRON_POINTS = [
|
|
3
|
+
[-1, GOLDEN, 0], [1, GOLDEN, 0], [-1, -GOLDEN, 0], [1, -GOLDEN, 0],
|
|
4
|
+
[0, -1, GOLDEN], [0, 1, GOLDEN], [0, -1, -GOLDEN], [0, 1, -GOLDEN],
|
|
5
|
+
[GOLDEN, 0, -1], [GOLDEN, 0, 1], [-GOLDEN, 0, -1], [-GOLDEN, 0, 1],
|
|
6
|
+
];
|
|
7
|
+
export const ICOSAHEDRON_FACES = [
|
|
8
|
+
[0, 11, 5], [0, 5, 1], [0, 1, 7], [0, 7, 10], [0, 10, 11],
|
|
9
|
+
[1, 5, 9], [5, 11, 4], [11, 10, 2], [10, 7, 6], [7, 1, 8],
|
|
10
|
+
[3, 9, 4], [3, 4, 2], [3, 2, 6], [3, 6, 8], [3, 8, 9],
|
|
11
|
+
[4, 9, 5], [2, 4, 11], [6, 2, 10], [8, 6, 7], [9, 8, 1],
|
|
12
|
+
];
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ICOSAHEDRON_FACES, ICOSAHEDRON_POINTS } from "./mesh-primitives.constants";
|
|
2
|
+
function onUnitSphere([x, y, z]) {
|
|
3
|
+
const length = Math.hypot(x, y, z);
|
|
4
|
+
return [x / length, y / length, z / length];
|
|
5
|
+
}
|
|
6
|
+
export const icosphere = (detail) => {
|
|
7
|
+
const points = ICOSAHEDRON_POINTS.map(onUnitSphere);
|
|
8
|
+
let faces = ICOSAHEDRON_FACES;
|
|
9
|
+
for (let level = 0; level < detail; level++) {
|
|
10
|
+
const middles = new Map();
|
|
11
|
+
const middle = (a, b) => {
|
|
12
|
+
const key = a < b ? `${a}-${b}` : `${b}-${a}`;
|
|
13
|
+
let index = middles.get(key);
|
|
14
|
+
if (index === undefined) {
|
|
15
|
+
const [p, q] = [points[a], points[b]];
|
|
16
|
+
index = points.push(onUnitSphere([(p[0] + q[0]) / 2, (p[1] + q[1]) / 2, (p[2] + q[2]) / 2])) - 1;
|
|
17
|
+
middles.set(key, index);
|
|
18
|
+
}
|
|
19
|
+
return index;
|
|
20
|
+
};
|
|
21
|
+
faces = faces.flatMap(([a, b, c]) => {
|
|
22
|
+
const [ab, bc, ca] = [middle(a, b), middle(b, c), middle(c, a)];
|
|
23
|
+
return [[a, ab, ca], [b, bc, ab], [c, ca, bc], [ab, bc, ca]];
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return { positions: points.flat(), indices: faces.flat() };
|
|
27
|
+
};
|
|
28
|
+
export const frustum = (sides, bottomRadius, topRadius, height) => {
|
|
29
|
+
const positions = [0, 0, 0, 0, 0, height];
|
|
30
|
+
for (const [radius, z] of [[bottomRadius, 0], [topRadius, height]]) {
|
|
31
|
+
for (let side = 0; side < sides; side++) {
|
|
32
|
+
const angle = side / sides * 2 * Math.PI;
|
|
33
|
+
positions.push(radius * Math.cos(angle), radius * Math.sin(angle), z);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const bottom = (side) => 2 + side % sides;
|
|
37
|
+
const top = (side) => 2 + sides + side % sides;
|
|
38
|
+
const indices = [];
|
|
39
|
+
for (let side = 0; side < sides; side++) {
|
|
40
|
+
indices.push(0, bottom(side + 1), bottom(side));
|
|
41
|
+
indices.push(1, top(side), top(side + 1));
|
|
42
|
+
indices.push(bottom(side), bottom(side + 1), top(side + 1));
|
|
43
|
+
indices.push(bottom(side), top(side + 1), top(side));
|
|
44
|
+
}
|
|
45
|
+
return { positions, indices };
|
|
46
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type * as Inputs from "../../inputs";
|
|
2
|
+
type Point3 = Inputs.Base.Point3;
|
|
3
|
+
export declare const expandedKnots: (knots: readonly number[], multiplicities: readonly number[]) => number[];
|
|
4
|
+
export declare const knotDomain: (degree: number, knots: readonly number[]) => [number, number];
|
|
5
|
+
export declare const nurbsCurvePoint: (degree: number, points: readonly Point3[], weights: readonly number[] | undefined, knots: readonly number[], t: number) => Point3;
|
|
6
|
+
export declare const nurbsSurfacePoint: (degrees: readonly [number, number], net: readonly (readonly Point3[])[], weights: readonly (readonly number[])[] | undefined, knots: readonly [readonly number[], readonly number[]], u: number, v: number) => Point3;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export const expandedKnots = (knots, multiplicities) => knots.flatMap((knot, at) => { var _a; return Array.from({ length: (_a = multiplicities[at]) !== null && _a !== void 0 ? _a : 0 }, () => knot); });
|
|
2
|
+
export const knotDomain = (degree, knots) => [knots[degree], knots[knots.length - degree - 1]];
|
|
3
|
+
const spanOf = (degree, knots, t) => {
|
|
4
|
+
const last = knots.length - degree - 2;
|
|
5
|
+
if (t >= knots[last + 1]) {
|
|
6
|
+
return last;
|
|
7
|
+
}
|
|
8
|
+
if (t <= knots[degree]) {
|
|
9
|
+
return degree;
|
|
10
|
+
}
|
|
11
|
+
let low = degree;
|
|
12
|
+
let high = last + 1;
|
|
13
|
+
while (high - low > 1) {
|
|
14
|
+
const middle = (low + high) >> 1;
|
|
15
|
+
if (t < knots[middle]) {
|
|
16
|
+
high = middle;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
low = middle;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return low;
|
|
23
|
+
};
|
|
24
|
+
const deBoor = (degree, knots, parameter, at) => {
|
|
25
|
+
const [first, last] = knotDomain(degree, knots);
|
|
26
|
+
const t = Math.min(Math.max(parameter, first), last);
|
|
27
|
+
const span = spanOf(degree, knots, t);
|
|
28
|
+
const points = Array.from({ length: degree + 1 }, (_, j) => at(span - degree + j));
|
|
29
|
+
for (let r = 1; r <= degree; r++) {
|
|
30
|
+
for (let j = degree; j >= r; j--) {
|
|
31
|
+
const left = knots[span - degree + j];
|
|
32
|
+
const right = knots[span + 1 + j - r];
|
|
33
|
+
const alpha = right === left ? 0 : (t - left) / (right - left);
|
|
34
|
+
const previous = points[j - 1];
|
|
35
|
+
const current = points[j];
|
|
36
|
+
const keep = 1 - alpha;
|
|
37
|
+
points[j] = [keep * previous[0] + alpha * current[0], keep * previous[1] + alpha * current[1], keep * previous[2] + alpha * current[2], keep * previous[3] + alpha * current[3]];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return points[degree];
|
|
41
|
+
};
|
|
42
|
+
const lifted = (point, weight) => [point[0] * weight, point[1] * weight, point[2] * weight, weight];
|
|
43
|
+
const projected = (point) => [point[0] / point[3], point[1] / point[3], point[2] / point[3]];
|
|
44
|
+
export const nurbsCurvePoint = (degree, points, weights, knots, t) => projected(deBoor(degree, knots, t, (index) => { var _a; return lifted(points[index], (_a = weights === null || weights === void 0 ? void 0 : weights[index]) !== null && _a !== void 0 ? _a : 1); }));
|
|
45
|
+
export const nurbsSurfacePoint = (degrees, net, weights, knots, u, v) => {
|
|
46
|
+
const [uDegree, vDegree] = degrees;
|
|
47
|
+
const [uKnots, vKnots] = knots;
|
|
48
|
+
const row = (index) => deBoor(vDegree, vKnots, v, (column) => { var _a, _b; return lifted(net[index][column], (_b = (_a = weights === null || weights === void 0 ? void 0 : weights[index]) === null || _a === void 0 ? void 0 : _a[column]) !== null && _b !== void 0 ? _b : 1); });
|
|
49
|
+
return projected(deBoor(uDegree, uKnots, u, row));
|
|
50
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type * as Inputs from "../../inputs";
|
|
2
|
+
type Vec3 = Inputs.Base.Vector3;
|
|
3
|
+
type Point3 = Inputs.Base.Point3;
|
|
4
|
+
export declare const pathTangents: (points: readonly Point3[], start?: Vec3, end?: Vec3) => Vec3[];
|
|
5
|
+
export declare const transportedAxes: (points: readonly Point3[], tangents: readonly Vec3[], first: Vec3) => Vec3[];
|
|
6
|
+
export declare const sweepMatrices: (points: readonly Point3[], tangents: readonly Vec3[], xAxes: readonly Vec3[]) => Inputs.Base.TransformMatrix[];
|
|
7
|
+
export declare const roundedPath3: (points: readonly Point3[], radius: number, segmentsFor: (sweep: number) => number) => Point3[];
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { arcThroughThreePoints, pointsOnArc } from "./arcs";
|
|
2
|
+
import { add3, cross3, dot3, length3, scale3, subtract3 } from "./vectors";
|
|
3
|
+
const STRAIGHT_SLACK = 1e-12;
|
|
4
|
+
const STRAIGHT_COSINE = 1 - STRAIGHT_SLACK;
|
|
5
|
+
const CORNERED_POINTS = 3;
|
|
6
|
+
const MAX_MITRE = 8;
|
|
7
|
+
const TINY_LENGTH = 1e-9;
|
|
8
|
+
const TINY_SQUARED = 1e-18;
|
|
9
|
+
const AXES = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
|
|
10
|
+
const unit3 = (vector) => scale3(vector, 1 / length3(vector));
|
|
11
|
+
const reflected = (vector, across, squared) => subtract3(vector, scale3(across, (2 / squared) * dot3(across, vector)));
|
|
12
|
+
const square = (vector, to) => {
|
|
13
|
+
const across = subtract3(vector, scale3(to, dot3(vector, to)));
|
|
14
|
+
const length = length3(across);
|
|
15
|
+
return length > TINY_LENGTH ? scale3(across, 1 / length) : undefined;
|
|
16
|
+
};
|
|
17
|
+
const anyAcross = (tangent) => {
|
|
18
|
+
const least = AXES.reduce((best, axis) => (Math.abs(dot3(axis, tangent)) < Math.abs(dot3(best, tangent)) ? axis : best));
|
|
19
|
+
return square(least, tangent);
|
|
20
|
+
};
|
|
21
|
+
export const pathTangents = (points, start, end) => {
|
|
22
|
+
const directions = points.slice(1).map((point, at) => unit3(subtract3(point, points[at])));
|
|
23
|
+
return points.map((_, at) => {
|
|
24
|
+
if (at === 0) {
|
|
25
|
+
return start === undefined ? directions[0] : unit3(start);
|
|
26
|
+
}
|
|
27
|
+
if (at === points.length - 1) {
|
|
28
|
+
return end === undefined ? directions[at - 1] : unit3(end);
|
|
29
|
+
}
|
|
30
|
+
const sum = add3(directions[at - 1], directions[at]);
|
|
31
|
+
return length3(sum) > TINY_LENGTH ? unit3(sum) : directions[at];
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
export const transportedAxes = (points, tangents, first) => {
|
|
35
|
+
var _a, _b;
|
|
36
|
+
const axes = [(_a = square(first, tangents[0])) !== null && _a !== void 0 ? _a : anyAcross(tangents[0])];
|
|
37
|
+
for (let at = 1; at < points.length; at++) {
|
|
38
|
+
const step = subtract3(points[at], points[at - 1]);
|
|
39
|
+
const stepSquared = dot3(step, step);
|
|
40
|
+
const previous = axes[at - 1];
|
|
41
|
+
const turnedAxis = reflected(previous, step, stepSquared);
|
|
42
|
+
const turnedTangent = reflected(tangents[at - 1], step, stepSquared);
|
|
43
|
+
const remaining = subtract3(tangents[at], turnedTangent);
|
|
44
|
+
const remainingSquared = dot3(remaining, remaining);
|
|
45
|
+
const axis = remainingSquared > TINY_SQUARED ? reflected(turnedAxis, remaining, remainingSquared) : turnedAxis;
|
|
46
|
+
axes.push((_b = square(axis, tangents[at])) !== null && _b !== void 0 ? _b : anyAcross(tangents[at]));
|
|
47
|
+
}
|
|
48
|
+
return axes;
|
|
49
|
+
};
|
|
50
|
+
const mitred = (vector, bend, stretch) => add3(vector, scale3(bend, (stretch - 1) * dot3(vector, bend)));
|
|
51
|
+
export const sweepMatrices = (points, tangents, xAxes) => points.map((origin, at) => {
|
|
52
|
+
const z = tangents[at];
|
|
53
|
+
let x = xAxes[at];
|
|
54
|
+
let y = cross3(z, x);
|
|
55
|
+
if (at > 0 && at < points.length - 1) {
|
|
56
|
+
const incoming = unit3(subtract3(origin, points[at - 1]));
|
|
57
|
+
const outgoing = unit3(subtract3(points[at + 1], origin));
|
|
58
|
+
const cosine = dot3(incoming, outgoing);
|
|
59
|
+
const bend = square(subtract3(outgoing, incoming), z);
|
|
60
|
+
if (cosine < STRAIGHT_COSINE && bend !== undefined) {
|
|
61
|
+
const stretch = Math.min(MAX_MITRE, 1 / Math.max(dot3(z, incoming), 1 / MAX_MITRE));
|
|
62
|
+
x = mitred(x, bend, stretch);
|
|
63
|
+
y = mitred(y, bend, stretch);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return [x[0], x[1], x[2], 0, y[0], y[1], y[2], 0, z[0], z[1], z[2], 0, origin[0], origin[1], origin[2], 1];
|
|
67
|
+
});
|
|
68
|
+
export const roundedPath3 = (points, radius, segmentsFor) => {
|
|
69
|
+
if (!(radius > 0) || points.length < CORNERED_POINTS) {
|
|
70
|
+
return [...points];
|
|
71
|
+
}
|
|
72
|
+
const rounded = [points[0]];
|
|
73
|
+
for (let at = 1; at < points.length - 1; at++) {
|
|
74
|
+
const corner = points[at];
|
|
75
|
+
const toPrevious = subtract3(points[at - 1], corner);
|
|
76
|
+
const toNext = subtract3(points[at + 1], corner);
|
|
77
|
+
const incoming = scale3(toPrevious, -1 / length3(toPrevious));
|
|
78
|
+
const outgoing = unit3(toNext);
|
|
79
|
+
const cosine = dot3(incoming, outgoing);
|
|
80
|
+
if (!(cosine < STRAIGHT_COSINE)) {
|
|
81
|
+
rounded.push(corner);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const turn = Math.acos(Math.max(-1, Math.min(1, cosine)));
|
|
85
|
+
const reach = Math.min(radius * Math.tan(turn / 2), length3(toPrevious) / 2, length3(toNext) / 2);
|
|
86
|
+
const start = add3(corner, scale3(incoming, -reach));
|
|
87
|
+
const end = add3(corner, scale3(outgoing, reach));
|
|
88
|
+
const inward = unit3(subtract3(outgoing, incoming));
|
|
89
|
+
const fitted = reach / Math.tan(turn / 2);
|
|
90
|
+
const middle = add3(corner, scale3(inward, fitted / Math.cos(turn / 2) - fitted));
|
|
91
|
+
const arc = arcThroughThreePoints(start, middle, end);
|
|
92
|
+
rounded.push(...(arc ? pointsOnArc(arc, start, end, segmentsFor(turn)) : [corner]));
|
|
93
|
+
}
|
|
94
|
+
rounded.push(points[points.length - 1]);
|
|
95
|
+
return rounded;
|
|
96
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type * as Inputs from "../../inputs";
|
|
2
|
+
type Point2 = Inputs.Base.Point2;
|
|
3
|
+
export declare const signedArea2: (points: readonly Point2[]) => number;
|
|
4
|
+
export declare const wound2: (points: readonly Point2[], counterClockwise: boolean) => Point2[];
|
|
5
|
+
export declare const isInsidePolygon2: (point: Point2, outline: readonly Point2[]) => boolean;
|
|
6
|
+
export declare const perimeter2: (points: readonly Point2[]) => number;
|
|
7
|
+
export declare const roundedCorners2: (points: readonly Point2[], radii: readonly number[], segmentsFor: (sweep: number) => number) => Point2[];
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { add2, dot2, length2, scale2, subtract2 } from "./vectors";
|
|
2
|
+
const STRAIGHT_TOLERANCE = 1e-9;
|
|
3
|
+
const FIT_TOLERANCE = 1e-9;
|
|
4
|
+
export const signedArea2 = (points) => {
|
|
5
|
+
let twice = 0;
|
|
6
|
+
for (let at = 0; at < points.length; at++) {
|
|
7
|
+
const a = points[at];
|
|
8
|
+
const b = points[(at + 1) % points.length];
|
|
9
|
+
twice += a[0] * b[1] - b[0] * a[1];
|
|
10
|
+
}
|
|
11
|
+
return twice / 2;
|
|
12
|
+
};
|
|
13
|
+
export const wound2 = (points, counterClockwise) => (signedArea2(points) > 0) === counterClockwise ? [...points] : [...points].reverse();
|
|
14
|
+
export const isInsidePolygon2 = (point, outline) => {
|
|
15
|
+
let inside = false;
|
|
16
|
+
for (let at = 0, previous = outline.length - 1; at < outline.length; previous = at++) {
|
|
17
|
+
const [x1, y1] = outline[at];
|
|
18
|
+
const [x2, y2] = outline[previous];
|
|
19
|
+
if ((y1 > point[1]) !== (y2 > point[1]) && point[0] < ((x2 - x1) * (point[1] - y1)) / (y2 - y1) + x1) {
|
|
20
|
+
inside = !inside;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return inside;
|
|
24
|
+
};
|
|
25
|
+
export const perimeter2 = (points) => points.reduce((sum, point, at) => sum + length2(subtract2(points[(at + 1) % points.length], point)), 0);
|
|
26
|
+
const unit2 = (vector) => scale2(vector, 1 / length2(vector));
|
|
27
|
+
export const roundedCorners2 = (points, radii, segmentsFor) => {
|
|
28
|
+
const rounded = [];
|
|
29
|
+
points.forEach((corner, at) => {
|
|
30
|
+
var _a;
|
|
31
|
+
const radius = (_a = radii[at]) !== null && _a !== void 0 ? _a : 0;
|
|
32
|
+
const previous = points[(at + points.length - 1) % points.length];
|
|
33
|
+
const next = points[(at + 1) % points.length];
|
|
34
|
+
const toPrevious = unit2(subtract2(previous, corner));
|
|
35
|
+
const toNext = unit2(subtract2(next, corner));
|
|
36
|
+
const half = Math.acos(Math.min(1, Math.max(-1, dot2(toPrevious, toNext)))) / 2;
|
|
37
|
+
if (!(radius > 0) || half < STRAIGHT_TOLERANCE || half > Math.PI / 2 - STRAIGHT_TOLERANCE) {
|
|
38
|
+
rounded.push(corner);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const reach = radius / Math.tan(half);
|
|
42
|
+
const room = Math.min(length2(subtract2(previous, corner)), length2(subtract2(next, corner)));
|
|
43
|
+
if (reach > room * (1 + FIT_TOLERANCE)) {
|
|
44
|
+
throw new Error(`A corner radius of ${radius} does not fit the edges beside its corner`);
|
|
45
|
+
}
|
|
46
|
+
const center = add2(corner, scale2(unit2(add2(toPrevious, toNext)), radius / Math.sin(half)));
|
|
47
|
+
const start = subtract2(add2(corner, scale2(toPrevious, reach)), center);
|
|
48
|
+
const end = subtract2(add2(corner, scale2(toNext, reach)), center);
|
|
49
|
+
const from = Math.atan2(start[1], start[0]);
|
|
50
|
+
let sweep = Math.atan2(end[1], end[0]) - from;
|
|
51
|
+
sweep -= Math.round(sweep / (2 * Math.PI)) * 2 * Math.PI;
|
|
52
|
+
const segments = segmentsFor(Math.abs(sweep));
|
|
53
|
+
for (let step = 0; step <= segments; step++) {
|
|
54
|
+
const angle = from + (sweep * step) / segments;
|
|
55
|
+
rounded.push([center[0] + radius * Math.cos(angle), center[1] + radius * Math.sin(angle)]);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return rounded;
|
|
59
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type * as Inputs from "../../inputs";
|
|
2
|
+
import type { RefinedSurfaceMesh } from "./helper-types";
|
|
3
|
+
type Point2 = Inputs.Base.Point2;
|
|
4
|
+
type Point3 = Inputs.Base.Point3;
|
|
5
|
+
export declare const refinedSurfaceMesh: (uv: readonly Point2[], positions: readonly Point3[], loops: readonly (readonly number[])[], pointAt: (u: number, v: number) => Point3) => RefinedSurfaceMesh;
|
|
6
|
+
export {};
|