@immugio/three-math-extensions 0.0.3 → 0.0.5

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