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