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