@immugio/three-math-extensions 0.0.13 → 0.0.15

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/src/Line3D.ts CHANGED
@@ -444,6 +444,64 @@ export class Line3D extends Line3 {
444
444
  return this;
445
445
  }
446
446
 
447
+ /**
448
+ * Calculates the intersection between this and `other` line. The lines are assumed to be infinite.
449
+ * In a lot of cases an actual intersection cannot be calculated due to rounding errors.
450
+ * Therefore, the intersection calculated by this method comes in a form of the shorted possible line segment connecting the two lines.
451
+ * Sources:
452
+ * http://paulbourke.net/geometry/pointlineplane/
453
+ * https://stackoverflow.com/questions/2316490/the-algorithm-to-find-the-point-of-intersection-of-two-3d-line-segment/2316934#2316934
454
+ * @param other
455
+ */
456
+ public intersect(other: Line3D): Line3D {
457
+ const p1: Vec3 = this.start.clone();
458
+ const p2: Vec3 = this.end.clone();
459
+
460
+ const p3: Vec3 = other.start.clone();
461
+ const p4: Vec3 = other.end.clone();
462
+
463
+ const p13: Vec3 = p1.clone().sub(p3);
464
+ const p43: Vec3 = p4.clone().sub(p3);
465
+
466
+ if (p43.lengthSq() <= Number.EPSILON) {
467
+ return null;
468
+ }
469
+
470
+ const p21 = p2.clone().sub(p1);
471
+ if (p21.lengthSq() <= Number.EPSILON) {
472
+ return null;
473
+ }
474
+
475
+ const d1343: number = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;
476
+ const d4321: number = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
477
+ const d1321: number = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
478
+ const d4343: number = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
479
+ const d2121: number = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;
480
+
481
+ const denominator: number = d2121 * d4343 - d4321 * d4321;
482
+ if (Math.abs(denominator) <= Number.EPSILON) {
483
+ return null;
484
+ }
485
+ const numerator: number = d1343 * d4321 - d1321 * d4343;
486
+
487
+ const mua: number = numerator / denominator;
488
+ const mub: number = (d1343 + d4321 * (mua)) / d4343;
489
+
490
+ const resultSegmentPoint1 = new Vec3(
491
+ (p1.x + mua * p21.x),
492
+ (p1.y + mua * p21.y),
493
+ (p1.z + mua * p21.z)
494
+ );
495
+
496
+ const resultSegmentPoint2 = new Vec3(
497
+ (p3.x + mub * p43.x),
498
+ (p3.y + mub * p43.y),
499
+ (p3.z + mub * p43.z)
500
+ );
501
+
502
+ return new Line3D(resultSegmentPoint1, resultSegmentPoint2);
503
+ }
504
+
447
505
  /**
448
506
  * Project the line to 2D space, Y value is dropped
449
507
  */
package/src/Vec2.ts CHANGED
@@ -3,7 +3,7 @@ import { Vec3 } from "./Vec3";
3
3
  import { Point2 } from "./Point2";
4
4
 
5
5
  /**
6
- * Vec2 represents a 2D vector. It extends `Vector2` from the `three` library.
6
+ * Vec2 represents a 2D vector. It extends `Vector2` from the `threejs` library.
7
7
  */
