@immugio/three-math-extensions 0.0.1

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