@immugio/three-math-extensions 0.0.14 → 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/CHANGELOG.md CHANGED
@@ -7,7 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
9
9
 
10
- ## [0.0.14](https://github.com/Immugio/three-math-extensions/compare/0.0.13...0.0.14)
10
+ ## [0.0.15](https://github.com/Immugio/three-math-extensions/compare/0.0.14...0.0.15)
11
+
12
+ ### Commits
13
+
14
+ - Add intersect method to Line3D [`6fe47de`](https://github.com/Immugio/three-math-extensions/commit/6fe47de7caaa1807b47a4363e551510c463757d7)
15
+
16
+ ## [0.0.14](https://github.com/Immugio/three-math-extensions/compare/0.0.13...0.0.14) - 2022-12-26
11
17
 
12
18
  ### Commits
13
19
 
package/cjs/Line2D.js CHANGED
@@ -550,6 +550,9 @@ class Line2D {
550
550
  }
551
551
  return null;
552
552
  }
553
+ equals(other) {
554
+ return !!other && this.start.equals(other.start) && this.end.equals(other.end);
555
+ }
553
556
  /**
554
557
  * Deep clone of this line
555
558
  */
@@ -559,8 +562,5 @@ class Line2D {
559
562
  toString() {
560
563
  return `Line(${this.start.x}, ${this.start.y}, ${this.end.x}, ${this.end.y})`;
561
564
  }
562
- equals(other) {
563
- return !!other && this.start.equals(other.start) && this.end.equals(other.end);
564
- }
565
565
  }
566
566
  exports.Line2D = Line2D;
package/cjs/Line3D.js CHANGED
@@ -367,6 +367,45 @@ class Line3D extends three_1.Line3 {
367
367
  this.end.add(p);
368
368
  return this;
369
369
  }
370
+ /**
371
+ * Calculates the intersection between this and `other` line. The lines are assumed to be infinite.
372
+ * In a lot of cases an actual intersection cannot be calculated due to rounding errors.
373
+ * Therefore, the intersection calculated by this method comes in a form of the shorted possible line segment connecting the two lines.
374
+ * Sources:
375
+ * http://paulbourke.net/geometry/pointlineplane/
376
+ * https://stackoverflow.com/questions/2316490/the-algorithm-to-find-the-point-of-intersection-of-two-3d-line-segment/2316934#2316934
377
+ * @param other
378
+ */
379
+ intersect(other) {
380
+ const p1 = this.start.clone();
381
+ const p2 = this.end.clone();
382
+ const p3 = other.start.clone();
383
+ const p4 = other.end.clone();
384
+ const p13 = p1.clone().sub(p3);
385
+ const p43 = p4.clone().sub(p3);
386
+ if (p43.lengthSq() <= Number.EPSILON) {
387
+ return null;
388
+ }
389
+ const p21 = p2.clone().sub(p1);
390
+ if (p21.lengthSq() <= Number.EPSILON) {
391
+ return null;
392
+ }
393
+ const d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;
394
+ const d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
395
+ const d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
396
+ const d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
397
+ const d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;
398
+ const denominator = d2121 * d4343 - d4321 * d4321;
399
+ if (Math.abs(denominator) <= Number.EPSILON) {
400
+ return null;
401
+ }
402
+ const numerator = d1343 * d4321 - d1321 * d4343;
403
+ const mua = numerator / denominator;
404
+ const mub = (d1343 + d4321 * (mua)) / d4343;
405
+ const resultSegmentPoint1 = new Vec3_1.Vec3((p1.x + mua * p21.x), (p1.y + mua * p21.y), (p1.z + mua * p21.z));
406
+ const resultSegmentPoint2 = new Vec3_1.Vec3((p3.x + mub * p43.x), (p3.y + mub * p43.y), (p3.z + mub * p43.z));
407
+ return new Line3D(resultSegmentPoint1, resultSegmentPoint2);
408
+ }
370
409
  /**
371
410
  * Project the line to 2D space, Y value is dropped
372
411
  */
