@maplat/tin 0.11.2 → 0.11.3
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 +218 -218
- package/README.md +219 -219
- package/dist/index.html +41 -41
- package/package.json +5 -4
- package/src/constrained-tin.ts +84 -84
- package/src/index.ts +41 -41
- package/src/kinks.ts +1039 -1039
- package/src/searchutils.ts +68 -68
- package/src/tin.ts +1027 -1027
- package/src/types/cdt2d.d.ts +4 -4
- package/src/types/constrainautor.d.ts +6 -6
- package/src/types/kinks.d.ts +49 -49
- package/src/types/tin.d.ts +54 -54
- package/src/vertexutils.ts +88 -88
package/src/tin.ts
CHANGED
|
@@ -1,1027 +1,1027 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tin (Triangulated Irregular Network) クラス
|
|
3
|
-
* 2つの平面座標系間の同相変換を実現します。
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import booleanPointInPolygon from "@turf/boolean-point-in-polygon";
|
|
7
|
-
import turfCentroid from "@turf/centroid";
|
|
8
|
-
import convex from "@turf/convex";
|
|
9
|
-
import { featureCollection, lineString, point, polygon } from "@turf/helpers";
|
|
10
|
-
import lineIntersect from "@turf/line-intersect";
|
|
11
|
-
import type { Feature, Point, Position } from "geojson";
|
|
12
|
-
import {
|
|
13
|
-
counterTri,
|
|
14
|
-
format_version,
|
|
15
|
-
normalizeEdges,
|
|
16
|
-
rotateVerticesTriangle,
|
|
17
|
-
Transform,
|
|
18
|
-
transformArr,
|
|
19
|
-
} from "@maplat/transform";
|
|
20
|
-
|
|
21
|
-
// Ensure format_version is available
|
|
22
|
-
const FALLBACK_FORMAT_VERSION = 2.00703;
|
|
23
|
-
const safeFormatVersion = typeof format_version !== "undefined"
|
|
24
|
-
? format_version
|
|
25
|
-
: FALLBACK_FORMAT_VERSION;
|
|
26
|
-
import type {
|
|
27
|
-
Compiled,
|
|
28
|
-
Edge,
|
|
29
|
-
EdgeSet,
|
|
30
|
-
EdgeSetLegacy,
|
|
31
|
-
PointSet,
|
|
32
|
-
PropertyTriKey,
|
|
33
|
-
StrictMode,
|
|
34
|
-
Tins,
|
|
35
|
-
TinsBD,
|
|
36
|
-
Tri,
|
|
37
|
-
VertexMode,
|
|
38
|
-
WeightBufferBD,
|
|
39
|
-
YaxisMode,
|
|
40
|
-
} from "@maplat/transform";
|
|
41
|
-
import constrainedTin from "./constrained-tin.ts";
|
|
42
|
-
import findIntersections from "./kinks.ts";
|
|
43
|
-
import { insertSearchIndex } from "./searchutils.ts";
|
|
44
|
-
import { counterPoint, createPoint, vertexCalc } from "./vertexutils.ts";
|
|
45
|
-
import type { SearchIndex } from "./searchutils.ts";
|
|
46
|
-
import type { PointsSetBD } from "./types/tin.d.ts";
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Tinクラスの初期化オプション
|
|
50
|
-
*/
|
|
51
|
-
export interface Options {
|
|
52
|
-
bounds?: Position[];
|
|
53
|
-
wh?: number[];
|
|
54
|
-
vertexMode?: VertexMode;
|
|
55
|
-
strictMode?: StrictMode;
|
|
56
|
-
yaxisMode?: YaxisMode;
|
|
57
|
-
importance?: number;
|
|
58
|
-
priority?: number;
|
|
59
|
-
stateFull?: boolean;
|
|
60
|
-
points?: PointSet[];
|
|
61
|
-
edges?: EdgeSet[];
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Tin (Triangulated Irregular Network) クラス
|
|
66
|
-
* Transformクラスを拡張し、TINネットワークの生成機能を追加
|
|
67
|
-
*/
|
|
68
|
-
export class Tin extends Transform {
|
|
69
|
-
importance: number;
|
|
70
|
-
priority: number;
|
|
71
|
-
pointsSet: PointsSetBD | undefined;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Tinクラスのインスタンスを生成します
|
|
75
|
-
* @param options - 初期化オプション
|
|
76
|
-
*/
|
|
77
|
-
constructor(options: Options = {}) {
|
|
78
|
-
super();
|
|
79
|
-
|
|
80
|
-
if (options.bounds) {
|
|
81
|
-
this.setBounds(options.bounds);
|
|
82
|
-
} else {
|
|
83
|
-
this.setWh(options.wh);
|
|
84
|
-
this.vertexMode = options.vertexMode || Tin.VERTEX_PLAIN;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
this.strictMode = options.strictMode || Tin.MODE_AUTO;
|
|
88
|
-
this.yaxisMode = options.yaxisMode || Tin.YAXIS_INVERT;
|
|
89
|
-
this.importance = options.importance || 0;
|
|
90
|
-
this.priority = options.priority || 0;
|
|
91
|
-
this.stateFull = options.stateFull || false;
|
|
92
|
-
|
|
93
|
-
if (options.points) {
|
|
94
|
-
this.setPoints(options.points);
|
|
95
|
-
}
|
|
96
|
-
if (options.edges) {
|
|
97
|
-
this.setEdges(options.edges);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* フォーマットバージョンを取得します
|
|
103
|
-
*/
|
|
104
|
-
getFormatVersion(): number {
|
|
105
|
-
return safeFormatVersion;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* 制御点(GCP: Ground Control Points)を設定します
|
|
110
|
-
*/
|
|
111
|
-
setPoints(points: PointSet[]): void {
|
|
112
|
-
if (this.yaxisMode === Tin.YAXIS_FOLLOW) {
|
|
113
|
-
points = points.map((point) => [
|
|
114
|
-
point[0],
|
|
115
|
-
[point[1][0], -1 * point[1][1]],
|
|
116
|
-
]);
|
|
117
|
-
}
|
|
118
|
-
this.points = points;
|
|
119
|
-
this.tins = undefined;
|
|
120
|
-
this.indexedTins = undefined;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* エッジ(制約線)を設定します
|
|
125
|
-
*/
|
|
126
|
-
setEdges(edges: EdgeSet[] | EdgeSetLegacy[] = []): void {
|
|
127
|
-
this.edges = normalizeEdges(edges);
|
|
128
|
-
this.edgeNodes = undefined;
|
|
129
|
-
this.tins = undefined;
|
|
130
|
-
this.indexedTins = undefined;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* 境界ポリゴンを設定します
|
|
135
|
-
*/
|
|
136
|
-
setBounds(bounds: Position[]): void {
|
|
137
|
-
this.bounds = bounds;
|
|
138
|
-
|
|
139
|
-
let minx = bounds[0][0];
|
|
140
|
-
let maxx = minx;
|
|
141
|
-
let miny = bounds[0][1];
|
|
142
|
-
let maxy = miny;
|
|
143
|
-
const coords = [bounds[0]];
|
|
144
|
-
|
|
145
|
-
for (let i = 1; i < bounds.length; i++) {
|
|
146
|
-
const bound = bounds[i];
|
|
147
|
-
if (bound[0] < minx) minx = bound[0];
|
|
148
|
-
if (bound[0] > maxx) maxx = bound[0];
|
|
149
|
-
if (bound[1] < miny) miny = bound[1];
|
|
150
|
-
if (bound[1] > maxy) maxy = bound[1];
|
|
151
|
-
coords.push(bound);
|
|
152
|
-
}
|
|
153
|
-
coords.push(bounds[0]);
|
|
154
|
-
|
|
155
|
-
this.boundsPolygon = polygon([coords]);
|
|
156
|
-
this.xy = [minx, miny];
|
|
157
|
-
this.wh = [maxx - minx, maxy - miny];
|
|
158
|
-
this.vertexMode = Tin.VERTEX_PLAIN;
|
|
159
|
-
this.tins = undefined;
|
|
160
|
-
this.indexedTins = undefined;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* 現在の設定を永続化可能な形式にコンパイルします
|
|
165
|
-
*/
|
|
166
|
-
getCompiled(): Compiled {
|
|
167
|
-
const compiled: any = {};
|
|
168
|
-
compiled.version = safeFormatVersion;
|
|
169
|
-
compiled.points = this.points;
|
|
170
|
-
compiled.weight_buffer = this.pointsWeightBuffer;
|
|
171
|
-
compiled.centroid_point = [
|
|
172
|
-
this.centroid!.forw!.geometry!.coordinates,
|
|
173
|
-
this.centroid!.forw!.properties!.target.geom,
|
|
174
|
-
];
|
|
175
|
-
compiled.vertices_params = [
|
|
176
|
-
this.vertices_params!.forw![0],
|
|
177
|
-
this.vertices_params!.bakw![0],
|
|
178
|
-
];
|
|
179
|
-
compiled.vertices_points = [];
|
|
180
|
-
|
|
181
|
-
const vertices = this.vertices_params!.forw![1];
|
|
182
|
-
if (vertices) {
|
|
183
|
-
[0, 1, 2, 3].map((i) => {
|
|
184
|
-
const vertex = vertices[i].features[0];
|
|
185
|
-
const forw = vertex.geometry!.coordinates[0][1];
|
|
186
|
-
const bakw = vertex.properties!.b.geom;
|
|
187
|
-
compiled.vertices_points[i] = [forw, bakw];
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
compiled.strict_status = this.strict_status;
|
|
192
|
-
compiled.tins_points = [[]];
|
|
193
|
-
|
|
194
|
-
this.tins!.forw!.features.map((tin: Tri) => {
|
|
195
|
-
compiled.tins_points[0].push(
|
|
196
|
-
(["a", "b", "c"] as PropertyTriKey[]).map((key) =>
|
|
197
|
-
tin.properties![key].index
|
|
198
|
-
),
|
|
199
|
-
);
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
if (this.strict_status === Tin.STATUS_LOOSE) {
|
|
203
|
-
compiled.tins_points[1] = [];
|
|
204
|
-
this.tins!.bakw!.features.map((tin: Tri) => {
|
|
205
|
-
compiled.tins_points[1].push(
|
|
206
|
-
(["a", "b", "c"] as PropertyTriKey[]).map((key) =>
|
|
207
|
-
tin.properties![key].index
|
|
208
|
-
),
|
|
209
|
-
);
|
|
210
|
-
});
|
|
211
|
-
} else if (this.strict_status === Tin.STATUS_ERROR && this.kinks?.bakw) {
|
|
212
|
-
compiled.kinks_points = this.kinks.bakw.features.map(
|
|
213
|
-
(kink) => kink.geometry!.coordinates,
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
compiled.yaxisMode = this.yaxisMode;
|
|
218
|
-
compiled.vertexMode = this.vertexMode;
|
|
219
|
-
compiled.strictMode = this.strictMode;
|
|
220
|
-
|
|
221
|
-
if (this.bounds) {
|
|
222
|
-
compiled.bounds = this.bounds;
|
|
223
|
-
compiled.boundsPolygon = this.boundsPolygon;
|
|
224
|
-
compiled.xy = this.xy;
|
|
225
|
-
compiled.wh = this.wh;
|
|
226
|
-
} else {
|
|
227
|
-
compiled.wh = this.wh;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
compiled.edges = this.edges;
|
|
231
|
-
compiled.edgeNodes = this.edgeNodes;
|
|
232
|
-
|
|
233
|
-
return compiled;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* 幅と高さを設定します
|
|
238
|
-
*/
|
|
239
|
-
setWh(wh?: number[]): void {
|
|
240
|
-
this.wh = wh || [100, 100]; // デフォルト値を設定
|
|
241
|
-
this.xy = [0, 0];
|
|
242
|
-
this.bounds = undefined;
|
|
243
|
-
this.boundsPolygon = undefined;
|
|
244
|
-
this.tins = undefined;
|
|
245
|
-
this.indexedTins = undefined;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* 頂点モードを設定します
|
|
250
|
-
*/
|
|
251
|
-
setVertexMode(mode: VertexMode): void {
|
|
252
|
-
this.vertexMode = mode;
|
|
253
|
-
this.tins = undefined;
|
|
254
|
-
this.indexedTins = undefined;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* 厳密性モードを設定します
|
|
259
|
-
*/
|
|
260
|
-
setStrictMode(mode: StrictMode): void {
|
|
261
|
-
this.strictMode = mode;
|
|
262
|
-
this.tins = undefined;
|
|
263
|
-
this.indexedTins = undefined;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* 厳密なTINを計算します
|
|
268
|
-
*/
|
|
269
|
-
calcurateStrictTin(): void {
|
|
270
|
-
const bakTins = this.tins!.forw!.features.map((tri: Tri) =>
|
|
271
|
-
counterTri(tri)
|
|
272
|
-
);
|
|
273
|
-
this.tins!.bakw = featureCollection(bakTins);
|
|
274
|
-
|
|
275
|
-
const searchIndex: SearchIndex = {};
|
|
276
|
-
this.tins!.forw!.features.forEach((forTri: Tri, index: number) => {
|
|
277
|
-
const bakTri = this.tins!.bakw!.features[index];
|
|
278
|
-
insertSearchIndex(searchIndex, { forw: forTri, bakw: bakTri });
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
const kinks = ["forw", "bakw"].map((direction) => {
|
|
282
|
-
const tins = this.tins![direction as keyof TinsBD]!.features.map(
|
|
283
|
-
(tin: Tri) => tin.geometry!.coordinates[0],
|
|
284
|
-
);
|
|
285
|
-
return findIntersections(tins);
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
if (kinks[0].length === 0 && kinks[1].length === 0) {
|
|
289
|
-
this.strict_status = Tin.STATUS_STRICT;
|
|
290
|
-
delete this.kinks;
|
|
291
|
-
} else {
|
|
292
|
-
this.strict_status = Tin.STATUS_ERROR;
|
|
293
|
-
this.kinks = {};
|
|
294
|
-
if (kinks[0].length > 0) {
|
|
295
|
-
this.kinks.forw = featureCollection(kinks[0]);
|
|
296
|
-
}
|
|
297
|
-
if (kinks[1].length > 0) {
|
|
298
|
-
this.kinks.bakw = featureCollection(kinks[1]);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* 点群セットを生成します
|
|
305
|
-
*/
|
|
306
|
-
generatePointsSet(): {
|
|
307
|
-
forw: Feature<Point>[];
|
|
308
|
-
bakw: Feature<Point>[];
|
|
309
|
-
edges: Edge[];
|
|
310
|
-
} {
|
|
311
|
-
const pointsSet: { forw: Feature<Point>[]; bakw: Feature<Point>[] } = {
|
|
312
|
-
forw: [],
|
|
313
|
-
bakw: [],
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
// Generate points
|
|
317
|
-
for (let i = 0; i < this.points.length; i++) {
|
|
318
|
-
const forw = this.points[i][0];
|
|
319
|
-
const bakw = this.points[i][1];
|
|
320
|
-
const forPoint = createPoint(forw, bakw, i);
|
|
321
|
-
pointsSet.forw.push(forPoint);
|
|
322
|
-
pointsSet.bakw.push(counterPoint(forPoint));
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// Generate edge nodes
|
|
326
|
-
const edges: Edge[] = [];
|
|
327
|
-
let edgeNodeIndex = 0;
|
|
328
|
-
this.edgeNodes = [];
|
|
329
|
-
|
|
330
|
-
if (!this.edges) this.edges = [];
|
|
331
|
-
|
|
332
|
-
for (let i = 0; i < this.edges.length; i++) {
|
|
333
|
-
const edge = this.edges[i][2];
|
|
334
|
-
const illstNodes = Object.assign([], this.edges[i][0]);
|
|
335
|
-
const mercNodes = Object.assign([], this.edges[i][1]);
|
|
336
|
-
|
|
337
|
-
if (illstNodes.length === 0 && mercNodes.length === 0) {
|
|
338
|
-
edges.push(edge);
|
|
339
|
-
continue;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// Add start and end points
|
|
343
|
-
illstNodes.unshift(this.points[edge[0]][0]);
|
|
344
|
-
illstNodes.push(this.points[edge[1]][0]);
|
|
345
|
-
mercNodes.unshift(this.points[edge[0]][1]);
|
|
346
|
-
mercNodes.push(this.points[edge[1]][1]);
|
|
347
|
-
|
|
348
|
-
// Calculate edge segments
|
|
349
|
-
const segments = [illstNodes, mercNodes].map((nodes) => {
|
|
350
|
-
const lengths = nodes.map((node, index, arr) => {
|
|
351
|
-
if (index === 0) return 0;
|
|
352
|
-
const prev = arr[index - 1];
|
|
353
|
-
return Math.sqrt(
|
|
354
|
-
Math.pow(node[0] - prev[0], 2) + Math.pow(node[1] - prev[1], 2),
|
|
355
|
-
);
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
const accumLengths = lengths.reduce((acc, len, idx) => {
|
|
359
|
-
if (idx === 0) return [0];
|
|
360
|
-
acc.push(acc[idx - 1] + len);
|
|
361
|
-
return acc;
|
|
362
|
-
}, [] as number[]);
|
|
363
|
-
|
|
364
|
-
return accumLengths.map((accum, idx, arr) => {
|
|
365
|
-
const ratio = accum / arr[arr.length - 1];
|
|
366
|
-
return [nodes[idx], lengths[idx], accumLengths[idx], ratio];
|
|
367
|
-
});
|
|
368
|
-
});
|
|
369
|
-
|
|
370
|
-
// Generate edge nodes
|
|
371
|
-
segments
|
|
372
|
-
.map((segment, idx) => {
|
|
373
|
-
const otherSegment = segments[idx ? 0 : 1];
|
|
374
|
-
return segment
|
|
375
|
-
.filter((item, index) => {
|
|
376
|
-
return !(
|
|
377
|
-
index === 0 ||
|
|
378
|
-
index === segment.length - 1 ||
|
|
379
|
-
(item as any)[4] === "handled"
|
|
380
|
-
);
|
|
381
|
-
})
|
|
382
|
-
.map((item) => {
|
|
383
|
-
const node = item[0] as unknown as Position;
|
|
384
|
-
const ratio = item[3] as number;
|
|
385
|
-
|
|
386
|
-
const counterpart = otherSegment.reduce(
|
|
387
|
-
(prev, curr, currIdx, arr) => {
|
|
388
|
-
if (prev) return prev;
|
|
389
|
-
const nextItem = arr[currIdx + 1];
|
|
390
|
-
if ((curr[3] as number) === ratio) {
|
|
391
|
-
(curr as any)[4] = "handled";
|
|
392
|
-
return [curr];
|
|
393
|
-
}
|
|
394
|
-
if (
|
|
395
|
-
(curr[3] as number) < ratio && nextItem &&
|
|
396
|
-
(nextItem[3] as number) > ratio
|
|
397
|
-
) {
|
|
398
|
-
return [curr, nextItem];
|
|
399
|
-
}
|
|
400
|
-
return undefined;
|
|
401
|
-
},
|
|
402
|
-
undefined as any,
|
|
403
|
-
);
|
|
404
|
-
|
|
405
|
-
if (counterpart && counterpart.length === 1) {
|
|
406
|
-
return idx === 0
|
|
407
|
-
? [node, counterpart[0][0], ratio]
|
|
408
|
-
: [counterpart[0][0], node, ratio];
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
if (counterpart && counterpart.length === 2) {
|
|
412
|
-
const curr = counterpart[0];
|
|
413
|
-
const next = counterpart[1];
|
|
414
|
-
const ratioInSegment = (ratio - (curr[3] as number)) /
|
|
415
|
-
((next[3] as number) - (curr[3] as number));
|
|
416
|
-
const interpNode: Position = [
|
|
417
|
-
((next[0] as Position)[0] - (curr[0] as Position)[0]) *
|
|
418
|
-
ratioInSegment + (curr[0] as Position)[0],
|
|
419
|
-
((next[0] as Position)[1] - (curr[0] as Position)[1]) *
|
|
420
|
-
ratioInSegment + (curr[0] as Position)[1],
|
|
421
|
-
];
|
|
422
|
-
return idx === 0
|
|
423
|
-
? [node, interpNode, ratio]
|
|
424
|
-
: [interpNode, node, ratio];
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
return [];
|
|
428
|
-
});
|
|
429
|
-
})
|
|
430
|
-
.reduce((prev, curr) => prev.concat(curr), [])
|
|
431
|
-
.sort((a, b) => (a[2] as number) < (b[2] as number) ? -1 : 1)
|
|
432
|
-
.map((item, index, arr) => {
|
|
433
|
-
this.edgeNodes![edgeNodeIndex] = [
|
|
434
|
-
item[0] as Position,
|
|
435
|
-
item[1] as Position,
|
|
436
|
-
];
|
|
437
|
-
const forPoint = createPoint(
|
|
438
|
-
item[0] as Position,
|
|
439
|
-
item[1] as Position,
|
|
440
|
-
`e${edgeNodeIndex}`,
|
|
441
|
-
);
|
|
442
|
-
edgeNodeIndex++;
|
|
443
|
-
pointsSet.forw!.push(forPoint);
|
|
444
|
-
pointsSet.bakw!.push(counterPoint(forPoint));
|
|
445
|
-
|
|
446
|
-
if (index === 0) {
|
|
447
|
-
edges.push([edge[0], pointsSet.forw!.length - 1]);
|
|
448
|
-
} else {
|
|
449
|
-
edges.push([
|
|
450
|
-
pointsSet.forw!.length - 2,
|
|
451
|
-
pointsSet.forw!.length - 1,
|
|
452
|
-
]);
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
if (index === arr.length - 1) {
|
|
456
|
-
edges.push([pointsSet.forw!.length - 1, edge[1]]);
|
|
457
|
-
}
|
|
458
|
-
});
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
return {
|
|
462
|
-
forw: pointsSet.forw,
|
|
463
|
-
bakw: pointsSet.bakw,
|
|
464
|
-
edges,
|
|
465
|
-
};
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
/**
|
|
469
|
-
* 入力データの検証と初期データの準備
|
|
470
|
-
*/
|
|
471
|
-
private validateAndPrepareInputs() {
|
|
472
|
-
const minx = this.xy![0] - 0.05 * this.wh![0];
|
|
473
|
-
const maxx = this.xy![0] + 1.05 * this.wh![0];
|
|
474
|
-
const miny = this.xy![1] - 0.05 * this.wh![1];
|
|
475
|
-
const maxy = this.xy![1] + 1.05 * this.wh![1];
|
|
476
|
-
|
|
477
|
-
const allPointsInside = this.points.reduce((prev, point) => {
|
|
478
|
-
return prev &&
|
|
479
|
-
(this.bounds
|
|
480
|
-
? booleanPointInPolygon(point[0] as any, this.boundsPolygon!)
|
|
481
|
-
: point[0][0] >= minx && point[0][0] <= maxx &&
|
|
482
|
-
point[0][1] >= miny && point[0][1] <= maxy);
|
|
483
|
-
}, true);
|
|
484
|
-
|
|
485
|
-
if (!allPointsInside) {
|
|
486
|
-
throw "SOME POINTS OUTSIDE";
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
let bbox: Position[] = [];
|
|
490
|
-
if (this.wh) {
|
|
491
|
-
bbox = [[minx, miny], [maxx, miny], [minx, maxy], [maxx, maxy]];
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
return {
|
|
495
|
-
pointsSet: this.generatePointsSet(),
|
|
496
|
-
bbox,
|
|
497
|
-
minx,
|
|
498
|
-
maxx,
|
|
499
|
-
miny,
|
|
500
|
-
maxy,
|
|
501
|
-
};
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
/**
|
|
505
|
-
* TINネットワークを更新し、座標変換の準備を行います
|
|
506
|
-
*/
|
|
507
|
-
updateTin(): void {
|
|
508
|
-
let strict = this.strictMode;
|
|
509
|
-
if (strict !== Tin.MODE_STRICT && strict !== Tin.MODE_LOOSE) {
|
|
510
|
-
strict = Tin.MODE_AUTO;
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
const { pointsSet: rawPointsSet, bbox, minx, maxx, miny, maxy } = this
|
|
514
|
-
.validateAndPrepareInputs();
|
|
515
|
-
|
|
516
|
-
// Create FeatureCollections for use in calculations
|
|
517
|
-
const pointsSetFC = {
|
|
518
|
-
forw: featureCollection(rawPointsSet.forw),
|
|
519
|
-
bakw: featureCollection(rawPointsSet.bakw),
|
|
520
|
-
};
|
|
521
|
-
|
|
522
|
-
const tinForw = constrainedTin(
|
|
523
|
-
pointsSetFC.forw,
|
|
524
|
-
rawPointsSet.edges,
|
|
525
|
-
"target",
|
|
526
|
-
);
|
|
527
|
-
const tinBakw = constrainedTin(
|
|
528
|
-
pointsSetFC.bakw,
|
|
529
|
-
rawPointsSet.edges,
|
|
530
|
-
"target",
|
|
531
|
-
);
|
|
532
|
-
|
|
533
|
-
if (tinForw.features.length === 0 || tinBakw.features.length === 0) {
|
|
534
|
-
throw "TOO LINEAR1";
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
const forCentroid = turfCentroid(pointsSetFC.forw);
|
|
538
|
-
const forwConvex = convex(pointsSetFC.forw);
|
|
539
|
-
if (!forwConvex) throw "TOO LINEAR2";
|
|
540
|
-
|
|
541
|
-
const convexBuf: Record<string, { forw: Position; bakw: Position }> = {};
|
|
542
|
-
const forwCoords = forwConvex.geometry!.coordinates[0];
|
|
543
|
-
|
|
544
|
-
// Calculate forward convex hull transformation
|
|
545
|
-
let convexCalc;
|
|
546
|
-
try {
|
|
547
|
-
convexCalc = forwCoords.map((coord: Position) => ({
|
|
548
|
-
forw: coord,
|
|
549
|
-
bakw: transformArr(point(coord), tinForw as any) as Position,
|
|
550
|
-
}));
|
|
551
|
-
convexCalc.forEach((item: any) => {
|
|
552
|
-
convexBuf[`${item.forw[0]}:${item.forw[1]}`] = item;
|
|
553
|
-
});
|
|
554
|
-
} catch {
|
|
555
|
-
throw "TOO LINEAR2";
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
// Calculate backward convex hull transformation
|
|
559
|
-
const bakwConvex = convex(pointsSetFC.bakw);
|
|
560
|
-
if (!bakwConvex) throw "TOO LINEAR2";
|
|
561
|
-
|
|
562
|
-
const bakwCoords = bakwConvex.geometry!.coordinates[0];
|
|
563
|
-
try {
|
|
564
|
-
convexCalc = bakwCoords.map((coord: Position) => ({
|
|
565
|
-
bakw: coord,
|
|
566
|
-
forw: transformArr(point(coord), tinBakw as any) as Position,
|
|
567
|
-
}));
|
|
568
|
-
convexCalc.forEach((item: any) => {
|
|
569
|
-
convexBuf[`${item.forw[0]}:${item.forw[1]}`] = item;
|
|
570
|
-
});
|
|
571
|
-
} catch {
|
|
572
|
-
throw "TOO LINEAR2";
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
// Set centroids
|
|
576
|
-
const centCalc = {
|
|
577
|
-
forw: forCentroid.geometry!.coordinates,
|
|
578
|
-
bakw: transformArr(forCentroid, tinForw as any) as Position,
|
|
579
|
-
};
|
|
580
|
-
|
|
581
|
-
const centroidPoint = createPoint(centCalc.forw, centCalc.bakw, "c");
|
|
582
|
-
this.centroid = {
|
|
583
|
-
forw: centroidPoint,
|
|
584
|
-
bakw: counterPoint(centroidPoint),
|
|
585
|
-
};
|
|
586
|
-
|
|
587
|
-
// Calculate vertices
|
|
588
|
-
const verticesSet = this.vertexMode === Tin.VERTEX_BIRDEYE
|
|
589
|
-
? this.calculateBirdeyeVertices(
|
|
590
|
-
convexBuf,
|
|
591
|
-
centCalc,
|
|
592
|
-
bbox,
|
|
593
|
-
minx,
|
|
594
|
-
maxx,
|
|
595
|
-
miny,
|
|
596
|
-
maxy,
|
|
597
|
-
)
|
|
598
|
-
: this.calculatePlainVertices(
|
|
599
|
-
convexBuf,
|
|
600
|
-
centCalc,
|
|
601
|
-
bbox,
|
|
602
|
-
minx,
|
|
603
|
-
maxx,
|
|
604
|
-
miny,
|
|
605
|
-
maxy,
|
|
606
|
-
);
|
|
607
|
-
|
|
608
|
-
// Add vertices to points set
|
|
609
|
-
const verticesList = {
|
|
610
|
-
forw: [] as Feature<Point>[],
|
|
611
|
-
bakw: [] as Feature<Point>[],
|
|
612
|
-
};
|
|
613
|
-
for (let i = 0; i < verticesSet.length; i++) {
|
|
614
|
-
const forw = verticesSet[i].forw;
|
|
615
|
-
const bakw = verticesSet[i].bakw;
|
|
616
|
-
const forPoint = createPoint(forw, bakw, `b${i}`);
|
|
617
|
-
const bakPoint = counterPoint(forPoint);
|
|
618
|
-
rawPointsSet.forw.push(forPoint);
|
|
619
|
-
rawPointsSet.bakw.push(bakPoint);
|
|
620
|
-
verticesList.forw.push(forPoint);
|
|
621
|
-
verticesList.bakw.push(bakPoint);
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
this.pointsSet = {
|
|
625
|
-
forw: featureCollection(rawPointsSet.forw),
|
|
626
|
-
bakw: featureCollection(rawPointsSet.bakw),
|
|
627
|
-
edges: [],
|
|
628
|
-
};
|
|
629
|
-
|
|
630
|
-
// Generate forward TIN
|
|
631
|
-
this.tins = {
|
|
632
|
-
forw: rotateVerticesTriangle(
|
|
633
|
-
constrainedTin(
|
|
634
|
-
this.pointsSet!.forw,
|
|
635
|
-
rawPointsSet.edges,
|
|
636
|
-
"target",
|
|
637
|
-
) as Tins,
|
|
638
|
-
),
|
|
639
|
-
};
|
|
640
|
-
|
|
641
|
-
// Calculate strict TIN if needed
|
|
642
|
-
if (strict === Tin.MODE_STRICT || strict === Tin.MODE_AUTO) {
|
|
643
|
-
this.calcurateStrictTin();
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
// Generate backward TIN if needed
|
|
647
|
-
if (
|
|
648
|
-
strict === Tin.MODE_LOOSE ||
|
|
649
|
-
(strict === Tin.MODE_AUTO && this.strict_status === Tin.STATUS_ERROR)
|
|
650
|
-
) {
|
|
651
|
-
this.tins!.bakw = rotateVerticesTriangle(
|
|
652
|
-
constrainedTin(
|
|
653
|
-
this.pointsSet!.bakw,
|
|
654
|
-
rawPointsSet.edges,
|
|
655
|
-
"target",
|
|
656
|
-
) as Tins,
|
|
657
|
-
);
|
|
658
|
-
delete this.kinks;
|
|
659
|
-
this.strict_status = Tin.STATUS_LOOSE;
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
// Calculate vertices parameters
|
|
663
|
-
this.vertices_params = {
|
|
664
|
-
forw: vertexCalc(verticesList.forw, this.centroid.forw!),
|
|
665
|
-
bakw: vertexCalc(verticesList.bakw, this.centroid.bakw!),
|
|
666
|
-
};
|
|
667
|
-
|
|
668
|
-
this.addIndexedTin();
|
|
669
|
-
this.calculatePointsWeight();
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
/**
|
|
673
|
-
* 通常の頂点を計算
|
|
674
|
-
*/
|
|
675
|
-
private calculatePlainVertices(
|
|
676
|
-
convexBuf: Record<string, { forw: Position; bakw: Position }>,
|
|
677
|
-
centCalc: { forw: Position; bakw: Position },
|
|
678
|
-
bbox: Position[],
|
|
679
|
-
minx: number,
|
|
680
|
-
maxx: number,
|
|
681
|
-
miny: number,
|
|
682
|
-
maxy: number,
|
|
683
|
-
): Array<{ forw: Position; bakw: Position }> {
|
|
684
|
-
// Calculate edge vertices
|
|
685
|
-
const edgeNodes = Object.keys(convexBuf).reduce((prev, key) => {
|
|
686
|
-
const item = convexBuf[key];
|
|
687
|
-
const forw = item.forw;
|
|
688
|
-
const bakw = item.bakw;
|
|
689
|
-
|
|
690
|
-
const vec = {
|
|
691
|
-
forw: [forw[0] - centCalc.forw[0], forw[1] - centCalc.forw[1]],
|
|
692
|
-
bakw: [bakw[0] - centCalc.bakw[0], bakw[1] - centCalc.bakw[1]],
|
|
693
|
-
};
|
|
694
|
-
|
|
695
|
-
const xRate = vec.forw[0] === 0
|
|
696
|
-
? Infinity
|
|
697
|
-
: ((vec.forw[0] < 0 ? minx : maxx) - centCalc.forw[0]) / vec.forw[0];
|
|
698
|
-
const yRate = vec.forw[1] === 0
|
|
699
|
-
? Infinity
|
|
700
|
-
: ((vec.forw[1] < 0 ? miny : maxy) - centCalc.forw[1]) / vec.forw[1];
|
|
701
|
-
|
|
702
|
-
if (Math.abs(xRate) / Math.abs(yRate) < 1.1) {
|
|
703
|
-
const node = {
|
|
704
|
-
forw: [
|
|
705
|
-
vec.forw[0] * xRate + centCalc.forw[0],
|
|
706
|
-
vec.forw[1] * xRate + centCalc.forw[1],
|
|
707
|
-
] as Position,
|
|
708
|
-
bakw: [
|
|
709
|
-
vec.bakw[0] * xRate + centCalc.bakw[0],
|
|
710
|
-
vec.bakw[1] * xRate + centCalc.bakw[1],
|
|
711
|
-
] as Position,
|
|
712
|
-
};
|
|
713
|
-
if (vec.forw[0] < 0) {
|
|
714
|
-
prev[3].push(node);
|
|
715
|
-
} else {
|
|
716
|
-
prev[1].push(node);
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
if (Math.abs(yRate) / Math.abs(xRate) < 1.1) {
|
|
721
|
-
const node = {
|
|
722
|
-
forw: [
|
|
723
|
-
vec.forw[0] * yRate + centCalc.forw[0],
|
|
724
|
-
vec.forw[1] * yRate + centCalc.forw[1],
|
|
725
|
-
] as Position,
|
|
726
|
-
bakw: [
|
|
727
|
-
vec.bakw[0] * yRate + centCalc.bakw[0],
|
|
728
|
-
vec.bakw[1] * yRate + centCalc.bakw[1],
|
|
729
|
-
] as Position,
|
|
730
|
-
};
|
|
731
|
-
if (vec.forw[1] < 0) {
|
|
732
|
-
prev[0].push(node);
|
|
733
|
-
} else {
|
|
734
|
-
prev[2].push(node);
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
return prev;
|
|
739
|
-
}, [[], [], [], []] as Array<Array<{ forw: Position; bakw: Position }>>);
|
|
740
|
-
|
|
741
|
-
// Calculate vertex angles
|
|
742
|
-
const vertexCalc = Object.keys(convexBuf).reduce(
|
|
743
|
-
(prev, key, index, arr) => {
|
|
744
|
-
const item = convexBuf[key];
|
|
745
|
-
const forw = item.forw;
|
|
746
|
-
const bakw = item.bakw;
|
|
747
|
-
|
|
748
|
-
const vec = {
|
|
749
|
-
forw: [forw[0] - centCalc.forw[0], forw[1] - centCalc.forw[1]],
|
|
750
|
-
bakw: [bakw[0] - centCalc.bakw[0], centCalc.bakw[1] - bakw[1]],
|
|
751
|
-
};
|
|
752
|
-
|
|
753
|
-
if (vec.forw[0] === 0 || vec.forw[1] === 0) return prev;
|
|
754
|
-
|
|
755
|
-
let quad = 0;
|
|
756
|
-
if (vec.forw[0] > 0) quad += 1;
|
|
757
|
-
if (vec.forw[1] > 0) quad += 2;
|
|
758
|
-
|
|
759
|
-
prev[quad].push([vec.forw, vec.bakw]);
|
|
760
|
-
|
|
761
|
-
if (index === arr.length - 1) {
|
|
762
|
-
return prev.map((quadArr) => {
|
|
763
|
-
return quadArr.reduce(
|
|
764
|
-
(
|
|
765
|
-
qprev: number[] | undefined,
|
|
766
|
-
curr: number[][],
|
|
767
|
-
qindex: number,
|
|
768
|
-
qarr: number[][][],
|
|
769
|
-
) => {
|
|
770
|
-
if (!qprev) qprev = [Infinity, 0, 0];
|
|
771
|
-
|
|
772
|
-
let ratio =
|
|
773
|
-
Math.sqrt(Math.pow(curr[0][0], 2) + Math.pow(curr[0][1], 2)) /
|
|
774
|
-
Math.sqrt(Math.pow(curr[1][0], 2) + Math.pow(curr[1][1], 2));
|
|
775
|
-
ratio = ratio < qprev[0] ? ratio : qprev[0];
|
|
776
|
-
|
|
777
|
-
const theta = Math.atan2(curr[0][0], curr[0][1]) -
|
|
778
|
-
Math.atan2(curr[1][0], curr[1][1]);
|
|
779
|
-
const sumcos = qprev[1] + Math.cos(theta);
|
|
780
|
-
const sumsin = qprev[2] + Math.sin(theta);
|
|
781
|
-
|
|
782
|
-
if (qindex === qarr.length - 1) {
|
|
783
|
-
return [ratio, Math.atan2(sumsin, sumcos)];
|
|
784
|
-
}
|
|
785
|
-
return [ratio, sumcos, sumsin];
|
|
786
|
-
},
|
|
787
|
-
null as any,
|
|
788
|
-
);
|
|
789
|
-
});
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
return prev;
|
|
793
|
-
},
|
|
794
|
-
[[], [], [], []] as any[],
|
|
795
|
-
);
|
|
796
|
-
|
|
797
|
-
let vertexRatio: any[] = vertexCalc;
|
|
798
|
-
if (vertexRatio.length === 1) {
|
|
799
|
-
vertexRatio = [
|
|
800
|
-
vertexRatio[0],
|
|
801
|
-
vertexRatio[0],
|
|
802
|
-
vertexRatio[0],
|
|
803
|
-
vertexRatio[0],
|
|
804
|
-
];
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
// Calculate vertices
|
|
808
|
-
const vertices = vertexRatio.map((ratio: any, index: number) => {
|
|
809
|
-
const forVertex = bbox[index];
|
|
810
|
-
const forDelta = [
|
|
811
|
-
forVertex[0] - centCalc.forw[0],
|
|
812
|
-
forVertex[1] - centCalc.forw[1],
|
|
813
|
-
];
|
|
814
|
-
const forDistance = Math.sqrt(
|
|
815
|
-
Math.pow(forDelta[0], 2) + Math.pow(forDelta[1], 2),
|
|
816
|
-
);
|
|
817
|
-
const bakDistance = forDistance / ratio[0];
|
|
818
|
-
const bakTheta = Math.atan2(forDelta[0], forDelta[1]) - ratio[1];
|
|
819
|
-
const bakw: Position = [
|
|
820
|
-
centCalc.bakw[0] + bakDistance * Math.sin(bakTheta),
|
|
821
|
-
centCalc.bakw[1] - bakDistance * Math.cos(bakTheta),
|
|
822
|
-
];
|
|
823
|
-
|
|
824
|
-
return { forw: forVertex, bakw };
|
|
825
|
-
});
|
|
826
|
-
|
|
827
|
-
// Swap vertices 2 and 3 for correct ordering
|
|
828
|
-
const swap = vertices[2];
|
|
829
|
-
vertices[2] = vertices[3];
|
|
830
|
-
vertices[3] = swap;
|
|
831
|
-
|
|
832
|
-
// Check edge intersections
|
|
833
|
-
this.checkAndAdjustVertices(vertices, edgeNodes, centCalc);
|
|
834
|
-
|
|
835
|
-
return vertices;
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
/**
|
|
839
|
-
* 鳥瞰図モードの頂点を計算
|
|
840
|
-
*/
|
|
841
|
-
private calculateBirdeyeVertices(
|
|
842
|
-
convexBuf: Record<string, { forw: Position; bakw: Position }>,
|
|
843
|
-
centCalc: { forw: Position; bakw: Position },
|
|
844
|
-
bbox: Position[],
|
|
845
|
-
minx: number,
|
|
846
|
-
maxx: number,
|
|
847
|
-
miny: number,
|
|
848
|
-
maxy: number,
|
|
849
|
-
): Array<{ forw: Position; bakw: Position }> {
|
|
850
|
-
// For birdeye mode, implement special vertex calculation
|
|
851
|
-
// This is a simplified version, actual implementation might be more complex
|
|
852
|
-
return this.calculatePlainVertices(
|
|
853
|
-
convexBuf,
|
|
854
|
-
centCalc,
|
|
855
|
-
bbox,
|
|
856
|
-
minx,
|
|
857
|
-
maxx,
|
|
858
|
-
miny,
|
|
859
|
-
maxy,
|
|
860
|
-
);
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
/**
|
|
864
|
-
* 頂点の位置を調整
|
|
865
|
-
*/
|
|
866
|
-
private checkAndAdjustVertices(
|
|
867
|
-
vertices: Array<{ forw: Position; bakw: Position }>,
|
|
868
|
-
edgeNodes: Array<Array<{ forw: Position; bakw: Position }>>,
|
|
869
|
-
centCalc: { forw: Position; bakw: Position },
|
|
870
|
-
): void {
|
|
871
|
-
const expandRatio = [1, 1, 1, 1];
|
|
872
|
-
|
|
873
|
-
for (let i = 0; i < 4; i++) {
|
|
874
|
-
const j = (i + 1) % 4;
|
|
875
|
-
const side = lineString([vertices[i].bakw, vertices[j].bakw]);
|
|
876
|
-
|
|
877
|
-
edgeNodes[i].map((node) => {
|
|
878
|
-
const line = lineString([centCalc.bakw, node.bakw]);
|
|
879
|
-
const intersect = lineIntersect(side, line);
|
|
880
|
-
|
|
881
|
-
if (intersect.features.length > 0 && intersect.features[0].geometry) {
|
|
882
|
-
const intersectPt = intersect.features[0];
|
|
883
|
-
const distance = Math.sqrt(
|
|
884
|
-
Math.pow(node.bakw[0] - centCalc.bakw[0], 2) +
|
|
885
|
-
Math.pow(node.bakw[1] - centCalc.bakw[1], 2),
|
|
886
|
-
);
|
|
887
|
-
const intDistance = Math.sqrt(
|
|
888
|
-
Math.pow(
|
|
889
|
-
intersectPt.geometry.coordinates[0] - centCalc.bakw[0],
|
|
890
|
-
2,
|
|
891
|
-
) +
|
|
892
|
-
Math.pow(
|
|
893
|
-
intersectPt.geometry.coordinates[1] - centCalc.bakw[1],
|
|
894
|
-
2,
|
|
895
|
-
),
|
|
896
|
-
);
|
|
897
|
-
const ratio = distance / intDistance;
|
|
898
|
-
|
|
899
|
-
if (ratio > expandRatio[i]) expandRatio[i] = ratio;
|
|
900
|
-
if (ratio > expandRatio[j]) expandRatio[j] = ratio;
|
|
901
|
-
}
|
|
902
|
-
});
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
vertices.forEach((vertex, i) => {
|
|
906
|
-
const ratio = expandRatio[i];
|
|
907
|
-
const bakw: Position = [
|
|
908
|
-
(vertex.bakw[0] - centCalc.bakw[0]) * ratio + centCalc.bakw[0],
|
|
909
|
-
(vertex.bakw[1] - centCalc.bakw[1]) * ratio + centCalc.bakw[1],
|
|
910
|
-
];
|
|
911
|
-
vertex.bakw = bakw;
|
|
912
|
-
});
|
|
913
|
-
}
|
|
914
|
-
|
|
915
|
-
/**
|
|
916
|
-
* 点の重み付けを計算します
|
|
917
|
-
*/
|
|
918
|
-
calculatePointsWeight(): void {
|
|
919
|
-
const calcTargets = ["forw"];
|
|
920
|
-
if (this.strict_status === Tin.STATUS_LOOSE) {
|
|
921
|
-
calcTargets.push("bakw");
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
const weightBuffer: WeightBufferBD = {};
|
|
925
|
-
|
|
926
|
-
calcTargets.forEach((target) => {
|
|
927
|
-
weightBuffer[target as keyof WeightBufferBD] = {};
|
|
928
|
-
const tin_points: Record<string, number> = {};
|
|
929
|
-
|
|
930
|
-
this.tins![target as keyof TinsBD]!.features.map((tri: Tri) => {
|
|
931
|
-
const props = ["a", "b", "c"] as PropertyTriKey[];
|
|
932
|
-
for (let i = 0; i < 3; i++) {
|
|
933
|
-
const j = (i + 1) % 3;
|
|
934
|
-
const prop_i = props[i];
|
|
935
|
-
const prop_j = props[j];
|
|
936
|
-
const index_i = tri.properties![prop_i].index;
|
|
937
|
-
const index_j = tri.properties![prop_j].index;
|
|
938
|
-
const key = [index_i, index_j].sort().join("-");
|
|
939
|
-
|
|
940
|
-
if (!tin_points[key]) {
|
|
941
|
-
const i_xy = tri.geometry!.coordinates[0][i];
|
|
942
|
-
const j_xy = tri.geometry!.coordinates[0][j];
|
|
943
|
-
const i_merc = tri.properties![prop_i].geom;
|
|
944
|
-
const j_merc = tri.properties![prop_j].geom;
|
|
945
|
-
|
|
946
|
-
tin_points[key] = 1;
|
|
947
|
-
const ratio = Math.sqrt(
|
|
948
|
-
Math.pow(i_merc[0] - j_merc[0], 2) +
|
|
949
|
-
Math.pow(i_merc[1] - j_merc[1], 2),
|
|
950
|
-
) / Math.sqrt(
|
|
951
|
-
Math.pow(i_xy[0] - j_xy[0], 2) + Math.pow(i_xy[1] - j_xy[1], 2),
|
|
952
|
-
);
|
|
953
|
-
|
|
954
|
-
if (!weightBuffer[target as keyof WeightBufferBD]) {
|
|
955
|
-
weightBuffer[target as keyof WeightBufferBD] = {};
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
const targetBuffer = weightBuffer[target as keyof WeightBufferBD]!;
|
|
959
|
-
targetBuffer[`${index_i}:${key}`] = ratio;
|
|
960
|
-
targetBuffer[`${index_j}:${key}`] = ratio;
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
});
|
|
964
|
-
});
|
|
965
|
-
|
|
966
|
-
// Calculate average weights
|
|
967
|
-
const pointsWeightBuffer: WeightBufferBD = {};
|
|
968
|
-
|
|
969
|
-
calcTargets.map((target) => {
|
|
970
|
-
pointsWeightBuffer[target as keyof WeightBufferBD] = {};
|
|
971
|
-
|
|
972
|
-
if (this.strict_status === Tin.STATUS_STRICT) {
|
|
973
|
-
pointsWeightBuffer.bakw = {};
|
|
974
|
-
}
|
|
975
|
-
|
|
976
|
-
const targetBuffer = weightBuffer[target as keyof WeightBufferBD]!;
|
|
977
|
-
const pointWeights: { [pointId: string]: number[] } = {};
|
|
978
|
-
|
|
979
|
-
// Group weights by point ID
|
|
980
|
-
Object.keys(targetBuffer).forEach((key) => {
|
|
981
|
-
const [pointId] = key.split(":");
|
|
982
|
-
if (!pointWeights[pointId]) {
|
|
983
|
-
pointWeights[pointId] = [];
|
|
984
|
-
}
|
|
985
|
-
pointWeights[pointId].push(targetBuffer[key]);
|
|
986
|
-
});
|
|
987
|
-
|
|
988
|
-
// Calculate average weight for each point
|
|
989
|
-
Object.keys(pointWeights).forEach((pointId) => {
|
|
990
|
-
const weights = pointWeights[pointId];
|
|
991
|
-
const avgWeight = weights.reduce((sum, w) => sum + w, 0) /
|
|
992
|
-
weights.length;
|
|
993
|
-
|
|
994
|
-
if (!pointsWeightBuffer[target as keyof WeightBufferBD]) {
|
|
995
|
-
pointsWeightBuffer[target as keyof WeightBufferBD] = {};
|
|
996
|
-
}
|
|
997
|
-
pointsWeightBuffer[target as keyof WeightBufferBD]![pointId] =
|
|
998
|
-
avgWeight;
|
|
999
|
-
|
|
1000
|
-
if (this.strict_status === Tin.STATUS_STRICT) {
|
|
1001
|
-
if (!pointsWeightBuffer.bakw) {
|
|
1002
|
-
pointsWeightBuffer.bakw = {};
|
|
1003
|
-
}
|
|
1004
|
-
pointsWeightBuffer.bakw[pointId] = 1 / avgWeight;
|
|
1005
|
-
}
|
|
1006
|
-
});
|
|
1007
|
-
|
|
1008
|
-
// Calculate centroid weight
|
|
1009
|
-
let centroidSum = 0;
|
|
1010
|
-
for (let i = 0; i < 4; i++) {
|
|
1011
|
-
const key = `b${i}`;
|
|
1012
|
-
const weight =
|
|
1013
|
-
pointsWeightBuffer[target as keyof WeightBufferBD]![key] || 0;
|
|
1014
|
-
centroidSum += weight;
|
|
1015
|
-
}
|
|
1016
|
-
pointsWeightBuffer[target as keyof WeightBufferBD]!["c"] = centroidSum /
|
|
1017
|
-
4;
|
|
1018
|
-
|
|
1019
|
-
if (this.strict_status === Tin.STATUS_STRICT && pointsWeightBuffer.bakw) {
|
|
1020
|
-
pointsWeightBuffer.bakw["c"] = 1 /
|
|
1021
|
-
pointsWeightBuffer[target as keyof WeightBufferBD]!["c"];
|
|
1022
|
-
}
|
|
1023
|
-
});
|
|
1024
|
-
|
|
1025
|
-
this.pointsWeightBuffer = pointsWeightBuffer;
|
|
1026
|
-
}
|
|
1027
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Tin (Triangulated Irregular Network) クラス
|
|
3
|
+
* 2つの平面座標系間の同相変換を実現します。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import booleanPointInPolygon from "@turf/boolean-point-in-polygon";
|
|
7
|
+
import turfCentroid from "@turf/centroid";
|
|
8
|
+
import convex from "@turf/convex";
|
|
9
|
+
import { featureCollection, lineString, point, polygon } from "@turf/helpers";
|
|
10
|
+
import lineIntersect from "@turf/line-intersect";
|
|
11
|
+
import type { Feature, Point, Position } from "geojson";
|
|
12
|
+
import {
|
|
13
|
+
counterTri,
|
|
14
|
+
format_version,
|
|
15
|
+
normalizeEdges,
|
|
16
|
+
rotateVerticesTriangle,
|
|
17
|
+
Transform,
|
|
18
|
+
transformArr,
|
|
19
|
+
} from "@maplat/transform";
|
|
20
|
+
|
|
21
|
+
// Ensure format_version is available
|
|
22
|
+
const FALLBACK_FORMAT_VERSION = 2.00703;
|
|
23
|
+
const safeFormatVersion = typeof format_version !== "undefined"
|
|
24
|
+
? format_version
|
|
25
|
+
: FALLBACK_FORMAT_VERSION;
|
|
26
|
+
import type {
|
|
27
|
+
Compiled,
|
|
28
|
+
Edge,
|
|
29
|
+
EdgeSet,
|
|
30
|
+
EdgeSetLegacy,
|
|
31
|
+
PointSet,
|
|
32
|
+
PropertyTriKey,
|
|
33
|
+
StrictMode,
|
|
34
|
+
Tins,
|
|
35
|
+
TinsBD,
|
|
36
|
+
Tri,
|
|
37
|
+
VertexMode,
|
|
38
|
+
WeightBufferBD,
|
|
39
|
+
YaxisMode,
|
|
40
|
+
} from "@maplat/transform";
|
|
41
|
+
import constrainedTin from "./constrained-tin.ts";
|
|
42
|
+
import findIntersections from "./kinks.ts";
|
|
43
|
+
import { insertSearchIndex } from "./searchutils.ts";
|
|
44
|
+
import { counterPoint, createPoint, vertexCalc } from "./vertexutils.ts";
|
|
45
|
+
import type { SearchIndex } from "./searchutils.ts";
|
|
46
|
+
import type { PointsSetBD } from "./types/tin.d.ts";
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Tinクラスの初期化オプション
|
|
50
|
+
*/
|
|
51
|
+
export interface Options {
|
|
52
|
+
bounds?: Position[];
|
|
53
|
+
wh?: number[];
|
|
54
|
+
vertexMode?: VertexMode;
|
|
55
|
+
strictMode?: StrictMode;
|
|
56
|
+
yaxisMode?: YaxisMode;
|
|
57
|
+
importance?: number;
|
|
58
|
+
priority?: number;
|
|
59
|
+
stateFull?: boolean;
|
|
60
|
+
points?: PointSet[];
|
|
61
|
+
edges?: EdgeSet[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Tin (Triangulated Irregular Network) クラス
|
|
66
|
+
* Transformクラスを拡張し、TINネットワークの生成機能を追加
|
|
67
|
+
*/
|
|
68
|
+
export class Tin extends Transform {
|
|
69
|
+
importance: number;
|
|
70
|
+
priority: number;
|
|
71
|
+
pointsSet: PointsSetBD | undefined;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Tinクラスのインスタンスを生成します
|
|
75
|
+
* @param options - 初期化オプション
|
|
76
|
+
*/
|
|
77
|
+
constructor(options: Options = {}) {
|
|
78
|
+
super();
|
|
79
|
+
|
|
80
|
+
if (options.bounds) {
|
|
81
|
+
this.setBounds(options.bounds);
|
|
82
|
+
} else {
|
|
83
|
+
this.setWh(options.wh);
|
|
84
|
+
this.vertexMode = options.vertexMode || Tin.VERTEX_PLAIN;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this.strictMode = options.strictMode || Tin.MODE_AUTO;
|
|
88
|
+
this.yaxisMode = options.yaxisMode || Tin.YAXIS_INVERT;
|
|
89
|
+
this.importance = options.importance || 0;
|
|
90
|
+
this.priority = options.priority || 0;
|
|
91
|
+
this.stateFull = options.stateFull || false;
|
|
92
|
+
|
|
93
|
+
if (options.points) {
|
|
94
|
+
this.setPoints(options.points);
|
|
95
|
+
}
|
|
96
|
+
if (options.edges) {
|
|
97
|
+
this.setEdges(options.edges);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* フォーマットバージョンを取得します
|
|
103
|
+
*/
|
|
104
|
+
getFormatVersion(): number {
|
|
105
|
+
return safeFormatVersion;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 制御点(GCP: Ground Control Points)を設定します
|
|
110
|
+
*/
|
|
111
|
+
setPoints(points: PointSet[]): void {
|
|
112
|
+
if (this.yaxisMode === Tin.YAXIS_FOLLOW) {
|
|
113
|
+
points = points.map((point) => [
|
|
114
|
+
point[0],
|
|
115
|
+
[point[1][0], -1 * point[1][1]],
|
|
116
|
+
]);
|
|
117
|
+
}
|
|
118
|
+
this.points = points;
|
|
119
|
+
this.tins = undefined;
|
|
120
|
+
this.indexedTins = undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* エッジ(制約線)を設定します
|
|
125
|
+
*/
|
|
126
|
+
setEdges(edges: EdgeSet[] | EdgeSetLegacy[] = []): void {
|
|
127
|
+
this.edges = normalizeEdges(edges);
|
|
128
|
+
this.edgeNodes = undefined;
|
|
129
|
+
this.tins = undefined;
|
|
130
|
+
this.indexedTins = undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 境界ポリゴンを設定します
|
|
135
|
+
*/
|
|
136
|
+
setBounds(bounds: Position[]): void {
|
|
137
|
+
this.bounds = bounds;
|
|
138
|
+
|
|
139
|
+
let minx = bounds[0][0];
|
|
140
|
+
let maxx = minx;
|
|
141
|
+
let miny = bounds[0][1];
|
|
142
|
+
let maxy = miny;
|
|
143
|
+
const coords = [bounds[0]];
|
|
144
|
+
|
|
145
|
+
for (let i = 1; i < bounds.length; i++) {
|
|
146
|
+
const bound = bounds[i];
|
|
147
|
+
if (bound[0] < minx) minx = bound[0];
|
|
148
|
+
if (bound[0] > maxx) maxx = bound[0];
|
|
149
|
+
if (bound[1] < miny) miny = bound[1];
|
|
150
|
+
if (bound[1] > maxy) maxy = bound[1];
|
|
151
|
+
coords.push(bound);
|
|
152
|
+
}
|
|
153
|
+
coords.push(bounds[0]);
|
|
154
|
+
|
|
155
|
+
this.boundsPolygon = polygon([coords]);
|
|
156
|
+
this.xy = [minx, miny];
|
|
157
|
+
this.wh = [maxx - minx, maxy - miny];
|
|
158
|
+
this.vertexMode = Tin.VERTEX_PLAIN;
|
|
159
|
+
this.tins = undefined;
|
|
160
|
+
this.indexedTins = undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 現在の設定を永続化可能な形式にコンパイルします
|
|
165
|
+
*/
|
|
166
|
+
getCompiled(): Compiled {
|
|
167
|
+
const compiled: any = {};
|
|
168
|
+
compiled.version = safeFormatVersion;
|
|
169
|
+
compiled.points = this.points;
|
|
170
|
+
compiled.weight_buffer = this.pointsWeightBuffer;
|
|
171
|
+
compiled.centroid_point = [
|
|
172
|
+
this.centroid!.forw!.geometry!.coordinates,
|
|
173
|
+
this.centroid!.forw!.properties!.target.geom,
|
|
174
|
+
];
|
|
175
|
+
compiled.vertices_params = [
|
|
176
|
+
this.vertices_params!.forw![0],
|
|
177
|
+
this.vertices_params!.bakw![0],
|
|
178
|
+
];
|
|
179
|
+
compiled.vertices_points = [];
|
|
180
|
+
|
|
181
|
+
const vertices = this.vertices_params!.forw![1];
|
|
182
|
+
if (vertices) {
|
|
183
|
+
[0, 1, 2, 3].map((i) => {
|
|
184
|
+
const vertex = vertices[i].features[0];
|
|
185
|
+
const forw = vertex.geometry!.coordinates[0][1];
|
|
186
|
+
const bakw = vertex.properties!.b.geom;
|
|
187
|
+
compiled.vertices_points[i] = [forw, bakw];
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
compiled.strict_status = this.strict_status;
|
|
192
|
+
compiled.tins_points = [[]];
|
|
193
|
+
|
|
194
|
+
this.tins!.forw!.features.map((tin: Tri) => {
|
|
195
|
+
compiled.tins_points[0].push(
|
|
196
|
+
(["a", "b", "c"] as PropertyTriKey[]).map((key) =>
|
|
197
|
+
tin.properties![key].index
|
|
198
|
+
),
|
|
199
|
+
);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
if (this.strict_status === Tin.STATUS_LOOSE) {
|
|
203
|
+
compiled.tins_points[1] = [];
|
|
204
|
+
this.tins!.bakw!.features.map((tin: Tri) => {
|
|
205
|
+
compiled.tins_points[1].push(
|
|
206
|
+
(["a", "b", "c"] as PropertyTriKey[]).map((key) =>
|
|
207
|
+
tin.properties![key].index
|
|
208
|
+
),
|
|
209
|
+
);
|
|
210
|
+
});
|
|
211
|
+
} else if (this.strict_status === Tin.STATUS_ERROR && this.kinks?.bakw) {
|
|
212
|
+
compiled.kinks_points = this.kinks.bakw.features.map(
|
|
213
|
+
(kink) => kink.geometry!.coordinates,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
compiled.yaxisMode = this.yaxisMode;
|
|
218
|
+
compiled.vertexMode = this.vertexMode;
|
|
219
|
+
compiled.strictMode = this.strictMode;
|
|
220
|
+
|
|
221
|
+
if (this.bounds) {
|
|
222
|
+
compiled.bounds = this.bounds;
|
|
223
|
+
compiled.boundsPolygon = this.boundsPolygon;
|
|
224
|
+
compiled.xy = this.xy;
|
|
225
|
+
compiled.wh = this.wh;
|
|
226
|
+
} else {
|
|
227
|
+
compiled.wh = this.wh;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
compiled.edges = this.edges;
|
|
231
|
+
compiled.edgeNodes = this.edgeNodes;
|
|
232
|
+
|
|
233
|
+
return compiled;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 幅と高さを設定します
|
|
238
|
+
*/
|
|
239
|
+
setWh(wh?: number[]): void {
|
|
240
|
+
this.wh = wh || [100, 100]; // デフォルト値を設定
|
|
241
|
+
this.xy = [0, 0];
|
|
242
|
+
this.bounds = undefined;
|
|
243
|
+
this.boundsPolygon = undefined;
|
|
244
|
+
this.tins = undefined;
|
|
245
|
+
this.indexedTins = undefined;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* 頂点モードを設定します
|
|
250
|
+
*/
|
|
251
|
+
setVertexMode(mode: VertexMode): void {
|
|
252
|
+
this.vertexMode = mode;
|
|
253
|
+
this.tins = undefined;
|
|
254
|
+
this.indexedTins = undefined;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* 厳密性モードを設定します
|
|
259
|
+
*/
|
|
260
|
+
setStrictMode(mode: StrictMode): void {
|
|
261
|
+
this.strictMode = mode;
|
|
262
|
+
this.tins = undefined;
|
|
263
|
+
this.indexedTins = undefined;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* 厳密なTINを計算します
|
|
268
|
+
*/
|
|
269
|
+
calcurateStrictTin(): void {
|
|
270
|
+
const bakTins = this.tins!.forw!.features.map((tri: Tri) =>
|
|
271
|
+
counterTri(tri)
|
|
272
|
+
);
|
|
273
|
+
this.tins!.bakw = featureCollection(bakTins);
|
|
274
|
+
|
|
275
|
+
const searchIndex: SearchIndex = {};
|
|
276
|
+
this.tins!.forw!.features.forEach((forTri: Tri, index: number) => {
|
|
277
|
+
const bakTri = this.tins!.bakw!.features[index];
|
|
278
|
+
insertSearchIndex(searchIndex, { forw: forTri, bakw: bakTri });
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
const kinks = ["forw", "bakw"].map((direction) => {
|
|
282
|
+
const tins = this.tins![direction as keyof TinsBD]!.features.map(
|
|
283
|
+
(tin: Tri) => tin.geometry!.coordinates[0],
|
|
284
|
+
);
|
|
285
|
+
return findIntersections(tins);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
if (kinks[0].length === 0 && kinks[1].length === 0) {
|
|
289
|
+
this.strict_status = Tin.STATUS_STRICT;
|
|
290
|
+
delete this.kinks;
|
|
291
|
+
} else {
|
|
292
|
+
this.strict_status = Tin.STATUS_ERROR;
|
|
293
|
+
this.kinks = {};
|
|
294
|
+
if (kinks[0].length > 0) {
|
|
295
|
+
this.kinks.forw = featureCollection(kinks[0]);
|
|
296
|
+
}
|
|
297
|
+
if (kinks[1].length > 0) {
|
|
298
|
+
this.kinks.bakw = featureCollection(kinks[1]);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* 点群セットを生成します
|
|
305
|
+
*/
|
|
306
|
+
generatePointsSet(): {
|
|
307
|
+
forw: Feature<Point>[];
|
|
308
|
+
bakw: Feature<Point>[];
|
|
309
|
+
edges: Edge[];
|
|
310
|
+
} {
|
|
311
|
+
const pointsSet: { forw: Feature<Point>[]; bakw: Feature<Point>[] } = {
|
|
312
|
+
forw: [],
|
|
313
|
+
bakw: [],
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
// Generate points
|
|
317
|
+
for (let i = 0; i < this.points.length; i++) {
|
|
318
|
+
const forw = this.points[i][0];
|
|
319
|
+
const bakw = this.points[i][1];
|
|
320
|
+
const forPoint = createPoint(forw, bakw, i);
|
|
321
|
+
pointsSet.forw.push(forPoint);
|
|
322
|
+
pointsSet.bakw.push(counterPoint(forPoint));
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Generate edge nodes
|
|
326
|
+
const edges: Edge[] = [];
|
|
327
|
+
let edgeNodeIndex = 0;
|
|
328
|
+
this.edgeNodes = [];
|
|
329
|
+
|
|
330
|
+
if (!this.edges) this.edges = [];
|
|
331
|
+
|
|
332
|
+
for (let i = 0; i < this.edges.length; i++) {
|
|
333
|
+
const edge = this.edges[i][2];
|
|
334
|
+
const illstNodes = Object.assign([], this.edges[i][0]);
|
|
335
|
+
const mercNodes = Object.assign([], this.edges[i][1]);
|
|
336
|
+
|
|
337
|
+
if (illstNodes.length === 0 && mercNodes.length === 0) {
|
|
338
|
+
edges.push(edge);
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Add start and end points
|
|
343
|
+
illstNodes.unshift(this.points[edge[0]][0]);
|
|
344
|
+
illstNodes.push(this.points[edge[1]][0]);
|
|
345
|
+
mercNodes.unshift(this.points[edge[0]][1]);
|
|
346
|
+
mercNodes.push(this.points[edge[1]][1]);
|
|
347
|
+
|
|
348
|
+
// Calculate edge segments
|
|
349
|
+
const segments = [illstNodes, mercNodes].map((nodes) => {
|
|
350
|
+
const lengths = nodes.map((node, index, arr) => {
|
|
351
|
+
if (index === 0) return 0;
|
|
352
|
+
const prev = arr[index - 1];
|
|
353
|
+
return Math.sqrt(
|
|
354
|
+
Math.pow(node[0] - prev[0], 2) + Math.pow(node[1] - prev[1], 2),
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
const accumLengths = lengths.reduce((acc, len, idx) => {
|
|
359
|
+
if (idx === 0) return [0];
|
|
360
|
+
acc.push(acc[idx - 1] + len);
|
|
361
|
+
return acc;
|
|
362
|
+
}, [] as number[]);
|
|
363
|
+
|
|
364
|
+
return accumLengths.map((accum, idx, arr) => {
|
|
365
|
+
const ratio = accum / arr[arr.length - 1];
|
|
366
|
+
return [nodes[idx], lengths[idx], accumLengths[idx], ratio];
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
// Generate edge nodes
|
|
371
|
+
segments
|
|
372
|
+
.map((segment, idx) => {
|
|
373
|
+
const otherSegment = segments[idx ? 0 : 1];
|
|
374
|
+
return segment
|
|
375
|
+
.filter((item, index) => {
|
|
376
|
+
return !(
|
|
377
|
+
index === 0 ||
|
|
378
|
+
index === segment.length - 1 ||
|
|
379
|
+
(item as any)[4] === "handled"
|
|
380
|
+
);
|
|
381
|
+
})
|
|
382
|
+
.map((item) => {
|
|
383
|
+
const node = item[0] as unknown as Position;
|
|
384
|
+
const ratio = item[3] as number;
|
|
385
|
+
|
|
386
|
+
const counterpart = otherSegment.reduce(
|
|
387
|
+
(prev, curr, currIdx, arr) => {
|
|
388
|
+
if (prev) return prev;
|
|
389
|
+
const nextItem = arr[currIdx + 1];
|
|
390
|
+
if ((curr[3] as number) === ratio) {
|
|
391
|
+
(curr as any)[4] = "handled";
|
|
392
|
+
return [curr];
|
|
393
|
+
}
|
|
394
|
+
if (
|
|
395
|
+
(curr[3] as number) < ratio && nextItem &&
|
|
396
|
+
(nextItem[3] as number) > ratio
|
|
397
|
+
) {
|
|
398
|
+
return [curr, nextItem];
|
|
399
|
+
}
|
|
400
|
+
return undefined;
|
|
401
|
+
},
|
|
402
|
+
undefined as any,
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
if (counterpart && counterpart.length === 1) {
|
|
406
|
+
return idx === 0
|
|
407
|
+
? [node, counterpart[0][0], ratio]
|
|
408
|
+
: [counterpart[0][0], node, ratio];
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (counterpart && counterpart.length === 2) {
|
|
412
|
+
const curr = counterpart[0];
|
|
413
|
+
const next = counterpart[1];
|
|
414
|
+
const ratioInSegment = (ratio - (curr[3] as number)) /
|
|
415
|
+
((next[3] as number) - (curr[3] as number));
|
|
416
|
+
const interpNode: Position = [
|
|
417
|
+
((next[0] as Position)[0] - (curr[0] as Position)[0]) *
|
|
418
|
+
ratioInSegment + (curr[0] as Position)[0],
|
|
419
|
+
((next[0] as Position)[1] - (curr[0] as Position)[1]) *
|
|
420
|
+
ratioInSegment + (curr[0] as Position)[1],
|
|
421
|
+
];
|
|
422
|
+
return idx === 0
|
|
423
|
+
? [node, interpNode, ratio]
|
|
424
|
+
: [interpNode, node, ratio];
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
return [];
|
|
428
|
+
});
|
|
429
|
+
})
|
|
430
|
+
.reduce((prev, curr) => prev.concat(curr), [])
|
|
431
|
+
.sort((a, b) => (a[2] as number) < (b[2] as number) ? -1 : 1)
|
|
432
|
+
.map((item, index, arr) => {
|
|
433
|
+
this.edgeNodes![edgeNodeIndex] = [
|
|
434
|
+
item[0] as Position,
|
|
435
|
+
item[1] as Position,
|
|
436
|
+
];
|
|
437
|
+
const forPoint = createPoint(
|
|
438
|
+
item[0] as Position,
|
|
439
|
+
item[1] as Position,
|
|
440
|
+
`e${edgeNodeIndex}`,
|
|
441
|
+
);
|
|
442
|
+
edgeNodeIndex++;
|
|
443
|
+
pointsSet.forw!.push(forPoint);
|
|
444
|
+
pointsSet.bakw!.push(counterPoint(forPoint));
|
|
445
|
+
|
|
446
|
+
if (index === 0) {
|
|
447
|
+
edges.push([edge[0], pointsSet.forw!.length - 1]);
|
|
448
|
+
} else {
|
|
449
|
+
edges.push([
|
|
450
|
+
pointsSet.forw!.length - 2,
|
|
451
|
+
pointsSet.forw!.length - 1,
|
|
452
|
+
]);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (index === arr.length - 1) {
|
|
456
|
+
edges.push([pointsSet.forw!.length - 1, edge[1]]);
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return {
|
|
462
|
+
forw: pointsSet.forw,
|
|
463
|
+
bakw: pointsSet.bakw,
|
|
464
|
+
edges,
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* 入力データの検証と初期データの準備
|
|
470
|
+
*/
|
|
471
|
+
private validateAndPrepareInputs() {
|
|
472
|
+
const minx = this.xy![0] - 0.05 * this.wh![0];
|
|
473
|
+
const maxx = this.xy![0] + 1.05 * this.wh![0];
|
|
474
|
+
const miny = this.xy![1] - 0.05 * this.wh![1];
|
|
475
|
+
const maxy = this.xy![1] + 1.05 * this.wh![1];
|
|
476
|
+
|
|
477
|
+
const allPointsInside = this.points.reduce((prev, point) => {
|
|
478
|
+
return prev &&
|
|
479
|
+
(this.bounds
|
|
480
|
+
? booleanPointInPolygon(point[0] as any, this.boundsPolygon!)
|
|
481
|
+
: point[0][0] >= minx && point[0][0] <= maxx &&
|
|
482
|
+
point[0][1] >= miny && point[0][1] <= maxy);
|
|
483
|
+
}, true);
|
|
484
|
+
|
|
485
|
+
if (!allPointsInside) {
|
|
486
|
+
throw "SOME POINTS OUTSIDE";
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
let bbox: Position[] = [];
|
|
490
|
+
if (this.wh) {
|
|
491
|
+
bbox = [[minx, miny], [maxx, miny], [minx, maxy], [maxx, maxy]];
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return {
|
|
495
|
+
pointsSet: this.generatePointsSet(),
|
|
496
|
+
bbox,
|
|
497
|
+
minx,
|
|
498
|
+
maxx,
|
|
499
|
+
miny,
|
|
500
|
+
maxy,
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* TINネットワークを更新し、座標変換の準備を行います
|
|
506
|
+
*/
|
|
507
|
+
updateTin(): void {
|
|
508
|
+
let strict = this.strictMode;
|
|
509
|
+
if (strict !== Tin.MODE_STRICT && strict !== Tin.MODE_LOOSE) {
|
|
510
|
+
strict = Tin.MODE_AUTO;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
const { pointsSet: rawPointsSet, bbox, minx, maxx, miny, maxy } = this
|
|
514
|
+
.validateAndPrepareInputs();
|
|
515
|
+
|
|
516
|
+
// Create FeatureCollections for use in calculations
|
|
517
|
+
const pointsSetFC = {
|
|
518
|
+
forw: featureCollection(rawPointsSet.forw),
|
|
519
|
+
bakw: featureCollection(rawPointsSet.bakw),
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
const tinForw = constrainedTin(
|
|
523
|
+
pointsSetFC.forw,
|
|
524
|
+
rawPointsSet.edges,
|
|
525
|
+
"target",
|
|
526
|
+
);
|
|
527
|
+
const tinBakw = constrainedTin(
|
|
528
|
+
pointsSetFC.bakw,
|
|
529
|
+
rawPointsSet.edges,
|
|
530
|
+
"target",
|
|
531
|
+
);
|
|
532
|
+
|
|
533
|
+
if (tinForw.features.length === 0 || tinBakw.features.length === 0) {
|
|
534
|
+
throw "TOO LINEAR1";
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const forCentroid = turfCentroid(pointsSetFC.forw);
|
|
538
|
+
const forwConvex = convex(pointsSetFC.forw);
|
|
539
|
+
if (!forwConvex) throw "TOO LINEAR2";
|
|
540
|
+
|
|
541
|
+
const convexBuf: Record<string, { forw: Position; bakw: Position }> = {};
|
|
542
|
+
const forwCoords = forwConvex.geometry!.coordinates[0];
|
|
543
|
+
|
|
544
|
+
// Calculate forward convex hull transformation
|
|
545
|
+
let convexCalc;
|
|
546
|
+
try {
|
|
547
|
+
convexCalc = forwCoords.map((coord: Position) => ({
|
|
548
|
+
forw: coord,
|
|
549
|
+
bakw: transformArr(point(coord), tinForw as any) as Position,
|
|
550
|
+
}));
|
|
551
|
+
convexCalc.forEach((item: any) => {
|
|
552
|
+
convexBuf[`${item.forw[0]}:${item.forw[1]}`] = item;
|
|
553
|
+
});
|
|
554
|
+
} catch {
|
|
555
|
+
throw "TOO LINEAR2";
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// Calculate backward convex hull transformation
|
|
559
|
+
const bakwConvex = convex(pointsSetFC.bakw);
|
|
560
|
+
if (!bakwConvex) throw "TOO LINEAR2";
|
|
561
|
+
|
|
562
|
+
const bakwCoords = bakwConvex.geometry!.coordinates[0];
|
|
563
|
+
try {
|
|
564
|
+
convexCalc = bakwCoords.map((coord: Position) => ({
|
|
565
|
+
bakw: coord,
|
|
566
|
+
forw: transformArr(point(coord), tinBakw as any) as Position,
|
|
567
|
+
}));
|
|
568
|
+
convexCalc.forEach((item: any) => {
|
|
569
|
+
convexBuf[`${item.forw[0]}:${item.forw[1]}`] = item;
|
|
570
|
+
});
|
|
571
|
+
} catch {
|
|
572
|
+
throw "TOO LINEAR2";
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// Set centroids
|
|
576
|
+
const centCalc = {
|
|
577
|
+
forw: forCentroid.geometry!.coordinates,
|
|
578
|
+
bakw: transformArr(forCentroid, tinForw as any) as Position,
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
const centroidPoint = createPoint(centCalc.forw, centCalc.bakw, "c");
|
|
582
|
+
this.centroid = {
|
|
583
|
+
forw: centroidPoint,
|
|
584
|
+
bakw: counterPoint(centroidPoint),
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
// Calculate vertices
|
|
588
|
+
const verticesSet = this.vertexMode === Tin.VERTEX_BIRDEYE
|
|
589
|
+
? this.calculateBirdeyeVertices(
|
|
590
|
+
convexBuf,
|
|
591
|
+
centCalc,
|
|
592
|
+
bbox,
|
|
593
|
+
minx,
|
|
594
|
+
maxx,
|
|
595
|
+
miny,
|
|
596
|
+
maxy,
|
|
597
|
+
)
|
|
598
|
+
: this.calculatePlainVertices(
|
|
599
|
+
convexBuf,
|
|
600
|
+
centCalc,
|
|
601
|
+
bbox,
|
|
602
|
+
minx,
|
|
603
|
+
maxx,
|
|
604
|
+
miny,
|
|
605
|
+
maxy,
|
|
606
|
+
);
|
|
607
|
+
|
|
608
|
+
// Add vertices to points set
|
|
609
|
+
const verticesList = {
|
|
610
|
+
forw: [] as Feature<Point>[],
|
|
611
|
+
bakw: [] as Feature<Point>[],
|
|
612
|
+
};
|
|
613
|
+
for (let i = 0; i < verticesSet.length; i++) {
|
|
614
|
+
const forw = verticesSet[i].forw;
|
|
615
|
+
const bakw = verticesSet[i].bakw;
|
|
616
|
+
const forPoint = createPoint(forw, bakw, `b${i}`);
|
|
617
|
+
const bakPoint = counterPoint(forPoint);
|
|
618
|
+
rawPointsSet.forw.push(forPoint);
|
|
619
|
+
rawPointsSet.bakw.push(bakPoint);
|
|
620
|
+
verticesList.forw.push(forPoint);
|
|
621
|
+
verticesList.bakw.push(bakPoint);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
this.pointsSet = {
|
|
625
|
+
forw: featureCollection(rawPointsSet.forw),
|
|
626
|
+
bakw: featureCollection(rawPointsSet.bakw),
|
|
627
|
+
edges: [],
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
// Generate forward TIN
|
|
631
|
+
this.tins = {
|
|
632
|
+
forw: rotateVerticesTriangle(
|
|
633
|
+
constrainedTin(
|
|
634
|
+
this.pointsSet!.forw,
|
|
635
|
+
rawPointsSet.edges,
|
|
636
|
+
"target",
|
|
637
|
+
) as Tins,
|
|
638
|
+
),
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
// Calculate strict TIN if needed
|
|
642
|
+
if (strict === Tin.MODE_STRICT || strict === Tin.MODE_AUTO) {
|
|
643
|
+
this.calcurateStrictTin();
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// Generate backward TIN if needed
|
|
647
|
+
if (
|
|
648
|
+
strict === Tin.MODE_LOOSE ||
|
|
649
|
+
(strict === Tin.MODE_AUTO && this.strict_status === Tin.STATUS_ERROR)
|
|
650
|
+
) {
|
|
651
|
+
this.tins!.bakw = rotateVerticesTriangle(
|
|
652
|
+
constrainedTin(
|
|
653
|
+
this.pointsSet!.bakw,
|
|
654
|
+
rawPointsSet.edges,
|
|
655
|
+
"target",
|
|
656
|
+
) as Tins,
|
|
657
|
+
);
|
|
658
|
+
delete this.kinks;
|
|
659
|
+
this.strict_status = Tin.STATUS_LOOSE;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// Calculate vertices parameters
|
|
663
|
+
this.vertices_params = {
|
|
664
|
+
forw: vertexCalc(verticesList.forw, this.centroid.forw!),
|
|
665
|
+
bakw: vertexCalc(verticesList.bakw, this.centroid.bakw!),
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
this.addIndexedTin();
|
|
669
|
+
this.calculatePointsWeight();
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* 通常の頂点を計算
|
|
674
|
+
*/
|
|
675
|
+
private calculatePlainVertices(
|
|
676
|
+
convexBuf: Record<string, { forw: Position; bakw: Position }>,
|
|
677
|
+
centCalc: { forw: Position; bakw: Position },
|
|
678
|
+
bbox: Position[],
|
|
679
|
+
minx: number,
|
|
680
|
+
maxx: number,
|
|
681
|
+
miny: number,
|
|
682
|
+
maxy: number,
|
|
683
|
+
): Array<{ forw: Position; bakw: Position }> {
|
|
684
|
+
// Calculate edge vertices
|
|
685
|
+
const edgeNodes = Object.keys(convexBuf).reduce((prev, key) => {
|
|
686
|
+
const item = convexBuf[key];
|
|
687
|
+
const forw = item.forw;
|
|
688
|
+
const bakw = item.bakw;
|
|
689
|
+
|
|
690
|
+
const vec = {
|
|
691
|
+
forw: [forw[0] - centCalc.forw[0], forw[1] - centCalc.forw[1]],
|
|
692
|
+
bakw: [bakw[0] - centCalc.bakw[0], bakw[1] - centCalc.bakw[1]],
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
const xRate = vec.forw[0] === 0
|
|
696
|
+
? Infinity
|
|
697
|
+
: ((vec.forw[0] < 0 ? minx : maxx) - centCalc.forw[0]) / vec.forw[0];
|
|
698
|
+
const yRate = vec.forw[1] === 0
|
|
699
|
+
? Infinity
|
|
700
|
+
: ((vec.forw[1] < 0 ? miny : maxy) - centCalc.forw[1]) / vec.forw[1];
|
|
701
|
+
|
|
702
|
+
if (Math.abs(xRate) / Math.abs(yRate) < 1.1) {
|
|
703
|
+
const node = {
|
|
704
|
+
forw: [
|
|
705
|
+
vec.forw[0] * xRate + centCalc.forw[0],
|
|
706
|
+
vec.forw[1] * xRate + centCalc.forw[1],
|
|
707
|
+
] as Position,
|
|
708
|
+
bakw: [
|
|
709
|
+
vec.bakw[0] * xRate + centCalc.bakw[0],
|
|
710
|
+
vec.bakw[1] * xRate + centCalc.bakw[1],
|
|
711
|
+
] as Position,
|
|
712
|
+
};
|
|
713
|
+
if (vec.forw[0] < 0) {
|
|
714
|
+
prev[3].push(node);
|
|
715
|
+
} else {
|
|
716
|
+
prev[1].push(node);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
if (Math.abs(yRate) / Math.abs(xRate) < 1.1) {
|
|
721
|
+
const node = {
|
|
722
|
+
forw: [
|
|
723
|
+
vec.forw[0] * yRate + centCalc.forw[0],
|
|
724
|
+
vec.forw[1] * yRate + centCalc.forw[1],
|
|
725
|
+
] as Position,
|
|
726
|
+
bakw: [
|
|
727
|
+
vec.bakw[0] * yRate + centCalc.bakw[0],
|
|
728
|
+
vec.bakw[1] * yRate + centCalc.bakw[1],
|
|
729
|
+
] as Position,
|
|
730
|
+
};
|
|
731
|
+
if (vec.forw[1] < 0) {
|
|
732
|
+
prev[0].push(node);
|
|
733
|
+
} else {
|
|
734
|
+
prev[2].push(node);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
return prev;
|
|
739
|
+
}, [[], [], [], []] as Array<Array<{ forw: Position; bakw: Position }>>);
|
|
740
|
+
|
|
741
|
+
// Calculate vertex angles
|
|
742
|
+
const vertexCalc = Object.keys(convexBuf).reduce(
|
|
743
|
+
(prev, key, index, arr) => {
|
|
744
|
+
const item = convexBuf[key];
|
|
745
|
+
const forw = item.forw;
|
|
746
|
+
const bakw = item.bakw;
|
|
747
|
+
|
|
748
|
+
const vec = {
|
|
749
|
+
forw: [forw[0] - centCalc.forw[0], forw[1] - centCalc.forw[1]],
|
|
750
|
+
bakw: [bakw[0] - centCalc.bakw[0], centCalc.bakw[1] - bakw[1]],
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
if (vec.forw[0] === 0 || vec.forw[1] === 0) return prev;
|
|
754
|
+
|
|
755
|
+
let quad = 0;
|
|
756
|
+
if (vec.forw[0] > 0) quad += 1;
|
|
757
|
+
if (vec.forw[1] > 0) quad += 2;
|
|
758
|
+
|
|
759
|
+
prev[quad].push([vec.forw, vec.bakw]);
|
|
760
|
+
|
|
761
|
+
if (index === arr.length - 1) {
|
|
762
|
+
return prev.map((quadArr) => {
|
|
763
|
+
return quadArr.reduce(
|
|
764
|
+
(
|
|
765
|
+
qprev: number[] | undefined,
|
|
766
|
+
curr: number[][],
|
|
767
|
+
qindex: number,
|
|
768
|
+
qarr: number[][][],
|
|
769
|
+
) => {
|
|
770
|
+
if (!qprev) qprev = [Infinity, 0, 0];
|
|
771
|
+
|
|
772
|
+
let ratio =
|
|
773
|
+
Math.sqrt(Math.pow(curr[0][0], 2) + Math.pow(curr[0][1], 2)) /
|
|
774
|
+
Math.sqrt(Math.pow(curr[1][0], 2) + Math.pow(curr[1][1], 2));
|
|
775
|
+
ratio = ratio < qprev[0] ? ratio : qprev[0];
|
|
776
|
+
|
|
777
|
+
const theta = Math.atan2(curr[0][0], curr[0][1]) -
|
|
778
|
+
Math.atan2(curr[1][0], curr[1][1]);
|
|
779
|
+
const sumcos = qprev[1] + Math.cos(theta);
|
|
780
|
+
const sumsin = qprev[2] + Math.sin(theta);
|
|
781
|
+
|
|
782
|
+
if (qindex === qarr.length - 1) {
|
|
783
|
+
return [ratio, Math.atan2(sumsin, sumcos)];
|
|
784
|
+
}
|
|
785
|
+
return [ratio, sumcos, sumsin];
|
|
786
|
+
},
|
|
787
|
+
null as any,
|
|
788
|
+
);
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
return prev;
|
|
793
|
+
},
|
|
794
|
+
[[], [], [], []] as any[],
|
|
795
|
+
);
|
|
796
|
+
|
|
797
|
+
let vertexRatio: any[] = vertexCalc;
|
|
798
|
+
if (vertexRatio.length === 1) {
|
|
799
|
+
vertexRatio = [
|
|
800
|
+
vertexRatio[0],
|
|
801
|
+
vertexRatio[0],
|
|
802
|
+
vertexRatio[0],
|
|
803
|
+
vertexRatio[0],
|
|
804
|
+
];
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// Calculate vertices
|
|
808
|
+
const vertices = vertexRatio.map((ratio: any, index: number) => {
|
|
809
|
+
const forVertex = bbox[index];
|
|
810
|
+
const forDelta = [
|
|
811
|
+
forVertex[0] - centCalc.forw[0],
|
|
812
|
+
forVertex[1] - centCalc.forw[1],
|
|
813
|
+
];
|
|
814
|
+
const forDistance = Math.sqrt(
|
|
815
|
+
Math.pow(forDelta[0], 2) + Math.pow(forDelta[1], 2),
|
|
816
|
+
);
|
|
817
|
+
const bakDistance = forDistance / ratio[0];
|
|
818
|
+
const bakTheta = Math.atan2(forDelta[0], forDelta[1]) - ratio[1];
|
|
819
|
+
const bakw: Position = [
|
|
820
|
+
centCalc.bakw[0] + bakDistance * Math.sin(bakTheta),
|
|
821
|
+
centCalc.bakw[1] - bakDistance * Math.cos(bakTheta),
|
|
822
|
+
];
|
|
823
|
+
|
|
824
|
+
return { forw: forVertex, bakw };
|
|
825
|
+
});
|
|
826
|
+
|
|
827
|
+
// Swap vertices 2 and 3 for correct ordering
|
|
828
|
+
const swap = vertices[2];
|
|
829
|
+
vertices[2] = vertices[3];
|
|
830
|
+
vertices[3] = swap;
|
|
831
|
+
|
|
832
|
+
// Check edge intersections
|
|
833
|
+
this.checkAndAdjustVertices(vertices, edgeNodes, centCalc);
|
|
834
|
+
|
|
835
|
+
return vertices;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* 鳥瞰図モードの頂点を計算
|
|
840
|
+
*/
|
|
841
|
+
private calculateBirdeyeVertices(
|
|
842
|
+
convexBuf: Record<string, { forw: Position; bakw: Position }>,
|
|
843
|
+
centCalc: { forw: Position; bakw: Position },
|
|
844
|
+
bbox: Position[],
|
|
845
|
+
minx: number,
|
|
846
|
+
maxx: number,
|
|
847
|
+
miny: number,
|
|
848
|
+
maxy: number,
|
|
849
|
+
): Array<{ forw: Position; bakw: Position }> {
|
|
850
|
+
// For birdeye mode, implement special vertex calculation
|
|
851
|
+
// This is a simplified version, actual implementation might be more complex
|
|
852
|
+
return this.calculatePlainVertices(
|
|
853
|
+
convexBuf,
|
|
854
|
+
centCalc,
|
|
855
|
+
bbox,
|
|
856
|
+
minx,
|
|
857
|
+
maxx,
|
|
858
|
+
miny,
|
|
859
|
+
maxy,
|
|
860
|
+
);
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/**
|
|
864
|
+
* 頂点の位置を調整
|
|
865
|
+
*/
|
|
866
|
+
private checkAndAdjustVertices(
|
|
867
|
+
vertices: Array<{ forw: Position; bakw: Position }>,
|
|
868
|
+
edgeNodes: Array<Array<{ forw: Position; bakw: Position }>>,
|
|
869
|
+
centCalc: { forw: Position; bakw: Position },
|
|
870
|
+
): void {
|
|
871
|
+
const expandRatio = [1, 1, 1, 1];
|
|
872
|
+
|
|
873
|
+
for (let i = 0; i < 4; i++) {
|
|
874
|
+
const j = (i + 1) % 4;
|
|
875
|
+
const side = lineString([vertices[i].bakw, vertices[j].bakw]);
|
|
876
|
+
|
|
877
|
+
edgeNodes[i].map((node) => {
|
|
878
|
+
const line = lineString([centCalc.bakw, node.bakw]);
|
|
879
|
+
const intersect = lineIntersect(side, line);
|
|
880
|
+
|
|
881
|
+
if (intersect.features.length > 0 && intersect.features[0].geometry) {
|
|
882
|
+
const intersectPt = intersect.features[0];
|
|
883
|
+
const distance = Math.sqrt(
|
|
884
|
+
Math.pow(node.bakw[0] - centCalc.bakw[0], 2) +
|
|
885
|
+
Math.pow(node.bakw[1] - centCalc.bakw[1], 2),
|
|
886
|
+
);
|
|
887
|
+
const intDistance = Math.sqrt(
|
|
888
|
+
Math.pow(
|
|
889
|
+
intersectPt.geometry.coordinates[0] - centCalc.bakw[0],
|
|
890
|
+
2,
|
|
891
|
+
) +
|
|
892
|
+
Math.pow(
|
|
893
|
+
intersectPt.geometry.coordinates[1] - centCalc.bakw[1],
|
|
894
|
+
2,
|
|
895
|
+
),
|
|
896
|
+
);
|
|
897
|
+
const ratio = distance / intDistance;
|
|
898
|
+
|
|
899
|
+
if (ratio > expandRatio[i]) expandRatio[i] = ratio;
|
|
900
|
+
if (ratio > expandRatio[j]) expandRatio[j] = ratio;
|
|
901
|
+
}
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
vertices.forEach((vertex, i) => {
|
|
906
|
+
const ratio = expandRatio[i];
|
|
907
|
+
const bakw: Position = [
|
|
908
|
+
(vertex.bakw[0] - centCalc.bakw[0]) * ratio + centCalc.bakw[0],
|
|
909
|
+
(vertex.bakw[1] - centCalc.bakw[1]) * ratio + centCalc.bakw[1],
|
|
910
|
+
];
|
|
911
|
+
vertex.bakw = bakw;
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* 点の重み付けを計算します
|
|
917
|
+
*/
|
|
918
|
+
calculatePointsWeight(): void {
|
|
919
|
+
const calcTargets = ["forw"];
|
|
920
|
+
if (this.strict_status === Tin.STATUS_LOOSE) {
|
|
921
|
+
calcTargets.push("bakw");
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
const weightBuffer: WeightBufferBD = {};
|
|
925
|
+
|
|
926
|
+
calcTargets.forEach((target) => {
|
|
927
|
+
weightBuffer[target as keyof WeightBufferBD] = {};
|
|
928
|
+
const tin_points: Record<string, number> = {};
|
|
929
|
+
|
|
930
|
+
this.tins![target as keyof TinsBD]!.features.map((tri: Tri) => {
|
|
931
|
+
const props = ["a", "b", "c"] as PropertyTriKey[];
|
|
932
|
+
for (let i = 0; i < 3; i++) {
|
|
933
|
+
const j = (i + 1) % 3;
|
|
934
|
+
const prop_i = props[i];
|
|
935
|
+
const prop_j = props[j];
|
|
936
|
+
const index_i = tri.properties![prop_i].index;
|
|
937
|
+
const index_j = tri.properties![prop_j].index;
|
|
938
|
+
const key = [index_i, index_j].sort().join("-");
|
|
939
|
+
|
|
940
|
+
if (!tin_points[key]) {
|
|
941
|
+
const i_xy = tri.geometry!.coordinates[0][i];
|
|
942
|
+
const j_xy = tri.geometry!.coordinates[0][j];
|
|
943
|
+
const i_merc = tri.properties![prop_i].geom;
|
|
944
|
+
const j_merc = tri.properties![prop_j].geom;
|
|
945
|
+
|
|
946
|
+
tin_points[key] = 1;
|
|
947
|
+
const ratio = Math.sqrt(
|
|
948
|
+
Math.pow(i_merc[0] - j_merc[0], 2) +
|
|
949
|
+
Math.pow(i_merc[1] - j_merc[1], 2),
|
|
950
|
+
) / Math.sqrt(
|
|
951
|
+
Math.pow(i_xy[0] - j_xy[0], 2) + Math.pow(i_xy[1] - j_xy[1], 2),
|
|
952
|
+
);
|
|
953
|
+
|
|
954
|
+
if (!weightBuffer[target as keyof WeightBufferBD]) {
|
|
955
|
+
weightBuffer[target as keyof WeightBufferBD] = {};
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
const targetBuffer = weightBuffer[target as keyof WeightBufferBD]!;
|
|
959
|
+
targetBuffer[`${index_i}:${key}`] = ratio;
|
|
960
|
+
targetBuffer[`${index_j}:${key}`] = ratio;
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
});
|
|
964
|
+
});
|
|
965
|
+
|
|
966
|
+
// Calculate average weights
|
|
967
|
+
const pointsWeightBuffer: WeightBufferBD = {};
|
|
968
|
+
|
|
969
|
+
calcTargets.map((target) => {
|
|
970
|
+
pointsWeightBuffer[target as keyof WeightBufferBD] = {};
|
|
971
|
+
|
|
972
|
+
if (this.strict_status === Tin.STATUS_STRICT) {
|
|
973
|
+
pointsWeightBuffer.bakw = {};
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
const targetBuffer = weightBuffer[target as keyof WeightBufferBD]!;
|
|
977
|
+
const pointWeights: { [pointId: string]: number[] } = {};
|
|
978
|
+
|
|
979
|
+
// Group weights by point ID
|
|
980
|
+
Object.keys(targetBuffer).forEach((key) => {
|
|
981
|
+
const [pointId] = key.split(":");
|
|
982
|
+
if (!pointWeights[pointId]) {
|
|
983
|
+
pointWeights[pointId] = [];
|
|
984
|
+
}
|
|
985
|
+
pointWeights[pointId].push(targetBuffer[key]);
|
|
986
|
+
});
|
|
987
|
+
|
|
988
|
+
// Calculate average weight for each point
|
|
989
|
+
Object.keys(pointWeights).forEach((pointId) => {
|
|
990
|
+
const weights = pointWeights[pointId];
|
|
991
|
+
const avgWeight = weights.reduce((sum, w) => sum + w, 0) /
|
|
992
|
+
weights.length;
|
|
993
|
+
|
|
994
|
+
if (!pointsWeightBuffer[target as keyof WeightBufferBD]) {
|
|
995
|
+
pointsWeightBuffer[target as keyof WeightBufferBD] = {};
|
|
996
|
+
}
|
|
997
|
+
pointsWeightBuffer[target as keyof WeightBufferBD]![pointId] =
|
|
998
|
+
avgWeight;
|
|
999
|
+
|
|
1000
|
+
if (this.strict_status === Tin.STATUS_STRICT) {
|
|
1001
|
+
if (!pointsWeightBuffer.bakw) {
|
|
1002
|
+
pointsWeightBuffer.bakw = {};
|
|
1003
|
+
}
|
|
1004
|
+
pointsWeightBuffer.bakw[pointId] = 1 / avgWeight;
|
|
1005
|
+
}
|
|
1006
|
+
});
|
|
1007
|
+
|
|
1008
|
+
// Calculate centroid weight
|
|
1009
|
+
let centroidSum = 0;
|
|
1010
|
+
for (let i = 0; i < 4; i++) {
|
|
1011
|
+
const key = `b${i}`;
|
|
1012
|
+
const weight =
|
|
1013
|
+
pointsWeightBuffer[target as keyof WeightBufferBD]![key] || 0;
|
|
1014
|
+
centroidSum += weight;
|
|
1015
|
+
}
|
|
1016
|
+
pointsWeightBuffer[target as keyof WeightBufferBD]!["c"] = centroidSum /
|
|
1017
|
+
4;
|
|
1018
|
+
|
|
1019
|
+
if (this.strict_status === Tin.STATUS_STRICT && pointsWeightBuffer.bakw) {
|
|
1020
|
+
pointsWeightBuffer.bakw["c"] = 1 /
|
|
1021
|
+
pointsWeightBuffer[target as keyof WeightBufferBD]!["c"];
|
|
1022
|
+
}
|
|
1023
|
+
});
|
|
1024
|
+
|
|
1025
|
+
this.pointsWeightBuffer = pointsWeightBuffer;
|
|
1026
|
+
}
|
|
1027
|
+
}
|