@maplat/tin 0.11.0 → 0.11.2

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/kinks.ts CHANGED
@@ -1,9 +1,10 @@
1
- import { Position } from "geojson";
1
+ import type { Feature, Point, Position } from "geojson";
2
2
  import { point } from "@turf/helpers";
3
+ import type { IntersectionPoint } from "./types/kinks.d.ts";
3
4
 
4
5
  /**
5
6
  * 線分の交差点を検出するメインの関数
6
- *
7
+ *
7
8
  * @param coords - 線分群の座標配列。各線分は始点と終点の座標で表現
8
9
  * @returns 検出された交差点のFeature配列
9
10
  */
@@ -11,14 +12,20 @@ export default function findIntersections(coords: Position[][]) {
11
12
  const arcs = new ArcCollection(coords);
12
13
  const xy = arcs.findSegmentIntersections();
13
14
  return dedupIntersections(xy).reduce(
14
- (prev: any, apoint: any, index: any, array: any) => {
15
+ (
16
+ prev: Record<string, IntersectionPoint> | Feature<Point>[],
17
+ apoint: IntersectionPoint,
18
+ index: number,
19
+ array: IntersectionPoint[],
20
+ ) => {
21
+ if (Array.isArray(prev)) return prev;
15
22
  if (!prev) prev = {};
16
23
  prev[`${apoint.x}:${apoint.y}`] = apoint;
17
24
  if (index != array.length - 1) return prev;
18
- return Object.keys(prev).map(key => point([prev[key].x, prev[key].y]));
25
+ return Object.keys(prev).map((key) => point([prev[key].x, prev[key].y]));
19
26
  },
20
- []
21
- );
27
+ {} as Record<string, IntersectionPoint>,
28
+ ) as Feature<Point>[];
22
29
  }
23
30
 
24
31
  /**
@@ -55,7 +62,7 @@ class ArcCollection {
55
62
  initArcs(arcs: Position[][]) {
56
63
  const xx: number[] = [],
57
64
  yy: number[] = [];
58
- const nn = arcs.map(points => {
65
+ const nn = arcs.map((points) => {
59
66
  const n = points ? points.length : 0;
60
67
  for (let i = 0; i < n; i++) {
61
68
  xx.push(points[i][0]);
@@ -125,7 +132,7 @@ class ArcCollection {
125
132
  }
126
133
  return {
127
134
  bb,
128
- bounds
135
+ bounds,
129
136
  };
130
137
  }
131
138
 
@@ -180,7 +187,7 @@ class ArcCollection {
180
187
  zz: this._zz,
181
188
  bb: this._bb,
182
189
  nn: this._nn,
183
- ii: this._ii
190
+ ii: this._ii,
184
191
  };
185
192
  }
186
193
 
@@ -200,7 +207,7 @@ class ArcCollection {
200
207
  (i: number, j: number, xx: number[], yy: number[]) => {
201
208
  dx += Math.abs(xx[i] - xx[j]);
202
209
  dy += Math.abs(yy[i] - yy[j]);
203
- }
210
+ },
204
211
  );
205
212
  return [dx / count || 0, dy / count || 0];
206
213
  }
@@ -222,7 +229,7 @@ class ArcCollection {
222
229
  /**
223
230
  * 線分の交差を検出
224
231
  * ストライプ分割による効率的な判定を実装
225
- *
232
+ *
226
233
  * @returns 検出された交差点の配列
227
234
  */
228
235
  findSegmentIntersections() {
@@ -231,10 +238,9 @@ class ArcCollection {
231
238
  yrange = bounds.ymax - ymin,
232
239
  stripeCount = this.calcSegmentIntersectionStripeCount(),
233
240
  stripeSizes = new Uint32Array(stripeCount),
234
- stripeId =
235
- stripeCount > 1
236
- ? (y: number) => Math.floor(((stripeCount - 1) * (y - ymin)) / yrange)
237
- : () => 0;
241
+ stripeId = stripeCount > 1
242
+ ? (y: number) => Math.floor(((stripeCount - 1) * (y - ymin)) / yrange)
243
+ : () => 0;
238
244
  let i, j;
239
245
 
240
246
  // Count segments in each stripe
@@ -247,7 +253,7 @@ class ArcCollection {
247
253
  if (s1 == s2) break;
248
254
  s1 += s2 > s1 ? 1 : -1;
249
255
  }
250
- }
256
+ },
251
257
  );
252
258
 
253
259
  // Allocate arrays for segments in each stripe
@@ -276,7 +282,7 @@ class ArcCollection {
276
282
  if (s1 == s2) break;
277
283
  s1 += s2 > s1 ? 1 : -1;
278
284
  }
279
- }
285
+ },
280
286
  );
281
287
 
