@bitbybit-dev/base 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/api/GlobalCDNProvider.js +1 -1
- package/lib/api/inputs/base-inputs.d.ts +13 -12
- package/lib/api/inputs/color-inputs.d.ts +97 -48
- package/lib/api/inputs/color-inputs.js +95 -46
- package/lib/api/inputs/dates-inputs.d.ts +67 -26
- package/lib/api/inputs/dates-inputs.js +57 -16
- package/lib/api/inputs/io-inputs.d.ts +13 -15
- package/lib/api/inputs/io-inputs.js +7 -8
- package/lib/api/inputs/line-inputs.d.ts +88 -33
- package/lib/api/inputs/line-inputs.js +61 -11
- package/lib/api/inputs/lists-inputs.d.ts +207 -96
- package/lib/api/inputs/lists-inputs.js +153 -45
- package/lib/api/inputs/logic-inputs.d.ts +52 -22
- package/lib/api/inputs/logic-inputs.js +41 -12
- package/lib/api/inputs/math-inputs.d.ts +113 -52
- package/lib/api/inputs/math-inputs.js +112 -51
- package/lib/api/inputs/mesh-inputs.d.ts +30 -11
- package/lib/api/inputs/mesh-inputs.js +21 -3
- package/lib/api/inputs/point-inputs.d.ts +240 -107
- package/lib/api/inputs/point-inputs.js +199 -73
- package/lib/api/inputs/polyline-inputs.d.ts +68 -28
- package/lib/api/inputs/polyline-inputs.js +48 -13
- package/lib/api/inputs/text-inputs.d.ts +117 -46
- package/lib/api/inputs/text-inputs.js +111 -41
- package/lib/api/inputs/transforms-inputs.d.ts +65 -23
- package/lib/api/inputs/transforms-inputs.js +64 -22
- package/lib/api/inputs/vector-inputs.d.ts +154 -76
- package/lib/api/inputs/vector-inputs.js +132 -57
- package/lib/api/models/point/hex-grid-data.d.ts +1 -1
- package/lib/api/models/point/hex-grid-data.js +1 -1
- package/lib/api/services/color.d.ts +149 -70
- package/lib/api/services/color.js +149 -70
- package/lib/api/services/dates.d.ts +313 -150
- package/lib/api/services/dates.js +313 -150
- package/lib/api/services/line.d.ts +138 -68
- package/lib/api/services/line.js +138 -68
- package/lib/api/services/lists.d.ts +357 -177
- package/lib/api/services/lists.js +357 -177
- package/lib/api/services/logic.d.ts +98 -45
- package/lib/api/services/logic.js +98 -45
- package/lib/api/services/math.d.ts +363 -211
- package/lib/api/services/math.js +363 -211
- package/lib/api/services/mesh.d.ts +73 -31
- package/lib/api/services/mesh.js +73 -31
- package/lib/api/services/point.d.ts +367 -147
- package/lib/api/services/point.js +367 -147
- package/lib/api/services/polyline.d.ts +146 -71
- package/lib/api/services/polyline.js +146 -71
- package/lib/api/services/text.d.ts +320 -165
- package/lib/api/services/text.js +320 -165
- package/lib/api/services/transforms.d.ts +145 -58
- package/lib/api/services/transforms.js +145 -58
- package/lib/api/services/vector.d.ts +381 -156
- package/lib/api/services/vector.js +381 -156
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Vector maths on plain number arrays. A vector is an array of numbers; in 3D it is `[x, y, z]`
|
|
3
|
+
* with Y pointing up, the same shape as a point, so the two can be passed to each other's methods.
|
|
4
|
+
* Every method returns a new array or a number and never changes its inputs. Angles are in degrees.
|
|
5
5
|
*/
|
|
6
6
|
export class Vector {
|
|
7
7
|
constructor(math, geometryHelper) {
|
|
@@ -9,49 +9,82 @@ export class Vector {
|
|
|
9
9
|
this.geometryHelper = geometryHelper;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
* Removes
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
12
|
+
* Removes every repeated vector from a list, keeping the first occurrence of each.
|
|
13
|
+
*
|
|
14
|
+
* Two vectors count as the same when every entry differs by less than `tolerance`.
|
|
15
|
+
* Example: [[1,2,3], [4,5,6], [1,2,3], [7,8,9]] -> [[1,2,3], [4,5,6], [7,8,9]]
|
|
16
|
+
* @param inputs - Vectors to filter and the tolerance
|
|
17
|
+
* @returns The vectors without repeats, in their original order
|
|
16
18
|
* @group remove
|
|
17
19
|
* @shortname remove all duplicates
|
|
18
20
|
* @drawable false
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const unique = bitbybit.vector.removeAllDuplicateVectors({
|
|
24
|
+
* vectors: [[1, 2, 3], [4, 5, 6], [1, 2, 3]],
|
|
25
|
+
* tolerance: 1e-7,
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
19
28
|
*/
|
|
20
29
|
removeAllDuplicateVectors(inputs) {
|
|
21
30
|
return this.geometryHelper.removeAllDuplicateVectors(inputs.vectors, inputs.tolerance);
|
|
22
31
|
}
|
|
23
32
|
/**
|
|
24
|
-
* Removes
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
33
|
+
* Removes a vector when it repeats the one right before it; the same vector further away is
|
|
34
|
+
* kept.
|
|
35
|
+
*
|
|
36
|
+
* With `checkFirstAndLast` on, a last vector that repeats the first is dropped too, which
|
|
37
|
+
* closes a loop of points cleanly. Entries within `tolerance` of each other count as equal.
|
|
38
|
+
* Example: [[1,2], [1,2], [3,4], [1,2]] -> [[1,2], [3,4], [1,2]]
|
|
39
|
+
* @param inputs - Vectors to filter, whether to compare the first and last, and the tolerance
|
|
40
|
+
* @returns The vectors without consecutive repeats
|
|
28
41
|
* @group remove
|
|
29
42
|
* @shortname remove consecutive duplicates
|
|
30
43
|
* @drawable false
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const cleaned = bitbybit.vector.removeConsecutiveDuplicateVectors({
|
|
47
|
+
* vectors: [[0, 0], [0, 0], [1, 1], [0, 0]],
|
|
48
|
+
* checkFirstAndLast: true,
|
|
49
|
+
* tolerance: 1e-7,
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
31
52
|
*/
|
|
32
53
|
removeConsecutiveDuplicateVectors(inputs) {
|
|
33
54
|
return this.geometryHelper.removeConsecutiveVectorDuplicates(inputs.vectors, inputs.checkFirstAndLast, inputs.tolerance);
|
|
34
55
|
}
|
|
35
56
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
57
|
+
* Tells whether two vectors are the same within a tolerance, entry by entry.
|
|
58
|
+
*
|
|
59
|
+
* Vectors of different length are never the same.
|
|
60
|
+
* Example: [1,2,3] and [1.0001,2.0001,3.0001] with tolerance 0.001 -> true
|
|
61
|
+
* @param inputs - The two vectors and the tolerance
|
|
62
|
+
* @returns True when every entry differs by less than the tolerance
|
|
40
63
|
* @group validate
|
|
41
64
|
* @shortname vectors the same
|
|
42
65
|
* @drawable false
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const same = bitbybit.vector.vectorsTheSame({ vec1: [1, 2, 3], vec2: [1, 2, 3.0000001], tolerance: 1e-6 });
|
|
69
|
+
* ```
|
|
43
70
|
*/
|
|
44
71
|
vectorsTheSame(inputs) {
|
|
45
72
|
return this.geometryHelper.vectorsTheSame(inputs.vec1, inputs.vec2, inputs.tolerance);
|
|
46
73
|
}
|
|
47
74
|
/**
|
|
48
|
-
* Measures the angle between two vectors in degrees
|
|
49
|
-
*
|
|
50
|
-
*
|
|
75
|
+
* Measures the angle between two vectors in degrees, always between 0 and 180.
|
|
76
|
+
*
|
|
77
|
+
* The direction of turning is not considered; use `signedAngleBetween` for that.
|
|
78
|
+
* Example: [1,0,0] and [0,1,0] -> 90
|
|
79
|
+
* @param inputs - The two vectors
|
|
80
|
+
* @returns Angle in degrees
|
|
51
81
|
* @group angles
|
|
52
82
|
* @shortname angle
|
|
53
|
-
* @returns Number in degrees
|
|
54
83
|
* @drawable false
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* const angle = bitbybit.vector.angleBetween({ first: [1, 0, 0], second: [0, 1, 0] });
|
|
87
|
+
* ```
|
|
55
88
|
*/
|
|
56
89
|
angleBetween(inputs) {
|
|
57
90
|
return this.math.radToDeg({
|
|
@@ -59,13 +92,20 @@ export class Vector {
|
|
|
59
92
|
});
|
|
60
93
|
}
|
|
61
94
|
/**
|
|
62
|
-
* Measures the
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
95
|
+
* Measures the signed angle from the first 2D vector to the second, in degrees from -180 to
|
|
96
|
+
* 180.
|
|
97
|
+
*
|
|
98
|
+
* Only the first two entries of each vector are used; a positive angle turns counter-clockwise.
|
|
99
|
+
* Example: [1,0] to [0,1] -> 90, [0,1] to [1,0] -> -90
|
|
100
|
+
* @param inputs - The two 2D vectors
|
|
101
|
+
* @returns Signed angle in degrees
|
|
66
102
|
* @group angles
|
|
67
103
|
* @shortname angle normalized 2d
|
|
68
104
|
* @drawable false
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const angle = bitbybit.vector.angleBetweenNormalized2d({ first: [1, 0], second: [0, 1] });
|
|
108
|
+
* ```
|
|
69
109
|
*/
|
|
70
110
|
angleBetweenNormalized2d(inputs) {
|
|
71
111
|
const perpDot = inputs.first[0] * inputs.second[1] - inputs.first[1] * inputs.second[0];
|
|
@@ -74,26 +114,39 @@ export class Vector {
|
|
|
74
114
|
});
|
|
75
115
|
}
|
|
76
116
|
/**
|
|
77
|
-
* Measures
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
117
|
+
* Measures the angle from the first vector to the second, turning around a reference direction,
|
|
118
|
+
* in degrees from 0 to 360.
|
|
119
|
+
*
|
|
120
|
+
* The turn is counter-clockwise when the reference vector points toward you.
|
|
121
|
+
* Example: [1,0,0] to [0,0,-1] around [0,1,0] -> 90
|
|
122
|
+
* @param inputs - The two vectors and the reference direction to turn around
|
|
123
|
+
* @returns Angle in degrees from 0 to 360
|
|
81
124
|
* @group angles
|
|
82
125
|
* @shortname positive angle
|
|
83
126
|
* @drawable false
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const angle = bitbybit.vector.positiveAngleBetween({ first: [1, 0, 0], second: [0, 0, -1], reference: [0, 1, 0] });
|
|
130
|
+
* ```
|
|
84
131
|
*/
|
|
85
132
|
positiveAngleBetween(inputs) {
|
|
86
133
|
const angle = this.signedAngleBetween(inputs);
|
|
87
134
|
return angle < 0 ? 360 + angle : angle;
|
|
88
135
|
}
|
|
89
136
|
/**
|
|
90
|
-
* Adds
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
137
|
+
* Adds a list of vectors together entry by entry into one vector.
|
|
138
|
+
*
|
|
139
|
+
* The result has as many entries as the first vector.
|
|
140
|
+
* Example: [[1,2,3], [4,5,6], [7,8,9]] -> [12,15,18]
|
|
141
|
+
* @param inputs - Vectors to add
|
|
142
|
+
* @returns The vector of sums
|
|
94
143
|
* @group sum
|
|
95
144
|
* @shortname add all
|
|
96
145
|
* @drawable false
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* const total = bitbybit.vector.addAll({ vectors: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] });
|
|
149
|
+
* ```
|
|
97
150
|
*/
|
|
98
151
|
addAll(inputs) {
|
|
99
152
|
const res = [];
|
|
@@ -107,13 +160,18 @@ export class Vector {
|
|
|
107
160
|
return res;
|
|
108
161
|
}
|
|
109
162
|
/**
|
|
110
|
-
* Adds two vectors
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* @
|
|
163
|
+
* Adds two vectors entry by entry.
|
|
164
|
+
*
|
|
165
|
+
* Example: [1,2,3] + [4,5,6] -> [5,7,9]
|
|
166
|
+
* @param inputs - The two vectors to add
|
|
167
|
+
* @returns The vector of sums
|
|
114
168
|
* @group sum
|
|
115
169
|
* @shortname add
|
|
116
170
|
* @drawable false
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* const sum = bitbybit.vector.add({ first: [1, 2, 3], second: [4, 5, 6] });
|
|
174
|
+
* ```
|
|
117
175
|
*/
|
|
118
176
|
add(inputs) {
|
|
119
177
|
const res = [];
|
|
@@ -123,25 +181,37 @@ export class Vector {
|
|
|
123
181
|
return res;
|
|
124
182
|
}
|
|
125
183
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* @
|
|
184
|
+
* Tells whether every value in a list of booleans is true.
|
|
185
|
+
*
|
|
186
|
+
* Example: [true, true, true] -> true, [true, false, true] -> false
|
|
187
|
+
* @param inputs - The booleans to check
|
|
188
|
+
* @returns True when no entry is false
|
|
130
189
|
* @group sum
|
|
131
190
|
* @shortname all
|
|
132
191
|
* @drawable false
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* const allTrue = bitbybit.vector.all({ vector: [true, true, false] });
|
|
195
|
+
* ```
|
|
133
196
|
*/
|
|
134
197
|
all(inputs) {
|
|
135
198
|
return inputs.vector.every(v => v);
|
|
136
199
|
}
|
|
137
200
|
/**
|
|
138
|
-
* Computes the cross product of two 3D vectors
|
|
139
|
-
*
|
|
140
|
-
*
|
|
201
|
+
* Computes the cross product of two 3D vectors: a vector at right angles to both.
|
|
202
|
+
*
|
|
203
|
+
* Its direction follows the right-hand rule and its length is the area of the parallelogram the
|
|
204
|
+
* two vectors span.
|
|
205
|
+
* Example: [1,0,0] x [0,1,0] -> [0,0,1]
|
|
206
|
+
* @param inputs - The two 3D vectors
|
|
207
|
+
* @returns The vector perpendicular to both
|
|
141
208
|
* @group base
|
|
142
209
|
* @shortname cross
|
|
143
|
-
* @returns Crossed vector
|
|
144
210
|
* @drawable false
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* const normal = bitbybit.vector.cross({ first: [1, 0, 0], second: [0, 1, 0] });
|
|
214
|
+
* ```
|
|
145
215
|
*/
|
|
146
216
|
cross(inputs) {
|
|
147
217
|
const res = [];
|
|
@@ -151,13 +221,19 @@ export class Vector {
|
|
|
151
221
|
return res;
|
|
152
222
|
}
|
|
153
223
|
/**
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
224
|
+
* Computes the squared distance between two vectors, which avoids the square root when only
|
|
225
|
+
* comparing distances.
|
|
226
|
+
*
|
|
227
|
+
* Example: [0,0,0] to [3,4,0] -> 25
|
|
228
|
+
* @param inputs - The two vectors
|
|
229
|
+
* @returns The squared distance
|
|
158
230
|
* @group distance
|
|
159
231
|
* @shortname dist squared
|
|
160
232
|
* @drawable false
|
|
233
|
+
* @example
|
|
234
|
+
* ```typescript
|
|
235
|
+
* const d2 = bitbybit.vector.distSquared({ first: [0, 0, 0], second: [3, 4, 0] });
|
|
236
|
+
* ```
|
|
161
237
|
*/
|
|
162
238
|
distSquared(inputs) {
|
|
163
239
|
let res = 0;
|
|
@@ -167,25 +243,35 @@ export class Vector {
|
|
|
167
243
|
return res;
|
|
168
244
|
}
|
|
169
245
|
/**
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
* @
|
|
246
|
+
* Computes the straight-line distance between two vectors.
|
|
247
|
+
*
|
|
248
|
+
* Example: [0,0,0] to [3,4,0] -> 5
|
|
249
|
+
* @param inputs - The two vectors
|
|
250
|
+
* @returns The distance in model units
|
|
174
251
|
* @group distance
|
|
175
252
|
* @shortname dist
|
|
176
253
|
* @drawable false
|
|
254
|
+
* @example
|
|
255
|
+
* ```typescript
|
|
256
|
+
* const distance = bitbybit.vector.dist({ first: [0, 0, 0], second: [3, 4, 0] });
|
|
257
|
+
* ```
|
|
177
258
|
*/
|
|
178
259
|
dist(inputs) {
|
|
179
260
|
return Math.sqrt(this.distSquared(inputs));
|
|
180
261
|
}
|
|
181
262
|
/**
|
|
182
|
-
* Divides
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
* @
|
|
263
|
+
* Divides every entry of a vector by one number.
|
|
264
|
+
*
|
|
265
|
+
* Example: [10,20,30] / 2 -> [5,10,15]
|
|
266
|
+
* @param inputs - The vector and the number to divide by
|
|
267
|
+
* @returns The divided vector
|
|
186
268
|
* @group base
|
|
187
269
|
* @shortname div
|
|
188
270
|
* @drawable false
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* const half = bitbybit.vector.div({ vector: [10, 20, 30], scalar: 2 });
|
|
274
|
+
* ```
|
|
189
275
|
*/
|
|
190
276
|
div(inputs) {
|
|
191
277
|
const res = [];
|
|
@@ -195,25 +281,37 @@ export class Vector {
|
|
|
195
281
|
return res;
|
|
196
282
|
}
|
|
197
283
|
/**
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
* @
|
|
284
|
+
* Subtracts the first value of a vector from its last, which for a sorted list is its range.
|
|
285
|
+
*
|
|
286
|
+
* Example: [1,3,5,9] -> 8
|
|
287
|
+
* @param inputs - The vector
|
|
288
|
+
* @returns Last value minus first value
|
|
202
289
|
* @group base
|
|
203
290
|
* @shortname domain
|
|
204
291
|
* @drawable false
|
|
292
|
+
* @example
|
|
293
|
+
* ```typescript
|
|
294
|
+
* const span = bitbybit.vector.domain({ vector: [1, 3, 5, 9] });
|
|
295
|
+
* ```
|
|
205
296
|
*/
|
|
206
297
|
domain(inputs) {
|
|
207
298
|
return inputs.vector[inputs.vector.length - 1] - inputs.vector[0];
|
|
208
299
|
}
|
|
209
300
|
/**
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
301
|
+
* Computes the dot product of two vectors: the sum of the products of matching entries.
|
|
302
|
+
*
|
|
303
|
+
* It is 0 for vectors at right angles and, for unit vectors, the cosine of the angle between
|
|
304
|
+
* them.
|
|
305
|
+
* Example: [1,2,3] and [4,5,6] -> 32
|
|
306
|
+
* @param inputs - The two vectors
|
|
307
|
+
* @returns The dot product
|
|
214
308
|
* @group base
|
|
215
309
|
* @shortname dot
|
|
216
310
|
* @drawable false
|
|
311
|
+
* @example
|
|
312
|
+
* ```typescript
|
|
313
|
+
* const projection = bitbybit.vector.dot({ first: [1, 2, 3], second: [4, 5, 6] });
|
|
314
|
+
* ```
|
|
217
315
|
*/
|
|
218
316
|
dot(inputs) {
|
|
219
317
|
let res = 0;
|
|
@@ -223,38 +321,53 @@ export class Vector {
|
|
|
223
321
|
return res;
|
|
224
322
|
}
|
|
225
323
|
/**
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
* @
|
|
230
|
-
*
|
|
324
|
+
* Marks which entries of a vector are finite numbers.
|
|
325
|
+
*
|
|
326
|
+
* Example: [1, 2, Infinity, 3] -> [true, true, false, true]
|
|
327
|
+
* @param inputs - The vector to check
|
|
328
|
+
* @returns One boolean per entry, true when it is finite
|
|
231
329
|
* @group validate
|
|
232
330
|
* @shortname finite
|
|
233
331
|
* @drawable false
|
|
332
|
+
* @example
|
|
333
|
+
* ```typescript
|
|
334
|
+
* const flags = bitbybit.vector.finite({ vector: [1, Infinity, 3] });
|
|
335
|
+
* ```
|
|
234
336
|
*/
|
|
235
337
|
finite(inputs) {
|
|
236
338
|
return inputs.vector.map(v => isFinite(v));
|
|
237
339
|
}
|
|
238
340
|
/**
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
* @
|
|
341
|
+
* Tells whether a vector has no length, that is, every entry is exactly 0.
|
|
342
|
+
*
|
|
343
|
+
* Example: [0,0,0] -> true, [0,0,0.001] -> false
|
|
344
|
+
* @param inputs - The vector to check
|
|
345
|
+
* @returns True when the length is 0
|
|
243
346
|
* @group validate
|
|
244
347
|
* @shortname isZero
|
|
245
348
|
* @drawable false
|
|
349
|
+
* @example
|
|
350
|
+
* ```typescript
|
|
351
|
+
* const zero = bitbybit.vector.isZero({ vector: [0, 0, 0] });
|
|
352
|
+
* ```
|
|
246
353
|
*/
|
|
247
354
|
isZero(inputs) {
|
|
248
355
|
return this.norm({ vector: inputs.vector }) === 0;
|
|
249
356
|
}
|
|
250
357
|
/**
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
358
|
+
* Blends two vectors linearly by a fraction.
|
|
359
|
+
*
|
|
360
|
+
* `fraction` is the share of `first`: 1 gives `first`, 0 gives `second`, 0.5 the midpoint.
|
|
361
|
+
* Example: [0,0,0] and [10,10,10] at 0.5 -> [5,5,5]
|
|
362
|
+
* @param inputs - The two vectors and the fraction of the first
|
|
363
|
+
* @returns The blended vector
|
|
255
364
|
* @group distance
|
|
256
365
|
* @shortname lerp
|
|
257
366
|
* @drawable false
|
|
367
|
+
* @example
|
|
368
|
+
* ```typescript
|
|
369
|
+
* const mid = bitbybit.vector.lerp({ first: [0, 0, 0], second: [10, 10, 10], fraction: 0.5 });
|
|
370
|
+
* ```
|
|
258
371
|
*/
|
|
259
372
|
lerp(inputs) {
|
|
260
373
|
return this.add({
|
|
@@ -263,37 +376,52 @@ export class Vector {
|
|
|
263
376
|
});
|
|
264
377
|
}
|
|
265
378
|
/**
|
|
266
|
-
* Finds the
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
* @
|
|
379
|
+
* Finds the largest value in a vector.
|
|
380
|
+
*
|
|
381
|
+
* Example: [3, 7, 2, 9, 1] -> 9
|
|
382
|
+
* @param inputs - The vector
|
|
383
|
+
* @returns The largest entry
|
|
270
384
|
* @group extract
|
|
271
385
|
* @shortname max
|
|
272
386
|
* @drawable false
|
|
387
|
+
* @example
|
|
388
|
+
* ```typescript
|
|
389
|
+
* const largest = bitbybit.vector.max({ vector: [3, 7, 2, 9, 1] });
|
|
390
|
+
* ```
|
|
273
391
|
*/
|
|
274
392
|
max(inputs) {
|
|
275
393
|
return Math.max(...inputs.vector);
|
|
276
394
|
}
|
|
277
395
|
/**
|
|
278
|
-
* Finds the
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
* @
|
|
396
|
+
* Finds the smallest value in a vector.
|
|
397
|
+
*
|
|
398
|
+
* Example: [3, 7, 2, 9, 1] -> 1
|
|
399
|
+
* @param inputs - The vector
|
|
400
|
+
* @returns The smallest entry
|
|
282
401
|
* @group extract
|
|
283
402
|
* @shortname min
|
|
284
403
|
* @drawable false
|
|
404
|
+
* @example
|
|
405
|
+
* ```typescript
|
|
406
|
+
* const smallest = bitbybit.vector.min({ vector: [3, 7, 2, 9, 1] });
|
|
407
|
+
* ```
|
|
285
408
|
*/
|
|
286
409
|
min(inputs) {
|
|
287
410
|
return Math.min(...inputs.vector);
|
|
288
411
|
}
|
|
289
412
|
/**
|
|
290
|
-
* Multiplies
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
* @
|
|
413
|
+
* Multiplies every entry of a vector by one number.
|
|
414
|
+
*
|
|
415
|
+
* Example: [2,3,4] x 5 -> [10,15,20]
|
|
416
|
+
* @param inputs - The vector and the number to multiply by
|
|
417
|
+
* @returns The scaled vector
|
|
294
418
|
* @group base
|
|
295
419
|
* @shortname mul
|
|
296
420
|
* @drawable false
|
|
421
|
+
* @example
|
|
422
|
+
* ```typescript
|
|
423
|
+
* const scaled = bitbybit.vector.mul({ vector: [2, 3, 4], scalar: 5 });
|
|
424
|
+
* ```
|
|
297
425
|
*/
|
|
298
426
|
mul(inputs) {
|
|
299
427
|
const res = [];
|
|
@@ -303,13 +431,18 @@ export class Vector {
|
|
|
303
431
|
return res;
|
|
304
432
|
}
|
|
305
433
|
/**
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
* @
|
|
434
|
+
* Flips the sign of every entry, so the vector points the opposite way.
|
|
435
|
+
*
|
|
436
|
+
* Example: [5,-3,2] -> [-5,3,-2]
|
|
437
|
+
* @param inputs - The vector to flip
|
|
438
|
+
* @returns The negated vector
|
|
310
439
|
* @group base
|
|
311
440
|
* @shortname neg
|
|
312
441
|
* @drawable false
|
|
442
|
+
* @example
|
|
443
|
+
* ```typescript
|
|
444
|
+
* const opposite = bitbybit.vector.neg({ vector: [5, -3, 2] });
|
|
445
|
+
* ```
|
|
313
446
|
*/
|
|
314
447
|
neg(inputs) {
|
|
315
448
|
const res = [];
|
|
@@ -319,38 +452,55 @@ export class Vector {
|
|
|
319
452
|
return res;
|
|
320
453
|
}
|
|
321
454
|
/**
|
|
322
|
-
* Computes the squared
|
|
323
|
-
*
|
|
324
|
-
*
|
|
325
|
-
*
|
|
455
|
+
* Computes the squared length of a vector, which avoids the square root when only comparing
|
|
456
|
+
* lengths.
|
|
457
|
+
*
|
|
458
|
+
* Example: [3,4,0] -> 25
|
|
459
|
+
* @param inputs - The vector
|
|
460
|
+
* @returns The squared length
|
|
326
461
|
* @group base
|
|
327
462
|
* @shortname norm squared
|
|
328
463
|
* @drawable false
|
|
464
|
+
* @example
|
|
465
|
+
* ```typescript
|
|
466
|
+
* const n2 = bitbybit.vector.normSquared({ vector: [3, 4, 0] });
|
|
467
|
+
* ```
|
|
329
468
|
*/
|
|
330
469
|
normSquared(inputs) {
|
|
331
470
|
return this.dot({ first: inputs.vector, second: inputs.vector });
|
|
332
471
|
}
|
|
333
472
|
/**
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
* @
|
|
473
|
+
* Computes the length of a vector.
|
|
474
|
+
*
|
|
475
|
+
* Example: [3,4,0] -> 5, [1,0,0] -> 1
|
|
476
|
+
* @param inputs - The vector
|
|
477
|
+
* @returns The length in model units
|
|
338
478
|
* @group base
|
|
339
479
|
* @shortname norm
|
|
340
480
|
* @drawable false
|
|
481
|
+
* @example
|
|
482
|
+
* ```typescript
|
|
483
|
+
* const len = bitbybit.vector.norm({ vector: [3, 4, 0] });
|
|
484
|
+
* ```
|
|
341
485
|
*/
|
|
342
486
|
norm(inputs) {
|
|
343
487
|
const norm2 = this.normSquared(inputs);
|
|
344
488
|
return norm2 !== 0.0 ? Math.sqrt(norm2) : norm2;
|
|
345
489
|
}
|
|
346
490
|
/**
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
491
|
+
* Scales a 3D vector to length 1 while keeping its direction.
|
|
492
|
+
*
|
|
493
|
+
* A vector shorter than 1e-8 has no direction to keep, so the result is undefined.
|
|
494
|
+
* Example: [3,4,0] -> [0.6,0.8,0]
|
|
495
|
+
* @param inputs - The 3D vector to normalize
|
|
496
|
+
* @returns The unit vector, or undefined for a zero-length input
|
|
351
497
|
* @group base
|
|
352
498
|
* @shortname normalized
|
|
353
499
|
* @drawable false
|
|
500
|
+
* @example
|
|
501
|
+
* ```typescript
|
|
502
|
+
* const direction = bitbybit.vector.normalized({ vector: [3, 4, 0] });
|
|
503
|
+
* ```
|
|
354
504
|
*/
|
|
355
505
|
normalized(inputs) {
|
|
356
506
|
const len = this.length({ vector: inputs.vector });
|
|
@@ -360,49 +510,70 @@ export class Vector {
|
|
|
360
510
|
return this.div({ scalar: this.norm(inputs), vector: inputs.vector });
|
|
361
511
|
}
|
|
362
512
|
/**
|
|
363
|
-
* Finds
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
513
|
+
* Finds the point at a given distance from a start point along a direction.
|
|
514
|
+
*
|
|
515
|
+
* The direction is used as given, so a direction of length 2 travels twice the distance.
|
|
516
|
+
* Example: start [0,0,0], direction [1,0,0], distance 5 -> [5,0,0]
|
|
517
|
+
* @param inputs - The start point, the direction and the distance
|
|
518
|
+
* @returns The point on the ray
|
|
367
519
|
* @group base
|
|
368
520
|
* @shortname on ray
|
|
369
521
|
* @drawable false
|
|
522
|
+
* @example
|
|
523
|
+
* ```typescript
|
|
524
|
+
* const ahead = bitbybit.vector.onRay({ point: [0, 0, 0], vector: [1, 0, 0], distance: 5 });
|
|
525
|
+
* ```
|
|
370
526
|
*/
|
|
371
527
|
onRay(inputs) {
|
|
372
528
|
return this.add({ first: inputs.point, second: this.mul({ vector: inputs.vector, scalar: inputs.distance }) });
|
|
373
529
|
}
|
|
374
530
|
/**
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
* @
|
|
531
|
+
* Builds a 3D vector from its x, y and z values.
|
|
532
|
+
*
|
|
533
|
+
* Example: x=1, y=2, z=3 -> [1,2,3]
|
|
534
|
+
* @param inputs - The three values
|
|
535
|
+
* @returns The vector `[x, y, z]`
|
|
379
536
|
* @group create
|
|
380
537
|
* @shortname vector XYZ
|
|
381
538
|
* @drawable true
|
|
539
|
+
* @example
|
|
540
|
+
* ```typescript
|
|
541
|
+
* const up = bitbybit.vector.vectorXYZ({ x: 0, y: 1, z: 0 });
|
|
542
|
+
* ```
|
|
382
543
|
*/
|
|
383
544
|
vectorXYZ(inputs) {
|
|
384
545
|
return [inputs.x, inputs.y, inputs.z];
|
|
385
546
|
}
|
|
386
547
|
/**
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
*
|
|
390
|
-
* @
|
|
548
|
+
* Builds a 2D vector from its x and y values.
|
|
549
|
+
*
|
|
550
|
+
* Example: x=3, y=4 -> [3,4]
|
|
551
|
+
* @param inputs - The two values
|
|
552
|
+
* @returns The vector `[x, y]`
|
|
391
553
|
* @group create
|
|
392
554
|
* @shortname vector XY
|
|
393
555
|
* @drawable true
|
|
556
|
+
* @example
|
|
557
|
+
* ```typescript
|
|
558
|
+
* const right = bitbybit.vector.vectorXY({ x: 1, y: 0 });
|
|
559
|
+
* ```
|
|
394
560
|
*/
|
|
395
561
|
vectorXY(inputs) {
|
|
396
562
|
return [inputs.x, inputs.y];
|
|
397
563
|
}
|
|
398
564
|
/**
|
|
399
|
-
*
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
* @
|
|
565
|
+
* Lists the whole numbers from 0 up to, but not including, `max`.
|
|
566
|
+
*
|
|
567
|
+
* Example: max=5 -> [0,1,2,3,4]
|
|
568
|
+
* @param inputs - The end of the range, which is left out
|
|
569
|
+
* @returns The numbers from 0 to max - 1
|
|
403
570
|
* @group create
|
|
404
571
|
* @shortname range
|
|
405
572
|
* @drawable false
|
|
573
|
+
* @example
|
|
574
|
+
* ```typescript
|
|
575
|
+
* const indices = bitbybit.vector.range({ max: 5 });
|
|
576
|
+
* ```
|
|
406
577
|
*/
|
|
407
578
|
range(inputs) {
|
|
408
579
|
const res = [];
|
|
@@ -412,13 +583,21 @@ export class Vector {
|
|
|
412
583
|
return res;
|
|
413
584
|
}
|
|
414
585
|
/**
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
586
|
+
* Measures the angle from the first vector to the second, turning around a reference direction,
|
|
587
|
+
* in degrees from 0 to 360.
|
|
588
|
+
*
|
|
589
|
+
* The turn is counter-clockwise when the reference vector points toward you: a clockwise turn
|
|
590
|
+
* of 30 degrees reads as 330.
|
|
591
|
+
* Example: [1,0,0] to [0,0,-1] around [0,1,0] -> 90
|
|
592
|
+
* @param inputs - The two vectors and the reference direction to turn around
|
|
593
|
+
* @returns Angle in degrees from 0 to 360
|
|
419
594
|
* @group angles
|
|
420
595
|
* @shortname signed angle
|
|
421
596
|
* @drawable false
|
|
597
|
+
* @example
|
|
598
|
+
* ```typescript
|
|
599
|
+
* const angle = bitbybit.vector.signedAngleBetween({ first: [1, 0, 0], second: [0, 0, -1], reference: [0, 1, 0] });
|
|
600
|
+
* ```
|
|
422
601
|
*/
|
|
423
602
|
signedAngleBetween(inputs) {
|
|
424
603
|
const nab = this.cross({ first: inputs.first, second: inputs.second });
|
|
@@ -434,13 +613,19 @@ export class Vector {
|
|
|
434
613
|
return this.math.radToDeg({ number: res });
|
|
435
614
|
}
|
|
436
615
|
/**
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
*
|
|
616
|
+
* Lists the numbers from `min` to `max`, stepping by `step`; `max` is included when a step
|
|
617
|
+
* lands on it.
|
|
618
|
+
*
|
|
619
|
+
* Example: min=0, max=10, step=2 -> [0,2,4,6,8,10]
|
|
620
|
+
* @param inputs - The start, the end and the step
|
|
621
|
+
* @returns The numbers in the span
|
|
441
622
|
* @group create
|
|
442
623
|
* @shortname span
|
|
443
624
|
* @drawable false
|
|
625
|
+
* @example
|
|
626
|
+
* ```typescript
|
|
627
|
+
* const values = bitbybit.vector.span({ min: 0, max: 10, step: 2.5 });
|
|
628
|
+
* ```
|
|
444
629
|
*/
|
|
445
630
|
span(inputs) {
|
|
446
631
|
const res = [];
|
|
@@ -450,13 +635,21 @@ export class Vector {
|
|
|
450
635
|
return res;
|
|
451
636
|
}
|
|
452
637
|
/**
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
*
|
|
456
|
-
*
|
|
638
|
+
* Lists `nrItems` numbers from `min` to `max` spaced by an easing curve, so they bunch up at
|
|
639
|
+
* one end or both.
|
|
640
|
+
*
|
|
641
|
+
* With `intervals` on, the result holds the gaps between neighbors instead of the values
|
|
642
|
+
* themselves.
|
|
643
|
+
* Example: min=0, max=100, nrItems=5, ease='easeInQuad' -> [0, 6.25, 25, 56.25, 100]
|
|
644
|
+
* @param inputs - The start, the end, the number of items, the easing and whether to return the gaps
|
|
645
|
+
* @returns The eased numbers, or the gaps between them
|
|
457
646
|
* @group create
|
|
458
647
|
* @shortname span ease items
|
|
459
648
|
* @drawable false
|
|
649
|
+
* @example
|
|
650
|
+
* ```typescript
|
|
651
|
+
* const eased = bitbybit.vector.spanEaseItems({ min: 0, max: 100, nrItems: 5, ease: Bit.Inputs.Math.easeEnum.easeInQuad, intervals: false });
|
|
652
|
+
* ```
|
|
460
653
|
*/
|
|
461
654
|
spanEaseItems(inputs) {
|
|
462
655
|
const res = [];
|
|
@@ -470,13 +663,18 @@ export class Vector {
|
|
|
470
663
|
return res;
|
|
471
664
|
}
|
|
472
665
|
/**
|
|
473
|
-
*
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
* @
|
|
666
|
+
* Lists `nrItems` evenly spaced numbers from `min` to `max`, both included.
|
|
667
|
+
*
|
|
668
|
+
* Example: min=0, max=10, nrItems=5 -> [0, 2.5, 5, 7.5, 10]
|
|
669
|
+
* @param inputs - The start, the end and the number of items
|
|
670
|
+
* @returns The evenly spaced numbers
|
|
477
671
|
* @group create
|
|
478
672
|
* @shortname span linear items
|
|
479
673
|
* @drawable false
|
|
674
|
+
* @example
|
|
675
|
+
* ```typescript
|
|
676
|
+
* const values = bitbybit.vector.spanLinearItems({ min: 0, max: 10, nrItems: 5 });
|
|
677
|
+
* ```
|
|
480
678
|
*/
|
|
481
679
|
spanLinearItems(inputs) {
|
|
482
680
|
const res = [];
|
|
@@ -488,13 +686,18 @@ export class Vector {
|
|
|
488
686
|
return res;
|
|
489
687
|
}
|
|
490
688
|
/**
|
|
491
|
-
* Subtracts the second vector from the first
|
|
492
|
-
*
|
|
493
|
-
*
|
|
494
|
-
* @
|
|
689
|
+
* Subtracts the second vector from the first, entry by entry.
|
|
690
|
+
*
|
|
691
|
+
* Example: [10,20,30] - [1,2,3] -> [9,18,27]
|
|
692
|
+
* @param inputs - The vector to subtract from and the vector to subtract
|
|
693
|
+
* @returns The vector of differences
|
|
495
694
|
* @group base
|
|
496
695
|
* @shortname sub
|
|
497
696
|
* @drawable false
|
|
697
|
+
* @example
|
|
698
|
+
* ```typescript
|
|
699
|
+
* const diff = bitbybit.vector.sub({ first: [10, 20, 30], second: [1, 2, 3] });
|
|
700
|
+
* ```
|
|
498
701
|
*/
|
|
499
702
|
sub(inputs) {
|
|
500
703
|
const res = [];
|
|
@@ -504,50 +707,72 @@ export class Vector {
|
|
|
504
707
|
return res;
|
|
505
708
|
}
|
|
506
709
|
/**
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
*
|
|
510
|
-
* @
|
|
710
|
+
* Adds up all values of a vector into one number.
|
|
711
|
+
*
|
|
712
|
+
* Example: [1,2,3,4] -> 10
|
|
713
|
+
* @param inputs - The vector to add up
|
|
714
|
+
* @returns The total
|
|
511
715
|
* @group base
|
|
512
716
|
* @shortname sum
|
|
513
717
|
* @drawable false
|
|
718
|
+
* @example
|
|
719
|
+
* ```typescript
|
|
720
|
+
* const total = bitbybit.vector.sum({ vector: [1, 2, 3, 4] });
|
|
721
|
+
* ```
|
|
514
722
|
*/
|
|
515
723
|
sum(inputs) {
|
|
516
724
|
return inputs.vector.reduce((a, b) => a + b, 0);
|
|
517
725
|
}
|
|
518
726
|
/**
|
|
519
|
-
* Computes the squared length
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
*
|
|
727
|
+
* Computes the squared length of a 3D vector, which avoids the square root when only comparing
|
|
728
|
+
* lengths.
|
|
729
|
+
*
|
|
730
|
+
* Example: [3,4,0] -> 25
|
|
731
|
+
* @param inputs - The 3D vector
|
|
732
|
+
* @returns The squared length
|
|
523
733
|
* @group base
|
|
524
734
|
* @shortname length squared
|
|
525
735
|
* @drawable false
|
|
736
|
+
* @example
|
|
737
|
+
* ```typescript
|
|
738
|
+
* const l2 = bitbybit.vector.lengthSq({ vector: [3, 4, 0] });
|
|
739
|
+
* ```
|
|
526
740
|
*/
|
|
527
741
|
lengthSq(inputs) {
|
|
528
742
|
const v = inputs.vector;
|
|
529
743
|
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
|
|
530
744
|
}
|
|
531
745
|
/**
|
|
532
|
-
* Computes the length
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
* @
|
|
746
|
+
* Computes the length of a 3D vector.
|
|
747
|
+
*
|
|
748
|
+
* Example: [3,4,0] -> 5
|
|
749
|
+
* @param inputs - The 3D vector
|
|
750
|
+
* @returns The length in model units
|
|
536
751
|
* @group base
|
|
537
752
|
* @shortname length
|
|
538
753
|
* @drawable false
|
|
754
|
+
* @example
|
|
755
|
+
* ```typescript
|
|
756
|
+
* const len = bitbybit.vector.length({ vector: [3, 4, 0] });
|
|
757
|
+
* ```
|
|
539
758
|
*/
|
|
540
759
|
length(inputs) {
|
|
541
760
|
return Math.sqrt(this.lengthSq(inputs));
|
|
542
761
|
}
|
|
543
762
|
/**
|
|
544
|
-
*
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
763
|
+
* Turns a list of number strings into numbers.
|
|
764
|
+
*
|
|
765
|
+
* A string that is not a number becomes NaN.
|
|
766
|
+
* Example: ['1', '2.5', '3'] -> [1, 2.5, 3]
|
|
767
|
+
* @param inputs - The strings to parse
|
|
768
|
+
* @returns The numbers
|
|
548
769
|
* @group create
|
|
549
770
|
* @shortname parse numbers
|
|
550
771
|
* @drawable false
|
|
772
|
+
* @example
|
|
773
|
+
* ```typescript
|
|
774
|
+
* const numbers = bitbybit.vector.parseNumbers({ vector: ["1", "2.5", "-3"] });
|
|
775
|
+
* ```
|
|
551
776
|
*/
|
|
552
777
|
parseNumbers(inputs) {
|
|
553
778
|
return inputs.vector.map(v => parseFloat(v));
|