@immugio/three-math-extensions 0.2.11 → 0.2.13
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 +28 -2
- package/cjs/Line3D.js +50 -5
- package/cjs/Vec2.js +0 -11
- package/esm/Line3D.js +50 -5
- package/esm/Vec2.js +0 -11
- package/package.json +8 -6
- package/src/Line2D.ts +2 -2
- package/src/Line3D.ts +56 -5
- package/src/Vec2.ts +0 -13
- package/types/Line3D.d.ts +15 -2
- package/types/Vec2.d.ts +0 -6
package/CHANGELOG.md
CHANGED
|
@@ -7,16 +7,20 @@ 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.
|
|
10
|
+
## [0.2.13](https://github.com/Immugio/three-math-extensions/compare/16.15.10...0.2.13)
|
|
11
11
|
|
|
12
12
|
### Commits
|
|
13
13
|
|
|
14
|
+
- Add eslint [`98e4912`](https://github.com/Immugio/three-math-extensions/commit/98e4912d637b42ba80e2f3267638b43296113019)
|
|
15
|
+
- Update jest packages [`af23b4f`](https://github.com/Immugio/three-math-extensions/commit/af23b4f08154bba3407f05b773865215e7e1cba8)
|
|
14
16
|
- Add Rectangle, update Polygon [`58ee875`](https://github.com/Immugio/three-math-extensions/commit/58ee87539af8f9ade186e5250cba9e01926da514)
|
|
15
17
|
- Add isPointInPolygon [`a59eb4b`](https://github.com/Immugio/three-math-extensions/commit/a59eb4be026f17a3106070ae626a0588cd4f4411)
|
|
16
18
|
- Improve Line2D.closestPointToPoint, add Vec2.signedAngle [`151f214`](https://github.com/Immugio/three-math-extensions/commit/151f21462e0358057ad8e9d75d5782563a1061f6)
|
|
17
19
|
- Improve documentation [`d0fcb51`](https://github.com/Immugio/three-math-extensions/commit/d0fcb5132f127b4382ac5f7291575a061b8ec121)
|
|
18
20
|
- Vec3, Line3D - update documentation [`67d9c32`](https://github.com/Immugio/three-math-extensions/commit/67d9c328e08cc0a5599932d2f0529e97f31c9213)
|
|
19
21
|
- Add Polygon [`629cff8`](https://github.com/Immugio/three-math-extensions/commit/629cff8ecbb963477e8ea76d7f8b16d95435cbad)
|
|
22
|
+
- Line3D.groupConnectedLines added [`29f372b`](https://github.com/Immugio/three-math-extensions/commit/29f372bc984c06b00c50fcd9428ce9a7b9cab799)
|
|
23
|
+
- Dependencies update and remove now duplicate Vec2.angleTo implementation [`4774abb`](https://github.com/Immugio/three-math-extensions/commit/4774abb81d882082df0da606c79a3c7bd3aecc57)
|
|
20
24
|
- Line2D.isCloserToHorizontal [`b6f1429`](https://github.com/Immugio/three-math-extensions/commit/b6f14292d1d2765f7314c45e4c74be91280ac764)
|
|
21
25
|
- Add Vec2.angleTo and tests [`dcf1e53`](https://github.com/Immugio/three-math-extensions/commit/dcf1e531aecf8c115f323e14e5e44059e5c5d15c)
|
|
22
26
|
- Add Vec2.signedAngle [`863c8f2`](https://github.com/Immugio/three-math-extensions/commit/863c8f27f11288cbda535e21bb688206259269ed)
|
|
@@ -24,9 +28,11 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
|
|
|
24
28
|
- Add Vec2.parallelTo [`989874d`](https://github.com/Immugio/three-math-extensions/commit/989874dcfe122d3ee84d8d56d79cb88e4e441736)
|
|
25
29
|
- Line2D.intersect - enable line segments intersection only [`1f1470e`](https://github.com/Immugio/three-math-extensions/commit/1f1470e1cf00118e643f5c44a135e0baf6b76d41)
|
|
26
30
|
- Add Line2D.projectOn [`4c52c5c`](https://github.com/Immugio/three-math-extensions/commit/4c52c5c2e649fbddb72ce3fca60cfd3f1319f72f)
|
|
31
|
+
- Add Line3D.index [`7ed13d2`](https://github.com/Immugio/three-math-extensions/commit/7ed13d213748056c9dafd86288c3bcec9a28d1ba)
|
|
27
32
|
- Polygon from bounding size [`eae6701`](https://github.com/Immugio/three-math-extensions/commit/eae67012f57f426f8b5259b765000447ce06d608)
|
|
28
33
|
- Line2D and Line3D to return this instead of specific type [`761ef6a`](https://github.com/Immugio/three-math-extensions/commit/761ef6a9d8cc4e35120b666576794e521aa3b991)
|
|
29
34
|
- Line2D.in3DSpace added [`a6ce0ec`](https://github.com/Immugio/three-math-extensions/commit/a6ce0ecb67f5c7b2a75fcc283c28af626153a4af)
|
|
35
|
+
- Line3D.connectsTo added [`6d2cfa0`](https://github.com/Immugio/three-math-extensions/commit/6d2cfa0f5335c665f325a694a32c57b574ec326d)
|
|
30
36
|
- Line2D.hasIntersectionWithAngle to support optional tolerance [`b313bbe`](https://github.com/Immugio/three-math-extensions/commit/b313bbe118d435d53750deefd9a9e29ba6ec5c71)
|
|
31
37
|
- Line2D.hasIntersectionWithAngle - modulo all angles [`5409aa0`](https://github.com/Immugio/three-math-extensions/commit/5409aa0bc41510efa86d548e91837e44e5b6c343)
|
|
32
38
|
- Use Vec2 instead of Vector2 [`7e6a6ea`](https://github.com/Immugio/three-math-extensions/commit/7e6a6ea272f4441ef4bc78b3fdec23fc783fa1db)
|
|
@@ -36,7 +42,27 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
|
|
|
36
42
|
- Documentation update [`d5c7a07`](https://github.com/Immugio/three-math-extensions/commit/d5c7a0765f6097f5d3a3be01967d4059f19682fb)
|
|
37
43
|
- Excluded files from build [`ec70614`](https://github.com/Immugio/three-math-extensions/commit/ec70614bc7df7a98f854c7a6693365118e04faf7)
|
|
38
44
|
|
|
39
|
-
## [16.15.10](https://github.com/Immugio/three-math-extensions/compare/0.2.
|
|
45
|
+
## [16.15.10](https://github.com/Immugio/three-math-extensions/compare/0.2.12...16.15.10) - 2023-01-02
|
|
46
|
+
|
|
47
|
+
## [0.2.12](https://github.com/Immugio/three-math-extensions/compare/0.2.11...0.2.12) - 2023-08-30
|
|
48
|
+
|
|
49
|
+
### Commits
|
|
50
|
+
|
|
51
|
+
- Add eslint [`98e4912`](https://github.com/Immugio/three-math-extensions/commit/98e4912d637b42ba80e2f3267638b43296113019)
|
|
52
|
+
- Update jest packages [`af23b4f`](https://github.com/Immugio/three-math-extensions/commit/af23b4f08154bba3407f05b773865215e7e1cba8)
|
|
53
|
+
- Line3D.groupConnectedLines added [`29f372b`](https://github.com/Immugio/three-math-extensions/commit/29f372bc984c06b00c50fcd9428ce9a7b9cab799)
|
|
54
|
+
- Dependencies update and remove now duplicate Vec2.angleTo implementation [`4774abb`](https://github.com/Immugio/three-math-extensions/commit/4774abb81d882082df0da606c79a3c7bd3aecc57)
|
|
55
|
+
- Add Line3D.index [`7ed13d2`](https://github.com/Immugio/three-math-extensions/commit/7ed13d213748056c9dafd86288c3bcec9a28d1ba)
|
|
56
|
+
|
|
57
|
+
## [0.2.11](https://github.com/Immugio/three-math-extensions/compare/0.2.10...0.2.11) - 2023-08-24
|
|
58
|
+
|
|
59
|
+
### Commits
|
|
60
|
+
|
|
61
|
+
- Line2D.isCloserToHorizontal [`b6f1429`](https://github.com/Immugio/three-math-extensions/commit/b6f14292d1d2765f7314c45e4c74be91280ac764)
|
|
62
|
+
- Add Vec2.angleTo and tests [`dcf1e53`](https://github.com/Immugio/three-math-extensions/commit/dcf1e531aecf8c115f323e14e5e44059e5c5d15c)
|
|
63
|
+
- Add Line2D.projectOn [`4c52c5c`](https://github.com/Immugio/three-math-extensions/commit/4c52c5c2e649fbddb72ce3fca60cfd3f1319f72f)
|
|
64
|
+
- Use Vec2 instead of Vector2 [`7e6a6ea`](https://github.com/Immugio/three-math-extensions/commit/7e6a6ea272f4441ef4bc78b3fdec23fc783fa1db)
|
|
65
|
+
- Documentation update [`d5c7a07`](https://github.com/Immugio/three-math-extensions/commit/d5c7a0765f6097f5d3a3be01967d4059f19682fb)
|
|
40
66
|
|
|
41
67
|
## [0.2.10](https://github.com/Immugio/three-math-extensions/compare/0.2.9...0.2.10) - 2023-05-30
|
|
42
68
|
|
package/cjs/Line3D.js
CHANGED
|
@@ -5,13 +5,15 @@ const three_1 = require("three");
|
|
|
5
5
|
const Vec3_1 = require("./Vec3");
|
|
6
6
|
const Line2D_1 = require("./Line2D");
|
|
7
7
|
class Line3D extends three_1.Line3 {
|
|
8
|
+
index;
|
|
8
9
|
#target;
|
|
9
|
-
constructor(start, end) {
|
|
10
|
+
constructor(start, end, index = 0) {
|
|
10
11
|
super(start, end);
|
|
12
|
+
this.index = index;
|
|
11
13
|
this.#target = new Vec3_1.Vec3();
|
|
12
14
|
}
|
|
13
|
-
static fromPoints(start, end) {
|
|
14
|
-
return new Line3D(new Vec3_1.Vec3(start.x, start.y, start.z), new Vec3_1.Vec3(end.x, end.y, end.z));
|
|
15
|
+
static fromPoints(start, end, index = 0) {
|
|
16
|
+
return new Line3D(new Vec3_1.Vec3(start.x, start.y, start.z), new Vec3_1.Vec3(end.x, end.y, end.z), index);
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
19
|
* Creates a polygon formed by an array of lines from points provided.
|
|
@@ -27,7 +29,7 @@ class Line3D extends three_1.Line3 {
|
|
|
27
29
|
}
|
|
28
30
|
const lines = [];
|
|
29
31
|
for (let i = 0; i < polygon.length - 1; i++) {
|
|
30
|
-
lines.push(Line3D.fromPoints(polygon[i], polygon[i + 1]));
|
|
32
|
+
lines.push(Line3D.fromPoints(polygon[i], polygon[i + 1], i));
|
|
31
33
|
}
|
|
32
34
|
return lines;
|
|
33
35
|
}
|
|
@@ -406,6 +408,49 @@ class Line3D extends three_1.Line3 {
|
|
|
406
408
|
const resultSegmentPoint2 = new Vec3_1.Vec3((p3.x + mub * p43.x), (p3.y + mub * p43.y), (p3.z + mub * p43.z));
|
|
407
409
|
return new Line3D(resultSegmentPoint1, resultSegmentPoint2);
|
|
408
410
|
}
|
|
411
|
+
/**
|
|
412
|
+
* Accepts an array of Line3D and groups them into arrays of connected lines
|
|
413
|
+
* @param lines Lines to be grouped
|
|
414
|
+
* @param tolerance Tolerance for considering lines as connected
|
|
415
|
+
*/
|
|
416
|
+
static groupConnectedLines(lines, tolerance = 0) {
|
|
417
|
+
const visited = new Set();
|
|
418
|
+
// Use graph-based approach. Each line can be considered as an edge in the graph, and the endpoints of the lines can be considered as vertices.
|
|
419
|
+
// Then use Depth-First Search (DFS) to find connected components in the graph.
|
|
420
|
+
const dfs = (line, group) => {
|
|
421
|
+
if (visited.has(line))
|
|
422
|
+
return;
|
|
423
|
+
visited.add(line);
|
|
424
|
+
group.push(line);
|
|
425
|
+
lines.forEach((neighbor) => {
|
|
426
|
+
if (!visited.has(neighbor)) {
|
|
427
|
+
if (line.connectsTo(neighbor, tolerance)) {
|
|
428
|
+
dfs(neighbor, group);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
};
|
|
433
|
+
const connectedLines = [];
|
|
434
|
+
lines.forEach((line) => {
|
|
435
|
+
if (!visited.has(line)) {
|
|
436
|
+
const group = [];
|
|
437
|
+
dfs(line, group);
|
|
438
|
+
connectedLines.push(group);
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
return connectedLines;
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Returns true if any endpoint of this line if within the tolerance distance of any endpoint of the @other line.
|
|
445
|
+
* @param other
|
|
446
|
+
* @param tolerance
|
|
447
|
+
*/
|
|
448
|
+
connectsTo(other, tolerance = 0) {
|
|
449
|
+
return this.start.isNear(other.start, tolerance) ||
|
|
450
|
+
this.start.isNear(other.end, tolerance) ||
|
|
451
|
+
this.end.isNear(other.start, tolerance) ||
|
|
452
|
+
this.end.isNear(other.end, tolerance);
|
|
453
|
+
}
|
|
409
454
|
/**
|
|
410
455
|
* Project the line to 2D space, Y value is dropped
|
|
411
456
|
*/
|
|
@@ -422,7 +467,7 @@ class Line3D extends three_1.Line3 {
|
|
|
422
467
|
* Deep clone of this line
|
|
423
468
|
*/
|
|
424
469
|
clone() {
|
|
425
|
-
return new Line3D(this.start.clone(), this.end.clone());
|
|
470
|
+
return new Line3D(this.start.clone(), this.end.clone(), this.index);
|
|
426
471
|
}
|
|
427
472
|
toString() {
|
|
428
473
|
return `Line3D { start: ${this.start.x}, ${this.start.y}, ${this.start.z}, end: ${this.end.x}, ${this.end.y}, ${this.end.z}}`;
|
package/cjs/Vec2.js
CHANGED
|
@@ -73,17 +73,6 @@ class Vec2 extends three_1.Vector2 {
|
|
|
73
73
|
const signed_angle = Math.atan2(this.y, this.x) - Math.atan2(0, 1);
|
|
74
74
|
return (0, normalizeAngleRadians_1.normalizeAngleRadians)(signed_angle);
|
|
75
75
|
}
|
|
76
|
-
/**
|
|
77
|
-
* Returns the angle between this vector and the given vector, the return value is between 0 and PI
|
|
78
|
-
* Both vectors are expected to be normalized
|
|
79
|
-
* @param other Vector2 normalized vector
|
|
80
|
-
*/
|
|
81
|
-
angleTo(other) {
|
|
82
|
-
// Calculate the dot product of the vectors
|
|
83
|
-
const dot = this.dot(other);
|
|
84
|
-
// Calculate the angle in radians between the two vectors
|
|
85
|
-
return Math.acos(Math.min(Math.max(dot, -1.0), 1.0));
|
|
86
|
-
}
|
|
87
76
|
/**
|
|
88
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
|
|
89
78
|
* @param other Vector2
|
package/esm/Line3D.js
CHANGED
|
@@ -2,13 +2,15 @@ import { Line3 } from "three";
|
|
|
2
2
|
import { Vec3 } from "./Vec3";
|
|
3
3
|
import { Line2D } from "./Line2D";
|
|
4
4
|
export class Line3D extends Line3 {
|
|
5
|
+
index;
|
|
5
6
|
#target;
|
|
6
|
-
constructor(start, end) {
|
|
7
|
+
constructor(start, end, index = 0) {
|
|
7
8
|
super(start, end);
|
|
9
|
+
this.index = index;
|
|
8
10
|
this.#target = new Vec3();
|
|
9
11
|
}
|
|
10
|
-
static fromPoints(start, end) {
|
|
11
|
-
return new Line3D(new Vec3(start.x, start.y, start.z), new Vec3(end.x, end.y, end.z));
|
|
12
|
+
static fromPoints(start, end, index = 0) {
|
|
13
|
+
return new Line3D(new Vec3(start.x, start.y, start.z), new Vec3(end.x, end.y, end.z), index);
|
|
12
14
|
}
|
|
13
15
|
/**
|
|
14
16
|
* Creates a polygon formed by an array of lines from points provided.
|
|
@@ -24,7 +26,7 @@ export class Line3D extends Line3 {
|
|
|
24
26
|
}
|
|
25
27
|
const lines = [];
|
|
26
28
|
for (let i = 0; i < polygon.length - 1; i++) {
|
|
27
|
-
lines.push(Line3D.fromPoints(polygon[i], polygon[i + 1]));
|
|
29
|
+
lines.push(Line3D.fromPoints(polygon[i], polygon[i + 1], i));
|
|
28
30
|
}
|
|
29
31
|
return lines;
|
|
30
32
|
}
|
|
@@ -403,6 +405,49 @@ export class Line3D extends Line3 {
|
|
|
403
405
|
const resultSegmentPoint2 = new Vec3((p3.x + mub * p43.x), (p3.y + mub * p43.y), (p3.z + mub * p43.z));
|
|
404
406
|
return new Line3D(resultSegmentPoint1, resultSegmentPoint2);
|
|
405
407
|
}
|
|
408
|
+
/**
|
|
409
|
+
* Accepts an array of Line3D and groups them into arrays of connected lines
|
|
410
|
+
* @param lines Lines to be grouped
|
|
411
|
+
* @param tolerance Tolerance for considering lines as connected
|
|
412
|
+
*/
|
|
413
|
+
static groupConnectedLines(lines, tolerance = 0) {
|
|
414
|
+
const visited = new Set();
|
|
415
|
+
// Use graph-based approach. Each line can be considered as an edge in the graph, and the endpoints of the lines can be considered as vertices.
|
|
416
|
+
// Then use Depth-First Search (DFS) to find connected components in the graph.
|
|
417
|
+
const dfs = (line, group) => {
|
|
418
|
+
if (visited.has(line))
|
|
419
|
+
return;
|
|
420
|
+
visited.add(line);
|
|
421
|
+
group.push(line);
|
|
422
|
+
lines.forEach((neighbor) => {
|
|
423
|
+
if (!visited.has(neighbor)) {
|
|
424
|
+
if (line.connectsTo(neighbor, tolerance)) {
|
|
425
|
+
dfs(neighbor, group);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
};
|
|
430
|
+
const connectedLines = [];
|
|
431
|
+
lines.forEach((line) => {
|
|
432
|
+
if (!visited.has(line)) {
|
|
433
|
+
const group = [];
|
|
434
|
+
dfs(line, group);
|
|
435
|
+
connectedLines.push(group);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
return connectedLines;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Returns true if any endpoint of this line if within the tolerance distance of any endpoint of the @other line.
|
|
442
|
+
* @param other
|
|
443
|
+
* @param tolerance
|
|
444
|
+
*/
|
|
445
|
+
connectsTo(other, tolerance = 0) {
|
|
446
|
+
return this.start.isNear(other.start, tolerance) ||
|
|
447
|
+
this.start.isNear(other.end, tolerance) ||
|
|
448
|
+
this.end.isNear(other.start, tolerance) ||
|
|
449
|
+
this.end.isNear(other.end, tolerance);
|
|
450
|
+
}
|
|
406
451
|
/**
|
|
407
452
|
* Project the line to 2D space, Y value is dropped
|
|
408
453
|
*/
|
|
@@ -419,7 +464,7 @@ export class Line3D extends Line3 {
|
|
|
419
464
|
* Deep clone of this line
|
|
420
465
|
*/
|
|
421
466
|
clone() {
|
|
422
|
-
return new Line3D(this.start.clone(), this.end.clone());
|
|
467
|
+
return new Line3D(this.start.clone(), this.end.clone(), this.index);
|
|
423
468
|
}
|
|
424
469
|
toString() {
|
|
425
470
|
return `Line3D { start: ${this.start.x}, ${this.start.y}, ${this.start.z}, end: ${this.end.x}, ${this.end.y}, ${this.end.z}}`;
|
package/esm/Vec2.js
CHANGED
|
@@ -70,17 +70,6 @@ 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
|
-
}
|
|
84
73
|
/**
|
|
85
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
|
|
86
75
|
* @param other Vector2
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@immugio/three-math-extensions",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.13",
|
|
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",
|
|
@@ -33,19 +33,21 @@
|
|
|
33
33
|
"postversion": "git push && git push --tags"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@types/jest": "^29.
|
|
36
|
+
"@types/jest": "^29.5.4",
|
|
37
37
|
"@types/offscreencanvas": "2019.7.0",
|
|
38
|
-
"@types/three": "0.
|
|
38
|
+
"@types/three": "0.152.0",
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^6.5.0",
|
|
39
40
|
"auto-changelog": "^2.4.0",
|
|
40
|
-
"jest": "^29.
|
|
41
|
+
"jest": "^29.6.4",
|
|
41
42
|
"rimraf": "^3.0.2",
|
|
42
43
|
"ts-jest": "^29.0.0",
|
|
43
44
|
"typedoc": "^0.23.23",
|
|
44
45
|
"typedoc-plugin-markdown": "^3.14.0",
|
|
45
|
-
"typescript": "4.9.5"
|
|
46
|
+
"typescript": "4.9.5",
|
|
47
|
+
"eslint": "^8.48.0"
|
|
46
48
|
},
|
|
47
49
|
"peerDependencies": {
|
|
48
|
-
"three": "
|
|
50
|
+
"three": "0.152.2"
|
|
49
51
|
},
|
|
50
52
|
"repository": {
|
|
51
53
|
"type": "git",
|
package/src/Line2D.ts
CHANGED
|
@@ -678,8 +678,8 @@ export class Line2D {
|
|
|
678
678
|
return direction.angleTo(directions2d.Right) < Math.PI / 4 || direction.angleTo(directions2d.Left) < Math.PI / 4;
|
|
679
679
|
}
|
|
680
680
|
|
|
681
|
-
public
|
|
682
|
-
return
|
|
681
|
+
public get isCloserToVertical(): boolean {
|
|
682
|
+
return !this.isCloserToHorizontal;
|
|
683
683
|
}
|
|
684
684
|
|
|
685
685
|
/**
|
package/src/Line3D.ts
CHANGED
|
@@ -10,13 +10,13 @@ export class Line3D extends Line3 {
|
|
|
10
10
|
|
|
11
11
|
readonly #target: Vec3;
|
|
12
12
|
|
|
13
|
-
constructor(start: Vec3, end: Vec3) {
|
|
13
|
+
constructor(start: Vec3, end: Vec3, public index: number = 0) {
|
|
14
14
|
super(start, end);
|
|
15
15
|
this.#target = new Vec3();
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
public static fromPoints(start: Point3, end: Point3): Line3D {
|
|
19
|
-
return new Line3D(new Vec3(start.x, start.y, start.z), new Vec3(end.x, end.y, end.z));
|
|
18
|
+
public static fromPoints(start: Point3, end: Point3, index: number = 0): Line3D {
|
|
19
|
+
return new Line3D(new Vec3(start.x, start.y, start.z), new Vec3(end.x, end.y, end.z), index);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
@@ -35,7 +35,7 @@ export class Line3D extends Line3 {
|
|
|
35
35
|
|
|
36
36
|
const lines: Line3D[] = [];
|
|
37
37
|
for (let i = 0; i < polygon.length - 1; i++) {
|
|
38
|
-
lines.push(Line3D.fromPoints(polygon[i], polygon[i + 1]));
|
|
38
|
+
lines.push(Line3D.fromPoints(polygon[i], polygon[i + 1], i));
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
return lines;
|
|
@@ -502,6 +502,57 @@ export class Line3D extends Line3 {
|
|
|
502
502
|
return new Line3D(resultSegmentPoint1, resultSegmentPoint2);
|
|
503
503
|
}
|
|
504
504
|
|
|
505
|
+
/**
|
|
506
|
+
* Accepts an array of Line3D and groups them into arrays of connected lines
|
|
507
|
+
* @param lines Lines to be grouped
|
|
508
|
+
* @param tolerance Tolerance for considering lines as connected
|
|
509
|
+
*/
|
|
510
|
+
public static groupConnectedLines(lines: Line3D[], tolerance: number = 0): Line3D[][] {
|
|
511
|
+
const visited: Set<Line3D> = new Set();
|
|
512
|
+
|
|
513
|
+
// Use graph-based approach. Each line can be considered as an edge in the graph, and the endpoints of the lines can be considered as vertices.
|
|
514
|
+
// Then use Depth-First Search (DFS) to find connected components in the graph.
|
|
515
|
+
const dfs = (line: Line3D, group: Line3D[]) => {
|
|
516
|
+
if (visited.has(line)) return;
|
|
517
|
+
visited.add(line);
|
|
518
|
+
group.push(line);
|
|
519
|
+
|
|
520
|
+
lines.forEach((neighbor) => {
|
|
521
|
+
if (!visited.has(neighbor)) {
|
|
522
|
+
if (
|
|
523
|
+
line.connectsTo(neighbor, tolerance)
|
|
524
|
+
) {
|
|
525
|
+
dfs(neighbor, group);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
const connectedLines: Line3D[][] = [];
|
|
532
|
+
|
|
533
|
+
lines.forEach((line) => {
|
|
534
|
+
if (!visited.has(line)) {
|
|
535
|
+
const group: Line3D[] = [];
|
|
536
|
+
dfs(line, group);
|
|
537
|
+
connectedLines.push(group);
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
return connectedLines;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Returns true if any endpoint of this line if within the tolerance distance of any endpoint of the @other line.
|
|
546
|
+
* @param other
|
|
547
|
+
* @param tolerance
|
|
548
|
+
*/
|
|
549
|
+
public connectsTo(other: Line3D, tolerance: number = 0): boolean {
|
|
550
|
+
return this.start.isNear(other.start, tolerance) ||
|
|
551
|
+
this.start.isNear(other.end, tolerance) ||
|
|
552
|
+
this.end.isNear(other.start, tolerance) ||
|
|
553
|
+
this.end.isNear(other.end, tolerance);
|
|
554
|
+
}
|
|
555
|
+
|
|
505
556
|
/**
|
|
506
557
|
* Project the line to 2D space, Y value is dropped
|
|
507
558
|
*/
|
|
@@ -520,7 +571,7 @@ export class Line3D extends Line3 {
|
|
|
520
571
|
* Deep clone of this line
|
|
521
572
|
*/
|
|
522
573
|
public clone(): this {
|
|
523
|
-
return new Line3D(this.start.clone(), this.end.clone()) as this;
|
|
574
|
+
return new Line3D(this.start.clone(), this.end.clone(), this.index) as this;
|
|
524
575
|
}
|
|
525
576
|
|
|
526
577
|
public toString(): string {
|
package/src/Vec2.ts
CHANGED
|
@@ -81,19 +81,6 @@ 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
|
-
|
|
97
84
|
/**
|
|
98
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
|
|
99
86
|
* @param other Vector2
|
package/types/Line3D.d.ts
CHANGED
|
@@ -4,10 +4,11 @@ import { Point3 } from "./Point3";
|
|
|
4
4
|
import { Line2D } from "./Line2D";
|
|
5
5
|
export declare class Line3D extends Line3 {
|
|
6
6
|
#private;
|
|
7
|
+
index: number;
|
|
7
8
|
start: Vec3;
|
|
8
9
|
end: Vec3;
|
|
9
|
-
constructor(start: Vec3, end: Vec3);
|
|
10
|
-
static fromPoints(start: Point3, end: Point3): Line3D;
|
|
10
|
+
constructor(start: Vec3, end: Vec3, index?: number);
|
|
11
|
+
static fromPoints(start: Point3, end: Point3, index?: number): Line3D;
|
|
11
12
|
/**
|
|
12
13
|
* Creates a polygon formed by an array of lines from points provided.
|
|
13
14
|
* The polygon will only be closed if either
|
|
@@ -145,6 +146,18 @@ export declare class Line3D extends Line3 {
|
|
|
145
146
|
* @param other
|
|
146
147
|
*/
|
|
147
148
|
intersect(other: Line3D): Line3D;
|
|
149
|
+
/**
|
|
150
|
+
* Accepts an array of Line3D and groups them into arrays of connected lines
|
|
151
|
+
* @param lines Lines to be grouped
|
|
152
|
+
* @param tolerance Tolerance for considering lines as connected
|
|
153
|
+
*/
|
|
154
|
+
static groupConnectedLines(lines: Line3D[], tolerance?: number): Line3D[][];
|
|
155
|
+
/**
|
|
156
|
+
* Returns true if any endpoint of this line if within the tolerance distance of any endpoint of the @other line.
|
|
157
|
+
* @param other
|
|
158
|
+
* @param tolerance
|
|
159
|
+
*/
|
|
160
|
+
connectsTo(other: Line3D, tolerance?: number): boolean;
|
|
148
161
|
/**
|
|
149
162
|
* Project the line to 2D space, Y value is dropped
|
|
150
163
|
*/
|
package/types/Vec2.d.ts
CHANGED
|
@@ -44,12 +44,6 @@ 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;
|
|
53
47
|
/**
|
|
54
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
|
|
55
49
|
* @param other Vector2
|