@immugio/three-math-extensions 0.2.32 → 0.2.33

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,13 @@ 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.32](https://github.com/Immugio/three-math-extensions/compare/0.2.31...0.2.32)
10
+ ## [0.2.33](https://github.com/Immugio/three-math-extensions/compare/0.2.32...0.2.33)
11
+
12
+ ### Commits
13
+
14
+ - Line2D.groupConnectedLines improve sort [`fa56c1e`](https://github.com/Immugio/three-math-extensions/commit/fa56c1e13b2b49f369034224985f31f069e3079f)
15
+
16
+ ## [0.2.32](https://github.com/Immugio/three-math-extensions/compare/0.2.31...0.2.32) - 2024-10-18
11
17
 
12
18
  ### Commits
13
19
 
@@ -274,29 +280,48 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
274
280
 
275
281
  - Add Vec2 documentation [`f625e66`](https://github.com/Immugio/three-math-extensions/commit/f625e66b60ee0d90c5c788a80989f64013d60265)
276
282
 
277
- ## [0.0.11](https://github.com/Immugio/three-math-extensions/compare/0.0.6...0.0.11) - 2022-12-20
283
+ ## [0.0.11](https://github.com/Immugio/three-math-extensions/compare/0.0.10...0.0.11) - 2022-12-20
278
284
 
279
285
  ### Commits
280
286
 
287
+ - Improve API, minor bug fix, improve test coverage [`881a5f0`](https://github.com/Immugio/three-math-extensions/commit/881a5f096823f0d5fd90faa76cd602f076291bc5)
281
288
  - Add Line2D.extendToOrTrimAtIntersection [`ab82a36`](https://github.com/Immugio/three-math-extensions/commit/ab82a36db52b9ae83cf8bfb848362d295140d073)
282
289
  - Added "isNear" to Vec2 & Vec3 as a shorthand for distanceTo <= maxDistance [`a27cff8`](https://github.com/Immugio/three-math-extensions/commit/a27cff8421472f625ed240439b9e6a7d2c430db8)
290
+ - Add .npmignore [`f58329a`](https://github.com/Immugio/three-math-extensions/commit/f58329a86b96589bb574d6ebed37d4cc4474663a)
283
291
  - Add Vec2.moveTowards [`b46ba12`](https://github.com/Immugio/three-math-extensions/commit/b46ba1286f9924cd9bfecfb37200664202e964ba)
284
292
  - Add Line2D.extendToOrTrimAtIntersection [`b22aa12`](https://github.com/Immugio/three-math-extensions/commit/b22aa120b5eb02562cebc4573374ebb61e2dba14)
293
+ - Correct change log version [`c6244bf`](https://github.com/Immugio/three-math-extensions/commit/c6244bf1488ad21bcc5589d1dff62c41d8182d48)
294
+
295
+ ## [0.0.10](https://github.com/Immugio/three-math-extensions/compare/0.0.9...0.0.10) - 2022-11-21
296
+
297
+ ### Commits
298
+
299
+ - Release 0.0.4 - tag pattern 6 [`c1c5454`](https://github.com/Immugio/three-math-extensions/commit/c1c54541ed400e1ad49bf42fe8926cd5293efefd)
300
+
301
+ ## [0.0.9](https://github.com/Immugio/three-math-extensions/compare/0.0.8...0.0.9) - 2022-11-21
302
+
303
+ ### Commits
304
+
305
+ - Release 0.0.4 - tag pattern 5 [`5245eed`](https://github.com/Immugio/three-math-extensions/commit/5245eed60195103e989c0fadf9bd642f39ef4589)
306
+
307
+ ## [0.0.8](https://github.com/Immugio/three-math-extensions/compare/0.0.7...0.0.8) - 2022-11-21
308
+
309
+ ### Commits
310
+
311
+ - Release 0.0.4 - tag pattern 4 [`ab9ad4e`](https://github.com/Immugio/three-math-extensions/commit/ab9ad4e49477a991482ed6d4d3a3ca4687a70b44)
312
+
313
+ ## [0.0.7](https://github.com/Immugio/three-math-extensions/compare/0.0.6...0.0.7) - 2022-11-21
285
314
 
286
315
  ## [0.0.6](https://github.com/Immugio/three-math-extensions/compare/0.0.5...0.0.6) - 2022-11-24
287
316
 
288
- ## 0.0.5 - 2023-09-01
317
+ ## [0.0.5](https://github.com/Immugio/three-math-extensions/compare/0.0.4...0.0.5) - 2023-09-01
289
318
 
290
319
  ### Commits
291
320
 
292
- - Automatic publish with change log [`a70c99d`](https://github.com/Immugio/three-math-extensions/commit/a70c99d0d42e60411ce01bcdb8dd4f3f489bb949)
293
- - Switch to npm [`ed47a17`](https://github.com/Immugio/three-math-extensions/commit/ed47a1787522db3bde5a2112bfd02480fe2edc54)
294
- - Initial commit [`3d1bf9e`](https://github.com/Immugio/three-math-extensions/commit/3d1bf9ef015570830007c9be99140c8c8d760d7f)
295
321
  - Generate documentation using TSDoc [`304c3a8`](https://github.com/Immugio/three-math-extensions/commit/304c3a84b5dcc49183db57083f550d134ae641b2)
296
322
  - Add eslint [`98e4912`](https://github.com/Immugio/three-math-extensions/commit/98e4912d637b42ba80e2f3267638b43296113019)
297
323
  - Update jest packages [`af23b4f`](https://github.com/Immugio/three-math-extensions/commit/af23b4f08154bba3407f05b773865215e7e1cba8)
298
324
  - Add Rectangle, update Polygon [`58ee875`](https://github.com/Immugio/three-math-extensions/commit/58ee87539af8f9ade186e5250cba9e01926da514)
299
- - Add Line2D [`9a1dd0f`](https://github.com/Immugio/three-math-extensions/commit/9a1dd0f58352b7b25828693c688aa4770e95c174)
300
325
  - Add isPointInPolygon [`a59eb4b`](https://github.com/Immugio/three-math-extensions/commit/a59eb4be026f17a3106070ae626a0588cd4f4411)
301
326
  - Improve Line2D.closestPointToPoint, add Vec2.signedAngle [`151f214`](https://github.com/Immugio/three-math-extensions/commit/151f21462e0358057ad8e9d75d5782563a1061f6)
302
327
  - Improve documentation [`d0fcb51`](https://github.com/Immugio/three-math-extensions/commit/d0fcb5132f127b4382ac5f7291575a061b8ec121)
@@ -310,7 +335,6 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
310
335
  - Add Vec2.signedAngle [`863c8f2`](https://github.com/Immugio/three-math-extensions/commit/863c8f27f11288cbda535e21bb688206259269ed)
311
336
  - Add intersect method to Line3D [`6fe47de`](https://github.com/Immugio/three-math-extensions/commit/6fe47de7caaa1807b47a4363e551510c463757d7)
312
337
  - Add Line2D.extendToOrTrimAtIntersection [`ab82a36`](https://github.com/Immugio/three-math-extensions/commit/ab82a36db52b9ae83cf8bfb848362d295140d073)
313
- - Readme, npm info [`dda4a28`](https://github.com/Immugio/three-math-extensions/commit/dda4a282a71a00308dcae858ffe53d67d4185be8)
314
338
  - Add Vec2.fromPoints to accept multiple points [`a261402`](https://github.com/Immugio/three-math-extensions/commit/a2614027cf5fb8263189b48f7e0bb9a23a552c15)
315
339
  - Improve documentation for Vec2 and Vec3 [`6a2373d`](https://github.com/Immugio/three-math-extensions/commit/6a2373d8b6754a87720dcaea5d98336bfa7bc5b5)
316
340
  - Add Vec2.parallelTo [`989874d`](https://github.com/Immugio/three-math-extensions/commit/989874dcfe122d3ee84d8d56d79cb88e4e441736)
@@ -322,11 +346,8 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
322
346
  - Add .npmignore [`f58329a`](https://github.com/Immugio/three-math-extensions/commit/f58329a86b96589bb574d6ebed37d4cc4474663a)
323
347
  - Polygon from bounding size [`eae6701`](https://github.com/Immugio/three-math-extensions/commit/eae67012f57f426f8b5259b765000447ce06d608)
324
348
  - Add Vec2 documentation [`f625e66`](https://github.com/Immugio/three-math-extensions/commit/f625e66b60ee0d90c5c788a80989f64013d60265)
325
- - Run tests in CI on commit [`e3f77ca`](https://github.com/Immugio/three-math-extensions/commit/e3f77ca76e25f4d99eef0130e5779b5e7c5aec6b)
326
349
  - Clean up documentation [`70c8d5e`](https://github.com/Immugio/three-math-extensions/commit/70c8d5efe8b7095d7a03af637df3af5d46615293)
327
- - Remove babel.config.js [`c9a1e16`](https://github.com/Immugio/three-math-extensions/commit/c9a1e1607cffec8a3d74adcca644320a79ca4c43)
328
350
  - Line2D and Line3D to return this instead of specific type [`761ef6a`](https://github.com/Immugio/three-math-extensions/commit/761ef6a9d8cc4e35120b666576794e521aa3b991)
329
- - Initial changelog [`6c98aa6`](https://github.com/Immugio/three-math-extensions/commit/6c98aa6ad631cf5e73d32dd76276f99b3ba5089f)
330
351
  - Line2D.in3DSpace added [`a6ce0ec`](https://github.com/Immugio/three-math-extensions/commit/a6ce0ecb67f5c7b2a75fcc283c28af626153a4af)
331
352
  - Line3D.connectsTo added [`6d2cfa0`](https://github.com/Immugio/three-math-extensions/commit/6d2cfa0f5335c665f325a694a32c57b574ec326d)
332
353
  - Line2D.hasIntersectionWithAngle to support optional tolerance [`b313bbe`](https://github.com/Immugio/three-math-extensions/commit/b313bbe118d435d53750deefd9a9e29ba6ec5c71)
@@ -335,21 +356,36 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
335
356
  - Use Vec2 instead of Vector2 [`7e6a6ea`](https://github.com/Immugio/three-math-extensions/commit/7e6a6ea272f4441ef4bc78b3fdec23fc783fa1db)
336
357
  - Add release instructions into README.md [`03653b1`](https://github.com/Immugio/three-math-extensions/commit/03653b1ffa55be606d3f9cd588e28a6084462c2e)
337
358
  - Documentation improvements [`160db8b`](https://github.com/Immugio/three-math-extensions/commit/160db8ba6d6e5eb63a4e91ed9c40efd6fa70181a)
338
- - Release 0.0.4 [`bdac840`](https://github.com/Immugio/three-math-extensions/commit/bdac840f33261a3f39ef33bc05c80772169cfe87)
339
359
  - Update release instructions [`5b41a2e`](https://github.com/Immugio/three-math-extensions/commit/5b41a2ed7e15450dbb6088a7f7ed0031a013badc)
340
- - Add test badge [`37c62e8`](https://github.com/Immugio/three-math-extensions/commit/37c62e809bdcdfc63f7fe135469fd131f190f950)
341
360
  - Add Polygon to exports [`ed66775`](https://github.com/Immugio/three-math-extensions/commit/ed66775c33e961835b23843222b822cfd9c16b1d)
342
361
  - Vec2.fromPoint and Vec3.fromPoint should accept null [`4b871af`](https://github.com/Immugio/three-math-extensions/commit/4b871af297bdcbe8584f1e2b99d602247b77687c)
343
362
  - Documentation improvements [`2f3e676`](https://github.com/Immugio/three-math-extensions/commit/2f3e6768b3b0139b60688fc151a4084f15566f8d)
344
- - Edit publish script condition [`dc77d89`](https://github.com/Immugio/three-math-extensions/commit/dc77d8978d87daab8732d6429ad1b01ba0ca6f97)
345
- - CI to use node version 16 [`dc42650`](https://github.com/Immugio/three-math-extensions/commit/dc426508939de922cc0316b7652bbd09aeed4610)
346
- - Action rename [`26a1f01`](https://github.com/Immugio/three-math-extensions/commit/26a1f014827faab2c02d30ca8ed18dac8f9ff8af)
347
363
  - Documentation update [`d5c7a07`](https://github.com/Immugio/three-math-extensions/commit/d5c7a0765f6097f5d3a3be01967d4059f19682fb)
348
364
  - Fix peer dependency version [`00dd116`](https://github.com/Immugio/three-math-extensions/commit/00dd1169f578d5769207031fa625f29c96a38c31)
349
365
  - Add Line2D.extendToOrTrimAtIntersection [`b22aa12`](https://github.com/Immugio/three-math-extensions/commit/b22aa120b5eb02562cebc4573374ebb61e2dba14)
350
366
  - Correct change log version [`c6244bf`](https://github.com/Immugio/three-math-extensions/commit/c6244bf1488ad21bcc5589d1dff62c41d8182d48)
351
367
  - Release 0.0.4 - tag pattern 6 [`c1c5454`](https://github.com/Immugio/three-math-extensions/commit/c1c54541ed400e1ad49bf42fe8926cd5293efefd)
352
368
  - Release 0.0.4 - tag pattern 5 [`5245eed`](https://github.com/Immugio/three-math-extensions/commit/5245eed60195103e989c0fadf9bd642f39ef4589)
369
+ - Excluded files from build [`ec70614`](https://github.com/Immugio/three-math-extensions/commit/ec70614bc7df7a98f854c7a6693365118e04faf7)
370
+ - Revert npm ignore [`590b26a`](https://github.com/Immugio/three-math-extensions/commit/590b26a9e57fc41466b51e711f3f5c648e2d56ed)
371
+
372
+ ## 0.0.4 - 2022-11-21
373
+
374
+ ### Commits
375
+
376
+ - Automatic publish with change log [`a70c99d`](https://github.com/Immugio/three-math-extensions/commit/a70c99d0d42e60411ce01bcdb8dd4f3f489bb949)
377
+ - Switch to npm [`ed47a17`](https://github.com/Immugio/three-math-extensions/commit/ed47a1787522db3bde5a2112bfd02480fe2edc54)
378
+ - Initial commit [`3d1bf9e`](https://github.com/Immugio/three-math-extensions/commit/3d1bf9ef015570830007c9be99140c8c8d760d7f)
379
+ - Add Line2D [`9a1dd0f`](https://github.com/Immugio/three-math-extensions/commit/9a1dd0f58352b7b25828693c688aa4770e95c174)
380
+ - Readme, npm info [`dda4a28`](https://github.com/Immugio/three-math-extensions/commit/dda4a282a71a00308dcae858ffe53d67d4185be8)
381
+ - Run tests in CI on commit [`e3f77ca`](https://github.com/Immugio/three-math-extensions/commit/e3f77ca76e25f4d99eef0130e5779b5e7c5aec6b)
382
+ - Remove babel.config.js [`c9a1e16`](https://github.com/Immugio/three-math-extensions/commit/c9a1e1607cffec8a3d74adcca644320a79ca4c43)
383
+ - Initial changelog [`6c98aa6`](https://github.com/Immugio/three-math-extensions/commit/6c98aa6ad631cf5e73d32dd76276f99b3ba5089f)
384
+ - Release 0.0.4 [`bdac840`](https://github.com/Immugio/three-math-extensions/commit/bdac840f33261a3f39ef33bc05c80772169cfe87)
385
+ - Add test badge [`37c62e8`](https://github.com/Immugio/three-math-extensions/commit/37c62e809bdcdfc63f7fe135469fd131f190f950)
386
+ - Edit publish script condition [`dc77d89`](https://github.com/Immugio/three-math-extensions/commit/dc77d8978d87daab8732d6429ad1b01ba0ca6f97)
387
+ - CI to use node version 16 [`dc42650`](https://github.com/Immugio/three-math-extensions/commit/dc426508939de922cc0316b7652bbd09aeed4610)
388
+ - Action rename [`26a1f01`](https://github.com/Immugio/three-math-extensions/commit/26a1f014827faab2c02d30ca8ed18dac8f9ff8af)
353
389
  - Release 0.0.4 - tag pattern 4 [`ab9ad4e`](https://github.com/Immugio/three-math-extensions/commit/ab9ad4e49477a991482ed6d4d3a3ca4687a70b44)
354
390
  - Release 0.0.4 - tag pattern 3 [`d7d4c8f`](https://github.com/Immugio/three-math-extensions/commit/d7d4c8f546e6550868ddd06a5213fdec6c68bcd2)
355
391
  - Release 0.0.4 - tag pattern 2 [`0dcf801`](https://github.com/Immugio/three-math-extensions/commit/0dcf80190bd67a3ca93d45131a403201ab39e00f)
@@ -357,6 +393,4 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
357
393
  - Release 0.0.5 [`c80d6bd`](https://github.com/Immugio/three-math-extensions/commit/c80d6bd12c25ddad0f67610cdf5c804d5759f084)
358
394
  - Status badge update [`370843e`](https://github.com/Immugio/three-math-extensions/commit/370843ed0a28b024761555572b7d51ccbdb4b77d)
359
395
  - Run tests in CI on commit [`aee8c55`](https://github.com/Immugio/three-math-extensions/commit/aee8c556ae1cab9025dbe0984dd17278fe6275a1)
360
- - Excluded files from build [`ec70614`](https://github.com/Immugio/three-math-extensions/commit/ec70614bc7df7a98f854c7a6693365118e04faf7)
361
- - Revert npm ignore [`590b26a`](https://github.com/Immugio/three-math-extensions/commit/590b26a9e57fc41466b51e711f3f5c648e2d56ed)
362
396
  - Add Line2D to index [`a5fb6bd`](https://github.com/Immugio/three-math-extensions/commit/a5fb6bdeee5d9f07f325bfffc31ef96f0ce167d1)
package/cjs/Line2D.js CHANGED
@@ -6,6 +6,7 @@ const Vec2_1 = require("./Vec2");
6
6
  const MathConstants_1 = require("./MathConstants");
7
7
  const Line3D_1 = require("./Line3D");
8
8
  const directions2d_1 = require("./directions2d");
9
+ const sortLinesByConnections_1 = require("./sortLinesByConnections");
9
10
  const _startP = /*@__PURE__*/ new Vec2_1.Vec2();
10
11
  const _startEnd = /*@__PURE__*/ new Vec2_1.Vec2();
11
12
  class Line2D {
@@ -682,21 +683,9 @@ class Line2D {
682
683
  if (!visited.has(line)) {
683
684
  const group = [];
684
685
  dfs(line, group);
685
- connectedLines.push(group);
686
+ connectedLines.push((0, sortLinesByConnections_1.sortLinesByConnections)(group, 1));
686
687
  }
687
688
  });
688
- // Sort each group based on connection order
689
- connectedLines.forEach(group => {
690
- group.sort((a, b) => {
691
- if (a.start.isNear(b.start, tolerance) || a.end.isNear(b.start, tolerance)) {
692
- return -1;
693
- }
694
- if (a.start.isNear(b.end, tolerance) || a.end.isNear(b.end, tolerance)) {
695
- return 1;
696
- }
697
- return 0;
698
- });
699
- });
700
689
  return connectedLines;
701
690
  }
702
691
  /**
package/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extendOrTrimPolylinesAtIntersections = exports.offsetPolyline = exports.polygonPerimeter = exports.isContinuousClosedShape = exports.directions2d = exports.directions = exports.isPointInPolygon = exports.HalfPI = exports.TwoPI = exports.normalizeAngleRadians = exports.normalizeAngleDegrees = exports.Rectangle = exports.BoundingBox = exports.Polygon = exports.Size2 = exports.Line3D = exports.Line2D = exports.Vec3 = exports.Vec2 = void 0;
3
+ exports.sortLinesByConnections = exports.extendOrTrimPolylinesAtIntersections = exports.offsetPolyline = exports.polygonPerimeter = exports.isContinuousClosedShape = exports.directions2d = exports.directions = exports.isPointInPolygon = exports.HalfPI = exports.TwoPI = exports.normalizeAngleRadians = exports.normalizeAngleDegrees = exports.Rectangle = exports.BoundingBox = exports.Polygon = exports.Size2 = exports.Line3D = exports.Line2D = exports.Vec3 = exports.Vec2 = void 0;
4
4
  var Vec2_1 = require("./Vec2");
5
5
  Object.defineProperty(exports, "Vec2", { enumerable: true, get: function () { return Vec2_1.Vec2; } });
6
6
  var Vec3_1 = require("./Vec3");
@@ -38,3 +38,5 @@ var offsetPolyline_1 = require("./offsetPolyline");
38
38
  Object.defineProperty(exports, "offsetPolyline", { enumerable: true, get: function () { return offsetPolyline_1.offsetPolyline; } });
39
39
  var extendOrTrimPolylinesAtIntersections_1 = require("./extendOrTrimPolylinesAtIntersections");
40
40
  Object.defineProperty(exports, "extendOrTrimPolylinesAtIntersections", { enumerable: true, get: function () { return extendOrTrimPolylinesAtIntersections_1.extendOrTrimPolylinesAtIntersections; } });
41
+ var sortLinesByConnections_1 = require("./sortLinesByConnections");
42
+ Object.defineProperty(exports, "sortLinesByConnections", { enumerable: true, get: function () { return sortLinesByConnections_1.sortLinesByConnections; } });
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sortLinesByConnections = void 0;
4
+ /**
5
+ * Sort connected lines by their connections.
6
+ * When the polygon is open, the first line must be the line that has no connection at the start.
7
+ * When the polygon is open, the last line must be the line that has no connection at the end.
8
+ * If the lines form a closed polygon, any line can be the first line.
9
+ */
10
+ function sortLinesByConnections(lines, tolerance = 0) {
11
+ const remainingLines = [...lines];
12
+ const startLineIndex = findStartLineIndex(remainingLines, tolerance);
13
+ const sortedLines = [remainingLines.splice(startLineIndex, 1)[0]];
14
+ while (remainingLines.length > 0) {
15
+ const lastLine = sortedLines[sortedLines.length - 1];
16
+ const nextLineIndex = remainingLines.findIndex(line => lastLine.end.isNear(line.start, tolerance));
17
+ if (nextLineIndex === -1) {
18
+ console.log("Lines do not form a connected path");
19
+ return [...sortedLines, ...remainingLines];
20
+ }
21
+ sortedLines.push(remainingLines.splice(nextLineIndex, 1)[0]);
22
+ }
23
+ return sortedLines;
24
+ }
25
+ exports.sortLinesByConnections = sortLinesByConnections;
26
+ /**
27
+ * Find the index of the starting line.
28
+ * A starting line is defined as a line that has no other line connected to its start.
29
+ * If such a line does not exist, it means that the lines form a closed polygon, return 0.
30
+ * @param lines
31
+ * @param tolerance
32
+ */
33
+ function findStartLineIndex(lines, tolerance) {
34
+ for (let i = 0; i < lines.length; i++) {
35
+ const startLine = lines[i];
36
+ const isStartLine = !lines.some(line => line.end.isNear(startLine.start, tolerance));
37
+ if (isStartLine) {
38
+ return i;
39
+ }
40
+ }
41
+ return 0;
42
+ }
package/esm/Line2D.js CHANGED
@@ -3,6 +3,7 @@ import { Vec2 } from "./Vec2";
3
3
  import { TwoPI } from "./MathConstants";
4
4
  import { Line3D } from "./Line3D";
5
5
  import { directions2d } from "./directions2d";
6
+ import { sortLinesByConnections } from "./sortLinesByConnections";
6
7
  const _startP = /*@__PURE__*/ new Vec2();
7
8
  const _startEnd = /*@__PURE__*/ new Vec2();
8
9
  export class Line2D {
@@ -679,21 +680,9 @@ export class Line2D {
679
680
  if (!visited.has(line)) {
680
681
  const group = [];
681
682
  dfs(line, group);
682
- connectedLines.push(group);
683
+ connectedLines.push(sortLinesByConnections(group, 1));
683
684
  }
684
685
  });
685
- // Sort each group based on connection order
686
- connectedLines.forEach(group => {
687
- group.sort((a, b) => {
688
- if (a.start.isNear(b.start, tolerance) || a.end.isNear(b.start, tolerance)) {
689
- return -1;
690
- }
691
- if (a.start.isNear(b.end, tolerance) || a.end.isNear(b.end, tolerance)) {
692
- return 1;
693
- }
694
- return 0;
695
- });
696
- });
697
686
  return connectedLines;
698
687
  }
699
688
  /**
package/esm/index.js CHANGED
@@ -16,3 +16,4 @@ export { isContinuousClosedShape } from "./isContinuousClosedShape";
16
16
  export { polygonPerimeter } from "./polygonPerimeter";
17
17
  export { offsetPolyline } from "./offsetPolyline";
18
18
  export { extendOrTrimPolylinesAtIntersections } from "./extendOrTrimPolylinesAtIntersections";
19
+ export { sortLinesByConnections } from "./sortLinesByConnections";
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Sort connected lines by their connections.
3
+ * When the polygon is open, the first line must be the line that has no connection at the start.
4
+ * When the polygon is open, the last line must be the line that has no connection at the end.
5
+ * If the lines form a closed polygon, any line can be the first line.
6
+ */
7
+ export function sortLinesByConnections(lines, tolerance = 0) {
8
+ const remainingLines = [...lines];
9
+ const startLineIndex = findStartLineIndex(remainingLines, tolerance);
10
+ const sortedLines = [remainingLines.splice(startLineIndex, 1)[0]];
11
+ while (remainingLines.length > 0) {
12
+ const lastLine = sortedLines[sortedLines.length - 1];
13
+ const nextLineIndex = remainingLines.findIndex(line => lastLine.end.isNear(line.start, tolerance));
14
+ if (nextLineIndex === -1) {
15
+ console.log("Lines do not form a connected path");
16
+ return [...sortedLines, ...remainingLines];
17
+ }
18
+ sortedLines.push(remainingLines.splice(nextLineIndex, 1)[0]);
19
+ }
20
+ return sortedLines;
21
+ }
22
+ /**
23
+ * Find the index of the starting line.
24
+ * A starting line is defined as a line that has no other line connected to its start.
25
+ * If such a line does not exist, it means that the lines form a closed polygon, return 0.
26
+ * @param lines
27
+ * @param tolerance
28
+ */
29
+ function findStartLineIndex(lines, tolerance) {
30
+ for (let i = 0; i < lines.length; i++) {
31
+ const startLine = lines[i];
32
+ const isStartLine = !lines.some(line => line.end.isNear(startLine.start, tolerance));
33
+ if (isStartLine) {
34
+ return i;
35
+ }
36
+ }
37
+ return 0;
38
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@immugio/three-math-extensions",
3
- "version": "0.2.32",
3
+ "version": "0.2.33",
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
@@ -4,6 +4,7 @@ import { Vec2 } from "./Vec2";
4
4
  import { TwoPI } from "./MathConstants";
5
5
  import { Line3D } from "./Line3D";
6
6
  import { directions2d } from "./directions2d";
7
+ import { sortLinesByConnections } from "./sortLinesByConnections";
7
8
 
8
9
  const _startP = /*@__PURE__*/ new Vec2();
9
10
  const _startEnd = /*@__PURE__*/ new Vec2();
@@ -803,23 +804,10 @@ export class Line2D {
803
804
  if (!visited.has(line)) {
804
805
  const group: Line2D[] = [];
805
806
  dfs(line, group);
806
- connectedLines.push(group);
807
+ connectedLines.push(sortLinesByConnections(group, 1));
807
808
  }
808
809
  });
809
810
 
810
- // Sort each group based on connection order
811
- connectedLines.forEach(group => {
812
- group.sort((a, b) => {
813
- if (a.start.isNear(b.start, tolerance) || a.end.isNear(b.start, tolerance)) {
814
- return -1;
815
- }
816
- if (a.start.isNear(b.end, tolerance) || a.end.isNear(b.end, tolerance)) {
817
- return 1;
818
- }
819
- return 0;
820
- });
821
- });
822
-
823
811
  return connectedLines;
824
812
  }
825
813
 
package/src/index.ts CHANGED
@@ -17,4 +17,5 @@ export { directions2d } from "./directions2d";
17
17
  export { isContinuousClosedShape } from "./isContinuousClosedShape";
18
18
  export { polygonPerimeter } from "./polygonPerimeter";
19
19
  export { offsetPolyline } from "./offsetPolyline";
20
- export { extendOrTrimPolylinesAtIntersections } from "./extendOrTrimPolylinesAtIntersections";
20
+ export { extendOrTrimPolylinesAtIntersections } from "./extendOrTrimPolylinesAtIntersections";
21
+ export { sortLinesByConnections } from "./sortLinesByConnections";
@@ -0,0 +1,46 @@
1
+ import { Line2D } from "./Line2D";
2
+
3
+ /**
4
+ * Sort connected lines by their connections.
5
+ * When the polygon is open, the first line must be the line that has no connection at the start.
6
+ * When the polygon is open, the last line must be the line that has no connection at the end.
7
+ * If the lines form a closed polygon, any line can be the first line.
8
+ */
9
+ export function sortLinesByConnections(lines: Line2D[], tolerance: number = 0): Line2D[] {
10
+ const remainingLines = [...lines];
11
+ const startLineIndex = findStartLineIndex(remainingLines, tolerance);
12
+
13
+ const sortedLines: Line2D[] = [remainingLines.splice(startLineIndex, 1)[0]];
14
+
15
+ while (remainingLines.length > 0) {
16
+ const lastLine = sortedLines[sortedLines.length - 1];
17
+ const nextLineIndex = remainingLines.findIndex(line => lastLine.end.isNear(line.start, tolerance));
18
+
19
+ if (nextLineIndex === -1) {
20
+ console.log("Lines do not form a connected path");
21
+ return [...sortedLines, ...remainingLines];
22
+ }
23
+
24
+ sortedLines.push(remainingLines.splice(nextLineIndex, 1)[0]);
25
+ }
26
+
27
+ return sortedLines;
28
+ }
29
+
30
+ /**
31
+ * Find the index of the starting line.
32
+ * A starting line is defined as a line that has no other line connected to its start.
33
+ * If such a line does not exist, it means that the lines form a closed polygon, return 0.
34
+ * @param lines
35
+ * @param tolerance
36
+ */
37
+ function findStartLineIndex(lines: Line2D[], tolerance: number): number {
38
+ for (let i = 0; i < lines.length; i++) {
39
+ const startLine = lines[i];
40
+ const isStartLine = !lines.some(line => line.end.isNear(startLine.start, tolerance));
41
+ if (isStartLine) {
42
+ return i;
43
+ }
44
+ }
45
+ return 0;
46
+ }
package/types/index.d.ts CHANGED
@@ -18,3 +18,4 @@ export { isContinuousClosedShape } from "./isContinuousClosedShape";
18
18
  export { polygonPerimeter } from "./polygonPerimeter";
19
19
  export { offsetPolyline } from "./offsetPolyline";
20
20
  export { extendOrTrimPolylinesAtIntersections } from "./extendOrTrimPolylinesAtIntersections";
21
+ export { sortLinesByConnections } from "./sortLinesByConnections";
@@ -0,0 +1,8 @@
1
+ import { Line2D } from "./Line2D";
2
+ /**
3
+ * Sort connected lines by their connections.
4
+ * When the polygon is open, the first line must be the line that has no connection at the start.
5
+ * When the polygon is open, the last line must be the line that has no connection at the end.
6
+ * If the lines form a closed polygon, any line can be the first line.
7
+ */
8
+ export declare function sortLinesByConnections(lines: Line2D[], tolerance?: number): Line2D[];