@immugio/three-math-extensions 0.0.3 → 0.0.5

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.
@@ -10,6 +10,35 @@ describe("Line3d", () => {
10
10
  expect(line.end).toEqual(new Vec3(4, 5, 6));
11
11
  });
12
12
 
13
+ it("should create a single line polygon from a 2 points polygon", () => {
14
+ const start = new Vec3(1, 2, 20);
15
+ const end = new Vec3(3, 4, 20);
16
+ const lines = Line3D.fromPolygon([start, end]);
17
+ expect(lines.length).toEqual(1);
18
+ expect(lines[0]).toEqual(new Line3D(start, end));
19
+ });
20
+
21
+ it("should create a 2 lines polygon from a 3 points polygon", () => {
22
+ const p1 = new Vec3(1, 2, 20);
23
+ const p2 = new Vec3(3, 4, 20);
24
+ const p3 = new Vec3(5, 2, 20);
25
+ const lines = Line3D.fromPolygon([p1, p2, p3]);
26
+ expect(lines.length).toEqual(2);
27
+ expect(lines[0]).toEqual(new Line3D(p1, p2));
28
+ expect(lines[1]).toEqual(new Line3D(p2, p3));
29
+ });
30
+
31
+ it("should create a closed 3 lines polygon from a 3 points polygon", () => {
32
+ const p1 = new Vec3(1, 2, 20);
33
+ const p2 = new Vec3(3, 4, 20);
34
+ const p3 = new Vec3(5, 2, 20);
35
+ const lines = Line3D.fromPolygon([p1, p2, p3], true);
36
+ expect(lines.length).toEqual(3);
37
+ expect(lines[0]).toEqual(new Line3D(p1, p2));
38
+ expect(lines[1]).toEqual(new Line3D(p2, p3));
39
+ expect(lines[2]).toEqual(new Line3D(p3, p1));
40
+ });
41
+
13
42
  it("should return the expected center", () => {
14
43
  const line = defaultLine();
15
44
  expect(line.center).toEqual(new Vec3(0, 0, 0));
@@ -0,0 +1,215 @@
1
+ import { Point2 } from "./Point2";
2
+ import { Vector2 } from "three";
3
+ import { Vec2 } from "./Vec2";
4
+ export declare class Line2D {
5
+ start: Vec2;
6
+ end: Vec2;
7
+ index: number;
8
+ constructor(start: Vec2, end: Vec2, index?: number);
9
+ static fromCoordinates(x1: number, y1: number, x2: number, y2: number, index?: number): Line2D;
10
+ static fromPoints(p1: Point2, p2: Point2, index?: number): Line2D;
11
+ /**
12
+ * Creates a polygon formed by an array of lines from points provided.
13
+ * The polygon will only be closed if either
14
+ * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
15
+ */
16
+ static fromPolygon(polygon: Point2[], forceClosedPolygon?: boolean): Line2D[];
17
+ static fromLength(length: number): Line2D;
18
+ get center(): Vec2;
19
+ /**
20
+ * Set the center of the line to the provided point. Length and direction remain unchanged.
21
+ * Modifies this line.
22
+ * @param value
23
+ */
24
+ set center(value: Point2);
25
+ /**
26
+ * Set the center of the line to the provided point. Length and direction remain unchanged.
27
+ * Modifies this line.
28
+ * @param value
29
+ */
30
+ setCenter(value: Point2): Line2D;
31
+ resize(amount: number): Line2D;
32
+ moveStartPoint(amount: number): Line2D;
33
+ /**
34
+ * Moves end point on the line by the given amount. Plus values move the point further away from the center.
35
+ * Modifies this line.
36
+ */
37
+ moveEndPoint(amount: number): Line2D;
38
+ private movePointOnThisLine;
39
+ /**
40
+ * Set the length of this line. Center and direction remain unchanged.
41
+ * Modifies this line.
42
+ * @param l
43
+ */
44
+ set length(l: number);
45
+ get length(): number;
46
+ /**
47
+ * Set the length of this line. Center and direction remain unchanged.
48
+ * @param length
49
+ */
50
+ setLength(length: number): this;
51
+ /**
52
+ * Returns the start and end points of the line as an array.
53
+ * Endpoints are not cloned.
54
+ */
55
+ get endpoints(): Vec2[];
56
+ /**
57
+ * Returns the direction of this line.
58
+ */
59
+ get direction(): Vec2;
60
+ /**
61
+ * Inverts the direction of the line.
62
+ * Modifies this line.
63
+ */
64
+ flip(): Line2D;
65
+ /**
66
+ * Rotates the line around the center by the given angle in radians.
67
+ * Modifies this line.
68
+ * @param radians Positive values rotate counter-clockwise.
69
+ * @param center
70
+ */
71
+ rotate(radians: number, center?: Vec2): Line2D;
72
+ /**
73
+ * Move the line by the given vector.
74
+ * Modifies this line.
75
+ */
76
+ translate(value: Point2): Line2D;
77
+ /**
78
+ * Move the line to its left by the given amount.
79
+ * Modifies this line.
80
+ */
81
+ translateLeft(amount: number): Line2D;
82
+ /**
83
+ * Move the line to its right by the given amount.
84
+ * Modifies this line.
85
+ */
86
+ translateRight(amount: number): Line2D;
87
+ /**
88
+ * Returns true when the point is actually inside the (finite) line segment.
89
+ * https://jsfiddle.net/c06zdxtL/2/
90
+ * https://stackoverflow.com/questions/6865832/detecting-if-a-point-is-of-a-line-segment/6877674
91
+ * @param point: Point2
92
+ */
93
+ isPointOnLineSection(point: Point2): boolean;
94
+ /**
95
+ * Returns true when the point is beside the line **segment** and within the maxDistance.
96
+ * @param point
97
+ * @param maxDistance
98
+ */
99
+ isPointCloseToAndBesideLineSection(point: Point2, maxDistance: number): boolean;
100
+ /**
101
+ * Returns true when the point is beside the line **segment**
102
+ * @param point
103
+ */
104
+ isPointBesideLineSection(point: Point2): boolean;
105
+ /**
106
+ * Returns true when the point is on the **infinite** line.
107
+ * @param point
108
+ */
109
+ isPointOnInfiniteLine(point: Point2): boolean;
110
+ /**
111
+ * Returns true if other line is collinear and overlaps or at least touching this line.
112
+ * @param other
113
+ */
114
+ isCollinearWithTouchOrOverlap(other: Line2D): boolean;
115
+ /**
116
+ * Returns true if there is any overlap between this line and the @other line section.
117
+ */
118
+ overlaps(other: Line2D): boolean;
119
+ /**
120
+ * Logical AND of this and the other line section.
121
+ * @param other
122
+ */
123
+ getOverlap(other: Line2D): Line2D;
124
+ /**
125
+ * Joins a copy of @line with the @other line.
126
+ * Other must be parallel to this line.
127
+ * Returns null if there is no overlap
128
+ * Clones the line, does not modify.
129
+ * @param line
130
+ * @param other
131
+ */
132
+ static joinLine(line: Line2D, other: Line2D): Line2D;
133
+ /**
134
+ * Joins provided lines into several joined lines.
135
+ * Lines must be parallel for joining.
136
+ * Clone the lines, does not modify.
137
+ * @param lines
138
+ */
139
+ static joinLines(lines: Line2D[]): Line2D[];
140
+ /**
141
+ * Divides the Line3D into a number of segments of the given length.
142
+ * Clone the line, does not modify.
143
+ * @param maxSegmentLength number
144
+ */
145
+ chunk(maxSegmentLength: number): Line2D[];
146
+ /**
147
+ * Returns the closest point parameter on the **infinite** line to the given point.
148
+ * @param point
149
+ */
150
+ closestPointToPointParameterOnInfiniteLine(point: Vector2): number;
151
+ /**
152
+ * Returns the closest point on the **infinite** line to the given point.
153
+ * @param point
154
+ */
155
+ closestPointOnInfiniteLine(point: Vector2): Vec2;
156
+ /**
157
+ * Returns the closest point on the line **section** to the given point.
158
+ * @param point
159
+ */
160
+ closestPointOnLine(point: Vector2): Vec2;
161
+ /**
162
+ * Returns the distance between the **infinite** line and the point.
163
+ * @param point
164
+ */
165
+ distanceToPointOnInfiniteLine(point: Point2): number;
166
+ /**
167
+ * Returns lines that are the result of clipping @source line by the @clips.
168
+ * Clips must be parallel to this line.
169
+ * Clones the line, does not modify this.
170
+ * @param source
171
+ * @param clips
172
+ */
173
+ static clipLines(source: Line2D, clips: Line2D[]): Line2D[];
174
+ /**
175
+ * Returns the original line section split into two parts, if the line **sections** overlap, otherwise null
176
+ */
177
+ splitAtIntersection(other: Line2D, tolerance?: number): Line2D[];
178
+ /**
179
+ * If lines **sections** overlap, returns the original line section split into two parts, sorted by length
180
+ * Else, if the **infinite** lines intersect, returns a new line extended to the intersection point
181
+ * Otherwise, null if the lines are parallel and do not intersect
182
+ */
183
+ splitAtOrExtendToIntersection(other: Line2D): Line2D[];
184
+ private static order;
185
+ private static subtractSingle;
186
+ /**
187
+ * If other line is not contained within this line, the excess is trimmed.
188
+ * Does not create a copy. Provided line is modified.
189
+ * @param lineToTrim
190
+ */
191
+ trimExcess(lineToTrim: Line2D): void;
192
+ /**
193
+ * If other line is shorter than this, endpoints are moved to extend other
194
+ * Does not create a copy. Provided line is modified.
195
+ * @param lineToExtend
196
+ * @param tolerance
197
+ */
198
+ extendToEnds(lineToExtend: Line2D, tolerance: number): void;
199
+ /**
200
+ * Returns the intersection point of two lines. The lines are assumed to be infinite.
201
+ */
202
+ intersect(other: Line2D): Vec2;
203
+ /**
204
+ * Check that the infinite lines intersect and that they are in the specified angle to each other
205
+ * @param other Line
206
+ * @param expectedAngleInRads number
207
+ */
208
+ hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number): Vec2;
209
+ /**
210
+ * Deep clone of this line
211
+ */
212
+ clone(): Line2D;
213
+ toString(): string;
214
+ equals(other: Line2D): boolean;
215
+ }
@@ -0,0 +1,151 @@
1
+ import { Line3, Vector3 } from "three";
2
+ import { Vec3 } from "./Vec3";
3
+ import { Point3 } from "./Point3";
4
+ import { Line2D } from "./Line2D";
5
+ export declare class Line3D extends Line3 {
6
+ #private;
7
+ start: Vec3;
8
+ end: Vec3;
9
+ constructor(start: Vec3, end: Vec3);
10
+ static fromPoints(start: Point3, end: Point3): Line3D;
11
+ /**
12
+ * Creates a polygon formed by an array of lines from points provided.
13
+ * The polygon will only be closed if either
14
+ * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
15
+ */
16
+ static fromPolygon(polygon: Point3[], forceClosedPolygon?: boolean): Line3D[];
17
+ /**
18
+ * Returns lines that are the result of clipping this line by the @other line.
19
+ * Clips must be parallel to this line.
20
+ * Clones the line, does not modify this.
21
+ * @param other
22
+ * @param parallelTolerance
23
+ */
24
+ clipLine(other: Line3D, parallelTolerance?: number): Line3D[];
25
+ /**
26
+ * Returns lines that are the result of clipping this line by the @clips.
27
+ * Clips must be parallel to this line.
28
+ * Clones the line, does not modify this.
29
+ * @param clips
30
+ * @param distanceTolerance
31
+ * @param parallelTolerance
32
+ */
33
+ clipLines(clips: Line3D[], distanceTolerance?: number, parallelTolerance?: number): Line3D[];
34
+ /**
35
+ * Joins a copy of this line with the @other line.
36
+ * Other must be parallel to this line.
37
+ * Returns null if there is no overlap
38
+ * Clones the line, does not modify this.
39
+ * @param other
40
+ * @param distanceTolerance
41
+ * @param parallelTolerance
42
+ */
43
+ joinLine(other: Line3D, distanceTolerance?: number, parallelTolerance?: number): Line3D;
44
+ /**
45
+ * Joins provided lines into several joined lines.
46
+ * Lines must be parallel for joining.
47
+ * @param lines
48
+ * @param distanceTolerance
49
+ * @param parallelTolerance
50
+ */
51
+ static joinLines(lines: Line3D[], distanceTolerance?: number, parallelTolerance?: number): Line3D[];
52
+ /**
53
+ * Returns true if this line section completely overlaps the @other line section.
54
+ * @param other
55
+ * @param tolerance
56
+ */
57
+ covers(other: Line3D, tolerance?: number): boolean;
58
+ /**
59
+ * Returns true if there is any overlap between this line and the @other line section.
60
+ * @param other
61
+ * @param distanceTolerance
62
+ * @param parallelTolerance
63
+ */
64
+ overlaps(other: Line3D, distanceTolerance?: number, parallelTolerance?: number): boolean;
65
+ /**
66
+ * Returns this line's length.
67
+ */
68
+ get length(): number;
69
+ /**
70
+ * Set the length of this line. Center and direction remain unchanged.
71
+ * @param length
72
+ */
73
+ setLength(length: number): this;
74
+ /**
75
+ * Returns the direction of this line.
76
+ */
77
+ get direction(): Vec3;
78
+ /**
79
+ * Returns the center of this line
80
+ */
81
+ get center(): Vec3;
82
+ /**
83
+ * Set the center of the line to the provided point. Length and direction remain unchanged.
84
+ * @param value
85
+ */
86
+ setCenter(value: Vector3): this;
87
+ /** Returns the start and end points of the line as an array. */
88
+ get endpoints(): Vec3[];
89
+ /**
90
+ * Check that this line section contains provided point.
91
+ * @param p
92
+ * @param tolerance
93
+ */
94
+ containsPoint(p: Vector3, tolerance?: number): boolean;
95
+ /**
96
+ * Distance from this line to provided point.
97
+ * @param p
98
+ * @param clampToLine
99
+ */
100
+ distanceToPoint(p: Vector3, clampToLine?: boolean): number;
101
+ /**
102
+ * Returns a copy of @other line, the direction of @other is reversed if needed.
103
+ * Returns null if lines are not parallel.
104
+ * @param other
105
+ * @param tolerance
106
+ */
107
+ getParallelLineInTheSameDirection(other: Line3D, tolerance?: number): Line3D;
108
+ /**
109
+ * Check if @other is parallel to this line.
110
+ * @param other
111
+ * @param tolerance
112
+ */
113
+ isParallelTo(other: Line3D, tolerance?: number): boolean;
114
+ resize(amount: number): this;
115
+ moveStartPoint(amount: number): Line3D;
116
+ moveEndPoint(amount: number): Line3D;
117
+ /**
118
+ * Returns a new line that is the projection of this line onto @other. Uses `closestPointToPoint` to find the projection.
119
+ * @param other
120
+ * @param clampToLine
121
+ */
122
+ projectOn(other: Line3D, clampToLine: boolean): Line3D;
123
+ /**
124
+ * Divides the Line3D into a number of segments of the given length.
125
+ * @param maxSegmentLength number
126
+ */
127
+ chunk(maxSegmentLength: number): Line3D[];
128
+ /**
129
+ * Note that this works well for moving the endpoints as it's currently used
130
+ * If it were to be made public, it would need to handle the situation where the point to move is in the center of the line which would require a different approach
131
+ */
132
+ private movePointOnThisLine;
133
+ /**
134
+ * Move this line by the given vector.
135
+ * @param p
136
+ */
137
+ translate(p: Vector3): this;
138
+ /**
139
+ * Project the line to 2D space, Y value is dropped
140
+ */
141
+ onPlan(): Line2D;
142
+ /**
143
+ * Equals with tolerance
144
+ */
145
+ equals(other: Line3D, tolerance?: number): boolean;
146
+ /**
147
+ * Deep clone of this line
148
+ */
149
+ clone(): this;
150
+ toString(): string;
151
+ }
@@ -0,0 +1,4 @@
1
+ export interface Point2 {
2
+ x: number;
3
+ y: number;
4
+ }
@@ -0,0 +1,5 @@
1
+ export interface Point3 {
2
+ x: number;
3
+ y: number;
4
+ z: number;
5
+ }
@@ -0,0 +1,8 @@
1
+ import { Vector2 } from "three";
2
+ import { Vec3 } from "./Vec3";
3
+ import { Point2 } from "./Point2";
4
+ export declare class Vec2 extends Vector2 {
5
+ static fromPoint(point: Point2): Vec2;
6
+ roundIfCloseToInteger(max?: number): this;
7
+ in3DSpace(z?: number): Vec3;
8
+ }
@@ -0,0 +1,17 @@
1
+ import { Vector3 } from "three";
2
+ import { Vec2 } from "./Vec2";
3
+ import { Point3 } from "./Point3";
4
+ export declare class Vec3 extends Vector3 {
5
+ #private;
6
+ static fromPoint(point: Point3): Vec3;
7
+ moveTowards(target: Vector3, amount: number): Vec3;
8
+ centerTowards(target: Vector3): Vec3;
9
+ addY(y: number): Vec3;
10
+ addX(x: number): Vec3;
11
+ scale(p: Vector3): Vec3;
12
+ closest(...points: Vector3[]): Vec3;
13
+ toPointWithFlippedYZ(): Vec3;
14
+ onPlan(): Vec2;
15
+ horizontalDistanceTo(point: Vector3): number;
16
+ clone(): this;
17
+ }
@@ -0,0 +1,4 @@
1
+ export { Vec2 } from "./Vec2";
2
+ export { Vec3 } from "./Vec3";
3
+ export { Line2D } from "./Line2D";
4
+ export { Line3D } from "./Line3D";