@maplat/tin 0.13.1 → 0.14.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.
- package/LICENSE +224 -224
- package/README.ja.md +220 -220
- package/README.md +221 -221
- package/dist/boundary-vertices.d.ts +33 -6
- package/dist/maplat_tin.js +2669 -2228
- package/dist/maplat_tin.umd.js +5 -5
- package/dist/tin.d.ts +16 -1
- package/dist/vertexutils.d.ts +6 -2
- package/dist/weight-buffer.d.ts +2 -0
- package/package.json +8 -8
- package/src/boundary-vertices.ts +557 -283
- package/src/constrained-tin.ts +84 -84
- package/src/edgebound/base.ts +65 -65
- package/src/edgebound/bitset.ts +109 -109
- package/src/edgebound/constrain.license.txt +15 -15
- package/src/edgebound/constrain.ts +498 -498
- package/src/edgebound/index.ts +10 -10
- package/src/index.ts +42 -42
- package/src/kinks.ts +1092 -1092
- package/src/searchutils.ts +102 -102
- package/src/strict-overlap.ts +461 -281
- package/src/tin.ts +821 -703
- package/src/types/cdt2d.d.ts +4 -4
- package/src/types/constrainautor.d.ts +6 -6
- package/src/types/kinks.d.ts +51 -51
- package/src/types/tin.d.ts +54 -54
- package/src/vertexutils.ts +92 -87
- package/src/weight-buffer.ts +114 -111
package/src/strict-overlap.ts
CHANGED
|
@@ -1,281 +1,461 @@
|
|
|
1
|
-
import { polygon } from "@turf/turf";
|
|
2
|
-
import type { PropertyTriKey, TinsBD, Tri } from "@maplat/transform";
|
|
3
|
-
import type { Position } from "geojson";
|
|
4
|
-
import { counterTri } from "@maplat/transform";
|
|
5
|
-
import type { SearchIndex } from "./searchutils.ts";
|
|
6
|
-
import { insertSearchIndex, removeSearchIndex } from "./searchutils.ts";
|
|
7
|
-
|
|
8
|
-
type ConstraintEdges = number[][];
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
)
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
1
|
+
import { polygon } from "@turf/turf";
|
|
2
|
+
import type { PropertyTriKey, TinsBD, Tri } from "@maplat/transform";
|
|
3
|
+
import type { Position } from "geojson";
|
|
4
|
+
import { counterTri } from "@maplat/transform";
|
|
5
|
+
import type { SearchIndex } from "./searchutils.ts";
|
|
6
|
+
import { insertSearchIndex, removeSearchIndex } from "./searchutils.ts";
|
|
7
|
+
|
|
8
|
+
type ConstraintEdges = number[][];
|
|
9
|
+
|
|
10
|
+
/** Matches the shape of PropertyTri in @maplat/transform (not publicly exported). */
|
|
11
|
+
type VertexInfo = {
|
|
12
|
+
prop: { index: number | string; geom: Position };
|
|
13
|
+
geom: Position;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type TrisEntry = {
|
|
17
|
+
forw: Tri;
|
|
18
|
+
bakw: Tri;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* bakw 三角形が縮退(面積ゼロ)かどうかを判定する。
|
|
23
|
+
* pointInTriangle は denom=0 のとき false を返すため、縮退三角形を
|
|
24
|
+
* 別途検出してフリップ対象に含める必要がある。
|
|
25
|
+
*
|
|
26
|
+
* 判定は 2×面積 = |cross product| < epsilon で行う。
|
|
27
|
+
* web mercator 座標系(値域 ~1e7)での実用的な最小三角形面積は数 m² 以上であり、
|
|
28
|
+
* epsilon=1e-6 は「実質ゼロ面積」のみを確実に捉える。
|
|
29
|
+
*/
|
|
30
|
+
function isDegenerate(triangle: [number, number][], epsilon = 1e-6): boolean {
|
|
31
|
+
const [ax, ay] = triangle[0];
|
|
32
|
+
const [bx, by] = triangle[1];
|
|
33
|
+
const [cx, cy] = triangle[2];
|
|
34
|
+
const doubleArea = Math.abs((bx - ax) * (cy - ay) - (cx - ax) * (by - ay));
|
|
35
|
+
return doubleArea < epsilon;
|
|
36
|
+
}
|
|
37
|
+
|
|
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;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function extractVertices(tri: Tri) {
|
|
55
|
+
return (["a", "b", "c"] as PropertyTriKey[]).map((key, index) => ({
|
|
56
|
+
prop: tri.properties![key],
|
|
57
|
+
geom: tri.geometry!.coordinates[0][index],
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// フリップ後に新規作成された三角形が forw 縮退になる場合があるため、
|
|
62
|
+
// 新規キーも処理できるよう反復ループを使用する(上限 MAX_FLIP_ITERATIONS 回)。
|
|
63
|
+
const MAX_FLIP_ITERATIONS = 10;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* bakw 縮退三角形のフリップ事前チェック:
|
|
67
|
+
* 縮退 T_deg=(A,B,C) の C は辺 A-B 上にある。
|
|
68
|
+
* フリップ後 T0_new=(A,C,D), T1_new=(B,C,D) が、
|
|
69
|
+
* 隣接三角形 T2=(A,C,E), T3=(B,C,F) と重ならないことを確認する。
|
|
70
|
+
* D と E が辺 AC の同じ側 → 重なり発生 → フリップ不可。同様に D と F で辺 BC を確認。
|
|
71
|
+
*/
|
|
72
|
+
function checkBakwDegenerateFlip(
|
|
73
|
+
bakwDegen0: boolean,
|
|
74
|
+
bakwDegen1: boolean,
|
|
75
|
+
nonSharedBakw: (VertexInfo | undefined)[],
|
|
76
|
+
sharedBakw: (VertexInfo | undefined)[],
|
|
77
|
+
searchIndex: SearchIndex,
|
|
78
|
+
trises: TrisEntry[],
|
|
79
|
+
): boolean {
|
|
80
|
+
if (!bakwDegen0 && !bakwDegen1) return false;
|
|
81
|
+
|
|
82
|
+
const degIdx = bakwDegen0 ? 0 : 1; // 縮退している三角形のインデックス
|
|
83
|
+
const otherIdx = 1 - degIdx;
|
|
84
|
+
const degNonShared = nonSharedBakw[degIdx]; // C(辺 AB 上の縮退頂点)
|
|
85
|
+
const otherNonShared = nonSharedBakw[otherIdx]; // D(正常三角形の非共有頂点)
|
|
86
|
+
|
|
87
|
+
if (!degNonShared || !otherNonShared) return false;
|
|
88
|
+
|
|
89
|
+
const D = toXY(otherNonShared.geom as Position);
|
|
90
|
+
// 隣接三角形が少なくとも1つ見つかり、かつ全て干渉なし → フリップ有効
|
|
91
|
+
// 隣接三角形がない場合はフリップ不要(干渉なし)と判断して false のまま
|
|
92
|
+
let anyNeighborFound = false;
|
|
93
|
+
let flipBlockedByNeighbor = false;
|
|
94
|
+
|
|
95
|
+
// 共有頂点 A, B それぞれについて隣接三角形を確認
|
|
96
|
+
for (let shIdx = 0; shIdx <= 1; shIdx++) {
|
|
97
|
+
const sh = sharedBakw[shIdx];
|
|
98
|
+
if (!sh) continue;
|
|
99
|
+
|
|
100
|
+
// 辺 (A, C) または (B, C) の searchIndex キーを構築
|
|
101
|
+
// calcSearchKeys と同じ文字列ソートを使うこと(prop.index は number | string 混在)
|
|
102
|
+
const edgeKey = [String(sh.prop.index), String(degNonShared.prop.index)]
|
|
103
|
+
.sort()
|
|
104
|
+
.join("-");
|
|
105
|
+
const neighborTris = searchIndex[edgeKey];
|
|
106
|
+
if (!neighborTris || neighborTris.length < 2) continue;
|
|
107
|
+
|
|
108
|
+
// 縮退三角形自身を除いた隣接三角形
|
|
109
|
+
const neighbor = neighborTris.find(
|
|
110
|
+
(t) => t.bakw !== trises[degIdx].bakw,
|
|
111
|
+
);
|
|
112
|
+
if (!neighbor) continue;
|
|
113
|
+
|
|
114
|
+
const neighborVerts = extractVertices(neighbor.bakw);
|
|
115
|
+
const neighborNonShared = neighborVerts.find(
|
|
116
|
+
(v) =>
|
|
117
|
+
String(v.prop.index) !== String(sh.prop.index) &&
|
|
118
|
+
String(v.prop.index) !== String(degNonShared.prop.index),
|
|
119
|
+
);
|
|
120
|
+
if (!neighborNonShared) continue;
|
|
121
|
+
|
|
122
|
+
anyNeighborFound = true;
|
|
123
|
+
|
|
124
|
+
const E = toXY(neighborNonShared.geom as Position);
|
|
125
|
+
const A_pos = toXY(sh.geom as Position);
|
|
126
|
+
const C_pos = toXY(degNonShared.geom as Position);
|
|
127
|
+
|
|
128
|
+
// D と E が辺 A-C の同じ側かを符号付き外積で判定
|
|
129
|
+
// cross(A→C, A→D) と cross(A→C, A→E) の符号が同じ → 同じ側 → フリップ不可
|
|
130
|
+
const acx = C_pos[0] - A_pos[0], acy = C_pos[1] - A_pos[1];
|
|
131
|
+
const crossD = acx * (D[1] - A_pos[1]) - acy * (D[0] - A_pos[0]);
|
|
132
|
+
const crossE = acx * (E[1] - A_pos[1]) - acy * (E[0] - A_pos[0]);
|
|
133
|
+
|
|
134
|
+
if (crossD * crossE > 0) {
|
|
135
|
+
// 同じ側 → フリップ後に T0_new/T1_new が隣接三角形と重なる
|
|
136
|
+
flipBlockedByNeighbor = true;
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return anyNeighborFound && !flipBlockedByNeighbor;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* forw 縮退三角形のフリップ事前チェック:
|
|
146
|
+
* forw 縮退の場合、bakw 空間の非共有2頂点が共有辺の両側にあるか確認し、
|
|
147
|
+
* フリップ後に bakw TIN が壊れないことを保証する。
|
|
148
|
+
* 面積比較は bakw 座標値(~1e7)で精度劣化するため、符号付き外積で凸性を直接判定する。
|
|
149
|
+
*/
|
|
150
|
+
function checkForwDegenerateFlip(
|
|
151
|
+
forwDegen0: boolean,
|
|
152
|
+
forwDegen1: boolean,
|
|
153
|
+
nonSharedBakw: (VertexInfo | undefined)[],
|
|
154
|
+
sharedBakw: (VertexInfo | undefined)[],
|
|
155
|
+
): boolean {
|
|
156
|
+
if (!forwDegen0 && !forwDegen1) return false;
|
|
157
|
+
|
|
158
|
+
if (nonSharedBakw[0] && nonSharedBakw[1] && sharedBakw[0] && sharedBakw[1]) {
|
|
159
|
+
const sBakw = sharedBakw.map((item) => toXY(item!.geom as Position)) as [number, number][];
|
|
160
|
+
const nsBakw = nonSharedBakw.map((item) => toXY(item!.geom as Position)) as [number, number][];
|
|
161
|
+
// 共有辺方向ベクトル
|
|
162
|
+
const dx = sBakw[1][0] - sBakw[0][0];
|
|
163
|
+
const dy = sBakw[1][1] - sBakw[0][1];
|
|
164
|
+
// 各非共有頂点の共有辺に対する外積(符号 = どちら側にあるか)
|
|
165
|
+
const cross0 = dx * (nsBakw[0][1] - sBakw[0][1]) - dy * (nsBakw[0][0] - sBakw[0][0]);
|
|
166
|
+
const cross1 = dx * (nsBakw[1][1] - sBakw[0][1]) - dy * (nsBakw[1][0] - sBakw[0][0]);
|
|
167
|
+
// 符号が異なる(逆側にある)→ 凸四角形 → フリップ安全
|
|
168
|
+
return cross0 * cross1 < 0;
|
|
169
|
+
}
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function resolveOverlaps(
|
|
174
|
+
tins: TinsBD,
|
|
175
|
+
searchIndex: SearchIndex,
|
|
176
|
+
edges: ConstraintEdges,
|
|
177
|
+
): boolean {
|
|
178
|
+
const processedKeys = new Set<string>();
|
|
179
|
+
let repaired = false;
|
|
180
|
+
|
|
181
|
+
for (let iter = 0; iter < MAX_FLIP_ITERATIONS; iter++) {
|
|
182
|
+
let anyFlippedThisIter = false;
|
|
183
|
+
|
|
184
|
+
for (const key of Object.keys(searchIndex)) {
|
|
185
|
+
if (processedKeys.has(key)) continue;
|
|
186
|
+
processedKeys.add(key);
|
|
187
|
+
|
|
188
|
+
const trises = searchIndex[key];
|
|
189
|
+
if (!trises || trises.length < 2) continue;
|
|
190
|
+
|
|
191
|
+
const sharedKeys = key.split("-");
|
|
192
|
+
if (sharedKeys.length !== 2) continue;
|
|
193
|
+
|
|
194
|
+
if (isConstraintEdge(key, edges)) continue;
|
|
195
|
+
|
|
196
|
+
const tri0Bakw = extractVertices(trises[0].bakw);
|
|
197
|
+
const tri1Bakw = extractVertices(trises[1].bakw);
|
|
198
|
+
const tri0Forw = extractVertices(trises[0].forw);
|
|
199
|
+
const tri1Forw = extractVertices(trises[1].forw);
|
|
200
|
+
|
|
201
|
+
const sharedBakw = sharedKeys.map((vertexKey) =>
|
|
202
|
+
tri0Bakw.find((info) => `${info.prop.index}` === vertexKey) ||
|
|
203
|
+
tri1Bakw.find((info) => `${info.prop.index}` === vertexKey),
|
|
204
|
+
);
|
|
205
|
+
const sharedForw = sharedKeys.map((vertexKey) =>
|
|
206
|
+
tri0Forw.find((info) => `${info.prop.index}` === vertexKey) ||
|
|
207
|
+
tri1Forw.find((info) => `${info.prop.index}` === vertexKey),
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
if (sharedBakw.some((vertex) => !vertex) || sharedForw.some((vertex) => !vertex)) {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const nonSharedBakw = [tri0Bakw, tri1Bakw].map((vertices) =>
|
|
215
|
+
vertices.find((info) => !sharedKeys.includes(`${info.prop.index}`)),
|
|
216
|
+
);
|
|
217
|
+
const nonSharedForw = [tri0Forw, tri1Forw].map((vertices) =>
|
|
218
|
+
vertices.find((info) => !sharedKeys.includes(`${info.prop.index}`)),
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
if (
|
|
222
|
+
nonSharedBakw.some((vertex) => !vertex) ||
|
|
223
|
+
nonSharedForw.some((vertex) => !vertex)
|
|
224
|
+
) {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const triangle0Bakw = trises[0].bakw.geometry!.coordinates[0]
|
|
229
|
+
.slice(0, 3)
|
|
230
|
+
.map((coord) => toXY(coord as Position)) as [number, number][];
|
|
231
|
+
const triangle1Bakw = trises[1].bakw.geometry!.coordinates[0]
|
|
232
|
+
.slice(0, 3)
|
|
233
|
+
.map((coord) => toXY(coord as Position)) as [number, number][];
|
|
234
|
+
|
|
235
|
+
const triangle0Forw = trises[0].forw.geometry!.coordinates[0]
|
|
236
|
+
.slice(0, 3)
|
|
237
|
+
.map((coord) => toXY(coord as Position)) as [number, number][];
|
|
238
|
+
const triangle1Forw = trises[1].forw.geometry!.coordinates[0]
|
|
239
|
+
.slice(0, 3)
|
|
240
|
+
.map((coord) => toXY(coord as Position)) as [number, number][];
|
|
241
|
+
|
|
242
|
+
// 縮退三角形(面積ゼロ)の検出: pointInTriangle は denom=0 のとき false を返すため
|
|
243
|
+
// 別途面積チェックを行い、縮退三角形もフリップ候補に含める。
|
|
244
|
+
// ただしフリップが有効か事前に隣接チェックを行う(後述)。
|
|
245
|
+
const bakwDegen0 = isDegenerate(triangle0Bakw); // T0 が縮退(bakw)
|
|
246
|
+
const bakwDegen1 = isDegenerate(triangle1Bakw); // T1 が縮退(bakw)
|
|
247
|
+
|
|
248
|
+
// forw 側の縮退検出(forw kink の原因)
|
|
249
|
+
const forwDegen0 = isDegenerate(triangle0Forw); // T0 が縮退(forw)
|
|
250
|
+
const forwDegen1 = isDegenerate(triangle1Forw); // T1 が縮退(forw)
|
|
251
|
+
|
|
252
|
+
const degenerateFlipValid = checkBakwDegenerateFlip(
|
|
253
|
+
bakwDegen0,
|
|
254
|
+
bakwDegen1,
|
|
255
|
+
nonSharedBakw,
|
|
256
|
+
sharedBakw,
|
|
257
|
+
searchIndex,
|
|
258
|
+
trises,
|
|
259
|
+
);
|
|
260
|
+
const forwDegenerateFlipValid = checkForwDegenerateFlip(
|
|
261
|
+
forwDegen0,
|
|
262
|
+
forwDegen1,
|
|
263
|
+
nonSharedBakw,
|
|
264
|
+
sharedBakw,
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
const overlaps =
|
|
268
|
+
(degenerateFlipValid) ||
|
|
269
|
+
(forwDegenerateFlipValid) ||
|
|
270
|
+
pointInTriangle(
|
|
271
|
+
toXY(nonSharedBakw[0]!.geom as Position),
|
|
272
|
+
triangle1Bakw,
|
|
273
|
+
) ||
|
|
274
|
+
pointInTriangle(
|
|
275
|
+
toXY(nonSharedBakw[1]!.geom as Position),
|
|
276
|
+
triangle0Bakw,
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
if (!overlaps) {
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const sharedForwPositions = sharedForw.map((item) =>
|
|
284
|
+
toXY(item!.geom as Position)
|
|
285
|
+
) as [number, number][];
|
|
286
|
+
const nonSharedForwPositions = nonSharedForw.map((item) =>
|
|
287
|
+
toXY(item!.geom as Position)
|
|
288
|
+
) as [number, number][];
|
|
289
|
+
|
|
290
|
+
const hull = convexHull([
|
|
291
|
+
...sharedForwPositions,
|
|
292
|
+
...nonSharedForwPositions,
|
|
293
|
+
]);
|
|
294
|
+
const hullArea = polygonArea(hull);
|
|
295
|
+
const unionArea = triangleArea(
|
|
296
|
+
sharedForwPositions[0],
|
|
297
|
+
sharedForwPositions[1],
|
|
298
|
+
nonSharedForwPositions[0],
|
|
299
|
+
) + triangleArea(
|
|
300
|
+
sharedForwPositions[0],
|
|
301
|
+
sharedForwPositions[1],
|
|
302
|
+
nonSharedForwPositions[1],
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
if (!almostEqual(hullArea, unionArea)) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
removeSearchIndex(searchIndex, trises[0], tins);
|
|
310
|
+
removeSearchIndex(searchIndex, trises[1], tins);
|
|
311
|
+
|
|
312
|
+
sharedBakw.forEach((shared) => {
|
|
313
|
+
if (!shared) return;
|
|
314
|
+
const newCoords = [
|
|
315
|
+
shared.geom,
|
|
316
|
+
nonSharedBakw[0]!.geom,
|
|
317
|
+
nonSharedBakw[1]!.geom,
|
|
318
|
+
shared.geom,
|
|
319
|
+
];
|
|
320
|
+
const newProps = {
|
|
321
|
+
a: shared.prop,
|
|
322
|
+
b: nonSharedBakw[0]!.prop,
|
|
323
|
+
c: nonSharedBakw[1]!.prop,
|
|
324
|
+
} as Tri["properties"];
|
|
325
|
+
const newBakTri = polygon([newCoords], newProps) as Tri;
|
|
326
|
+
const newForTri = counterTri(newBakTri);
|
|
327
|
+
|
|
328
|
+
insertSearchIndex(searchIndex, {
|
|
329
|
+
forw: newForTri,
|
|
330
|
+
bakw: newBakTri,
|
|
331
|
+
}, tins);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
anyFlippedThisIter = true;
|
|
335
|
+
repaired = true;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (!anyFlippedThisIter) break;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return repaired;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function toXY(position: Position): [number, number] {
|
|
345
|
+
return [position[0], position[1]];
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function pointInTriangle(
|
|
349
|
+
point: [number, number],
|
|
350
|
+
triangle: [number, number][],
|
|
351
|
+
): boolean {
|
|
352
|
+
const [ax, ay] = triangle[0];
|
|
353
|
+
const [bx, by] = triangle[1];
|
|
354
|
+
const [cx, cy] = triangle[2];
|
|
355
|
+
|
|
356
|
+
const v0x = cx - ax;
|
|
357
|
+
const v0y = cy - ay;
|
|
358
|
+
const v1x = bx - ax;
|
|
359
|
+
const v1y = by - ay;
|
|
360
|
+
const v2x = point[0] - ax;
|
|
361
|
+
const v2y = point[1] - ay;
|
|
362
|
+
|
|
363
|
+
const dot00 = v0x * v0x + v0y * v0y;
|
|
364
|
+
const dot01 = v0x * v1x + v0y * v1y;
|
|
365
|
+
const dot02 = v0x * v2x + v0y * v2y;
|
|
366
|
+
const dot11 = v1x * v1x + v1y * v1y;
|
|
367
|
+
const dot12 = v1x * v2x + v1y * v2y;
|
|
368
|
+
|
|
369
|
+
const denom = dot00 * dot11 - dot01 * dot01;
|
|
370
|
+
if (denom === 0) return false;
|
|
371
|
+
|
|
372
|
+
const invDenom = 1 / denom;
|
|
373
|
+
const u = (dot11 * dot02 - dot01 * dot12) * invDenom;
|
|
374
|
+
const v = (dot00 * dot12 - dot01 * dot02) * invDenom;
|
|
375
|
+
|
|
376
|
+
const epsilon = 1e-9;
|
|
377
|
+
return u >= -epsilon && v >= -epsilon && u + v <= 1 + epsilon;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function convexHull(points: [number, number][]): [number, number][] {
|
|
381
|
+
const unique = points
|
|
382
|
+
.map((point) => point.slice() as [number, number])
|
|
383
|
+
.filter(
|
|
384
|
+
(point, index, arr) =>
|
|
385
|
+
arr.findIndex(
|
|
386
|
+
(candidate) =>
|
|
387
|
+
almostEqual(candidate[0], point[0]) &&
|
|
388
|
+
almostEqual(candidate[1], point[1]),
|
|
389
|
+
) === index,
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
if (unique.length <= 1) return unique;
|
|
393
|
+
|
|
394
|
+
const sorted = unique.sort((a, b) =>
|
|
395
|
+
a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]
|
|
396
|
+
);
|
|
397
|
+
|
|
398
|
+
const cross = (
|
|
399
|
+
origin: [number, number],
|
|
400
|
+
p1: [number, number],
|
|
401
|
+
p2: [number, number],
|
|
402
|
+
) => (p1[0] - origin[0]) * (p2[1] - origin[1]) -
|
|
403
|
+
(p1[1] - origin[1]) * (p2[0] - origin[0]);
|
|
404
|
+
|
|
405
|
+
const lower: [number, number][] = [];
|
|
406
|
+
for (const point of sorted) {
|
|
407
|
+
while (lower.length >= 2 && cross(
|
|
408
|
+
lower[lower.length - 2],
|
|
409
|
+
lower[lower.length - 1],
|
|
410
|
+
point,
|
|
411
|
+
) <= 0) {
|
|
412
|
+
lower.pop();
|
|
413
|
+
}
|
|
414
|
+
lower.push(point);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const upper: [number, number][] = [];
|
|
418
|
+
for (let i = sorted.length - 1; i >= 0; i--) {
|
|
419
|
+
const point = sorted[i];
|
|
420
|
+
while (upper.length >= 2 && cross(
|
|
421
|
+
upper[upper.length - 2],
|
|
422
|
+
upper[upper.length - 1],
|
|
423
|
+
point,
|
|
424
|
+
) <= 0) {
|
|
425
|
+
upper.pop();
|
|
426
|
+
}
|
|
427
|
+
upper.push(point);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
upper.pop();
|
|
431
|
+
lower.pop();
|
|
432
|
+
|
|
433
|
+
return lower.concat(upper);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function polygonArea(points: [number, number][]): number {
|
|
437
|
+
if (points.length < 3) return 0;
|
|
438
|
+
let area = 0;
|
|
439
|
+
for (let i = 0; i < points.length; i++) {
|
|
440
|
+
const [x1, y1] = points[i];
|
|
441
|
+
const [x2, y2] = points[(i + 1) % points.length];
|
|
442
|
+
area += x1 * y2 - x2 * y1;
|
|
443
|
+
}
|
|
444
|
+
return Math.abs(area) / 2;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function triangleArea(
|
|
448
|
+
a: [number, number],
|
|
449
|
+
b: [number, number],
|
|
450
|
+
c: [number, number],
|
|
451
|
+
): number {
|
|
452
|
+
return Math.abs(
|
|
453
|
+
(a[0] * (b[1] - c[1]) +
|
|
454
|
+
b[0] * (c[1] - a[1]) +
|
|
455
|
+
c[0] * (a[1] - b[1])) / 2,
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function almostEqual(a: number, b: number, epsilon = 1e-9): boolean {
|
|
460
|
+
return Math.abs(a - b) <= epsilon;
|
|
461
|
+
}
|