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