@immugio/three-math-extensions 0.2.9 → 0.2.11

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.
@@ -72,7 +72,7 @@ Adds x amount to this Vec3 instance and return this
72
72
 
73
73
  #### Defined in
74
74
 
75
- [src/Vec3.ts:65](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L65)
75
+ [src/Vec3.ts:65](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L65)
76
76
 
77
77
  ___
78
78
 
@@ -94,7 +94,7 @@ Adds y amount to this Vec3 instance and return this
94
94
 
95
95
  #### Defined in
96
96
 
97
- [src/Vec3.ts:56](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L56)
97
+ [src/Vec3.ts:56](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L56)
98
98
 
99
99
  ___
100
100
 
@@ -112,7 +112,7 @@ Vector3.clone
112
112
 
113
113
  #### Defined in
114
114
 
115
- [src/Vec3.ts:114](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L114)
115
+ [src/Vec3.ts:114](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L114)
116
116
 
117
117
  ___
118
118
 
@@ -134,7 +134,7 @@ Returns a clone of the point closest to this from the given points.
134
134
 
135
135
  #### Defined in
136
136
 
137
- [src/Vec3.ts:74](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L74)
137
+ [src/Vec3.ts:74](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L74)
138
138
 
139
139
  ___
140
140
 
@@ -156,7 +156,7 @@ Get distance to another vector while ignoring the y-axis.
156
156
 
157
157
  #### Defined in
158
158
 
159
- [src/Vec3.ts:98](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L98)
159
+ [src/Vec3.ts:98](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L98)
160
160
 
161
161
  ___
162
162
 
@@ -180,7 +180,7 @@ maxDistance is the maximum distance between the two vectors within which they ar
180
180
 
181
181
  #### Defined in
182
182
 
183
- [src/Vec3.ts:106](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L106)
183
+ [src/Vec3.ts:106](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L106)
184
184
 
185
185
  ___
186
186
 
@@ -204,7 +204,7 @@ This Vec3 instance.
204
204
 
205
205
  #### Defined in
206
206
 
207
- [src/Vec3.ts:43](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L43)
207
+ [src/Vec3.ts:43](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L43)
208
208
 
209
209
  ___
210
210
 
@@ -229,7 +229,7 @@ This Vec3 instance.
229
229
 
230
230
  #### Defined in
231
231
 
232
- [src/Vec3.ts:27](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L27)
232
+ [src/Vec3.ts:27](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L27)
233
233
 
234
234
  ___
235
235
 
@@ -245,7 +245,7 @@ Projects this Vec3 instance onto 2d plan. Vec3.z becomes Vec2.y and Vec3.y is ig
245
245
 
246
246
  #### Defined in
247
247
 
248
- [src/Vec3.ts:90](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L90)
248
+ [src/Vec3.ts:90](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L90)
249
249
 
250
250
  ___
251
251
 
@@ -261,7 +261,7 @@ Returns a clone of this Vec3 instance with y and z swapped.
261
261
 
262
262
  #### Defined in
263
263
 
264
- [src/Vec3.ts:83](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L83)
264
+ [src/Vec3.ts:83](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L83)
265
265
 
266
266
  ___
267
267
 
@@ -285,4 +285,4 @@ A new Vec3 instance.
285
285
 
286
286
  #### Defined in
287
287
 
288
- [src/Vec3.ts:17](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L17)
288
+ [src/Vec3.ts:17](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L17)
@@ -17,7 +17,7 @@
17
17
 
18
18
  #### Defined in
19
19
 