8
8
  export class Vec2 extends Vector2 {
9
9
 
@@ -44,12 +44,12 @@ export class Vec2 extends Vector2 {
44
44
  }
45
45
 
46
46
  /**
47
- * Projects this Vec2 instance to a Vec3 instance in 3D space.
48
- * @param z - The z value of the new Vec3 instance.
47
+ * Projects this Vec2 instance to a Vec3 instance in 3D space. Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
48
+ * @param y - The y value of the new Vec3 instance.
49
49
  * @returns A new Vec3 instance.
50
50
  */
51
- public in3DSpace(z: number = 0): Vec3 {
52
- return new Vec3(this.x, z, this.y);
51
+ public in3DSpace(y: number = 0): Vec3 {
52
+ return new Vec3(this.x, y, this.y);
53
53
  }
54
54
 
55
55
  /**
package/src/Vec3.ts CHANGED
@@ -2,14 +2,28 @@ import { Vector3 } from "three";
2
2
  import { Vec2 } from "./Vec2";
3
3
  import { Point3 } from "./Point3";
4
4
 
5
+ /**
6
+ * Vec3 represents a 2D vector. It extends `Vector3` from the `threejs` library.
7
+ */
5
8
  export class Vec3 extends Vector3 {
6
9
 
7
10
  #target: Vector3;
8
11
 
12
+ /**
13
+ * Creates a new Vec3 instance from an {x, y, z} object.
14
+ * @param point - The {x, y, z} instance.
15
+ * @returns A new Vec3 instance.
16
+ */
9
17
  public static fromPoint(point: Point3): Vec3 {
10
18
  return new Vec3(point.x, point.y, point.z);
11
19
  }
12
20
 
21
+ /**
22
+ * Moves this Vec3 instance towards the target Vec3 by the given amount.
23
+ * @param target - The target Vec3.
24
+ * @param amount - The distance to move.
25
+ * @returns This Vec3 instance.
26
+ */
13
27
  public moveTowards(target: Vector3, amount: number): Vec3 {
14
28
  if (this.#target === undefined) {
15
29
  this.#target = new Vector3();
@@ -21,7 +35,12 @@ export class Vec3 extends Vector3 {
21
35
  return this;
22
36
  }
23
37
 
24
- public centerTowards(target: Vector3): Vec3 {
38
+ /**
39
+ * Moves this Vec3 instance halfway towards the target Vec3 by the given amount.
40
+ * @param target - The target Vec3.
41
+ * @returns This Vec3 instance.
42
+ */
43
+ public moveHalfWayTowards(target: Vector3): Vec3 {
25
44
  if (this.#target === undefined) {
26
45
  this.#target = new Vector3();
27
46
  }
@@ -29,41 +48,61 @@ export class Vec3 extends Vector3 {
29
48
  return this.moveTowards(target, this.distanceTo(target) / 2);
30
49
  }
31
50
 
51
+
52
+ /**
53
+ * Adds y amount to this Vec3 instance and return this
54
+ * @param y
55
+ */
32
56
  public addY(y: number): Vec3 {
33
57
  this.y += y;
34
58
  return this;
35
59
  }
36
60
 
61
+ /**
62
+ * Adds x amount to this Vec3 instance and return this
63
+ * @param x
64
+ */
37
65
  public addX(x: number): Vec3 {
38
66
  this.x += x;
39
67
  return this;
40
68
  }
41
69
 
42
- public scale(p: Vector3): Vec3 {
43
- this.x *= p.x;
44
- this.y *= p.y;
45
- this.z *= p.z;
46
- return this;
47
- }
48
-
70
+ /**
71
+ * Returns a clone of the point closest to this from the given points.
72
+ * @param points
73
+ */
49
74
  public closest(...points: Vector3[]): Vec3 {
50
75
  const withDistances = points.map(p => ({ point: p, distance: this.distanceTo(p) }));
51
76
  const closest = withDistances.reduce((a, b) => a.distance < b.distance ? a : b);
52
77
  return Vec3.fromPoint(closest.point);
53
78
  }
54
79
 
80
+ /**
81
+ * Returns a clone of this Vec3 instance with y and z swapped.
82
+ */
55
83
  public toPointWithFlippedYZ(): Vec3 {
56
84
  return new Vec3(this.x, this.z, this.y);
57
85
  }
58
86
 
87
+ /**
88
+ * Projects this Vec3 instance onto 2d plan. Vec3.z becomes Vec2.y and Vec3.y is ignored.
89
+ */
59
90
  public onPlan(): Vec2 {
60
91
  return new Vec2(this.x, this.z);
61
92
  }
62
93
 
94
+ /**
95
+ * Get distance to another vector while ignoring the y-axis.
96
+ * @param point
97
+ */
63
98
  public horizontalDistanceTo(point: Vector3): number {
64
99
  return new Vector3(this.x, 0, this.z).distanceTo(new Vector3(point.x, 0, point.z));
65
100
  }
66
101
 
102
+ /**
103
+ * Determines if this Vec2 instance is near the target Vec2.
104
+ * maxDistance is the maximum distance between the two vectors within which they are considered `near`.
105
+ */
67
106
  public isNear(v: Vector3, maxDistance: number = undefined): boolean {
68
107
  if (maxDistance === undefined) {
69
108
  return this.equals(v);
package/types/Line2D.d.ts CHANGED
@@ -213,10 +213,10 @@ export declare class Line2D {
213
213
  * @param expectedAngleInRads number
214
214
  */
215
215
  hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number): Vec2;
216
+ equals(other: Line2D): boolean;
216
217
  /**
217
218
  * Deep clone of this line
218
219
  */
219
220
  clone(): Line2D;
220
221
  toString(): string;
221
- equals(other: Line2D): boolean;
222
222
  }
package/types/Line3D.d.ts CHANGED
@@ -135,6 +135,16 @@ export declare class Line3D extends Line3 {
135
135
  * @param p
136
136
  */
137
137
  translate(p: Vector3): this;
138
+ /**
139
+ * Calculates the intersection between this and `other` line. The lines are assumed to be infinite.
140
+ * In a lot of cases an actual intersection cannot be calculated due to rounding errors.
141
+ * Therefore, the intersection calculated by this method comes in a form of the shorted possible line segment connecting the two lines.
142
+ * Sources:
143
+ * http://paulbourke.net/geometry/pointlineplane/
144
+ * https://stackoverflow.com/questions/2316490/the-algorithm-to-find-the-point-of-intersection-of-two-3d-line-segment/2316934#2316934
145
+ * @param other
146
+ */
147
+ intersect(other: Line3D): Line3D;
138
148
  /**
139
149
  * Project the line to 2D space, Y value is dropped
140
150
  */
package/types/Vec2.d.ts CHANGED
@@ -2,7 +2,7 @@ import { Vector2 } from "three";
2
2
  import { Vec3 } from "./Vec3";
3
3
  import { Point2 } from "./Point2";
4
4
  /**
5
- * Vec2 represents a 2D vector. It extends `Vector2` from the `three` library.
5
+ * Vec2 represents a 2D vector. It extends `Vector2` from the `threejs` library.
6
6
  */
7
7
  export declare class Vec2 extends Vector2 {
8
8
  /**
@@ -25,11 +25,11 @@ export declare class Vec2 extends Vector2 {
25
25
  */
26
26
  roundIfCloseToInteger(max?: number): this;
27
27
  /**
28
- * Projects this Vec2 instance to a Vec3 instance in 3D space.
29
- * @param z - The z value of the new Vec3 instance.
28
+ * Projects this Vec2 instance to a Vec3 instance in 3D space. Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
29
+ * @param y - The y value of the new Vec3 instance.
30
30
  * @returns A new Vec3 instance.
31
31
  */
32
- in3DSpace(z?: number): Vec3;
32
+ in3DSpace(y?: number): Vec3;
33
33
  /**
34
34
  * Determines if this Vec2 instance is near the target Vec2.
35
35
  * maxDistance is the maximum distance between the two vectors within which they are considered `near`.
package/types/Vec3.d.ts CHANGED
@@ -1,18 +1,62 @@
1
1
  import { Vector3 } from "three";
2
2
  import { Vec2 } from "./Vec2";
3
3
  import { Point3 } from "./Point3";
4
+ /**
5
+ * Vec3 represents a 2D vector. It extends `Vector3` from the `threejs` library.
6
+ */
4
7
  export declare class Vec3 extends Vector3 {
5
8
  #private;
9
+ /**
10
+ * Creates a new Vec3 instance from an {x, y, z} object.
11
+ * @param point - The {x, y, z} instance.
12
+ * @returns A new Vec3 instance.
13
+ */
6
14
  static fromPoint(point: Point3): Vec3;
15
+ /**
16
+ * Moves this Vec3 instance towards the target Vec3 by the given amount.
17
+ * @param target - The target Vec3.
18
+ * @param amount - The distance to move.
19
+ * @returns This Vec3 instance.
20
+ */
7
21
  moveTowards(target: Vector3, amount: number): Vec3;
8
- centerTowards(target: Vector3): Vec3;
22
+ /**
23
+ * Moves this Vec3 instance halfway towards the target Vec3 by the given amount.
24
+ * @param target - The target Vec3.
25
+ * @returns This Vec3 instance.
26
+ */
27
+ moveHalfWayTowards(target: Vector3): Vec3;
28
+ /**
29
+ * Adds y amount to this Vec3 instance and return this
30
+ * @param y
31
+ */
9
32
  addY(y: number): Vec3;
33
+ /**
34
+ * Adds x amount to this Vec3 instance and return this
35
+ * @param x
36
+ */
10
37
  addX(x: number): Vec3;
11
- scale(p: Vector3): Vec3;
38
+ /**
39
+ * Returns a clone of the point closest to this from the given points.
40
+ * @param points
41
+ */
12
42
  closest(...points: Vector3[]): Vec3;
43
+ /**
44
+ * Returns a clone of this Vec3 instance with y and z swapped.
45
+ */
13
46
  toPointWithFlippedYZ(): Vec3;
47
+ /**
48
+ * Projects this Vec3 instance onto 2d plan. Vec3.z becomes Vec2.y and Vec3.y is ignored.
49
+ */
14
50
  onPlan(): Vec2;
51
+ /**
52
+ * Get distance to another vector while ignoring the y-axis.
53
+ * @param point
54
+ */
15
55
  horizontalDistanceTo(point: Vector3): number;
56
+ /**
57
+ * Determines if this Vec2 instance is near the target Vec2.
58
+ * maxDistance is the maximum distance between the two vectors within which they are considered `near`.
59
+ */
16
60
  isNear(v: Vector3, maxDistance?: number): boolean;
17
61
  clone(): this;
18
62
  }