@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/cjs/Line2D.js CHANGED
@@ -1,566 +1,566 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Line2D = void 0;
4
- const three_1 = require("three");
5
- const Vec2_1 = require("./Vec2");
6
- class Line2D {
7
- start;
8
- end;
9
- index;
10
- constructor(start, end, index = 0) {
11
- this.start = start;
12
- this.end = end;
13
- this.index = index;
14
- }
15
- static fromCoordinates(x1, y1, x2, y2, index = 0) {
16
- return new Line2D(new Vec2_1.Vec2(x1, y1), new Vec2_1.Vec2(x2, y2), index);
17
- }
18
- static fromPoints(p1, p2, index = 0) {
19
- return new Line2D(new Vec2_1.Vec2(p1.x, p1.y), new Vec2_1.Vec2(p2.x, p2.y), index);
20
- }
21
- /**
22
- * Creates a polygon formed by an array of lines from points provided.
23
- * The polygon will only be closed if either
24
- * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
25
- */
26
- static fromPolygon(polygon, forceClosedPolygon = false) {
27
- if (!polygon || polygon.length < 2) {
28
- return [];
29
- }
30
- if (forceClosedPolygon && (polygon[0].x !== polygon.at(-1).x || polygon[0].y !== polygon.at(-1).y)) {
31
- polygon = [...polygon, polygon[0]];
32
- }
33
- const lines = [];
34
- for (let i = 0; i < polygon.length - 1; i++) {
35
- lines.push(Line2D.fromPoints(polygon[i], polygon[i + 1], i));
36
- }
37
- return lines;
38
- }
39
- static fromLength(length) {
40
- return Line2D.fromCoordinates(-length / 2, 0, length / 2, 0);
41
- }
42
- get center() {
43
- return new Vec2_1.Vec2((this.start.x + this.end.x) / 2, (this.start.y + this.end.y) / 2);
44
- }
45
- /**
46
- * Set the center of the line to the provided point. Length and direction remain unchanged.
47
- * Modifies this line.
48
- * @param value
49
- */
50
- set center(value) {
51
- const current = this.center;
52
- const diffX = current.x - value.x;
53
- const diffY = current.y - value.y;
54
- this.start.x -= diffX;
55
- this.start.y -= diffY;
56
- this.end.x -= diffX;
57
- this.end.y -= diffY;
58
- }
59
- /**
60
- * Set the center of the line to the provided point. Length and direction remain unchanged.
61
- * Modifies this line.
62
- * @param value
63
- */
64
- setCenter(value) {
65
- this.center = value;
66
- return this;
67
- }
68
- /*
69
- * Extends or reduces the line by the given length while keeping the center of the line constant.
70
- * Modifies this line.
71
- */
72
- resize(amount) {
73
- this.moveStartPoint(amount / 2);
74
- this.moveEndPoint(amount / 2);
75
- return this;
76
- }
77
- /*
78
- * Moves start point on the line by the given amount. Plus values move the point further away from the center.
79
- * Modifies this line.
80
- */
81
- moveStartPoint(amount) {
82
- const p1 = this.movePointOnThisLine(this.start, amount);
83
- this.start.copy(p1);
84
- return this;
85
- }
86
- /**
87
- * Moves end point on the line by the given amount. Plus values move the point further away from the center.
88
- * Modifies this line.
89
- */
90
- moveEndPoint(amount) {
91
- const p2 = this.movePointOnThisLine(this.end, amount);
92
- this.end.copy(p2);
93
- return this;
94
- }
95
- movePointOnThisLine(point, amount) {
96
- const vec = new three_1.Vector2(this.center.x - point.x, this.center.y - point.y);
97
- const length = vec.length();
98
- vec.normalize().multiplyScalar(length + amount);
99
- return new Vec2_1.Vec2(this.center.x - vec.x, this.center.y - vec.y);
100
- }
101
- /**
102
- * Set the length of this line. Center and direction remain unchanged.
103
- * Modifies this line.
104
- * @param l
105
- */
106
- set length(l) {
107
- const length = this.length;
108
- this.resize(l - length);
109
- }
110
- get length() {
111
- return this.start.distanceTo(this.end);
112
- }
113
- /**
114
- * Set the length of this line. Center and direction remain unchanged.
115
- * @param length
116
- */
117
- setLength(length) {
118
- this.length = length;
119
- return this;
120
- }
121
- /**
122
- * Returns the start and end points of the line as an array.
123
- * Endpoints are not cloned.
124
- */
125
- get endpoints() {
126
- return [this.start, this.end];
127
- }
128
- /**
129
- * Returns the direction of this line.
130
- */
131
- get direction() {
132
- return this.end.clone().sub(this.start).normalize();
133
- }
134
- /**
135
- * Inverts the direction of the line.
136
- * Modifies this line.
137
- */
138
- flip() {
139
- const temp = this.start.clone();
140
- this.start.copy(this.end);
141
- this.end.copy(temp);
142
- return this;
143
- }
144
- /**
145
- * Rotates the line around the center by the given angle in radians.
146
- * Modifies this line.
147
- * @param radians Positive values rotate counter-clockwise.
148
- * @param center
149
- */
150
- rotate(radians, center = this.center) {
151
- this.start.rotateAround(center, radians);
152
- this.end.rotateAround(center, radians);
153
- return this;
154
- }
155
- /**
156
- * Move the line by the given vector.
157
- * Modifies this line.
158
- */
159
- translate(value) {
160
- this.start.x += value.x;
161
- this.start.y += value.y;
162
- this.end.x += value.x;
163
- this.end.y += value.y;
164
- return this;
165
- }
166
- /**
167
- * Move the line to its left by the given amount.
168
- * Modifies this line.
169
- */
170
- translateLeft(amount) {
171
- const translation = this.direction.rotateAround(new Vec2_1.Vec2(), -Math.PI / 2).multiplyScalar(amount);
172
- return this.translate(translation);
173
- }
174
- /**
175
- * Move the line to its right by the given amount.
176
- * Modifies this line.
177
- */
178
- translateRight(amount) {
179
- const translation = this.direction.rotateAround(new Vec2_1.Vec2(), Math.PI / 2).multiplyScalar(amount);
180
- return this.translate(translation);
181
- }
182
- /**
183
- * Returns true when the point is actually inside the (finite) line segment.
184
- * https://jsfiddle.net/c06zdxtL/2/
185
- * https://stackoverflow.com/questions/6865832/detecting-if-a-point-is-of-a-line-segment/6877674
186
- * @param point: Point2
187
- */
188
- isPointOnLineSection(point) {
189
- if (!this.isPointOnInfiniteLine(point)) {
190
- return false;
191
- }
192
- return this.isPointBesideLineSection(point);
193
- }
194
- /**
195
- * Returns true when the point is beside the line **segment** and within the maxDistance.
196
- * @param point
197
- * @param maxDistance
198
- */
199
- isPointCloseToAndBesideLineSection(point, maxDistance) {
200
- const distance = this.distanceToPointOnInfiniteLine(point);
201
- return distance <= maxDistance && this.isPointBesideLineSection(point);
202
- }
203
- /**
204
- * Returns true when the point is beside the line **segment**
205
- * @param point
206
- */
207
- isPointBesideLineSection(point) {
208
- const l2 = (((this.end.x - this.start.x) * (this.end.x - this.start.x)) + ((this.end.y - this.start.y) * (this.end.y - this.start.y)));
209
- if (l2 == 0)
210
- return false;
211
- const r = (((point.x - this.start.x) * (this.end.x - this.start.x)) + ((point.y - this.start.y) * (this.end.y - this.start.y))) / l2;
212
- return (0 <= r) && (r <= 1);
213
- }
214
- /**
215
- * Returns true when the point is on the **infinite** line.
216
- * @param point
217
- */
218
- isPointOnInfiniteLine(point) {
219
- return (point.y - this.start.y) * (this.end.x - this.start.x) === (this.end.y - this.start.y) * (point.x - this.start.x);
220
- }
221
- /**
222
- * Returns true if other line is collinear and overlaps or at least touching this line.
223
- * @param other
224
- */
225
- isCollinearWithTouchOrOverlap(other) {
226
- if (!this.isPointOnInfiniteLine(other.start) || !this.isPointOnInfiniteLine(other.end)) {
227
- return false;
228
- }
229
- return this.isPointOnLineSection(other.start) || this.isPointOnLineSection(other.end) ||
230
- other.isPointOnLineSection(this.start) || other.isPointOnLineSection(this.end);
231
- }
232
- /**
233
- * Returns true if there is any overlap between this line and the @other line section.
234
- */
235
- overlaps(other) {
236
- if (!this.isCollinearWithTouchOrOverlap(other)) {
237
- return false;
238
- }
239
- if (this.start.equals(other.start) && this.end.equals(other.end)) {
240
- return true;
241
- }
242
- return !this.start.equals(other.end) && !this.end.equals(other.start);
243
- }
244
- /**
245
- * Logical AND of this and the other line section.
246
- * @param other
247
- */
248
- getOverlap(other) {
249
- if (!this.overlaps(other)) {
250
- return null;
251
- }
252
- if (this.equals(other)) {
253
- return this.clone();
254
- }
255
- const points = [
256
- [this.start, this.end].filter(thisPoint => other.isPointOnLineSection(thisPoint)),
257
- [other.start, other.end].filter(otherPoint => this.isPointOnLineSection(otherPoint))
258
- ].flat();
259
- if (points.length !== 2) {
260
- return null;
261
- }
262
- const overlap = Line2D.fromPoints(points[0], points[1]);
263
- if (overlap.direction.manhattanDistanceTo(this.direction) > Number.EPSILON) {
264
- overlap.flip();
265
- }
266
- return overlap;
267
- }
268
- /**
269
- * Joins a copy of @line with the @other line.
270
- * Other must be parallel to this line.
271
- * Returns null if there is no overlap
272
- * Clones the line, does not modify.
273
- * @param line
274
- * @param other
275
- */
276
- static joinLine(line, other) {
277
- if (!line.isCollinearWithTouchOrOverlap(other)) {
278
- return null;
279
- }
280
- const p1 = !line.isPointOnLineSection(other.start) ? other.start : line.start;
281
- const p2 = !line.isPointOnLineSection(other.end) ? other.end : line.end;
282
- return new Line2D(p1.clone(), p2.clone(), line.index);
283
- }
284
- /**
285
- * Joins provided lines into several joined lines.
286
- * Lines must be parallel for joining.
287
- * Clone the lines, does not modify.
288
- * @param lines
289
- */
290
- static joinLines(lines) {
291
- if (lines.length < 2) {
292
- return lines.map(x => x.clone());
293
- }
294
- const toProcess = lines.slice();
295
- const result = [];
296
- while (toProcess.length > 0) {
297
- const current = toProcess.pop();
298
- let joined = false;
299
- for (let i = 0; i < result.length; i++) {
300
- const other = result[i];
301
- const joinedLine = Line2D.joinLine(current, other);
302
- if (joinedLine) {
303
- result[i] = joinedLine;
304
- joined = true;
305
- break;
306
- }
307
- }
308
- if (!joined) {
309
- result.push(current.clone());
310
- }
311
- }
312
- return result;
313
- }
314
- /**
315
- * Divides the Line3D into a number of segments of the given length.
316
- * Clone the line, does not modify.
317
- * @param maxSegmentLength number
318
- */
319
- chunk(maxSegmentLength) {
320
- const source = this.clone();
321
- const result = [];
322
- while (source.length > maxSegmentLength) {
323
- const chunk = source.clone();
324
- chunk.moveEndPoint(-(chunk.length - maxSegmentLength));
325
- result.push(chunk);
326
- source.start = chunk.end.clone();
327
- }
328
- result.push(source);
329
- return result;
330
- }
331
- /**
332
- * Returns the closest point parameter on the **infinite** line to the given point.
333
- * @param point
334
- */
335
- closestPointToPointParameterOnInfiniteLine(point) {
336
- const startP = new Vec2_1.Vec2().subVectors(point, this.start);
337
- const startEnd = new Vec2_1.Vec2().subVectors(this.end, this.start);
338
- const startEnd2 = startEnd.dot(startEnd);
339
- const startEnd_startP = startEnd.dot(startP);
340
- return startEnd_startP / startEnd2;
341
- }
342
- /**
343
- * Returns the closest point on the **infinite** line to the given point.
344
- * @param point
345
- */
346
- closestPointOnInfiniteLine(point) {
347
- const t = this.closestPointToPointParameterOnInfiniteLine(point);
348
- return new Vec2_1.Vec2().subVectors(this.end, this.start).multiplyScalar(t).add(this.start);
349
- }
350
- /**
351
- * Returns the closest point on the line **section** to the given point.
352
- * @param point
353
- */
354
- closestPointOnLine(point) {
355
- const closestPoint = this.closestPointOnInfiniteLine(point);
356
- if (this.isPointOnLineSection(closestPoint)) {
357
- return closestPoint;
358
- }
359
- return closestPoint.distanceTo(this.start) < closestPoint.distanceTo(this.end) ? this.start : this.end;
360
- }
361
- /**
362
- * Returns the distance between the **infinite** line and the point.
363
- * @param point
364
- */
365
- distanceToPointOnInfiniteLine(point) {
366
- const l2 = (((this.end.x - this.start.x) * (this.end.x - this.start.x)) + ((this.end.y - this.start.y) * (this.end.y - this.start.y)));
367
- if (l2 == 0)
368
- return Infinity;
369
- const s = (((this.start.y - point.y) * (this.end.x - this.start.x)) - ((this.start.x - point.x) * (this.end.y - this.start.y))) / l2;
370
- return Math.abs(s) * Math.sqrt(l2);
371
- }
372
- /**
373
- * Returns lines that are the result of clipping @source line by the @clips.
374
- * Clips must be parallel to this line.
375
- * Clones the line, does not modify this.
376
- * @param source
377
- * @param clips
378
- */
379
- static clipLines(source, clips) {
380
- if (!clips || clips.length === 0)
381
- return [source];
382
- clips = clips.map(c => {
383
- const copy = c.clone();
384
- if (copy.direction.manhattanDistanceTo(source.direction) > Number.EPSILON) {
385
- copy.flip();
386
- }
387
- return copy;
388
- });
389
- const free = [];
390
- const sources = [source];
391
- while (sources.length > 0) {
392
- let isFree = true;
393
- const tested = sources.pop();
394
- for (const cover of clips) {
395
- if (tested.overlaps(cover)) {
396
- isFree = false;
397
- const subtracted = this.subtractSingle(tested, cover);
398
- sources.push(...subtracted);
399
- break;
400
- }
401
- }
402
- if (isFree)
403
- free.push(tested);
404
- }
405
- return this.order(source, free);
406
- }
407
- /**
408
- * Returns the original line section split into two parts, if the line **sections** overlap, otherwise null
409
- */
410
- splitAtIntersection(other, tolerance = 0) {
411
- const intersection = this.intersect(other);
412
- if (intersection) {
413
- if (this.isPointCloseToAndBesideLineSection(intersection, tolerance) && other.isPointCloseToAndBesideLineSection(intersection, tolerance)) {
414
- return [
415
- Line2D.fromPoints(this.start, intersection),
416
- Line2D.fromPoints(intersection, this.end)
417
- ];
418
- }
419
- }
420
- return null;
421
- }
422
- /**
423
- * If lines **sections** overlap, returns the original line section split into two parts, sorted by length
424
- * Else, if the **infinite** lines intersect, returns a new line extended to the intersection point
425
- * Otherwise, null if the lines are parallel and do not intersect
426
- */
427
- splitAtOrExtendToIntersection(other) {
428
- const intersection = this.intersect(other);
429
- if (intersection) {
430
- return [
431
- Line2D.fromPoints(this.start, intersection),
432
- Line2D.fromPoints(intersection, this.end)
433
- ].filter(x => x.length > Number.EPSILON).sort((a, b) => a.length - b.length);
434
- }
435
- return null;
436
- }
437
- static order(source, lines) {
438
- if (source.start.x < source.end.x) {
439
- lines.sort((a, b) => a.start.x - b.start.x);
440
- }
441
- else if (source.start.x > source.end.x) {
442
- lines.sort((a, b) => b.start.x - a.start.x);
443
- }
444
- if (source.start.y < source.end.y) {
445
- lines.sort((a, b) => a.start.y - b.start.y);
446
- }
447
- else if (source.start.y > source.end.y) {
448
- lines.sort((a, b) => b.start.y - a.start.y);
449
- }
450
- return lines;
451
- }
452
- static subtractSingle(source, cover) {
453
- const left = source.clone();
454
- left.end.copy(cover.start);
455
- const right = source.clone();
456
- right.start.copy(cover.end);
457
- return [left, right].filter(x => x.length > 1 && x.direction.manhattanDistanceTo(source.direction) < Number.EPSILON);
458
- }
459
- /**
460
- * If other line is not contained within this line, the excess is trimmed.
461
- * Does not create a copy. Provided line is modified.
462
- * @param lineToTrim
463
- */
464
- trimExcess(lineToTrim) {
465
- if (!this.isCollinearWithTouchOrOverlap(lineToTrim)) {
466
- return;
467
- }
468
- if (!this.isPointOnLineSection(lineToTrim.start)) {
469
- const closest = this.closestPointOnLine(lineToTrim.start);
470
- lineToTrim.start.copy(closest);
471
- }
472
- if (!this.isPointOnLineSection(lineToTrim.end)) {
473
- const closest = this.closestPointOnLine(lineToTrim.end);
474
- lineToTrim.end.copy(closest);
475
- }
476
- }
477
- /**
478
- * If other line is shorter than this, endpoints are moved to extend other
479
- * Does not create a copy. Provided line is modified.
480
- * @param lineToExtend
481
- * @param tolerance
482
- */
483
- extendToEnds(lineToExtend, tolerance) {
484
- if (!this.isCollinearWithTouchOrOverlap(lineToExtend)) {
485
- console.log("Can't clip, lines that are not collinear with touch or overlap");
486
- return;
487
- }
488
- if (this.start.distanceTo(lineToExtend.start) <= tolerance) {
489
- lineToExtend.start.copy(this.start);
490
- }
491
- if (this.end.distanceTo(lineToExtend.end) <= tolerance) {
492
- lineToExtend.end.copy(this.end);
493
- }
494
- }
495
- /**
496
- * If there is an intersection between this and other, this line is extended to the intersection point. Lines are assumed to be infinite.
497
- * Modifies this line.
498
- * @param other
499
- * @param maxDistanceToIntersection
500
- */
501
- extendToOrTrimAtIntersection(other, maxDistanceToIntersection = Number.MAX_VALUE) {
502
- const intersection = this.intersect(other);
503
- if (intersection) {
504
- const distanceToStart = this.start.distanceTo(intersection);
505
- const distanceToEnd = this.end.distanceTo(intersection);
506
- if (distanceToStart <= maxDistanceToIntersection || distanceToEnd <= maxDistanceToIntersection) {
507
- if (distanceToStart < distanceToEnd) {
508
- this.start.copy(intersection);
509
- }
510
- else {
511
- this.end.copy(intersection);
512
- }
513
- }
514
- }
515
- return this;
516
- }
517
- /**
518
- * Returns the intersection point of two lines. The lines are assumed to be infinite.
519
- */
520
- intersect(other) {
521
- // Check if none of the lines are of length 0
522
- if ((this.start.x === this.end.x && this.start.y === this.end.y) || (other.start.x === other.end.x && other.start.y === other.end.y)) {
523
- return null;
524
- }
525
- const denominator = ((other.end.y - other.start.y) * (this.end.x - this.start.x) - (other.end.x - other.start.x) * (this.end.y - this.start.y));
526
- // Lines are parallel
527
- if (denominator === 0) {
528
- return null;
529
- }
530
- const ua = ((other.end.x - other.start.x) * (this.start.y - other.start.y) - (other.end.y - other.start.y) * (this.start.x - other.start.x)) / denominator;
531
- // Return an object with the x and y coordinates of the intersection
532
- const x = this.start.x + ua * (this.end.x - this.start.x);
533
- const y = this.start.y + ua * (this.end.y - this.start.y);
534
- return new Vec2_1.Vec2(x, y);
535
- }
536
- /**
537
- * Check that the infinite lines intersect and that they are in the specified angle to each other
538
- * @param other Line
539
- * @param expectedAngleInRads number
540
- */
541
- hasIntersectionWithAngle(other, expectedAngleInRads) {
542
- const angle = this.direction.angle();
543
- const otherAngle = other.direction.angle();
544
- const actualAngle = Math.abs(angle - otherAngle);
545
- if (Math.abs(actualAngle - expectedAngleInRads) < Number.EPSILON) {
546
- const intersection = this.intersect(other);
547
- if (intersection && this.isPointOnLineSection(intersection) && other.isPointOnLineSection(intersection)) {
548
- return intersection;
549
- }
550
- }
551
- return null;
552
- }
553
- /**
554
- * Deep clone of this line
555
- */
556
- clone() {
557
- return new Line2D(this.start.clone(), this.end.clone(), this.index);
558
- }
559
- toString() {
560
- return `Line(${this.start.x}, ${this.start.y}, ${this.end.x}, ${this.end.y})`;
561
- }
562
- equals(other) {
563
- return !!other && this.start.equals(other.start) && this.end.equals(other.end);
564
- }
565
- }
566
- exports.Line2D = Line2D;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Line2D = void 0;
4
+ const three_1 = require("three");
5
+ const Vec2_1 = require("./Vec2");
6
+ class Line2D {
7
+ start;
8
+ end;
9
+ index;
10
+ constructor(start, end, index = 0) {
11
+ this.start = start;
12
+ this.end = end;
13
+ this.index = index;
14
+ }
15
+ static fromCoordinates(x1, y1, x2, y2, index = 0) {
16
+ return new Line2D(new Vec2_1.Vec2(x1, y1), new Vec2_1.Vec2(x2, y2), index);
17
+ }
18
+ static fromPoints(p1, p2, index = 0) {
19
+ return new Line2D(new Vec2_1.Vec2(p1.x, p1.y), new Vec2_1.Vec2(p2.x, p2.y), index);
20
+ }
21
+ /**
22
+ * Creates a polygon formed by an array of lines from points provided.
23
+ * The polygon will only be closed if either
24
+ * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
25
+ */
26
+ static fromPolygon(polygon, forceClosedPolygon = false) {
27
+ if (!polygon || polygon.length < 2) {
28
+ return [];
29
+ }
30
+ if (forceClosedPolygon && (polygon[0].x !== polygon.at(-1).x || polygon[0].y !== polygon.at(-1).y)) {
31
+ polygon = [...polygon, polygon[0]];
32
+ }
33
+ const lines = [];
34
+ for (let i = 0; i < polygon.length - 1; i++) {
35
+ lines.push(Line2D.fromPoints(polygon[i], polygon[i + 1], i));
36
+ }
37
+ return lines;
38
+ }
39
+ static fromLength(length) {
40
+ return Line2D.fromCoordinates(-length / 2, 0, length / 2, 0);
41
+ }
42
+ get center() {
43
+ return new Vec2_1.Vec2((this.start.x + this.end.x) / 2, (this.start.y + this.end.y) / 2);
44
+ }
45
+ /**
46
+ * Set the center of the line to the provided point. Length and direction remain unchanged.
47
+ * Modifies this line.
48
+ * @param value
49
+ */
50
+ set center(value) {
51
+ const current = this.center;
52
+ const diffX = current.x - value.x;
53
+ const diffY = current.y - value.y;
54
+ this.start.x -= diffX;
55
+ this.start.y -= diffY;
56
+ this.end.x -= diffX;
57
+ this.end.y -= diffY;
58
+ }
59
+ /**
60
+ * Set the center of the line to the provided point. Length and direction remain unchanged.
61
+ * Modifies this line.
62
+ * @param value
63
+ */
64
+ setCenter(value) {
65
+ this.center = value;
66
+ return this;
67
+ }
68
+ /*
69
+ * Extends or reduces the line by the given length while keeping the center of the line constant.
70
+ * Modifies this line.
71
+ */
72
+ resize(amount) {
73
+ this.moveStartPoint(amount / 2);
74
+ this.moveEndPoint(amount / 2);
75
+ return this;
76
+ }
77
+ /*
78
+ * Moves start point on the line by the given amount. Plus values move the point further away from the center.
79
+ * Modifies this line.
80
+ */
81
+ moveStartPoint(amount) {
82
+ const p1 = this.movePointOnThisLine(this.start, amount);
83
+ this.start.copy(p1);
84
+ return this;
85
+ }
86
+ /**
87
+ * Moves end point on the line by the given amount. Plus values move the point further away from the center.
88
+ * Modifies this line.
89
+ */
90
+ moveEndPoint(amount) {
91
+ const p2 = this.movePointOnThisLine(this.end, amount);
92
+ this.end.copy(p2);
93
+ return this;
94
+ }
95
+ movePointOnThisLine(point, amount) {
96
+ const vec = new three_1.Vector2(this.center.x - point.x, this.center.y - point.y);
97
+ const length = vec.length();
98
+ vec.normalize().multiplyScalar(length + amount);
99
+ return new Vec2_1.Vec2(this.center.x - vec.x, this.center.y - vec.y);
100
+ }
101
+ /**
102
+ * Set the length of this line. Center and direction remain unchanged.
103
+ * Modifies this line.
104
+ * @param l
105
+ */
106
+ set length(l) {
107
+ const length = this.length;
108
+ this.resize(l - length);
109
+ }
110
+ get length() {
111
+ return this.start.distanceTo(this.end);
112
+ }
113
+ /**
114
+ * Set the length of this line. Center and direction remain unchanged.
115
+ * @param length
116
+ */
117
+ setLength(length) {
118
+ this.length = length;
119
+ return this;
120
+ }
121
+ /**
122
+ * Returns the start and end points of the line as an array.
123
+ * Endpoints are not cloned.
124
+ */
125
+ get endpoints() {
126
+ return [this.start, this.end];
127
+ }
128
+ /**
129
+ * Returns the direction of this line.
130
+ */
131
+ get direction() {
132
+ return this.end.clone().sub(this.start).normalize();
133
+ }
134
+ /**
135
+ * Inverts the direction of the line.
136
+ * Modifies this line.
137
+ */
138
+ flip() {
139
+ const temp = this.start.clone();
140
+ this.start.copy(this.end);
141
+ this.end.copy(temp);
142
+ return this;
143
+ }
144
+ /**
145
+ * Rotates the line around the center by the given angle in radians.
146
+ * Modifies this line.
147
+ * @param radians Positive values rotate counter-clockwise.
148
+ * @param center
149
+ */
150
+ rotate(radians, center = this.center) {
151
+ this.start.rotateAround(center, radians);
152
+ this.end.rotateAround(center, radians);
153
+ return this;
154
+ }
155
+ /**
156
+ * Move the line by the given vector.
157
+ * Modifies this line.
158
+ */
159
+ translate(value) {
160
+ this.start.x += value.x;
161
+ this.start.y += value.y;
162
+ this.end.x += value.x;
163
+ this.end.y += value.y;
164
+ return this;
165
+ }
166
+ /**
167
+ * Move the line to its left by the given amount.
168
+ * Modifies this line.
169
+ */
170
+ translateLeft(amount) {
171
+ const translation = this.direction.rotateAround(new Vec2_1.Vec2(), -Math.PI / 2).multiplyScalar(amount);
172
+ return this.translate(translation);
173
+ }
174
+ /**
175
+ * Move the line to its right by the given amount.
176
+ * Modifies this line.
177
+ */
178
+ translateRight(amount) {
179
+ const translation = this.direction.rotateAround(new Vec2_1.Vec2(), Math.PI / 2).multiplyScalar(amount);
180
+ return this.translate(translation);
181
+ }
182
+ /**
183
+ * Returns true when the point is actually inside the (finite) line segment.
184
+ * https://jsfiddle.net/c06zdxtL/2/
185
+ * https://stackoverflow.com/questions/6865832/detecting-if-a-point-is-of-a-line-segment/6877674
186
+ * @param point: Point2
187
+ */
188
+ isPointOnLineSection(point) {
189
+ if (!this.isPointOnInfiniteLine(point)) {
190
+ return false;
191
+ }
192
+ return this.isPointBesideLineSection(point);
193
+ }
194
+ /**
195
+ * Returns true when the point is beside the line **segment** and within the maxDistance.
196
+ * @param point
197
+ * @param maxDistance
198
+ */
199
+ isPointCloseToAndBesideLineSection(point, maxDistance) {
200
+ const distance = this.distanceToPointOnInfiniteLine(point);
201
+ return distance <= maxDistance && this.isPointBesideLineSection(point);
202
+ }
203
+ /**
204
+ * Returns true when the point is beside the line **segment**
205
+ * @param point
206
+ */
207
+ isPointBesideLineSection(point) {
208
+ const l2 = (((this.end.x - this.start.x) * (this.end.x - this.start.x)) + ((this.end.y - this.start.y) * (this.end.y - this.start.y)));
209
+ if (l2 == 0)
210
+ return false;
211
+ const r = (((point.x - this.start.x) * (this.end.x - this.start.x)) + ((point.y - this.start.y) * (this.end.y - this.start.y))) / l2;
212
+ return (0 <= r) && (r <= 1);
213
+ }
214
+ /**
215
+ * Returns true when the point is on the **infinite** line.
216
+ * @param point
217
+ */
218
+ isPointOnInfiniteLine(point) {
219
+ return (point.y - this.start.y) * (this.end.x - this.start.x) === (this.end.y - this.start.y) * (point.x - this.start.x);
220
+ }
221
+ /**
222
+ * Returns true if other line is collinear and overlaps or at least touching this line.
223
+ * @param other
224
+ */
225
+ isCollinearWithTouchOrOverlap(other) {
226
+ if (!this.isPointOnInfiniteLine(other.start) || !this.isPointOnInfiniteLine(other.end)) {
227
+ return false;
228
+ }
229
+ return this.isPointOnLineSection(other.start) || this.isPointOnLineSection(other.end) ||
230
+ other.isPointOnLineSection(this.start) || other.isPointOnLineSection(this.end);
231
+ }
232
+ /**
233
+ * Returns true if there is any overlap between this line and the @other line section.
234
+ */
235
+ overlaps(other) {
236
+ if (!this.isCollinearWithTouchOrOverlap(other)) {
237
+ return false;
238
+ }
239
+ if (this.start.equals(other.start) && this.end.equals(other.end)) {
240
+ return true;
241
+ }
242
+ return !this.start.equals(other.end) && !this.end.equals(other.start);
243
+ }
244
+ /**
245
+ * Logical AND of this and the other line section.
246
+ * @param other
247
+ */
248
+ getOverlap(other) {
249
+ if (!this.overlaps(other)) {
250
+ return null;
251
+ }
252
+ if (this.equals(other)) {
253
+ return this.clone();
254
+ }
255
+ const points = [
256
+ [this.start, this.end].filter(thisPoint => other.isPointOnLineSection(thisPoint)),
257
+ [other.start, other.end].filter(otherPoint => this.isPointOnLineSection(otherPoint))
258
+ ].flat();
259
+ if (points.length !== 2) {
260
+ return null;
261
+ }
262
+ const overlap = Line2D.fromPoints(points[0], points[1]);
263
+ if (overlap.direction.manhattanDistanceTo(this.direction) > Number.EPSILON) {
264
+ overlap.flip();
265
+ }
266
+ return overlap;
267
+ }
268
+ /**
269
+ * Joins a copy of @line with the @other line.
270
+ * Other must be parallel to this line.
271
+ * Returns null if there is no overlap
272
+ * Clones the line, does not modify.
273
+ * @param line
274
+ * @param other
275
+ */
276
+ static joinLine(line, other) {
277
+ if (!line.isCollinearWithTouchOrOverlap(other)) {
278
+ return null;
279
+ }
280
+ const p1 = !line.isPointOnLineSection(other.start) ? other.start : line.start;
281
+ const p2 = !line.isPointOnLineSection(other.end) ? other.end : line.end;
282
+ return new Line2D(p1.clone(), p2.clone(), line.index);
283
+ }
284
+ /**
285
+ * Joins provided lines into several joined lines.
286
+ * Lines must be parallel for joining.
287
+ * Clone the lines, does not modify.
288
+ * @param lines
289
+ */
290
+ static joinLines(lines) {
291
+ if (lines.length < 2) {
292
+ return lines.map(x => x.clone());
293
+ }
294
+ const toProcess = lines.slice();
295
+ const result = [];
296
+ while (toProcess.length > 0) {
297
+ const current = toProcess.pop();
298
+ let joined = false;
299
+ for (let i = 0; i < result.length; i++) {
300
+ const other = result[i];
301
+ const joinedLine = Line2D.joinLine(current, other);
302
+ if (joinedLine) {
303
+ result[i] = joinedLine;
304
+ joined = true;
305
+ break;
306
+ }
307
+ }
308
+ if (!joined) {
309
+ result.push(current.clone());
310
+ }
311
+ }
312
+ return result;
313
+ }
314
+ /**
315
+ * Divides the Line3D into a number of segments of the given length.
316
+ * Clone the line, does not modify.
317
+ * @param maxSegmentLength number
318
+ */
319
+ chunk(maxSegmentLength) {
320
+ const source = this.clone();
321
+ const result = [];
322
+ while (source.length > maxSegmentLength) {
323
+ const chunk = source.clone();
324
+ chunk.moveEndPoint(-(chunk.length - maxSegmentLength));
325
+ result.push(chunk);
326
+ source.start = chunk.end.clone();
327
+ }
328
+ result.push(source);
329
+ return result;
330
+ }
331
+ /**
332
+ * Returns the closest point parameter on the **infinite** line to the given point.
333
+ * @param point
334
+ */
335
+ closestPointToPointParameterOnInfiniteLine(point) {
336
+ const startP = new Vec2_1.Vec2().subVectors(point, this.start);
337
+ const startEnd = new Vec2_1.Vec2().subVectors(this.end, this.start);
338
+ const startEnd2 = startEnd.dot(startEnd);
339
+ const startEnd_startP = startEnd.dot(startP);
340
+ return startEnd_startP / startEnd2;
341
+ }
342
+ /**
343
+ * Returns the closest point on the **infinite** line to the given point.
344
+ * @param point
345
+ */
346
+ closestPointOnInfiniteLine(point) {
347
+ const t = this.closestPointToPointParameterOnInfiniteLine(point);
348
+ return new Vec2_1.Vec2().subVectors(this.end, this.start).multiplyScalar(t).add(this.start);
349
+ }
350
+ /**
351
+ * Returns the closest point on the line **section** to the given point.
352
+ * @param point
353
+ */
354
+ closestPointOnLine(point) {
355
+ const closestPoint = this.closestPointOnInfiniteLine(point);
356
+ if (this.isPointOnLineSection(closestPoint)) {
357
+ return closestPoint;
358
+ }
359
+ return closestPoint.distanceTo(this.start) < closestPoint.distanceTo(this.end) ? this.start : this.end;
360
+ }
361
+ /**
362
+ * Returns the distance between the **infinite** line and the point.
363
+ * @param point
364
+ */
365
+ distanceToPointOnInfiniteLine(point) {
366
+ const l2 = (((this.end.x - this.start.x) * (this.end.x - this.start.x)) + ((this.end.y - this.start.y) * (this.end.y - this.start.y)));
367
+ if (l2 == 0)
368
+ return Infinity;
369
+ const s = (((this.start.y - point.y) * (this.end.x - this.start.x)) - ((this.start.x - point.x) * (this.end.y - this.start.y))) / l2;
370
+ return Math.abs(s) * Math.sqrt(l2);
371
+ }
372
+ /**
373
+ * Returns lines that are the result of clipping @source line by the @clips.
374
+ * Clips must be parallel to this line.
375
+ * Clones the line, does not modify this.
376
+ * @param source
377
+ * @param clips
378
+ */
379
+ static clipLines(source, clips) {
380
+ if (!clips || clips.length === 0)
381
+ return [source];
382
+ clips = clips.map(c => {
383
+ const copy = c.clone();
384
+ if (copy.direction.manhattanDistanceTo(source.direction) > Number.EPSILON) {
385
+ copy.flip();
386
+ }
387
+ return copy;
388
+ });
389
+ const free = [];
390
+ const sources = [source];
391
+ while (sources.length > 0) {
392
+ let isFree = true;
393
+ const tested = sources.pop();
394
+ for (const cover of clips) {
395
+ if (tested.overlaps(cover)) {
396
+ isFree = false;
397
+ const subtracted = this.subtractSingle(tested, cover);
398
+ sources.push(...subtracted);
399
+ break;
400
+ }
401
+ }
402
+ if (isFree)
403
+ free.push(tested);
404
+ }
405
+ return this.order(source, free);
406
+ }
407
+ /**
408
+ * Returns the original line section split into two parts, if the line **sections** overlap, otherwise null
409
+ */
410
+ splitAtIntersection(other, tolerance = 0) {
411
+ const intersection = this.intersect(other);
412
+ if (intersection) {
413
+ if (this.isPointCloseToAndBesideLineSection(intersection, tolerance) && other.isPointCloseToAndBesideLineSection(intersection, tolerance)) {
414
+ return [
415
+ Line2D.fromPoints(this.start, intersection),
416
+ Line2D.fromPoints(intersection, this.end)
417
+ ];
418
+ }
419
+ }
420
+ return null;
421
+ }
422
+ /**
423
+ * If lines **sections** overlap, returns the original line section split into two parts, sorted by length
424
+ * Else, if the **infinite** lines intersect, returns a new line extended to the intersection point
425
+ * Otherwise, null if the lines are parallel and do not intersect
426
+ */
427
+ splitAtOrExtendToIntersection(other) {
428
+ const intersection = this.intersect(other);
429
+ if (intersection) {
430
+ return [
431
+ Line2D.fromPoints(this.start, intersection),
432
+ Line2D.fromPoints(intersection, this.end)
433
+ ].filter(x => x.length > Number.EPSILON).sort((a, b) => a.length - b.length);
434
+ }
435
+ return null;
436
+ }
437
+ static order(source, lines) {
438
+ if (source.start.x < source.end.x) {
439
+ lines.sort((a, b) => a.start.x - b.start.x);
440
+ }
441
+ else if (source.start.x > source.end.x) {
442
+ lines.sort((a, b) => b.start.x - a.start.x);
443
+ }
444
+ if (source.start.y < source.end.y) {
445
+ lines.sort((a, b) => a.start.y - b.start.y);
446
+ }
447
+ else if (source.start.y > source.end.y) {
448
+ lines.sort((a, b) => b.start.y - a.start.y);
449
+ }
450
+ return lines;
451
+ }
452
+ static subtractSingle(source, cover) {
453
+ const left = source.clone();
454
+ left.end.copy(cover.start);
455
+ const right = source.clone();
456
+ right.start.copy(cover.end);
457
+ return [left, right].filter(x => x.length > 1 && x.direction.manhattanDistanceTo(source.direction) < Number.EPSILON);
458
+ }
459
+ /**
460
+ * If other line is not contained within this line, the excess is trimmed.
461
+ * Does not create a copy. Provided line is modified.
462
+ * @param lineToTrim
463
+ */
464
+ trimExcess(lineToTrim) {
465
+ if (!this.isCollinearWithTouchOrOverlap(lineToTrim)) {
466
+ return;
467
+ }
468
+ if (!this.isPointOnLineSection(lineToTrim.start)) {
469
+ const closest = this.closestPointOnLine(lineToTrim.start);
470
+ lineToTrim.start.copy(closest);
471
+ }
472
+ if (!this.isPointOnLineSection(lineToTrim.end)) {
473
+ const closest = this.closestPointOnLine(lineToTrim.end);
474
+ lineToTrim.end.copy(closest);
475
+ }
476
+ }
477
+ /**
478
+ * If other line is shorter than this, endpoints are moved to extend other
479
+ * Does not create a copy. Provided line is modified.
480
+ * @param lineToExtend
481
+ * @param tolerance
482
+ */
483
+ extendToEnds(lineToExtend, tolerance) {
484
+ if (!this.isCollinearWithTouchOrOverlap(lineToExtend)) {
485
+ console.log("Can't clip, lines that are not collinear with touch or overlap");
486
+ return;
487
+ }
488
+ if (this.start.distanceTo(lineToExtend.start) <= tolerance) {
489
+ lineToExtend.start.copy(this.start);
490
+ }
491
+ if (this.end.distanceTo(lineToExtend.end) <= tolerance) {
492
+ lineToExtend.end.copy(this.end);
493
+ }
494
+ }
495
+ /**
496
+ * If there is an intersection between this and other, this line is extended to the intersection point. Lines are assumed to be infinite.
497
+ * Modifies this line.
498
+ * @param other
499
+ * @param maxDistanceToIntersection
500
+ */
501
+ extendToOrTrimAtIntersection(other, maxDistanceToIntersection = Number.MAX_VALUE) {
502
+ const intersection = this.intersect(other);
503
+ if (intersection) {
504
+ const distanceToStart = this.start.distanceTo(intersection);
505
+ const distanceToEnd = this.end.distanceTo(intersection);
506
+ if (distanceToStart <= maxDistanceToIntersection || distanceToEnd <= maxDistanceToIntersection) {
507
+ if (distanceToStart < distanceToEnd) {
508
+ this.start.copy(intersection);
509
+ }
510
+ else {
511
+ this.end.copy(intersection);
512
+ }
513
+ }
514
+ }
515
+ return this;
516
+ }
517
+ /**
518
+ * Returns the intersection point of two lines. The lines are assumed to be infinite.
519
+ */
520
+ intersect(other) {
521
+ // Check if none of the lines are of length 0
522
+ if ((this.start.x === this.end.x && this.start.y === this.end.y) || (other.start.x === other.end.x && other.start.y === other.end.y)) {
523
+ return null;
524
+ }
525
+ const denominator = ((other.end.y - other.start.y) * (this.end.x - this.start.x) - (other.end.x - other.start.x) * (this.end.y - this.start.y));
526
+ // Lines are parallel
527
+ if (denominator === 0) {
528
+ return null;
529
+ }
530
+ const ua = ((other.end.x - other.start.x) * (this.start.y - other.start.y) - (other.end.y - other.start.y) * (this.start.x - other.start.x)) / denominator;
531
+ // Return an object with the x and y coordinates of the intersection
532
+ const x = this.start.x + ua * (this.end.x - this.start.x);
533
+ const y = this.start.y + ua * (this.end.y - this.start.y);
534
+ return new Vec2_1.Vec2(x, y);
535
+ }
536
+ /**
537
+ * Check that the infinite lines intersect and that they are in the specified angle to each other
538
+ * @param other Line
539
+ * @param expectedAngleInRads number
540
+ */
541
+ hasIntersectionWithAngle(other, expectedAngleInRads) {
542
+ const angle = this.direction.angle();
543
+ const otherAngle = other.direction.angle();
544
+ const actualAngle = Math.abs(angle - otherAngle);
545
+ if (Math.abs(actualAngle - expectedAngleInRads) < Number.EPSILON) {
546
+ const intersection = this.intersect(other);
547
+ if (intersection && this.isPointOnLineSection(intersection) && other.isPointOnLineSection(intersection)) {
548
+ return intersection;
549
+ }
550
+ }
551
+ return null;
552
+ }
553
+ /**
554
+ * Deep clone of this line
555
+ */
556
+ clone() {
557
+ return new Line2D(this.start.clone(), this.end.clone(), this.index);
558
+ }
559
+ toString() {
560
+ return `Line(${this.start.x}, ${this.start.y}, ${this.end.x}, ${this.end.y})`;
561
+ }
562
+ equals(other) {
563
+ return !!other && this.start.equals(other.start) && this.end.equals(other.end);
564
+ }
565
+ }
566
+ exports.Line2D = Line2D;