@maplat/tin 0.11.1 → 0.11.2

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