@bitbybit-dev/base 1.3.2-rc.1 → 1.4.0-rc.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/README.md +1 -1
- package/lib/api/GlobalCDNProvider.js +1 -1
- package/lib/api/index.d.ts +2 -0
- package/lib/api/index.js +2 -0
- package/lib/api/inputs/base-inputs.d.ts +10 -4
- package/lib/api/inputs/color-inputs.d.ts +24 -23
- package/lib/api/inputs/color-inputs.js +1 -0
- package/lib/api/inputs/dates-inputs.d.ts +16 -16
- package/lib/api/inputs/frame-inputs.d.ts +515 -0
- package/lib/api/inputs/frame-inputs.js +617 -0
- package/lib/api/inputs/index.d.ts +1 -0
- package/lib/api/inputs/index.js +1 -0
- package/lib/api/inputs/io-inputs.d.ts +3 -3
- package/lib/api/inputs/io-inputs.js +1 -1
- package/lib/api/inputs/lists-inputs.d.ts +23 -23
- package/lib/api/inputs/logic-inputs.d.ts +11 -11
- package/lib/api/inputs/math-inputs.d.ts +49 -49
- package/lib/api/inputs/math-inputs.js +4 -4
- package/lib/api/inputs/mesh-inputs.d.ts +3 -3
- package/lib/api/inputs/mesh-inputs.js +3 -3
- package/lib/api/inputs/point-inputs.d.ts +39 -38
- package/lib/api/inputs/point-inputs.js +6 -5
- package/lib/api/inputs/polyline-inputs.d.ts +5 -4
- package/lib/api/inputs/polyline-inputs.js +3 -3
- package/lib/api/inputs/text-inputs.d.ts +34 -33
- package/lib/api/inputs/text-inputs.js +15 -12
- package/lib/api/inputs/transforms-inputs.d.ts +16 -16
- package/lib/api/inputs/vector-inputs.d.ts +26 -25
- package/lib/api/inputs/vector-inputs.js +2 -1
- package/lib/api/kernel-calls/call-by-path.d.ts +12 -0
- package/lib/api/kernel-calls/call-by-path.js +36 -0
- package/lib/api/kernel-calls/constraints.d.ts +53 -0
- package/lib/api/kernel-calls/constraints.js +21 -0
- package/lib/api/kernel-calls/describe-failure.d.ts +32 -0
- package/lib/api/kernel-calls/describe-failure.js +164 -0
- package/lib/api/kernel-calls/errors.d.ts +55 -0
- package/lib/api/kernel-calls/errors.js +42 -0
- package/lib/api/kernel-calls/failure-message.d.ts +18 -0
- package/lib/api/kernel-calls/failure-message.js +36 -0
- package/lib/api/kernel-calls/index.d.ts +10 -0
- package/lib/api/kernel-calls/index.js +10 -0
- package/lib/api/kernel-calls/input-rules.d.ts +67 -0
- package/lib/api/kernel-calls/input-rules.js +135 -0
- package/lib/api/kernel-calls/input-validation.d.ts +130 -0
- package/lib/api/kernel-calls/input-validation.js +221 -0
- package/lib/api/kernel-calls/rehydrate.d.ts +16 -0
- package/lib/api/kernel-calls/rehydrate.js +38 -0
- package/lib/api/kernel-calls/resolve-dto.d.ts +59 -0
- package/lib/api/kernel-calls/resolve-dto.js +61 -0
- package/lib/api/kernel-calls/with-defaults.d.ts +16 -0
- package/lib/api/kernel-calls/with-defaults.js +53 -0
- package/lib/api/resolved-inputs/index.d.ts +304 -0
- package/lib/api/resolved-inputs/index.js +1 -0
- package/lib/api/services/color.js +43 -26
- package/lib/api/services/dates.js +53 -33
- package/lib/api/services/frame.d.ts +670 -0
- package/lib/api/services/frame.js +1040 -0
- package/lib/api/services/helpers/dxf/dxf-generator.d.ts +2 -3
- package/lib/api/services/helpers/dxf/dxf-generator.js +27 -37
- package/lib/api/services/helpers/dxf/dxf.js +10 -4
- package/lib/api/services/helpers/frame-axes.d.ts +36 -0
- package/lib/api/services/helpers/frame-axes.js +57 -0
- package/lib/api/services/helpers/matrices.d.ts +17 -0
- package/lib/api/services/helpers/matrices.js +51 -0
- package/lib/api/services/index.d.ts +1 -0
- package/lib/api/services/index.js +1 -0
- package/lib/api/services/line.js +15 -12
- package/lib/api/services/lists.js +174 -136
- package/lib/api/services/logic.js +44 -34
- package/lib/api/services/math.js +127 -82
- package/lib/api/services/mesh.js +20 -13
- package/lib/api/services/point.js +62 -46
- package/lib/api/services/polyline.js +22 -18
- package/lib/api/services/text.js +81 -46
- package/lib/api/services/transforms.js +38 -25
- package/lib/api/services/vector.js +37 -22
- package/package.json +1 -1
|
@@ -108,9 +108,8 @@ export declare class DxfGenerator {
|
|
|
108
108
|
*/
|
|
109
109
|
private getNextHandle;
|
|
110
110
|
/**
|
|
111
|
-
* Convert color to DXF
|
|
112
|
-
*
|
|
113
|
-
* Returns appropriate DXF color codes based on colorFormat setting
|
|
111
|
+
* Convert a hex color (#RRGGBB) to DXF color codes, as the nearest ACI index or as true color
|
|
112
|
+
* depending on the colorFormat setting; anything else is written as ACI 7
|
|
114
113
|
*/
|
|
115
114
|
private convertColorToDxf;
|
|
116
115
|
/**
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import * as Inputs from "../../../inputs";
|
|
2
|
+
import { resolveDto } from "../../../kernel-calls";
|
|
1
3
|
export class DxfGenerator {
|
|
2
4
|
constructor() {
|
|
3
5
|
this.entityHandle = 256;
|
|
@@ -8,13 +10,14 @@ export class DxfGenerator {
|
|
|
8
10
|
* Generate a complete DXF file content from path-based entities
|
|
9
11
|
*/
|
|
10
12
|
generateDxf(dxfInputs) {
|
|
11
|
-
|
|
12
|
-
this.
|
|
13
|
+
const resolved = resolveDto(Inputs.IO.DxfModelDto, dxfInputs);
|
|
14
|
+
this.colorFormat = resolved.colorFormat || "aci";
|
|
15
|
+
this.acadVersion = resolved.acadVersion || "AC1009";
|
|
13
16
|
const dxfContent = [];
|
|
14
17
|
dxfContent.push(...this.generateHeader());
|
|
15
|
-
dxfContent.push(...this.generateTables(
|
|
18
|
+
dxfContent.push(...this.generateTables(resolved));
|
|
16
19
|
dxfContent.push(...this.generateBlocks());
|
|
17
|
-
dxfContent.push(...this.generateEntities(
|
|
20
|
+
dxfContent.push(...this.generateEntities(resolved));
|
|
18
21
|
dxfContent.push("0", "EOF");
|
|
19
22
|
return dxfContent.join("\n");
|
|
20
23
|
}
|
|
@@ -282,7 +285,8 @@ export class DxfGenerator {
|
|
|
282
285
|
generateLayerTable(dxfInputs) {
|
|
283
286
|
const layers = new Set();
|
|
284
287
|
if (dxfInputs.dxfPathsParts) {
|
|
285
|
-
dxfInputs.dxfPathsParts.forEach(
|
|
288
|
+
dxfInputs.dxfPathsParts.forEach(pathsPart => {
|
|
289
|
+
const part = resolveDto(Inputs.IO.DxfPathsPartDto, pathsPart);
|
|
286
290
|
if (part.layer) {
|
|
287
291
|
layers.add(part.layer);
|
|
288
292
|
}
|
|
@@ -323,7 +327,8 @@ export class DxfGenerator {
|
|
|
323
327
|
"ENTITIES"
|
|
324
328
|
];
|
|
325
329
|
if (dxfInputs.dxfPathsParts) {
|
|
326
|
-
dxfInputs.dxfPathsParts.forEach(
|
|
330
|
+
dxfInputs.dxfPathsParts.forEach(pathsPart => {
|
|
331
|
+
const part = resolveDto(Inputs.IO.DxfPathsPartDto, pathsPart);
|
|
327
332
|
if (part.paths) {
|
|
328
333
|
part.paths.forEach(path => {
|
|
329
334
|
if (path.segments) {
|
|
@@ -399,10 +404,8 @@ export class DxfGenerator {
|
|
|
399
404
|
"8",
|
|
400
405
|
part.layer || "0"
|
|
401
406
|
];
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
colorCodes.forEach(cc => entity.push(cc.code, cc.value));
|
|
405
|
-
}
|
|
407
|
+
const colorCodes = this.convertColorToDxf(part.color);
|
|
408
|
+
colorCodes.forEach(cc => entity.push(cc.code, cc.value));
|
|
406
409
|
entity.push("10", line.start[0].toFixed(6), "20", line.start[1].toFixed(6), "30", "0.00", "11", line.end[0].toFixed(6), "21", line.end[1].toFixed(6), "31", "0.00");
|
|
407
410
|
if (this.acadVersion === "AC1015") {
|
|
408
411
|
entity.splice(2, 0, "5", this.getNextHandle(), "100", "AcDbEntity");
|
|
@@ -421,10 +424,8 @@ export class DxfGenerator {
|
|
|
421
424
|
"8",
|
|
422
425
|
part.layer || "0"
|
|
423
426
|
];
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
colorCodes.forEach(cc => entity.push(cc.code, cc.value));
|
|
427
|
-
}
|
|
427
|
+
const colorCodes = this.convertColorToDxf(part.color);
|
|
428
|
+
colorCodes.forEach(cc => entity.push(cc.code, cc.value));
|
|
428
429
|
entity.push("10", circle.center[0].toFixed(6), "20", circle.center[1].toFixed(6), "30", "0.00", "40", circle.radius.toFixed(6));
|
|
429
430
|
if (this.acadVersion === "AC1015") {
|
|
430
431
|
entity.splice(2, 0, "5", this.getNextHandle(), "100", "AcDbEntity");
|
|
@@ -446,10 +447,8 @@ export class DxfGenerator {
|
|
|
446
447
|
if (this.acadVersion === "AC1009") {
|
|
447
448
|
entity.push("6", " ");
|
|
448
449
|
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
colorCodes.forEach(cc => entity.push(cc.code, cc.value));
|
|
452
|
-
}
|
|
450
|
+
const colorCodes = this.convertColorToDxf(part.color);
|
|
451
|
+
colorCodes.forEach(cc => entity.push(cc.code, cc.value));
|
|
453
452
|
entity.push("10", arc.center[0].toFixed(6), "20", arc.center[1].toFixed(6), "30", this.acadVersion === "AC1009" ? "" : "0.0", "40", arc.radius.toFixed(6), "50", arc.startAngle.toFixed(6), "51", arc.endAngle.toFixed(6));
|
|
454
453
|
if (this.acadVersion === "AC1015") {
|
|
455
454
|
entity.splice(2, 0, "5", this.getNextHandle(), "100", "AcDbEntity");
|
|
@@ -463,17 +462,16 @@ export class DxfGenerator {
|
|
|
463
462
|
/**
|
|
464
463
|
* Generate a LWPOLYLINE entity
|
|
465
464
|
*/
|
|
466
|
-
generatePolylineEntity(
|
|
465
|
+
generatePolylineEntity(segment, part) {
|
|
466
|
+
const polyline = resolveDto(Inputs.IO.DxfPolylineSegmentDto, segment);
|
|
467
467
|
const entity = [
|
|
468
468
|
"0",
|
|
469
469
|
"LWPOLYLINE",
|
|
470
470
|
"8",
|
|
471
471
|
part.layer || "0"
|
|
472
472
|
];
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
colorCodes.forEach(cc => entity.push(cc.code, cc.value));
|
|
476
|
-
}
|
|
473
|
+
const colorCodes = this.convertColorToDxf(part.color);
|
|
474
|
+
colorCodes.forEach(cc => entity.push(cc.code, cc.value));
|
|
477
475
|
const isClosed = polyline.closed || (polyline.points.length > 2 && this.isClosedPolyline(polyline.points));
|
|
478
476
|
entity.push("90", polyline.points.length.toString(), "70", isClosed ? "1" : "0");
|
|
479
477
|
if (this.acadVersion === "AC1015") {
|
|
@@ -495,17 +493,16 @@ export class DxfGenerator {
|
|
|
495
493
|
/**
|
|
496
494
|
* Generate a SPLINE entity
|
|
497
495
|
*/
|
|
498
|
-
generateSplineEntity(
|
|
496
|
+
generateSplineEntity(segment, part) {
|
|
497
|
+
const spline = resolveDto(Inputs.IO.DxfSplineSegmentDto, segment);
|
|
499
498
|
const entity = [
|
|
500
499
|
"0",
|
|
501
500
|
"SPLINE",
|
|
502
501
|
"8",
|
|
503
502
|
part.layer || "0"
|
|
504
503
|
];
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
colorCodes.forEach(cc => entity.push(cc.code, cc.value));
|
|
508
|
-
}
|
|
504
|
+
const colorCodes = this.convertColorToDxf(part.color);
|
|
505
|
+
colorCodes.forEach(cc => entity.push(cc.code, cc.value));
|
|
509
506
|
const degree = spline.degree || 3;
|
|
510
507
|
const numControlPoints = spline.controlPoints.length;
|
|
511
508
|
const numKnots = numControlPoints + degree + 1;
|
|
@@ -542,17 +539,10 @@ export class DxfGenerator {
|
|
|
542
539
|
return (++this.entityHandle).toString(16).toUpperCase();
|
|
543
540
|
}
|
|
544
541
|
/**
|
|
545
|
-
* Convert color to DXF
|
|
546
|
-
*
|
|
547
|
-
* Returns appropriate DXF color codes based on colorFormat setting
|
|
542
|
+
* Convert a hex color (#RRGGBB) to DXF color codes, as the nearest ACI index or as true color
|
|
543
|
+
* depending on the colorFormat setting; anything else is written as ACI 7
|
|
548
544
|
*/
|
|
549
545
|
convertColorToDxf(color) {
|
|
550
|
-
if (/^\d+$/.test(color)) {
|
|
551
|
-
const colorIndex = parseInt(color, 10);
|
|
552
|
-
if (colorIndex >= 1 && colorIndex <= 255) {
|
|
553
|
-
return [{ code: "62", value: color }];
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
546
|
if (color.startsWith("#")) {
|
|
557
547
|
const hex = color.substring(1);
|
|
558
548
|
if (hex.length === 6) {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import * as Inputs from "../../../inputs";
|
|
1
2
|
import { DxfGenerator } from "./dxf-generator";
|
|
3
|
+
import { resolveDto } from "../../../kernel-calls";
|
|
2
4
|
export class Dxf {
|
|
3
5
|
constructor() {
|
|
4
6
|
this.dxfGenerator = new DxfGenerator();
|
|
@@ -49,7 +51,8 @@ export class Dxf {
|
|
|
49
51
|
* @drawable false
|
|
50
52
|
*/
|
|
51
53
|
polylineSegment(inputs) {
|
|
52
|
-
|
|
54
|
+
const resolved = resolveDto(Inputs.IO.DxfPolylineSegmentDto, inputs);
|
|
55
|
+
return resolved;
|
|
53
56
|
}
|
|
54
57
|
/**
|
|
55
58
|
* Creates a spline segment definition for DXF export (smooth curve through control points).
|
|
@@ -61,7 +64,8 @@ export class Dxf {
|
|
|
61
64
|
* @drawable false
|
|
62
65
|
*/
|
|
63
66
|
splineSegment(inputs) {
|
|
64
|
-
|
|
67
|
+
const resolved = resolveDto(Inputs.IO.DxfSplineSegmentDto, inputs);
|
|
68
|
+
return resolved;
|
|
65
69
|
}
|
|
66
70
|
/**
|
|
67
71
|
* Creates a path from multiple segments (combines lines, arcs, circles, polylines, splines).
|
|
@@ -87,7 +91,8 @@ export class Dxf {
|
|
|
87
91
|
* @drawable false
|
|
88
92
|
*/
|
|
89
93
|
pathsPart(inputs) {
|
|
90
|
-
|
|
94
|
+
const resolved = resolveDto(Inputs.IO.DxfPathsPartDto, inputs);
|
|
95
|
+
return resolved;
|
|
91
96
|
}
|
|
92
97
|
/**
|
|
93
98
|
* Generates a complete DXF file from paths parts (exports 2D CAD drawing format).
|
|
@@ -100,6 +105,7 @@ export class Dxf {
|
|
|
100
105
|
* @drawable false
|
|
101
106
|
*/
|
|
102
107
|
dxfCreate(inputs) {
|
|
103
|
-
|
|
108
|
+
const resolved = resolveDto(Inputs.IO.DxfModelDto, inputs);
|
|
109
|
+
return this.dxfGenerator.generateDxf(resolved);
|
|
104
110
|
}
|
|
105
111
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as Inputs from "../../inputs";
|
|
2
|
+
type Vec3 = Inputs.Base.Vector3;
|
|
3
|
+
/**
|
|
4
|
+
* The sine below which two directions count as parallel wherever a frame is squared: a direction
|
|
5
|
+
* closer than this to its normal leaves the X axis to rounding.
|
|
6
|
+
*/
|
|
7
|
+
export declare const PARALLEL_SINE = 1e-9;
|
|
8
|
+
/**
|
|
9
|
+
* A frame as the four vectors the maths reads: the origin and three unit axes at right angles,
|
|
10
|
+
* with `y` the cross product of `z` and `x`.
|
|
11
|
+
*/
|
|
12
|
+
export interface FrameAxes {
|
|
13
|
+
readonly origin: Vec3;
|
|
14
|
+
readonly x: Vec3;
|
|
15
|
+
readonly y: Vec3;
|
|
16
|
+
readonly z: Vec3;
|
|
17
|
+
}
|
|
18
|
+
/** Whether a value is three finite numbers. */
|
|
19
|
+
export declare const isTriple: (value: unknown) => value is Vec3;
|
|
20
|
+
/** Whether a value has the shape of a frame: an `origin`, a `normal` and a `direction`, three finite numbers each. */
|
|
21
|
+
export declare const isFrameShaped: (value: unknown) => value is Inputs.Base.Frame;
|
|
22
|
+
/**
|
|
23
|
+
* A vector scaled to length 1, or undefined for one of no length. It is divided by its largest
|
|
24
|
+
* component before it is measured, so no square overflows or underflows at any finite size.
|
|
25
|
+
*/
|
|
26
|
+
export declare const unitOf: (vector: Vec3) => Vec3 | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* The axes of an origin, a normal and a rough X direction: the normal scaled to length 1 and the
|
|
29
|
+
* direction turned square to it. Gives the name of the part that cannot be squared instead: a
|
|
30
|
+
* normal of no length, or a direction of no length or within `PARALLEL_SINE` of the normal.
|
|
31
|
+
*
|
|
32
|
+
* The direction is projected twice: a direction close to the normal loses most of its digits to
|
|
33
|
+
* the first subtraction, and the second takes out what rounding left along the normal.
|
|
34
|
+
*/
|
|
35
|
+
export declare const squareFrame: (origin: Vec3, normal: Vec3, direction: Vec3) => FrameAxes | "normal" | "direction";
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sine below which two directions count as parallel wherever a frame is squared: a direction
|
|
3
|
+
* closer than this to its normal leaves the X axis to rounding.
|
|
4
|
+
*/
|
|
5
|
+
export const PARALLEL_SINE = 1e-9;
|
|
6
|
+
/** Whether a value is three finite numbers. */
|
|
7
|
+
export const isTriple = (value) => Array.isArray(value) && value.length === 3 && value.every(n => typeof n === "number" && Number.isFinite(n));
|
|
8
|
+
/** Whether a value has the shape of a frame: an `origin`, a `normal` and a `direction`, three finite numbers each. */
|
|
9
|
+
export const isFrameShaped = (value) => {
|
|
10
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const fields = value;
|
|
14
|
+
return isTriple(fields["origin"]) && isTriple(fields["normal"]) && isTriple(fields["direction"]);
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* A vector scaled to length 1, or undefined for one of no length. It is divided by its largest
|
|
18
|
+
* component before it is measured, so no square overflows or underflows at any finite size.
|
|
19
|
+
*/
|
|
20
|
+
export const unitOf = (vector) => {
|
|
21
|
+
const largest = Math.max(Math.abs(vector[0]), Math.abs(vector[1]), Math.abs(vector[2]));
|
|
22
|
+
if (!(largest > 0 && largest < Infinity)) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
const scaled = [vector[0] / largest, vector[1] / largest, vector[2] / largest];
|
|
26
|
+
const length = Math.hypot(scaled[0], scaled[1], scaled[2]);
|
|
27
|
+
return [scaled[0] / length, scaled[1] / length, scaled[2] / length];
|
|
28
|
+
};
|
|
29
|
+
const withoutPartAlong = (vector, unit) => {
|
|
30
|
+
const along = vector[0] * unit[0] + vector[1] * unit[1] + vector[2] * unit[2];
|
|
31
|
+
return [vector[0] - along * unit[0], vector[1] - along * unit[1], vector[2] - along * unit[2]];
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* The axes of an origin, a normal and a rough X direction: the normal scaled to length 1 and the
|
|
35
|
+
* direction turned square to it. Gives the name of the part that cannot be squared instead: a
|
|
36
|
+
* normal of no length, or a direction of no length or within `PARALLEL_SINE` of the normal.
|
|
37
|
+
*
|
|
38
|
+
* The direction is projected twice: a direction close to the normal loses most of its digits to
|
|
39
|
+
* the first subtraction, and the second takes out what rounding left along the normal.
|
|
40
|
+
*/
|
|
41
|
+
export const squareFrame = (origin, normal, direction) => {
|
|
42
|
+
const z = unitOf(normal);
|
|
43
|
+
if (z === undefined) {
|
|
44
|
+
return "normal";
|
|
45
|
+
}
|
|
46
|
+
const unitDirection = unitOf(direction);
|
|
47
|
+
if (unitDirection === undefined) {
|
|
48
|
+
return "direction";
|
|
49
|
+
}
|
|
50
|
+
const square = withoutPartAlong(unitDirection, z);
|
|
51
|
+
if (!(Math.hypot(square[0], square[1], square[2]) > PARALLEL_SINE)) {
|
|
52
|
+
return "direction";
|
|
53
|
+
}
|
|
54
|
+
const x = unitOf(withoutPartAlong(square, z));
|
|
55
|
+
const y = [z[1] * x[2] - z[2] * x[1], z[2] * x[0] - z[0] * x[2], z[0] * x[1] - z[1] * x[0]];
|
|
56
|
+
return { origin: [origin[0], origin[1], origin[2]], x, y, z };
|
|
57
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as Inputs from "../../inputs";
|
|
2
|
+
type Vec3 = Inputs.Base.Vector3;
|
|
3
|
+
/** Whether a value is one column-major 4 x 4 matrix: sixteen finite numbers. */
|
|
4
|
+
export declare const isTransformMatrix: (value: unknown) => value is Inputs.Base.TransformMatrix;
|
|
5
|
+
/** The product of two column-major 4 x 4 matrices: `first` applied, then `second`. */
|
|
6
|
+
export declare const followedBy: (first: readonly number[], second: readonly number[]) => Inputs.Base.TransformMatrix;
|
|
7
|
+
/** One column-major 4 x 4 matrix that applies a list of them first to last; the identity for an empty list. */
|
|
8
|
+
export declare const composed: (matrices: readonly (readonly number[])[]) => Inputs.Base.TransformMatrix;
|
|
9
|
+
/**
|
|
10
|
+
* The eigenvalues of a symmetric 3 x 3 matrix, largest first, each with its unit eigenvector, by
|
|
11
|
+
* cyclic Jacobi rotations; the vectors come out at right angles to each other.
|
|
12
|
+
*/
|
|
13
|
+
export declare const symmetricEigen: (matrix: readonly (readonly number[])[]) => {
|
|
14
|
+
value: number;
|
|
15
|
+
vector: Vec3;
|
|
16
|
+
}[];
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** Whether a value is one column-major 4 x 4 matrix: sixteen finite numbers. */
|
|
2
|
+
export const isTransformMatrix = (value) => Array.isArray(value) && value.length === 16 && value.every(n => typeof n === "number" && Number.isFinite(n));
|
|
3
|
+
/** The product of two column-major 4 x 4 matrices: `first` applied, then `second`. */
|
|
4
|
+
export const followedBy = (first, second) => {
|
|
5
|
+
const product = Array.from({ length: 16 }, (_, index) => {
|
|
6
|
+
const column = Math.floor(index / 4);
|
|
7
|
+
const row = index % 4;
|
|
8
|
+
return second[row] * first[column * 4] + second[4 + row] * first[column * 4 + 1]
|
|
9
|
+
+ second[8 + row] * first[column * 4 + 2] + second[12 + row] * first[column * 4 + 3];
|
|
10
|
+
});
|
|
11
|
+
return product;
|
|
12
|
+
};
|
|
13
|
+
/** One column-major 4 x 4 matrix that applies a list of them first to last; the identity for an empty list. */
|
|
14
|
+
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
|
+
/**
|
|
16
|
+
* The eigenvalues of a symmetric 3 x 3 matrix, largest first, each with its unit eigenvector, by
|
|
17
|
+
* cyclic Jacobi rotations; the vectors come out at right angles to each other.
|
|
18
|
+
*/
|
|
19
|
+
export const symmetricEigen = (matrix) => {
|
|
20
|
+
let a = matrix.map(row => [...row]);
|
|
21
|
+
let v = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
|
|
22
|
+
const multiply = (p, q) => p.map((row, i) => q[0].map((_, j) => row.reduce((total, _unused, k) => total + p[i][k] * q[k][j], 0)));
|
|
23
|
+
const transpose = (p) => p[0].map((_, j) => p.map(row => row[j]));
|
|
24
|
+
const size = a.flat().reduce((total, entry) => total + entry * entry, 0);
|
|
25
|
+
for (let sweep = 0; sweep < 64; sweep++) {
|
|
26
|
+
const off = Math.pow(a[0][1], 2) + Math.pow(a[0][2], 2) + Math.pow(a[1][2], 2);
|
|
27
|
+
if (off <= 1e-30 * size) {
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
for (const [p, q] of [[0, 1], [0, 2], [1, 2]]) {
|
|
31
|
+
const apq = a[p][q];
|
|
32
|
+
if (apq === 0) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const theta = (a[q][q] - a[p][p]) / (2 * apq);
|
|
36
|
+
const t = (theta >= 0 ? 1 : -1) / (Math.abs(theta) + Math.sqrt(theta * theta + 1));
|
|
37
|
+
const c = 1 / Math.sqrt(t * t + 1);
|
|
38
|
+
const s = t * c;
|
|
39
|
+
const rotation = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
|
|
40
|
+
rotation[p][p] = c;
|
|
41
|
+
rotation[q][q] = c;
|
|
42
|
+
rotation[p][q] = s;
|
|
43
|
+
rotation[q][p] = -s;
|
|
44
|
+
a = multiply(transpose(rotation), multiply(a, rotation));
|
|
45
|
+
v = multiply(v, rotation);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return [0, 1, 2]
|
|
49
|
+
.map(k => ({ value: a[k][k], vector: [v[0][k], v[1][k], v[2][k]] }))
|
|
50
|
+
.sort((first, second) => second.value - first.value);
|
|
51
|
+
};
|
package/lib/api/services/line.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import * as Inputs from "../inputs";
|
|
2
|
+
import { resolveDto } from "../kernel-calls";
|
|
1
3
|
/**
|
|
2
4
|
* Straight lines between two points, held as plain objects of the form `{ start, end }`, and the
|
|
3
5
|
* segment form `[start, end]` of the same thing. The methods here build lines, measure and
|
|
@@ -175,10 +177,10 @@ export class Line {
|
|
|
175
177
|
* ```
|
|
176
178
|
*/
|
|
177
179
|
getPointOnLine(inputs) {
|
|
178
|
-
|
|
179
|
-
const point1 =
|
|
180
|
-
const point2 =
|
|
181
|
-
const parameter =
|
|
180
|
+
const resolved = resolveDto(Inputs.Line.PointOnLineDto, inputs);
|
|
181
|
+
const point1 = resolved.line.start;
|
|
182
|
+
const point2 = resolved.line.end;
|
|
183
|
+
const parameter = resolved.param;
|
|
182
184
|
const direction = [point2[0] - point1[0], point2[1] - point1[1], point2[2] - point1[2]];
|
|
183
185
|
const point = [point1[0] + parameter * direction[0], point1[1] + parameter * direction[1], point1[2] + parameter * direction[2]];
|
|
184
186
|
return point;
|
|
@@ -304,10 +306,11 @@ export class Line {
|
|
|
304
306
|
* ```
|
|
305
307
|
*/
|
|
306
308
|
lineLineIntersection(inputs) {
|
|
307
|
-
const
|
|
308
|
-
const
|
|
309
|
-
const
|
|
310
|
-
const
|
|
309
|
+
const resolved = resolveDto(Inputs.Line.LineLineIntersectionDto, inputs);
|
|
310
|
+
const epsilon = resolved.tolerance;
|
|
311
|
+
const checkSegments = resolved.checkSegmentsOnly;
|
|
312
|
+
const line1 = resolved.line1;
|
|
313
|
+
const line2 = resolved.line2;
|
|
311
314
|
if (!(line1 === null || line1 === void 0 ? void 0 : line1.start) || !line1.end || !(line2 === null || line2 === void 0 ? void 0 : line2.start) || !line2.end ||
|
|
312
315
|
line1.start.length !== 3 || line1.end.length !== 3 ||
|
|
313
316
|
line2.start.length !== 3 || line2.end.length !== 3) {
|
|
@@ -321,16 +324,16 @@ export class Line {
|
|
|
321
324
|
const p21 = this.vector.sub({ first: p2, second: p1 });
|
|
322
325
|
const lenSq1 = this.vector.lengthSq({ vector: d1 });
|
|
323
326
|
const lenSq2 = this.vector.lengthSq({ vector: d2 });
|
|
324
|
-
if (lenSq1
|
|
327
|
+
if (lenSq1 <= epsilon * epsilon || lenSq2 <= epsilon * epsilon) {
|
|
325
328
|
return undefined;
|
|
326
329
|
}
|
|
327
330
|
const d1_cross_d2 = this.vector.cross({ first: d1, second: d2 });
|
|
328
331
|
const crossMagSq = this.vector.lengthSq({ vector: d1_cross_d2 });
|
|
329
332
|
const parallel_tolerance_sq = epsilon * epsilon;
|
|
330
|
-
if (crossMagSq
|
|
333
|
+
if (crossMagSq <= parallel_tolerance_sq) {
|
|
331
334
|
const p21_cross_d1 = this.vector.cross({ first: p21, second: d1 });
|
|
332
335
|
const collinear_tolerance_sq = epsilon * epsilon * lenSq1;
|
|
333
|
-
if (this.vector.lengthSq({ vector: p21_cross_d1 })
|
|
336
|
+
if (this.vector.lengthSq({ vector: p21_cross_d1 }) <= collinear_tolerance_sq) {
|
|
334
337
|
if (!checkSegments) {
|
|
335
338
|
return p1;
|
|
336
339
|
}
|
|
@@ -368,7 +371,7 @@ export class Line {
|
|
|
368
371
|
const d1p21 = this.vector.dot({ first: d1, second: p21 });
|
|
369
372
|
const d2p21 = this.vector.dot({ first: d2, second: p21 });
|
|
370
373
|
const denominator = d1d1 * d2d2 - d1d2 * d1d2;
|
|
371
|
-
if (Math.abs(denominator)
|
|
374
|
+
if (Math.abs(denominator) <= epsilon * epsilon) {
|
|
372
375
|
console.error("Internal error: Denominator near zero after non-parallel check.");
|
|
373
376
|
return undefined;
|
|
374
377
|
}
|