@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
|
@@ -0,0 +1,1040 @@
|
|
|
1
|
+
import * as Inputs from "../inputs";
|
|
2
|
+
import { InputError, resolveDto } from "../kernel-calls";
|
|
3
|
+
import { isFrameShaped, isTriple, PARALLEL_SINE, squareFrame, unitOf } from "./helpers/frame-axes";
|
|
4
|
+
import { composed, symmetricEigen } from "./helpers/matrices";
|
|
5
|
+
const WORLD = { origin: [0, 0, 0], x: [1, 0, 0], y: [0, 1, 0], z: [0, 0, 1] };
|
|
6
|
+
/** The largest absolute coordinate in a list of vectors, by a loop, so a long list cannot overflow the call stack. */
|
|
7
|
+
const reachOf = (vectors) => {
|
|
8
|
+
let reach = 0;
|
|
9
|
+
for (const vector of vectors) {
|
|
10
|
+
reach = Math.max(reach, Math.abs(vector[0]), Math.abs(vector[1]), Math.abs(vector[2]));
|
|
11
|
+
}
|
|
12
|
+
return reach;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Frames: a frame is a point with three axes at right angles, written as its `origin`, its `normal`
|
|
16
|
+
* (the Z axis) and its `direction` (the X axis), the Y axis following from those two. Frames place
|
|
17
|
+
* things: a shape lands on one, points convert between one and the world, and frame patterns lay
|
|
18
|
+
* out copies. Every method returns new values and never changes its inputs.
|
|
19
|
+
*/
|
|
20
|
+
export class Frame {
|
|
21
|
+
constructor(vector, math, geometryHelper) {
|
|
22
|
+
this.vector = vector;
|
|
23
|
+
this.math = math;
|
|
24
|
+
this.geometryHelper = geometryHelper;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Builds a frame from where it sits, where its Z axis points and roughly where its X axis
|
|
28
|
+
* points.
|
|
29
|
+
*
|
|
30
|
+
* `direction` is turned square to `normal` and both are scaled to length 1, so they only have
|
|
31
|
+
* to be roughly right. A `normal` of zero length or a `direction` along it throws.
|
|
32
|
+
* @param inputs - The origin, the normal and the rough X direction
|
|
33
|
+
* @returns A new frame
|
|
34
|
+
* @group create
|
|
35
|
+
* @shortname frame
|
|
36
|
+
* @drawable true
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const tilted = bitbybit.frame.create({ origin: [0, 5, 0], normal: [0, 1, 1], direction: [1, 0, 0] });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
create(inputs) {
|
|
43
|
+
const resolved = resolveDto(Inputs.Frame.CreateFrameDto, inputs);
|
|
44
|
+
const origin = this.pointOf(resolved.origin, "origin");
|
|
45
|
+
const normal = this.vectorOf(resolved.normal, "normal");
|
|
46
|
+
const direction = this.vectorOf(resolved.direction, "direction");
|
|
47
|
+
return this.frameOf(this.squared(origin, normal, direction, {
|
|
48
|
+
normal: "`normal` has no length, so it gives the Z axis no direction.",
|
|
49
|
+
direction: "`direction` runs along `normal` or has no length, so it does not fix the X axis.",
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Gives the world frame: its origin at zero, its X, Y and Z axes along the world's.
|
|
54
|
+
*
|
|
55
|
+
* Placing something on the world frame leaves it where it is, and it is the frame the other
|
|
56
|
+
* methods use when one is left out.
|
|
57
|
+
* @returns The world frame
|
|
58
|
+
* @group create
|
|
59
|
+
* @shortname world
|
|
60
|
+
* @drawable true
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const world = bitbybit.frame.world();
|
|
64
|
+
* const raised = bitbybit.frame.offset({ frame: world, distance: 5 });
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
world() {
|
|
68
|
+
return this.frameOf(WORLD);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Builds a frame lying in the XY plane: its X axis along the world X, its Y axis along the
|
|
72
|
+
* world Y and its normal along the world Z.
|
|
73
|
+
* @param inputs - Where the frame sits
|
|
74
|
+
* @returns A new frame
|
|
75
|
+
* @group create
|
|
76
|
+
* @shortname xy
|
|
77
|
+
* @drawable true
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const floor = bitbybit.frame.xy({ origin: [0, 0, 2] });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
xy(inputs) {
|
|
84
|
+
const resolved = resolveDto(Inputs.Frame.OriginDto, inputs);
|
|
85
|
+
return this.frameOf(Object.assign(Object.assign({}, WORLD), { origin: this.pointOf(resolved.origin, "origin") }));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Builds a frame lying in the YZ plane: its X axis along the world Y, its Y axis along the
|
|
89
|
+
* world Z and its normal along the world X.
|
|
90
|
+
* @param inputs - Where the frame sits
|
|
91
|
+
* @returns A new frame
|
|
92
|
+
* @group create
|
|
93
|
+
* @shortname yz
|
|
94
|
+
* @drawable true
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const side = bitbybit.frame.yz({ origin: [3, 0, 0] });
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
yz(inputs) {
|
|
101
|
+
const resolved = resolveDto(Inputs.Frame.OriginDto, inputs);
|
|
102
|
+
return this.frameOf({ origin: this.pointOf(resolved.origin, "origin"), x: [0, 1, 0], y: [0, 0, 1], z: [1, 0, 0] });
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Builds a frame lying in the ZX plane, the ground when Y points up: its X axis along the world
|
|
106
|
+
* Z, its Y axis along the world X and its normal along the world Y.
|
|
107
|
+
* @param inputs - Where the frame sits
|
|
108
|
+
* @returns A new frame
|
|
109
|
+
* @group create
|
|
110
|
+
* @shortname zx
|
|
111
|
+
* @drawable true
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const ground = bitbybit.frame.zx({ origin: [0, 0, 0] });
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
zx(inputs) {
|
|
118
|
+
const resolved = resolveDto(Inputs.Frame.OriginDto, inputs);
|
|
119
|
+
return this.frameOf({ origin: this.pointOf(resolved.origin, "origin"), x: [0, 0, 1], y: [1, 0, 0], z: [0, 1, 0] });
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Builds a frame from three points: where it sits, a point its X axis runs toward and a point
|
|
123
|
+
* on the side its Y axis points to.
|
|
124
|
+
*
|
|
125
|
+
* The normal follows by the right-hand rule. `xPoint` at `origin`, or three points on one line,
|
|
126
|
+
* throws.
|
|
127
|
+
* @param inputs - The origin, a point along the X axis and a point in the plane
|
|
128
|
+
* @returns A new frame
|
|
129
|
+
* @group create
|
|
130
|
+
* @shortname from three points
|
|
131
|
+
* @drawable true
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const frame = bitbybit.frame.fromThreePoints({ origin: [0, 0, 0], xPoint: [4, 0, 0], planePoint: [0, 0, -3] });
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
fromThreePoints(inputs) {
|
|
138
|
+
const resolved = resolveDto(Inputs.Frame.ThreePointsDto, inputs);
|
|
139
|
+
const origin = this.pointOf(resolved.origin, "origin");
|
|
140
|
+
const toX = unitOf(this.vector.sub({ first: this.pointOf(resolved.xPoint, "xPoint"), second: origin }));
|
|
141
|
+
if (toX === undefined) {
|
|
142
|
+
throw new InputError("`xPoint` is at `origin`, so it gives the X axis no direction.", "xPoint");
|
|
143
|
+
}
|
|
144
|
+
const toPlane = unitOf(this.vector.sub({ first: this.pointOf(resolved.planePoint, "planePoint"), second: origin }));
|
|
145
|
+
const normal = toPlane === undefined ? undefined : this.vector.cross({ first: toX, second: toPlane });
|
|
146
|
+
if (normal === undefined || !(this.vector.length({ vector: normal }) > PARALLEL_SINE)) {
|
|
147
|
+
throw new InputError("`planePoint` lies on the line through `origin` and `xPoint`, or within a billionth of a radian of it, so the three points do not fix a plane.", "planePoint");
|
|
148
|
+
}
|
|
149
|
+
return this.frameOf(this.squared(origin, normal, toX, { normal: "The three points do not fix a plane.", direction: "The three points do not fix a plane.", property: "planePoint" }));
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Builds a frame from a point and a normal, choosing its X axis by a fixed rule.
|
|
153
|
+
*
|
|
154
|
+
* The same normal always gives the same X axis: square to the normal and to the world axis the
|
|
155
|
+
* normal leans on least. A normal of zero length throws.
|
|
156
|
+
* @param inputs - The origin and the normal
|
|
157
|
+
* @returns A new frame
|
|
158
|
+
* @group create
|
|
159
|
+
* @shortname from point and normal
|
|
160
|
+
* @drawable true
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* const onSlope = bitbybit.frame.fromPointAndNormal({ origin: [2, 1, 0], normal: [0, 1, 1] });
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
fromPointAndNormal(inputs) {
|
|
167
|
+
const resolved = resolveDto(Inputs.Frame.PointAndNormalDto, inputs);
|
|
168
|
+
const origin = this.pointOf(resolved.origin, "origin");
|
|
169
|
+
const normal = this.vectorOf(resolved.normal, "normal");
|
|
170
|
+
const [a, b, c] = normal;
|
|
171
|
+
const [aAbs, bAbs, cAbs] = [Math.abs(a), Math.abs(b), Math.abs(c)];
|
|
172
|
+
let direction;
|
|
173
|
+
if (bAbs <= aAbs && bAbs <= cAbs) {
|
|
174
|
+
direction = aAbs > cAbs ? [-c, 0, a] : [c, 0, -a];
|
|
175
|
+
}
|
|
176
|
+
else if (aAbs <= bAbs && aAbs <= cAbs) {
|
|
177
|
+
direction = bAbs > cAbs ? [0, -c, b] : [0, c, -b];
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
direction = aAbs > bAbs ? [-b, a, 0] : [b, -a, 0];
|
|
181
|
+
}
|
|
182
|
+
return this.frameOf(this.squared(origin, normal, direction, {
|
|
183
|
+
normal: "`normal` has no length, so it gives the Z axis no direction.",
|
|
184
|
+
direction: "`normal` does not fix a frame.",
|
|
185
|
+
property: "normal",
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Fits a frame to points that lie on or near one plane.
|
|
190
|
+
*
|
|
191
|
+
* It sits at their average. The normal follows their order by the right-hand rule, and X runs
|
|
192
|
+
* along their widest spread, toward the first point; with no widest spread, as around a square,
|
|
193
|
+
* X points at the first point. Fewer than three points, or nearly collinear ones, throw.
|
|
194
|
+
* @param inputs - The points to fit
|
|
195
|
+
* @returns A new frame in the plane that fits the points best
|
|
196
|
+
* @group create
|
|
197
|
+
* @shortname best fit
|
|
198
|
+
* @drawable true
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* const frame = bitbybit.frame.bestFit({ points: [[0, 0, 0], [4, 0.1, 0], [4, 3, 0.1], [0, 3, 0]] });
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
bestFit(inputs) {
|
|
205
|
+
var _a;
|
|
206
|
+
const points = this.pointsOf(inputs.points, "points");
|
|
207
|
+
if (points.length < 3) {
|
|
208
|
+
throw new InputError("`points` holds fewer than three points, so no plane fits them.", "points");
|
|
209
|
+
}
|
|
210
|
+
const center = points.reduce((total, point) => this.vector.add({ first: total, second: this.vector.mul({ vector: point, scalar: 1 / points.length }) }), [0, 0, 0]);
|
|
211
|
+
const reach = reachOf(points.map(point => this.vector.sub({ first: point, second: center })));
|
|
212
|
+
if (!(reach > 0 && reach < Infinity)) {
|
|
213
|
+
throw new InputError("`points` all lie at one point, so no plane fits them.", "points");
|
|
214
|
+
}
|
|
215
|
+
const offsets = points.map(point => this.vector.mul({ vector: this.vector.sub({ first: point, second: center }), scalar: 1 / reach }));
|
|
216
|
+
const covariance = [0, 1, 2].map(i => [0, 1, 2].map(j => offsets.reduce((total, offset) => total + offset[i] * offset[j], 0)));
|
|
217
|
+
const [widest, middle, flattest] = symmetricEigen(covariance);
|
|
218
|
+
if (!(middle.value > 1e-12 * widest.value)) {
|
|
219
|
+
throw new InputError("`points` lie on one line, or so near one that their spread across it is under a millionth of their spread along it, so no plane fits them reliably.", "points");
|
|
220
|
+
}
|
|
221
|
+
const spread = offsets.reduce((total, offset) => total + this.vector.dot({ first: offset, second: offset }), 0);
|
|
222
|
+
const turning = offsets.reduce((total, offset, i) => this.vector.add({ first: total, second: this.vector.cross({ first: offset, second: offsets[(i + 1) % offsets.length] }) }), [0, 0, 0]);
|
|
223
|
+
const turn = this.vector.dot({ first: turning, second: flattest.vector });
|
|
224
|
+
const normal = Math.abs(turn) > 1e-9 * spread ? (turn < 0 ? this.vector.neg({ vector: flattest.vector }) : flattest.vector) : this.withLargestPositive(flattest.vector);
|
|
225
|
+
const direction = widest.value - middle.value > 1e-6 * widest.value
|
|
226
|
+
? this.alongWidest(widest.vector, offsets[0], spread)
|
|
227
|
+
: (_a = this.towardFirstPoint(offsets, normal, spread)) !== null && _a !== void 0 ? _a : widest.vector;
|
|
228
|
+
return this.frameOf(this.squared(center, normal, direction, { normal: "`points` do not fix a plane.", direction: "`points` do not fix a plane.", property: "points" }));
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Reads where a frame sits: the point its three axes start from.
|
|
232
|
+
* @param inputs - The frame to read
|
|
233
|
+
* @returns The origin
|
|
234
|
+
* @group read
|
|
235
|
+
* @shortname origin
|
|
236
|
+
* @drawable true
|
|
237
|
+
*/
|
|
238
|
+
origin(inputs) {
|
|
239
|
+
return [...this.axesOf(inputs.frame, "frame").origin];
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Reads where a frame's Z axis points, as a vector of length 1.
|
|
243
|
+
* @param inputs - The frame to read
|
|
244
|
+
* @returns The normal
|
|
245
|
+
* @group read
|
|
246
|
+
* @shortname normal
|
|
247
|
+
* @drawable false
|
|
248
|
+
*/
|
|
249
|
+
normal(inputs) {
|
|
250
|
+
return [...this.axesOf(inputs.frame, "frame").z];
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Reads where a frame's X axis points, as a vector of length 1 square to the normal.
|
|
254
|
+
* @param inputs - The frame to read
|
|
255
|
+
* @returns The X direction
|
|
256
|
+
* @group read
|
|
257
|
+
* @shortname direction
|
|
258
|
+
* @drawable false
|
|
259
|
+
*/
|
|
260
|
+
direction(inputs) {
|
|
261
|
+
return [...this.axesOf(inputs.frame, "frame").x];
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Reads where a frame's Y axis points: the normal crossed with the direction, as a vector of
|
|
265
|
+
* length 1.
|
|
266
|
+
* @param inputs - The frame to read
|
|
267
|
+
* @returns The Y direction
|
|
268
|
+
* @group read
|
|
269
|
+
* @shortname y direction
|
|
270
|
+
* @drawable false
|
|
271
|
+
*/
|
|
272
|
+
yDirection(inputs) {
|
|
273
|
+
return [...this.axesOf(inputs.frame, "frame").y];
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Moves a frame by a vector in world coordinates, keeping the directions of its axes.
|
|
277
|
+
* @param inputs - The frame and the vector to move it by
|
|
278
|
+
* @returns A new frame
|
|
279
|
+
* @group change
|
|
280
|
+
* @shortname translate
|
|
281
|
+
* @drawable true
|
|
282
|
+
* @example
|
|
283
|
+
* ```typescript
|
|
284
|
+
* const moved = bitbybit.frame.translate({ frame: bitbybit.frame.world(), translation: [2, 0, 1] });
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
translate(inputs) {
|
|
288
|
+
const resolved = resolveDto(Inputs.Frame.TranslateDto, inputs);
|
|
289
|
+
const axes = this.axesOf(resolved.frame, "frame");
|
|
290
|
+
return this.translated(axes, this.vectorOf(resolved.translation, "translation"));
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Moves frames by one vector in world coordinates, as `translate` moves one; their axes keep
|
|
294
|
+
* their directions.
|
|
295
|
+
* @param inputs - The frames and the vector to move them by
|
|
296
|
+
* @returns New frames, in the same order
|
|
297
|
+
* @group change
|
|
298
|
+
* @shortname translate frames
|
|
299
|
+
* @drawable true
|
|
300
|
+
* @example
|
|
301
|
+
* ```typescript
|
|
302
|
+
* const floor = bitbybit.frame.grid({ countX: 3, countY: 3, spacingX: 2, spacingY: 2, centered: true });
|
|
303
|
+
* const ceiling = bitbybit.frame.translateFrames({ frames: floor, translation: [0, 0, 3] });
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
306
|
+
translateFrames(inputs) {
|
|
307
|
+
const resolved = resolveDto(Inputs.Frame.TranslateFramesDto, inputs);
|
|
308
|
+
const frames = this.axesOfEach(resolved.frames, "frames");
|
|
309
|
+
const translation = this.vectorOf(resolved.translation, "translation");
|
|
310
|
+
return frames.map(axes => this.translated(axes, translation));
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Moves a frame along its own normal, keeping the directions of its axes.
|
|
314
|
+
*
|
|
315
|
+
* A negative distance moves it against the normal.
|
|
316
|
+
* @param inputs - The frame and the distance in model units
|
|
317
|
+
* @returns A new frame
|
|
318
|
+
* @group change
|
|
319
|
+
* @shortname offset
|
|
320
|
+
* @drawable true
|
|
321
|
+
* @example
|
|
322
|
+
* ```typescript
|
|
323
|
+
* const lid = bitbybit.frame.offset({ frame: bitbybit.frame.zx({ origin: [0, 0, 0] }), distance: 10 });
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
offset(inputs) {
|
|
327
|
+
const resolved = resolveDto(Inputs.Frame.OffsetDto, inputs);
|
|
328
|
+
const axes = this.axesOf(resolved.frame, "frame");
|
|
329
|
+
return this.offsetBy(axes, this.numberOf(resolved.distance, "distance"));
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Moves each frame along its own normal by the same distance, as `offset` moves one; their
|
|
333
|
+
* axes keep their directions.
|
|
334
|
+
*
|
|
335
|
+
* Frames facing different ways move different ways: a negative distance moves each against its
|
|
336
|
+
* own normal.
|
|
337
|
+
* @param inputs - The frames and the distance in model units
|
|
338
|
+
* @returns New frames, in the same order
|
|
339
|
+
* @group change
|
|
340
|
+
* @shortname offset frames
|
|
341
|
+
* @drawable true
|
|
342
|
+
* @example
|
|
343
|
+
* ```typescript
|
|
344
|
+
* const ring = bitbybit.frame.polar({ count: 8, radius: 5, angle: 360, startAngle: 0, rotate: true });
|
|
345
|
+
* const raised = bitbybit.frame.offsetFrames({ frames: ring, distance: 2 });
|
|
346
|
+
* ```
|
|
347
|
+
*/
|
|
348
|
+
offsetFrames(inputs) {
|
|
349
|
+
const resolved = resolveDto(Inputs.Frame.OffsetFramesDto, inputs);
|
|
350
|
+
const frames = this.axesOfEach(resolved.frames, "frames");
|
|
351
|
+
const distance = this.numberOf(resolved.distance, "distance");
|
|
352
|
+
return frames.map(axes => this.offsetBy(axes, distance));
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Turns a frame about one of its own axes, through its origin.
|
|
356
|
+
*
|
|
357
|
+
* The angle is in degrees; positive is counter-clockwise when that axis points toward you.
|
|
358
|
+
* @param inputs - The frame, the axis to turn about and the angle
|
|
359
|
+
* @returns A new frame
|
|
360
|
+
* @group change
|
|
361
|
+
* @shortname rotate
|
|
362
|
+
* @drawable true
|
|
363
|
+
* @example
|
|
364
|
+
* ```typescript
|
|
365
|
+
* const tilted = bitbybit.frame.rotate({ frame: bitbybit.frame.world(), axis: Bit.Inputs.Frame.frameAxisEnum.x, angle: 30 });
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
rotate(inputs) {
|
|
369
|
+
const resolved = resolveDto(Inputs.Frame.RotateDto, inputs);
|
|
370
|
+
const axes = this.axesOf(resolved.frame, "frame");
|
|
371
|
+
return this.turned(axes, this.axisOf(resolved.axis), this.math.degToRad({ number: this.numberOf(resolved.angle, "angle") }));
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Turns each frame about one of its own axes, through its own origin, as `rotate` turns one.
|
|
375
|
+
*
|
|
376
|
+
* The angle is in degrees; positive is counter-clockwise when that axis points toward you.
|
|
377
|
+
* @param inputs - The frames, the axis to turn each about and the angle
|
|
378
|
+
* @returns New frames, in the same order
|
|
379
|
+
* @group change
|
|
380
|
+
* @shortname rotate frames
|
|
381
|
+
* @drawable true
|
|
382
|
+
* @example
|
|
383
|
+
* ```typescript
|
|
384
|
+
* const row = bitbybit.frame.grid({ countX: 4, countY: 1, spacingX: 2, spacingY: 2, centered: true });
|
|
385
|
+
* const tilted = bitbybit.frame.rotateFrames({ frames: row, axis: Bit.Inputs.Frame.frameAxisEnum.x, angle: 30 });
|
|
386
|
+
* ```
|
|
387
|
+
*/
|
|
388
|
+
rotateFrames(inputs) {
|
|
389
|
+
const resolved = resolveDto(Inputs.Frame.RotateFramesDto, inputs);
|
|
390
|
+
const frames = this.axesOfEach(resolved.frames, "frames");
|
|
391
|
+
const axis = this.axisOf(resolved.axis);
|
|
392
|
+
const angle = this.math.degToRad({ number: this.numberOf(resolved.angle, "angle") });
|
|
393
|
+
return frames.map(axes => this.turned(axes, axis, angle));
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Turns a frame over: its normal points the other way and its Y axis with it, its origin and X
|
|
397
|
+
* axis stay.
|
|
398
|
+
* @param inputs - The frame to turn over
|
|
399
|
+
* @returns A new frame
|
|
400
|
+
* @group change
|
|
401
|
+
* @shortname flip
|
|
402
|
+
* @drawable true
|
|
403
|
+
* @example
|
|
404
|
+
* ```typescript
|
|
405
|
+
* const underside = bitbybit.frame.flip({ frame: bitbybit.frame.zx({ origin: [0, 0, 0] }) });
|
|
406
|
+
* ```
|
|
407
|
+
*/
|
|
408
|
+
flip(inputs) {
|
|
409
|
+
return this.flipped(this.axesOf(inputs.frame, "frame"));
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Turns each frame over, as `flip` turns one: its normal and Y axis point the other way, its
|
|
413
|
+
* origin and X axis stay.
|
|
414
|
+
* @param inputs - The frames to turn over
|
|
415
|
+
* @returns New frames, in the same order
|
|
416
|
+
* @group change
|
|
417
|
+
* @shortname flip frames
|
|
418
|
+
* @drawable true
|
|
419
|
+
* @example
|
|
420
|
+
* ```typescript
|
|
421
|
+
* const tops = bitbybit.frame.grid({ countX: 2, countY: 2, spacingX: 3, spacingY: 3, centered: true });
|
|
422
|
+
* const bottoms = bitbybit.frame.flipFrames({ frames: tops });
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
flipFrames(inputs) {
|
|
426
|
+
return this.axesOfEach(inputs.frames, "frames").map(axes => this.flipped(axes));
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Places a frame given in another frame's coordinates into the world: a frame within a frame.
|
|
430
|
+
*
|
|
431
|
+
* `child` is read as if `parent` were the world, so a `child` at the origin sits on `parent`
|
|
432
|
+
* and one lifted along Z rises along the parent's normal.
|
|
433
|
+
* @param inputs - The parent frame and the child frame given in its coordinates
|
|
434
|
+
* @returns The child frame in world coordinates
|
|
435
|
+
* @group frame in frame
|
|
436
|
+
* @shortname frame to world
|
|
437
|
+
* @drawable true
|
|
438
|
+
* @example
|
|
439
|
+
* ```typescript
|
|
440
|
+
* const table = bitbybit.frame.zx({ origin: [0, 1, 0] });
|
|
441
|
+
* const onTable = bitbybit.frame.frameToWorld({ parent: table, child: bitbybit.frame.xy({ origin: [0.5, 0.2, 0] }) });
|
|
442
|
+
* ```
|
|
443
|
+
*/
|
|
444
|
+
frameToWorld(inputs) {
|
|
445
|
+
const parent = this.axesOf(inputs.parent, "parent");
|
|
446
|
+
return this.childToWorld(parent, this.axesOf(inputs.child, "child"));
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Places frames given in one parent frame's coordinates into the world, as `frameToWorld`
|
|
450
|
+
* places one: every child sits on `parent` as it would sit on the world.
|
|
451
|
+
* @param inputs - The parent frame and the frames given in its coordinates
|
|
452
|
+
* @returns The frames in world coordinates, in the same order
|
|
453
|
+
* @group frame in frame
|
|
454
|
+
* @shortname frames to world
|
|
455
|
+
* @drawable true
|
|
456
|
+
* @example
|
|
457
|
+
* ```typescript
|
|
458
|
+
* const table = bitbybit.frame.zx({ origin: [0, 1, 0] });
|
|
459
|
+
* const spots = bitbybit.frame.grid({ countX: 3, countY: 2, spacingX: 0.4, spacingY: 0.4, centered: true });
|
|
460
|
+
* const onTable = bitbybit.frame.framesToWorld({ parent: table, children: spots });
|
|
461
|
+
* ```
|
|
462
|
+
*/
|
|
463
|
+
framesToWorld(inputs) {
|
|
464
|
+
const parent = this.axesOf(inputs.parent, "parent");
|
|
465
|
+
return this.axesOfEach(inputs.children, "children").map(child => this.childToWorld(parent, child));
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Describes a frame given in world coordinates in another frame's coordinates, the reverse of
|
|
469
|
+
* `frameToWorld`.
|
|
470
|
+
*
|
|
471
|
+
* The result is where `child` sits and how it turns as seen from `parent`, as if `parent` were
|
|
472
|
+
* the world.
|
|
473
|
+
* @param inputs - The parent frame and the child frame in world coordinates
|
|
474
|
+
* @returns The child frame in the parent's coordinates
|
|
475
|
+
* @group frame in frame
|
|
476
|
+
* @shortname frame to local
|
|
477
|
+
* @drawable true
|
|
478
|
+
* @example
|
|
479
|
+
* ```typescript
|
|
480
|
+
* const table = bitbybit.frame.zx({ origin: [0, 1, 0] });
|
|
481
|
+
* const seenFromTable = bitbybit.frame.frameToLocal({ parent: table, child: bitbybit.frame.world() });
|
|
482
|
+
* ```
|
|
483
|
+
*/
|
|
484
|
+
frameToLocal(inputs) {
|
|
485
|
+
const parent = this.axesOf(inputs.parent, "parent");
|
|
486
|
+
return this.childToLocal(parent, this.axesOf(inputs.child, "child"));
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Describes frames given in world coordinates in one parent frame's coordinates, the reverse
|
|
490
|
+
* of `framesToWorld`, as `frameToLocal` describes one.
|
|
491
|
+
* @param inputs - The parent frame and the frames in world coordinates
|
|
492
|
+
* @returns The frames in the parent's coordinates, in the same order
|
|
493
|
+
* @group frame in frame
|
|
494
|
+
* @shortname frames to local
|
|
495
|
+
* @drawable true
|
|
496
|
+
* @example
|
|
497
|
+
* ```typescript
|
|
498
|
+
* const table = bitbybit.frame.zx({ origin: [0, 1, 0] });
|
|
499
|
+
* const seen = bitbybit.frame.framesToLocal({ parent: table, children: [bitbybit.frame.world(), bitbybit.frame.yz({ origin: [2, 0, 0] })] });
|
|
500
|
+
* ```
|
|
501
|
+
*/
|
|
502
|
+
framesToLocal(inputs) {
|
|
503
|
+
const parent = this.axesOf(inputs.parent, "parent");
|
|
504
|
+
return this.axesOfEach(inputs.children, "children").map(child => this.childToLocal(parent, child));
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Converts a point given in a frame's coordinates into world coordinates.
|
|
508
|
+
*
|
|
509
|
+
* `[1, 2, 3]` becomes the point 1 along the frame's X axis, 2 along its Y axis and 3 along its
|
|
510
|
+
* normal, from its origin.
|
|
511
|
+
* @param inputs - The frame and the point in its coordinates
|
|
512
|
+
* @returns The point in world coordinates
|
|
513
|
+
* @group coordinates
|
|
514
|
+
* @shortname point to world
|
|
515
|
+
* @drawable true
|
|
516
|
+
* @example
|
|
517
|
+
* ```typescript
|
|
518
|
+
* const corner = bitbybit.frame.pointToWorld({ frame: bitbybit.frame.zx({ origin: [0, 2, 0] }), point: [1, 1, 0] });
|
|
519
|
+
* ```
|
|
520
|
+
*/
|
|
521
|
+
pointToWorld(inputs) {
|
|
522
|
+
const axes = this.axesOf(inputs.frame, "frame");
|
|
523
|
+
return this.vector.add({ first: axes.origin, second: this.along(axes, ...this.pointOf(inputs.point, "point")) });
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Converts a point given in world coordinates into a frame's coordinates, the reverse of
|
|
527
|
+
* `pointToWorld`.
|
|
528
|
+
*
|
|
529
|
+
* The third coordinate of the result is the point's signed distance from the frame's plane.
|
|
530
|
+
* @param inputs - The frame and the point in world coordinates
|
|
531
|
+
* @returns The point in the frame's coordinates
|
|
532
|
+
* @group coordinates
|
|
533
|
+
* @shortname point to local
|
|
534
|
+
* @drawable true
|
|
535
|
+
* @example
|
|
536
|
+
* ```typescript
|
|
537
|
+
* const local = bitbybit.frame.pointToLocal({ frame: bitbybit.frame.zx({ origin: [0, 2, 0] }), point: [1, 5, 1] });
|
|
538
|
+
* ```
|
|
539
|
+
*/
|
|
540
|
+
pointToLocal(inputs) {
|
|
541
|
+
const axes = this.axesOf(inputs.frame, "frame");
|
|
542
|
+
return this.against(axes, this.vector.sub({ first: this.pointOf(inputs.point, "point"), second: axes.origin }));
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Converts points given in a frame's coordinates into world coordinates, as `pointToWorld` does
|
|
546
|
+
* for one.
|
|
547
|
+
* @param inputs - The frame and the points in its coordinates
|
|
548
|
+
* @returns The points in world coordinates, in the same order
|
|
549
|
+
* @group coordinates
|
|
550
|
+
* @shortname points to world
|
|
551
|
+
* @drawable true
|
|
552
|
+
* @example
|
|
553
|
+
* ```typescript
|
|
554
|
+
* const square = bitbybit.frame.pointsToWorld({ frame: bitbybit.frame.yz({ origin: [3, 0, 0] }), points: [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]] });
|
|
555
|
+
* ```
|
|
556
|
+
*/
|
|
557
|
+
pointsToWorld(inputs) {
|
|
558
|
+
const axes = this.axesOf(inputs.frame, "frame");
|
|
559
|
+
return this.pointsOf(inputs.points, "points").map(point => this.vector.add({ first: axes.origin, second: this.along(axes, ...point) }));
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Converts points given in world coordinates into a frame's coordinates, as `pointToLocal` does
|
|
563
|
+
* for one.
|
|
564
|
+
* @param inputs - The frame and the points in world coordinates
|
|
565
|
+
* @returns The points in the frame's coordinates, in the same order
|
|
566
|
+
* @group coordinates
|
|
567
|
+
* @shortname points to local
|
|
568
|
+
* @drawable true
|
|
569
|
+
* @example
|
|
570
|
+
* ```typescript
|
|
571
|
+
* const flat = bitbybit.frame.pointsToLocal({ frame: bitbybit.frame.yz({ origin: [3, 0, 0] }), points: [[3, 0, 0], [3, 1, 0], [3, 1, 1]] });
|
|
572
|
+
* ```
|
|
573
|
+
*/
|
|
574
|
+
pointsToLocal(inputs) {
|
|
575
|
+
const axes = this.axesOf(inputs.frame, "frame");
|
|
576
|
+
return this.pointsOf(inputs.points, "points").map(point => this.against(axes, this.vector.sub({ first: point, second: axes.origin })));
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Turns a vector given along a frame's axes into one along the world axes; the frame's origin
|
|
580
|
+
* plays no part.
|
|
581
|
+
* @param inputs - The frame and the vector along its axes
|
|
582
|
+
* @returns The vector along the world axes
|
|
583
|
+
* @group coordinates
|
|
584
|
+
* @shortname vector to world
|
|
585
|
+
* @drawable false
|
|
586
|
+
* @example
|
|
587
|
+
* ```typescript
|
|
588
|
+
* const up = bitbybit.frame.vectorToWorld({ frame: bitbybit.frame.zx({ origin: [0, 0, 0] }), vector: [0, 0, 1] });
|
|
589
|
+
* ```
|
|
590
|
+
*/
|
|
591
|
+
vectorToWorld(inputs) {
|
|
592
|
+
return this.along(this.axesOf(inputs.frame, "frame"), ...this.vectorOf(inputs.vector, "vector"));
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Turns a vector given along the world axes into one along a frame's axes, the reverse of
|
|
596
|
+
* `vectorToWorld`.
|
|
597
|
+
* @param inputs - The frame and the vector along the world axes
|
|
598
|
+
* @returns The vector along the frame's axes
|
|
599
|
+
* @group coordinates
|
|
600
|
+
* @shortname vector to local
|
|
601
|
+
* @drawable false
|
|
602
|
+
* @example
|
|
603
|
+
* ```typescript
|
|
604
|
+
* const local = bitbybit.frame.vectorToLocal({ frame: bitbybit.frame.zx({ origin: [0, 0, 0] }), vector: [0, 1, 0] });
|
|
605
|
+
* ```
|
|
606
|
+
*/
|
|
607
|
+
vectorToLocal(inputs) {
|
|
608
|
+
return this.against(this.axesOf(inputs.frame, "frame"), this.vectorOf(inputs.vector, "vector"));
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Gives the transformation that moves anything from the world frame onto a frame.
|
|
612
|
+
*
|
|
613
|
+
* It takes the world origin to the frame's origin and the world axes to the frame's axes, and
|
|
614
|
+
* comes as a list holding one matrix, the form every transformation input takes.
|
|
615
|
+
* @param inputs - The frame
|
|
616
|
+
* @returns The transformation onto the frame
|
|
617
|
+
* @group matrices
|
|
618
|
+
* @shortname to matrix
|
|
619
|
+
* @drawable false
|
|
620
|
+
* @example
|
|
621
|
+
* ```typescript
|
|
622
|
+
* const onFrame = bitbybit.frame.toMatrix({ frame: bitbybit.frame.zx({ origin: [0, 2, 0] }) });
|
|
623
|
+
* const moved = bitbybit.point.transformPoints({ points: [[1, 0, 0]], transformation: onFrame });
|
|
624
|
+
* ```
|
|
625
|
+
*/
|
|
626
|
+
toMatrix(inputs) {
|
|
627
|
+
return [this.matrixBetween(WORLD, this.axesOf(inputs.frame, "frame"))];
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Reads the frame a transformation puts the world frame on: where it moves the world origin
|
|
631
|
+
* and where it turns the world X and Z axes.
|
|
632
|
+
*
|
|
633
|
+
* Scaling is dropped, a shear keeps the Z axis and squares X to it, and a mirror keeps X and Z
|
|
634
|
+
* with the Y axis that follows from them. A perspective part throws.
|
|
635
|
+
* @param inputs - The transformation
|
|
636
|
+
* @returns A new frame
|
|
637
|
+
* @group matrices
|
|
638
|
+
* @shortname from matrix
|
|
639
|
+
* @drawable true
|
|
640
|
+
* @example
|
|
641
|
+
* ```typescript
|
|
642
|
+
* const turned = bitbybit.transforms.rotationCenterAxis({ center: [0, 0, 0], axis: [0, 1, 0], angle: 45 });
|
|
643
|
+
* const frame = bitbybit.frame.fromMatrix({ transformation: turned });
|
|
644
|
+
* ```
|
|
645
|
+
*/
|
|
646
|
+
fromMatrix(inputs) {
|
|
647
|
+
const transformation = inputs.transformation;
|
|
648
|
+
if (!Array.isArray(transformation) || transformation.length === 0) {
|
|
649
|
+
throw new InputError("`transformation` is not a transformation: it needs a matrix of sixteen numbers, or a list of them.", "transformation");
|
|
650
|
+
}
|
|
651
|
+
const matrices = transformation.every(entry => typeof entry === "number")
|
|
652
|
+
? [transformation]
|
|
653
|
+
: transformation;
|
|
654
|
+
const flat = this.geometryHelper.getFlatTransformations(matrices);
|
|
655
|
+
if (!flat.every(matrix => Array.isArray(matrix) && matrix.length === 16 && matrix.every(n => typeof n === "number" && Number.isFinite(n)))) {
|
|
656
|
+
throw new InputError("`transformation` is not a transformation: every matrix needs sixteen finite numbers.", "transformation");
|
|
657
|
+
}
|
|
658
|
+
const projective = flat.findIndex(matrix => Math.abs(matrix[3]) > 1e-9 || Math.abs(matrix[7]) > 1e-9 || Math.abs(matrix[11]) > 1e-9 || Math.abs(matrix[15] - 1) > 1e-9);
|
|
659
|
+
if (projective !== -1) {
|
|
660
|
+
throw new InputError(`\`transformation\` has a perspective part: the bottom row of matrix ${projective} is not 0, 0, 0, 1, so no frame follows from it.`, "transformation");
|
|
661
|
+
}
|
|
662
|
+
const combined = composed(flat);
|
|
663
|
+
const flattened = "`transformation` flattens the X or the Z axis, so no frame follows from it.";
|
|
664
|
+
return this.frameOf(this.squared([combined[12], combined[13], combined[14]], [combined[8], combined[9], combined[10]], [combined[0], combined[1], combined[2]], { normal: flattened, direction: flattened, property: "transformation" }));
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Gives the transformation that carries anything placed on one frame onto another, turning it
|
|
668
|
+
* the same way.
|
|
669
|
+
*
|
|
670
|
+
* Left out, `from` is the world frame, which makes this `toMatrix` of `to`. The result is a list
|
|
671
|
+
* holding one matrix.
|
|
672
|
+
* @param inputs - The frame to move onto and the frame to move from
|
|
673
|
+
* @returns The transformation from `from` onto `to`
|
|
674
|
+
* @group matrices
|
|
675
|
+
* @shortname matrix from to
|
|
676
|
+
* @drawable false
|
|
677
|
+
* @example
|
|
678
|
+
* ```typescript
|
|
679
|
+
* const from = bitbybit.frame.xy({ origin: [0, 0, 0] });
|
|
680
|
+
* const to = bitbybit.frame.zx({ origin: [0, 3, 0] });
|
|
681
|
+
* const carry = bitbybit.frame.matrixFromTo({ to, from });
|
|
682
|
+
* ```
|
|
683
|
+
*/
|
|
684
|
+
matrixFromTo(inputs) {
|
|
685
|
+
const from = inputs.from === undefined ? WORLD : this.axesOf(inputs.from, "from");
|
|
686
|
+
return [this.matrixBetween(from, this.axesOf(inputs.to, "to"))];
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Lays out a rectangular grid of frames in a frame's plane, each turned the same way as that
|
|
690
|
+
* frame.
|
|
691
|
+
*
|
|
692
|
+
* The frames come row by row, along X first. Left out, `frame` is the world frame, so the grid
|
|
693
|
+
* lies in the XY plane.
|
|
694
|
+
* @param inputs - The frame to follow, the counts and spacings, and whether to center the grid
|
|
695
|
+
* @returns The frames of the grid
|
|
696
|
+
* @group patterns
|
|
697
|
+
* @shortname grid
|
|
698
|
+
* @drawable true
|
|
699
|
+
* @example
|
|
700
|
+
* ```typescript
|
|
701
|
+
* const spots = bitbybit.frame.grid({ frame: bitbybit.frame.zx({ origin: [0, 0, 0] }), countX: 4, countY: 3, spacingX: 2, spacingY: 2, centered: true });
|
|
702
|
+
* ```
|
|
703
|
+
*/
|
|
704
|
+
grid(inputs) {
|
|
705
|
+
const resolved = resolveDto(Inputs.Frame.GridDto, inputs);
|
|
706
|
+
const axes = resolved.frame === undefined ? WORLD : this.axesOf(resolved.frame, "frame");
|
|
707
|
+
const countX = this.countOf(resolved.countX, "countX");
|
|
708
|
+
const countY = this.countOf(resolved.countY, "countY");
|
|
709
|
+
const spacingX = this.numberOf(resolved.spacingX, "spacingX");
|
|
710
|
+
const spacingY = this.numberOf(resolved.spacingY, "spacingY");
|
|
711
|
+
const shiftX = resolved.centered ? (countX - 1) * spacingX / 2 : 0;
|
|
712
|
+
const shiftY = resolved.centered ? (countY - 1) * spacingY / 2 : 0;
|
|
713
|
+
const frames = [];
|
|
714
|
+
for (let row = 0; row < countY; row++) {
|
|
715
|
+
for (let column = 0; column < countX; column++) {
|
|
716
|
+
frames.push(this.frameOf(Object.assign(Object.assign({}, axes), { origin: this.vector.add({ first: axes.origin, second: this.along(axes, column * spacingX - shiftX, row * spacingY - shiftY, 0) }) })));
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
return frames;
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Lays out a ring of frames around a frame's normal, in its plane.
|
|
723
|
+
*
|
|
724
|
+
* Each is `frame` turned by its angle about the normal and moved out by `radius` along its
|
|
725
|
+
* turned X axis. A full turn spaces them evenly, a smaller `angle` puts one at each end, and a
|
|
726
|
+
* larger one throws. Left out, `frame` is the world frame.
|
|
727
|
+
* @param inputs - The frame to turn around, the count, radius, angles and whether frames turn
|
|
728
|
+
* @returns The frames of the ring, in the direction of the angle
|
|
729
|
+
* @group patterns
|
|
730
|
+
* @shortname polar
|
|
731
|
+
* @drawable true
|
|
732
|
+
* @example
|
|
733
|
+
* ```typescript
|
|
734
|
+
* const bolts = bitbybit.frame.polar({ frame: bitbybit.frame.zx({ origin: [0, 0, 0] }), count: 8, radius: 5, angle: 360, startAngle: 0, rotate: true });
|
|
735
|
+
* ```
|
|
736
|
+
*/
|
|
737
|
+
polar(inputs) {
|
|
738
|
+
const resolved = resolveDto(Inputs.Frame.PolarDto, inputs);
|
|
739
|
+
const axes = resolved.frame === undefined ? WORLD : this.axesOf(resolved.frame, "frame");
|
|
740
|
+
const count = this.countOf(resolved.count, "count");
|
|
741
|
+
const radius = this.numberOf(resolved.radius, "radius");
|
|
742
|
+
if (radius < 0) {
|
|
743
|
+
throw new InputError("`radius` is negative; a ring needs a radius of zero or more.", "radius");
|
|
744
|
+
}
|
|
745
|
+
const sweep = this.numberOf(resolved.angle, "angle");
|
|
746
|
+
if (Math.abs(sweep) > 360) {
|
|
747
|
+
throw new InputError(`\`angle\` is ${sweep}, beyond a full turn, where frames would land on each other; it must lie within -360 and 360.`, "angle");
|
|
748
|
+
}
|
|
749
|
+
const start = this.numberOf(resolved.startAngle, "startAngle");
|
|
750
|
+
const step = Math.abs(sweep) === 360 ? sweep / count : count > 1 ? sweep / (count - 1) : 0;
|
|
751
|
+
const frames = [];
|
|
752
|
+
for (let i = 0; i < count; i++) {
|
|
753
|
+
const angle = this.math.degToRad({ number: start + i * step });
|
|
754
|
+
const outward = this.combined(axes.x, Math.cos(angle), axes.y, Math.sin(angle));
|
|
755
|
+
const origin = this.vector.add({ first: axes.origin, second: this.vector.mul({ vector: outward, scalar: radius }) });
|
|
756
|
+
frames.push(this.frameOf(resolved.rotate
|
|
757
|
+
? { origin, x: outward, y: this.vector.cross({ first: axes.z, second: outward }), z: axes.z }
|
|
758
|
+
: Object.assign(Object.assign({}, axes), { origin })));
|
|
759
|
+
}
|
|
760
|
+
return frames;
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* Lays out a honeycomb of frames in a frame's plane, one at each hexagon's center, turned as
|
|
764
|
+
* that frame is.
|
|
765
|
+
*
|
|
766
|
+
* The hexagons have corners toward the frame's Y axis and flat sides toward X. Rows run along X,
|
|
767
|
+
* every second shifted by half a hexagon, and frames come row by row. Left out, `frame` is the
|
|
768
|
+
* world frame.
|
|
769
|
+
* @param inputs - The frame to follow, the counts, the hexagon size and whether to center it
|
|
770
|
+
* @returns The frames of the honeycomb
|
|
771
|
+
* @group patterns
|
|
772
|
+
* @shortname hex grid
|
|
773
|
+
* @drawable true
|
|
774
|
+
* @example
|
|
775
|
+
* ```typescript
|
|
776
|
+
* const cells = bitbybit.frame.hexGrid({ frame: bitbybit.frame.zx({ origin: [0, 0, 0] }), countX: 5, countY: 4, radius: 1, centered: true });
|
|
777
|
+
* ```
|
|
778
|
+
*/
|
|
779
|
+
hexGrid(inputs) {
|
|
780
|
+
const resolved = resolveDto(Inputs.Frame.HexGridDto, inputs);
|
|
781
|
+
const axes = resolved.frame === undefined ? WORLD : this.axesOf(resolved.frame, "frame");
|
|
782
|
+
const countX = this.countOf(resolved.countX, "countX");
|
|
783
|
+
const countY = this.countOf(resolved.countY, "countY");
|
|
784
|
+
const radius = this.numberOf(resolved.radius, "radius");
|
|
785
|
+
if (!(radius > 0)) {
|
|
786
|
+
throw new InputError("`radius` must be more than zero for the hexagons to have a size.", "radius");
|
|
787
|
+
}
|
|
788
|
+
const columnSpacing = Math.sqrt(3) * radius;
|
|
789
|
+
const rowSpacing = 1.5 * radius;
|
|
790
|
+
const shiftU = resolved.centered ? ((countX - 1) * columnSpacing + (countY > 1 ? columnSpacing / 2 : 0)) / 2 : 0;
|
|
791
|
+
const shiftV = resolved.centered ? (countY - 1) * rowSpacing / 2 : 0;
|
|
792
|
+
const frames = [];
|
|
793
|
+
for (let row = 0; row < countY; row++) {
|
|
794
|
+
for (let column = 0; column < countX; column++) {
|
|
795
|
+
const u = column * columnSpacing + (row % 2 === 1 ? columnSpacing / 2 : 0);
|
|
796
|
+
frames.push(this.frameOf(Object.assign(Object.assign({}, axes), { origin: this.vector.add({ first: axes.origin, second: this.along(axes, u - shiftU, row * rowSpacing - shiftV, 0) }) })));
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return frames;
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* The widest spread's axis, pointing toward the first point, or with its largest component
|
|
803
|
+
* positive when the first point lies across it.
|
|
804
|
+
* @ignore true
|
|
805
|
+
*/
|
|
806
|
+
alongWidest(widest, first, spread) {
|
|
807
|
+
const toFirst = this.vector.dot({ first, second: widest });
|
|
808
|
+
if (!(Math.abs(toFirst) > 1e-9 * Math.sqrt(spread))) {
|
|
809
|
+
return this.withLargestPositive(widest);
|
|
810
|
+
}
|
|
811
|
+
return toFirst < 0 ? this.vector.neg({ vector: widest }) : widest;
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* Where the first point lies from the center, in the fitted plane, or the next point that does
|
|
815
|
+
* not sit on the center: the X axis of points that spread evenly every way in their plane.
|
|
816
|
+
* @ignore true
|
|
817
|
+
*/
|
|
818
|
+
towardFirstPoint(offsets, normal, spread) {
|
|
819
|
+
return offsets
|
|
820
|
+
.map(offset => this.vector.sub({ first: offset, second: this.vector.mul({ vector: normal, scalar: this.vector.dot({ first: offset, second: normal }) }) }))
|
|
821
|
+
.find(inPlane => this.vector.length({ vector: inPlane }) > 1e-9 * Math.sqrt(spread));
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* @ignore true
|
|
825
|
+
*/
|
|
826
|
+
translated(axes, translation) {
|
|
827
|
+
return this.frameOf(Object.assign(Object.assign({}, axes), { origin: this.vector.add({ first: axes.origin, second: translation }) }));
|
|
828
|
+
}
|
|
829
|
+
/**
|
|
830
|
+
* @ignore true
|
|
831
|
+
*/
|
|
832
|
+
offsetBy(axes, distance) {
|
|
833
|
+
return this.frameOf(Object.assign(Object.assign({}, axes), { origin: this.vector.add({ first: axes.origin, second: this.vector.mul({ vector: axes.z, scalar: distance }) }) }));
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* The axes turned about one of their own by `angle` radians, through their origin.
|
|
837
|
+
* @ignore true
|
|
838
|
+
*/
|
|
839
|
+
turned(axes, axis, angle) {
|
|
840
|
+
const c = Math.cos(angle);
|
|
841
|
+
const s = Math.sin(angle);
|
|
842
|
+
const turn = (first, second) => [this.combined(first, c, second, s), this.combined(first, -s, second, c)];
|
|
843
|
+
switch (axis) {
|
|
844
|
+
case Inputs.Frame.frameAxisEnum.x: {
|
|
845
|
+
const [y, z] = turn(axes.y, axes.z);
|
|
846
|
+
return this.frameOf({ origin: axes.origin, x: axes.x, y, z });
|
|
847
|
+
}
|
|
848
|
+
case Inputs.Frame.frameAxisEnum.y: {
|
|
849
|
+
const [z, x] = turn(axes.z, axes.x);
|
|
850
|
+
return this.frameOf({ origin: axes.origin, x, y: axes.y, z });
|
|
851
|
+
}
|
|
852
|
+
default: {
|
|
853
|
+
const [x, y] = turn(axes.x, axes.y);
|
|
854
|
+
return this.frameOf({ origin: axes.origin, x, y, z: axes.z });
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* @ignore true
|
|
860
|
+
*/
|
|
861
|
+
flipped(axes) {
|
|
862
|
+
return this.frameOf({ origin: axes.origin, x: axes.x, y: this.vector.neg({ vector: axes.y }), z: this.vector.neg({ vector: axes.z }) });
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* @ignore true
|
|
866
|
+
*/
|
|
867
|
+
childToWorld(parent, child) {
|
|
868
|
+
return this.frameOf({
|
|
869
|
+
origin: this.vector.add({ first: parent.origin, second: this.along(parent, ...child.origin) }),
|
|
870
|
+
x: this.along(parent, ...child.x),
|
|
871
|
+
y: this.along(parent, ...child.y),
|
|
872
|
+
z: this.along(parent, ...child.z),
|
|
873
|
+
});
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* @ignore true
|
|
877
|
+
*/
|
|
878
|
+
childToLocal(parent, child) {
|
|
879
|
+
return this.frameOf({
|
|
880
|
+
origin: this.against(parent, this.vector.sub({ first: child.origin, second: parent.origin })),
|
|
881
|
+
x: this.against(parent, child.x),
|
|
882
|
+
y: this.against(parent, child.y),
|
|
883
|
+
z: this.against(parent, child.z),
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
/**
|
|
887
|
+
* The vector `u` along `x`, `v` along `y` and `w` along `z` of the axes, from their origin.
|
|
888
|
+
* @ignore true
|
|
889
|
+
*/
|
|
890
|
+
along(axes, u, v, w) {
|
|
891
|
+
return this.vector.add({ first: this.combined(axes.x, u, axes.y, v), second: this.vector.mul({ vector: axes.z, scalar: w }) });
|
|
892
|
+
}
|
|
893
|
+
/**
|
|
894
|
+
* The same vector read against the axes: its components along `x`, `y` and `z`.
|
|
895
|
+
* @ignore true
|
|
896
|
+
*/
|
|
897
|
+
against(axes, vector) {
|
|
898
|
+
return [this.vector.dot({ first: vector, second: axes.x }), this.vector.dot({ first: vector, second: axes.y }), this.vector.dot({ first: vector, second: axes.z })];
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* `first` scaled by `a` plus `second` scaled by `b`.
|
|
902
|
+
* @ignore true
|
|
903
|
+
*/
|
|
904
|
+
combined(first, a, second, b) {
|
|
905
|
+
return this.vector.add({ first: this.vector.mul({ vector: first, scalar: a }), second: this.vector.mul({ vector: second, scalar: b }) });
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* Flips a unit vector whose largest component is negative, so a sign the data leaves open is
|
|
909
|
+
* decided the same way every time. Components within 1e-12 of each other count as equal and the
|
|
910
|
+
* first of them decides, so rounding cannot tip the choice; OCCT's frames break the tie the
|
|
911
|
+
* same way.
|
|
912
|
+
* @ignore true
|
|
913
|
+
*/
|
|
914
|
+
withLargestPositive(vector) {
|
|
915
|
+
let largest = 0;
|
|
916
|
+
for (let i = 1; i < 3; i++) {
|
|
917
|
+
if (Math.abs(vector[i]) > Math.abs(vector[largest]) + 1e-12) {
|
|
918
|
+
largest = i;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
return vector[largest] < 0 ? this.vector.neg({ vector }) : vector;
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* The column-major matrix carrying the `from` axes onto the `to` axes.
|
|
925
|
+
* @ignore true
|
|
926
|
+
*/
|
|
927
|
+
matrixBetween(from, to) {
|
|
928
|
+
const columns = [0, 1, 2].map(i => this.along(to, from.x[i], from.y[i], from.z[i]));
|
|
929
|
+
const origin = this.vector.add({ first: to.origin, second: this.along(to, ...this.vector.neg({ vector: this.against(from, from.origin) })) });
|
|
930
|
+
return [
|
|
931
|
+
...columns[0], 0,
|
|
932
|
+
...columns[1], 0,
|
|
933
|
+
...columns[2], 0,
|
|
934
|
+
...origin, 1,
|
|
935
|
+
];
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* The axes of a frame a caller handed in, squared, or an error naming the input at fault.
|
|
939
|
+
* @ignore true
|
|
940
|
+
*/
|
|
941
|
+
axesOf(frame, property, subject = `\`${property}\``) {
|
|
942
|
+
if (!isFrameShaped(frame)) {
|
|
943
|
+
throw new InputError(`${subject} is not a frame: it needs \`origin\`, \`normal\` and \`direction\`, three finite numbers each.`, property);
|
|
944
|
+
}
|
|
945
|
+
return this.squared(frame.origin, frame.normal, frame.direction, {
|
|
946
|
+
normal: `${subject} is not a frame: its \`normal\` has no length.`,
|
|
947
|
+
direction: `${subject} is not a frame: its \`direction\` runs along its \`normal\` or has no length.`,
|
|
948
|
+
property,
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* The axes of every frame in a list a caller handed in, or an error naming the list and the
|
|
953
|
+
* position at fault.
|
|
954
|
+
* @ignore true
|
|
955
|
+
*/
|
|
956
|
+
axesOfEach(frames, property) {
|
|
957
|
+
if (!Array.isArray(frames)) {
|
|
958
|
+
throw new InputError(`\`${property}\` is not a list of frames.`, property);
|
|
959
|
+
}
|
|
960
|
+
return frames.map((frame, index) => this.axesOf(frame, property, `\`${property}\` at position ${index}`));
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* @ignore true
|
|
964
|
+
*/
|
|
965
|
+
axisOf(axis) {
|
|
966
|
+
if (axis !== Inputs.Frame.frameAxisEnum.x && axis !== Inputs.Frame.frameAxisEnum.y && axis !== Inputs.Frame.frameAxisEnum.z) {
|
|
967
|
+
throw new InputError(`\`axis\` must be x, y or z; it is ${String(axis)}.`, "axis");
|
|
968
|
+
}
|
|
969
|
+
return axis;
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* An origin with a unit normal and a unit X axis square to it, or an error with the message for
|
|
973
|
+
* the part that cannot be squared, naming `property`, or that part when none is given.
|
|
974
|
+
* @ignore true
|
|
975
|
+
*/
|
|
976
|
+
squared(origin, normal, direction, faults) {
|
|
977
|
+
var _a;
|
|
978
|
+
const axes = squareFrame(origin, normal, direction);
|
|
979
|
+
if (typeof axes === "string") {
|
|
980
|
+
throw new InputError(faults[axes], (_a = faults.property) !== null && _a !== void 0 ? _a : axes);
|
|
981
|
+
}
|
|
982
|
+
return axes;
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* The frame the axes describe, as new arrays.
|
|
986
|
+
* @ignore true
|
|
987
|
+
*/
|
|
988
|
+
frameOf(axes) {
|
|
989
|
+
return { origin: [...axes.origin], normal: [...axes.z], direction: [...axes.x] };
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* @ignore true
|
|
993
|
+
*/
|
|
994
|
+
pointOf(value, property) {
|
|
995
|
+
if (!isTriple(value)) {
|
|
996
|
+
throw new InputError(`\`${property}\` is not a point: it needs three finite numbers.`, property);
|
|
997
|
+
}
|
|
998
|
+
return [...value];
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* @ignore true
|
|
1002
|
+
*/
|
|
1003
|
+
vectorOf(value, property) {
|
|
1004
|
+
if (!isTriple(value)) {
|
|
1005
|
+
throw new InputError(`\`${property}\` is not a vector: it needs three finite numbers.`, property);
|
|
1006
|
+
}
|
|
1007
|
+
return [...value];
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* @ignore true
|
|
1011
|
+
*/
|
|
1012
|
+
pointsOf(value, property) {
|
|
1013
|
+
if (!Array.isArray(value)) {
|
|
1014
|
+
throw new InputError(`\`${property}\` is not a list of points.`, property);
|
|
1015
|
+
}
|
|
1016
|
+
const faulty = value.findIndex(point => !isTriple(point));
|
|
1017
|
+
if (faulty !== -1) {
|
|
1018
|
+
throw new InputError(`\`${property}\` holds something other than a point at position ${faulty}; each point needs three finite numbers.`, property);
|
|
1019
|
+
}
|
|
1020
|
+
return value.map(point => [...point]);
|
|
1021
|
+
}
|
|
1022
|
+
/**
|
|
1023
|
+
* @ignore true
|
|
1024
|
+
*/
|
|
1025
|
+
numberOf(value, property) {
|
|
1026
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
1027
|
+
throw new InputError(`\`${property}\` is not a finite number.`, property);
|
|
1028
|
+
}
|
|
1029
|
+
return value;
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* @ignore true
|
|
1033
|
+
*/
|
|
1034
|
+
countOf(value, property) {
|
|
1035
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 1) {
|
|
1036
|
+
throw new InputError(`\`${property}\` must be a whole number of at least 1.`, property);
|
|
1037
|
+
}
|
|
1038
|
+
return value;
|
|
1039
|
+
}
|
|
1040
|
+
}
|