@immugio/three-math-extensions 0.2.37 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,7 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
9
9
 
10
- ## [0.2.37](https://github.com/Immugio/three-math-extensions/compare/0.2.36...0.2.37)
10
+ ## [0.3.0](https://github.com/Immugio/three-math-extensions/compare/0.2.38...0.3.0)
11
+
12
+ ### Commits
13
+
14
+ - Add Line2D.split [`8f218fb`](https://github.com/Immugio/three-math-extensions/commit/8f218fb6d8682e65f1da49c9084d56ec3e9a68b7)
15
+
16
+ ## [0.2.38](https://github.com/Immugio/three-math-extensions/compare/0.2.37...0.2.38) - 2024-12-09
17
+
18
+ ### Commits
19
+
20
+ - Improve Polygon.offsetContour algorithm [`60a28f8`](https://github.com/Immugio/three-math-extensions/commit/60a28f84bd5561b9bb5934561b7bb92f06861a53)
21
+
22
+ ## [0.2.37](https://github.com/Immugio/three-math-extensions/compare/0.2.36...0.2.37) - 2024-12-05
11
23
 
12
24
  ### Commits
13
25
 
package/cjs/Line2D.js CHANGED
@@ -96,8 +96,7 @@ class Line2D {
96
96
  * Modifies this line.
97
97
  */
98
98
  moveStartPoint(amount) {
99
- const p1 = this.movePointOnThisLine(this.start, amount);
100
- this.start.copy(p1);
99
+ this.start.add(this.direction.multiplyScalar(-amount));
101
100
  return this;
102
101
  }
103
102
  /**
@@ -105,16 +104,9 @@ class Line2D {
105
104
  * Modifies this line.
106
105
  */
107
106
  moveEndPoint(amount) {
108
- const p2 = this.movePointOnThisLine(this.end, amount);
109
- this.end.copy(p2);
107
+ this.end.add(this.direction.multiplyScalar(amount));
110
108
  return this;
111
109
  }
112
- movePointOnThisLine(point, amount) {
113
- const vec = new three_1.Vector2(this.center.x - point.x, this.center.y - point.y);
114
- const length = vec.length();
115
- vec.normalize().multiplyScalar(length + amount);
116
- return new Vec2_1.Vec2(this.center.x - vec.x, this.center.y - vec.y);
117
- }
118
110
  /**
119
111
  * Set the length of this line. Center and direction remain unchanged.
120
112
  * Modifies this line.
@@ -419,6 +411,17 @@ class Line2D {
419
411
  result.push(source);
420
412
  return result;
421
413
  }
414
+ split(segmentsCount) {
415
+ const result = [];
416
+ const segmentLength = this.length / segmentsCount;
417
+ for (let i = 0; i < segmentsCount; i++) {
418
+ const line = this.clone();
419
+ line.moveStartPoint(-i * segmentLength);
420
+ line.moveEndPoint(-(segmentsCount - i - 1) * segmentLength);
421
+ result.push(line);
422
+ }
423
+ return result;
424
+ }
422
425
  /**
423
426
  * Returns the closest point on the line to the given point.
424
427
  * @param point
package/cjs/Polygon.js CHANGED
@@ -152,8 +152,17 @@ class Polygon {
152
152
  console.error("The contour should be closed");
153
153
  return this;
154
154
  }
155
- for (let i = 0; i < this.contour.length - 1; i++) {
156
- this.translateContourLine(i, offset);
155
+ const lines = Line2D_1.Line2D
156
+ .fromPolygon(this.contour)
157
+ .map(line => line.translateLeft(offset));
158
+ for (let i = 0; i < lines.length; i++) {
159
+ const line = lines[i];
160
+ const next = lines[(i + 1) % lines.length];
161
+ line.extendToOrTrimAtIntersection(next);
162
+ next.extendToOrTrimAtIntersection(line);
163
+ }
164
+ for (let i = 0; i < this.contour.length; i++) {
165
+ this.contour[i].copy(lines[i % lines.length].start);
157
166
  }
158
167
  return this;
159
168
  }
package/esm/Line2D.js CHANGED
@@ -1,4 +1,4 @@
1
- import { MathUtils, Vector2 } from "three";
1
+ import { MathUtils } from "three";
2
2
  import { Vec2 } from "./Vec2";
3
3
  import { TwoPI } from "./MathConstants";
4
4
  import { Line3D } from "./Line3D";
@@ -93,8 +93,7 @@ export class Line2D {
93
93
  * Modifies this line.
94
94
  */
95
95
  moveStartPoint(amount) {
96
- const p1 = this.movePointOnThisLine(this.start, amount);
97
- this.start.copy(p1);
96
+ this.start.add(this.direction.multiplyScalar(-amount));
98
97
  return this;
99
98
  }
100
99
  /**
@@ -102,16 +101,9 @@ export class Line2D {
102
101
  * Modifies this line.
103
102
  */
104
103
  moveEndPoint(amount) {
105
- const p2 = this.movePointOnThisLine(this.end, amount);
106
- this.end.copy(p2);
104
+ this.end.add(this.direction.multiplyScalar(amount));
107
105
  return this;
108
106
  }
109
- movePointOnThisLine(point, amount) {
110
- const vec = new Vector2(this.center.x - point.x, this.center.y - point.y);
111
- const length = vec.length();
112
- vec.normalize().multiplyScalar(length + amount);
113
- return new Vec2(this.center.x - vec.x, this.center.y - vec.y);
114
- }
115
107
  /**
116
108
  * Set the length of this line. Center and direction remain unchanged.
117
109
  * Modifies this line.
@@ -416,6 +408,17 @@ export class Line2D {
416
408
  result.push(source);
417
409
  return result;
418
410
  }
411
+ split(segmentsCount) {
412
+ const result = [];
413
+ const segmentLength = this.length / segmentsCount;
414
+ for (let i = 0; i < segmentsCount; i++) {
415
+ const line = this.clone();
416
+ line.moveStartPoint(-i * segmentLength);
417
+ line.moveEndPoint(-(segmentsCount - i - 1) * segmentLength);
418
+ result.push(line);
419
+ }
420
+ return result;
421
+ }
419
422
  /**
420
423
  * Returns the closest point on the line to the given point.
421
424
  * @param point
package/esm/Polygon.js CHANGED
@@ -149,8 +149,17 @@ export class Polygon {
149
149
  console.error("The contour should be closed");
150
150
  return this;
151
151
  }
152
- for (let i = 0; i < this.contour.length - 1; i++) {
153
- this.translateContourLine(i, offset);
152
+ const lines = Line2D
153
+ .fromPolygon(this.contour)
154
+ .map(line => line.translateLeft(offset));
155
+ for (let i = 0; i < lines.length; i++) {
156
+ const line = lines[i];
157
+ const next = lines[(i + 1) % lines.length];
158
+ line.extendToOrTrimAtIntersection(next);
159
+ next.extendToOrTrimAtIntersection(line);
160
+ }
161
+ for (let i = 0; i < this.contour.length; i++) {
162
+ this.contour[i].copy(lines[i % lines.length].start);
154
163
  }
155
164
  return this;
156
165
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@immugio/three-math-extensions",
3
- "version": "0.2.37",
3
+ "version": "0.3.0",
4
4
  "description": "Set of utilities for 2d and 3d line math built on top of three.js",
5
5
  "author": "Jan Mikeska <janmikeska@gmail.com>",
6
6
  "license": "ISC",
package/src/Line2D.ts CHANGED
@@ -107,9 +107,7 @@ export class Line2D {
107
107
  * Modifies this line.
108
108
  */
109
109
  public moveStartPoint(amount: number): this {
110
- const p1 = this.movePointOnThisLine(this.start, amount);
111
- this.start.copy(p1);
112
-
110
+ this.start.add(this.direction.multiplyScalar(-amount));
113
111
  return this;
114
112
  }
115
113
 
@@ -118,20 +116,10 @@ export class Line2D {
118
116
  * Modifies this line.
119
117
  */
120
118
  public moveEndPoint(amount: number): this {
121
- const p2 = this.movePointOnThisLine(this.end, amount);
122
- this.end.copy(p2);
123
-
119
+ this.end.add(this.direction.multiplyScalar(amount));
124
120
  return this;
125
121
  }
126
122
 
127
- private movePointOnThisLine(point: Point2, amount: number): Vec2 {
128
- const vec = new Vector2(this.center.x - point.x, this.center.y - point.y);
129
- const length = vec.length();
130
- vec.normalize().multiplyScalar(length + amount);
131
-
132
- return new Vec2(this.center.x - vec.x,this.center.y - vec.y);
133
- }
134
-
135
123
  /**
136
124
  * Set the length of this line. Center and direction remain unchanged.
137
125
  * Modifies this line.
@@ -490,6 +478,18 @@ export class Line2D {
490
478
  return result;
491
479
  }
492
480
 
481
+ public split(segmentsCount: number): Line2D[] {
482
+ const result: Line2D[] = [];
483
+ const segmentLength = this.length / segmentsCount;
484
+ for (let i = 0; i < segmentsCount; i++) {
485
+ const line = this.clone();
486
+ line.moveStartPoint(-i * segmentLength);
487
+ line.moveEndPoint(-(segmentsCount - i - 1) * segmentLength);
488
+ result.push(line);
489
+ }
490
+ return result;
491
+ }
492
+
493
493
  /**
494
494
  * Returns the closest point on the line to the given point.
495
495
  * @param point
package/src/Polygon.ts CHANGED
@@ -180,9 +180,21 @@ export class Polygon {
180
180
  return this;
181
181
  }
182
182
 
183
- for (let i = 0; i < this.contour.length - 1; i++) {
184
- this.translateContourLine(i, offset);
183
+ const lines = Line2D
184
+ .fromPolygon(this.contour)
185
+ .map(line => line.translateLeft(offset));
186
+
187
+ for (let i = 0; i < lines.length; i++) {
188
+ const line = lines[i];
189
+ const next = lines[(i + 1) % lines.length];
190
+ line.extendToOrTrimAtIntersection(next);
191
+ next.extendToOrTrimAtIntersection(line);
185
192
  }
193
+
194
+ for (let i = 0; i < this.contour.length; i++) {
195
+ this.contour[i].copy(lines[i % lines.length].start);
196
+ }
197
+
186
198
  return this;
187
199
  }
188
200
 
package/types/Line2D.d.ts CHANGED
@@ -38,7 +38,6 @@ export declare class Line2D {
38
38
  * Modifies this line.
39
39
  */
40
40
  moveEndPoint(amount: number): this;
41
- private movePointOnThisLine;
42
41
  /**
43
42
  * Set the length of this line. Center and direction remain unchanged.
44
43
  * Modifies this line.
@@ -177,6 +176,7 @@ export declare class Line2D {
177
176
  * @param maxSegmentLength number
178
177
  */
179
178
  chunk(maxSegmentLength: number): Line2D[];
179
+ split(segmentsCount: number): Line2D[];
180
180
  /**
181
181
  * Returns the closest point on the line to the given point.
182
182
  * @param point