forked from julemarie/bezier-splines
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbezierc2spline.js
More file actions
1145 lines (994 loc) · 32.6 KB
/
bezierc2spline.js
File metadata and controls
1145 lines (994 loc) · 32.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Code from
https://github.com/Zunawe/bezier-spline
https://github.com/Zunawe/vecn
copied into one single file for simple inclusion without npm.
*/
/*
Copyright (c) 2018 Bryce Wilson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* An object for memoizing vecType functions.
* @type {Object}
* @private
*/
let vecTypes = (function () {
const handler = {
get: function (obj, prop) {
if (!obj.hasOwnProperty(prop)) {
obj[prop] = getVecType(prop)
}
return obj[prop]
}
}
return new Proxy({}, handler)
})()
/**
* A class for fixed-size vectors of numbers.
* @extends Array
*/
class vecn extends Array {
/**
* Creates a vecn of the specified dimension. This should never be called
* by the user (as if this were an abstract class).
* @param {number} dimension The dimension of this vector.
* @param {number[]} [args=[]] The numbers to be put in the vector.
*/
constructor (dimension, args) {
args = flattenOuter(args)
if (!args.every((x) => type(x) === 'Number')) {
throw new TypeError('All arguments must be numbers.')
}
if (args.length > 1 && args.length !== dimension) {
throw new Error('Argument list must be empty, have a single number, or have a length equal to the dimension.')
}
if (args.length === 0) {
args = [0]
}
if (args.length === 1 && type(args[0]) === 'Number') {
args = Array(dimension).fill(args[0])
}
if (dimension > 1) {
super(...args)
} else {
super(1)
this[0] = args[0]
}
Reflect.defineProperty(this, 'pop', {
value: undefined,
enumerable: false
})
Reflect.defineProperty(this, 'push', {
value: undefined,
enumerable: false
})
Reflect.defineProperty(this, 'shift', {
value: undefined,
enumerable: false
})
Reflect.defineProperty(this, 'unshift', {
value: undefined,
enumerable: false
})
}
/**
* The L2 norm (Euclidian norm) of the vector.
* @type {number}
*/
get magnitude () {
return this.pnorm(2)
}
// --------------------------------------------------------------------------
// Arithmetic
/**
* Returns a vector where this is divided by v componentwise. If v is
* a single number, the vector is scaled by 1/v.
* @param {number|number[]} v The value to multiply with.
*
* @returns {vecn} A new vector with the divided components.
*/
div (v) {
checkCompatibility(v, this.dim, true)
if (type(v) === 'Number') {
v = (new Array(this.dim)).fill(v)
}
let result = []
for (let i = 0; i < this.length; ++i) {
result[i] = this[i] / v[i]
}
return vecTypes[this.dim](result)
}
/**
* Returns a vector where v is subtracted from the components of this
* vector. If v is a single number, it is subtracted to each component. If v
* is a vector, the vectors are combined componentwise.
* @param {number|number[]} v The value to subtract from this vector.
*
* @returns {vecn} A new vector with the combined components.
*/
minus (v) {
checkCompatibility(v, this.dim, true)
if (type(v) === 'Number') {
v = (new Array(this.dim)).fill(v)
}
let result = []
for (let i = 0; i < this.dim; ++i) {
result[i] = this[i] - v[i]
}
return vecTypes[this.dim](result)
}
/**
* Negates each element in this vector.
* @returns {vecn} A new vector where all elements are negated.
*/
neg () {
return vecTypes[this.dim](this.times(-1))
}
/**
* Returns a vector where v is added to the components of this vector. If v
* is a single number, it is added to each component. If v is a vector, the
* vectors are added componentwise.
* @param {number|number[]} v The value to add to this vector.
*
* @returns {vecn} A new vector with the summed components.
*/
plus (v) {
checkCompatibility(v, this.dim, true)
if (type(v) === 'Number') {
v = (new Array(this.dim)).fill(v)
}
let result = []
for (let i = 0; i < this.dim; ++i) {
result[i] = this[i] + v[i]
}
return vecTypes[this.dim](result)
}
/**
* Returns a vector where each component of this was raised to a power p.
* @param {number} p The power to raise each component by.
*
* @returns {vecn} A new vector with the exponentiated components.
*/
pow (p) {
let result = []
for (let i = 0; i < this.dim; ++i) {
result[i] = Math.pow(this[i], p)
}
return vecTypes[this.dim](result)
}
/**
* Returns a vector where v and this are multiplied componentwise. If v is
* a single number, the vector is scaled by v.
* @param {number|number[]} v The value to multiply with.
*
* @returns {vecn} A new vector with the multiplied components.
*/
times (v) {
checkCompatibility(v, this.dim, true)
if (type(v) === 'Number') {
v = (new Array(this.dim)).fill(v)
}
let result = []
for (let i = 0; i < this.dim; ++i) {
result[i] = this[i] * v[i]
}
return vecTypes[this.dim](result)
}
// --------------------------------------------------------------------------
// Vector Operations
/**
* Dot product of two vectors.
* @param {number[]} v The vector to dot with this one.
*
* @returns {number} The dot product between this and v.
*/
dot (v) {
checkCompatibility(v, this.dim)
let result = 0
for (let i = 0; i < this.dim; ++i) {
result += this[i] * v[i]
}
return result
}
/**
* Scales this vector to a magnitude of 1.
*
* @returns {vecn} A new vector with scaled components.
*/
normalize () {
return this.div(this.magnitude)
}
/**
* Evaluates the p-norm (or lp-norm) of this vector.
* @param {number} p The p-value to evaluate.
*
* @returns {number} The norm of this vector.
*/
pnorm (p) {
let result = 0
for (let i = 0; i < this.dim; ++i) {
result += Math.pow(Math.abs(this[i]), p)
}
return Math.pow(result, 1 / p)
}
/**
* Reflects this vector across the provided vector. The normal can be imagined
* as a surface normal or as describing a hyperpalane.
* @param {number[]} normal A vector describing the hyperplane to reflect off of.
*
* @returns {vecn} The reflected vector.
*/
reflect (normal) {
const n = normal.normalize()
return this.minus(n.times(2 * this.dot(n)))
}
// --------------------------------------------------------------------------
// Extras
/**
* Finds the indices of the max value in this vector.
*
* @returns {number[]} An array of indices corresponding to the max values.
*/
argmax () {
const maxVal = this.max()
return this.reduce((acc, x, i) => x === maxVal ? acc.concat([i]) : acc, [])
}
/**
* Finds the indices of the min value in this vector.
*
* @returns {number[]} An array of indices corresponding to the min values.
*/
argmin () {
const minVal = this.min()
return this.reduce((acc, x, i) => x === minVal ? acc.concat([i]) : acc, [])
}
/**
* Creates a new vector from the provided indices of this one. Basically
* equivalent to swizzling.
* @param {number[]} indices The indices to select into a new vector.
*
* @returns {vecn} A new vector from the provided indices.
*/
choose (indices) {
if (!Array.isArray(indices)) {
throw new TypeError('Argument must be a list of indices.')
}
if (!indices.every((i) => i < this.dim && isIndex(i.toString()))) {
throw new RangeError('All elements of argument must be valid indices.')
}
let v = []
indices.forEach((i) => v.push(this[i]))
return vecTypes[v.length](v)
}
/**
* Creates a duplicate of this vector. Same as passing this vector through
* the factory that created it.
*
* @returns {vecn} A deep copy of this vector.
*/
copy () {
return vecTypes[this.dim](this)
}
/**
* Returns whether every element in each vector is equal.
* @param {number[]} v A vector to test against.
*
* @returns {boolean} True if both vectors have the same dimension and values.
*/
equals (v) {
return v.length === this.dim && v.every((x, i) => this[i] === x)
}
/**
* Returns whether every element in each vector is approximately equal.
* @param {number[]} v A vector to test against.
* @param {number} epsilon The largest meaningful difference between two values.
*
* @returns {boolean} True if both vectors have the same dimension and the
* distance between each number is less than epsilon.
*/
approximatelyEquals (v, epsilon = 0.00000001) {
return v.length === this.dim && v.every((x, i) => Math.abs(this[i] - x) < epsilon)
}
/**
* Returns the max value of this vector.
*
* @returns {number} The max value of this vector.
*/
max () {
return Math.max(...this)
}
/**
* Returns the min value of this vector.
*
* @returns {number} The min value of this vector.
*/
min () {
return Math.min(...this)
}
/**
* Sums the components of this vector.
*
* @returns {number} The sum of the components of this vector.
*/
sum () {
return this.reduce((acc, x) => acc + x, 0)
}
/**
* Converts this vector into an Array.
*
* @returns {number[]} An array of the contents of this vector.
*/
toArray () {
return Array.from(this)
}
// --------------------------------------------------------------------------
// Array Overrides
/**
* Same as Array.prototype.concat, but return value is of a new vecType.
*
* @returns {vecn}
*/
concat (...args) {
const result = super.concat.apply(this.toArray(), args)
return vecTypes[result.length](result)
}
/**
* Same as Array.prototype.filter, but returns an Array if the result has 0
* entries.
*
* @returns {vecn|number[]}
*/
filter (...args) {
const result = super.filter.apply(this.toArray(), args)
if (result.length > 0) {
return vecTypes[result.length](result)
}
return result
}
/**
* Same as Array.prototype.map, but returns an Array if the result contains
* non-numbers.
*
* @returns {vecn|Array}
*/
map (...args) {
const result = super.map(...args)
if (result.every((x) => type(x) === 'Number')) {
return result
}
return result.toArray()
}
/**
* Same as Array.prototype.slice, but returns an Array if the result has 0
* entries.
*/
slice (...args) {
const result = super.slice.apply(this.toArray(), args)
if (result.length > 0) {
return vecTypes[result.length](result)
}
return result
}
/**
* A restrictive version of the Array.prototype.splice that requires all
* removed elements to be replaced.
*/
splice (...args) {
let test = this.toArray()
test.splice(...args)
if (test.length !== this.dim) {
throw new Error('All removed elements must be replaced.')
}
if (!test.every((x) => type(x) === 'Number')) {
throw new TypeError('All elements must be numbers.')
}
test.forEach((x, i) => { this[i] = x })
}
toString () {
return this.reduce((s, x, i) => {
return s + x + (i === this.dim - 1 ? ' ' : ', ')
}, '[ ') + ']'
}
}
// --------------------------------------------------------------------------
// General Tools
/**
* Adds an arbitrary number of vectors together. All vectors must be of the same
* dimension.
* @param {...vecn} vecs Vectors to add together.
*
* @returns {vecn} The sum of all the provided vectors.
*/
function add (...vecs) {
const dim = vecs[0].dim
if (!vecs.every((v) => v.dim === dim)) {
throw new TypeError('All vectors must have the same dimension.')
}
return vecs.reduce((acc, v) => acc.plus(v), vecTypes[dim]())
}
/**
* The validator to be used in the proxy for all vec objects. Catches swizzling
* properties, makes sure assignment only works for indices, and disallows
* non-numerical assignments. Used in getVecType.
* @constant
* @type {Object}
* @private
*/
const validator = {
set: function (obj, prop, value) {
if (prop === 'length') {
return false
}
if (isIndex(prop)) {
if (Number(prop) >= obj.dim) {
throw new RangeError('Vector may not have more elements than dimension.')
} else if (type(value) !== 'Number') {
throw new TypeError('Vectors may only contain numbers.')
} else {
obj[prop] = value
return true
}
}
const swizzleSymbolMap = getSwizzleSymbolMap(prop.toString())
if (obj.dim <= 4 && swizzleSymbolMap) {
swizzleSet(obj, prop.toString(), swizzleSymbolMap, value)
return true
}
return false
},
get: function (obj, prop) {
const swizzleSymbolMap = getSwizzleSymbolMap(prop.toString())
if (obj.dim <= 4 && swizzleSymbolMap) {
return swizzleGet(obj, prop, swizzleSymbolMap)
}
return obj[prop]
}
}
/**
* Returns a factory function for vectors of the specified dimension.
* @param {number} dim The dimension of the new vector type.
*
* @returns {Function} A factory (not a constructor) for creating new vecs.
*/
function getVecType (dim) {
dim = Number(dim)
if (!(dim in vecTypes)) {
if (isNaN(dim)) throw new TypeError('Dimension must be coercible to a number.')
if (dim <= 0) throw new RangeError('Dimension must be positive.')
if (!Number.isInteger(dim)) throw new RangeError('Dimension must be positive.')
// Doing a little bit of exploiting ES6 to dynamically name the class
let classname = 'vec' + dim
let VecType = ({[classname]: class extends vecn {
constructor (...args) {
if (args.length === 1 && args[0] instanceof vecn) {
if (args[0].dim > dim) {
throw new TypeError('Cannot demote vectors.')
}
args = promoteArrayDimension(args[0].toArray(), dim)
}
super(dim, args)
Reflect.defineProperty(this, 'dim', {
value: dim,
writable: false,
enumerable: false
})
}
}})[classname]
let factory = function factory (...args) {
let target = new VecType(...args)
Object.preventExtensions(target)
return new Proxy(target, validator)
}
vecTypes[dim] = factory
}
return vecTypes[dim]
}
/**
* The correct function for determining whether an object is a vecn.
* @param {*} v The object in question.
*
* @returns {boolean} True if the object is an instance of vecn.
*/
function isVec (v) {
return v instanceof vecn
}
/**
* Linearly interpolates between two vectors.
* @param {vecn} v1 The starting vector.
* @param {vecn} v2 The ending vector.
* @param {number} t The interpolant, which is clamped to the inteval [0, 1].
*
* @returns {vecn} The interpolated vector.
*/
function lerp (v1, v2, t) {
if (v1.dim !== v2.dim) throw new TypeError('Vectors must have the same dimension.')
t = t < 0 ? 0 : (t > 1 ? 1 : t)
return v1.plus(v2.minus(v1).times(t))
}
/**
* Multiplies an arbitrary number of vectors together. All vectors must be of the same
* dimension.
* @param {...vecn} vecs Vectors to multiply together.
*
* @returns {vecn} The product of all the provided vectors.
*/
function multiply (...vecs) {
const dim = vecs[0].dim
if (!vecs.every((v) => v.dim === dim)) throw new TypeError('All vectors must have the same dimension.')
return vecs.reduce((acc, v) => acc.times(v), vecTypes[dim](1))
}
/**
* Spherically interpolates between two vectors.
* @param {vecn} v1 The starting vector.
* @param {vecn} v2 The ending vector.
* @param {number} t The interpolant, which is clamped to the inteval [0, 1].
*
* @returns {vecn} The interpolated vector.
*/
function slerp (v1, v2, t) {
if (v1.dim !== v2.dim) throw new TypeError('Vectors must have the same dimension.')
t = t < 0 ? 0 : (t > 1 ? 1 : t)
let dot = v1.normalize().dot(v2.normalize())
dot = dot < -1 ? -1 : (dot > 1 ? 1 : dot)
const theta = Math.acos(dot) * t
const relative = v2.minus(v1.times(dot)).normalize()
const magnitude = v1.magnitude + ((v2.magnitude - v1.magnitude) * t)
return v1.times(Math.cos(theta)).plus(relative.times(Math.sin(theta)))
.normalize().times(magnitude)
}
// --------------------------------------------------------------------------
// Swizzling
/**
* The index corresponding to common names for indexing vectors.
* @constant
* @type {Object}
* @private
*/
const namedIndices = [
{x: 0, y: 1, z: 2, w: 3},
{r: 0, g: 1, b: 2, a: 3},
{s: 0, t: 1, p: 2, q: 3}
]
/**
* Gets the set of symbols corresponding to indices used for swizzling.
* @private
* @param {string} s The string used as a property to swizzle.
*
* @returns {Object} A map from characters to indices.
*/
function getSwizzleSymbolMap (s) {
return namedIndices.find((map) => {
return s.split('').every((c) => c in map)
})
}
/**
* Creates a new vector from the named indices given by swizzling.
* @private
* @param {vecn} v The vector to pull data from. The dimension is assumed to be
* 2, 3, or 4, but this isn't enforced here.
* @param {string} s The property being used to swizzle (e.g. 'xxy' or 'z').
* @param {Object} set A map from characters to indices (assumed to be valid).
*
* @returns {undefined|number|vecn} Either undefined (if s isn't a valid swizzle
* string), a number (if s has a length of 1), or a vecn where the values have
* been rearranged according to the order given in s.
*/
function swizzleGet (v, s, set) {
const newDim = s.length
if (newDim === 1) {
return v[set[s]]
}
let values = s.split('').reduce((acc, x) => {
let i = set[x]
return acc && i < v.dim ? acc.concat([v[i]]) : undefined
}, [])
return values ? new vecTypes[newDim](...values) : undefined
}
/**
* Assigns the indexed values in v to the values in newVals in the order they
* are described in in s.
* @private
* @param {vecn} v The starting vector.
* @param {string} s The property being used to swizzle (e.g. 'xyz' or 'xz').
* @param {Object} map A map from characters to indices (assumed to be valid).
* @param {number|number[]} newVals The right hand side of the assignment
*
* @returns {vecn} A copy of v with the correct elements replaced.
*/
function swizzleSet (v, s, map, newVals) {
if (s.length === 1) {
if (type(newVals) !== 'Number') {
throw new TypeError('Must set to a number')
}
v[map[s]] = newVals
return
}
if (!Array.isArray(newVals)) throw new TypeError('Right-hand side must be an array.')
if (s.length !== newVals.length) throw new TypeError('Right-hand side must have matching length.')
if (!newVals.every((item) => type(item) === 'Number')) throw new TypeError('All new values must be numbers.')
if (s.split('').some((c) => map[c] >= v.dim)) {
return
}
let valid = true
for (let i = 0, unique = {}; i < s.length; ++i) {
if (unique.hasOwnProperty(s[i])) {
valid = false
break
}
unique[s[i]] = true
}
if (!valid) throw new SyntaxError('Swizzle assignment does not allow symbols to be repeated.')
s.split('').map((c) => map[c]).forEach((index, i) => { v[index] = newVals[i] })
}
// --------------------------------------------------------------------------
// Helpers
/**
* Checks whether something is valid to do vector operations with and throws
* a TypeError if not.
* @private
* @param {*} o An object to check.
* @param {number} dim The dimension to check against.
* @param {boolean} [numberValid=false] Whether scalars are compatible for the operation.
*/
function checkCompatibility (o, dim, numberValid = false) {
if (numberValid && type(o) === 'Number') {
return
} else if (o.length && o.length === dim) {
return
}
throw new TypeError(`Invalid argument. Input must have matching dimension${numberValid ? 'or be a scalar' : ''}.`)
}
/**
* Removes outer arrays and returns a reference to the innermost array. For
* example, [[1, 2]] becomes [1, 2]. [[[['a'], true]]] becomes [['a'], true].
* @private
* @param {Array} arr The array to flatten.
*
* @returns {Array} A reference to the innermost array in arr.
*/
function flattenOuter (arr) {
if (!(arr instanceof Array) || arr.length !== 1) {
return arr
}
if (arr[0] instanceof Array) {
return flattenOuter(arr[0])
}
return arr
}
/**
* Checks whether a provided string can be used as a valid index into an array.
* @private
* @param {string} n A string representation of the number in question.
*
* @returns {boolean} True if n can be used to index an array.
*/
function isIndex (n) {
return !isNaN(n) &&
Number(n).toString() === n &&
Number.isInteger(Number(n)) &&
Number(n) >= 0
}
/**
* Lengthens an exsting array and fills new entries with 0 (does not mutate).
* @private
* @param {Array} arr The source array.
* @param {number} dim The dimension of the new array.
*
* @returns {Array} A new array with length dim and arr as a prefix.
*/
function promoteArrayDimension (arr, dim) {
return [...Array(dim)].map((_, i) => i < arr.length ? arr[i] : 0)
}
/**
* Returns a string representing the type of an object. Similar to typeof, but
* better with wrapped primitives, null, Array, etc...
* @private
* @param {*} obj The object to check the type of.
*
* @returns {string} A capitalized string describing the perceived type (i.e. 'Number', 'Array', etc...)
*/
function type (obj) {
return Object.prototype.toString.call(obj).slice(8, -1)
}
/**
* Solves for all real roots of a cubic function of the form ax^3 + bx^2 + cx + d.
* @private
* @param {number} a The coefficient of the third-degree term.
* @param {number} b The coefficient of the second-degree term.
* @param {number} c The coefficient of the first-degree term.
* @param {number} d The constant term.
*
* @returns {number[]} A list of all real roots of the described function.
*/
function solveCubic (a, b, c, d) {
if (a === 0) {
return solveQuadratic(b, c, d)
}
let D = a * b * c * d * 18
D -= Math.pow(b, 3) * d * 4
D += Math.pow(b, 2) * Math.pow(c, 2)
D -= a * Math.pow(c, 3) * 4
D -= Math.pow(a, 2) * Math.pow(d, 2) * 27
let D0 = Math.pow(b, 2) - (a * c * 3)
if (D === 0) {
if (D0 === 0) {
let root1 = -b / (a * 3)
return [root1]
} else {
let root1 = a * b * c * 4
root1 -= a * a * d * 9
root1 -= b * b * b
root1 /= a * D0
let root2 = ((a * d * 9) - b * c) / (D0 * 2)
return [root1, root2]
}
} else {
let f = ((3 * (c / a)) - ((Math.pow(b, 2)) / (Math.pow(a, 2)))) / 3
let g = (2 * (Math.pow(b, 3)) / (Math.pow(a, 3)))
g -= 9 * b * c / Math.pow(a, 2)
g += 27 * d / a
g /= 27
let h = (Math.pow(g, 2) / 4) + (Math.pow(f, 3) / 27)
if (h > 0) {
let R = -(g / 2) + Math.sqrt(h)
let S = Math.cbrt(R)
let T = -(g / 2) - Math.sqrt(h)
let U = Math.cbrt(T)
let root1 = (S + U) - (b / (3 * a))
return [root1]
} else {
let i = Math.sqrt((Math.pow(g, 2) / 4) - h)
let j = Math.cbrt(i)
let k = Math.acos(-g / (2 * i))
let L = -j
let M = Math.cos(k / 3)
let N = Math.sqrt(3) * Math.sin(k / 3)
let P = -b / (3 * a)
let root1 = 2 * j * Math.cos(k / 3) - (b / (3 * a))
let root2 = (L * (M + N)) + P
let root3 = (L * (M - N)) + P
return [root1, root2, root3]
}
}
}
/**
* Solves for all real roots of a quadratic function of the form ax^2 + bx + c.
* @private
* @param {number} a The coefficient of the second-degree term.
* @param {number} b The coefficient of the first-degree term.
* @param {number} c The constant term.
*
* @returns {number[]} A list of all real roots of the described function.
*/
function solveQuadratic (a, b, c) {
if (a === 0) {
return solveLinear(b, c)
}
let d = Math.sqrt((b * b) - (4 * a * c))
let root1 = (-b - d) / (2 * a)
let root2 = (-b + d) / (2 * a)
return [root1, root2]
}
/**
* Solves for all real roots of a linear function of the form ax + b. If there
* are zero or infinitely many solutions, an empty array is returned.
* @private
* @param {number} a The coefficient of the first-degree term.
* @param {number} b The constant term.
*
* @returns {number[]} A list of all real roots of the described function.
*/
function solveLinear (a, b) {
if (a === 0) {
return []
}
return [-b / a]
}
/**
* A class with a couple helper functions for working with Bezier curves in
* multiple dimensions. Array-like with 4 elements.
*/
class BezierCurve {
/**
* Creates a curve from control points.
* @param {number[][]} controlPoints The control points that define a Bezier curve
*/
constructor (controlPoints) {
controlPoints = controlPoints[0].length ? controlPoints : controlPoints.map((n) => [n])
Reflect.defineProperty(this, 'length', {
value: 4,
enumerable: false,
writable: false
})
for (let i = 0; i < this.length; ++i) {
this[i] = Array.from(controlPoints[i])
}
}
/**
* Evaluates the bezier curve at the given value of t.
* @param {number} t The parameter to plug in. (Clamped to the interval [0, 1])
*
* @returns {number[]} The point at which t equals the provided value.
*/
at (t) {
t = t < 0 ? 0 : (t > 1 ? 1 : t)
let vec = getVecType(this[0].length)
let controlPoints = [...new Array(4)].map((_, i) => vec(this[i]))
let terms = []
terms.push(controlPoints[0].times(Math.pow(1 - t, 3)))
terms.push(controlPoints[1].times(3 * Math.pow(1 - t, 2) * t))
terms.push(controlPoints[2].times(3 * (1 - t) * Math.pow(t, 2)))
terms.push(controlPoints[3].times(Math.pow(t, 3)))
return add(...terms).toArray()
}
/**
* Finds all values of t for which a particular dimension is equal to a particular value.
* @param {number} [axis=0] The index of the axis along which to solve (i.e. if your vectors are [x, y, z], 0 means solve for when x = value).
* @param {number} [value=0] The value to solve for (i.e. the Bezier cubic is on the left of an equation and this value is on the right).
*
* @returns {number[]} All real roots of the described equation on the interval [0, 1].
*/
solve (axis = 0, value = 0) {
let points = Array.prototype.map.call(this, (v) => v[axis])
let a = -points[0] + (3 * points[1]) - (3 * points[2]) + points[3]
let b = (3 * points[0]) - (6 * points[1]) + (3 * points[2])
let c = -(3 * points[0]) + (3 * points[1])
let d = points[0] - value
return solveCubic(a, b, c, d)
.map((r) => r === 0 ? 0 : r)
.filter((t) => t >= 0 && t <= 1)
.sort()
}
}
/**
* Solves a matrix equation Ax = d for x where A is a tridiagonal square matrix.
* @private
* @param {number[]|number[][]} a The (i-1)th entry of each row. The first element does not exist, but must still be input as a number or vector. 0 is adequate.
* @param {number[]|number[][]} b The diagonal entry of each row.
* @param {number[]|number[][]} c The (i+1)th entry of each row. The last element does not exist, but must still be input as a number or vector. 0 is adequate.
* @param {number[]|number[][]} d The resultant vector in the equation.
*
* @returns {number[][]}
*/
function thomas (a, b, c, d) {
const dim = d[0].length ? d[0].length : 1
const vec = getVecType(dim)
const n = d.length
a = a.map((x) => vec(x))
b = b.map((x) => vec(x))