@immugio/three-math-extensions 0.0.13 → 0.0.15
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 +18 -1
- package/HowToRelease.txt +13 -0
- package/README.md +4 -7
- package/cjs/Line2D.js +3 -3
- package/cjs/Line3D.js +39 -0
- package/cjs/Vec2.js +5 -5
- package/cjs/Vec3.js +46 -7
- package/docs/README.md +5 -0
- package/docs/classes/Line2D.md +1092 -0
- package/docs/classes/Line3D.md +732 -0
- package/docs/classes/Vec2.md +167 -0
- package/docs/classes/Vec3.md +288 -0
- package/docs/modules.md +12 -0
- package/esm/Line2D.js +3 -3
- package/esm/Line3D.js +39 -0
- package/esm/Vec2.js +5 -5
- package/esm/Vec3.js +46 -7
- package/package.json +3 -1
- package/src/Line2D.ts +4 -4
- package/src/Line3D.ts +58 -0
- package/src/Vec2.ts +5 -5
- package/src/Vec3.ts +47 -8
- package/types/Line2D.d.ts +1 -1
- package/types/Line3D.d.ts +10 -0
- package/types/Vec2.d.ts +4 -4
- package/types/Vec3.d.ts +46 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,7 +7,24 @@ 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.0.
|
|
10
|
+
## [0.0.15](https://github.com/Immugio/three-math-extensions/compare/0.0.14...0.0.15)
|
|
11
|
+
|
|
12
|
+
### Commits
|
|
13
|
+
|
|
14
|
+
- Add intersect method to Line3D [`6fe47de`](https://github.com/Immugio/three-math-extensions/commit/6fe47de7caaa1807b47a4363e551510c463757d7)
|
|
15
|
+
|
|
16
|
+
## [0.0.14](https://github.com/Immugio/three-math-extensions/compare/0.0.13...0.0.14) - 2022-12-26
|
|
17
|
+
|
|
18
|
+
### Commits
|
|
19
|
+
|
|
20
|
+
- Generate documentation using TSDoc [`304c3a8`](https://github.com/Immugio/three-math-extensions/commit/304c3a84b5dcc49183db57083f550d134ae641b2)
|
|
21
|
+
- Improve documentation for Vec2 and Vec3 [`6a2373d`](https://github.com/Immugio/three-math-extensions/commit/6a2373d8b6754a87720dcaea5d98336bfa7bc5b5)
|
|
22
|
+
- Clean up documentation [`70c8d5e`](https://github.com/Immugio/three-math-extensions/commit/70c8d5efe8b7095d7a03af637df3af5d46615293)
|
|
23
|
+
- Documentation improvements [`160db8b`](https://github.com/Immugio/three-math-extensions/commit/160db8ba6d6e5eb63a4e91ed9c40efd6fa70181a)
|
|
24
|
+
- Documentation improvements [`2f3e676`](https://github.com/Immugio/three-math-extensions/commit/2f3e6768b3b0139b60688fc151a4084f15566f8d)
|
|
25
|
+
- Revert npm ignore [`590b26a`](https://github.com/Immugio/three-math-extensions/commit/590b26a9e57fc41466b51e711f3f5c648e2d56ed)
|
|
26
|
+
|
|
27
|
+
## [0.0.13](https://github.com/Immugio/three-math-extensions/compare/0.0.12...0.0.13) - 2022-12-21
|
|
11
28
|
|
|
12
29
|
### Commits
|
|
13
30
|
|
package/HowToRelease.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Update documentation:
|
|
2
|
+
npx typedoc --plugin typedoc-plugin-markdown --out docs src/index.ts --excludeExternals --excludeProtected --excludePrivate --githubPages false
|
|
3
|
+
|
|
4
|
+
Before release
|
|
5
|
+
- Ensure clean working directory
|
|
6
|
+
- Ensure that the last release commit has a tag that matches the latest release number in the format "0.0.11". This step should have been completed in the previous release. It's only mentioned here in case it's for any reason missing.
|
|
7
|
+
- Run "prerelease" script to verify that the code builds and the tests pass
|
|
8
|
+
|
|
9
|
+
Release:
|
|
10
|
+
- Bump up version in package.json e.g. "0.0.11" -> "0.0.12"
|
|
11
|
+
- Run "version" script, it should add the new commits since the last release into `CHANGELOG.md`
|
|
12
|
+
- Commit and push the changes in `package.json` and `CHANGELOG.md`, ideally, call the commit as the new release e.g. "`0.0.12`". This is not necessary but makes it easier to find
|
|
13
|
+
- **Tag the commit** the new version number. In this example "`0.0.12`" and push. This is required for CI to build and publish to npm.
|
package/README.md
CHANGED
|
@@ -2,10 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/Immugio/three-math-extensions/actions/workflows/build.yml)
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
-
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
- Run "version" script, it should add the new commits since the last release into `CHANGELOG.md`
|
|
10
|
-
- Commit and push the changes in `package.json` and `CHANGELOG.md`, ideally, call the commit as the new release e.g. "`0.0.12`". This is not necessary but makes it easier to find
|
|
11
|
-
- NOW THE CRITICAL PART - **tag the commit** with the new version number. In this example "`0.0.12`" and push. This is required for CI to build and release and publish to npm.
|
|
5
|
+
- [Line2D](docs/classes/Line2D.md)
|
|
6
|
+
- [Line3D](docs/classes/Line3D.md)
|
|
7
|
+
- [Vec2](docs/classes/Vec2.md)
|
|
8
|
+
- [Vec3](docs/classes/Vec3.md)
|
package/cjs/Line2D.js
CHANGED
|
@@ -550,6 +550,9 @@ class Line2D {
|
|
|
550
550
|
}
|
|
551
551
|
return null;
|
|
552
552
|
}
|
|
553
|
+
equals(other) {
|
|
554
|
+
return !!other && this.start.equals(other.start) && this.end.equals(other.end);
|
|
555
|
+
}
|
|
553
556
|
/**
|
|
554
557
|
* Deep clone of this line
|
|
555
558
|
*/
|
|
@@ -559,8 +562,5 @@ class Line2D {
|
|
|
559
562
|
toString() {
|
|
560
563
|
return `Line(${this.start.x}, ${this.start.y}, ${this.end.x}, ${this.end.y})`;
|
|
561
564
|
}
|
|
562
|
-
equals(other) {
|
|
563
|
-
return !!other && this.start.equals(other.start) && this.end.equals(other.end);
|
|
564
|
-
}
|
|
565
565
|
}
|
|
566
566
|
exports.Line2D = Line2D;
|
package/cjs/Line3D.js
CHANGED
|
@@ -367,6 +367,45 @@ class Line3D extends three_1.Line3 {
|
|
|
367
367
|
this.end.add(p);
|
|
368
368
|
return this;
|
|
369
369
|
}
|
|
370
|
+
/**
|
|
371
|
+
* Calculates the intersection between this and `other` line. The lines are assumed to be infinite.
|
|
372
|
+
* In a lot of cases an actual intersection cannot be calculated due to rounding errors.
|
|
373
|
+
* Therefore, the intersection calculated by this method comes in a form of the shorted possible line segment connecting the two lines.
|
|
374
|
+
* Sources:
|
|
375
|
+
* http://paulbourke.net/geometry/pointlineplane/
|
|
376
|
+
* https://stackoverflow.com/questions/2316490/the-algorithm-to-find-the-point-of-intersection-of-two-3d-line-segment/2316934#2316934
|
|
377
|
+
* @param other
|
|
378
|
+
*/
|
|
379
|
+
intersect(other) {
|
|
380
|
+
const p1 = this.start.clone();
|
|
381
|
+
const p2 = this.end.clone();
|
|
382
|
+
const p3 = other.start.clone();
|
|
383
|
+
const p4 = other.end.clone();
|
|
384
|
+
const p13 = p1.clone().sub(p3);
|
|
385
|
+
const p43 = p4.clone().sub(p3);
|
|
386
|
+
if (p43.lengthSq() <= Number.EPSILON) {
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
const p21 = p2.clone().sub(p1);
|
|
390
|
+
if (p21.lengthSq() <= Number.EPSILON) {
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
const d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;
|
|
394
|
+
const d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
|
|
395
|
+
const d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
|
|
396
|
+
const d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
|
|
397
|
+
const d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;
|
|
398
|
+
const denominator = d2121 * d4343 - d4321 * d4321;
|
|
399
|
+
if (Math.abs(denominator) <= Number.EPSILON) {
|
|
400
|
+
return null;
|
|
401
|
+
}
|
|
402
|
+
const numerator = d1343 * d4321 - d1321 * d4343;
|
|
403
|
+
const mua = numerator / denominator;
|
|
404
|
+
const mub = (d1343 + d4321 * (mua)) / d4343;
|
|
405
|
+
const resultSegmentPoint1 = new Vec3_1.Vec3((p1.x + mua * p21.x), (p1.y + mua * p21.y), (p1.z + mua * p21.z));
|
|
406
|
+
const resultSegmentPoint2 = new Vec3_1.Vec3((p3.x + mub * p43.x), (p3.y + mub * p43.y), (p3.z + mub * p43.z));
|
|
407
|
+
return new Line3D(resultSegmentPoint1, resultSegmentPoint2);
|
|
408
|
+
}
|
|
370
409
|
/**
|
|
371
410
|
* Project the line to 2D space, Y value is dropped
|
|
372
411
|
*/
|
package/cjs/Vec2.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.Vec2 = void 0;
|
|
|
4
4
|
const three_1 = require("three");
|
|
5
5
|
const Vec3_1 = require("./Vec3");
|
|
6
6
|
/**
|
|
7
|
-
* Vec2 represents a 2D vector. It extends `Vector2` from the `
|
|
7
|
+
* Vec2 represents a 2D vector. It extends `Vector2` from the `threejs` library.
|
|
8
8
|
*/
|
|
9
9
|
class Vec2 extends three_1.Vector2 {
|
|
10
10
|
/**
|
|
@@ -41,12 +41,12 @@ class Vec2 extends three_1.Vector2 {
|
|
|
41
41
|
return this;
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
|
-
* Projects this Vec2 instance to a Vec3 instance in 3D space.
|
|
45
|
-
* @param
|
|
44
|
+
* Projects this Vec2 instance to a Vec3 instance in 3D space. Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
|
|
45
|
+
* @param y - The y value of the new Vec3 instance.
|
|
46
46
|
* @returns A new Vec3 instance.
|
|
47
47
|
*/
|
|
48
|
-
in3DSpace(
|
|
49
|
-
return new Vec3_1.Vec3(this.x,
|
|
48
|
+
in3DSpace(y = 0) {
|
|
49
|
+
return new Vec3_1.Vec3(this.x, y, this.y);
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* Determines if this Vec2 instance is near the target Vec2.
|
package/cjs/Vec3.js
CHANGED
|
@@ -3,11 +3,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Vec3 = void 0;
|
|
4
4
|
const three_1 = require("three");
|
|
5
5
|
const Vec2_1 = require("./Vec2");
|
|
6
|
+
/**
|
|
7
|
+
* Vec3 represents a 2D vector. It extends `Vector3` from the `threejs` library.
|
|
8
|
+
*/
|
|
6
9
|
class Vec3 extends three_1.Vector3 {
|
|
7
10
|
#target;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new Vec3 instance from an {x, y, z} object.
|
|
13
|
+
* @param point - The {x, y, z} instance.
|
|
14
|
+
* @returns A new Vec3 instance.
|
|
15
|
+
*/
|
|
8
16
|
static fromPoint(point) {
|
|
9
17
|
return new Vec3(point.x, point.y, point.z);
|
|
10
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Moves this Vec3 instance towards the target Vec3 by the given amount.
|
|
21
|
+
* @param target - The target Vec3.
|
|
22
|
+
* @param amount - The distance to move.
|
|
23
|
+
* @returns This Vec3 instance.
|
|
24
|
+
*/
|
|
11
25
|
moveTowards(target, amount) {
|
|
12
26
|
if (this.#target === undefined) {
|
|
13
27
|
this.#target = new three_1.Vector3();
|
|
@@ -16,40 +30,65 @@ class Vec3 extends three_1.Vector3 {
|
|
|
16
30
|
this.add(this.#target.sub(this).normalize().multiplyScalar(amount));
|
|
17
31
|
return this;
|
|
18
32
|
}
|
|
19
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Moves this Vec3 instance halfway towards the target Vec3 by the given amount.
|
|
35
|
+
* @param target - The target Vec3.
|
|
36
|
+
* @returns This Vec3 instance.
|
|
37
|
+
*/
|
|
38
|
+
moveHalfWayTowards(target) {
|
|
20
39
|
if (this.#target === undefined) {
|
|
21
40
|
this.#target = new three_1.Vector3();
|
|
22
41
|
}
|
|
23
42
|
return this.moveTowards(target, this.distanceTo(target) / 2);
|
|
24
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Adds y amount to this Vec3 instance and return this
|
|
46
|
+
* @param y
|
|
47
|
+
*/
|
|
25
48
|
addY(y) {
|
|
26
49
|
this.y += y;
|
|
27
50
|
return this;
|
|
28
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Adds x amount to this Vec3 instance and return this
|
|
54
|
+
* @param x
|
|
55
|
+
*/
|
|
29
56
|
addX(x) {
|
|
30
57
|
this.x += x;
|
|
31
58
|
return this;
|
|
32
59
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return this;
|
|
38
|
-
}
|
|
60
|
+
/**
|
|
61
|
+
* Returns a clone of the point closest to this from the given points.
|
|
62
|
+
* @param points
|
|
63
|
+
*/
|
|
39
64
|
closest(...points) {
|
|
40
65
|
const withDistances = points.map(p => ({ point: p, distance: this.distanceTo(p) }));
|
|
41
66
|
const closest = withDistances.reduce((a, b) => a.distance < b.distance ? a : b);
|
|
42
67
|
return Vec3.fromPoint(closest.point);
|
|
43
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns a clone of this Vec3 instance with y and z swapped.
|
|
71
|
+
*/
|
|
44
72
|
toPointWithFlippedYZ() {
|
|
45
73
|
return new Vec3(this.x, this.z, this.y);
|
|
46
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Projects this Vec3 instance onto 2d plan. Vec3.z becomes Vec2.y and Vec3.y is ignored.
|
|
77
|
+
*/
|
|
47
78
|
onPlan() {
|
|
48
79
|
return new Vec2_1.Vec2(this.x, this.z);
|
|
49
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Get distance to another vector while ignoring the y-axis.
|
|
83
|
+
* @param point
|
|
84
|
+
*/
|
|
50
85
|
horizontalDistanceTo(point) {
|
|
51
86
|
return new three_1.Vector3(this.x, 0, this.z).distanceTo(new three_1.Vector3(point.x, 0, point.z));
|
|
52
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Determines if this Vec2 instance is near the target Vec2.
|
|
90
|
+
* maxDistance is the maximum distance between the two vectors within which they are considered `near`.
|
|
91
|
+
*/
|
|
53
92
|
isNear(v, maxDistance = undefined) {
|
|
54
93
|
if (maxDistance === undefined) {
|
|
55
94
|
return this.equals(v);
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
@immugio/three-math-extensions / [Exports](modules.md)
|
|
2
|
+
|
|
3
|
+
# Set of utilities for 2d and 3d line math built on top of three.js
|
|
4
|
+
|
|
5
|
+
[](https://github.com/Immugio/three-math-extensions/actions/workflows/build.yml)
|