@immugio/three-math-extensions 0.2.21 → 0.2.22

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.
@@ -1,4 +1,4 @@
1
- [@immugio/three-math-extensions](../README.md) / [Exports](../modules.md) / Vec3
1
+ [@immugio/three-math-extensions](../../README.md) / [Exports](../modules.md) / Vec3
2
2
 
3
3
  # Class: Vec3
4
4
 
@@ -20,6 +20,7 @@ Vec3 represents a 3D vector. It extends `Vector3` from the `threejs` library.
20
20
 
21
21
  - [addX](Vec3.md#addx)
22
22
  - [addY](Vec3.md#addy)
23
+ - [addZ](Vec3.md#addz)
23
24
  - [clone](Vec3.md#clone)
24
25
  - [closest](Vec3.md#closest)
25
26
  - [horizontalDistanceTo](Vec3.md#horizontaldistanceto)
@@ -29,12 +30,13 @@ Vec3 represents a 3D vector. It extends `Vector3` from the `threejs` library.
29
30
  - [onPlan](Vec3.md#onplan)
30
31
  - [toPointWithFlippedYZ](Vec3.md#topointwithflippedyz)
31
32
  - [fromPoint](Vec3.md#frompoint)
33
+ - [fromPoints](Vec3.md#frompoints)
32
34
 
33
35
  ## Constructors
34
36
 
35
37
  ### constructor
36
38
 
37
- • **new Vec3**(`x?`, `y?`, `z?`)
39
+ • **new Vec3**(`x?`, `y?`, `z?`): [`Vec3`](Vec3.md)
38
40
 
39
41
  #### Parameters
40
42
 
@@ -44,13 +46,17 @@ Vec3 represents a 3D vector. It extends `Vector3` from the `threejs` library.
44
46
  | `y?` | `number` |
45
47
  | `z?` | `number` |
46
48
 
49
+ #### Returns
50
+
51
+ [`Vec3`](Vec3.md)
52
+
47
53
  #### Inherited from
48
54
 
49
55
  Vector3.constructor
50
56
 
51
57
  #### Defined in
52
58
 
53
- node_modules/@types/three/src/math/Vector3.d.ts:26
59
+ node_modules/@types/three/src/math/Vector3.d.ts:27
54
60
 
55
61
  ## Methods
56
62
 
@@ -72,7 +78,7 @@ Adds x amount to this Vec3 instance and return this
72
78
 
73
79
  #### Defined in
74
80
 
75
- [src/Vec3.ts:65](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L65)
81
+ [src/Vec3.ts:73](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L73)
76
82
 
77
83
  ___
78
84
 
@@ -94,7 +100,29 @@ Adds y amount to this Vec3 instance and return this
94
100
 
95
101
  #### Defined in
96
102
 
97
- [src/Vec3.ts:56](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L56)
103
+ [src/Vec3.ts:64](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L64)
104
+
105
+ ___
106
+
107
+ ### addZ
108
+
109
+ ▸ **addZ**(`z`): [`Vec3`](Vec3.md)
110
+
111
+ Adds z amount to this Vec3 instance and return this
112
+
113
+ #### Parameters
114
+
115
+ | Name | Type |
116
+ | :------ | :------ |
117
+ | `z` | `number` |
118
+
119
+ #### Returns
120
+
121
+ [`Vec3`](Vec3.md)
122
+
123
+ #### Defined in
124
+
125
+ [src/Vec3.ts:82](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L82)
98
126
 
99
127
  ___
100
128
 
@@ -112,7 +140,7 @@ Vector3.clone
112
140
 
113
141
  #### Defined in
114
142
 
115
- [src/Vec3.ts:114](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L114)
143
+ [src/Vec3.ts:131](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L131)
116
144
 
117
145
  ___
118
146
 
@@ -134,7 +162,7 @@ Returns a clone of the point closest to this from the given points.
134
162
 
135
163
  #### Defined in
136
164
 
137
- [src/Vec3.ts:74](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L74)
165
+ [src/Vec3.ts:91](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L91)
138
166
 
139
167
  ___
140
168
 
@@ -156,7 +184,7 @@ Get distance to another vector while ignoring the y-axis.
156
184
 
157
185
  #### Defined in
158
186
 
159
- [src/Vec3.ts:98](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L98)
187
+ [src/Vec3.ts:115](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L115)
160
188
 
161
189
  ___
162
190
 
@@ -180,7 +208,7 @@ maxDistance is the maximum distance between the two vectors within which they ar
180
208
 
181
209
  #### Defined in
182
210
 
183
- [src/Vec3.ts:106](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L106)
211
+ [src/Vec3.ts:123](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L123)
184
212
 
185
213
  ___
186
214
 
@@ -204,7 +232,7 @@ This Vec3 instance.
204
232
 
205
233
  #### Defined in
206
234
 
207
- [src/Vec3.ts:43](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L43)
235
+ [src/Vec3.ts:51](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L51)
208
236
 
209
237
  ___
210
238
 
@@ -229,7 +257,7 @@ This Vec3 instance.
229
257
 
230
258
  #### Defined in
231
259
 
232
- [src/Vec3.ts:27](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L27)
260
+ [src/Vec3.ts:35](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L35)
233
261
 
234
262
  ___
235
263
 
@@ -245,7 +273,7 @@ Projects this Vec3 instance onto 2d plan. Vec3.z becomes Vec2.y and Vec3.y is ig
245
273
 
246
274
  #### Defined in
247
275
 
248
- [src/Vec3.ts:90](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L90)
276
+ [src/Vec3.ts:107](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L107)
249
277
 
250
278
  ___
251
279
 
@@ -261,13 +289,13 @@ Returns a clone of this Vec3 instance with y and z swapped.
261
289
 
262
290
  #### Defined in
263
291
 
264
- [src/Vec3.ts:83](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L83)
292
+ [src/Vec3.ts:100](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L100)
265
293
 
266
294
  ___
267
295
 
268
296
  ### fromPoint
269
297
 
270
- `Static` **fromPoint**(`point`): [`Vec3`](Vec3.md)
298
+ ▸ **fromPoint**(`point`): [`Vec3`](Vec3.md)
271
299
 
272
300
  Creates a new Vec3 instance from an {x, y, z} object.
273
301
 
@@ -285,4 +313,26 @@ A new Vec3 instance.
285
313
 
286
314
  #### Defined in
287
315
 
288
- [src/Vec3.ts:17](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/Vec3.ts#L17)
316
+ [src/Vec3.ts:17](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L17)
317
+
318
+ ___
319
+
320
+ ### fromPoints
321
+
322
+ ▸ **fromPoints**(`...points`): [`Vec3`](Vec3.md)[]
323
+
324
+ Creates a new Vec3[] array from arguments of {x, y, z} objects.
325
+
326
+ #### Parameters
327
+
328
+ | Name | Type | Description |
329
+ | :------ | :------ | :------ |
330
+ | `...points` | [`Point3`](../interfaces/Point3.md)[] | The ...{x, y, z} instances. |
331
+
332
+ #### Returns
333
+
334
+ [`Vec3`](Vec3.md)[]
335
+
336
+ #### Defined in
337
+
338
+ [src/Vec3.ts:25](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Vec3.ts#L25)
@@ -1,4 +1,4 @@
1
- [@immugio/three-math-extensions](../README.md) / [Exports](../modules.md) / Point2
1
+ [@immugio/three-math-extensions](../../README.md) / [Exports](../modules.md) / Point2
2
2
 
3
3
  # Interface: Point2
4
4
 
@@ -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/66cba15/src/Point2.ts#L2)
20
+ [src/Point2.ts:2](https://github.com/Immugio/three-math-extensions/blob/905d178/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/66cba15/src/Point2.ts#L3)
30
+ [src/Point2.ts:3](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Point2.ts#L3)
@@ -1,4 +1,4 @@
1
- [@immugio/three-math-extensions](../README.md) / [Exports](../modules.md) / Point3
1
+ [@immugio/three-math-extensions](../../README.md) / [Exports](../modules.md) / Point3
2
2
 
3
3
  # Interface: Point3
4
4
 
@@ -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/66cba15/src/Point3.ts#L2)
21
+ [src/Point3.ts:2](https://github.com/Immugio/three-math-extensions/blob/905d178/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/66cba15/src/Point3.ts#L3)
31
+ [src/Point3.ts:3](https://github.com/Immugio/three-math-extensions/blob/905d178/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/66cba15/src/Point3.ts#L4)
41
+ [src/Point3.ts:4](https://github.com/Immugio/three-math-extensions/blob/905d178/src/Point3.ts#L4)
package/docs/modules.md CHANGED
@@ -1,4 +1,4 @@
1
- [@immugio/three-math-extensions](README.md) / Exports
1
+ [@immugio/three-math-extensions](../README.md) / Exports
2
2
 
3
3
  # @immugio/three-math-extensions
4
4
 
@@ -22,26 +22,107 @@
22
22
 
23
23
  ### Variables
24
24
 
25
+ - [HalfPI](modules.md#halfpi)
25
26
  - [TwoPI](modules.md#twopi)
27
+ - [directions](modules.md#directions)
28
+ - [directions2d](modules.md#directions2d)
26
29
 
27
30
  ### Functions
28
31
 
32
+ - [isContinuousClosedShape](modules.md#iscontinuousclosedshape)
29
33
  - [isPointInPolygon](modules.md#ispointinpolygon)
30
34
  - [normalizeAngleDegrees](modules.md#normalizeangledegrees)
31
35
  - [normalizeAngleRadians](modules.md#normalizeangleradians)
32
36
 
33
37
  ## Variables
34
38
 
39
+ ### HalfPI
40
+
41
+ • `Const` **HalfPI**: `number`
42
+
43
+ #### Defined in
44
+
45
+ [src/MathConstants.ts:2](https://github.com/Immugio/three-math-extensions/blob/905d178/src/MathConstants.ts#L2)
46
+
47
+ ___
48
+
35
49
  ### TwoPI
36
50
 
37
51
  • `Const` **TwoPI**: `number`
38
52
 
39
53
  #### Defined in
40
54
 
41
- [src/MathConstants.ts:1](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/MathConstants.ts#L1)
55
+ [src/MathConstants.ts:1](https://github.com/Immugio/three-math-extensions/blob/905d178/src/MathConstants.ts#L1)
56
+
57
+ ___
58
+
59
+ ### directions
60
+
61
+ • `Const` **directions**: `Object`
62
+
63
+ #### Type declaration
64
+
65
+ | Name | Type |
66
+ | :------ | :------ |
67
+ | `Down` | [`Vec3`](classes/Vec3.md) |
68
+ | `East` | [`Vec3`](classes/Vec3.md) |
69
+ | `North` | [`Vec3`](classes/Vec3.md) |
70
+ | `South` | [`Vec3`](classes/Vec3.md) |
71
+ | `Up` | [`Vec3`](classes/Vec3.md) |
72
+ | `West` | [`Vec3`](classes/Vec3.md) |
73
+
74
+ #### Defined in
75
+
76
+ [src/directions.ts:3](https://github.com/Immugio/three-math-extensions/blob/905d178/src/directions.ts#L3)
77
+
78
+ ___
79
+
80
+ ### directions2d
81
+
82
+ • `Const` **directions2d**: `Object`
83
+
84
+ #### Type declaration
85
+
86
+ | Name | Type |
87
+ | :------ | :------ |
88
+ | `Down` | [`Vec2`](classes/Vec2.md) |
89
+ | `Left` | [`Vec2`](classes/Vec2.md) |
90
+ | `Right` | [`Vec2`](classes/Vec2.md) |
91
+ | `Up` | [`Vec2`](classes/Vec2.md) |
92
+
93
+ #### Defined in
94
+
95
+ [src/directions2d.ts:3](https://github.com/Immugio/three-math-extensions/blob/905d178/src/directions2d.ts#L3)
42
96
 
43
97
  ## Functions
44
98
 
99
+ ### isContinuousClosedShape
100
+
101
+ ▸ **isContinuousClosedShape**\<`T`\>(`lines`, `tolerance?`): `boolean`
102
+
103
+ #### Type parameters
104
+
105
+ | Name | Type |
106
+ | :------ | :------ |
107
+ | `T` | extends [`Line3D`](classes/Line3D.md) \| [`Line2D`](classes/Line2D.md) |
108
+
109
+ #### Parameters
110
+
111
+ | Name | Type | Default value |
112
+ | :------ | :------ | :------ |
113
+ | `lines` | `T`[] | `undefined` |
114
+ | `tolerance` | `number` | `0` |
115
+
116
+ #### Returns
117
+
118
+ `boolean`
119
+
120
+ #### Defined in
121
+
122
+ [src/isContinuousClosedShape.ts:4](https://github.com/Immugio/three-math-extensions/blob/905d178/src/isContinuousClosedShape.ts#L4)
123
+
124
+ ___
125
+
45
126
  ### isPointInPolygon
46
127
 
47
128
  ▸ **isPointInPolygon**(`p`, `point`): `boolean`
@@ -59,7 +140,7 @@
59
140
 
60
141
  #### Defined in
61
142
 
62
- [src/isPointInPolygon.ts:3](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/isPointInPolygon.ts#L3)
143
+ [src/isPointInPolygon.ts:3](https://github.com/Immugio/three-math-extensions/blob/905d178/src/isPointInPolygon.ts#L3)
63
144
 
64
145
  ___
65
146
 
@@ -81,7 +162,7 @@ Normalizes an angle in degrees to the range [0, 360].
81
162
 
82
163
  #### Defined in
83
164
 
84
- [src/normalizeAngleDegrees.ts:5](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/normalizeAngleDegrees.ts#L5)
165
+ [src/normalizeAngleDegrees.ts:5](https://github.com/Immugio/three-math-extensions/blob/905d178/src/normalizeAngleDegrees.ts#L5)
85
166
 
86
167
  ___
87
168
 
@@ -103,4 +184,4 @@ Normalize an angle in radians to the range of 0 to 2π.
103
184
 
104
185
  #### Defined in
105
186
 
106
- [src/normalizeAngleRadians.ts:7](https://github.com/Immugio/three-math-extensions/blob/66cba15/src/normalizeAngleRadians.ts#L7)
187
+ [src/normalizeAngleRadians.ts:7](https://github.com/Immugio/three-math-extensions/blob/905d178/src/normalizeAngleRadians.ts#L7)
package/esm/Line2D.js CHANGED
@@ -10,6 +10,7 @@ export class Line2D {
10
10
  start;
11
11
  end;
12
12
  index;
13
+ #target = new Vec2();
13
14
  constructor(start, end, index = 0) {
14
15
  this.start = start;
15
16
  this.end = end;
@@ -138,6 +139,24 @@ export class Line2D {
138
139
  get endpoints() {
139
140
  return [this.start, this.end];
140
141
  }
142
+ /**
143
+ * Check that this line segment contains provided point.
144
+ * @param p
145
+ * @param tolerance
146
+ */
147
+ containsPoint(p, tolerance = 0) {
148
+ const closestPointToPoint = this.closestPointToPoint(p, true, this.#target);
149
+ return closestPointToPoint.distanceTo(p) <= tolerance;
150
+ }
151
+ /**
152
+ * Distance from this line to the provided point.
153
+ * @param param
154
+ * @param clampToLine
155
+ */
156
+ distanceToPoint(p, clampToLine = true) {
157
+ const closestPointToPoint = this.closestPointToPoint(p, clampToLine, this.#target);
158
+ return closestPointToPoint.distanceTo(p);
159
+ }
141
160
  /**
142
161
  * Returns the direction of this line.
143
162
  */
@@ -245,14 +264,32 @@ export class Line2D {
245
264
  /**
246
265
  * Returns true if there is any overlap between this line and the @other line section.
247
266
  */
248
- overlaps(other) {
249
- if (!this.isCollinearWithTouchOrOverlap(other)) {
250
- return false;
251
- }
252
- if (this.start.equals(other.start) && this.end.equals(other.end)) {
267
+ overlaps(other, distanceTolerance = 0, parallelTolerance = 0) {
268
+ // Special case
269
+ if (this.equals(other, distanceTolerance)) {
253
270
  return true;
254
271
  }
255
- return !this.start.equals(other.end) && !this.end.equals(other.start);
272
+ // Always have to be parallel
273
+ if (this.isParallelTo(other, parallelTolerance)) {
274
+ // To pass as overlapping, at least one point has to be within the other line, but they mush not be equal - "touching" is not considered overlapping
275
+ const otherStartEqualsToAnyOfThisPoint = other.start.distanceTo(this.start) <= distanceTolerance || other.start.distanceTo(this.end) <= distanceTolerance;
276
+ if (this.containsPoint(other.start, distanceTolerance) && !otherStartEqualsToAnyOfThisPoint) {
277
+ return true;
278
+ }
279
+ const otherEndEqualsToAnyOfThisPoint = other.end.distanceTo(this.start) <= distanceTolerance || other.end.distanceTo(this.end) <= distanceTolerance;
280
+ if (this.containsPoint(other.end, distanceTolerance) && !otherEndEqualsToAnyOfThisPoint) {
281
+ return true;
282
+ }
283
+ const thisStartEqualsToAnyOfOtherPoint = this.start.distanceTo(other.start) <= distanceTolerance || this.start.distanceTo(other.end) <= distanceTolerance;
284
+ if (other.containsPoint(this.start, distanceTolerance) && !thisStartEqualsToAnyOfOtherPoint) {
285
+ return true;
286
+ }
287
+ const thisEndEqualsToAnyOfOtherPoint = this.end.distanceTo(other.start) <= distanceTolerance || this.end.distanceTo(other.end) <= distanceTolerance;
288
+ if (other.containsPoint(this.end, distanceTolerance) && !thisEndEqualsToAnyOfOtherPoint) {
289
+ return true;
290
+ }
291
+ }
292
+ return false;
256
293
  }
257
294
  /**
258
295
  * Logical AND of this and the other line section.
@@ -324,6 +361,17 @@ export class Line2D {
324
361
  }
325
362
  return result;
326
363
  }
364
+ /**
365
+ * Checks if the current line covers another line.
366
+ * A line is considered to cover another line if they are parallel and both the start and end points of the other line are contained within the current line.
367
+ * Both distance and angle tolerance can be provided.
368
+ */
369
+ covers(other, tolerance = 0, parallelTolerance = 0) {
370
+ if (!this.isParallelTo(other, parallelTolerance)) {
371
+ return false;
372
+ }
373
+ return this.containsPoint(other.start, tolerance) && this.containsPoint(other.end, tolerance);
374
+ }
327
375
  /**
328
376
  * Returns a new line that is the projection of this line onto @other. Uses `closestPointToPoint` to find the projection.
329
377
  * @param other
@@ -640,8 +688,11 @@ export class Line2D {
640
688
  in3DSpace(y = 0) {
641
689
  return new Line3D(this.start.in3DSpace(y), this.end.in3DSpace(y));
642
690
  }
643
- equals(other) {
644
- return !!other && this.start.equals(other.start) && this.end.equals(other.end);
691
+ equals(other, tolerance = 0) {
692
+ if (!tolerance) {
693
+ return !!other && this.start.equals(other.start) && this.end.equals(other.end);
694
+ }
695
+ return !!other && this.start.distanceTo(other.start) <= tolerance && this.end.distanceTo(other.end) <= tolerance;
645
696
  }
646
697
  /**
647
698
  * Deep clone of this line
package/esm/Line3D.js CHANGED
@@ -250,7 +250,7 @@ export class Line3D extends Line3 {
250
250
  return closestPointToPoint.distanceTo(p) <= tolerance;
251
251
  }
252
252
  /**
253
- * Distance from this line to provided point.
253
+ * Distance from this line to the provided point.
254
254
  * @param p
255
255
  * @param clampToLine
256
256
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@immugio/three-math-extensions",
3
- "version": "0.2.21",
3
+ "version": "0.2.22",
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
@@ -11,6 +11,8 @@ const _startEnd = /*@__PURE__*/ new Vec2();
11
11
 
12
12
  export class Line2D {
13
13
 
14
+ readonly #target: Vec2 = new Vec2();
15
+
14
16
  constructor(public start: Vec2, public end: Vec2, public index: number = 0) {
15
17
  }
16
18
 
@@ -161,6 +163,26 @@ export class Line2D {
161
163
  return [this.start, this.end];
162
164
  }
163
165
 
166
+ /**
167
+ * Check that this line segment contains provided point.
168
+ * @param p
169
+ * @param tolerance
170
+ */
171
+ public containsPoint(p: Vector2, tolerance: number = 0): boolean {
172
+ const closestPointToPoint = this.closestPointToPoint(p, true, this.#target);
173
+ return closestPointToPoint.distanceTo(p) <= tolerance;
174
+ }
175
+
176
+ /**
177
+ * Distance from this line to the provided point.
178
+ * @param param
179
+ * @param clampToLine
180
+ */
181
+ public distanceToPoint(p: Vector2, clampToLine: boolean = true): number {
182
+ const closestPointToPoint = this.closestPointToPoint(p, clampToLine, this.#target);
183
+ return closestPointToPoint.distanceTo(p);
184
+ }
185
+
164
186
  /**
165
187
  * Returns the direction of this line.
166
188
  */
@@ -284,16 +306,38 @@ export class Line2D {
284
306
  /**
285
307
  * Returns true if there is any overlap between this line and the @other line section.
286
308
  */
287
- public overlaps(other: Line2D): boolean {
288
- if (!this.isCollinearWithTouchOrOverlap(other)) {
289
- return false;
309
+ public overlaps(other: Line2D, distanceTolerance: number = 0, parallelTolerance: number = 0): boolean {
310
+ // Special case
311
+ if (this.equals(other, distanceTolerance)) {
312
+ return true;
290
313
  }
291
314
 
292
- if (this.start.equals(other.start) && this.end.equals(other.end)) {
293
- return true;
315
+ // Always have to be parallel
316
+ if (this.isParallelTo(other, parallelTolerance)) {
317
+ // To pass as overlapping, at least one point has to be within the other line, but they mush not be equal - "touching" is not considered overlapping
318
+
319
+ const otherStartEqualsToAnyOfThisPoint = other.start.distanceTo(this.start) <= distanceTolerance || other.start.distanceTo(this.end) <= distanceTolerance;
320
+ if (this.containsPoint(other.start, distanceTolerance) && !otherStartEqualsToAnyOfThisPoint) {
321
+ return true;
322
+ }
323
+
324
+ const otherEndEqualsToAnyOfThisPoint = other.end.distanceTo(this.start) <= distanceTolerance || other.end.distanceTo(this.end) <= distanceTolerance;
325
+ if (this.containsPoint(other.end, distanceTolerance) && !otherEndEqualsToAnyOfThisPoint) {
326
+ return true;
327
+ }
328
+
329
+ const thisStartEqualsToAnyOfOtherPoint = this.start.distanceTo(other.start) <= distanceTolerance || this.start.distanceTo(other.end) <= distanceTolerance;
330
+ if (other.containsPoint(this.start, distanceTolerance) && !thisStartEqualsToAnyOfOtherPoint) {
331
+ return true;
332
+ }
333
+
334
+ const thisEndEqualsToAnyOfOtherPoint = this.end.distanceTo(other.start) <= distanceTolerance || this.end.distanceTo(other.end) <= distanceTolerance;
335
+ if (other.containsPoint(this.end, distanceTolerance) && !thisEndEqualsToAnyOfOtherPoint) {
336
+ return true;
337
+ }
294
338
  }
295
339
 
296
- return !this.start.equals(other.end) && !this.end.equals(other.start);
340
+ return false;
297
341
  }
298
342
 
299
343
  /**
@@ -382,6 +426,19 @@ export class Line2D {
382
426
  return result;
383
427
  }
384
428
 
429
+ /**
430
+ * Checks if the current line covers another line.
431
+ * A line is considered to cover another line if they are parallel and both the start and end points of the other line are contained within the current line.
432
+ * Both distance and angle tolerance can be provided.
433
+ */
434
+ public covers(other: Line2D, tolerance: number = 0, parallelTolerance: number = 0): boolean {
435
+ if (!this.isParallelTo(other, parallelTolerance)) {
436
+ return false;
437
+ }
438
+
439
+ return this.containsPoint(other.start, tolerance) && this.containsPoint(other.end, tolerance);
440
+ }
441
+
385
442
  /**
386
443
  * Returns a new line that is the projection of this line onto @other. Uses `closestPointToPoint` to find the projection.
387
444
  * @param other
@@ -759,8 +816,12 @@ export class Line2D {
759
816
  return new Line3D(this.start.in3DSpace(y), this.end.in3DSpace(y));
760
817
  }
761
818
 
762
- public equals(other: Line2D): boolean {
763
- return !!other && this.start.equals(other.start) && this.end.equals(other.end);
819
+ public equals(other: Line2D, tolerance: number = 0): boolean {
820
+ if (!tolerance) {
821
+ return !!other && this.start.equals(other.start) && this.end.equals(other.end);
822
+ }
823
+
824
+ return !!other && this.start.distanceTo(other.start) <= tolerance && this.end.distanceTo(other.end) <= tolerance;
764
825
  }
765
826
 
766
827
  /**
package/src/Line3D.ts CHANGED
@@ -305,7 +305,7 @@ export class Line3D extends Line3 {
305
305
  }
306
306
 
307
307
  /**
308
- * Distance from this line to provided point.
308
+ * Distance from this line to the provided point.
309
309
  * @param p
310
310
  * @param clampToLine
311
311
  */
package/types/Line2D.d.ts CHANGED
@@ -3,6 +3,7 @@ import { Vector2 } from "three";
3
3
  import { Vec2 } from "./Vec2";
4
4
  import { Line3D } from "./Line3D";
5
5
  export declare class Line2D {
6
+ #private;
6
7
  start: Vec2;
7
8
  end: Vec2;
8
9
  index: number;
@@ -55,6 +56,18 @@ export declare class Line2D {
55
56
  * Endpoints are not cloned.
56
57
  */
57
58
  get endpoints(): Vec2[];
59
+ /**
60
+ * Check that this line segment contains provided point.
61
+ * @param p
62
+ * @param tolerance
63
+ */
64
+ containsPoint(p: Vector2, tolerance?: number): boolean;
65
+ /**
66
+ * Distance from this line to the provided point.
67
+ * @param param
68
+ * @param clampToLine
69
+ */
70
+ distanceToPoint(p: Vector2, clampToLine?: boolean): number;
58
71
  /**
59
72
  * Returns the direction of this line.
60
73
  */
@@ -117,7 +130,7 @@ export declare class Line2D {
117
130
  /**
118
131
  * Returns true if there is any overlap between this line and the @other line section.
119
132
  */
120
- overlaps(other: Line2D): boolean;
133
+ overlaps(other: Line2D, distanceTolerance?: number, parallelTolerance?: number): boolean;
121
134
  /**
122
135
  * Logical AND of this and the other line section.
123
136
  * @param other
@@ -139,6 +152,12 @@ export declare class Line2D {
139
152
  * @param lines
140
153
  */
141
154
  static joinLines(lines: Line2D[]): Line2D[];
155
+ /**
156
+ * Checks if the current line covers another line.
157
+ * A line is considered to cover another line if they are parallel and both the start and end points of the other line are contained within the current line.
158
+ * Both distance and angle tolerance can be provided.
159
+ */
160
+ covers(other: Line2D, tolerance?: number, parallelTolerance?: number): boolean;
142
161
  /**
143
162
  * Returns a new line that is the projection of this line onto @other. Uses `closestPointToPoint` to find the projection.
144
163
  * @param other
@@ -241,7 +260,7 @@ export declare class Line2D {
241
260
  * @returns A new Line3D instance.
242
261
  */
243
262
  in3DSpace(y?: number): Line3D;
244
- equals(other: Line2D): boolean;
263
+ equals(other: Line2D, tolerance?: number): boolean;
245
264
  /**
246
265
  * Deep clone of this line
247
266
  */