@maplat/tin 1.0.0-rc1 → 1.1.0-rc.1

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.
@@ -7,6 +7,12 @@ import { insertSearchIndex, removeSearchIndex } from "./searchutils.ts";
7
7
 
8
8
  type ConstraintEdges = number[][];
9
9
 
10
+ /**
11
+ * pointsSet 内の配列位置 → 頂点 index(properties.target.index)の対応表。
12
+ * GCP は数値、エッジ中間ノードは "e12"、境界頂点は "b3" のような文字列。
13
+ */
14
+ export type PointIndices = readonly (number | string)[];
15
+
10
16
  /** Matches the shape of PropertyTri in @maplat/transform (not publicly exported). */
11
17
  type VertexInfo = {
12
18
  prop: { index: number | string; geom: Position };
@@ -35,20 +41,35 @@ function isDegenerate(triangle: [number, number][], epsilon = 1e-6): boolean {
35
41
  return doubleArea < epsilon;
36
42
  }
37
43
 
38
- function isConstraintEdge(key: string, edges: ConstraintEdges): boolean {
39
- const parts = key.split("-");
40
- if (parts.length !== 2) return false;
41
- if (!parts.every((value) => /^-?\d+$/.test(value))) return false;
42
-
43
- const [a, b] = parts.map((value) => parseInt(value, 10)).sort((x, y) => x - y);
44
-
45
- return edges.some((edge) => {
46
- if (edge.length !== 2) return false;
47
- const parsed = edge.map((value) => parseInt(`${value}`, 10));
48
- if (parsed.some((val) => Number.isNaN(val))) return false;
49
- const sorted = parsed.sort((x, y) => x - y);
50
- return sorted[0] === a && sorted[1] === b;
44
+ /**
45
+ * 制約エッジを searchIndex のキー形式へ正規化した集合を作る。
46
+ *
47
+ * 二つの名前空間が混在している点に注意すること:
48
+ * - `edges` の要素は **pointsSet 内の配列位置**(数値)
49
+ * - searchIndex のキーは **頂点 index**(properties.target.index。
50
+ * GCP は数値だがエッジ中間ノードは "e58" のような文字列)
51
+ *
52
+ * GCP は「配列位置 == index」なので偶然一致するが、エッジ中間ノードは
53
+ * 配列位置と `e` の連番が別物であり、対応表なしでは一致しない。
54
+ * そのため pointIndices で配列位置を頂点 index へ引き直してからキー化する。
55
+ *
56
+ * キーの作り方は searchutils.ts の calcSearchKeys と厳密に揃える
57
+ * (String 化 → sort(既定の文字列比較)→ "-" 連結)。
58
+ */
59
+ export function buildConstraintEdgeKeys(
60
+ edges: ConstraintEdges,
61
+ pointIndices?: PointIndices,
62
+ ): Set<string> {
63
+ const keys = new Set<string>();
64
+ edges.forEach((edge) => {
65
+ if (edge.length !== 2) return;
66
+ const resolved = edge.map((position) => {
67
+ const index = pointIndices?.[position];
68
+ return `${index ?? position}`;
69
+ });
70
+ keys.add(resolved.sort().join("-"));
51
71
  });
72
+ return keys;
52
73
  }
53
74
 
54
75
  function extractVertices(tri: Tri) {
@@ -174,7 +195,9 @@ export function resolveOverlaps(
174
195
  tins: TinsBD,
175
196
  searchIndex: SearchIndex,
176
197
  edges: ConstraintEdges,
198
+ pointIndices?: PointIndices,
177
199
  ): boolean {
200
+ const constraintEdgeKeys = buildConstraintEdgeKeys(edges, pointIndices);
178
201
  const processedKeys = new Set<string>();
179
202
  let repaired = false;
180
203
 
@@ -191,7 +214,7 @@ export function resolveOverlaps(
191
214
  const sharedKeys = key.split("-");
192
215
  if (sharedKeys.length !== 2) continue;
193
216
 
194
- if (isConstraintEdge(key, edges)) continue;
217
+ if (constraintEdgeKeys.has(key)) continue;
195
218
 
196
219
  const tri0Bakw = extractVertices(trises[0].bakw);
197
220
  const tri1Bakw = extractVertices(trises[1].bakw);
package/src/tin.ts CHANGED
@@ -14,14 +14,16 @@ import {
14
14
  import type { Feature, Point, Polygon, Position } from "geojson";
15
15
  import {
16
16
  counterTri,
17
- format_version as FORMAT_VERSION_V2,
18
17
  normalizeEdges,
19
18
  rotateVerticesTriangle,
20
19
  Transform,
21
20
  transformArr,
22
21
  } from "@maplat/transform";
23
22
 
24
- const FORMAT_VERSION_V3 = 3.00000;
23
+ // Tin が書き出す compiled の版。2.00704 / 3.00001 で重みバッファを廃止(空 {} を書く)。
24
+ // @maplat/transform の publish 時期に依存させないため Tin が自前で持つ
25
+ export const FORMAT_VERSION_V2 = 2.00704;
26
+ export const FORMAT_VERSION_V3 = 3.00001;
25
27
  import type {
26
28
  Compiled,
27
29
  CompiledLegacy,
@@ -46,7 +48,6 @@ import {
46
48
  import findIntersections from "./kinks.ts";
47
49
  import { insertSearchIndex } from "./searchutils.ts";
48
50
  import { counterPoint, createPoint, vertexCalc } from "./vertexutils.ts";
49
- import { buildPointsWeightBuffer } from "./weight-buffer.ts";
50
51
  import { resolveOverlaps } from "./strict-overlap.ts";
51
52
  import type { SearchIndex } from "./searchutils.ts";
52
53
  import type { PointsSetBD, VertexPosition } from "./types/tin.d.ts";
@@ -182,7 +183,9 @@ export class Tin extends Transform {
182
183
  const compiled: Compiled = {} as Compiled;
183
184
  compiled.version = this.useV2Algorithm ? FORMAT_VERSION_V2 : FORMAT_VERSION_V3;
184
185
  compiled.points = this.points;
185
- compiled.weight_buffer = this.pointsWeightBuffer ?? {};
186
+ // 重みバッファは廃止(2.00704)。以前の @maplat/transform は compiled に
187
+ // 重みキーが無いと transform() で TypeError になるため、空オブジェクトを必ず書く
188
+ compiled.weight_buffer = {};
186
189
  compiled.centroid_point = [
187
190
  this.centroid!.forw!.geometry!.coordinates,
188
191
  this.centroid!.forw!.properties!.target.geom,
@@ -298,7 +301,7 @@ export class Tin extends Transform {
298
301
  /**
299
302
  * 厳密なTINを計算します
300
303
  */
301
- calcurateStrictTin(): void {
304
+ calculateStrictTin(): void {
302
305
  const bakTins = this.tins!.forw!.features.map((tri: Tri) =>
303
306
  counterTri(tri)
304
307
  );
@@ -310,10 +313,19 @@ export class Tin extends Transform {
310
313
  insertSearchIndex(searchIndex, { forw: forTri, bakw: bakTri });
311
314
  });
312
315
 
316
+ // pointsSet.edges の要素は pointsSet 内の「配列位置」だが、searchIndex の
317
+ // キーは頂点 index(properties.target.index)で作られている。
318
+ // エッジ中間ノードは配列位置と index("e58" 等)が一致しないため、
319
+ // 対応表を渡して resolveOverlaps 側でキーを揃えさせる。
320
+ const pointIndices = (this.pointsSet?.forw.features ?? []).map(
321
+ (point: Feature<Point>) => point.properties!.target.index as number | string,
322
+ );
323
+
313
324
  resolveOverlaps(
314
325
  this.tins!,
315
326
  searchIndex,
316
327
  this.pointsSet?.edges || [],
328
+ pointIndices,
317
329
  );
318
330
 
319
331
  const kinks = ["forw", "bakw"].map((direction) => {
@@ -770,7 +782,7 @@ export class Tin extends Transform {
770
782
 
771
783
  // Calculate strict TIN if needed
772
784
  if (strict === Tin.MODE_STRICT || strict === Tin.MODE_AUTO) {
773
- this.calcurateStrictTin();
785
+ this.calculateStrictTin();
774
786
  }
775
787
 
776
788
  // Generate backward TIN if needed
@@ -797,18 +809,11 @@ export class Tin extends Transform {
797
809
 
798
810
  this.addIndexedTin();
799
811
 
800
- const targets: Array<keyof TinsBD> = ["forw"];
801
- if (this.strict_status === Tin.STATUS_LOOSE) {
802
- targets.push("bakw");
803
- }
804
-
805
- const includeReciprocals = this.strict_status === Tin.STATUS_STRICT;
806
- this.pointsWeightBuffer = buildPointsWeightBuffer({
807
- tins: this.tins!,
808
- targets,
809
- includeReciprocals,
810
- numBoundaryVertices: verticesSet.length,
811
- });
812
+ // 旧 @maplat/transform の transform() は、重みバッファの forw/bakw を無条件に読む
813
+ // (旧 transform.ts:304-306)。Tin の CI・手元はその旧版と組まれるので、未設定だと
814
+ // updateTin() 直後の transform() が TypeError になる。{} の .forw は undefined なので
815
+ // 旧版でも純アフィンになる。新 Transform はこの値を読まない。
816
+ this.pointsWeightBuffer = {};
812
817
  }
813
818
 
814
819
  /**
@@ -1,13 +0,0 @@
1
- import { TinsBD, WeightBufferBD } from '@maplat/transform';
2
- interface WeightBufferOptions {
3
- tins: TinsBD;
4
- targets: Array<keyof TinsBD>;
5
- includeReciprocals: boolean;
6
- /** Number of boundary vertices (b0..b{N-1}). Defaults to 4 for v2 format. */
7
- numBoundaryVertices?: number;
8
- }
9
- /**
10
- * Calculate per-point stretch ratios used for non-linear interpolation.
11
- */
12
- export declare function buildPointsWeightBuffer(options: WeightBufferOptions): WeightBufferBD;
13
- export {};
@@ -1,114 +0,0 @@
1
- import type {
2
- PropertyTriKey,
3
- TinsBD,
4
- Tri,
5
- WeightBufferBD,
6
- } from "@maplat/transform";
7
-
8
- interface WeightBufferOptions {
9
- tins: TinsBD;
10
- targets: Array<keyof TinsBD>;
11
- includeReciprocals: boolean;
12
- /** Number of boundary vertices (b0..b{N-1}). Defaults to 4 for v2 format. */
13
- numBoundaryVertices?: number;
14
- }
15
-
16
- /**
17
- * Calculate per-point stretch ratios used for non-linear interpolation.
18
- */
19
- export function buildPointsWeightBuffer(
20
- options: WeightBufferOptions,
21
- ): WeightBufferBD {
22
- const { tins, targets, includeReciprocals, numBoundaryVertices = 4 } = options;
23
- const edgeRatios: WeightBufferBD = {};
24
-
25
- targets.forEach((target) => {
26
- const triCollection = tins[target];
27
- if (!triCollection || !triCollection.features) return;
28
-
29
- edgeRatios[target as keyof WeightBufferBD] = {};
30
- const seenEdges: Record<string, boolean> = {};
31
-
32
- triCollection.features.forEach((tri: Tri) => {
33
- const props = ["a", "b", "c"] as PropertyTriKey[];
34
- for (let i = 0; i < 3; i++) {
35
- const j = (i + 1) % 3;
36
- const prop_i = props[i];
37
- const prop_j = props[j];
38
- const index_i = tri.properties![prop_i].index;
39
- const index_j = tri.properties![prop_j].index;
40
- const key = [index_i, index_j].sort().join("-");
41
-
42
- if (seenEdges[key]) continue;
43
- seenEdges[key] = true;
44
-
45
- const i_xy = tri.geometry!.coordinates[0][i];
46
- const j_xy = tri.geometry!.coordinates[0][j];
47
- const i_merc = tri.properties![prop_i].geom;
48
- const j_merc = tri.properties![prop_j].geom;
49
-
50
- const ratio = Math.sqrt(
51
- Math.pow(i_merc[0] - j_merc[0], 2) +
52
- Math.pow(i_merc[1] - j_merc[1], 2),
53
- ) / Math.sqrt(
54
- Math.pow(i_xy[0] - j_xy[0], 2) + Math.pow(i_xy[1] - j_xy[1], 2),
55
- );
56
-
57
- const targetBuffer = edgeRatios[target as keyof WeightBufferBD]!;
58
- targetBuffer[`${index_i}:${key}`] = ratio;
59
- targetBuffer[`${index_j}:${key}`] = ratio;
60
- }
61
- });
62
- });
63
-
64
- const pointsWeightBuffer: WeightBufferBD = {};
65
- if (includeReciprocals) {
66
- pointsWeightBuffer.bakw = {};
67
- }
68
-
69
- targets.forEach((target) => {
70
- const targetBuffer = edgeRatios[target as keyof WeightBufferBD];
71
- pointsWeightBuffer[target as keyof WeightBufferBD] = {};
72
-
73
- if (!targetBuffer) {
74
- return;
75
- }
76
-
77
- const pointWeights: Record<string, number[]> = {};
78
- Object.keys(targetBuffer).forEach((key) => {
79
- const [pointId] = key.split(":");
80
- if (!pointWeights[pointId]) {
81
- pointWeights[pointId] = [];
82
- }
83
- pointWeights[pointId].push(targetBuffer[key]);
84
- });
85
-
86
- Object.keys(pointWeights).forEach((pointId) => {
87
- const weights = pointWeights[pointId];
88
- const avgWeight = weights.reduce((sum, w) => sum + w, 0) / weights.length;
89
-
90
- pointsWeightBuffer[target as keyof WeightBufferBD]![pointId] = avgWeight;
91
-
92
- if (includeReciprocals && pointsWeightBuffer.bakw) {
93
- pointsWeightBuffer.bakw[pointId] = 1 / avgWeight;
94
- }
95
- });
96
-
97
- let centroidSum = 0;
98
- for (let i = 0; i < numBoundaryVertices; i++) {
99
- const key = `b${i}`;
100
- const weight =
101
- pointsWeightBuffer[target as keyof WeightBufferBD]![key] || 0;
102
- centroidSum += weight;
103
- }
104
- pointsWeightBuffer[target as keyof WeightBufferBD]!["c"] =
105
- centroidSum / numBoundaryVertices;
106
-
107
- if (includeReciprocals && pointsWeightBuffer.bakw) {
108
- pointsWeightBuffer.bakw["c"] = 1 /
109
- pointsWeightBuffer[target as keyof WeightBufferBD]!["c"];
110
- }
111
- });
112
-
113
- return pointsWeightBuffer;
114
- }