@maplat/tin 1.1.1 → 1.1.2-rc.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/README.ja.md +6 -6
- package/README.md +6 -6
- package/dist/boundary-vertices.d.ts +1 -1
- package/dist/edge-nodes.d.ts +27 -0
- package/dist/edgebound/constrain.d.ts +23 -2
- package/dist/edgebound/index.d.ts +4 -4
- package/dist/index.d.ts +11 -11
- package/dist/kinks.d.ts +20 -0
- package/dist/maplat_tin.js +1533 -1420
- package/dist/maplat_tin.umd.js +3 -3
- package/dist/strict-overlap.d.ts +1 -1
- package/dist/tin.d.ts +5 -2
- package/dist/types/kinks.d.ts +12 -0
- package/dist/types/tin.d.ts +49 -0
- package/package.json +6 -7
- package/src/boundary-vertices.ts +1 -1
- package/src/constrained-tin.ts +1 -1
- package/src/edge-nodes.ts +126 -0
- package/src/edgebound/constrain.ts +74 -0
- package/src/index.ts +2 -2
- package/src/kinks.ts +76 -42
- package/src/tin.ts +45 -91
- package/src/types/kinks.ts +24 -0
- package/src/types/kinks.d.ts +0 -51
- /package/src/types/{tin.d.ts → tin.ts} +0 -0
package/src/constrained-tin.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { FeatureCollection, Point, Polygon } from "geojson";
|
|
|
3
3
|
import Delaunator from "delaunator";
|
|
4
4
|
import { Constrain as EdgeBound } from "./edgebound/index.ts";
|
|
5
5
|
import type { Edge } from "@maplat/transform";
|
|
6
|
-
import type { TriangleProperties } from "./types/tin.
|
|
6
|
+
import type { TriangleProperties } from "./types/tin.ts";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* 制約付きTIN(Triangulated Irregular Network)を生成します
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { Position } from "geojson";
|
|
2
|
+
|
|
3
|
+
type EdgeSegmentItem = [Position, number, number, number, string?];
|
|
4
|
+
/** [紙の面の位置, 地理面の位置, 折れ線の全長に対する比] */
|
|
5
|
+
export type EdgeNodeItem = [Position, Position, number];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 両面の折れ点を「同じ位置」とみなす累積長比の許容幅。
|
|
9
|
+
*
|
|
10
|
+
* 比は 0 以上 1 以下なので絶対許容幅で足りる。`Math.sqrt` と加算・除算の丸めだけで
|
|
11
|
+
* 生じる比のずれは理論上 (2n+12)×Number.EPSILON(n は片側のセグメント数、実測では
|
|
12
|
+
* 最大 2×Number.EPSILON)に収まるため、実測値の 128 倍・理論値(n=122)を覆う
|
|
13
|
+
* 256×Number.EPSILON を採る。正当に別の位置にある折れ点の比の間隔は実データで
|
|
14
|
+
* 6.9e-3 以上(合成の悪条件でも 1e-11)あるため、この幅では誤って結びつかない。
|
|
15
|
+
*/
|
|
16
|
+
export const RATIO_MATCH_TOLERANCE = 256 * Number.EPSILON;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 必須エッジの中継点を作る。両面の折れ線(端点を含む)を、累積長の比で突き合わせ、片方の面の折れ点を
|
|
20
|
+
* 相手の面へ写す。比の順に並べて返す。
|
|
21
|
+
*
|
|
22
|
+
* 比が許容幅内に一致する折れ点同士は 1 つの中継点へまとめ、どちらの面の折れ点の位置もそのまま使う。
|
|
23
|
+
* 一致する相手が無ければ、相手面の該当する区間を線形補間する。丸めで比がわずかにずれただけの
|
|
24
|
+
* 対応する折れ点を 2 つに分けて作らせないため、範囲チェックより前に許容幅内の最寄りを探す。
|
|
25
|
+
*
|
|
26
|
+
* `Tin#generatePointsSet` がこの関数を使う。
|
|
27
|
+
*
|
|
28
|
+
* @param illstNodes 紙の面の折れ線(始点・折れ点・終点)
|
|
29
|
+
* @param mercNodes 地理面の折れ線(始点・折れ点・終点)
|
|
30
|
+
*/
|
|
31
|
+
export function interpolateEdgeNodes(illstNodes: Position[], mercNodes: Position[]): EdgeNodeItem[] {
|
|
32
|
+
// Calculate edge segments
|
|
33
|
+
const segments = [illstNodes, mercNodes].map((nodes: Position[]): EdgeSegmentItem[] => {
|
|
34
|
+
const lengths = nodes.map((node: Position, index: number, arr: Position[]) => {
|
|
35
|
+
if (index === 0) return 0;
|
|
36
|
+
const prev = arr[index - 1];
|
|
37
|
+
return Math.sqrt(
|
|
38
|
+
Math.pow(node[0] - prev[0], 2) + Math.pow(node[1] - prev[1], 2),
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const accumLengths = lengths.reduce((acc: number[], len: number, idx: number) => {
|
|
43
|
+
if (idx === 0) return [0];
|
|
44
|
+
acc.push(acc[idx - 1] + len);
|
|
45
|
+
return acc;
|
|
46
|
+
}, [] as number[]);
|
|
47
|
+
|
|
48
|
+
return accumLengths.map((accum: number, idx: number, arr: number[]): EdgeSegmentItem => {
|
|
49
|
+
const ratio = accum / arr[arr.length - 1];
|
|
50
|
+
return [nodes[idx], lengths[idx], accumLengths[idx], ratio];
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Generate edge nodes
|
|
55
|
+
const edgeNodes: EdgeNodeItem[] = [];
|
|
56
|
+
segments.forEach((segment: EdgeSegmentItem[], idx: number) => {
|
|
57
|
+
const otherSegment = segments[idx ? 0 : 1];
|
|
58
|
+
segment.forEach((item: EdgeSegmentItem, index: number) => {
|
|
59
|
+
// 端点(GCP と同じ位置)と対応が確定した折れ点からは中継点を作らない
|
|
60
|
+
if (
|
|
61
|
+
index === 0 ||
|
|
62
|
+
index === segment.length - 1 ||
|
|
63
|
+
item[4] === "handled"
|
|
64
|
+
) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const node = item[0];
|
|
68
|
+
const ratio = item[3] as number;
|
|
69
|
+
|
|
70
|
+
// 許容幅内で最も近い相手の折れ点を探す。丸めで比が数 ULP ずれた対応する
|
|
71
|
+
// 折れ点はここで 1 つにまとまる。相手面の端点は GCP の位置そのものなので
|
|
72
|
+
// 対象外にし、端点付近の内点と GCP が重なるのを防ぐ。
|
|
73
|
+
let matched: EdgeSegmentItem | undefined;
|
|
74
|
+
let matchedDiff = Infinity;
|
|
75
|
+
for (let i = 1; i < otherSegment.length - 1; i++) {
|
|
76
|
+
const curr = otherSegment[i];
|
|
77
|
+
if (curr[4] === "handled") continue;
|
|
78
|
+
const diff = Math.abs((curr[3] as number) - ratio);
|
|
79
|
+
if (diff <= RATIO_MATCH_TOLERANCE && diff < matchedDiff) {
|
|
80
|
+
matched = curr;
|
|
81
|
+
matchedDiff = diff;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (matched) {
|
|
86
|
+
// 両側を対応確定にして、逆方向の走査でもう一度中継点を作らせない
|
|
87
|
+
matched[4] = "handled";
|
|
88
|
+
item[4] = "handled";
|
|
89
|
+
edgeNodes.push(
|
|
90
|
+
idx === 0
|
|
91
|
+
? [node, matched[0], ratio]
|
|
92
|
+
: [matched[0], node, ratio],
|
|
93
|
+
);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 一致する折れ点が無ければ、比が入る区間を線形補間する
|
|
98
|
+
for (let i = 0; i < otherSegment.length - 1; i++) {
|
|
99
|
+
const curr = otherSegment[i];
|
|
100
|
+
const next = otherSegment[i + 1];
|
|
101
|
+
const currRatio = curr[3] as number;
|
|
102
|
+
const nextRatio = next[3] as number;
|
|
103
|
+
if (currRatio < ratio && nextRatio > ratio) {
|
|
104
|
+
const ratioInSegment = (ratio - currRatio) / (nextRatio - currRatio);
|
|
105
|
+
const interpNode: Position = [
|
|
106
|
+
(next[0][0] - curr[0][0]) *
|
|
107
|
+
ratioInSegment + curr[0][0],
|
|
108
|
+
(next[0][1] - curr[0][1]) *
|
|
109
|
+
ratioInSegment + curr[0][1],
|
|
110
|
+
];
|
|
111
|
+
edgeNodes.push(
|
|
112
|
+
idx === 0
|
|
113
|
+
? [node, interpNode, ratio]
|
|
114
|
+
: [interpNode, node, ratio],
|
|
115
|
+
);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// 比がどの区間にも入らない(比が端点と一致するなどの退化した入力)ときは中継点を作らない
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// 比の順に並べる。同値のときは作成順(stable sort)を保つ
|
|
125
|
+
return edgeNodes.sort((a: EdgeNodeItem, b: EdgeNodeItem) => a[2] - b[2]);
|
|
126
|
+
}
|
|
@@ -75,11 +75,25 @@ export class Constrain extends Base {
|
|
|
75
75
|
*/
|
|
76
76
|
constrainOne(segP1: number, segP2: number): number {
|
|
77
77
|
const { triangles, halfedges } = this.del;
|
|
78
|
+
this.assertVertex(segP1, segP1, segP2);
|
|
79
|
+
this.assertVertex(segP2, segP1, segP2);
|
|
80
|
+
this.assertInTriangulation(segP1, segP1, segP2);
|
|
81
|
+
this.assertInTriangulation(segP2, segP1, segP2);
|
|
78
82
|
const start = this.vertMap[segP1];
|
|
79
83
|
|
|
84
|
+
// The limit only guards against a walk that never terminates.
|
|
85
|
+
// See maxConstrainSteps() for how it relates to valid input.
|
|
86
|
+
const maxSteps = this.maxConstrainSteps();
|
|
87
|
+
let steps = 0;
|
|
88
|
+
|
|
80
89
|
// Loop over the edges touching segP1.
|
|
81
90
|
let edg = start;
|
|
82
91
|
do {
|
|
92
|
+
if (++steps > maxSteps) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
`Constraining edge [${segP1}, ${segP2}] exceeded the iteration limit`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
83
97
|
const p4 = triangles[edg];
|
|
84
98
|
const nxt = nextEdge(edg);
|
|
85
99
|
|
|
@@ -115,6 +129,11 @@ export class Constrain extends Base {
|
|
|
115
129
|
// no more intersects.
|
|
116
130
|
let rescan = -1;
|
|
117
131
|
while (edg !== -1) {
|
|
132
|
+
if (++steps > maxSteps) {
|
|
133
|
+
throw new Error(
|
|
134
|
+
`Constraining edge [${segP1}, ${segP2}] exceeded the iteration limit`,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
118
137
|
const adj = halfedges[edg];
|
|
119
138
|
const bot = prevEdge(edg);
|
|
120
139
|
const top = prevEdge(adj);
|
|
@@ -220,6 +239,50 @@ export class Constrain extends Base {
|
|
|
220
239
|
return this.findEdge(segP1, segP2);
|
|
221
240
|
}
|
|
222
241
|
|
|
242
|
+
/**
|
|
243
|
+
* Upper bound for the number of steps taken while constraining one edge.
|
|
244
|
+
*
|
|
245
|
+
* A constraint that crosses k edges is resolved in passes over the crossed
|
|
246
|
+
* edges, so the number of steps can grow with k squared (for example when
|
|
247
|
+
* the crossed edges fan out between two bowed rows of points). k is less
|
|
248
|
+
* than the number of half-edges, so the square of that number is never
|
|
249
|
+
* reached by a walk that terminates. The floor keeps tiny meshes away from
|
|
250
|
+
* the limit.
|
|
251
|
+
*/
|
|
252
|
+
private maxConstrainSteps(): number {
|
|
253
|
+
const numEdges = this.del.triangles.length;
|
|
254
|
+
return Math.max(numEdges * numEdges, 4096);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Throw unless `vertex` is part of the triangulation. Delaunator leaves out
|
|
259
|
+
* points that coincide with another point, so a valid point id may have no
|
|
260
|
+
* triangle. An edge cannot start or end at such a point.
|
|
261
|
+
*/
|
|
262
|
+
private assertInTriangulation(
|
|
263
|
+
vertex: number,
|
|
264
|
+
segP1: number,
|
|
265
|
+
segP2: number,
|
|
266
|
+
): void {
|
|
267
|
+
if (this.vertMap[vertex] >= this.del.triangles.length) {
|
|
268
|
+
throw new Error(
|
|
269
|
+
`Invalid edge [${segP1}, ${segP2}]: vertex ${vertex} is not part of the triangulation`,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Throw unless `vertex` is an id of a point given to Delaunator.
|
|
276
|
+
*/
|
|
277
|
+
private assertVertex(vertex: number, segP1: number, segP2: number): void {
|
|
278
|
+
const numPoints = this.vertMap.length;
|
|
279
|
+
if (!Number.isInteger(vertex) || vertex < 0 || vertex >= numPoints) {
|
|
280
|
+
throw new Error(
|
|
281
|
+
`Invalid edge [${String(segP1)}, ${String(segP2)}]: vertex ids must be integers from 0 to ${numPoints - 1}`,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
223
286
|
/**
|
|
224
287
|
* Fix the Delaunay condition.
|
|
225
288
|
*/
|
|
@@ -281,10 +344,21 @@ export class Constrain extends Base {
|
|
|
281
344
|
findEdge(p1: number, p2: number): number {
|
|
282
345
|
const start1 = this.vertMap[p2];
|
|
283
346
|
const { triangles, halfedges } = this.del;
|
|
347
|
+
// p2 is not a point of the triangulation: there is no edge to find.
|
|
348
|
+
if (!(start1 < triangles.length)) {
|
|
349
|
+
return Infinity;
|
|
350
|
+
}
|
|
284
351
|
let edg = start1;
|
|
285
352
|
let prv: number;
|
|
353
|
+
// A walk around one vertex visits each half-edge at most once.
|
|
354
|
+
let steps = 0;
|
|
286
355
|
// Walk around p2, iterating over the edges pointing to it.
|
|
287
356
|
do {
|
|
357
|
+
if (++steps > triangles.length) {
|
|
358
|
+
throw new Error(
|
|
359
|
+
`Looking up edge [${p1}, ${p2}] exceeded the iteration limit`,
|
|
360
|
+
);
|
|
361
|
+
}
|
|
288
362
|
if (triangles[edg] === p1) {
|
|
289
363
|
return edg;
|
|
290
364
|
}
|
package/src/index.ts
CHANGED
|
@@ -18,8 +18,8 @@ export { counterPoint, createPoint, vertexCalc } from "./vertexutils.ts";
|
|
|
18
18
|
|
|
19
19
|
// Type exports
|
|
20
20
|
export type { SearchIndex } from "./searchutils.ts";
|
|
21
|
-
export type { IntersectionPoint } from "./types/kinks.
|
|
22
|
-
export type { TriangleProperties } from "./types/tin.
|
|
21
|
+
export type { IntersectionPoint } from "./types/kinks.ts";
|
|
22
|
+
export type { TriangleProperties } from "./types/tin.ts";
|
|
23
23
|
|
|
24
24
|
// Re-export types from @maplat/transform
|
|
25
25
|
export type {
|
package/src/kinks.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Feature, Point, Position } from "geojson";
|
|
2
2
|
import { point } from "@turf/turf";
|
|
3
|
-
import type { IntersectionPoint } from "./types/kinks.
|
|
3
|
+
import type { IntersectionPoint } from "./types/kinks.ts";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* 線分の交差点を検出するメインの関数
|
|
@@ -11,18 +11,48 @@ import type { IntersectionPoint } from "./types/kinks.d.ts";
|
|
|
11
11
|
*/
|
|
12
12
|
export default function findIntersections(
|
|
13
13
|
coords: Position[][],
|
|
14
|
+
): Feature<Point>[] {
|
|
15
|
+
return findIntersectionsUpTo(coords, Infinity);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Tin が 1 方向(forw / bakw)について数え上げる交差の件数の上限
|
|
20
|
+
* (同じ位置の交差を 1 件に数えた件数)。
|
|
21
|
+
*
|
|
22
|
+
* 交差の件数は線分の数の 2 乗まで増えうる。対応が大きく乱れた制御点では
|
|
23
|
+
* 時間とメモリを使い切るため、ここで打ち切る。
|
|
24
|
+
* 同梱の実データで strict の交差が最も多いもの(約 3 万件)の 3 倍強にあたる。
|
|
25
|
+
*/
|
|
26
|
+
export const MAX_KINKS_PER_DIRECTION = 100000;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 件数の上限 1 件あたりに許す「見つけた交差の総数(重複を含む)」。
|
|
30
|
+
*
|
|
31
|
+
* 同じ位置の交差は 1 件に数えるので、位置が重なる交差ばかりの入力では
|
|
32
|
+
* 件数の上限に届かない。総数がこの倍率を超えたら数え上げをやめる。
|
|
33
|
+
* 同梱の実データでは、総数は残る件数の約 4 倍。
|
|
34
|
+
*/
|
|
35
|
+
const MAX_FOUND_PER_LIMIT = 16;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 線分の交差点を、件数の上限つきで検出する(Tin の内部用)
|
|
39
|
+
*
|
|
40
|
+
* @param coords - 線分群の座標配列
|
|
41
|
+
* @param limit - 返す交差点の件数の上限。同じ位置の交差を 1 件に数えた件数で数え、
|
|
42
|
+
* 届いた時点で検出をやめる。位置が重なる交差ばかりのときは、見つけた交差の総数が
|
|
43
|
+
* limit の 16 倍に届いた時点でもやめる
|
|
44
|
+
* @returns 検出された交差点のFeature配列(同じ位置の重複なし・高々 limit 件)
|
|
45
|
+
* @throws {Error} 座標に有限数(Number.isFinite)でない値が含まれる場合
|
|
46
|
+
*/
|
|
47
|
+
export function findIntersectionsUpTo(
|
|
48
|
+
coords: Position[][],
|
|
49
|
+
limit: number,
|
|
14
50
|
): Feature<Point>[] {
|
|
15
51
|
validateCoordinateValues(coords);
|
|
16
52
|
const arcs = new ArcCollection(coords);
|
|
17
|
-
const
|
|
18
|
-
const deduped: IntersectionPoint[] = dedupIntersections(xy);
|
|
19
|
-
const unique = new Map<string, IntersectionPoint>();
|
|
20
|
-
|
|
21
|
-
deduped.forEach((apoint: IntersectionPoint) => {
|
|
22
|
-
unique.set(`${apoint.x}:${apoint.y}`, apoint);
|
|
23
|
-
});
|
|
53
|
+
const unique = arcs.findSegmentIntersections(limit);
|
|
24
54
|
|
|
25
|
-
return
|
|
55
|
+
return unique.map((apoint) =>
|
|
26
56
|
point([apoint.x, apoint.y])
|
|
27
57
|
);
|
|
28
58
|
}
|
|
@@ -271,9 +301,10 @@ class ArcCollection {
|
|
|
271
301
|
* 線分の交差を検出
|
|
272
302
|
* ストライプ分割による効率的な判定を実装
|
|
273
303
|
*
|
|
274
|
-
* @
|
|
304
|
+
* @param limit - 返す交差点の件数の上限(同じ位置の交差は 1 件に数える)
|
|
305
|
+
* @returns 検出された交差点の配列(同じ位置の重複なし)
|
|
275
306
|
*/
|
|
276
|
-
findSegmentIntersections() {
|
|
307
|
+
findSegmentIntersections(limit = Infinity) {
|
|
277
308
|
const bounds = this.getBounds(),
|
|
278
309
|
ymin = bounds.ymin || 0,
|
|
279
310
|
yrange = (bounds.ymax || 0) - ymin,
|
|
@@ -285,7 +316,7 @@ class ArcCollection {
|
|
|
285
316
|
// 線分が属するストライプを辿る走査は、高々 stripeCount 回で終わる。
|
|
286
317
|
// 万一の迷走(非有限数に由来する NaN など)に備えて上限を設ける。
|
|
287
318
|
const maxStripeSteps = stripeCount + 1;
|
|
288
|
-
let i
|
|
319
|
+
let i;
|
|
289
320
|
|
|
290
321
|
// Count segments in each stripe
|
|
291
322
|
this.forEachSegment(
|
|
@@ -350,18 +381,30 @@ class ArcCollection {
|
|
|
350
381
|
);
|
|
351
382
|
|
|
352
383
|
// Detect intersections among segments in each stripe.
|
|
353
|
-
const raw = this.getVertexData()
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
384
|
+
const raw = this.getVertexData();
|
|
385
|
+
// 同じ線分の組は複数のストライプで見つかることがあり、別の組が同じ位置で
|
|
386
|
+
// 交わることもある。上限は、同じ位置の交差を 1 件に数えた件数に掛ける。
|
|
387
|
+
// 位置が重なる交差ばかりだと、件数の上限に届かないまま数え上げが続く。
|
|
388
|
+
// 見つけた交差の総数(重複を含む)にも上限を置き、時間と seenKeys の大きさを抑える。
|
|
389
|
+
const maxFound = limit * MAX_FOUND_PER_LIMIT;
|
|
390
|
+
let found = 0;
|
|
391
|
+
const seenKeys = new Set<string>();
|
|
392
|
+
const unique = new Map<string, IntersectionPoint>();
|
|
393
|
+
const collect = (o: IntersectionPoint): boolean => {
|
|
394
|
+
found++;
|
|
395
|
+
const key = getIntersectionKey(o);
|
|
396
|
+
if (!seenKeys.has(key)) {
|
|
397
|
+
seenKeys.add(key);
|
|
398
|
+
unique.set(`${o.x}:${o.y}`, o);
|
|
399
|
+
}
|
|
400
|
+
return unique.size >= limit || found >= maxFound;
|
|
401
|
+
};
|
|
402
|
+
if (raw.xx && raw.yy) {
|
|
403
|
+
for (i = 0; i < stripeCount; i++) {
|
|
404
|
+
if (intersectSegments(stripes[i], raw.xx, raw.yy, collect)) break;
|
|
362
405
|
}
|
|
363
406
|
}
|
|
364
|
-
return
|
|
407
|
+
return Array.from(unique.values());
|
|
365
408
|
}
|
|
366
409
|
}
|
|
367
410
|
|
|
@@ -444,9 +487,9 @@ function intersectSegments(
|
|
|
444
487
|
ids: Uint32Array,
|
|
445
488
|
xx: Float64Array,
|
|
446
489
|
yy: Float64Array,
|
|
447
|
-
)
|
|
448
|
-
|
|
449
|
-
|
|
490
|
+
collect: (intersection: IntersectionPoint) => boolean,
|
|
491
|
+
): boolean {
|
|
492
|
+
const lim = ids.length - 2;
|
|
450
493
|
let s1p1,
|
|
451
494
|
s1p2,
|
|
452
495
|
s2p1,
|
|
@@ -521,18 +564,21 @@ function intersectSegments(
|
|
|
521
564
|
if (hit) {
|
|
522
565
|
seg1 = [s1p1, s1p2];
|
|
523
566
|
seg2 = [s2p1, s2p2];
|
|
524
|
-
|
|
567
|
+
// collect() returns true once enough intersections have been found.
|
|
568
|
+
if (collect(formatIntersection(hit, seg1, seg2, xx, yy))) return true;
|
|
525
569
|
if (hit.length == 4) {
|
|
526
570
|
// two collinear segments may have two endpoint intersections
|
|
527
|
-
|
|
528
|
-
formatIntersection(hit.slice(2), seg1, seg2, xx, yy)
|
|
529
|
-
)
|
|
571
|
+
if (
|
|
572
|
+
collect(formatIntersection(hit.slice(2), seg1, seg2, xx, yy))
|
|
573
|
+
) {
|
|
574
|
+
return true;
|
|
575
|
+
}
|
|
530
576
|
}
|
|
531
577
|
}
|
|
532
578
|
}
|
|
533
579
|
i += 2;
|
|
534
580
|
}
|
|
535
|
-
return
|
|
581
|
+
return false;
|
|
536
582
|
}
|
|
537
583
|
|
|
538
584
|
/**
|
|
@@ -971,18 +1017,6 @@ function formatIntersectingSegment(
|
|
|
971
1017
|
* @param arr - 交差点情報の配列
|
|
972
1018
|
* @returns 重複を除去した交差点情報の配列
|
|
973
1019
|
*/
|
|
974
|
-
function dedupIntersections(arr: IntersectionPoint[]) {
|
|
975
|
-
const index: Record<string, boolean> = {};
|
|
976
|
-
return arr.filter((o: IntersectionPoint) => {
|
|
977
|
-
const key = getIntersectionKey(o);
|
|
978
|
-
if (key in index) {
|
|
979
|
-
return false;
|
|
980
|
-
}
|
|
981
|
-
index[key] = true;
|
|
982
|
-
return true;
|
|
983
|
-
});
|
|
984
|
-
}
|
|
985
|
-
|
|
986
1020
|
// Get an indexable key from an intersection object
|
|
987
1021
|
// Assumes that vertex ids of o.a and o.b are sorted
|
|
988
1022
|
function getIntersectionKey(o: IntersectionPoint) {
|
package/src/tin.ts
CHANGED
|
@@ -40,20 +40,19 @@ import type {
|
|
|
40
40
|
YaxisMode,
|
|
41
41
|
} from "@maplat/transform";
|
|
42
42
|
import constrainedTin from "./constrained-tin.ts";
|
|
43
|
+
import { interpolateEdgeNodes } from "./edge-nodes.ts";
|
|
44
|
+
import type { EdgeNodeItem } from "./edge-nodes.ts";
|
|
43
45
|
import {
|
|
44
46
|
calculateBirdeyeVertices,
|
|
45
47
|
calculatePlainVertices,
|
|
46
48
|
type BoundaryVerticesParams,
|
|
47
49
|
} from "./boundary-vertices.ts";
|
|
48
|
-
import
|
|
50
|
+
import { findIntersectionsUpTo, MAX_KINKS_PER_DIRECTION } from "./kinks.ts";
|
|
49
51
|
import { insertSearchIndex } from "./searchutils.ts";
|
|
50
52
|
import { counterPoint, createPoint, vertexCalc } from "./vertexutils.ts";
|
|
51
53
|
import { resolveOverlaps } from "./strict-overlap.ts";
|
|
52
54
|
import type { SearchIndex } from "./searchutils.ts";
|
|
53
|
-
import type { PointsSetBD, VertexPosition } from "./types/tin.
|
|
54
|
-
|
|
55
|
-
type EdgeSegmentItem = [Position, number, number, number, string?];
|
|
56
|
-
type EdgeNodeItem = [Position, Position, number];
|
|
55
|
+
import type { PointsSetBD, VertexPosition } from "./types/tin.ts";
|
|
57
56
|
|
|
58
57
|
/**
|
|
59
58
|
* Tinクラスの初期化オプション
|
|
@@ -300,8 +299,11 @@ export class Tin extends Transform {
|
|
|
300
299
|
|
|
301
300
|
/**
|
|
302
301
|
* 厳密なTINを計算します
|
|
302
|
+
*
|
|
303
|
+
* @param kinkLimit - forw / bakw それぞれで残す交差(kinks)の件数の上限
|
|
304
|
+
* (同じ位置の交差を 1 件に数えた件数)。届いた時点で数え上げをやめます
|
|
303
305
|
*/
|
|
304
|
-
calculateStrictTin(): void {
|
|
306
|
+
calculateStrictTin(kinkLimit: number = MAX_KINKS_PER_DIRECTION): void {
|
|
305
307
|
const bakTins = this.tins!.forw!.features.map((tri: Tri) =>
|
|
306
308
|
counterTri(tri)
|
|
307
309
|
);
|
|
@@ -332,7 +334,7 @@ export class Tin extends Transform {
|
|
|
332
334
|
const tins = this.tins![direction as keyof TinsBD]!.features.map(
|
|
333
335
|
(tin: Tri) => tin.geometry!.coordinates[0],
|
|
334
336
|
);
|
|
335
|
-
return
|
|
337
|
+
return findIntersectionsUpTo(tins, kinkLimit);
|
|
336
338
|
});
|
|
337
339
|
|
|
338
340
|
if (kinks[0].length === 0 && kinks[1].length === 0) {
|
|
@@ -382,6 +384,10 @@ export class Tin extends Transform {
|
|
|
382
384
|
const illstNodes = Object.assign([], this.edges[i][0]);
|
|
383
385
|
const mercNodes = Object.assign([], this.edges[i][1]);
|
|
384
386
|
|
|
387
|
+
// 端点は GCP の添字で指す。範囲に無い・整数でない添字は、
|
|
388
|
+
// 以降の計算に渡さずここで止める。
|
|
389
|
+
assertEdgeIndices(edge, this.points.length, i);
|
|
390
|
+
|
|
385
391
|
if (illstNodes.length === 0 && mercNodes.length === 0) {
|
|
386
392
|
edges.push(edge);
|
|
387
393
|
continue;
|
|
@@ -393,90 +399,8 @@ export class Tin extends Transform {
|
|
|
393
399
|
mercNodes.unshift(this.points[edge[0]][1]);
|
|
394
400
|
mercNodes.push(this.points[edge[1]][1]);
|
|
395
401
|
|
|
396
|
-
// Calculate edge segments
|
|
397
|
-
const segments = [illstNodes, mercNodes].map((nodes: Position[]): EdgeSegmentItem[] => {
|
|
398
|
-
const lengths = nodes.map((node: Position, index: number, arr: Position[]) => {
|
|
399
|
-
if (index === 0) return 0;
|
|
400
|
-
const prev = arr[index - 1];
|
|
401
|
-
return Math.sqrt(
|
|
402
|
-
Math.pow(node[0] - prev[0], 2) + Math.pow(node[1] - prev[1], 2),
|
|
403
|
-
);
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
const accumLengths = lengths.reduce((acc: number[], len: number, idx: number) => {
|
|
407
|
-
if (idx === 0) return [0];
|
|
408
|
-
acc.push(acc[idx - 1] + len);
|
|
409
|
-
return acc;
|
|
410
|
-
}, [] as number[]);
|
|
411
|
-
|
|
412
|
-
return accumLengths.map((accum: number, idx: number, arr: number[]): EdgeSegmentItem => {
|
|
413
|
-
const ratio = accum / arr[arr.length - 1];
|
|
414
|
-
return [nodes[idx], lengths[idx], accumLengths[idx], ratio];
|
|
415
|
-
});
|
|
416
|
-
});
|
|
417
|
-
|
|
418
402
|
// Generate edge nodes
|
|
419
|
-
|
|
420
|
-
.map((segment: EdgeSegmentItem[], idx: number) => {
|
|
421
|
-
const otherSegment = segments[idx ? 0 : 1];
|
|
422
|
-
return segment
|
|
423
|
-
.filter((item: EdgeSegmentItem, index: number) => {
|
|
424
|
-
return !(
|
|
425
|
-
index === 0 ||
|
|
426
|
-
index === segment.length - 1 ||
|
|
427
|
-
item[4] === "handled"
|
|
428
|
-
);
|
|
429
|
-
})
|
|
430
|
-
.flatMap((item: EdgeSegmentItem): EdgeNodeItem[] => {
|
|
431
|
-
const node = item[0];
|
|
432
|
-
const ratio = item[3];
|
|
433
|
-
|
|
434
|
-
const counterpart = otherSegment.reduce(
|
|
435
|
-
(prev: EdgeSegmentItem[] | undefined, curr: EdgeSegmentItem, currIdx: number, arr: EdgeSegmentItem[]) => {
|
|
436
|
-
if (prev) return prev;
|
|
437
|
-
const nextItem = arr[currIdx + 1];
|
|
438
|
-
if (curr[3] === ratio) {
|
|
439
|
-
curr[4] = "handled";
|
|
440
|
-
return [curr];
|
|
441
|
-
}
|
|
442
|
-
if (
|
|
443
|
-
curr[3] < ratio && nextItem &&
|
|
444
|
-
nextItem[3] > ratio
|
|
445
|
-
) {
|
|
446
|
-
return [curr, nextItem];
|
|
447
|
-
}
|
|
448
|
-
return undefined;
|
|
449
|
-
},
|
|
450
|
-
undefined as EdgeSegmentItem[] | undefined,
|
|
451
|
-
);
|
|
452
|
-
|
|
453
|
-
if (counterpart && counterpart.length === 1) {
|
|
454
|
-
return idx === 0
|
|
455
|
-
? [[node, counterpart[0][0], ratio]]
|
|
456
|
-
: [[counterpart[0][0], node, ratio]];
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
if (counterpart && counterpart.length === 2) {
|
|
460
|
-
const curr = counterpart[0];
|
|
461
|
-
const next = counterpart[1];
|
|
462
|
-
const ratioInSegment = (ratio - (curr[3] as number)) /
|
|
463
|
-
((next[3] as number) - (curr[3] as number));
|
|
464
|
-
const interpNode: Position = [
|
|
465
|
-
(next[0][0] - curr[0][0]) *
|
|
466
|
-
ratioInSegment + curr[0][0],
|
|
467
|
-
(next[0][1] - curr[0][1]) *
|
|
468
|
-
ratioInSegment + curr[0][1],
|
|
469
|
-
];
|
|
470
|
-
return idx === 0
|
|
471
|
-
? [[node, interpNode, ratio]]
|
|
472
|
-
: [[interpNode, node, ratio]];
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
return [];
|
|
476
|
-
});
|
|
477
|
-
})
|
|
478
|
-
.reduce((prev: EdgeNodeItem[], curr: EdgeNodeItem[]) => prev.concat(curr), [])
|
|
479
|
-
.sort((a: EdgeNodeItem, b: EdgeNodeItem) => a[2] < b[2] ? -1 : 1)
|
|
403
|
+
interpolateEdgeNodes(illstNodes, mercNodes)
|
|
480
404
|
.map((item: EdgeNodeItem, index: number, arr: EdgeNodeItem[]) => {
|
|
481
405
|
this.edgeNodes![edgeNodeIndex] = [
|
|
482
406
|
item[0],
|
|
@@ -782,7 +706,11 @@ export class Tin extends Transform {
|
|
|
782
706
|
|
|
783
707
|
// Calculate strict TIN if needed
|
|
784
708
|
if (strict === Tin.MODE_STRICT || strict === Tin.MODE_AUTO) {
|
|
785
|
-
|
|
709
|
+
// auto は交差の有無しか使わない(あれば loose に切り替えて kinks を捨てる)
|
|
710
|
+
// ので、1 件見つけた時点で数え上げをやめる。
|
|
711
|
+
this.calculateStrictTin(
|
|
712
|
+
strict === Tin.MODE_AUTO ? 1 : MAX_KINKS_PER_DIRECTION,
|
|
713
|
+
);
|
|
786
714
|
}
|
|
787
715
|
|
|
788
716
|
// Generate backward TIN if needed
|
|
@@ -824,3 +752,29 @@ export class Tin extends Transform {
|
|
|
824
752
|
this.updateTin();
|
|
825
753
|
}
|
|
826
754
|
}
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* edge の両端が GCP の添字(0 以上・GCP の数未満の整数)であることを確かめます。
|
|
758
|
+
*
|
|
759
|
+
* @param edge - 検査する edge の両端([始点の添字, 終点の添字])
|
|
760
|
+
* @param pointCount - GCP の数
|
|
761
|
+
* @param edgeIndex - edges の中での位置(エラーメッセージ用)
|
|
762
|
+
* @throws {Error} 添字が整数でない、または GCP の範囲に無い場合
|
|
763
|
+
*/
|
|
764
|
+
function assertEdgeIndices(
|
|
765
|
+
edge: Edge,
|
|
766
|
+
pointCount: number,
|
|
767
|
+
edgeIndex: number,
|
|
768
|
+
): void {
|
|
769
|
+
const isValid = (index: unknown) =>
|
|
770
|
+
Number.isInteger(index) &&
|
|
771
|
+
(index as number) >= 0 &&
|
|
772
|
+
(index as number) < pointCount;
|
|
773
|
+
if (!Array.isArray(edge) || !isValid(edge[0]) || !isValid(edge[1])) {
|
|
774
|
+
throw new Error(
|
|
775
|
+
`Invalid edge at edges[${edgeIndex}]: start/end ${
|
|
776
|
+
JSON.stringify(edge)
|
|
777
|
+
} must be integer indices of points (0 to ${pointCount - 1})`,
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface IntersectionPoint {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
a: number[];
|
|
5
|
+
b: number[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type ArcCallback = (
|
|
9
|
+
i: number,
|
|
10
|
+
j: number,
|
|
11
|
+
xx: Float64Array,
|
|
12
|
+
yy: Float64Array,
|
|
13
|
+
) => void;
|
|
14
|
+
export type ForEachSegmentCallback = (
|
|
15
|
+
v1: number,
|
|
16
|
+
v2: number,
|
|
17
|
+
xx: Float64Array,
|
|
18
|
+
yy: Float64Array,
|
|
19
|
+
) => void;
|
|
20
|
+
|
|
21
|
+
export interface Arc {
|
|
22
|
+
i: number;
|
|
23
|
+
j: number;
|
|
24
|
+
}
|