@immugio/three-math-extensions 0.2.6 → 0.2.8

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,7 @@ 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.2.6](https://github.com/Immugio/three-math-extensions/compare/16.15.10...0.2.6)
10
+ ## [0.2.8](https://github.com/Immugio/three-math-extensions/compare/16.15.10...0.2.8)
11
11
 
12
12
  ### Commits
13
13
 
@@ -17,13 +17,36 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
17
17
  - Vec3, Line3D - update documentation [`67d9c32`](https://github.com/Immugio/three-math-extensions/commit/67d9c328e08cc0a5599932d2f0529e97f31c9213)
18
18
  - Add Polygon [`629cff8`](https://github.com/Immugio/three-math-extensions/commit/629cff8ecbb963477e8ea76d7f8b16d95435cbad)
19
19
  - Add Vec2.signedAngle [`863c8f2`](https://github.com/Immugio/three-math-extensions/commit/863c8f27f11288cbda535e21bb688206259269ed)
20
+ - Add Vec2.fromPoints to accept multiple points [`a261402`](https://github.com/Immugio/three-math-extensions/commit/a2614027cf5fb8263189b48f7e0bb9a23a552c15)
21
+ - Add Vec2.parallelTo [`989874d`](https://github.com/Immugio/three-math-extensions/commit/989874dcfe122d3ee84d8d56d79cb88e4e441736)
20
22
  - Polygon from bounding size [`eae6701`](https://github.com/Immugio/three-math-extensions/commit/eae67012f57f426f8b5259b765000447ce06d608)
21
23
  - Line2D and Line3D to return this instead of specific type [`761ef6a`](https://github.com/Immugio/three-math-extensions/commit/761ef6a9d8cc4e35120b666576794e521aa3b991)
24
+ - Line2D.in3DSpace added [`a6ce0ec`](https://github.com/Immugio/three-math-extensions/commit/a6ce0ecb67f5c7b2a75fcc283c28af626153a4af)
25
+ - Line2D.hasIntersectionWithAngle to support optional tolerance [`b313bbe`](https://github.com/Immugio/three-math-extensions/commit/b313bbe118d435d53750deefd9a9e29ba6ec5c71)
26
+ - Line2D.hasIntersectionWithAngle - modulo all angles [`5409aa0`](https://github.com/Immugio/three-math-extensions/commit/5409aa0bc41510efa86d548e91837e44e5b6c343)
27
+ - Update release instructions [`5b41a2e`](https://github.com/Immugio/three-math-extensions/commit/5b41a2ed7e15450dbb6088a7f7ed0031a013badc)
22
28
  - Add Polygon to exports [`ed66775`](https://github.com/Immugio/three-math-extensions/commit/ed66775c33e961835b23843222b822cfd9c16b1d)
23
29
  - Vec2.fromPoint and Vec3.fromPoint should accept null [`4b871af`](https://github.com/Immugio/three-math-extensions/commit/4b871af297bdcbe8584f1e2b99d602247b77687c)
24
30
  - Excluded files from build [`ec70614`](https://github.com/Immugio/three-math-extensions/commit/ec70614bc7df7a98f854c7a6693365118e04faf7)
25
31
 
26
- ## [16.15.10](https://github.com/Immugio/three-math-extensions/compare/0.2.5...16.15.10) - 2023-01-02
32
+ ## [16.15.10](https://github.com/Immugio/three-math-extensions/compare/0.2.7...16.15.10) - 2023-01-02
33
+
34
+ ## [0.2.7](https://github.com/Immugio/three-math-extensions/compare/0.2.6...0.2.7) - 2023-04-26
35
+
36
+ ### Commits
37
+
38
+ - Add Vec2.fromPoints to accept multiple points [`a261402`](https://github.com/Immugio/three-math-extensions/commit/a2614027cf5fb8263189b48f7e0bb9a23a552c15)
39
+ - Add Vec2.parallelTo [`989874d`](https://github.com/Immugio/three-math-extensions/commit/989874dcfe122d3ee84d8d56d79cb88e4e441736)
40
+ - Line2D.hasIntersectionWithAngle to support optional tolerance [`b313bbe`](https://github.com/Immugio/three-math-extensions/commit/b313bbe118d435d53750deefd9a9e29ba6ec5c71)
41
+ - Line2D.hasIntersectionWithAngle - modulo all angles [`5409aa0`](https://github.com/Immugio/three-math-extensions/commit/5409aa0bc41510efa86d548e91837e44e5b6c343)
42
+
43
+ ## [0.2.6](https://github.com/Immugio/three-math-extensions/compare/0.2.5...0.2.6) - 2023-04-14
44
+
45
+ ### Commits
46
+
47
+ - Improve Line2D.closestPointToPoint, add Vec2.signedAngle [`151f214`](https://github.com/Immugio/three-math-extensions/commit/151f21462e0358057ad8e9d75d5782563a1061f6)
48
+ - Improve documentation [`d0fcb51`](https://github.com/Immugio/three-math-extensions/commit/d0fcb5132f127b4382ac5f7291575a061b8ec121)
49
+ - Excluded files from build [`ec70614`](https://github.com/Immugio/three-math-extensions/commit/ec70614bc7df7a98f854c7a6693365118e04faf7)
27
50
 
28
51
  ## [0.2.5](https://github.com/Immugio/three-math-extensions/compare/0.2.4...0.2.5) - 2023-04-11
29
52
 
package/cjs/Line2D.js CHANGED
@@ -4,6 +4,8 @@ exports.Line2D = void 0;
4
4
  const three_1 = require("three");
5
5
  const Vec2_1 = require("./Vec2");
6
6
  const three_2 = require("three");
7
+ const MathConstants_1 = require("./MathConstants");
8
+ const Line3D_1 = require("./Line3D");
7
9
  const _startP = /*@__PURE__*/ new Vec2_1.Vec2();
8
10
  const _startEnd = /*@__PURE__*/ new Vec2_1.Vec2();
9
11
  class Line2D {
@@ -531,22 +533,34 @@ class Line2D {
531
533
  return new Vec2_1.Vec2(x, y);
532
534
  }
533
535
  /**
534
- * Check that the infinite lines intersect and that they are in the specified angle to each other
536
+ * Check that the line section intersect and that they are in the specified angle to each other
535
537
  * @param other Line
536
538
  * @param expectedAngleInRads number
539
+ * @param angleTolerance number
540
+ * @param distanceTolerance number
537
541
  */
538
- hasIntersectionWithAngle(other, expectedAngleInRads) {
539
- const angle = this.direction.angle();
540
- const otherAngle = other.direction.angle();
542
+ hasIntersectionWithAngle(other, expectedAngleInRads, angleTolerance = Number.EPSILON, distanceTolerance = Number.EPSILON) {
543
+ const angle = this.direction.angle() % MathConstants_1.TwoPI;
544
+ const otherAngle = other.direction.angle() % MathConstants_1.TwoPI;
541
545
  const actualAngle = Math.abs(angle - otherAngle);
542
- if (Math.abs(actualAngle - expectedAngleInRads) < Number.EPSILON) {
546
+ if (Math.abs(actualAngle - expectedAngleInRads) <= angleTolerance) {
543
547
  const intersection = this.intersect(other);
544
- if (intersection && this.isPointOnLineSection(intersection) && other.isPointOnLineSection(intersection)) {
548
+ if (intersection &&
549
+ this.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance &&
550
+ other.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance) {
545
551
  return intersection;
546
552
  }
547
553
  }
548
554
  return null;
549
555
  }
556
+ /**
557
+ * Project the line to 2D space. For start and end points Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
558
+ * @param y - The y value of the new Vec3 instance.
559
+ * @returns A new Line3D instance.
560
+ */
561
+ in3DSpace(y = 0) {
562
+ return new Line3D_1.Line3D(this.start.in3DSpace(y), this.end.in3DSpace(y));
563
+ }
550
564
  equals(other) {
551
565
  return !!other && this.start.equals(other.start) && this.end.equals(other.end);
552
566
  }
package/cjs/Vec2.js CHANGED
@@ -16,6 +16,13 @@ class Vec2 extends three_1.Vector2 {
16
16
  static fromPoint(point) {
17
17
  return new Vec2(point?.x, point?.y);
18
18
  }
19
+ /**
20
+ * Creates a new Vec2[] array from arguments of {x, y} objects.
21
+ * @param points - The ...{x, y} instances.
22
+ */
23
+ static fromPoints(...points) {
24
+ return points?.map(p => Vec2.fromPoint(p)) ?? [];
25
+ }
19
26
  /**
20
27
  * Moves this Vec2 instance towards the target Vec2 by the given amount.
21
28
  * @param target - The target Vec2.
@@ -66,5 +73,19 @@ class Vec2 extends three_1.Vector2 {
66
73
  const signed_angle = Math.atan2(this.y, this.x) - Math.atan2(0, 1);
67
74
  return (0, normalizeAngleRadians_1.normalizeAngleRadians)(signed_angle);
68
75
  }
76
+ /**
77
+ * check if the angle between the two vectors is close enough to 0 or 180 degrees (same or opposite direction) within the given tolerance
78
+ * @param other Vector2
79
+ * @param toleranceRadians number angle tolerance in radians
80
+ */
81
+ parallelTo(other, toleranceRadians = 0) {
82
+ const v1 = this.clone().normalize(); // Normalize both vectors to remove magnitude influence
83
+ const v2 = other.clone().normalize();
84
+ const dotProduct = v1.dot(v2); // Calculate the dot product to find the cosine of the angle between the vectors
85
+ // Calculate the angle in radians
86
+ const angle = Math.acos(dotProduct);
87
+ // Check if the angle is within the tolerance of 0 or 180 degrees (Math.PI)
88
+ return Math.abs(angle) <= toleranceRadians || Math.abs(angle - Math.PI) <= toleranceRadians;
89
+ }
69
90
  }
70
91
  exports.Vec2 = Vec2;
package/esm/Line2D.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { Vector2 } from "three";
2
2
  import { Vec2 } from "./Vec2";
3
3
  import { MathUtils } from "three";
4
+ import { TwoPI } from "./MathConstants";
5
+ import { Line3D } from "./Line3D";
4
6
  const _startP = /*@__PURE__*/ new Vec2();
5
7
  const _startEnd = /*@__PURE__*/ new Vec2();
6
8
  export class Line2D {
@@ -528,22 +530,34 @@ export class Line2D {
528
530
  return new Vec2(x, y);
529
531
  }
530
532
  /**
531
- * Check that the infinite lines intersect and that they are in the specified angle to each other
533
+ * Check that the line section intersect and that they are in the specified angle to each other
532
534
  * @param other Line
533
535
  * @param expectedAngleInRads number
536
+ * @param angleTolerance number
537
+ * @param distanceTolerance number
534
538
  */
535
- hasIntersectionWithAngle(other, expectedAngleInRads) {
536
- const angle = this.direction.angle();
537
- const otherAngle = other.direction.angle();
539
+ hasIntersectionWithAngle(other, expectedAngleInRads, angleTolerance = Number.EPSILON, distanceTolerance = Number.EPSILON) {
540
+ const angle = this.direction.angle() % TwoPI;
541
+ const otherAngle = other.direction.angle() % TwoPI;
538
542
  const actualAngle = Math.abs(angle - otherAngle);
539
- if (Math.abs(actualAngle - expectedAngleInRads) < Number.EPSILON) {
543
+ if (Math.abs(actualAngle - expectedAngleInRads) <= angleTolerance) {
540
544
  const intersection = this.intersect(other);
541
- if (intersection && this.isPointOnLineSection(intersection) && other.isPointOnLineSection(intersection)) {
545
+ if (intersection &&
546
+ this.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance &&
547
+ other.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance) {
542
548
  return intersection;
543
549
  }
544
550
  }
545
551
  return null;
546
552
  }
553
+ /**
554
+ * Project the line to 2D space. For start and end points Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
555
+ * @param y - The y value of the new Vec3 instance.
556
+ * @returns A new Line3D instance.
557
+ */
558
+ in3DSpace(y = 0) {
559
+ return new Line3D(this.start.in3DSpace(y), this.end.in3DSpace(y));
560
+ }
547
561
  equals(other) {
548
562
  return !!other && this.start.equals(other.start) && this.end.equals(other.end);
549
563
  }
package/esm/Vec2.js CHANGED
@@ -13,6 +13,13 @@ export class Vec2 extends Vector2 {
13
13
  static fromPoint(point) {
14
14
  return new Vec2(point?.x, point?.y);
15
15
  }
16
+ /**
17
+ * Creates a new Vec2[] array from arguments of {x, y} objects.
18
+ * @param points - The ...{x, y} instances.
19
+ */
20
+ static fromPoints(...points) {
21
+ return points?.map(p => Vec2.fromPoint(p)) ?? [];
22
+ }
16
23
  /**
17
24
  * Moves this Vec2 instance towards the target Vec2 by the given amount.
18
25
  * @param target - The target Vec2.
@@ -63,4 +70,18 @@ export class Vec2 extends Vector2 {
63
70
  const signed_angle = Math.atan2(this.y, this.x) - Math.atan2(0, 1);
64
71
  return normalizeAngleRadians(signed_angle);
65
72
  }
73
+ /**
74
+ * check if the angle between the two vectors is close enough to 0 or 180 degrees (same or opposite direction) within the given tolerance
75
+ * @param other Vector2
76
+ * @param toleranceRadians number angle tolerance in radians
77
+ */
78
+ parallelTo(other, toleranceRadians = 0) {
79
+ const v1 = this.clone().normalize(); // Normalize both vectors to remove magnitude influence
80
+ const v2 = other.clone().normalize();
81
+ const dotProduct = v1.dot(v2); // Calculate the dot product to find the cosine of the angle between the vectors
82
+ // Calculate the angle in radians
83
+ const angle = Math.acos(dotProduct);
84
+ // Check if the angle is within the tolerance of 0 or 180 degrees (Math.PI)
85
+ return Math.abs(angle) <= toleranceRadians || Math.abs(angle - Math.PI) <= toleranceRadians;
86
+ }
66
87
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@immugio/three-math-extensions",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
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
@@ -2,6 +2,8 @@
2
2
  import { Vector2 } from "three";
3
3
  import { Vec2 } from "./Vec2";
4
4
  import { MathUtils } from "three";
5
+ import { TwoPI } from "./MathConstants";
6
+ import { Line3D } from "./Line3D";
5
7
 
6
8
  const _startP = /*@__PURE__*/ new Vec2();
7
9
  const _startEnd = /*@__PURE__*/ new Vec2();
@@ -623,19 +625,24 @@ export class Line2D {
623
625
  }
624
626
 
625
627
  /**
626
- * Check that the infinite lines intersect and that they are in the specified angle to each other
628
+ * Check that the line section intersect and that they are in the specified angle to each other
627
629
  * @param other Line
628
630
  * @param expectedAngleInRads number
631
+ * @param angleTolerance number
632
+ * @param distanceTolerance number
629
633
  */
630
- public hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number): Vec2 {
631
- const angle = this.direction.angle();
632
- const otherAngle = other.direction.angle();
634
+ public hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number, angleTolerance = Number.EPSILON, distanceTolerance = Number.EPSILON): Vec2 {
635
+ const angle = this.direction.angle() % TwoPI;
636
+ const otherAngle = other.direction.angle() % TwoPI;
633
637
  const actualAngle = Math.abs(angle - otherAngle);
634
638
 
635
- if (Math.abs(actualAngle - expectedAngleInRads) < Number.EPSILON) {
639
+ if (Math.abs(actualAngle - expectedAngleInRads) <= angleTolerance) {
636
640
  const intersection = this.intersect(other);
637
- if (intersection && this.isPointOnLineSection(intersection) && other.isPointOnLineSection(intersection)) {
638
-
641
+ if (
642
+ intersection &&
643
+ this.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance &&
644
+ other.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance
645
+ ) {
639
646
  return intersection;
640
647
  }
641
648
  }
@@ -643,6 +650,15 @@ export class Line2D {
643
650
  return null;
644
651
  }
645
652
 
653
+ /**
654
+ * Project the line to 2D space. For start and end points Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
655
+ * @param y - The y value of the new Vec3 instance.
656
+ * @returns A new Line3D instance.
657
+ */
658
+ public in3DSpace(y: number = 0): Line3D {
659
+ return new Line3D(this.start.in3DSpace(y), this.end.in3DSpace(y));
660
+ }
661
+
646
662
  public equals(other: Line2D): boolean {
647
663
  return !!other && this.start.equals(other.start) && this.end.equals(other.end);
648
664
  }
package/src/Vec2.ts CHANGED
@@ -17,6 +17,14 @@ export class Vec2 extends Vector2 {
17
17
  return new Vec2(point?.x, point?.y);
18
18
  }
19
19
 
20
+ /**
21
+ * Creates a new Vec2[] array from arguments of {x, y} objects.
22
+ * @param points - The ...{x, y} instances.
23
+ */
24
+ public static fromPoints(...points: Point2[]): Vec2[] {
25
+ return points?.map(p => Vec2.fromPoint(p)) ?? [];
26
+ }
27
+
20
28
  /**
21
29
  * Moves this Vec2 instance towards the target Vec2 by the given amount.
22
30
  * @param target - The target Vec2.
@@ -72,4 +80,22 @@ export class Vec2 extends Vector2 {
72
80
  const signed_angle = Math.atan2(this.y, this.x) - Math.atan2(0, 1);
73
81
  return normalizeAngleRadians(signed_angle);
74
82
  }
83
+
84
+ /**
85
+ * check if the angle between the two vectors is close enough to 0 or 180 degrees (same or opposite direction) within the given tolerance
86
+ * @param other Vector2
87
+ * @param toleranceRadians number angle tolerance in radians
88
+ */
89
+ public parallelTo(other: Vector2, toleranceRadians: number = 0): boolean {
90
+ const v1 = this.clone().normalize(); // Normalize both vectors to remove magnitude influence
91
+ const v2 = other.clone().normalize();
92
+
93
+ const dotProduct = v1.dot(v2); // Calculate the dot product to find the cosine of the angle between the vectors
94
+
95
+ // Calculate the angle in radians
96
+ const angle = Math.acos(dotProduct);
97
+
98
+ // Check if the angle is within the tolerance of 0 or 180 degrees (Math.PI)
99
+ return Math.abs(angle) <= toleranceRadians || Math.abs(angle - Math.PI) <= toleranceRadians;
100
+ }
75
101
  }
package/types/Line2D.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Point2 } from "./Point2";
2
2
  import { Vector2 } from "three";
3
3
  import { Vec2 } from "./Vec2";
4
+ import { Line3D } from "./Line3D";
4
5
  export declare class Line2D {
5
6
  start: Vec2;
6
7
  end: Vec2;
@@ -202,11 +203,19 @@ export declare class Line2D {
202
203
  */
203
204
  intersect(other: Line2D): Vec2;
204
205
  /**
205
- * Check that the infinite lines intersect and that they are in the specified angle to each other
206
+ * Check that the line section intersect and that they are in the specified angle to each other
206
207
  * @param other Line
207
208
  * @param expectedAngleInRads number
209
+ * @param angleTolerance number
210
+ * @param distanceTolerance number
208
211
  */
209
- hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number): Vec2;
212
+ hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number, angleTolerance?: number, distanceTolerance?: number): Vec2;
213
+ /**
214
+ * Project the line to 2D space. For start and end points Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
215
+ * @param y - The y value of the new Vec3 instance.
216
+ * @returns A new Line3D instance.
217
+ */
218
+ in3DSpace(y?: number): Line3D;
210
219
  equals(other: Line2D): boolean;
211
220
  /**
212
221
  * Deep clone of this line
package/types/Vec2.d.ts CHANGED
@@ -11,6 +11,11 @@ export declare class Vec2 extends Vector2 {
11
11
  * @returns A new Vec2 instance.
12
12
  */
13
13
  static fromPoint(point: Point2): Vec2;
14
+ /**
15
+ * Creates a new Vec2[] array from arguments of {x, y} objects.
16
+ * @param points - The ...{x, y} instances.
17
+ */
18
+ static fromPoints(...points: Point2[]): Vec2[];
14
19
  /**
15
20
  * Moves this Vec2 instance towards the target Vec2 by the given amount.
16
21
  * @param target - The target Vec2.
@@ -39,4 +44,10 @@ export declare class Vec2 extends Vector2 {
39
44
  * Returns the angle between this vector and positive x-axis, the return value is between 0 and 2PI
40
45
  */
41
46
  signedAngle(): number;
47
+ /**
48
+ * check if the angle between the two vectors is close enough to 0 or 180 degrees (same or opposite direction) within the given tolerance
49
+ * @param other Vector2
50
+ * @param toleranceRadians number angle tolerance in radians
51
+ */
52
+ parallelTo(other: Vector2, toleranceRadians?: number): boolean;
42
53
  }