/
Point3dVector3d.ts
1615 lines (1607 loc) · 64.1 KB
/
Point3dVector3d.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module CartesianGeometry
*/
import { Geometry, PerpParallelOptions } from "../Geometry";
import { Point4d } from "../geometry4d/Point4d";
import { Angle } from "./Angle";
import { HasZ, XAndY, XYAndZ, XYZProps } from "./XYZProps";
// cspell:words CWXY CCWXY arctan Rodrigues
/**
* * `XYZ` is a minimal object containing x,y,z and operations that are meaningful without change in both
* point and vector.
* * `XYZ` is not instantiable.
* * The derived (instantiable) classes are
* * `Point3d`
* * `Vector3d`
* @public
*/
export class XYZ implements XYAndZ {
/** x coordinate */
public x: number;
/** y coordinate */
public y: number;
/** z coordinate */
public z: number;
/**
* Set the x,y,z parts.
* @param x (optional) x part
* @param y (optional) y part
* @param z (optional) z part
*/
public set(x: number = 0, y: number = 0, z: number = 0) {
this.x = x;
this.y = y;
this.z = z;
}
/** Set the x,y,z parts to zero. */
public setZero() {
this.x = 0;
this.y = 0;
this.z = 0;
}
protected constructor(x: number = 0, y: number = 0, z: number = 0) {
this.x = x;
this.y = y;
this.z = z;
}
/** Type guard for XAndY.
* @note this will return true for an XYAndZ. If you wish to distinguish between the two, call isXYAndZ first.
*/
public static isXAndY(arg: any): arg is XAndY {
return arg.x !== undefined && arg.y !== undefined;
}
/** Type guard to determine whether an object has a member called "z" */
public static hasZ(arg: any): arg is HasZ {
return arg.z !== undefined;
}
/** Type guard for XYAndZ. */
public static isXYAndZ(arg: any): arg is XYAndZ {
return this.isXAndY(arg) && this.hasZ(arg);
}
/**
* Test if arg is any of:
* * XAndY
* * XYAndZ
* * [number,number]
* * [number,number,number]
*/
public static isAnyImmediatePointType(arg: any): arg is XAndY | XYAndZ | number[] {
return Point3d.isXAndY(arg) || Geometry.isNumberArray(arg, 2);
}
/**
* Look for (in order) an x coordinate present as:
* * arg.x
* * arg[0]
*/
public static accessX(arg: any, defaultValue?: number): number | undefined {
if (arg.x !== undefined)
return arg.x;
if (Array.isArray(arg) && arg.length > 0 && Number.isFinite(arg[0]))
return arg[0];
return defaultValue;
}
/**
* Look for (in order) an x coordinate present as:
* * arg.y
* * arg[1]
*/
public static accessY(arg: any, defaultValue?: number): number | undefined {
if (arg.y !== undefined)
return arg.y;
if (Array.isArray(arg) && arg.length > 1 && Number.isFinite(arg[1]))
return arg[1];
return defaultValue;
}
/**
* Look for (in order) an x coordinate present as:
* * arg.z
* * arg[2]
*/
public static accessZ(arg: any, defaultValue?: number): number | undefined {
if (arg.z !== undefined)
return arg.z;
if (Array.isArray(arg) && arg.length > 2 && Number.isFinite(arg[2]))
return arg[2];
return defaultValue;
}
/**
* Set the x,y,z parts from one of these input types
* * XYZ -- copy the x,y,z parts
* * Float64Array -- Copy from indices 0,1,2 to x,y,z
* * XY -- copy the x, y parts and set z=0
*/
public setFrom(other: Float64Array | XAndY | XYAndZ | undefined) {
if (other === undefined) {
this.setZero();
} else if (XYZ.isXAndY(other)) {
this.x = other.x;
this.y = other.y;
this.z = XYZ.hasZ(other) ? other.z : 0;
} else {
this.x = other[0];
this.y = other[1];
this.z = other[2];
}
}
/**
* Set the x,y,z parts from a Point3d.
* This is the same effect as `setFrom(other)` with no pretesting of variant input type
* * Set to zeros if `other` is undefined.
*/
public setFromPoint3d(other?: XYAndZ) {
if (other) {
this.x = other.x;
this.y = other.y;
this.z = other.z;
} else {
this.setZero();
}
}
/**
* Set the x,y,z parts from a Vector3d
* This is the same effect as `setFrom(other)` with no pretesting of variant input type
* * Set to zeros if `other` is undefined.
*/
public setFromVector3d(other?: Vector3d) {
if (other) {
this.x = other.x;
this.y = other.y;
this.z = other.z;
} else {
this.setZero();
}
}
/**
* Returns true if this and other have equal x,y,z parts within Geometry.smallMetricDistance.
* @param other The other XYAndZ to compare
* @param tol The tolerance for the comparison. If undefined, use [[Geometry.smallMetricDistance]]
*/
public isAlmostEqual(other: Readonly<XYAndZ>, tol?: number): boolean {
return XYAndZ.almostEqual(this, other, tol);
}
/** Return true if this and other have equal x,y,z parts within Geometry.smallMetricDistance. */
public isAlmostEqualXYZ(x: number, y: number, z: number, tol?: number): boolean {
return Geometry.isSameCoordinate(this.x, x, tol)
&& Geometry.isSameCoordinate(this.y, y, tol)
&& Geometry.isSameCoordinate(this.z, z, tol);
}
/**
* Return true if this and {other + vector*scale} have equal x,y,z parts within Geometry.smallMetricDistance.
* * this method is useful in testing "point on ray" without explicitly constructing the projection point
*/
public isAlmostEqualPointPlusScaledVector(other: XYAndZ, vector: XYAndZ, scale: number, tol?: number): boolean {
return Geometry.isSameCoordinate(this.x, other.x + vector.x * scale, tol)
&& Geometry.isSameCoordinate(this.y, other.y + vector.y * scale, tol)
&& Geometry.isSameCoordinate(this.z, other.z + vector.z * scale, tol);
}
/** Return true if this and other have equal x,y parts within Geometry.smallMetricDistance. */
public isAlmostEqualXY(other: XAndY, tol?: number): boolean {
return Geometry.isSameCoordinate(this.x, other.x, tol)
&& Geometry.isSameCoordinate(this.y, other.y, tol);
}
/** Return a JSON object as array `[x,y,z]` */
public toJSON(): XYZProps {
return this.toArray();
}
/** Return as an array `[x,y,z]` */
public toArray(): number[] {
return [this.x, this.y, this.z];
}
/** Return a JSON object as key value pairs `{x: value, y: value, z: value}` */
public toJSONXYZ(): XYZProps {
return { x: this.x, y: this.y, z: this.z };
}
/** Pack the x,y,z values in a Float64Array. */
public toFloat64Array(): Float64Array {
return Float64Array.of(this.x, this.y, this.z);
}
/**
* Set the x,y,z properties from one of several json forms:
*
* * array of numbers: [x,y,z]
* * object with x,y, and (optional) z as numeric properties {x: xValue, y: yValue, z: zValue}
*/
public setFromJSON(json?: XYZProps): void {
if (Array.isArray(json)) {
this.set(json[0] || 0, json[1] || 0, json[2] || 0);
return;
}
if (json) {
this.set(json.x || 0, json.y || 0, json.z || 0);
return;
}
this.set(0, 0, 0);
}
/** Return the distance from this point to other */
public distance(other: XYAndZ): number {
const xDist = other.x - this.x;
const yDist = other.y - this.y;
const zDist = other.z - this.z;
return (Math.sqrt(xDist * xDist + yDist * yDist + zDist * zDist));
}
/** Return squared distance from this point to other */
public distanceSquared(other: XYAndZ): number {
const xDist = other.x - this.x;
const yDist = other.y - this.y;
const zDist = other.z - this.z;
return (xDist * xDist + yDist * yDist + zDist * zDist);
}
/** Return the XY distance from this point to other */
public distanceXY(other: XAndY): number {
const xDist = other.x - this.x;
const yDist = other.y - this.y;
return (Math.sqrt(xDist * xDist + yDist * yDist));
}
/** Return squared XY distance from this point to other */
public distanceSquaredXY(other: XAndY): number {
const xDist = other.x - this.x;
const yDist = other.y - this.y;
return (xDist * xDist + yDist * yDist);
}
/** Return the largest absolute distance between corresponding components */
public maxDiff(other: XYAndZ): number {
return Math.max(Math.abs(this.x - other.x), Math.abs(this.y - other.y), Math.abs(this.z - other.z));
}
/** Return the x,y, z component corresponding to 0,1,2 */
public at(index: number): number {
if (index < 0.5)
return this.x;
if (index > 1.5)
return this.z;
return this.y;
}
/** Set value at index 0 or 1 or 2 */
public setAt(index: number, value: number): void {
if (index < 0.5)
this.x = value;
else if (index > 1.5)
this.z = value;
else
this.y = value;
}
/** Return the index (0,1,2) of the x,y,z component with largest absolute value */
public indexOfMaxAbs(): number {
let index = 0;
let a = Math.abs(this.x);
let b = Math.abs(this.y);
if (b > a) {
index = 1;
a = b;
}
b = Math.abs(this.z);
if (b > a) {
index = 2;
}
return index;
}
/** Return true if the x,y,z components are all nearly zero to tolerance Geometry.smallMetricDistance */
public get isAlmostZero(): boolean {
return Geometry.isSmallMetricDistance(this.x) &&
Geometry.isSmallMetricDistance(this.y) &&
Geometry.isSmallMetricDistance(this.z);
}
/** Return true if the x,y,z components are all exactly zero */
public get isZero(): boolean {
return this.x === 0.0 && this.y === 0.0 && this.z === 0.0;
}
/** Return the largest absolute value of any component */
public maxAbs(): number {
return Math.max(Math.abs(this.x), Math.abs(this.y), Math.abs(this.z));
}
/** Return the sqrt of the sum of squared x,y,z parts */
public magnitude(): number {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
/** Return the sum of squared x,y,z parts */
public magnitudeSquared(): number {
return this.x * this.x + this.y * this.y + this.z * this.z;
}
/** Return sqrt of the sum of squared x,y parts */
public magnitudeXY(): number {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
/** Return the sum of squared x,y parts */
public magnitudeSquaredXY(): number {
return this.x * this.x + this.y * this.y;
}
/** Exact equality test. */
public isExactEqual(other: XYAndZ): boolean {
return this.x === other.x && this.y === other.y && this.z === other.z;
}
/** Equality test with Geometry.smallMetricDistance tolerance */
public isAlmostEqualMetric(other: XYAndZ): boolean {
return this.maxDiff(other) <= Geometry.smallMetricDistance;
}
/** Add x,y,z from other in place. */
public addInPlace(other: XYAndZ): void {
this.x += other.x;
this.y += other.y;
this.z += other.z;
}
/** Add x,y,z from other in place. */
public subtractInPlace(other: XYAndZ): void {
this.x -= other.x;
this.y -= other.y;
this.z -= other.z;
}
/** Add (in place) the scaled x,y,z of other */
public addScaledInPlace(other: XYAndZ, scale: number): void {
this.x += scale * other.x;
this.y += scale * other.y;
this.z += scale * other.z;
}
/** Multiply the x, y, z parts by scale. */
public scaleInPlace(scale: number) {
this.x *= scale;
this.y *= scale;
this.z *= scale;
}
/** Add to x, y, z parts */
public addXYZInPlace(dx: number = 0.0, dy: number = 0.0, dz: number = 0.0) {
this.x += dx;
this.y += dy;
this.z += dz;
}
/** Clone strongly typed as Point3d */
public cloneAsPoint3d(): Point3d {
return Point3d.create(this.x, this.y, this.z);
}
/** Return a (full length) vector from this point to other */
public vectorTo(other: XYAndZ, result?: Vector3d): Vector3d {
return Vector3d.create(other.x - this.x, other.y - this.y, other.z - this.z, result);
}
/** Return a multiple of a the (full length) vector from this point to other */
public scaledVectorTo(other: XYAndZ, scale: number, result?: Vector3d): Vector3d {
return Vector3d.create(scale * (other.x - this.x),
scale * (other.y - this.y),
scale * (other.z - this.z), result);
}
/**
* Return a unit vector from this vector to other. Return a 000 vector if the input is too small to normalize.
* @param other target of created vector.
* @param result optional result vector.
*/
public unitVectorTo(target: XYAndZ, result?: Vector3d): Vector3d | undefined {
return this.vectorTo(target, result).normalize(result);
}
/** Freeze this XYZ */
public freeze(): Readonly<this> {
return Object.freeze(this);
}
/** Access x part of XYZProps (which may be .x or [0]) */
public static x(xyz: XYZProps | undefined, defaultValue: number = 0): number {
if (xyz === undefined)
return defaultValue;
if (Array.isArray(xyz))
return xyz[0];
if (xyz.x !== undefined)
return xyz.x;
return defaultValue;
}
/** Access x part of XYZProps (which may be .x or [0]) */
public static y(xyz: XYZProps | undefined, defaultValue: number = 0): number {
if (xyz === undefined)
return defaultValue;
if (Array.isArray(xyz))
return xyz[1];
if (xyz.y !== undefined)
return xyz.y;
return defaultValue;
}
/** Access x part of XYZProps (which may be .x or [0]) */
public static z(xyz: XYZProps | undefined, defaultValue: number = 0): number {
if (xyz === undefined)
return defaultValue;
if (Array.isArray(xyz))
return xyz[2];
if (xyz.z !== undefined)
return xyz.z;
return defaultValue;
}
}
/** 3D point with `x`,`y`,`z` as properties
* @public
*/
export class Point3d extends XYZ {
/** Constructor for Point3d */
constructor(x: number = 0, y: number = 0, z: number = 0) {
super(x, y, z);
}
/**
* Convert json to Point3d. Accepted forms are:
* * `[1,2,3]` --- array of numbers
* * array of numbers: [x,y,z]
* * object with x,y, and (optional) z as numeric properties {x: xValue, y: yValue, z: zValue}
* @param json json value.
*/
public static fromJSON(json?: XYZProps): Point3d {
const val = new Point3d();
val.setFromJSON(json);
return val;
}
/** Return a new Point3d with the same coordinates */
public clone(result?: Point3d): Point3d {
return Point3d.create(this.x, this.y, this.z, result);
}
/**
* Create a new Point3d with given coordinates
* @param x x part
* @param y y part
* @param z z part
*/
public static create(x: number = 0, y: number = 0, z: number = 0, result?: Point3d): Point3d {
if (result) {
result.x = x;
result.y = y;
result.z = z;
return result;
}
return new Point3d(x, y, z);
}
/** Copy contents from another Point3d, Point2d, Vector2d, or Vector3d */
public static createFrom(data: XYAndZ | XAndY | Float64Array, result?: Point3d): Point3d {
if (data instanceof Float64Array) {
let x = 0;
let y = 0;
let z = 0;
if (data.length > 0)
x = data[0];
if (data.length > 1)
y = data[1];
if (data.length > 2)
z = data[2];
return Point3d.create(x, y, z, result);
}
return Point3d.create(data.x, data.y, XYZ.hasZ(data) ? data.z : 0, result);
}
/**
* Copy x,y,z from
* @param xyzData flat array of xyzxyz for multiple points
* @param pointIndex index of point to extract. This index is multiplied by 3 to obtain starting index in the array.
* @param result optional result point.
*/
public static createFromPacked(xyzData: Float64Array, pointIndex: number, result?: Point3d): Point3d | undefined {
const indexX = pointIndex * 3;
if (indexX >= 0 && indexX + 2 < xyzData.length)
return Point3d.create(xyzData[indexX], xyzData[indexX + 1], xyzData[indexX + 2], result);
return undefined;
}
/**
* Copy and unweight xyzw.
* @param xyzwData flat array of weighted homogeneous points: xw,yw,zw,w
* @param pointIndex index of point to extract. This index is multiplied by 4 to obtain starting index in the array.
* @param result optional result point.
* @return unweighted xyz
*/
public static createFromPackedXYZW(xyzwData: Float64Array, pointIndex: number, result?: Point3d): Point3d | undefined {
const indexX = pointIndex * 4;
if (indexX >= 0 && indexX + 3 < xyzwData.length) {
const w = xyzwData[indexX + 3];
if (!Geometry.isSmallMetricDistance(w)) {
const divW = 1.0 / w;
return Point3d.create(divW * xyzwData[indexX], divW * xyzwData[indexX + 1], divW * xyzwData[indexX + 2], result);
}
}
return undefined;
}
/**
* Return an array of points constructed from groups of 3 entries in a Float64Array.
* Any incomplete group at the tail of the array is ignored.
*/
public static createArrayFromPackedXYZ(data: Float64Array): Point3d[] {
const result = [];
for (let i = 0; i + 2 < data.length; i += 3)
result.push(new Point3d(data[i], data[i + 1], data[i + 2]));
return result;
}
/** Create a new point with 000 xyz */
public static createZero(result?: Point3d): Point3d {
return Point3d.create(0, 0, 0, result);
}
/**
* Return the cross product of the vectors from this to pointA and pointB
* * the result is a vector
* * the result is perpendicular to both vectors, with right hand orientation
* * the magnitude of the vector is twice the area of the triangle.
*/
public crossProductToPoints(pointA: Point3d, pointB: Point3d, result?: Vector3d): Vector3d {
return Vector3d.createCrossProduct(
pointA.x - this.x, pointA.y - this.y, pointA.z - this.z,
pointB.x - this.x, pointB.y - this.y, pointB.z - this.z,
result,
);
}
/** Return the magnitude of the cross product of the vectors from this to pointA and pointB */
public crossProductToPointsMagnitude(pointA: Point3d, pointB: Point3d): number {
return Geometry.crossProductMagnitude(
pointA.x - this.x, pointA.y - this.y, pointA.z - this.z,
pointB.x - this.x, pointB.y - this.y, pointB.z - this.z,
);
}
/**
* Return the triple product of the vectors from this to pointA, pointB, pointC
* * This is a scalar (number)
* * This is 6 times the (signed) volume of the tetrahedron on the 4 points.
*/
public tripleProductToPoints(pointA: Point3d, pointB: Point3d, pointC: Point3d): number {
return Geometry.tripleProduct(
pointA.x - this.x, pointA.y - this.y, pointA.z - this.z,
pointB.x - this.x, pointB.y - this.y, pointB.z - this.z,
pointC.x - this.x, pointC.y - this.y, pointC.z - this.z,
);
}
/**
* Return the cross product of the vectors from this to pointA and pointB
* * the result is a scalar
* * the magnitude of the vector is twice the signed area of the triangle.
* * this is positive for counter-clockwise order of the points, negative for clockwise.
*/
public crossProductToPointsXY(pointA: Point3d, pointB: Point3d): number {
return Geometry.crossProductXYXY(pointA.x - this.x, pointA.y - this.y, pointB.x - this.x, pointB.y - this.y);
}
/**
* Return a point interpolated between `this` point and the `other` point.
* * Fraction specifies where the interpolated point is located on the line passing `this` and `other`.
*/
public interpolate(fraction: number, other: XYAndZ, result?: Point3d): Point3d {
if (fraction <= 0.5)
return Point3d.create(
this.x + fraction * (other.x - this.x),
this.y + fraction * (other.y - this.y),
this.z + fraction * (other.z - this.z),
result,
);
const t: number = fraction - 1.0;
return Point3d.create(
other.x + t * (other.x - this.x),
other.y + t * (other.y - this.y),
other.z + t * (other.z - this.z),
result,
);
}
/** Return a point with independent x,y,z fractional interpolation. */
public interpolateXYZ(
fractionX: number, fractionY: number, fractionZ: number, other: Point3d, result?: Point3d,
): Point3d {
return Point3d.create(
Geometry.interpolate(this.x, fractionX, other.x),
Geometry.interpolate(this.y, fractionY, other.y),
Geometry.interpolate(this.z, fractionZ, other.z),
result,
);
}
/** Interpolate between points, then add a shift in the xy plane by a fraction of the XY projection perpendicular. */
public interpolatePerpendicularXY(
fraction: number, pointB: Point3d, fractionXYPerp: number, result?: Point3d,
): Point3d {
result = result ? result : new Point3d();
const vector = pointB.minus(this);
this.interpolate(fraction, pointB, result);
result.x -= fractionXYPerp * vector.y;
result.y += fractionXYPerp * vector.x;
return result;
}
/** Return point minus vector */
public minus(vector: XYAndZ, result?: Point3d): Point3d {
return Point3d.create(this.x - vector.x, this.y - vector.y, this.z - vector.z, result);
}
/** Return point plus vector */
public plus(vector: XYAndZ, result?: Point3d): Point3d {
return Point3d.create(this.x + vector.x, this.y + vector.y, this.z + vector.z, result);
}
/** Return point plus vector */
public plusXYZ(dx: number = 0, dy: number = 0, dz: number = 0, result?: Point3d): Point3d {
return Point3d.create(this.x + dx, this.y + dy, this.z + dz, result);
}
/** Return point + vector * scalar */
public plusScaled(vector: XYAndZ, scaleFactor: number, result?: Point3d): Point3d {
return Point3d.create(this.x + vector.x * scaleFactor,
this.y + vector.y * scaleFactor,
this.z + vector.z * scaleFactor,
result,
);
}
/** Return point + vectorA * scalarA + vectorB * scalarB */
public plus2Scaled(vectorA: XYAndZ, scalarA: number, vectorB: XYZ, scalarB: number, result?: Point3d): Point3d {
return Point3d.create(this.x + vectorA.x * scalarA + vectorB.x * scalarB,
this.y + vectorA.y * scalarA + vectorB.y * scalarB,
this.z + vectorA.z * scalarA + vectorB.z * scalarB,
result,
);
}
/** Return point + vectorA * scalarA + vectorB * scalarB + vectorC * scalarC */
public plus3Scaled(
vectorA: XYAndZ, scalarA: number, vectorB: XYAndZ, scalarB: number, vectorC: XYAndZ, scalarC: number, result?: Point3d,
): Point3d {
return Point3d.create(
this.x + vectorA.x * scalarA + vectorB.x * scalarB + vectorC.x * scalarC,
this.y + vectorA.y * scalarA + vectorB.y * scalarB + vectorC.y * scalarC,
this.z + vectorA.z * scalarA + vectorB.z * scalarB + vectorC.z * scalarC,
result,
);
}
/**
* Return a point that is scaled from the source point.
* @param source existing point
* @param scale scale factor to apply to its x,y,z parts
* @param result optional point to receive coordinates
*/
public static createScale(source: XYAndZ, scale: number, result?: Point3d): Point3d {
return Point3d.create(source.x * scale, source.y * scale, source.z * scale, result);
}
/**
* Create a point that is a linear combination (weighted sum) of 2 input points.
* @param pointA first input point
* @param scaleA scale factor for pointA
* @param pointB second input point
* @param scaleB scale factor for pointB
*/
public static createAdd2Scaled(
pointA: XYAndZ, scaleA: number, pointB: XYAndZ, scaleB: number, result?: Point3d,
): Point3d {
return Point3d.create(
pointA.x * scaleA + pointB.x * scaleB,
pointA.y * scaleA + pointB.y * scaleB,
pointA.z * scaleA + pointB.z * scaleB,
result,
);
}
/** Create a point that is a linear combination (weighted sum) of 3 input points.
* @param pointA first input point
* @param scaleA scale factor for pointA
* @param pointB second input point
* @param scaleB scale factor for pointB
* @param pointC third input point.
* @param scaleC scale factor for pointC
*/
public static createAdd3Scaled(
pointA: XYAndZ, scaleA: number, pointB: XYAndZ, scaleB: number, pointC: XYAndZ, scaleC: number, result?: Point3d,
): Point3d {
return Point3d.create(
pointA.x * scaleA + pointB.x * scaleB + pointC.x * scaleC,
pointA.y * scaleA + pointB.y * scaleB + pointC.y * scaleC,
pointA.z * scaleA + pointB.z * scaleB + pointC.z * scaleC,
result,
);
}
/**
* Return the dot product of vectors from this to pointA and this to pointB.
* @param targetA target point for first vector
* @param targetB target point for second vector
*/
public dotVectorsToTargets(targetA: Point3d, targetB: Point3d): number {
return (targetA.x - this.x) * (targetB.x - this.x) +
(targetA.y - this.y) * (targetB.y - this.y) +
(targetA.z - this.z) * (targetB.z - this.z);
}
/** Return the fractional projection of this onto a line between points. */
public fractionOfProjectionToLine(startPoint: Point3d, endPoint: Point3d, defaultFraction: number = 0): number {
const denominator = startPoint.distanceSquared(endPoint);
if (denominator < Geometry.smallMetricDistanceSquared)
return defaultFraction;
return startPoint.dotVectorsToTargets(endPoint, this) / denominator;
}
}
/**
* 3D vector with `x`,`y`,`z` as properties
* @public
*/
export class Vector3d extends XYZ {
constructor(x: number = 0, y: number = 0, z: number = 0) {
super(x, y, z);
}
/**
* Return an array of vectors constructed from groups of 3 entries in a Float64Array.
* Any incomplete group at the tail of the array is ignored.
*/
public static createArrayFromPackedXYZ(data: Float64Array): Vector3d[] {
const result = [];
for (let i = 0; i + 2 < data.length; i += 3)
result.push(new Vector3d(data[i], data[i + 1], data[i + 2]));
return result;
}
/**
* Copy xyz from this instance to a new (or optionally reused) Vector3d
* @param result optional instance to reuse.
*/
public clone(result?: Vector3d): Vector3d {
return Vector3d.create(this.x, this.y, this.z, result);
}
/**
* Return a Vector3d (new or reused from optional result)
* @param x x component
* @param y y component
* @param z z component
* @param result optional instance to reuse
*/
public static create(x: number = 0, y: number = 0, z: number = 0, result?: Vector3d): Vector3d {
if (result) {
result.x = x;
result.y = y;
result.z = z;
return result;
}
return new Vector3d(x, y, z);
}
/**
* Create a vector which is cross product of two vectors supplied as separate arguments
* @param ux x coordinate of vector u
* @param uy y coordinate of vector u
* @param uz z coordinate of vector u
* @param vx x coordinate of vector v
* @param vy y coordinate of vector v
* @param vz z coordinate of vector v
* @param result optional result vector.
*/
public static createCrossProduct(
ux: number, uy: number, uz: number, vx: number, vy: number, vz: number, result?: Vector3d,
): Vector3d {
return Vector3d.create(uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx, result);
}
/**
* Accumulate a vector which is cross product vectors from origin (ax,ay,az) to targets (bx,by,bz) and (cx,cy,cz)
* @param ax x coordinate of origin
* @param ay y coordinate of origin
* @param az z coordinate of origin
* @param bx x coordinate of target point b
* @param by y coordinate of target point b
* @param bz z coordinate of target point b
* @param cx x coordinate of target point c
* @param cy y coordinate of target point c
* @param cz z coordinate of target point c
*/
public addCrossProductToTargetsInPlace(
ax: number, ay: number, az: number, bx: number, by: number, bz: number, cx: number, cy: number, cz: number,
) {
const ux = bx - ax;
const uy = by - ay;
const uz = bz - az;
const vx = cx - ax;
const vy = cy - ay;
const vz = cz - az;
this.x += uy * vz - uz * vy;
this.y += uz * vx - ux * vz;
this.z += ux * vy - uy * vx;
}
/**
* Return the cross product of the vectors from origin to pointA and pointB.
* * the result is a vector
* * the result is perpendicular to both vectors, with right hand orientation
* * the magnitude of the vector is twice the area of the triangle.
*/
public static createCrossProductToPoints(origin: XYAndZ, pointA: XYAndZ, pointB: XYAndZ, result?: Vector3d): Vector3d {
return Vector3d.createCrossProduct(pointA.x - origin.x, pointA.y - origin.y, pointA.z - origin.z,
pointB.x - origin.x, pointB.y - origin.y, pointB.z - origin.z, result);
}
/**
* Return the NORMALIZED cross product of the vectors from origin to pointA and pointB, or undefined
*
* * the result is a vector
* * the result is perpendicular to both vectors, with right hand orientation
* * the magnitude of the vector is twice the area of the triangle.
*/
public static createUnitCrossProductToPoints(origin: XYAndZ, pointA: XYAndZ, pointB: XYAndZ, result?: Vector3d): Vector3d | undefined {
const vector = Vector3d.createCrossProduct(pointA.x - origin.x, pointA.y - origin.y, pointA.z - origin.z,
pointB.x - origin.x, pointB.y - origin.y, pointB.z - origin.z, result);
return vector.normalize();
}
/**
* Return a vector defined by polar coordinates distance and angle from x axis
* @param r distance measured from origin
* @param theta angle from x axis to the vector (in xy plane)
* @param z optional z coordinate
*/
public static createPolar(r: number, theta: Angle, z?: number): Vector3d {
return Vector3d.create(r * theta.cos(), r * theta.sin(), z);
}
/**
* Return a vector defined in spherical coordinates.
* @param r sphere radius
* @param theta angle in xy plane
* @param phi angle from xy plane to the vector
*/
public static createSpherical(r: number, theta: Angle, phi: Angle): Vector3d {
const cosPhi = phi.cos();
return Vector3d.create(cosPhi * r * theta.cos(), cosPhi * r * theta.sin(), r * phi.sin());
}
/**
* Convert json to Vector3d. Accepted forms are:
* * `[1,2,3]` --- array of numbers
* * array of numbers: [x,y,z]
* * object with x,y, and (optional) z as numeric properties {x: xValue, y: yValue, z: zValue}
* @param json json value.
*/
public static fromJSON(json?: XYZProps): Vector3d {
const val = new Vector3d();
val.setFromJSON(json);
return val;
}
/** Copy contents from another Point3d, Point2d, Vector2d, or Vector3d */
public static createFrom(data: XYAndZ | XAndY | Float64Array | number[], result?: Vector3d): Vector3d {
if (data instanceof Float64Array) {
let x = 0;
let y = 0;
let z = 0;
if (data.length > 0)
x = data[0];
if (data.length > 1)
y = data[1];
if (data.length > 2)
z = data[2];
return Vector3d.create(x, y, z, result);
} else if (Array.isArray(data)) {
return Vector3d.create(data[0], data[1], data.length > 2 ? data[2] : 0);
}
return Vector3d.create(data.x, data.y, XYZ.hasZ(data) ? data.z : 0.0, result);
}
/**
* Return a vector defined by start and end points (end - start).
* @param start start point for vector.
* @param end end point for vector.
* @param result optional result.
*/
public static createStartEnd(start: XAndY | XYAndZ, end: XAndY | XYAndZ, result?: Vector3d): Vector3d {
const zStart = XYZ.accessZ(start, 0.0) as number;
const zEnd = XYZ.accessZ(end, 0.0) as number;
const dz = zEnd - zStart;
if (result) {
result.set(end.x - start.x, end.y - start.y, dz);
return result;
}
return new Vector3d(end.x - start.x, end.y - start.y, dz);
}
/**
* Return a vector (optionally in preallocated result, otherwise newly created) from [x0,y0,z0] to [x1,y1,z1]
* @param x0 start point x coordinate.
* @param y0 start point y coordinate.
* @param z0 start point z coordinate.
* @param x1 end point x coordinate.
* @param y1 end point y coordinate.
* @param z1 end point z coordinate.
* @param result optional result vector.
*/
public static createStartEndXYZXYZ(
x0: number, y0: number, z0: number, x1: number, y1: number, z1: number, result?: Vector3d,
): Vector3d {
return this.create(x1 - x0, y1 - y0, z1 - z0, result);
}
/**
* Return a vector which is the input `vector` rotated by `angle` around the `axis` vector.
* @param vector initial vector.
* @param axis axis of rotation.
* @param angle angle of rotation. If undefined, 90 degrees is implied.
* @param result optional result vector
* @returns undefined if axis has no length.
*/
public static createRotateVectorAroundVector(vector: Vector3d, axis: Vector3d, angle?: Angle): Vector3d | undefined {
// Rodriguez formula, https://en.wikipedia.org/wiki/Rodrigues'_rotation_formula
const unitAxis = axis.normalize();
if (unitAxis) {
const xProduct = unitAxis.crossProduct(vector);
let c, s;
if (angle) {
c = angle.cos();
s = angle.sin();
} else {
c = 0.0;
s = 1.0;
}
return Vector3d.createAdd3Scaled(vector, c, xProduct, s, unitAxis, unitAxis.dotProduct(vector) * (1.0 - c));
}
return undefined;
}
/**
* Set (replace) xyz components so they are a vector from point0 to point1
* @param point0 start point of computed vector.
* @param point1 end point of computed vector.
*/
public setStartEnd(point0: XYAndZ, point1: XYAndZ) {
this.x = point1.x - point0.x;
this.y = point1.y - point0.y;
this.z = point1.z - point0.z;
}
/** Return a vector with 000 xyz parts. */
public static createZero(result?: Vector3d): Vector3d {
return Vector3d.create(0, 0, 0, result);
}
/** Return a unit X vector optionally multiplied by a scale */
public static unitX(scale: number = 1): Vector3d {
return new Vector3d(scale, 0, 0);
}
/** Return a unit Y vector optionally multiplied by a scale */
public static unitY(scale: number = 1): Vector3d {
return new Vector3d(0, scale, 0);
}
/** Return a unit Z vector optionally multiplied by a scale */
public static unitZ(scale: number = 1): Vector3d {
return new Vector3d(0, 0, scale);
}
/**
* Scale the instance by 1.0/`denominator`.
* @param denominator number by which to divide the coordinates of this instance
* @param result optional pre-allocated object to return
* @return scaled vector, or undefined if `denominator` is exactly zero (in which case instance is untouched).
*/
public safeDivideOrNull(denominator: number, result?: Vector3d): Vector3d | undefined {
if (denominator !== 0.0) {
return this.scale(1.0 / denominator, result);
}
return undefined;
}
/**
* Return a normalized instance and instance length.
* @param result optional pre-allocated object to return as `v` property
* @returns object containing the properties:
* * `v`: unit vector in the direction of the instance, or undefined if `mag` is near zero
* * `mag`: length of the instance prior to normalization
*/
public normalizeWithLength(result?: Vector3d): {
v: Vector3d | undefined;
mag: number;
} {
const originalMagnitude = this.magnitude();
const correctedMagnitude = Geometry.correctSmallFraction(originalMagnitude);
result = result ? result : new Vector3d();
return { v: this.safeDivideOrNull(correctedMagnitude, result), mag: originalMagnitude };
}
/**
* Return a unit vector parallel with this. Return undefined if this.magnitude is near zero.
* @param result optional result.
*/
public normalize(result?: Vector3d): Vector3d | undefined {
return this.normalizeWithLength(result).v;
}
/**
* If this vector has nonzero length, divide by the length to change to a unit vector.
* @returns true if normalization was successful
*/
public normalizeInPlace(): boolean {
return this.normalizeWithLength(this).v !== undefined;
}
/**
* Create a normalized vector from the inputs.
* @param result optional result
* @returns undefined if and only if normalization fails
*/
public static createNormalized(x: number = 0, y: number = 0, z: number = 0, result?: Vector3d): Vector3d | undefined {
if (undefined === result)
result = Vector3d.create(x, y, z);
else
result.set(x, y, z);
if (result.normalizeInPlace())
return result;
return undefined;
}
/**
* Create a normalized vector from startPoint to endPoint
* @param startPoint start point of vector
* @param endPoint end point of vector
* @param result optional result
* @returns undefined if and only if normalization fails.
*/
public static createNormalizedStartEnd(startPoint: XYAndZ, endPoint: XYAndZ, result?: Vector3d): Vector3d | undefined {
result = Vector3d.createStartEnd(startPoint, endPoint, result);
if (result.normalizeInPlace())
return result;
return undefined;
}
/**
* Return fractional length of the projection of the instance onto the target vector.
* * To find the projection vector, scale the target vector by the return value.
* * Math details can be found at docs/learning/geometry/PointVector.md