282
288
  // Detect intersections among segments in each stripe.
@@ -432,7 +438,7 @@ function intersectSegments(ids: any, xx: any, yy: any) {
432
438
  s2p1x,
433
439
  s2p1y,
434
440
  s2p2x,
435
- s2p2y
441
+ s2p2y,
436
442
  );
437
443
  if (hit) {
438
444
  seg1 = [s1p1, s1p2];
@@ -441,7 +447,7 @@ function intersectSegments(ids: any, xx: any, yy: any) {
441
447
  if (hit.length == 4) {
442
448
  // two collinear segments may have two endpoint intersections
443
449
  intersections.push(
444
- formatIntersection(hit.slice(2), seg1, seg2, xx, yy)
450
+ formatIntersection(hit.slice(2), seg1, seg2, xx, yy),
445
451
  );
446
452
  }
447
453
  }
@@ -453,17 +459,17 @@ function intersectSegments(ids: any, xx: any, yy: any) {
453
459
 
454
460
  /**
455
461
  * 2つの2D線分間の交差を判定し、交差点を計算します
456
- *
462
+ *
457
463
  * @param ax, ay - 第1線分の始点座標
458
464
  * @param bx, by - 第1線分の終点座標
459
465
  * @param cx, cy - 第2線分の始点座標
460
466
  * @param dx, dy - 第2線分の終点座標
461
- *
467
+ *
462
468
  * @returns 以下のいずれか:
463
469
  * - null: 交差なし
464
470
  * - [x, y]: 1点での交差
465
471
  * - [x1, y1, x2, y2]: 線分が重なる場合の2点での交差
466
- *
472
+ *
467
473
  * 特殊ケースの扱い:
468
474
  * 1. 両線分の端点で接触する場合 → 交差としない
469
475
  * 2. T字型に接触する場合 → 交差として扱う
@@ -479,7 +485,7 @@ function segmentIntersection(
479
485
  cx: any,
480
486
  cy: any,
481
487
  dx: any,
482
- dy: any
488
+ dy: any,
483
489
  ) {
484
490
  const hit = segmentHit(ax, ay, bx, by, cx, cy, dx, dy);
485
491
  let p = null;
@@ -505,7 +511,7 @@ function segmentHit(
505
511
  cx: any,
506
512
  cy: any,
507
513
  dx: any,
508
- dy: any
514
+ dy: any,
509
515
  ) {
510
516
  return (
511
517
  orient2D(ax, ay, bx, by, cx, cy) * orient2D(ax, ay, bx, by, dx, dy) <= 0 &&
@@ -523,7 +529,7 @@ function orient2D(ax: any, ay: any, bx: any, by: any, cx: any, cy: any) {
523
529
 
524
530
  /**
525
531
  * 2次元の行列式を計算
526
- *
532
+ *
527
533
  * @param a, b - 行列の第1行
528
534
  * @param c, d - 行列の第2行
529
535
  * @returns 行列式の値
@@ -542,7 +548,7 @@ function crossIntersection(
542
548
  cx: any,
543
549
  cy: any,
544
550
  dx: any,
545
- dy: any
551
+ dy: any,
546
552
  ) {
547
553
  let p = lineIntersection(ax, ay, bx, by, cx, cy, dx, dy);
548
554
  let nearest;
@@ -572,7 +578,7 @@ function lineIntersection(
572
578
  cx: any,
573
579
  cy: any,
574
580
  dx: any,
575
- dy: any
581
+ dy: any,
576
582
  ) {
577
583
  const den = determinant2D(bx - ax, by - ay, dx - cx, dy - cy);
578
584
  const eps = 1e-18;
@@ -600,7 +606,7 @@ function findEndpointInRange(
600
606
  cx: any,
601
607
  cy: any,
602
608
  dx: any,
603
- dy: any
609
+ dy: any,
604
610
  ) {
605
611
  let p = null;
606
612
  if (!outsideRange(ax, cx, dx) && !outsideRange(ay, cy, dy)) {
@@ -617,7 +623,7 @@ function findEndpointInRange(
617
623
 
618
624
  /**
619
625
  * 点が線分の範囲外にあるかを判定
620
- *
626
+ *
621
627
  * @param a - 判定する点の座標
622
628
  * @param b - 線分の一方の端点
623
629
  * @param c - 線分のもう一方の端点
@@ -664,7 +670,7 @@ function clampIntersectionPoint(
664
670
  cx: any,
665
671
  cy: any,
666
672
  dx: any,
667
- dy: any
673
+ dy: any,
668
674
  ) {
669
675
  // Handle intersection points that fall outside the x-y range of either
670
676
  // segment by snapping to nearest endpoint coordinate. Out-of-range
@@ -703,7 +709,7 @@ function collinearIntersection(
703
709
  cx: any,
704
710
  cy: any,
705
711
  dx: any,
706
- dy: any
712
+ dy: any,
707
713
  ) {
708
714
  const minX = Math.min(ax, bx, cx, dx),
709
715
  maxX = Math.max(ax, bx, cx, dx),
@@ -746,7 +752,7 @@ function endpointHit(
746
752
  cx: any,
747
753
  cy: any,
748
754
  dx: any,
749
- dy: any
755
+ dy: any,
750
756
  ) {
751
757
  return (
752
758
  (ax == cx && ay == cy) ||
@@ -846,7 +852,7 @@ function formatIntersectingSegment(
846
852
  id1: any,
847
853
  id2: any,
848
854
  xx: any,
849
- yy: any
855
+ yy: any,
850
856
  ) {
851
857
  let i = id1 < id2 ? id1 : id2,
852
858
  j = i === id1 ? id2 : id1;
@@ -860,7 +866,7 @@ function formatIntersectingSegment(
860
866
 
861
867
  /**
862
868
  * 交差判定結果から重複を除去
863
- *
869
+ *
864
870
  * @param arr - 交差点情報の配列
865
871
  * @returns 重複を除去した交差点情報の配列
866
872
  */
@@ -904,7 +910,7 @@ function calcArcBounds(
904
910
  xx: Float64Array,
905
911
  yy: Float64Array,
906
912
  start: number,
907
- len: number
913
+ len: number,
908
914
  ) {
909
915
  let i = start | 0;
910
916
  const n = isNaN(len) ? xx.length - i : len + i;
@@ -1,4 +1,4 @@
1
- import type { Tri, Tins } from "@maplat/transform";
1
+ import type { Tins, Tri } from "@maplat/transform";
2
2
  import type { PropertyTriKey } from "@maplat/transform";
3
3
 
4
4
  type BiDirectionKey = "forw" | "bakw";
@@ -9,23 +9,23 @@ export type SearchIndex = { [key: string]: SearchTris[] };
9
9
  /**
10
10
  * 三角形のインデックスキーを計算する
11
11
  * 三つの頂点のインデックスから、ソートされたキー配列を生成
12
- *
12
+ *
13
13
  * @param tri インデックスキーを計算する三角形
14
14
  * @returns ソートされたキー配列
15
15
  */
16
16
  function calcSearchKeys(tri: Tri): string[] {
17
17
  const vtx = (["a", "b", "c"] as PropertyTriKey[]).map(
18
- key => tri.properties![key].index
18
+ (key) => tri.properties![key].index,
19
19
  );
20
20
  return [
21
21
  [0, 1],
22
22
  [0, 2],
23
23
  [1, 2],
24
- [0, 1, 2]
24
+ [0, 1, 2],
25
25
  ]
26
- .map(set =>
26
+ .map((set) =>
27
27
  set
28
- .map(i => vtx[i])
28
+ .map((i) => vtx[i])
29
29
  .sort()
30
30
  .join("-")
31
31
  )
@@ -34,7 +34,7 @@ function calcSearchKeys(tri: Tri): string[] {
34
34
 
35
35
  /**
36
36
  * 検索インデックスに三角形を追加する
37
- *
37
+ *
38
38
  * @param searchIndex 検索インデックス
39
39
  * @param tris 追加する三角形ペア
40
40
  * @param tins (オプション)追加先の三角形群
@@ -43,14 +43,17 @@ function calcSearchKeys(tri: Tri): string[] {
43
43
  function insertSearchIndex(
44
44
  searchIndex: SearchIndex,
45
45
  tris: SearchTris,
46
- tins?: TinsBD
46
+ tins?: TinsBD,
47
47
  ) {
48
48
  const keys = calcSearchKeys(tris.forw);
49
49
  const bakKeys = calcSearchKeys(tris.bakw);
50
- if (JSON.stringify(keys) != JSON.stringify(bakKeys))
51
- throw `${JSON.stringify(tris, null, 2)}\n${JSON.stringify(
52
- keys
53
- )}\n${JSON.stringify(bakKeys)}`;
50
+ if (JSON.stringify(keys) != JSON.stringify(bakKeys)) {
51
+ throw `${JSON.stringify(tris, null, 2)}\n${
52
+ JSON.stringify(
53
+ keys,
54
+ )
55
+ }\n${JSON.stringify(bakKeys)}`;
56
+ }
54
57
  for (let i = 0; i < keys.length; i++) {
55
58
  const key = keys[i];
56
59
  if (!searchIndex[key]) searchIndex[key] = [];
@@ -62,6 +65,4 @@ function insertSearchIndex(
62
65
  }
63
66
  }
64
67
 
65
- export {
66
- insertSearchIndex
67
- };
68
+ export { insertSearchIndex };