@bitbybit-dev/base 1.1.1 → 1.2.0-rc.1
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/README.md +14 -4
- package/index.js +0 -3
- package/lib/api/GlobalCDNProvider.js +1 -1
- package/lib/api/inputs/base-inputs.d.ts +114 -0
- package/lib/api/inputs/base-inputs.js +15 -0
- package/lib/api/inputs/color-inputs.d.ts +4 -0
- package/lib/api/inputs/color-inputs.js +4 -0
- package/lib/api/inputs/dates-inputs.d.ts +4 -0
- package/lib/api/inputs/dates-inputs.js +4 -0
- package/lib/api/inputs/io-inputs.d.ts +10 -6
- package/lib/api/inputs/io-inputs.js +4 -0
- package/lib/api/inputs/line-inputs.d.ts +36 -31
- package/lib/api/inputs/line-inputs.js +13 -0
- package/lib/api/inputs/lists-inputs.d.ts +28 -20
- package/lib/api/inputs/lists-inputs.js +16 -0
- package/lib/api/inputs/logic-inputs.d.ts +15 -4
- package/lib/api/inputs/logic-inputs.js +65 -0
- package/lib/api/inputs/math-inputs.d.ts +25 -0
- package/lib/api/inputs/math-inputs.js +40 -0
- package/lib/api/inputs/mesh-inputs.d.ts +16 -11
- package/lib/api/inputs/mesh-inputs.js +5 -0
- package/lib/api/inputs/point-inputs.d.ts +33 -27
- package/lib/api/inputs/point-inputs.js +14 -0
- package/lib/api/inputs/polyline-inputs.d.ts +33 -28
- package/lib/api/inputs/polyline-inputs.js +5 -0
- package/lib/api/inputs/text-inputs.d.ts +27 -14
- package/lib/api/inputs/text-inputs.js +13 -0
- package/lib/api/inputs/transforms-inputs.d.ts +9 -3
- package/lib/api/inputs/transforms-inputs.js +6 -0
- package/lib/api/inputs/vector-inputs.d.ts +5 -0
- package/lib/api/inputs/vector-inputs.js +69 -0
- package/lib/api/models/point/hex-grid-data.d.ts +8 -3
- package/lib/api/models/point/hex-grid-data.js +5 -0
- package/lib/api/models/simplex.d.ts +19 -250
- package/lib/api/models/simplex.js +7 -35
- package/lib/api/models/text/vector-char-data.d.ts +3 -3
- package/lib/api/models/text/vector-text-data.d.ts +3 -3
- package/lib/api/services/color.js +5 -4
- package/lib/api/services/geometry-helper.d.ts +5 -5
- package/lib/api/services/geometry-helper.js +101 -2
- package/lib/api/services/helpers/dxf/dxf-generator.js +20 -69
- package/lib/api/services/helpers/mesh-normals.d.ts +9 -0
- package/lib/api/services/helpers/mesh-normals.js +56 -0
- package/lib/api/services/io.d.ts +4 -0
- package/lib/api/services/io.js +4 -0
- package/lib/api/services/line.d.ts +2 -2
- package/lib/api/services/line.js +18 -51
- package/lib/api/services/lists.js +1 -6
- package/lib/api/services/mesh.d.ts +2 -1
- package/lib/api/services/mesh.js +14 -32
- package/lib/api/services/point.d.ts +3 -3
- package/lib/api/services/point.js +26 -82
- package/lib/api/services/polyline.d.ts +2 -2
- package/lib/api/services/polyline.js +13 -43
- package/lib/api/services/text.js +1 -9
- package/lib/api/services/transforms.js +10 -16
- package/lib/api/services/vector.d.ts +4 -4
- package/lib/api/services/vector.js +3 -3
- package/package.json +53 -91
- package/babel.config.cjs +0 -14
- package/babel.config.d.cts +0 -5
- package/lib/api/unit-test-helper.d.ts +0 -20
- package/lib/api/unit-test-helper.js +0 -130
package/lib/api/services/line.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Contains various methods for lines and segments. Line in bitbybit is a simple object that has start and
|
|
3
|
-
* { start: [ x, y, z ], end: [ x, y, z ] }
|
|
2
|
+
* Contains various methods for lines and segments. Line in bitbybit is a simple object that has start and
|
|
3
|
+
* end point properties. { start: [ x, y, z ], end: [ x, y, z ] }
|
|
4
4
|
*/
|
|
5
5
|
export class Line {
|
|
6
6
|
constructor(vector, point, geometryHelper) {
|
|
@@ -134,12 +134,11 @@ export class Line {
|
|
|
134
134
|
* @drawable true
|
|
135
135
|
*/
|
|
136
136
|
getPointOnLine(inputs) {
|
|
137
|
-
|
|
137
|
+
var _a;
|
|
138
138
|
const point1 = inputs.line.start;
|
|
139
139
|
const point2 = inputs.line.end;
|
|
140
|
-
const parameter = inputs.param;
|
|
140
|
+
const parameter = (_a = inputs.param) !== null && _a !== void 0 ? _a : 0.5;
|
|
141
141
|
const direction = [point2[0] - point1[0], point2[1] - point1[1], point2[2] - point1[2]];
|
|
142
|
-
// Calculate point on line segment corresponding to parameter value
|
|
143
142
|
const point = [point1[0] + parameter * direction[0], point1[1] + parameter * direction[1], point1[2] + parameter * direction[2]];
|
|
144
143
|
return point;
|
|
145
144
|
}
|
|
@@ -235,11 +234,10 @@ export class Line {
|
|
|
235
234
|
* @drawable true
|
|
236
235
|
*/
|
|
237
236
|
lineLineIntersection(inputs) {
|
|
238
|
-
const epsilon = inputs.tolerance || 1e-6;
|
|
237
|
+
const epsilon = inputs.tolerance || 1e-6;
|
|
239
238
|
const checkSegments = inputs.checkSegmentsOnly;
|
|
240
239
|
const line1 = inputs.line1;
|
|
241
240
|
const line2 = inputs.line2;
|
|
242
|
-
// Input validation
|
|
243
241
|
if (!(line1 === null || line1 === void 0 ? void 0 : line1.start) || !line1.end || !(line2 === null || line2 === void 0 ? void 0 : line2.start) || !line2.end ||
|
|
244
242
|
line1.start.length !== 3 || line1.end.length !== 3 ||
|
|
245
243
|
line2.start.length !== 3 || line2.end.length !== 3) {
|
|
@@ -247,102 +245,71 @@ export class Line {
|
|
|
247
245
|
return undefined;
|
|
248
246
|
}
|
|
249
247
|
const p1 = line1.start;
|
|
250
|
-
const d1 = this.vector.sub({ first: line1.end, second: line1.start });
|
|
248
|
+
const d1 = this.vector.sub({ first: line1.end, second: line1.start });
|
|
251
249
|
const p2 = line2.start;
|
|
252
|
-
const d2 = this.vector.sub({ first: line2.end, second: line2.start });
|
|
253
|
-
const p21 = this.vector.sub({ first: p2, second: p1 });
|
|
254
|
-
// --- Check for Zero-Length Segments ---
|
|
250
|
+
const d2 = this.vector.sub({ first: line2.end, second: line2.start });
|
|
251
|
+
const p21 = this.vector.sub({ first: p2, second: p1 });
|
|
255
252
|
const lenSq1 = this.vector.lengthSq({ vector: d1 });
|
|
256
253
|
const lenSq2 = this.vector.lengthSq({ vector: d2 });
|
|
257
|
-
// Compare squared length against squared epsilon
|
|
258
254
|
if (lenSq1 < epsilon * epsilon || lenSq2 < epsilon * epsilon) {
|
|
259
255
|
return undefined;
|
|
260
256
|
}
|
|
261
|
-
// --- Check for Parallelism ---
|
|
262
257
|
const d1_cross_d2 = this.vector.cross({ first: d1, second: d2 });
|
|
263
258
|
const crossMagSq = this.vector.lengthSq({ vector: d1_cross_d2 });
|
|
264
|
-
|
|
265
|
-
// Use epsilon squared as a base tolerance, potentially scale by magnitudes
|
|
266
|
-
const parallel_tolerance_sq = epsilon * epsilon; // May need adjustment: * lenSq1 * lenSq2;
|
|
259
|
+
const parallel_tolerance_sq = epsilon * epsilon;
|
|
267
260
|
if (crossMagSq < parallel_tolerance_sq) {
|
|
268
|
-
// Potentially Parallel or Collinear
|
|
269
|
-
// Check if collinear: p21 must be parallel to d1
|
|
270
261
|
const p21_cross_d1 = this.vector.cross({ first: p21, second: d1 });
|
|
271
|
-
|
|
272
|
-
const collinear_tolerance_sq = epsilon * epsilon * lenSq1; // Scale by line1 length
|
|
262
|
+
const collinear_tolerance_sq = epsilon * epsilon * lenSq1;
|
|
273
263
|
if (this.vector.lengthSq({ vector: p21_cross_d1 }) < collinear_tolerance_sq) {
|
|
274
|
-
// Collinear
|
|
275
264
|
if (!checkSegments) {
|
|
276
|
-
return p1;
|
|
265
|
+
return p1;
|
|
277
266
|
}
|
|
278
267
|
else {
|
|
279
|
-
|
|
280
|
-
const d1d1 = lenSq1; // Reuse calculated squared length
|
|
281
|
-
// Avoid division by zero if lenSq1 is extremely small (should be caught earlier)
|
|
268
|
+
const d1d1 = lenSq1;
|
|
282
269
|
const safe_d1d1 = (d1d1 < epsilon * epsilon) ? 1.0 : d1d1;
|
|
283
|
-
const d1p21 = this.vector.dot({ first: d1, second: p21 });
|
|
284
|
-
const t_p2 = d1p21 / safe_d1d1;
|
|
270
|
+
const d1p21 = this.vector.dot({ first: d1, second: p21 });
|
|
271
|
+
const t_p2 = d1p21 / safe_d1d1;
|
|
285
272
|
const vec_e2_p1 = this.vector.sub({ first: line2.end, second: p1 });
|
|
286
|
-
const t_e2 = this.vector.dot({ first: d1, second: vec_e2_p1 }) / safe_d1d1;
|
|
273
|
+
const t_e2 = this.vector.dot({ first: d1, second: vec_e2_p1 }) / safe_d1d1;
|
|
287
274
|
const interval2_t = [Math.min(t_p2, t_e2), Math.max(t_p2, t_e2)];
|
|
288
|
-
const interval1_t = [0, 1];
|
|
275
|
+
const interval1_t = [0, 1];
|
|
289
276
|
const overlap_start = Math.max(interval1_t[0], interval2_t[0]);
|
|
290
277
|
const overlap_end = Math.min(interval1_t[1], interval2_t[1]);
|
|
291
|
-
// Check for overlap including tolerance
|
|
292
278
|
if (overlap_start <= overlap_end + epsilon) {
|
|
293
|
-
// Overlap exists, but intersection is a segment, not a single point
|
|
294
279
|
return undefined;
|
|
295
280
|
}
|
|
296
281
|
else {
|
|
297
|
-
// Collinear but segments do not overlap
|
|
298
282
|
return undefined;
|
|
299
283
|
}
|
|
300
284
|
}
|
|
301
285
|
}
|
|
302
286
|
else {
|
|
303
|
-
// Parallel but not collinear
|
|
304
287
|
return undefined;
|
|
305
288
|
}
|
|
306
289
|
}
|
|
307
|
-
// --- Lines are NOT Parallel - Check for Skewness using Scalar Triple Product ---
|
|
308
290
|
const scalarTripleProduct = this.vector.dot({ first: p21, second: d1_cross_d2 });
|
|
309
|
-
// If the scalar triple product is significantly non-zero, the lines are skew.
|
|
310
|
-
// Tolerance needs consideration - relates to the "volume" formed by the vectors.
|
|
311
|
-
// A simple absolute check against epsilon^3 or similar might work for typical scales.
|
|
312
|
-
// Consider scaling tolerance if coordinates can be very large/small.
|
|
313
291
|
const skew_tolerance = epsilon * epsilon * epsilon;
|
|
314
292
|
if (Math.abs(scalarTripleProduct) > skew_tolerance) {
|
|
315
|
-
// Lines are Skew
|
|
316
293
|
return undefined;
|
|
317
294
|
}
|
|
318
|
-
// --- Lines are Intersecting (Coplanar and Non-Parallel) ---
|
|
319
|
-
// Calculate intersection parameters t (for line1) and u (for line2)
|
|
320
|
-
// We can use the formulas derived earlier, which are valid for intersecting lines.
|
|
321
295
|
const d1d1 = lenSq1;
|
|
322
296
|
const d2d2 = lenSq2;
|
|
323
297
|
const d1d2 = this.vector.dot({ first: d1, second: d2 });
|
|
324
298
|
const d1p21 = this.vector.dot({ first: d1, second: p21 });
|
|
325
299
|
const d2p21 = this.vector.dot({ first: d2, second: p21 });
|
|
326
|
-
// Denominator for parameter calculation (same as crossMagSq, essentially)
|
|
327
300
|
const denominator = d1d1 * d2d2 - d1d2 * d1d2;
|
|
328
|
-
// Denominator *should* be non-zero based on the parallelism check above,
|
|
329
|
-
// but add a defensive check.
|
|
330
301
|
if (Math.abs(denominator) < epsilon * epsilon) {
|
|
331
302
|
console.error("Internal error: Denominator near zero after non-parallel check.");
|
|
332
|
-
return undefined;
|
|
303
|
+
return undefined;
|
|
333
304
|
}
|
|
334
305
|
const t = (d2d2 * d1p21 - d1d2 * d2p21) / denominator;
|
|
335
306
|
const u = (d1d2 * d1p21 - d1d1 * d2p21) / denominator;
|
|
336
|
-
// --- Optional check: Is intersection within segment bounds? ---
|
|
337
307
|
if (checkSegments) {
|
|
338
|
-
// Check if t and u are within the range [0, 1] (using tolerance)
|
|
339
308
|
if (t < -epsilon || t > 1.0 + epsilon || u < -epsilon || u > 1.0 + epsilon) {
|
|
340
|
-
return undefined;
|
|
309
|
+
return undefined;
|
|
341
310
|
}
|
|
342
311
|
}
|
|
343
|
-
// --- Calculate Intersection Point ---
|
|
344
312
|
const intersectionPoint = this.getPointOnLine({ param: t, line: line1 });
|
|
345
|
-
// Clip near-zero results based on the input epsilon
|
|
346
313
|
return [
|
|
347
314
|
Math.abs(intersectionPoint[0]) < epsilon ? 0 : intersectionPoint[0],
|
|
348
315
|
Math.abs(intersectionPoint[1]) < epsilon ? 0 : intersectionPoint[1],
|
|
@@ -189,7 +189,6 @@ export class Lists {
|
|
|
189
189
|
const elToMerge = [];
|
|
190
190
|
const result = [];
|
|
191
191
|
lists.forEach(list => {
|
|
192
|
-
// flatten to certain level;
|
|
193
192
|
const elementsToMerge = list.flat(level);
|
|
194
193
|
elToMerge.push(elementsToMerge);
|
|
195
194
|
});
|
|
@@ -242,11 +241,8 @@ export class Lists {
|
|
|
242
241
|
longestSoFar = l.length;
|
|
243
242
|
}
|
|
244
243
|
});
|
|
245
|
-
return longestSoFar;
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
return undefined;
|
|
249
244
|
}
|
|
245
|
+
return longestSoFar;
|
|
250
246
|
}
|
|
251
247
|
/**
|
|
252
248
|
* Reverses the order of items in the list.
|
|
@@ -815,7 +811,6 @@ export class Lists {
|
|
|
815
811
|
* @drawable false
|
|
816
812
|
*/
|
|
817
813
|
repeatInPattern(inputs) {
|
|
818
|
-
// will repeat the items provided in the patten till the certain length of the list is reached
|
|
819
814
|
let inpList = inputs.list;
|
|
820
815
|
if (inputs.clone) {
|
|
821
816
|
inpList = structuredClone(inputs.list);
|
|
@@ -2,7 +2,8 @@ import * as Inputs from "../inputs";
|
|
|
2
2
|
import { Polyline } from "./polyline";
|
|
3
3
|
import { Vector } from "./vector";
|
|
4
4
|
/**
|
|
5
|
-
* Contains various mesh helper methods that are not necessarily present in higher level CAD kernels that
|
|
5
|
+
* Contains various mesh helper methods that are not necessarily present in higher level CAD kernels that
|
|
6
|
+
* bitbybit is using.
|
|
6
7
|
*/
|
|
7
8
|
export declare class MeshBitByBit {
|
|
8
9
|
private readonly vector;
|
package/lib/api/services/mesh.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Contains various mesh helper methods that are not necessarily present in higher level CAD kernels that
|
|
2
|
+
* Contains various mesh helper methods that are not necessarily present in higher level CAD kernels that
|
|
3
|
+
* bitbybit is using.
|
|
3
4
|
*/
|
|
4
5
|
export class MeshBitByBit {
|
|
5
6
|
constructor(vector, polyline) {
|
|
@@ -34,9 +35,8 @@ export class MeshBitByBit {
|
|
|
34
35
|
const edge2 = this.vector.sub({ first: inputs.triangle[2], second: inputs.triangle[0] });
|
|
35
36
|
const normal = this.vector.cross({ first: edge1, second: edge2 });
|
|
36
37
|
if (this.vector.lengthSq({ vector: normal }) < EPSILON_SQ) {
|
|
37
|
-
return undefined;
|
|
38
|
+
return undefined;
|
|
38
39
|
}
|
|
39
|
-
// Defensive copy if normalize modifies in-place
|
|
40
40
|
const normalizedNormal = this.vector.normalized({ vector: normal });
|
|
41
41
|
const d = this.vector.dot({ first: normalizedNormal, second: inputs.triangle[0] });
|
|
42
42
|
return { normal: normalizedNormal, d: d };
|
|
@@ -82,15 +82,13 @@ export class MeshBitByBit {
|
|
|
82
82
|
const allDistPZero = distP_Plane2.every(d => Math.abs(d) < EPSILON);
|
|
83
83
|
const allDistQZero = distQ_Plane1.every(d => Math.abs(d) < EPSILON);
|
|
84
84
|
if (allDistPZero && allDistQZero) {
|
|
85
|
-
return undefined;
|
|
85
|
+
return undefined;
|
|
86
86
|
}
|
|
87
87
|
const lineDir = this.vector.cross({ first: plane1.normal, second: plane2.normal });
|
|
88
|
-
const det = this.vector.dot({ first: lineDir, second: lineDir });
|
|
88
|
+
const det = this.vector.dot({ first: lineDir, second: lineDir });
|
|
89
89
|
if (det < EPSILON * EPSILON) {
|
|
90
|
-
return undefined;
|
|
90
|
+
return undefined;
|
|
91
91
|
}
|
|
92
|
-
// --- Calculate Interval Projections ---
|
|
93
|
-
// Store the 3D points that define the intervals on the line
|
|
94
92
|
const t1_intersection_points_3d = [];
|
|
95
93
|
const t2_intersection_points_3d = [];
|
|
96
94
|
const edges1 = [[p1, p2], [p2, p3], [p3, p1]];
|
|
@@ -101,9 +99,8 @@ export class MeshBitByBit {
|
|
|
101
99
|
const du = dists1[i];
|
|
102
100
|
const dv = dists1[(i + 1) % 3];
|
|
103
101
|
if (Math.abs(du) < EPSILON)
|
|
104
|
-
t1_intersection_points_3d.push(u);
|
|
105
|
-
|
|
106
|
-
if ((du * dv) < 0 && Math.abs(du - dv) > EPSILON) { // Edge crosses plane2
|
|
102
|
+
t1_intersection_points_3d.push(u);
|
|
103
|
+
if ((du * dv) < 0 && Math.abs(du - dv) > EPSILON) {
|
|
107
104
|
const t = du / (du - dv);
|
|
108
105
|
t1_intersection_points_3d.push(this.computeIntersectionPoint(u, v, t));
|
|
109
106
|
}
|
|
@@ -116,58 +113,43 @@ export class MeshBitByBit {
|
|
|
116
113
|
const du = dists2[i];
|
|
117
114
|
const dv = dists2[(i + 1) % 3];
|
|
118
115
|
if (Math.abs(du) < EPSILON)
|
|
119
|
-
t2_intersection_points_3d.push(u);
|
|
120
|
-
|
|
121
|
-
if ((du * dv) < 0 && Math.abs(du - dv) > EPSILON) { // Edge crosses plane1
|
|
116
|
+
t2_intersection_points_3d.push(u);
|
|
117
|
+
if ((du * dv) < 0 && Math.abs(du - dv) > EPSILON) {
|
|
122
118
|
const t = du / (du - dv);
|
|
123
119
|
t2_intersection_points_3d.push(this.computeIntersectionPoint(u, v, t));
|
|
124
120
|
}
|
|
125
121
|
}
|
|
126
|
-
// We expect exactly two points for each triangle in the standard piercing case.
|
|
127
|
-
// Handle potential duplicates or edge cases if more points are generated (e.g., edge lies on plane)
|
|
128
|
-
// A simple check for the common case:
|
|
129
122
|
if (t1_intersection_points_3d.length < 2 || t2_intersection_points_3d.length < 2) {
|
|
130
|
-
|
|
131
|
-
// or due to numerical precision near edges/vertices.
|
|
132
|
-
return undefined; // Treat touch as no intersection segment
|
|
123
|
+
return undefined;
|
|
133
124
|
}
|
|
134
|
-
// Calculate a robust origin ON the intersection line
|
|
135
125
|
const n1 = plane1.normal;
|
|
136
126
|
const n2 = plane2.normal;
|
|
137
127
|
const d1 = plane1.d;
|
|
138
128
|
const d2 = plane2.d;
|
|
139
|
-
// Point P = ( (d1 * N2 - d2 * N1) x D ) / (D dot D)
|
|
140
129
|
const term1 = this.vector.mul({ vector: n2, scalar: d1 });
|
|
141
130
|
const term2 = this.vector.mul({ vector: n1, scalar: d2 });
|
|
142
131
|
const termSub = this.vector.sub({ first: term1, second: term2 });
|
|
143
132
|
const crossTerm = this.vector.cross({ first: termSub, second: lineDir });
|
|
144
133
|
const lineOrigin = this.vector.mul({ vector: crossTerm, scalar: 1.0 / det });
|
|
145
|
-
// Project the 3D intersection points onto the lineDir, relative to lineOrigin
|
|
146
134
|
const t1_params = t1_intersection_points_3d.map(p => this.vector.dot({ first: this.vector.sub({ first: p, second: lineOrigin }), second: lineDir }));
|
|
147
135
|
const t2_params = t2_intersection_points_3d.map(p => this.vector.dot({ first: this.vector.sub({ first: p, second: lineOrigin }), second: lineDir }));
|
|
148
|
-
// Find the intervals
|
|
149
136
|
const t1Interval = [Math.min(...t1_params), Math.max(...t1_params)];
|
|
150
137
|
const t2Interval = [Math.min(...t2_params), Math.max(...t2_params)];
|
|
151
|
-
// Find the overlap of the two intervals
|
|
152
138
|
const intersectionMinParam = Math.max(t1Interval[0], t2Interval[0]);
|
|
153
139
|
const intersectionMaxParam = Math.min(t1Interval[1], t2Interval[1]);
|
|
154
|
-
|
|
155
|
-
if (intersectionMinParam < intersectionMaxParam - (EPSILON * det)) { // Let's use scaled epsilon for robustness against small det values.
|
|
156
|
-
// Convert the final parameters back to 3D points using the lineOrigin
|
|
157
|
-
// P = lineOrigin + dir * (param / det)
|
|
140
|
+
if (intersectionMinParam < intersectionMaxParam - (EPSILON * det)) {
|
|
158
141
|
const point1 = this.vector.add({ first: lineOrigin, second: this.vector.mul({ vector: lineDir, scalar: intersectionMinParam / det }) });
|
|
159
142
|
const point2 = this.vector.add({ first: lineOrigin, second: this.vector.mul({ vector: lineDir, scalar: intersectionMaxParam / det }) });
|
|
160
|
-
// Check if the resulting segment has non-zero length
|
|
161
143
|
const segVec = this.vector.sub({ first: point1, second: point2 });
|
|
162
144
|
if (this.vector.lengthSq({ vector: segVec }) > EPSILON * EPSILON) {
|
|
163
145
|
return [point1, point2];
|
|
164
146
|
}
|
|
165
147
|
else {
|
|
166
|
-
return undefined;
|
|
148
|
+
return undefined;
|
|
167
149
|
}
|
|
168
150
|
}
|
|
169
151
|
else {
|
|
170
|
-
return undefined;
|
|
152
|
+
return undefined;
|
|
171
153
|
}
|
|
172
154
|
}
|
|
173
155
|
/**
|
|
@@ -5,8 +5,8 @@ import { Vector } from "./vector";
|
|
|
5
5
|
import * as Models from "../models";
|
|
6
6
|
import { Lists } from "./lists";
|
|
7
7
|
/**
|
|
8
|
-
* Contains various methods for points. Point in bitbybit is simply an array containing 3 numbers for [x, y,
|
|
9
|
-
* Because of this form Point can be interchanged with Vector, which also is an array in [x, y, z] form.
|
|
8
|
+
* Contains various methods for points. Point in bitbybit is simply an array containing 3 numbers for [x, y,
|
|
9
|
+
* z]. Because of this form Point can be interchanged with Vector, which also is an array in [x, y, z] form.
|
|
10
10
|
* When creating 2D points, z coordinate is simply set to 0 - [x, y, 0].
|
|
11
11
|
*/
|
|
12
12
|
export declare class Point {
|
|
@@ -336,7 +336,7 @@ export declare class Point {
|
|
|
336
336
|
* @shortname normal from 3 points
|
|
337
337
|
* @drawable true
|
|
338
338
|
*/
|
|
339
|
-
normalFromThreePoints(inputs: Inputs.Point.ThreePointsNormalDto): Inputs.Base.Vector3;
|
|
339
|
+
normalFromThreePoints(inputs: Inputs.Point.ThreePointsNormalDto): Inputs.Base.Vector3 | undefined;
|
|
340
340
|
private closestPointFromPointData;
|
|
341
341
|
/**
|
|
342
342
|
* Checks if two points are approximately equal within tolerance (distance-based comparison).
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Contains various methods for points. Point in bitbybit is simply an array containing 3 numbers for [x, y,
|
|
3
|
-
* Because of this form Point can be interchanged with Vector, which also is an array in [x, y, z] form.
|
|
2
|
+
* Contains various methods for points. Point in bitbybit is simply an array containing 3 numbers for [x, y,
|
|
3
|
+
* z]. Because of this form Point can be interchanged with Vector, which also is an array in [x, y, z] form.
|
|
4
4
|
* When creating 2D points, z coordinate is simply set to 0 - [x, y, 0].
|
|
5
5
|
*/
|
|
6
6
|
export class Point {
|
|
@@ -410,21 +410,19 @@ export class Point {
|
|
|
410
410
|
* @drawable false
|
|
411
411
|
*/
|
|
412
412
|
hexGridScaledToFit(inputs) {
|
|
413
|
-
var _a, _b, _c, _d;
|
|
414
|
-
let width = inputs.width;
|
|
415
|
-
let height = inputs.height;
|
|
416
|
-
let nrHexagonsInHeight = inputs.nrHexagonsInHeight;
|
|
417
|
-
let nrHexagonsInWidth = inputs.nrHexagonsInWidth;
|
|
418
|
-
let extendTop = (
|
|
419
|
-
let extendBottom = (
|
|
420
|
-
let extendLeft = (
|
|
421
|
-
let extendRight = (
|
|
413
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
414
|
+
let width = (_a = inputs.width) !== null && _a !== void 0 ? _a : 10;
|
|
415
|
+
let height = (_b = inputs.height) !== null && _b !== void 0 ? _b : 10;
|
|
416
|
+
let nrHexagonsInHeight = (_c = inputs.nrHexagonsInHeight) !== null && _c !== void 0 ? _c : 10;
|
|
417
|
+
let nrHexagonsInWidth = (_d = inputs.nrHexagonsInWidth) !== null && _d !== void 0 ? _d : 10;
|
|
418
|
+
let extendTop = (_e = inputs.extendTop) !== null && _e !== void 0 ? _e : false;
|
|
419
|
+
let extendBottom = (_f = inputs.extendBottom) !== null && _f !== void 0 ? _f : false;
|
|
420
|
+
let extendLeft = (_g = inputs.extendLeft) !== null && _g !== void 0 ? _g : false;
|
|
421
|
+
let extendRight = (_h = inputs.extendRight) !== null && _h !== void 0 ? _h : false;
|
|
422
422
|
const { flatTop = false, centerGrid = false, pointsOnGround = false } = inputs;
|
|
423
|
-
// we flip the width and height if the hexagons are flat-topped and will then rotate resuls afterwards as default
|
|
424
|
-
// computes pointy-top hexagons
|
|
425
423
|
if (flatTop) {
|
|
426
424
|
const oldWidth = width;
|
|
427
|
-
width =
|
|
425
|
+
width = height;
|
|
428
426
|
height = oldWidth;
|
|
429
427
|
const oldNrHexagonsInWidth = nrHexagonsInWidth;
|
|
430
428
|
nrHexagonsInWidth = nrHexagonsInHeight;
|
|
@@ -438,27 +436,22 @@ export class Point {
|
|
|
438
436
|
extendLeft = extendBottomOld;
|
|
439
437
|
extendRight = extendTopOld;
|
|
440
438
|
}
|
|
441
|
-
// --- Input Validation ---
|
|
442
439
|
if (width <= 0 || height <= 0 || nrHexagonsInWidth < 1 || nrHexagonsInHeight < 1) {
|
|
443
440
|
console.warn("Hex grid dimensions and counts must be positive.");
|
|
444
441
|
return { centers: [], hexagons: [], shortestDistEdge: undefined, longestDistEdge: undefined, maxFilletRadius: undefined };
|
|
445
442
|
}
|
|
446
|
-
// --- Generate Unscaled Regular Grid Centers (Radius = 1) ---
|
|
447
|
-
// Use the *existing* hexGrid function, ensuring it doesn't center or project yet.
|
|
448
443
|
const BASE_RADIUS = 1.0;
|
|
449
444
|
const unscaledCenters = this.hexGrid({
|
|
450
445
|
radiusHexagon: BASE_RADIUS,
|
|
451
446
|
nrHexagonsX: nrHexagonsInWidth,
|
|
452
447
|
nrHexagonsY: nrHexagonsInHeight,
|
|
453
|
-
orientOnCenter: false,
|
|
454
|
-
pointsOnGround: false
|
|
448
|
+
orientOnCenter: false,
|
|
449
|
+
pointsOnGround: false
|
|
455
450
|
});
|
|
456
451
|
if (unscaledCenters.length === 0) {
|
|
457
|
-
return { centers: [], hexagons: [], shortestDistEdge: undefined, longestDistEdge: undefined, maxFilletRadius: undefined };
|
|
452
|
+
return { centers: [], hexagons: [], shortestDistEdge: undefined, longestDistEdge: undefined, maxFilletRadius: undefined };
|
|
458
453
|
}
|
|
459
|
-
// --- Generate Unscaled Regular Hexagon Vertices (Radius = 1) ---
|
|
460
454
|
const unscaledHexagons = unscaledCenters.map(center => this.getRegularHexagonVertices(center, BASE_RADIUS));
|
|
461
|
-
// --- Determine Dimensions of the Unscaled Grid Bounding Box ---
|
|
462
455
|
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
|
|
463
456
|
for (const hex of unscaledHexagons) {
|
|
464
457
|
for (const vertex of hex) {
|
|
@@ -474,30 +467,21 @@ export class Point {
|
|
|
474
467
|
}
|
|
475
468
|
const unscaledWidth = maxX - minX;
|
|
476
469
|
const unscaledHeight = maxY - minY;
|
|
477
|
-
// --- Step 4: Calculate Scaling Factors ---
|
|
478
|
-
// Handle potential zero dimensions if only 1 hex (W/H would be based on hex size)
|
|
479
470
|
const scaleX = (unscaledWidth > 1e-9) ? width / unscaledWidth : 1;
|
|
480
471
|
const scaleY = (unscaledHeight > 1e-9) ? height / unscaledHeight : 1;
|
|
481
|
-
// If unscaled W/H is 0 (e.g., 1x1 grid), scale=1 means the final hex will have
|
|
482
|
-
// width/height derived from its regular R=1 shape, not fitting totalW/H.
|
|
483
|
-
// This might need adjustment if a single hex *must* fill the total W/H.
|
|
484
|
-
// For now, assume nrU/nrV > 1 or accept R=1 size for single hex.
|
|
485
|
-
// --- Scale Centers and Vertices ---
|
|
486
|
-
// Scale relative to the min corner of the unscaled grid (minX, minY)
|
|
487
472
|
let scaledCenters = unscaledCenters.map(p => [
|
|
488
473
|
(p[0] - minX) * scaleX,
|
|
489
474
|
(p[1] - minY) * scaleY,
|
|
490
|
-
0
|
|
475
|
+
0
|
|
491
476
|
]);
|
|
492
477
|
let scaledHexagons = unscaledHexagons.map(hex => hex.map(v => [
|
|
493
478
|
(v[0] - minX) * scaleX,
|
|
494
479
|
(v[1] - minY) * scaleY,
|
|
495
|
-
0
|
|
480
|
+
0
|
|
496
481
|
]));
|
|
497
482
|
let shortestDistEdge = Infinity;
|
|
498
483
|
let longestDistEdge = -Infinity;
|
|
499
484
|
let maxFilletRadius = 0;
|
|
500
|
-
// --- Calculate Shortes/Longest & Extensions ---
|
|
501
485
|
if (scaledHexagons.length !== 0) {
|
|
502
486
|
const firstHex = scaledHexagons[0];
|
|
503
487
|
maxFilletRadius = this.safestPointsMaxFilletHalfLine({
|
|
@@ -505,7 +489,6 @@ export class Point {
|
|
|
505
489
|
checkLastWithFirst: true,
|
|
506
490
|
tolerance: 1e-7
|
|
507
491
|
});
|
|
508
|
-
// Calculate the shortest and longest edge distances
|
|
509
492
|
firstHex.forEach((pt, index) => {
|
|
510
493
|
const nextPt = firstHex[(index + 1) % firstHex.length];
|
|
511
494
|
const dist = this.distance({ startPoint: pt, endPoint: nextPt });
|
|
@@ -602,7 +585,6 @@ export class Point {
|
|
|
602
585
|
}
|
|
603
586
|
}
|
|
604
587
|
if (flatTop) {
|
|
605
|
-
// width and height are swapped
|
|
606
588
|
scaledCenters = this.rotatePointsCenterAxis({
|
|
607
589
|
points: scaledCenters,
|
|
608
590
|
center: [width / 2, height / 2, 0],
|
|
@@ -617,7 +599,6 @@ export class Point {
|
|
|
617
599
|
angle: 90
|
|
618
600
|
});
|
|
619
601
|
});
|
|
620
|
-
// translate to new center
|
|
621
602
|
const vecTranslation = this.vector.sub({
|
|
622
603
|
first: [height / 2, width / 2, 0],
|
|
623
604
|
second: [width / 2, height / 2, 0]
|
|
@@ -633,8 +614,6 @@ export class Point {
|
|
|
633
614
|
});
|
|
634
615
|
});
|
|
635
616
|
}
|
|
636
|
-
// --- Apply Optional Centering ---
|
|
637
|
-
// Center the scaled grid (currently starting at [0,0]) around [0,0]
|
|
638
617
|
if (centerGrid) {
|
|
639
618
|
let shiftX = width / 2;
|
|
640
619
|
let shiftY = height / 2;
|
|
@@ -653,7 +632,6 @@ export class Point {
|
|
|
653
632
|
}
|
|
654
633
|
}
|
|
655
634
|
}
|
|
656
|
-
// --- Apply Optional Ground Projection ---
|
|
657
635
|
if (pointsOnGround) {
|
|
658
636
|
for (let i = 0; i < scaledCenters.length; i++) {
|
|
659
637
|
scaledCenters[i] = [scaledCenters[i][0], 0, scaledCenters[i][1]];
|
|
@@ -664,11 +642,10 @@ export class Point {
|
|
|
664
642
|
}
|
|
665
643
|
}
|
|
666
644
|
}
|
|
667
|
-
// We need to adjust orders to be column first and then row first if we choose flat top
|
|
668
645
|
if (flatTop) {
|
|
669
646
|
const grouped = this.lists.groupNth({
|
|
670
647
|
list: scaledHexagons.reverse(),
|
|
671
|
-
nrElements: inputs.nrHexagonsInWidth,
|
|
648
|
+
nrElements: (_j = inputs.nrHexagonsInWidth) !== null && _j !== void 0 ? _j : 10,
|
|
672
649
|
keepRemainder: true,
|
|
673
650
|
});
|
|
674
651
|
const res = this.lists.flipLists({
|
|
@@ -678,7 +655,7 @@ export class Point {
|
|
|
678
655
|
scaledHexagons = res.flat();
|
|
679
656
|
const groupedCenters = this.lists.groupNth({
|
|
680
657
|
list: scaledCenters.reverse(),
|
|
681
|
-
nrElements: inputs.nrHexagonsInWidth,
|
|
658
|
+
nrElements: (_k = inputs.nrHexagonsInWidth) !== null && _k !== void 0 ? _k : 10,
|
|
682
659
|
keepRemainder: true,
|
|
683
660
|
});
|
|
684
661
|
const resCenters = this.lists.flipLists({
|
|
@@ -687,7 +664,6 @@ export class Point {
|
|
|
687
664
|
resCenters.forEach(s => s.reverse());
|
|
688
665
|
scaledCenters = resCenters.flat();
|
|
689
666
|
}
|
|
690
|
-
// --- Return Result ---
|
|
691
667
|
return {
|
|
692
668
|
centers: scaledCenters,
|
|
693
669
|
hexagons: scaledHexagons,
|
|
@@ -720,30 +696,18 @@ export class Point {
|
|
|
720
696
|
if (!normV1 || !normV2) {
|
|
721
697
|
return 0;
|
|
722
698
|
}
|
|
723
|
-
// Calculate the cosine of the angle between the vectors
|
|
724
|
-
// Clamp to [-1, 1] to avoid potential domain errors with acos due to floating point inaccuracies
|
|
725
699
|
const cosAlpha = Math.max(-1.0, Math.min(1.0, this.vector.dot({ first: normV1, second: normV2 })));
|
|
726
|
-
// Check for collinearity
|
|
727
|
-
// If vectors point in the same direction (angle ~ 0), no fillet
|
|
728
700
|
if (cosAlpha > 1.0 - tolerance) {
|
|
729
701
|
return 0;
|
|
730
702
|
}
|
|
731
|
-
// If vectors point in opposite directions (angle ~ 180 deg), no corner for a fillet
|
|
732
703
|
if (cosAlpha < -1.0 + tolerance) {
|
|
733
704
|
return 0;
|
|
734
705
|
}
|
|
735
|
-
// Calculate the angle alpha (0 < alpha < PI)
|
|
736
706
|
const alpha = Math.acos(cosAlpha);
|
|
737
|
-
// Calculate tan(alpha / 2)
|
|
738
|
-
// alpha/2 is between 0 and PI/2, so tan is positive and non-zero
|
|
739
707
|
const tanHalfAlpha = Math.tan(alpha / 2.0);
|
|
740
|
-
// If tanHalfAlpha is extremely small (alpha near 0, shouldn't happen due to collinearity check), return 0
|
|
741
708
|
if (tanHalfAlpha < tolerance) {
|
|
742
709
|
return 0;
|
|
743
710
|
}
|
|
744
|
-
// The distance 'd' from corner C to the tangent point must be less than or equal to the segment lengths.
|
|
745
|
-
// d = r / tan(alpha/2) <= min(len1, len2)
|
|
746
|
-
// r <= min(len1, len2) * tan(alpha/2)
|
|
747
711
|
const maxRadius = Math.min(len1, len2) * tanHalfAlpha;
|
|
748
712
|
return maxRadius;
|
|
749
713
|
}
|
|
@@ -773,17 +737,13 @@ export class Point {
|
|
|
773
737
|
}
|
|
774
738
|
const cosAlpha = Math.max(-1.0, Math.min(1.0, this.vector.dot({ first: normV1, second: normV2 })));
|
|
775
739
|
if (cosAlpha > 1.0 - tolerance || cosAlpha < -1.0 + tolerance) {
|
|
776
|
-
return 0;
|
|
740
|
+
return 0;
|
|
777
741
|
}
|
|
778
742
|
const alpha = Math.acos(cosAlpha);
|
|
779
743
|
const tanHalfAlpha = Math.tan(alpha / 2.0);
|
|
780
744
|
if (tanHalfAlpha < tolerance) {
|
|
781
745
|
return 0;
|
|
782
746
|
}
|
|
783
|
-
// The distance 'd' from corner C to the tangent point must be less than or equal
|
|
784
|
-
// to HALF the length of each segment.
|
|
785
|
-
// d = r / tan(alpha/2) <= min(len1 / 2, len2 / 2)
|
|
786
|
-
// r <= min(len1 / 2, len2 / 2) * tan(alpha/2)
|
|
787
747
|
const maxRadius = Math.min(len1 / 2.0, len2 / 2.0) * tanHalfAlpha;
|
|
788
748
|
return maxRadius;
|
|
789
749
|
}
|
|
@@ -801,17 +761,13 @@ export class Point {
|
|
|
801
761
|
const { points, checkLastWithFirst = false, tolerance = 1e-7 } = inputs;
|
|
802
762
|
const n = points.length;
|
|
803
763
|
const results = [];
|
|
804
|
-
// Need at least 3 points to form a corner
|
|
805
764
|
if (n < 3) {
|
|
806
765
|
return results;
|
|
807
766
|
}
|
|
808
|
-
// 1. Calculate fillets for internal corners (P[1] to P[n-2])
|
|
809
767
|
for (let i = 1; i < n - 1; i++) {
|
|
810
768
|
const p_prev = points[i - 1];
|
|
811
769
|
const p_corner = points[i];
|
|
812
770
|
const p_next = points[i + 1];
|
|
813
|
-
// Map geometric points to the DTO structure used by calculateMaxFilletRadiusHalfLine
|
|
814
|
-
// DTO: { start: P_prev, center: P_next, end: P_corner, tolerance }
|
|
815
771
|
const cornerInput = {
|
|
816
772
|
start: p_prev,
|
|
817
773
|
center: p_next,
|
|
@@ -820,10 +776,8 @@ export class Point {
|
|
|
820
776
|
};
|
|
821
777
|
results.push(this.maxFilletRadiusHalfLine(cornerInput));
|
|
822
778
|
}
|
|
823
|
-
// 2. Calculate fillets for closing corners if it's a closed polyline
|
|
824
779
|
if (checkLastWithFirst && n >= 3) {
|
|
825
|
-
|
|
826
|
-
const p_prev_start = points[n - 1]; // Previous point is the last point
|
|
780
|
+
const p_prev_start = points[n - 1];
|
|
827
781
|
const p_corner_start = points[0];
|
|
828
782
|
const p_next_start = points[1];
|
|
829
783
|
const startCornerInput = {
|
|
@@ -833,10 +787,9 @@ export class Point {
|
|
|
833
787
|
tolerance: tolerance
|
|
834
788
|
};
|
|
835
789
|
results.push(this.maxFilletRadiusHalfLine(startCornerInput));
|
|
836
|
-
// Corner at P[n-1] (formed by P[n-2]-P[n-1] and P[0]-P[n-1])
|
|
837
790
|
const p_prev_end = points[n - 2];
|
|
838
791
|
const p_corner_end = points[n - 1];
|
|
839
|
-
const p_next_end = points[0];
|
|
792
|
+
const p_next_end = points[0];
|
|
840
793
|
const endCornerInput = {
|
|
841
794
|
start: p_prev_end,
|
|
842
795
|
center: p_next_end,
|
|
@@ -863,13 +816,9 @@ export class Point {
|
|
|
863
816
|
safestPointsMaxFilletHalfLine(inputs) {
|
|
864
817
|
const allMaxRadii = this.maxFilletsHalfLine(inputs);
|
|
865
818
|
if (allMaxRadii.length === 0) {
|
|
866
|
-
// No corners, or fewer than 3 points. No fillet possible.
|
|
867
819
|
return 0;
|
|
868
820
|
}
|
|
869
|
-
// Find the minimum radius among all calculated maximums.
|
|
870
|
-
// If any corner calculation resulted in 0, the safest radius is 0.
|
|
871
821
|
const safestRadius = Math.min(...allMaxRadii);
|
|
872
|
-
// Ensure we don't return a negative radius if Math.min had weird input (shouldn't happen here)
|
|
873
822
|
return Math.max(0, safestRadius);
|
|
874
823
|
}
|
|
875
824
|
/**
|
|
@@ -900,24 +849,18 @@ export class Point {
|
|
|
900
849
|
if (!p1 || !p2 || !p3 || p1.length !== 3 || p2.length !== 3 || p3.length !== 3) {
|
|
901
850
|
throw new Error("All points must be arrays of 3 numbers [x, y, z]");
|
|
902
851
|
}
|
|
903
|
-
// Calculate vector A = p2 - p1
|
|
904
852
|
const ax = p2[0] - p1[0];
|
|
905
853
|
const ay = p2[1] - p1[1];
|
|
906
854
|
const az = p2[2] - p1[2];
|
|
907
|
-
// Calculate vector B = p3 - p1
|
|
908
855
|
const bx = p3[0] - p1[0];
|
|
909
856
|
const by = p3[1] - p1[1];
|
|
910
857
|
const bz = p3[2] - p1[2];
|
|
911
|
-
// Calculate the cross product N = A x B
|
|
912
858
|
let nx = (ay * bz) - (az * by);
|
|
913
859
|
let ny = (az * bx) - (ax * bz);
|
|
914
860
|
let nz = (ax * by) - (ay * bx);
|
|
915
|
-
// Check for collinear points (resulting in a zero vector)
|
|
916
|
-
// A zero vector indicates the points don't form a unique plane.
|
|
917
|
-
// You might want to handle this case depending on your application.
|
|
918
861
|
if (nx === 0 && ny === 0 && nz === 0) {
|
|
919
862
|
console.warn("Points are collinear or coincident; cannot calculate a unique normal.");
|
|
920
|
-
return undefined;
|
|
863
|
+
return undefined;
|
|
921
864
|
}
|
|
922
865
|
if (inputs.reverseNormal) {
|
|
923
866
|
nx = -nx;
|
|
@@ -951,10 +894,11 @@ export class Point {
|
|
|
951
894
|
* @drawable false
|
|
952
895
|
*/
|
|
953
896
|
twoPointsAlmostEqual(inputs) {
|
|
897
|
+
var _a;
|
|
954
898
|
const p1 = inputs.point1;
|
|
955
899
|
const p2 = inputs.point2;
|
|
956
900
|
const dist = this.distance({ startPoint: p1, endPoint: p2 });
|
|
957
|
-
return dist < inputs.tolerance;
|
|
901
|
+
return dist < ((_a = inputs.tolerance) !== null && _a !== void 0 ? _a : 1e-7);
|
|
958
902
|
}
|
|
959
903
|
/**
|
|
960
904
|
* Sorts points lexicographically (by X, then Y, then Z coordinates).
|
|
@@ -991,7 +935,7 @@ export class Point {
|
|
|
991
935
|
vertices.push([
|
|
992
936
|
cx + radius * Math.sin(angle),
|
|
993
937
|
cy + radius * Math.cos(angle),
|
|
994
|
-
cz
|
|
938
|
+
cz
|
|
995
939
|
]);
|
|
996
940
|
}
|
|
997
941
|
return vertices;
|