@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/esm/Line3D.js CHANGED
@@ -1,388 +1,388 @@
1
- import { Line3 } from "three";
2
- import { Vec3 } from "./Vec3";
3
- import { Line2D } from "./Line2D";
4
- export class Line3D extends Line3 {
5
- #target;
6
- constructor(start, end) {
7
- super(start, end);
8
- this.#target = new Vec3();
9
- }
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
- }
13
- /**
14
- * Creates a polygon formed by an array of lines from points provided.
15
- * The polygon will only be closed if either
16
- * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
17
- */
18
- static fromPolygon(polygon, forceClosedPolygon = false) {
19
- if (!polygon || polygon.length < 2) {
20
- return [];
21
- }
22
- if (forceClosedPolygon && (polygon[0].x !== polygon.at(-1).x || polygon[0].y !== polygon.at(-1).y || polygon[0].z !== polygon.at(-1).z)) {
23
- polygon = [...polygon, polygon[0]];
24
- }
25
- const lines = [];
26
- for (let i = 0; i < polygon.length - 1; i++) {
27
- lines.push(Line3D.fromPoints(polygon[i], polygon[i + 1]));
28
- }
29
- return lines;
30
- }
31
- /**
32
- * Returns lines that are the result of clipping this line by the @other line.
33
- * Clips must be parallel to this line.
34
- * Clones the line, does not modify this.
35
- * @param other
36
- * @param parallelTolerance
37
- */
38
- clipLine(other, parallelTolerance = Number.EPSILON) {
39
- other = this.getParallelLineInTheSameDirection(other, parallelTolerance);
40
- // 1) Lines aren't parallel
41
- if (!other) {
42
- return [this.clone()];
43
- }
44
- const left = this.clone();
45
- left.end.copy(other.start);
46
- const right = this.clone();
47
- right.start.copy(other.end);
48
- return [left, right].filter(x => x.direction.manhattanDistanceTo(this.direction) <= parallelTolerance);
49
- }
50
- /**
51
- * Returns lines that are the result of clipping this line by the @clips.
52
- * Clips must be parallel to this line.
53
- * Clones the line, does not modify this.
54
- * @param clips
55
- * @param distanceTolerance
56
- * @param parallelTolerance
57
- */
58
- clipLines(clips, distanceTolerance = 0, parallelTolerance = Number.EPSILON) {
59
- const free = [];
60
- const sources = [this.clone()];
61
- while (sources.length > 0) {
62
- let isFree = true;
63
- const tested = sources.pop();
64
- for (const clip of clips) {
65
- if (tested.overlaps(clip, distanceTolerance, parallelTolerance)) {
66
- isFree = false;
67
- const subtracted = tested.clipLine(clip, parallelTolerance);
68
- sources.push(...subtracted);
69
- break;
70
- }
71
- }
72
- if (isFree)
73
- free.push(tested);
74
- }
75
- return free;
76
- }
77
- /**
78
- * Joins a copy of this line with the @other line.
79
- * Other must be parallel to this line.
80
- * Returns null if there is no overlap
81
- * Clones the line, does not modify this.
82
- * @param other
83
- * @param distanceTolerance
84
- * @param parallelTolerance
85
- */
86
- joinLine(other, distanceTolerance = 0, parallelTolerance = Number.EPSILON) {
87
- // 6 possible cases:
88
- const otherParallel = this.getParallelLineInTheSameDirection(other, parallelTolerance);
89
- // 1) Lines aren't parallel
90
- if (!otherParallel) {
91
- return null;
92
- }
93
- const thisContainsOtherStartPoint = this.containsPoint(otherParallel.start, distanceTolerance);
94
- const thisContainsOtherEndPoint = this.containsPoint(otherParallel.end, distanceTolerance);
95
- const otherContainsThisStartPoint = otherParallel.containsPoint(this.start, distanceTolerance);
96
- const otherContainsThisEndPoint = otherParallel.containsPoint(this.end, distanceTolerance);
97
- // 2) Lines don't overlap at all
98
- if (!thisContainsOtherStartPoint &&
99
- !thisContainsOtherEndPoint &&
100
- !otherContainsThisStartPoint &&
101
- !otherContainsThisEndPoint) {
102
- return null;
103
- }
104
- // 3) This line entirely covers the other line
105
- if (thisContainsOtherStartPoint && thisContainsOtherEndPoint) {
106
- return this.clone();
107
- }
108
- // 4) The other line entirely covers this line
109
- if (otherContainsThisStartPoint && otherContainsThisEndPoint) {
110
- return otherParallel.clone();
111
- }
112
- // 5) This line is overlapped by the start of the other line
113
- if (thisContainsOtherStartPoint && !thisContainsOtherEndPoint) {
114
- return new Line3D(this.start, otherParallel.end);
115
- }
116
- // 6) This line is overlapped by the end of the other line
117
- if (thisContainsOtherEndPoint && !thisContainsOtherStartPoint) {
118
- return new Line3D(otherParallel.start, this.end);
119
- }
120
- return null;
121
- }
122
- /**
123
- * Joins provided lines into several joined lines.
124
- * Lines must be parallel for joining.
125
- * @param lines
126
- * @param distanceTolerance
127
- * @param parallelTolerance
128
- */
129
- static joinLines(lines, distanceTolerance = 0, parallelTolerance = Number.EPSILON) {
130
- if (lines.length < 2) {
131
- return lines.map(x => x.clone());
132
- }
133
- const toProcess = lines.slice();
134
- const result = [];
135
- while (toProcess.length > 0) {
136
- const current = toProcess.pop();
137
- let joinedLine;
138
- for (let i = 0; i < result.length; i++) {
139
- const other = result[i];
140
- joinedLine = current.joinLine(other, distanceTolerance, parallelTolerance);
141
- if (joinedLine) {
142
- result.splice(i, 1);
143
- toProcess.push(joinedLine);
144
- break;
145
- }
146
- }
147
- if (!joinedLine) {
148
- result.push(current.clone());
149
- }
150
- }
151
- return result;
152
- }
153
- /**
154
- * Returns true if this line section completely overlaps the @other line section.
155
- * @param other
156
- * @param tolerance
157
- */
158
- covers(other, tolerance = 0) {
159
- return this.containsPoint(other.start, tolerance) && this.containsPoint(other.end, tolerance);
160
- }
161
- /**
162
- * Returns true if there is any overlap between this line and the @other line section.
163
- * @param other
164
- * @param distanceTolerance
165
- * @param parallelTolerance
166
- */
167
- overlaps(other, distanceTolerance = 0, parallelTolerance = Number.EPSILON) {
168
- // Special case
169
- if (this.equals(other, distanceTolerance)) {
170
- return true;
171
- }
172
- // Always have to be parallel
173
- if (this.isParallelTo(other, parallelTolerance)) {
174
- // 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
175
- const otherStartEqualsToAnyOfThisPoint = other.start.distanceTo(this.start) <= distanceTolerance || other.start.distanceTo(this.end) <= distanceTolerance;
176
- if (this.containsPoint(other.start, distanceTolerance) && !otherStartEqualsToAnyOfThisPoint) {
177
- return true;
178
- }
179
- const otherEndEqualsToAnyOfThisPoint = other.end.distanceTo(this.start) <= distanceTolerance || other.end.distanceTo(this.end) <= distanceTolerance;
180
- if (this.containsPoint(other.end, distanceTolerance) && !otherEndEqualsToAnyOfThisPoint) {
181
- return true;
182
- }
183
- const thisStartEqualsToAnyOfOtherPoint = this.start.distanceTo(other.start) <= distanceTolerance || this.start.distanceTo(other.end) <= distanceTolerance;
184
- if (other.containsPoint(this.start, distanceTolerance) && !thisStartEqualsToAnyOfOtherPoint) {
185
- return true;
186
- }
187
- const thisEndEqualsToAnyOfOtherPoint = this.end.distanceTo(other.start) <= distanceTolerance || this.end.distanceTo(other.end) <= distanceTolerance;
188
- if (other.containsPoint(this.end, distanceTolerance) && !thisEndEqualsToAnyOfOtherPoint) {
189
- return true;
190
- }
191
- }
192
- return false;
193
- }
194
- /**
195
- * Returns this line's length.
196
- */
197
- get length() {
198
- return this.start.distanceTo(this.end);
199
- }
200
- /**
201
- * Set the length of this line. Center and direction remain unchanged.
202
- * @param length
203
- */
204
- setLength(length) {
205
- const diff = length - this.length;
206
- return this.resize(diff);
207
- }
208
- /**
209
- * Returns the direction of this line.
210
- */
211
- get direction() {
212
- return this.end.clone().sub(this.start).normalize();
213
- }
214
- /**
215
- * Returns the center of this line
216
- */
217
- get center() {
218
- return this.getCenter(new Vec3());
219
- }
220
- /**
221
- * Set the center of the line to the provided point. Length and direction remain unchanged.
222
- * @param value
223
- */
224
- setCenter(value) {
225
- const current = this.center;
226
- const diffX = current.x - value.x;
227
- const diffY = current.y - value.y;
228
- const diffZ = current.z - value.z;
229
- this.start.x -= diffX;
230
- this.start.y -= diffY;
231
- this.start.z -= diffZ;
232
- this.end.x -= diffX;
233
- this.end.y -= diffY;
234
- this.end.z -= diffZ;
235
- return this;
236
- }
237
- /** Returns the start and end points of the line as an array. */
238
- get endpoints() {
239
- return [this.start, this.end];
240
- }
241
- /**
242
- * Check that this line section contains provided point.
243
- * @param p
244
- * @param tolerance
245
- */
246
- containsPoint(p, tolerance = 0) {
247
- const closestPointToPoint = this.closestPointToPoint(p, true, this.#target);
248
- return closestPointToPoint.distanceTo(p) <= tolerance;
249
- }
250
- /**
251
- * Distance from this line to provided point.
252
- * @param p
253
- * @param clampToLine
254
- */
255
- distanceToPoint(p, clampToLine = true) {
256
- const closestPointToPoint = this.closestPointToPoint(p, clampToLine, this.#target);
257
- return closestPointToPoint.distanceTo(p);
258
- }
259
- /**
260
- * Returns a copy of @other line, the direction of @other is reversed if needed.
261
- * Returns null if lines are not parallel.
262
- * @param other
263
- * @param tolerance
264
- */
265
- getParallelLineInTheSameDirection(other, tolerance = Number.EPSILON) {
266
- const direction = this.direction;
267
- const areTheSameDirection = direction.manhattanDistanceTo(other.direction) < tolerance;
268
- if (areTheSameDirection) {
269
- return other.clone();
270
- }
271
- const otherLineOppositeDirection = new Line3D(other.end, other.start);
272
- if (otherLineOppositeDirection.direction.manhattanDistanceTo(direction) < tolerance) {
273
- return otherLineOppositeDirection;
274
- }
275
- return null;
276
- }
277
- /**
278
- * Check if @other is parallel to this line.
279
- * @param other
280
- * @param tolerance
281
- */
282
- isParallelTo(other, tolerance = Number.EPSILON) {
283
- const direction = this.direction;
284
- const otherDirection = other.direction;
285
- const areTheSameDirection = direction.manhattanDistanceTo(otherDirection) <= tolerance;
286
- if (areTheSameDirection) {
287
- return true;
288
- }
289
- return direction.negate().manhattanDistanceTo(otherDirection) < tolerance;
290
- }
291
- /*
292
- * Extends or reduces the line to the given length while keeping the center of the line constant.
293
- */
294
- resize(amount) {
295
- this.moveStartPoint(amount / 2);
296
- this.moveEndPoint(amount / 2);
297
- return this;
298
- }
299
- /*
300
- * Moves start on the line by the given amount. Plus values move the point further away from the center.
301
- */
302
- moveStartPoint(amount) {
303
- const start = this.movePointOnThisLine(this.start, amount);
304
- this.start.x = start.x;
305
- this.start.y = start.y;
306
- this.start.z = start.z;
307
- return this;
308
- }
309
- /*
310
- * Moves end on the line by the given amount in the current direction. Plus values move the point further away from the center.
311
- */
312
- moveEndPoint(amount) {
313
- const end = this.movePointOnThisLine(this.end, amount);
314
- this.end.x = end.x;
315
- this.end.y = end.y;
316
- this.end.z = end.z;
317
- return this;
318
- }
319
- /**
320
- * Returns a new line that is the projection of this line onto @other. Uses `closestPointToPoint` to find the projection.
321
- * @param other
322
- * @param clampToLine
323
- */
324
- projectOn(other, clampToLine) {
325
- const p1 = other.closestPointToPoint(this.start, clampToLine, new Vec3());
326
- const p2 = other.closestPointToPoint(this.end, clampToLine, new Vec3());
327
- return p1.distanceTo(this.start) < p2.distanceTo(this.start) ? new Line3D(p1, p2) : new Line3D(p2, p1);
328
- }
329
- /**
330
- * Divides the Line3D into a number of segments of the given length.
331
- * @param maxSegmentLength number
332
- */
333
- chunk(maxSegmentLength) {
334
- const source = this.clone();
335
- const result = [];
336
- while (source.length > maxSegmentLength) {
337
- const chunk = source.clone();
338
- chunk.moveEndPoint(-(chunk.length - maxSegmentLength));
339
- result.push(chunk);
340
- source.start.copy(chunk.end);
341
- }
342
- if (source.length > 0) {
343
- result.push(source);
344
- }
345
- return result;
346
- }
347
- /**
348
- * Note that this works well for moving the endpoints as it's currently used
349
- * 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
350
- */
351
- movePointOnThisLine(point, amount) {
352
- const center = this.getCenter(this.#target);
353
- const vec = new Vec3(center.x - point.x, center.y - point.y, center.z - point.z);
354
- const length = vec.length();
355
- vec.normalize().multiplyScalar(length + amount);
356
- return new Vec3(center.x - vec.x, center.y - vec.y, center.z - vec.z);
357
- }
358
- /**
359
- * Move this line by the given vector.
360
- * @param p
361
- */
362
- translate(p) {
363
- this.start.add(p);
364
- this.end.add(p);
365
- return this;
366
- }
367
- /**
368
- * Project the line to 2D space, Y value is dropped
369
- */
370
- onPlan() {
371
- return new Line2D(this.start.onPlan(), this.end.onPlan());
372
- }
373
- /**
374
- * Equals with tolerance
375
- */
376
- equals(other, tolerance = 0) {
377
- return !!other && this.start.distanceTo(other.start) <= tolerance && this.end.distanceTo(other.end) <= tolerance;
378
- }
379
- /**
380
- * Deep clone of this line
381
- */
382
- clone() {
383
- return new Line3D(this.start.clone(), this.end.clone());
384
- }
385
- toString() {
386
- return `Line3D { start: ${this.start.x}, ${this.start.y}, ${this.start.z}, end: ${this.end.x}, ${this.end.y}, ${this.end.z}}`;
387
- }
388
- }
1
+ import { Line3 } from "three";
2
+ import { Vec3 } from "./Vec3";
3
+ import { Line2D } from "./Line2D";
4
+ export class Line3D extends Line3 {
5
+ #target;
6
+ constructor(start, end) {
7
+ super(start, end);
8
+ this.#target = new Vec3();
9
+ }
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
+ }
13
+ /**
14
+ * Creates a polygon formed by an array of lines from points provided.
15
+ * The polygon will only be closed if either
16
+ * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
17
+ */
18
+ static fromPolygon(polygon, forceClosedPolygon = false) {
19
+ if (!polygon || polygon.length < 2) {
20
+ return [];
21
+ }
22
+ if (forceClosedPolygon && (polygon[0].x !== polygon.at(-1).x || polygon[0].y !== polygon.at(-1).y || polygon[0].z !== polygon.at(-1).z)) {
23
+ polygon = [...polygon, polygon[0]];
24
+ }
25
+ const lines = [];
26
+ for (let i = 0; i < polygon.length - 1; i++) {
27
+ lines.push(Line3D.fromPoints(polygon[i], polygon[i + 1]));
28
+ }
29
+ return lines;
30
+ }
31
+ /**
32
+ * Returns lines that are the result of clipping this line by the @other line.
33
+ * Clips must be parallel to this line.
34
+ * Clones the line, does not modify this.
35
+ * @param other
36
+ * @param parallelTolerance
37
+ */
38
+ clipLine(other, parallelTolerance = Number.EPSILON) {
39
+ other = this.getParallelLineInTheSameDirection(other, parallelTolerance);
40
+ // 1) Lines aren't parallel
41
+ if (!other) {
42
+ return [this.clone()];
43
+ }
44
+ const left = this.clone();
45
+ left.end.copy(other.start);
46
+ const right = this.clone();
47
+ right.start.copy(other.end);
48
+ return [left, right].filter(x => x.direction.manhattanDistanceTo(this.direction) <= parallelTolerance);
49
+ }
50
+ /**
51
+ * Returns lines that are the result of clipping this line by the @clips.
52
+ * Clips must be parallel to this line.
53
+ * Clones the line, does not modify this.
54
+ * @param clips
55
+ * @param distanceTolerance
56
+ * @param parallelTolerance
57
+ */
58
+ clipLines(clips, distanceTolerance = 0, parallelTolerance = Number.EPSILON) {
59
+ const free = [];
60
+ const sources = [this.clone()];
61
+ while (sources.length > 0) {
62
+ let isFree = true;
63
+ const tested = sources.pop();
64
+ for (const clip of clips) {
65
+ if (tested.overlaps(clip, distanceTolerance, parallelTolerance)) {
66
+ isFree = false;
67
+ const subtracted = tested.clipLine(clip, parallelTolerance);
68
+ sources.push(...subtracted);
69
+ break;
70
+ }
71
+ }
72
+ if (isFree)
73
+ free.push(tested);
74
+ }
75
+ return free;
76
+ }
77
+ /**
78
+ * Joins a copy of this line with the @other line.
79
+ * Other must be parallel to this line.
80
+ * Returns null if there is no overlap
81
+ * Clones the line, does not modify this.
82
+ * @param other
83
+ * @param distanceTolerance
84
+ * @param parallelTolerance
85
+ */
86
+ joinLine(other, distanceTolerance = 0, parallelTolerance = Number.EPSILON) {
87
+ // 6 possible cases:
88
+ const otherParallel = this.getParallelLineInTheSameDirection(other, parallelTolerance);
89
+ // 1) Lines aren't parallel
90
+ if (!otherParallel) {
91
+ return null;
92
+ }
93
+ const thisContainsOtherStartPoint = this.containsPoint(otherParallel.start, distanceTolerance);
94
+ const thisContainsOtherEndPoint = this.containsPoint(otherParallel.end, distanceTolerance);
95
+ const otherContainsThisStartPoint = otherParallel.containsPoint(this.start, distanceTolerance);
96
+ const otherContainsThisEndPoint = otherParallel.containsPoint(this.end, distanceTolerance);
97
+ // 2) Lines don't overlap at all
98
+ if (!thisContainsOtherStartPoint &&
99
+ !thisContainsOtherEndPoint &&
100
+ !otherContainsThisStartPoint &&
101
+ !otherContainsThisEndPoint) {
102
+ return null;
103
+ }
104
+ // 3) This line entirely covers the other line
105
+ if (thisContainsOtherStartPoint && thisContainsOtherEndPoint) {
106
+ return this.clone();
107
+ }
108
+ // 4) The other line entirely covers this line
109
+ if (otherContainsThisStartPoint && otherContainsThisEndPoint) {
110
+ return otherParallel.clone();
111
+ }
112
+ // 5) This line is overlapped by the start of the other line
113
+ if (thisContainsOtherStartPoint && !thisContainsOtherEndPoint) {
114
+ return new Line3D(this.start, otherParallel.end);
115
+ }
116
+ // 6) This line is overlapped by the end of the other line
117
+ if (thisContainsOtherEndPoint && !thisContainsOtherStartPoint) {
118
+ return new Line3D(otherParallel.start, this.end);
119
+ }
120
+ return null;
121
+ }
122
+ /**
123
+ * Joins provided lines into several joined lines.
124
+ * Lines must be parallel for joining.
125
+ * @param lines
126
+ * @param distanceTolerance
127
+ * @param parallelTolerance
128
+ */
129
+ static joinLines(lines, distanceTolerance = 0, parallelTolerance = Number.EPSILON) {
130
+ if (lines.length < 2) {
131
+ return lines.map(x => x.clone());
132
+ }
133
+ const toProcess = lines.slice();
134
+ const result = [];
135
+ while (toProcess.length > 0) {
136
+ const current = toProcess.pop();
137
+ let joinedLine;
138
+ for (let i = 0; i < result.length; i++) {
139
+ const other = result[i];
140
+ joinedLine = current.joinLine(other, distanceTolerance, parallelTolerance);
141
+ if (joinedLine) {
142
+ result.splice(i, 1);
143
+ toProcess.push(joinedLine);
144
+ break;
145
+ }
146
+ }
147
+ if (!joinedLine) {
148
+ result.push(current.clone());
149
+ }
150
+ }
151
+ return result;
152
+ }
153
+ /**
154
+ * Returns true if this line section completely overlaps the @other line section.
155
+ * @param other
156
+ * @param tolerance
157
+ */
158
+ covers(other, tolerance = 0) {
159
+ return this.containsPoint(other.start, tolerance) && this.containsPoint(other.end, tolerance);
160
+ }
161
+ /**
162
+ * Returns true if there is any overlap between this line and the @other line section.
163
+ * @param other
164
+ * @param distanceTolerance
165
+ * @param parallelTolerance
166
+ */
167
+ overlaps(other, distanceTolerance = 0, parallelTolerance = Number.EPSILON) {
168
+ // Special case
169
+ if (this.equals(other, distanceTolerance)) {
170
+ return true;
171
+ }
172
+ // Always have to be parallel
173
+ if (this.isParallelTo(other, parallelTolerance)) {
174
+ // 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
175
+ const otherStartEqualsToAnyOfThisPoint = other.start.distanceTo(this.start) <= distanceTolerance || other.start.distanceTo(this.end) <= distanceTolerance;
176
+ if (this.containsPoint(other.start, distanceTolerance) && !otherStartEqualsToAnyOfThisPoint) {
177
+ return true;
178
+ }
179
+ const otherEndEqualsToAnyOfThisPoint = other.end.distanceTo(this.start) <= distanceTolerance || other.end.distanceTo(this.end) <= distanceTolerance;
180
+ if (this.containsPoint(other.end, distanceTolerance) && !otherEndEqualsToAnyOfThisPoint) {
181
+ return true;
182
+ }
183
+ const thisStartEqualsToAnyOfOtherPoint = this.start.distanceTo(other.start) <= distanceTolerance || this.start.distanceTo(other.end) <= distanceTolerance;
184
+ if (other.containsPoint(this.start, distanceTolerance) && !thisStartEqualsToAnyOfOtherPoint) {
185
+ return true;
186
+ }
187
+ const thisEndEqualsToAnyOfOtherPoint = this.end.distanceTo(other.start) <= distanceTolerance || this.end.distanceTo(other.end) <= distanceTolerance;
188
+ if (other.containsPoint(this.end, distanceTolerance) && !thisEndEqualsToAnyOfOtherPoint) {
189
+ return true;
190
+ }
191
+ }
192
+ return false;
193
+ }
194
+ /**
195
+ * Returns this line's length.
196
+ */
197
+ get length() {
198
+ return this.start.distanceTo(this.end);
199
+ }
200
+ /**
201
+ * Set the length of this line. Center and direction remain unchanged.
202
+ * @param length
203
+ */
204
+ setLength(length) {
205
+ const diff = length - this.length;
206
+ return this.resize(diff);
207
+ }
208
+ /**
209
+ * Returns the direction of this line.
210
+ */
211
+ get direction() {
212
+ return this.end.clone().sub(this.start).normalize();
213
+ }
214
+ /**
215
+ * Returns the center of this line
216
+ */
217
+ get center() {
218
+ return this.getCenter(new Vec3());
219
+ }
220
+ /**
221
+ * Set the center of the line to the provided point. Length and direction remain unchanged.
222
+ * @param value
223
+ */
224
+ setCenter(value) {
225
+ const current = this.center;
226
+ const diffX = current.x - value.x;
227
+ const diffY = current.y - value.y;
228
+ const diffZ = current.z - value.z;
229
+ this.start.x -= diffX;
230
+ this.start.y -= diffY;
231
+ this.start.z -= diffZ;
232
+ this.end.x -= diffX;
233
+ this.end.y -= diffY;
234
+ this.end.z -= diffZ;
235
+ return this;
236
+ }
237
+ /** Returns the start and end points of the line as an array. */
238
+ get endpoints() {
239
+ return [this.start, this.end];
240
+ }
241
+ /**
242
+ * Check that this line section contains provided point.
243
+ * @param p
244
+ * @param tolerance
245
+ */
246
+ containsPoint(p, tolerance = 0) {
247
+ const closestPointToPoint = this.closestPointToPoint(p, true, this.#target);
248
+ return closestPointToPoint.distanceTo(p) <= tolerance;
249
+ }
250
+ /**
251
+ * Distance from this line to provided point.
252
+ * @param p
253
+ * @param clampToLine
254
+ */
255
+ distanceToPoint(p, clampToLine = true) {
256
+ const closestPointToPoint = this.closestPointToPoint(p, clampToLine, this.#target);
257
+ return closestPointToPoint.distanceTo(p);
258
+ }
259
+ /**
260
+ * Returns a copy of @other line, the direction of @other is reversed if needed.
261
+ * Returns null if lines are not parallel.
262
+ * @param other
263
+ * @param tolerance
264
+ */
265
+ getParallelLineInTheSameDirection(other, tolerance = Number.EPSILON) {
266
+ const direction = this.direction;
267
+ const areTheSameDirection = direction.manhattanDistanceTo(other.direction) < tolerance;
268
+ if (areTheSameDirection) {
269
+ return other.clone();
270
+ }
271
+ const otherLineOppositeDirection = new Line3D(other.end, other.start);
272
+ if (otherLineOppositeDirection.direction.manhattanDistanceTo(direction) < tolerance) {
273
+ return otherLineOppositeDirection;
274
+ }
275
+ return null;
276
+ }
277
+ /**
278
+ * Check if @other is parallel to this line.
279
+ * @param other
280
+ * @param tolerance
281
+ */
282
+ isParallelTo(other, tolerance = Number.EPSILON) {
283
+ const direction = this.direction;
284
+ const otherDirection = other.direction;
285
+ const areTheSameDirection = direction.manhattanDistanceTo(otherDirection) <= tolerance;
286
+ if (areTheSameDirection) {
287
+ return true;
288
+ }
289
+ return direction.negate().manhattanDistanceTo(otherDirection) < tolerance;
290
+ }
291
+ /*
292
+ * Extends or reduces the line to the given length while keeping the center of the line constant.
293
+ */
294
+ resize(amount) {
295
+ this.moveStartPoint(amount / 2);
296
+ this.moveEndPoint(amount / 2);
297
+ return this;
298
+ }
299
+ /*
300
+ * Moves start on the line by the given amount. Plus values move the point further away from the center.
301
+ */
302
+ moveStartPoint(amount) {
303
+ const start = this.movePointOnThisLine(this.start, amount);
304
+ this.start.x = start.x;
305
+ this.start.y = start.y;
306
+ this.start.z = start.z;
307
+ return this;
308
+ }
309
+ /*
310
+ * Moves end on the line by the given amount in the current direction. Plus values move the point further away from the center.
311
+ */
312
+ moveEndPoint(amount) {
313
+ const end = this.movePointOnThisLine(this.end, amount);
314
+ this.end.x = end.x;
315
+ this.end.y = end.y;
316
+ this.end.z = end.z;
317
+ return this;
318
+ }
319
+ /**
320
+ * Returns a new line that is the projection of this line onto @other. Uses `closestPointToPoint` to find the projection.
321
+ * @param other
322
+ * @param clampToLine
323
+ */
324
+ projectOn(other, clampToLine) {
325
+ const p1 = other.closestPointToPoint(this.start, clampToLine, new Vec3());
326
+ const p2 = other.closestPointToPoint(this.end, clampToLine, new Vec3());
327
+ return p1.distanceTo(this.start) < p2.distanceTo(this.start) ? new Line3D(p1, p2) : new Line3D(p2, p1);
328
+ }
329
+ /**
330
+ * Divides the Line3D into a number of segments of the given length.
331
+ * @param maxSegmentLength number
332
+ */
333
+ chunk(maxSegmentLength) {
334
+ const source = this.clone();
335
+ const result = [];
336
+ while (source.length > maxSegmentLength) {
337
+ const chunk = source.clone();
338
+ chunk.moveEndPoint(-(chunk.length - maxSegmentLength));
339
+ result.push(chunk);
340
+ source.start.copy(chunk.end);
341
+ }
342
+ if (source.length > 0) {
343
+ result.push(source);
344
+ }
345
+ return result;
346
+ }
347
+ /**
348
+ * Note that this works well for moving the endpoints as it's currently used
349
+ * 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
350
+ */
351
+ movePointOnThisLine(point, amount) {
352
+ const center = this.getCenter(this.#target);
353
+ const vec = new Vec3(center.x - point.x, center.y - point.y, center.z - point.z);
354
+ const length = vec.length();
355
+ vec.normalize().multiplyScalar(length + amount);
356
+ return new Vec3(center.x - vec.x, center.y - vec.y, center.z - vec.z);
357
+ }
358
+ /**
359
+ * Move this line by the given vector.
360
+ * @param p
361
+ */
362
+ translate(p) {
363
+ this.start.add(p);
364
+ this.end.add(p);
365
+ return this;
366
+ }
367
+ /**
368
+ * Project the line to 2D space, Y value is dropped
369
+ */
370
+ onPlan() {
371
+ return new Line2D(this.start.onPlan(), this.end.onPlan());
372
+ }
373
+ /**
374
+ * Equals with tolerance
375
+ */
376
+ equals(other, tolerance = 0) {
377
+ return !!other && this.start.distanceTo(other.start) <= tolerance && this.end.distanceTo(other.end) <= tolerance;
378
+ }
379
+ /**
380
+ * Deep clone of this line
381
+ */
382
+ clone() {
383
+ return new Line3D(this.start.clone(), this.end.clone());
384
+ }
385
+ toString() {
386
+ return `Line3D { start: ${this.start.x}, ${this.start.y}, ${this.start.z}, end: ${this.end.x}, ${this.end.y}, ${this.end.z}}`;
387
+ }
388
+ }