@immugio/three-math-extensions 0.2.6 → 0.2.7

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.7](https://github.com/Immugio/three-math-extensions/compare/16.15.10...0.2.7)
11
11
 
12
12
  ### Commits
13
13
 
@@ -17,13 +17,25 @@ 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.hasIntersectionWithAngle to support optional tolerance [`b313bbe`](https://github.com/Immugio/three-math-extensions/commit/b313bbe118d435d53750deefd9a9e29ba6ec5c71)
25
+ - Line2D.hasIntersectionWithAngle - modulo all angles [`5409aa0`](https://github.com/Immugio/three-math-extensions/commit/5409aa0bc41510efa86d548e91837e44e5b6c343)
22
26
  - Add Polygon to exports [`ed66775`](https://github.com/Immugio/three-math-extensions/commit/ed66775c33e961835b23843222b822cfd9c16b1d)
23
27
  - Vec2.fromPoint and Vec3.fromPoint should accept null [`4b871af`](https://github.com/Immugio/three-math-extensions/commit/4b871af297bdcbe8584f1e2b99d602247b77687c)
24
28
  - Excluded files from build [`ec70614`](https://github.com/Immugio/three-math-extensions/commit/ec70614bc7df7a98f854c7a6693365118e04faf7)
25
29
 
26
- ## [16.15.10](https://github.com/Immugio/three-math-extensions/compare/0.2.5...16.15.10) - 2023-01-02
30
+ ## [16.15.10](https://github.com/Immugio/three-math-extensions/compare/0.2.6...16.15.10) - 2023-01-02
31
+
32
+ ## [0.2.6](https://github.com/Immugio/three-math-extensions/compare/0.2.5...0.2.6) - 2023-04-14
33
+
34
+ ### Commits
35
+
36
+ - Improve Line2D.closestPointToPoint, add Vec2.signedAngle [`151f214`](https://github.com/Immugio/three-math-extensions/commit/151f21462e0358057ad8e9d75d5782563a1061f6)
37
+ - Improve documentation [`d0fcb51`](https://github.com/Immugio/three-math-extensions/commit/d0fcb5132f127b4382ac5f7291575a061b8ec121)
38
+ - Excluded files from build [`ec70614`](https://github.com/Immugio/three-math-extensions/commit/ec70614bc7df7a98f854c7a6693365118e04faf7)
27
39
 
28
40
  ## [0.2.5](https://github.com/Immugio/three-math-extensions/compare/0.2.4...0.2.5) - 2023-04-11
29
41
 
package/cjs/Line2D.js CHANGED
@@ -4,6 +4,7 @@ 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");
7
8
  const _startP = /*@__PURE__*/ new Vec2_1.Vec2();
8
9
  const _startEnd = /*@__PURE__*/ new Vec2_1.Vec2();
9
10
  class Line2D {
@@ -531,17 +532,21 @@ class Line2D {
531
532
  return new Vec2_1.Vec2(x, y);
532
533
  }
533
534
  /**
534
- * Check that the infinite lines intersect and that they are in the specified angle to each other
535
+ * Check that the line section intersect and that they are in the specified angle to each other
535
536
  * @param other Line
536
537
  * @param expectedAngleInRads number
538
+ * @param angleTolerance number
539
+ * @param distanceTolerance number
537
540
  */
538
- hasIntersectionWithAngle(other, expectedAngleInRads) {
539
- const angle = this.direction.angle();
540
- const otherAngle = other.direction.angle();
541
+ hasIntersectionWithAngle(other, expectedAngleInRads, angleTolerance = Number.EPSILON, distanceTolerance = Number.EPSILON) {
542
+ const angle = this.direction.angle() % MathConstants_1.TwoPI;
543
+ const otherAngle = other.direction.angle() % MathConstants_1.TwoPI;
541
544
  const actualAngle = Math.abs(angle - otherAngle);
542
- if (Math.abs(actualAngle - expectedAngleInRads) < Number.EPSILON) {
545
+ if (Math.abs(actualAngle - expectedAngleInRads) <= angleTolerance) {
543
546
  const intersection = this.intersect(other);
544
- if (intersection && this.isPointOnLineSection(intersection) && other.isPointOnLineSection(intersection)) {
547
+ if (intersection &&
548
+ this.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance &&
549
+ other.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance) {
545
550
  return intersection;
546
551
  }
547
552
  }
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,7 @@
1
1
  import { Vector2 } from "three";
2
2
  import { Vec2 } from "./Vec2";
3
3
  import { MathUtils } from "three";
4
+ import { TwoPI } from "./MathConstants";
4
5
  const _startP = /*@__PURE__*/ new Vec2();
5
6
  const _startEnd = /*@__PURE__*/ new Vec2();
6
7
  export class Line2D {
@@ -528,17 +529,21 @@ export class Line2D {
528
529
  return new Vec2(x, y);
529
530
  }
530
531
  /**
531
- * Check that the infinite lines intersect and that they are in the specified angle to each other
532
+ * Check that the line section intersect and that they are in the specified angle to each other
532
533
  * @param other Line
533
534
  * @param expectedAngleInRads number
535
+ * @param angleTolerance number
536
+ * @param distanceTolerance number
534
537
  */
535
- hasIntersectionWithAngle(other, expectedAngleInRads) {
536
- const angle = this.direction.angle();
537
- const otherAngle = other.direction.angle();
538
+ hasIntersectionWithAngle(other, expectedAngleInRads, angleTolerance = Number.EPSILON, distanceTolerance = Number.EPSILON) {
539
+ const angle = this.direction.angle() % TwoPI;
540
+ const otherAngle = other.direction.angle() % TwoPI;
538
541
  const actualAngle = Math.abs(angle - otherAngle);
539
- if (Math.abs(actualAngle - expectedAngleInRads) < Number.EPSILON) {
542
+ if (Math.abs(actualAngle - expectedAngleInRads) <= angleTolerance) {
540
543
  const intersection = this.intersect(other);
541
- if (intersection && this.isPointOnLineSection(intersection) && other.isPointOnLineSection(intersection)) {
544
+ if (intersection &&
545
+ this.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance &&
546
+ other.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance) {
542
547
  return intersection;
543
548
  }
544
549
  }
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.7",
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,7 @@
2
2
  import { Vector2 } from "three";
3
3
  import { Vec2 } from "./Vec2";
4
4
  import { MathUtils } from "three";
5
+ import { TwoPI } from "./MathConstants";
5
6
 
6
7
  const _startP = /*@__PURE__*/ new Vec2();
7
8
  const _startEnd = /*@__PURE__*/ new Vec2();
@@ -623,19 +624,24 @@ export class Line2D {
623
624
  }
624
625
 
625
626
  /**
626
- * Check that the infinite lines intersect and that they are in the specified angle to each other
627
+ * Check that the line section intersect and that they are in the specified angle to each other
627
628
  * @param other Line
628
629
  * @param expectedAngleInRads number
630
+ * @param angleTolerance number
631
+ * @param distanceTolerance number
629
632
  */
630
- public hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number): Vec2 {
631
- const angle = this.direction.angle();
632
- const otherAngle = other.direction.angle();
633
+ public hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number, angleTolerance = Number.EPSILON, distanceTolerance = Number.EPSILON): Vec2 {
634
+ const angle = this.direction.angle() % TwoPI;
635
+ const otherAngle = other.direction.angle() % TwoPI;
633
636
  const actualAngle = Math.abs(angle - otherAngle);
634
637
 
635
- if (Math.abs(actualAngle - expectedAngleInRads) < Number.EPSILON) {
638
+ if (Math.abs(actualAngle - expectedAngleInRads) <= angleTolerance) {
636
639
  const intersection = this.intersect(other);
637
- if (intersection && this.isPointOnLineSection(intersection) && other.isPointOnLineSection(intersection)) {
638
-
640
+ if (
641
+ intersection &&
642
+ this.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance &&
643
+ other.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance
644
+ ) {
639
645
  return intersection;
640
646
  }
641
647
  }
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
@@ -202,11 +202,13 @@ export declare class Line2D {
202
202
  */
203
203
  intersect(other: Line2D): Vec2;
204
204
  /**
205
- * Check that the infinite lines intersect and that they are in the specified angle to each other
205
+ * Check that the line section intersect and that they are in the specified angle to each other
206
206
  * @param other Line
207
207
  * @param expectedAngleInRads number
208
+ * @param angleTolerance number
209
+ * @param distanceTolerance number
208
210
  */
209
- hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number): Vec2;
211
+ hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number, angleTolerance?: number, distanceTolerance?: number): Vec2;
210
212
  equals(other: Line2D): boolean;
211
213
  /**
212
214
  * 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
  }