package/esm/Line2D.js CHANGED
@@ -547,6 +547,9 @@ export class Line2D {
547
547
  }
548
548
  return null;
549
549
  }
550
+ equals(other) {
551
+ return !!other && this.start.equals(other.start) && this.end.equals(other.end);
552
+ }
550
553
  /**
551
554
  * Deep clone of this line
552
555
  */
@@ -556,7 +559,4 @@ export class Line2D {
556
559
  toString() {
557
560
  return `Line(${this.start.x}, ${this.start.y}, ${this.end.x}, ${this.end.y})`;
558
561
  }
559
- equals(other) {
560
- return !!other && this.start.equals(other.start) && this.end.equals(other.end);
561
- }
562
562
  }
package/esm/Line3D.js CHANGED
@@ -364,6 +364,45 @@ export class Line3D extends Line3 {
364
364
  this.end.add(p);
365
365
  return this;
366
366
  }
367
+ /**
368
+ * Calculates the intersection between this and `other` line. The lines are assumed to be infinite.
369
+ * In a lot of cases an actual intersection cannot be calculated due to rounding errors.
370
+ * Therefore, the intersection calculated by this method comes in a form of the shorted possible line segment connecting the two lines.
371
+ * Sources:
372
+ * http://paulbourke.net/geometry/pointlineplane/
373
+ * https://stackoverflow.com/questions/2316490/the-algorithm-to-find-the-point-of-intersection-of-two-3d-line-segment/2316934#2316934
374
+ * @param other
375
+ */
376
+ intersect(other) {
377
+ const p1 = this.start.clone();
378
+ const p2 = this.end.clone();
379
+ const p3 = other.start.clone();
380
+ const p4 = other.end.clone();
381
+ const p13 = p1.clone().sub(p3);
382
+ const p43 = p4.clone().sub(p3);
383
+ if (p43.lengthSq() <= Number.EPSILON) {
384
+ return null;
385
+ }
386
+ const p21 = p2.clone().sub(p1);
387
+ if (p21.lengthSq() <= Number.EPSILON) {
388
+ return null;
389
+ }
390
+ const d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;
391
+ const d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
392
+ const d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
393
+ const d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
394
+ const d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;
395
+ const denominator = d2121 * d4343 - d4321 * d4321;
396
+ if (Math.abs(denominator) <= Number.EPSILON) {
397
+ return null;
398
+ }
399
+ const numerator = d1343 * d4321 - d1321 * d4343;
400
+ const mua = numerator / denominator;
401
+ const mub = (d1343 + d4321 * (mua)) / d4343;
402
+ const resultSegmentPoint1 = new Vec3((p1.x + mua * p21.x), (p1.y + mua * p21.y), (p1.z + mua * p21.z));
403
+ const resultSegmentPoint2 = new Vec3((p3.x + mub * p43.x), (p3.y + mub * p43.y), (p3.z + mub * p43.z));
404
+ return new Line3D(resultSegmentPoint1, resultSegmentPoint2);
405
+ }
367
406
  /**
368
407
  * Project the line to 2D space, Y value is dropped
369
408
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@immugio/three-math-extensions",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "Set of utilities for 2d and 3d line math built on top of three.js",
5
5
  "author": "Jan Mikeska <janmikeska@gmail.com>",
6
6
  "license": "ISC",
package/src/Line2D.ts CHANGED
@@ -644,6 +644,10 @@ export class Line2D {
644
644
  return null;
645
645
  }
646
646
 
647
+ public equals(other: Line2D): boolean {
648
+ return !!other && this.start.equals(other.start) && this.end.equals(other.end);
649
+ }
650
+
647
651
  /**
648
652
  * Deep clone of this line
649
653
  */
@@ -654,8 +658,4 @@ export class Line2D {
654
658
  public toString(): string {
655
659
  return `Line(${this.start.x}, ${this.start.y}, ${this.end.x}, ${this.end.y})`;
656
660
  }
657
-
658
- public equals(other: Line2D): boolean {
659
- return !!other && this.start.equals(other.start) && this.end.equals(other.end);
660
- }
661
661
  }
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/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
  */