20
- [src/Point2.ts:2](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Point2.ts#L2)
20
+ [src/Point2.ts:2](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Point2.ts#L2)
21
21
 
22
22
  ___
23
23
 
@@ -27,4 +27,4 @@ ___
27
27
 
28
28
  #### Defined in
29
29
 
30
- [src/Point2.ts:3](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Point2.ts#L3)
30
+ [src/Point2.ts:3](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Point2.ts#L3)
@@ -18,7 +18,7 @@
18
18
 
19
19
  #### Defined in
20
20
 
21
- [src/Point3.ts:2](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Point3.ts#L2)
21
+ [src/Point3.ts:2](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Point3.ts#L2)
22
22
 
23
23
  ___
24
24
 
@@ -28,7 +28,7 @@ ___
28
28
 
29
29
  #### Defined in
30
30
 
31
- [src/Point3.ts:3](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Point3.ts#L3)
31
+ [src/Point3.ts:3](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Point3.ts#L3)
32
32
 
33
33
  ___
34
34
 
@@ -38,4 +38,4 @@ ___
38
38
 
39
39
  #### Defined in
40
40
 
41
- [src/Point3.ts:4](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Point3.ts#L4)
41
+ [src/Point3.ts:4](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Point3.ts#L4)
package/docs/modules.md CHANGED
@@ -26,6 +26,7 @@
26
26
 
27
27
  ### Functions
28
28
 
29
+ - [isPointInPolygon](modules.md#ispointinpolygon)
29
30
  - [normalizeAngleDegrees](modules.md#normalizeangledegrees)
30
31
  - [normalizeAngleRadians](modules.md#normalizeangleradians)
31
32
 
@@ -37,10 +38,31 @@
37
38
 
38
39
  #### Defined in
39
40
 
40
- [src/MathConstants.ts:1](https://github.com/Immugio/three-math-extensions/blob/151f214/src/MathConstants.ts#L1)
41
+ [src/MathConstants.ts:1](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/MathConstants.ts#L1)
41
42
 
42
43
  ## Functions
43
44
 
45
+ ### isPointInPolygon
46
+
47
+ ▸ **isPointInPolygon**(`p`, `point`): `boolean`
48
+
49
+ #### Parameters
50
+
51
+ | Name | Type |
52
+ | :------ | :------ |
53
+ | `p` | [`Point2`](interfaces/Point2.md)[] |
54
+ | `point` | [`Point2`](interfaces/Point2.md) |
55
+
56
+ #### Returns
57
+
58
+ `boolean`
59
+
60
+ #### Defined in
61
+
62
+ [src/isPointInPolygon.ts:3](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/isPointInPolygon.ts#L3)
63
+
64
+ ___
65
+
44
66
  ### normalizeAngleDegrees
45
67
 
46
68
  ▸ **normalizeAngleDegrees**(`angle`): `number`
@@ -59,7 +81,7 @@ Normalizes an angle in degrees to the range [0, 360].
59
81
 
60
82
  #### Defined in
61
83
 
62
- [src/normalizeAngleDegrees.ts:5](https://github.com/Immugio/three-math-extensions/blob/151f214/src/normalizeAngleDegrees.ts#L5)
84
+ [src/normalizeAngleDegrees.ts:5](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/normalizeAngleDegrees.ts#L5)
63
85
 
64
86
  ___
65
87
 
@@ -81,4 +103,4 @@ Normalize an angle in radians to the range of 0 to 2π.
81
103
 
82
104
  #### Defined in
83
105
 
84
- [src/normalizeAngleRadians.ts:7](https://github.com/Immugio/three-math-extensions/blob/151f214/src/normalizeAngleRadians.ts#L7)
106
+ [src/normalizeAngleRadians.ts:7](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/normalizeAngleRadians.ts#L7)
package/esm/Line2D.js CHANGED
@@ -3,6 +3,7 @@ import { Vec2 } from "./Vec2";
3
3
  import { MathUtils } from "three";
4
4
  import { TwoPI } from "./MathConstants";
5
5
  import { Line3D } from "./Line3D";
6
+ import { directions2d } from "./directions2d";
6
7
  const _startP = /*@__PURE__*/ new Vec2();
7
8
  const _startEnd = /*@__PURE__*/ new Vec2();
8
9
  export class Line2D {
@@ -313,6 +314,16 @@ export class Line2D {
313
314
  }
314
315
  return result;
315
316
  }
317
+ /**
318
+ * Returns a new line that is the projection of this line onto @other. Uses `closestPointToPoint` to find the projection.
319
+ * @param other
320
+ * @param clampToLine
321
+ */
322
+ projectOn(other, clampToLine) {
323
+ const p1 = other.closestPointToPoint(this.start, clampToLine, new Vec2());
324
+ const p2 = other.closestPointToPoint(this.end, clampToLine, new Vec2());
325
+ return p1.distanceTo(this.start) < p2.distanceTo(this.start) ? new Line2D(p1, p2) : new Line2D(p2, p1);
326
+ }
316
327
  /**
317
328
  * Divides the Line3D into a number of segments of the given length.
318
329
  * Clone the line, does not modify.
@@ -559,6 +570,13 @@ export class Line2D {
559
570
  }
560
571
  return null;
561
572
  }
573
+ get isCloserToHorizontal() {
574
+ const direction = this.direction;
575
+ return direction.angleTo(directions2d.Right) < Math.PI / 4 || direction.angleTo(directions2d.Left) < Math.PI / 4;
576
+ }
577
+ get isCloserToVertical() {
578
+ return !this.isCloserToHorizontal;
579
+ }
562
580
  /**
563
581
  * Project the line to 2D space. For start and end points Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
564
582
  * @param y - The y value of the new Vec3 instance.
package/esm/Vec2.js CHANGED
@@ -70,6 +70,17 @@ export class Vec2 extends Vector2 {
70
70
  const signed_angle = Math.atan2(this.y, this.x) - Math.atan2(0, 1);
71
71
  return normalizeAngleRadians(signed_angle);
72
72
  }
73
+ /**
74
+ * Returns the angle between this vector and the given vector, the return value is between 0 and PI
75
+ * Both vectors are expected to be normalized
76
+ * @param other Vector2 normalized vector
77
+ */
78
+ angleTo(other) {
79
+ // Calculate the dot product of the vectors
80
+ const dot = this.dot(other);
81
+ // Calculate the angle in radians between the two vectors
82
+ return Math.acos(Math.min(Math.max(dot, -1.0), 1.0));
83
+ }
73
84
  /**
74
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
75
86
  * @param other Vector2
@@ -0,0 +1,9 @@
1
+ import { Vec3 } from "./Vec3";
2
+ export const directions = {
3
+ North: new Vec3(0, 0, -1),
4
+ South: new Vec3(0, 0, 1),
5
+ East: new Vec3(1, 0, 0),
6
+ West: new Vec3(-1, 0, 0),
7
+ Up: new Vec3(0, 1, 0),
8
+ Down: new Vec3(0, -1, 0)
9
+ };
@@ -0,0 +1,7 @@
1
+ import { Vec2 } from "./Vec2";
2
+ export const directions2d = {
3
+ Up: new Vec2(0, -1),
4
+ Down: new Vec2(0, 1),
5
+ Left: new Vec2(-1, 0),
6
+ Right: new Vec2(1, 0)
7
+ };
package/esm/index.js CHANGED
@@ -9,3 +9,6 @@ export { Rectangle } from "./Rectangle";
9
9
  export { normalizeAngleDegrees } from "./normalizeAngleDegrees";
10
10
  export { normalizeAngleRadians } from "./normalizeAngleRadians";
11
11
  export { TwoPI } from "./MathConstants";
12
+ export { isPointInPolygon } from "./isPointInPolygon";
13
+ export { directions } from "./directions";
14
+ export { directions2d } from "./directions2d";
@@ -0,0 +1,12 @@
1
+ export function isPointInPolygon(p, point) {
2
+ const x = point.x, y = point.y;
3
+ let i, j, c = false;
4
+ for (i = 0, j = p.length - 1; i < p.length; j = i++) {
5
+ if ((((p[i].y <= y) && (y < p[j].y)) ||
6
+ ((p[j].y <= y) && (y < p[i].y))) &&
7
+ (x < (p[j].x - p[i].x) * (y - p[i].y) / (p[j].y - p[i].y) + p[i].x)) {
8
+ c = !c;
9
+ }
10
+ }
11
+ return c;
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@immugio/three-math-extensions",
3
- "version": "0.2.9",
3
+ "version": "0.2.11",
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
@@ -4,6 +4,7 @@ import { Vec2 } from "./Vec2";
4
4
  import { MathUtils } from "three";
5
5
  import { TwoPI } from "./MathConstants";
6
6
  import { Line3D } from "./Line3D";
7
+ import {directions2d} from "./directions2d";
7
8
 
8
9
  const _startP = /*@__PURE__*/ new Vec2();
9
10
  const _startEnd = /*@__PURE__*/ new Vec2();
@@ -368,6 +369,18 @@ export class Line2D {
368
369
  return result;
369
370
  }
370
371
 
372
+ /**
373
+ * Returns a new line that is the projection of this line onto @other. Uses `closestPointToPoint` to find the projection.
374
+ * @param other
375
+ * @param clampToLine
376
+ */
377
+ public projectOn(other: Line2D, clampToLine: boolean): Line2D {
378
+ const p1 = other.closestPointToPoint(this.start, clampToLine, new Vec2());
379
+ const p2 = other.closestPointToPoint(this.end, clampToLine, new Vec2());
380
+
381
+ return p1.distanceTo(this.start) < p2.distanceTo(this.start) ? new Line2D(p1, p2) : new Line2D(p2, p1);
382
+ }
383
+
371
384
  /**
372
385
  * Divides the Line3D into a number of segments of the given length.
373
386
  * Clone the line, does not modify.
@@ -660,6 +673,15 @@ export class Line2D {
660
673
  return null;
661
674
  }
662
675
 
676
+ public get isCloserToHorizontal(): boolean {
677
+ const direction = this.direction;
678
+ return direction.angleTo(directions2d.Right) < Math.PI / 4 || direction.angleTo(directions2d.Left) < Math.PI / 4;
679
+ }
680
+
681
+ public get isCloserToVertical(): boolean {
682
+ return !this.isCloserToHorizontal;
683
+ }
684
+
663
685
  /**
664
686
  * Project the line to 2D space. For start and end points Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
665
687
  * @param y - The y value of the new Vec3 instance.
package/src/Vec2.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Vector2 } from "three";
2
- import { Vec3 } from "./Vec3";
3
- import { Point2 } from "./Point2";
4
- import { normalizeAngleRadians } from "./normalizeAngleRadians";
1
+ import {Vector2} from "three";
2
+ import {Vec3} from "./Vec3";
3
+ import {Point2} from "./Point2";
4
+ import {normalizeAngleRadians} from "./normalizeAngleRadians";
5
5
 
6
6
  /**
7
7
  * Vec2 represents a 2D vector. It extends `Vector2` from the `threejs` library.
@@ -81,6 +81,19 @@ export class Vec2 extends Vector2 {
81
81
  return normalizeAngleRadians(signed_angle);
82
82
  }
83
83
 
84
+ /**
85
+ * Returns the angle between this vector and the given vector, the return value is between 0 and PI
86
+ * Both vectors are expected to be normalized
87
+ * @param other Vector2 normalized vector
88
+ */
89
+ public angleTo(other: Vector2): number {
90
+ // Calculate the dot product of the vectors
91
+ const dot = this.dot(other);
92
+
93
+ // Calculate the angle in radians between the two vectors
94
+ return Math.acos(Math.min(Math.max(dot, -1.0), 1.0));
95
+ }
96
+
84
97
  /**
85
98
  * 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
99
  * @param other Vector2
@@ -0,0 +1,10 @@
1
+ import { Vec3 } from "./Vec3";
2
+
3
+ export const directions = {
4
+ North: new Vec3(0, 0, -1),
5
+ South: new Vec3(0, 0, 1),
6
+ East: new Vec3(1, 0, 0),
7
+ West: new Vec3(-1, 0, 0),
8
+ Up: new Vec3(0, 1, 0),
9
+ Down: new Vec3(0, -1, 0)
10
+ };
@@ -0,0 +1,8 @@
1
+ import { Vec2 } from "./Vec2";
2
+
3
+ export const directions2d = {
4
+ Up: new Vec2(0, -1),
5
+ Down: new Vec2(0, 1),
6
+ Left: new Vec2(-1, 0),
7
+ Right: new Vec2(1, 0)
8
+ };
package/src/index.ts CHANGED
@@ -10,4 +10,7 @@ export { normalizeAngleDegrees } from "./normalizeAngleDegrees";
10
10
  export { normalizeAngleRadians } from "./normalizeAngleRadians";
11
11
  export { TwoPI } from "./MathConstants";
12
12
  export { Point2 } from "./Point2";
13
- export { Point3 } from "./Point3";
13
+ export { Point3 } from "./Point3";
14
+ export { isPointInPolygon } from "./isPointInPolygon";
15
+ export { directions } from "./directions";
16
+ export { directions2d } from "./directions2d";
@@ -0,0 +1,19 @@
1
+ import { Point2 } from "./Point2";
2
+
3
+ export function isPointInPolygon(p: Point2[], point: Point2): boolean {
4
+
5
+ const x = point.x, y = point.y;
6
+
7
+ let i: number, j: number, c = false;
8
+
9
+ for (i = 0, j = p.length - 1; i < p.length; j = i++) {
10
+
11
+ if ((((p[i].y <= y) && (y < p[j].y)) ||
12
+ ((p[j].y <= y) && (y < p[i].y))) &&
13
+ (x < (p[j].x - p[i].x) * (y - p[i].y) / (p[j].y - p[i].y) + p[i].x)) {
14
+ c = !c;
15
+ }
16
+ }
17
+
18
+ return c;
19
+ }
package/types/Line2D.d.ts CHANGED
@@ -138,6 +138,12 @@ export declare class Line2D {
138
138
  * @param lines
139
139
  */
140
140
  static joinLines(lines: Line2D[]): Line2D[];
141
+ /**
142
+ * Returns a new line that is the projection of this line onto @other. Uses `closestPointToPoint` to find the projection.
143
+ * @param other
144
+ * @param clampToLine
145
+ */
146
+ projectOn(other: Line2D, clampToLine: boolean): Line2D;
141
147
  /**
142
148
  * Divides the Line3D into a number of segments of the given length.
143
149
  * Clone the line, does not modify.
@@ -212,6 +218,8 @@ export declare class Line2D {
212
218
  * @param distanceTolerance number
213
219
  */
214
220
  hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number, angleTolerance?: number, distanceTolerance?: number): Vec2;
221
+ get isCloserToHorizontal(): boolean;
222
+ get isCloserToVertical(): boolean;
215
223
  /**
216
224
  * Project the line to 2D space. For start and end points Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
217
225
  * @param y - The y value of the new Vec3 instance.
package/types/Vec2.d.ts CHANGED
@@ -44,6 +44,12 @@ export declare class Vec2 extends Vector2 {
44
44
  * Returns the angle between this vector and positive x-axis, the return value is between 0 and 2PI
45
45
  */
46
46
  signedAngle(): number;
47
+ /**
48
+ * Returns the angle between this vector and the given vector, the return value is between 0 and PI
49
+ * Both vectors are expected to be normalized
50
+ * @param other Vector2 normalized vector
51
+ */
52
+ angleTo(other: Vector2): number;
47
53
  /**
48
54
  * 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
55
  * @param other Vector2
@@ -0,0 +1,9 @@
1
+ import { Vec3 } from "./Vec3";
2
+ export declare const directions: {
3
+ North: Vec3;
4
+ South: Vec3;
5
+ East: Vec3;
6
+ West: Vec3;
7
+ Up: Vec3;
8
+ Down: Vec3;
9
+ };
@@ -0,0 +1,7 @@
1
+ import { Vec2 } from "./Vec2";
2
+ export declare const directions2d: {
3
+ Up: Vec2;
4
+ Down: Vec2;
5
+ Left: Vec2;
6
+ Right: Vec2;
7
+ };
package/types/index.d.ts CHANGED
@@ -11,3 +11,6 @@ export { normalizeAngleRadians } from "./normalizeAngleRadians";
11
11
  export { TwoPI } from "./MathConstants";
12
12
  export { Point2 } from "./Point2";
13
13
  export { Point3 } from "./Point3";
14
+ export { isPointInPolygon } from "./isPointInPolygon";
15
+ export { directions } from "./directions";
16
+ export { directions2d } from "./directions2d";
@@ -0,0 +1,2 @@
1
+ import { Point2 } from "./Point2";
2
+ export declare function isPointInPolygon(p: Point2[], point: Point2): boolean;