@maplat/tin 0.11.4 → 0.12.0
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 +30 -35
- package/README.md +30 -35
- package/dist/boundary-vertices.d.ts +27 -0
- package/dist/edgebound/base.d.ts +31 -0
- package/dist/edgebound/bitset.d.ts +58 -0
- package/dist/edgebound/constrain.d.ts +68 -0
- package/dist/edgebound/index.d.ts +4 -0
- package/dist/index.cjs +7 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.html +19 -12
- package/dist/maplat_tin.cjs +4 -2
- package/dist/maplat_tin.js +2641 -1592
- package/dist/maplat_tin.umd.js +4 -2
- package/dist/searchutils.d.ts +11 -2
- package/dist/strict-overlap.d.ts +5 -0
- package/dist/tin.d.ts +12 -18
- package/dist/weight-buffer.d.ts +11 -0
- package/package.json +40 -41
- package/src/boundary-vertices.ts +284 -0
- package/src/constrained-tin.ts +1 -1
- package/src/edgebound/base.ts +65 -0
- package/src/edgebound/bitset.ts +109 -0
- package/src/edgebound/constrain.license.txt +15 -0
- package/src/edgebound/constrain.ts +498 -0
- package/src/edgebound/index.ts +10 -0
- package/src/index.ts +1 -0
- package/src/kinks.ts +13 -16
- package/src/searchutils.ts +36 -2
- package/src/strict-overlap.ts +281 -0
- package/src/tin.ts +50 -382
- package/src/weight-buffer.ts +111 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { polygon } from "@turf/helpers";
|
|
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
|
+
function isConstraintEdge(key: string, edges: ConstraintEdges): boolean {
|
|
11
|
+
const parts = key.split("-");
|
|
12
|
+
if (parts.length !== 2) return false;
|
|
13
|
+
if (!parts.every((value) => /^-?\d+$/.test(value))) return false;
|
|
14
|
+
|
|
15
|
+
const [a, b] = parts.map((value) => parseInt(value, 10)).sort((x, y) => x - y);
|
|
16
|
+
|
|
17
|
+
return edges.some((edge) => {
|
|
18
|
+
if (edge.length !== 2) return false;
|
|
19
|
+
const parsed = edge.map((value) => parseInt(`${value}`, 10));
|
|
20
|
+
if (parsed.some((val) => Number.isNaN(val))) return false;
|
|
21
|
+
const sorted = parsed.sort((x, y) => x - y);
|
|
22
|
+
return sorted[0] === a && sorted[1] === b;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function extractVertices(tri: Tri) {
|
|
27
|
+
return (["a", "b", "c"] as PropertyTriKey[]).map((key, index) => ({
|
|
28
|
+
prop: tri.properties![key],
|
|
29
|
+
geom: tri.geometry!.coordinates[0][index],
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function resolveOverlaps(
|
|
34
|
+
tins: TinsBD,
|
|
35
|
+
searchIndex: SearchIndex,
|
|
36
|
+
edges: ConstraintEdges,
|
|
37
|
+
): boolean {
|
|
38
|
+
const processedKeys = new Set<string>();
|
|
39
|
+
let repaired = false;
|
|
40
|
+
|
|
41
|
+
const initialKeys = Object.keys(searchIndex);
|
|
42
|
+
for (const key of initialKeys) {
|
|
43
|
+
if (processedKeys.has(key)) continue;
|
|
44
|
+
processedKeys.add(key);
|
|
45
|
+
|
|
46
|
+
const trises = searchIndex[key];
|
|
47
|
+
if (!trises || trises.length < 2) continue;
|
|
48
|
+
|
|
49
|
+
const sharedKeys = key.split("-");
|
|
50
|
+
if (sharedKeys.length !== 2) continue;
|
|
51
|
+
|
|
52
|
+
if (isConstraintEdge(key, edges)) continue;
|
|
53
|
+
|
|
54
|
+
const tri0Bakw = extractVertices(trises[0].bakw);
|
|
55
|
+
const tri1Bakw = extractVertices(trises[1].bakw);
|
|
56
|
+
const tri0Forw = extractVertices(trises[0].forw);
|
|
57
|
+
const tri1Forw = extractVertices(trises[1].forw);
|
|
58
|
+
|
|
59
|
+
const sharedBakw = sharedKeys.map((vertexKey) =>
|
|
60
|
+
tri0Bakw.find((info) => `${info.prop.index}` === vertexKey) ||
|
|
61
|
+
tri1Bakw.find((info) => `${info.prop.index}` === vertexKey),
|
|
62
|
+
);
|
|
63
|
+
const sharedForw = sharedKeys.map((vertexKey) =>
|
|
64
|
+
tri0Forw.find((info) => `${info.prop.index}` === vertexKey) ||
|
|
65
|
+
tri1Forw.find((info) => `${info.prop.index}` === vertexKey),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
if (sharedBakw.some((vertex) => !vertex) || sharedForw.some((vertex) => !vertex)) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const nonSharedBakw = [tri0Bakw, tri1Bakw].map((vertices) =>
|
|
73
|
+
vertices.find((info) => !sharedKeys.includes(`${info.prop.index}`)),
|
|
74
|
+
);
|
|
75
|
+
const nonSharedForw = [tri0Forw, tri1Forw].map((vertices) =>
|
|
76
|
+
vertices.find((info) => !sharedKeys.includes(`${info.prop.index}`)),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
if (
|
|
80
|
+
nonSharedBakw.some((vertex) => !vertex) ||
|
|
81
|
+
nonSharedForw.some((vertex) => !vertex)
|
|
82
|
+
) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const triangle0Bakw = trises[0].bakw.geometry!.coordinates[0]
|
|
87
|
+
.slice(0, 3)
|
|
88
|
+
.map((coord) => toXY(coord as Position)) as [number, number][];
|
|
89
|
+
const triangle1Bakw = trises[1].bakw.geometry!.coordinates[0]
|
|
90
|
+
.slice(0, 3)
|
|
91
|
+
.map((coord) => toXY(coord as Position)) as [number, number][];
|
|
92
|
+
|
|
93
|
+
const overlaps =
|
|
94
|
+
pointInTriangle(
|
|
95
|
+
toXY(nonSharedBakw[0]!.geom as Position),
|
|
96
|
+
triangle1Bakw,
|
|
97
|
+
) ||
|
|
98
|
+
pointInTriangle(
|
|
99
|
+
toXY(nonSharedBakw[1]!.geom as Position),
|
|
100
|
+
triangle0Bakw,
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
if (!overlaps) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const sharedForwPositions = sharedForw.map((item) =>
|
|
108
|
+
toXY(item!.geom as Position)
|
|
109
|
+
) as [number, number][];
|
|
110
|
+
const nonSharedForwPositions = nonSharedForw.map((item) =>
|
|
111
|
+
toXY(item!.geom as Position)
|
|
112
|
+
) as [number, number][];
|
|
113
|
+
|
|
114
|
+
const hull = convexHull([
|
|
115
|
+
...sharedForwPositions,
|
|
116
|
+
...nonSharedForwPositions,
|
|
117
|
+
]);
|
|
118
|
+
const hullArea = polygonArea(hull);
|
|
119
|
+
const unionArea = triangleArea(
|
|
120
|
+
sharedForwPositions[0],
|
|
121
|
+
sharedForwPositions[1],
|
|
122
|
+
nonSharedForwPositions[0],
|
|
123
|
+
) + triangleArea(
|
|
124
|
+
sharedForwPositions[0],
|
|
125
|
+
sharedForwPositions[1],
|
|
126
|
+
nonSharedForwPositions[1],
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
if (!almostEqual(hullArea, unionArea)) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
removeSearchIndex(searchIndex, trises[0], tins);
|
|
134
|
+
removeSearchIndex(searchIndex, trises[1], tins);
|
|
135
|
+
|
|
136
|
+
sharedBakw.forEach((shared) => {
|
|
137
|
+
if (!shared) return;
|
|
138
|
+
const newCoords = [
|
|
139
|
+
shared.geom,
|
|
140
|
+
nonSharedBakw[0]!.geom,
|
|
141
|
+
nonSharedBakw[1]!.geom,
|
|
142
|
+
shared.geom,
|
|
143
|
+
];
|
|
144
|
+
const newProps = {
|
|
145
|
+
a: shared.prop,
|
|
146
|
+
b: nonSharedBakw[0]!.prop,
|
|
147
|
+
c: nonSharedBakw[1]!.prop,
|
|
148
|
+
} as Tri["properties"];
|
|
149
|
+
const newBakTri = polygon([newCoords], newProps) as Tri;
|
|
150
|
+
const newForTri = counterTri(newBakTri);
|
|
151
|
+
|
|
152
|
+
insertSearchIndex(searchIndex, {
|
|
153
|
+
forw: newForTri,
|
|
154
|
+
bakw: newBakTri,
|
|
155
|
+
}, tins);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
repaired = true;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return repaired;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function toXY(position: Position): [number, number] {
|
|
165
|
+
return [position[0], position[1]];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function pointInTriangle(
|
|
169
|
+
point: [number, number],
|
|
170
|
+
triangle: [number, number][],
|
|
171
|
+
): boolean {
|
|
172
|
+
const [ax, ay] = triangle[0];
|
|
173
|
+
const [bx, by] = triangle[1];
|
|
174
|
+
const [cx, cy] = triangle[2];
|
|
175
|
+
|
|
176
|
+
const v0x = cx - ax;
|
|
177
|
+
const v0y = cy - ay;
|
|
178
|
+
const v1x = bx - ax;
|
|
179
|
+
const v1y = by - ay;
|
|
180
|
+
const v2x = point[0] - ax;
|
|
181
|
+
const v2y = point[1] - ay;
|
|
182
|
+
|
|
183
|
+
const dot00 = v0x * v0x + v0y * v0y;
|
|
184
|
+
const dot01 = v0x * v1x + v0y * v1y;
|
|
185
|
+
const dot02 = v0x * v2x + v0y * v2y;
|
|
186
|
+
const dot11 = v1x * v1x + v1y * v1y;
|
|
187
|
+
const dot12 = v1x * v2x + v1y * v2y;
|
|
188
|
+
|
|
189
|
+
const denom = dot00 * dot11 - dot01 * dot01;
|
|
190
|
+
if (denom === 0) return false;
|
|
191
|
+
|
|
192
|
+
const invDenom = 1 / denom;
|
|
193
|
+
const u = (dot11 * dot02 - dot01 * dot12) * invDenom;
|
|
194
|
+
const v = (dot00 * dot12 - dot01 * dot02) * invDenom;
|
|
195
|
+
|
|
196
|
+
const epsilon = 1e-9;
|
|
197
|
+
return u >= -epsilon && v >= -epsilon && u + v <= 1 + epsilon;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function convexHull(points: [number, number][]): [number, number][] {
|
|
201
|
+
const unique = points
|
|
202
|
+
.map((point) => point.slice() as [number, number])
|
|
203
|
+
.filter(
|
|
204
|
+
(point, index, arr) =>
|
|
205
|
+
arr.findIndex(
|
|
206
|
+
(candidate) =>
|
|
207
|
+
almostEqual(candidate[0], point[0]) &&
|
|
208
|
+
almostEqual(candidate[1], point[1]),
|
|
209
|
+
) === index,
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
if (unique.length <= 1) return unique;
|
|
213
|
+
|
|
214
|
+
const sorted = unique.sort((a, b) =>
|
|
215
|
+
a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
const cross = (
|
|
219
|
+
origin: [number, number],
|
|
220
|
+
p1: [number, number],
|
|
221
|
+
p2: [number, number],
|
|
222
|
+
) => (p1[0] - origin[0]) * (p2[1] - origin[1]) -
|
|
223
|
+
(p1[1] - origin[1]) * (p2[0] - origin[0]);
|
|
224
|
+
|
|
225
|
+
const lower: [number, number][] = [];
|
|
226
|
+
for (const point of sorted) {
|
|
227
|
+
while (lower.length >= 2 && cross(
|
|
228
|
+
lower[lower.length - 2],
|
|
229
|
+
lower[lower.length - 1],
|
|
230
|
+
point,
|
|
231
|
+
) <= 0) {
|
|
232
|
+
lower.pop();
|
|
233
|
+
}
|
|
234
|
+
lower.push(point);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const upper: [number, number][] = [];
|
|
238
|
+
for (let i = sorted.length - 1; i >= 0; i--) {
|
|
239
|
+
const point = sorted[i];
|
|
240
|
+
while (upper.length >= 2 && cross(
|
|
241
|
+
upper[upper.length - 2],
|
|
242
|
+
upper[upper.length - 1],
|
|
243
|
+
point,
|
|
244
|
+
) <= 0) {
|
|
245
|
+
upper.pop();
|
|
246
|
+
}
|
|
247
|
+
upper.push(point);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
upper.pop();
|
|
251
|
+
lower.pop();
|
|
252
|
+
|
|
253
|
+
return lower.concat(upper);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function polygonArea(points: [number, number][]): number {
|
|
257
|
+
if (points.length < 3) return 0;
|
|
258
|
+
let area = 0;
|
|
259
|
+
for (let i = 0; i < points.length; i++) {
|
|
260
|
+
const [x1, y1] = points[i];
|
|
261
|
+
const [x2, y2] = points[(i + 1) % points.length];
|
|
262
|
+
area += x1 * y2 - x2 * y1;
|
|
263
|
+
}
|
|
264
|
+
return Math.abs(area) / 2;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function triangleArea(
|
|
268
|
+
a: [number, number],
|
|
269
|
+
b: [number, number],
|
|
270
|
+
c: [number, number],
|
|
271
|
+
): number {
|
|
272
|
+
return Math.abs(
|
|
273
|
+
(a[0] * (b[1] - c[1]) +
|
|
274
|
+
b[0] * (c[1] - a[1]) +
|
|
275
|
+
c[0] * (a[1] - b[1])) / 2,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function almostEqual(a: number, b: number, epsilon = 1e-9): boolean {
|
|
280
|
+
return Math.abs(a - b) <= epsilon;
|
|
281
|
+
}
|