@immugio/three-math-extensions 0.0.11 → 0.0.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 +13 -1
- package/README.md +11 -3
- package/cjs/Line2D.js +566 -566
- package/cjs/Line3D.js +392 -392
- package/cjs/Point2.js +2 -2
- package/cjs/Point3.js +2 -2
- package/cjs/Vec2.js +62 -34
- package/cjs/Vec3.js +63 -63
- package/cjs/index.js +11 -11
- package/esm/Line2D.js +562 -562
- package/esm/Line3D.js +388 -388
- package/esm/Point2.js +1 -1
- package/esm/Point3.js +1 -1
- package/esm/Vec2.js +58 -30
- package/esm/Vec3.js +59 -59
- package/esm/index.js +4 -4
- package/package.json +52 -52
- package/src/Line2D.ts +660 -660
- package/src/Line3D.ts +470 -470
- package/src/Point2.ts +3 -3
- package/src/Point3.ts +6 -6
- package/src/Vec2.ts +65 -37
- package/src/Vec3.ts +77 -77
- package/src/index.ts +3 -3
- package/types/Line2D.d.ts +222 -222
- package/types/Line3D.d.ts +151 -151
- package/types/Point2.d.ts +4 -4
- package/types/Point3.d.ts +5 -5
- package/types/Vec2.d.ts +38 -10
- package/types/Vec3.d.ts +18 -18
- package/types/index.d.ts +4 -4
package/src/Point2.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export interface Point2 {
|
|
2
|
-
x: number,
|
|
3
|
-
y: number,
|
|
1
|
+
export interface Point2 {
|
|
2
|
+
x: number,
|
|
3
|
+
y: number,
|
|
4
4
|
}
|
package/src/Point3.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export interface Point3 {
|
|
2
|
-
x: number,
|
|
3
|
-
y: number,
|
|
4
|
-
z: number
|
|
5
|
-
}
|
|
6
|
-
|
|
1
|
+
export interface Point3 {
|
|
2
|
+
x: number,
|
|
3
|
+
y: number,
|
|
4
|
+
z: number
|
|
5
|
+
}
|
|
6
|
+
|
package/src/Vec2.ts
CHANGED
|
@@ -1,38 +1,66 @@
|
|
|
1
|
-
import { Vector2 } from "three";
|
|
2
|
-
import { Vec3 } from "./Vec3";
|
|
3
|
-
import { Point2 } from "./Point2";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
1
|
+
import { Vector2 } from "three";
|
|
2
|
+
import { Vec3 } from "./Vec3";
|
|
3
|
+
import { Point2 } from "./Point2";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Vec2 represents a 2D vector. It extends `Vector2` from the `three` library.
|
|
7
|
+
*/
|
|
8
|
+
export class Vec2 extends Vector2 {
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new Vec2 instance from an {x, y} object.
|
|
12
|
+
* @param point - The {x, y} instance.
|
|
13
|
+
* @returns A new Vec2 instance.
|
|
14
|
+
*/
|
|
15
|
+
public static fromPoint(point: Point2): Vec2 {
|
|
16
|
+
return new Vec2(point.x, point.y);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Moves this Vec2 instance towards the target Vec2 by the given amount.
|
|
21
|
+
* @param target - The target Vec2.
|
|
22
|
+
* @param amount - The distance to move.
|
|
23
|
+
* @returns This Vec2 instance.
|
|
24
|
+
*/
|
|
25
|
+
public moveTowards(target: Vector2, amount: number): Vec2 {
|
|
26
|
+
const move = target.clone().sub(this).normalize().multiplyScalar(amount);
|
|
27
|
+
this.add(move);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Rounds the x and y values of this Vec2 instance if they are close to an integer value.
|
|
33
|
+
* @param max - The maximum difference between the value and the nearest integer.
|
|
34
|
+
* @returns This Vec2 instance.
|
|
35
|
+
*/
|
|
36
|
+
public roundIfCloseToInteger(max: number = 0.000000000001): this {
|
|
37
|
+
if (Math.abs(this.x - Math.round(this.x)) < max) {
|
|
38
|
+
this.x = Math.round(this.x);
|
|
39
|
+
}
|
|
40
|
+
if (Math.abs(this.y - Math.round(this.y)) < max) {
|
|
41
|
+
this.y = Math.round(this.y);
|
|
42
|
+
}
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Projects this Vec2 instance to a Vec3 instance in 3D space.
|
|
48
|
+
* @param z - The z value of the new Vec3 instance.
|
|
49
|
+
* @returns A new Vec3 instance.
|
|
50
|
+
*/
|
|
51
|
+
public in3DSpace(z: number = 0): Vec3 {
|
|
52
|
+
return new Vec3(this.x, z, this.y);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Determines if this Vec2 instance is near the target Vec2.
|
|
57
|
+
* maxDistance is the maximum distance between the two vectors within which they are considered `near`.
|
|
58
|
+
*/
|
|
59
|
+
public isNear(v: Vector2, maxDistance: number = undefined): boolean {
|
|
60
|
+
if (!maxDistance) {
|
|
61
|
+
return this.equals(v);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return this.distanceTo(v) <= maxDistance;
|
|
65
|
+
}
|
|
38
66
|
}
|
package/src/Vec3.ts
CHANGED
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import { Vector3 } from "three";
|
|
2
|
-
import { Vec2 } from "./Vec2";
|
|
3
|
-
import { Point3 } from "./Point3";
|
|
4
|
-
|
|
5
|
-
export class Vec3 extends Vector3 {
|
|
6
|
-
|
|
7
|
-
#target: Vector3;
|
|
8
|
-
|
|
9
|
-
public static fromPoint(point: Point3): Vec3 {
|
|
10
|
-
return new Vec3(point.x, point.y, point.z);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public moveTowards(target: Vector3, amount: number): Vec3 {
|
|
14
|
-
if (this.#target === undefined) {
|
|
15
|
-
this.#target = new Vector3();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
this.#target.copy(target);
|
|
19
|
-
this.add(this.#target.sub(this).normalize().multiplyScalar(amount));
|
|
20
|
-
|
|
21
|
-
return this;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public centerTowards(target: Vector3): Vec3 {
|
|
25
|
-
if (this.#target === undefined) {
|
|
26
|
-
this.#target = new Vector3();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return this.moveTowards(target, this.distanceTo(target) / 2);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public addY(y: number): Vec3 {
|
|
33
|
-
this.y += y;
|
|
34
|
-
return this;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public addX(x: number): Vec3 {
|
|
38
|
-
this.x += x;
|
|
39
|
-
return this;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public scale(p: Vector3): Vec3 {
|
|
43
|
-
this.x *= p.x;
|
|
44
|
-
this.y *= p.y;
|
|
45
|
-
this.z *= p.z;
|
|
46
|
-
return this;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
public closest(...points: Vector3[]): Vec3 {
|
|
50
|
-
const withDistances = points.map(p => ({ point: p, distance: this.distanceTo(p) }));
|
|
51
|
-
const closest = withDistances.reduce((a, b) => a.distance < b.distance ? a : b);
|
|
52
|
-
return Vec3.fromPoint(closest.point);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public toPointWithFlippedYZ(): Vec3 {
|
|
56
|
-
return new Vec3(this.x, this.z, this.y);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public onPlan(): Vec2 {
|
|
60
|
-
return new Vec2(this.x, this.z);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public horizontalDistanceTo(point: Vector3): number {
|
|
64
|
-
return new Vector3(this.x, 0, this.z).distanceTo(new Vector3(point.x, 0, point.z));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
public isNear(v: Vector3, maxDistance: number = undefined): boolean {
|
|
68
|
-
if (maxDistance === undefined) {
|
|
69
|
-
return this.equals(v);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return this.distanceTo(v) <= maxDistance;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public clone(): this {
|
|
76
|
-
return super.clone();
|
|
77
|
-
}
|
|
1
|
+
import { Vector3 } from "three";
|
|
2
|
+
import { Vec2 } from "./Vec2";
|
|
3
|
+
import { Point3 } from "./Point3";
|
|
4
|
+
|
|
5
|
+
export class Vec3 extends Vector3 {
|
|
6
|
+
|
|
7
|
+
#target: Vector3;
|
|
8
|
+
|
|
9
|
+
public static fromPoint(point: Point3): Vec3 {
|
|
10
|
+
return new Vec3(point.x, point.y, point.z);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public moveTowards(target: Vector3, amount: number): Vec3 {
|
|
14
|
+
if (this.#target === undefined) {
|
|
15
|
+
this.#target = new Vector3();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
this.#target.copy(target);
|
|
19
|
+
this.add(this.#target.sub(this).normalize().multiplyScalar(amount));
|
|
20
|
+
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public centerTowards(target: Vector3): Vec3 {
|
|
25
|
+
if (this.#target === undefined) {
|
|
26
|
+
this.#target = new Vector3();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return this.moveTowards(target, this.distanceTo(target) / 2);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public addY(y: number): Vec3 {
|
|
33
|
+
this.y += y;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public addX(x: number): Vec3 {
|
|
38
|
+
this.x += x;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public scale(p: Vector3): Vec3 {
|
|
43
|
+
this.x *= p.x;
|
|
44
|
+
this.y *= p.y;
|
|
45
|
+
this.z *= p.z;
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public closest(...points: Vector3[]): Vec3 {
|
|
50
|
+
const withDistances = points.map(p => ({ point: p, distance: this.distanceTo(p) }));
|
|
51
|
+
const closest = withDistances.reduce((a, b) => a.distance < b.distance ? a : b);
|
|
52
|
+
return Vec3.fromPoint(closest.point);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public toPointWithFlippedYZ(): Vec3 {
|
|
56
|
+
return new Vec3(this.x, this.z, this.y);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public onPlan(): Vec2 {
|
|
60
|
+
return new Vec2(this.x, this.z);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public horizontalDistanceTo(point: Vector3): number {
|
|
64
|
+
return new Vector3(this.x, 0, this.z).distanceTo(new Vector3(point.x, 0, point.z));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public isNear(v: Vector3, maxDistance: number = undefined): boolean {
|
|
68
|
+
if (maxDistance === undefined) {
|
|
69
|
+
return this.equals(v);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return this.distanceTo(v) <= maxDistance;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public clone(): this {
|
|
76
|
+
return super.clone();
|
|
77
|
+
}
|
|
78
78
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Vec2 } from "./Vec2";
|
|
2
|
-
export { Vec3 } from "./Vec3";
|
|
3
|
-
export { Line2D } from "./Line2D";
|
|
1
|
+
export { Vec2 } from "./Vec2";
|
|
2
|
+
export { Vec3 } from "./Vec3";
|
|
3
|
+
export { Line2D } from "./Line2D";
|
|
4
4
|
export { Line3D } from "./Line3D";
|