@maplat/tin 0.12.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,284 +1,283 @@
1
- import { lineString } from "@turf/helpers";
2
- import lineIntersect from "@turf/line-intersect";
3
- import type { Position } from "geojson";
4
- import type { VertexPosition } from "./types/tin.d.ts";
5
-
6
- interface ConvexEntry {
7
- forw: Position;
8
- bakw: Position;
9
- }
10
-
11
- interface BoundaryVerticesParams {
12
- convexBuf: Record<string, ConvexEntry>;
13
- centroid: { forw: Position; bakw: Position };
14
- bbox: Position[];
15
- minx: number;
16
- maxx: number;
17
- miny: number;
18
- maxy: number;
19
- }
20
-
21
- type EdgeRayBuckets = Array<Array<ConvexEntry>>;
22
- type SamplePair = {
23
- forw: [number, number];
24
- bakw: [number, number];
25
- };
26
-
27
- function buildEdgeBuckets(
28
- convexBuf: Record<string, ConvexEntry>,
29
- centroid: { forw: Position; bakw: Position },
30
- minx: number,
31
- maxx: number,
32
- miny: number,
33
- maxy: number,
34
- ): EdgeRayBuckets {
35
- return Object.keys(convexBuf).reduce((prev, key) => {
36
- const item = convexBuf[key];
37
- const forw = item.forw;
38
- const bakw = item.bakw;
39
-
40
- const vec = {
41
- forw: [forw[0] - centroid.forw[0], forw[1] - centroid.forw[1]],
42
- bakw: [bakw[0] - centroid.bakw[0], bakw[1] - centroid.bakw[1]],
43
- };
44
-
45
- const xRate = vec.forw[0] === 0
46
- ? Infinity
47
- : ((vec.forw[0] < 0 ? minx : maxx) - centroid.forw[0]) / vec.forw[0];
48
- const yRate = vec.forw[1] === 0
49
- ? Infinity
50
- : ((vec.forw[1] < 0 ? miny : maxy) - centroid.forw[1]) / vec.forw[1];
51
-
52
- if (Math.abs(xRate) / Math.abs(yRate) < 1.1) {
53
- const node = {
54
- forw: [
55
- vec.forw[0] * xRate + centroid.forw[0],
56
- vec.forw[1] * xRate + centroid.forw[1],
57
- ] as Position,
58
- bakw: [
59
- vec.bakw[0] * xRate + centroid.bakw[0],
60
- vec.bakw[1] * xRate + centroid.bakw[1],
61
- ] as Position,
62
- };
63
- if (vec.forw[0] < 0) {
64
- prev[3].push(node);
65
- } else {
66
- prev[1].push(node);
67
- }
68
- }
69
-
70
- if (Math.abs(yRate) / Math.abs(xRate) < 1.1) {
71
- const node = {
72
- forw: [
73
- vec.forw[0] * yRate + centroid.forw[0],
74
- vec.forw[1] * yRate + centroid.forw[1],
75
- ] as Position,
76
- bakw: [
77
- vec.bakw[0] * yRate + centroid.bakw[0],
78
- vec.bakw[1] * yRate + centroid.bakw[1],
79
- ] as Position,
80
- };
81
- if (vec.forw[1] < 0) {
82
- prev[0].push(node);
83
- } else {
84
- prev[2].push(node);
85
- }
86
- }
87
-
88
- return prev;
89
- }, [[], [], [], []] as EdgeRayBuckets);
90
- }
91
-
92
- function collectSamples(
93
- convexBuf: Record<string, ConvexEntry>,
94
- centroid: { forw: Position; bakw: Position },
95
- ): { perQuad: SamplePair[][]; aggregate: SamplePair[] } {
96
- const perQuad: SamplePair[][] = [[], [], [], []];
97
- const aggregate: SamplePair[] = [];
98
-
99
- Object.keys(convexBuf).forEach((key) => {
100
- const item = convexBuf[key];
101
- const forw = item.forw;
102
- const bakw = item.bakw;
103
-
104
- const forwDelta: [number, number] = [
105
- forw[0] - centroid.forw[0],
106
- forw[1] - centroid.forw[1],
107
- ];
108
- const bakwDelta: [number, number] = [
109
- bakw[0] - centroid.bakw[0],
110
- centroid.bakw[1] - bakw[1],
111
- ];
112
-
113
- const sample: SamplePair = { forw: forwDelta, bakw: bakwDelta };
114
- aggregate.push(sample);
115
-
116
- if (forwDelta[0] === 0 || forwDelta[1] === 0) {
117
- return;
118
- }
119
-
120
- let quad = 0;
121
- if (forwDelta[0] > 0) quad += 1;
122
- if (forwDelta[1] > 0) quad += 2;
123
- perQuad[quad].push(sample);
124
- });
125
-
126
- return { perQuad, aggregate };
127
- }
128
-
129
- function reduceSamples(samples: SamplePair[]): [number, number] {
130
- let minRatio = Infinity;
131
- let sumCos = 0;
132
- let sumSin = 0;
133
-
134
- samples.forEach((sample) => {
135
- const { forw, bakw } = sample;
136
- const forwNorm = Math.hypot(forw[0], forw[1]);
137
- const bakwNorm = Math.hypot(bakw[0], bakw[1]);
138
- if (bakwNorm === 0) return;
139
-
140
- const ratio = forwNorm / bakwNorm;
141
- const theta = Math.atan2(forw[0], forw[1]) - Math.atan2(bakw[0], bakw[1]);
142
-
143
- minRatio = Math.min(minRatio, ratio);
144
- sumCos += Math.cos(theta);
145
- sumSin += Math.sin(theta);
146
- });
147
-
148
- if (!isFinite(minRatio)) {
149
- return [1, 0];
150
- }
151
-
152
- return [minRatio, Math.atan2(sumSin, sumCos)];
153
- }
154
-
155
- function buildVertexRatio(
156
- convexBuf: Record<string, ConvexEntry>,
157
- centroid: { forw: Position; bakw: Position },
158
- mode: "plain" | "birdeye",
159
- ): Array<[number, number]> {
160
- const { perQuad, aggregate } = collectSamples(convexBuf, centroid);
161
- const hasAllQuadrants = perQuad.every((samples) => samples.length > 0);
162
-
163
- const groups: SamplePair[][] = mode === "birdeye"
164
- ? (hasAllQuadrants ? perQuad : [aggregate])
165
- : [aggregate];
166
-
167
- const vertexRatio = groups.map((samples) => reduceSamples(samples));
168
-
169
- if (vertexRatio.length === 1) {
170
- return [vertexRatio[0], vertexRatio[0], vertexRatio[0], vertexRatio[0]];
171
- }
172
-
173
- return vertexRatio;
174
- }
175
-
176
- function checkAndAdjustVertices(
177
- vertices: VertexPosition[],
178
- edgeNodes: EdgeRayBuckets,
179
- centroid: { forw: Position; bakw: Position },
180
- ): void {
181
- const expandRatio = [1, 1, 1, 1];
182
-
183
- for (let i = 0; i < 4; i++) {
184
- const j = (i + 1) % 4;
185
- const side = lineString([vertices[i].bakw, vertices[j].bakw]);
186
-
187
- edgeNodes[i].map((node) => {
188
- const line = lineString([centroid.bakw, node.bakw]);
189
- const intersect = lineIntersect(side, line);
190
-
191
- if (intersect.features.length > 0 && intersect.features[0].geometry) {
192
- const intersectPt = intersect.features[0];
193
- const distance = Math.sqrt(
194
- Math.pow(node.bakw[0] - centroid.bakw[0], 2) +
195
- Math.pow(node.bakw[1] - centroid.bakw[1], 2),
196
- );
197
- const intDistance = Math.sqrt(
198
- Math.pow(
199
- intersectPt.geometry.coordinates[0] - centroid.bakw[0],
200
- 2,
201
- ) +
202
- Math.pow(
203
- intersectPt.geometry.coordinates[1] - centroid.bakw[1],
204
- 2,
205
- ),
206
- );
207
- const ratio = distance / intDistance;
208
-
209
- if (ratio > expandRatio[i]) expandRatio[i] = ratio;
210
- if (ratio > expandRatio[j]) expandRatio[j] = ratio;
211
- }
212
- });
213
- }
214
-
215
- vertices.forEach((vertex, i) => {
216
- const ratio = expandRatio[i];
217
- const bakw: Position = [
218
- (vertex.bakw[0] - centroid.bakw[0]) * ratio + centroid.bakw[0],
219
- (vertex.bakw[1] - centroid.bakw[1]) * ratio + centroid.bakw[1],
220
- ];
221
- vertex.bakw = bakw;
222
- });
223
- }
224
-
225
- function finalizeVertices(
226
- vertexRatio: Array<[number, number]>,
227
- bbox: Position[],
228
- centroid: { forw: Position; bakw: Position },
229
- edgeNodes: EdgeRayBuckets,
230
- ): VertexPosition[] {
231
- const vertices = vertexRatio.map((ratio, index) => {
232
- const forVertex = bbox[index];
233
- const forDelta = [
234
- forVertex[0] - centroid.forw[0],
235
- forVertex[1] - centroid.forw[1],
236
- ];
237
- const forDistance = Math.sqrt(
238
- Math.pow(forDelta[0], 2) + Math.pow(forDelta[1], 2),
239
- );
240
- const bakDistance = forDistance / ratio[0];
241
- const bakTheta = Math.atan2(forDelta[0], forDelta[1]) - ratio[1];
242
- const bakw: Position = [
243
- centroid.bakw[0] + bakDistance * Math.sin(bakTheta),
244
- centroid.bakw[1] - bakDistance * Math.cos(bakTheta),
245
- ];
246
-
247
- return { forw: forVertex, bakw };
248
- });
249
-
250
- const swap = vertices[2];
251
- vertices[2] = vertices[3];
252
- vertices[3] = swap;
253
-
254
- checkAndAdjustVertices(vertices, edgeNodes, centroid);
255
-
256
- return vertices;
257
- }
258
-
259
- /**
260
- * Calculate the standard four boundary vertices around the centroid.
261
- */
262
- export function calculatePlainVertices(
263
- params: BoundaryVerticesParams,
264
- ): VertexPosition[] {
265
- const { convexBuf, centroid, bbox, minx, maxx, miny, maxy } = params;
266
-
267
- const edgeNodes = buildEdgeBuckets(convexBuf, centroid, minx, maxx, miny, maxy);
268
- const vertexRatio = buildVertexRatio(convexBuf, centroid, "plain");
269
-
270
- return finalizeVertices(vertexRatio, bbox, centroid, edgeNodes);
271
- }
272
-
273
- /**
274
- * Calculate boundary vertices for bird's-eye mode using quadrant-aware ratios.
275
- */
276
- export function calculateBirdeyeVertices(
277
- params: BoundaryVerticesParams,
278
- ): VertexPosition[] {
279
- const { convexBuf, centroid, bbox, minx, maxx, miny, maxy } = params;
280
- const edgeNodes = buildEdgeBuckets(convexBuf, centroid, minx, maxx, miny, maxy);
281
- const vertexRatio = buildVertexRatio(convexBuf, centroid, "birdeye");
282
-
283
- return finalizeVertices(vertexRatio, bbox, centroid, edgeNodes);
284
- }
1
+ import { lineString, lineIntersect } from "@turf/turf";
2
+ import type { Position } from "geojson";
3
+ import type { VertexPosition } from "./types/tin.d.ts";
4
+
5
+ interface ConvexEntry {
6
+ forw: Position;
7
+ bakw: Position;
8
+ }
9
+
10
+ interface BoundaryVerticesParams {
11
+ convexBuf: Record<string, ConvexEntry>;
12
+ centroid: { forw: Position; bakw: Position };
13
+ bbox: Position[];
14
+ minx: number;
15
+ maxx: number;
16
+ miny: number;
17
+ maxy: number;
18
+ }
19
+
20
+ type EdgeRayBuckets = Array<Array<ConvexEntry>>;
21
+ type SamplePair = {
22
+ forw: [number, number];
23
+ bakw: [number, number];
24
+ };
25
+
26
+ function buildEdgeBuckets(
27
+ convexBuf: Record<string, ConvexEntry>,
28
+ centroid: { forw: Position; bakw: Position },
29
+ minx: number,
30
+ maxx: number,
31
+ miny: number,
32
+ maxy: number,
33
+ ): EdgeRayBuckets {
34
+ return Object.keys(convexBuf).reduce((prev, key) => {
35
+ const item = convexBuf[key];
36
+ const forw = item.forw;
37
+ const bakw = item.bakw;
38
+
39
+ const vec = {
40
+ forw: [forw[0] - centroid.forw[0], forw[1] - centroid.forw[1]],
41
+ bakw: [bakw[0] - centroid.bakw[0], bakw[1] - centroid.bakw[1]],
42
+ };
43
+
44
+ const xRate = vec.forw[0] === 0
45
+ ? Infinity
46
+ : ((vec.forw[0] < 0 ? minx : maxx) - centroid.forw[0]) / vec.forw[0];
47
+ const yRate = vec.forw[1] === 0
48
+ ? Infinity
49
+ : ((vec.forw[1] < 0 ? miny : maxy) - centroid.forw[1]) / vec.forw[1];
50
+
51
+ if (Math.abs(xRate) / Math.abs(yRate) < 1.1) {
52
+ const node = {
53
+ forw: [
54
+ vec.forw[0] * xRate + centroid.forw[0],
55
+ vec.forw[1] * xRate + centroid.forw[1],
56
+ ] as Position,
57
+ bakw: [
58
+ vec.bakw[0] * xRate + centroid.bakw[0],
59
+ vec.bakw[1] * xRate + centroid.bakw[1],
60
+ ] as Position,
61
+ };
62
+ if (vec.forw[0] < 0) {
63
+ prev[3].push(node);
64
+ } else {
65
+ prev[1].push(node);
66
+ }
67
+ }
68
+
69
+ if (Math.abs(yRate) / Math.abs(xRate) < 1.1) {
70
+ const node = {
71
+ forw: [
72
+ vec.forw[0] * yRate + centroid.forw[0],
73
+ vec.forw[1] * yRate + centroid.forw[1],
74
+ ] as Position,
75
+ bakw: [
76
+ vec.bakw[0] * yRate + centroid.bakw[0],
77
+ vec.bakw[1] * yRate + centroid.bakw[1],
78
+ ] as Position,
79
+ };
80
+ if (vec.forw[1] < 0) {
81
+ prev[0].push(node);
82
+ } else {
83
+ prev[2].push(node);
84
+ }
85
+ }
86
+
87
+ return prev;
88
+ }, [[], [], [], []] as EdgeRayBuckets);
89
+ }
90
+
91
+ function collectSamples(
92
+ convexBuf: Record<string, ConvexEntry>,
93
+ centroid: { forw: Position; bakw: Position },
94
+ ): { perQuad: SamplePair[][]; aggregate: SamplePair[] } {
95
+ const perQuad: SamplePair[][] = [[], [], [], []];
96
+ const aggregate: SamplePair[] = [];
97
+
98
+ Object.keys(convexBuf).forEach((key) => {
99
+ const item = convexBuf[key];
100
+ const forw = item.forw;
101
+ const bakw = item.bakw;
102
+
103
+ const forwDelta: [number, number] = [
104
+ forw[0] - centroid.forw[0],
105
+ forw[1] - centroid.forw[1],
106
+ ];
107
+ const bakwDelta: [number, number] = [
108
+ bakw[0] - centroid.bakw[0],
109
+ centroid.bakw[1] - bakw[1],
110
+ ];
111
+
112
+ const sample: SamplePair = { forw: forwDelta, bakw: bakwDelta };
113
+ aggregate.push(sample);
114
+
115
+ if (forwDelta[0] === 0 || forwDelta[1] === 0) {
116
+ return;
117
+ }
118
+
119
+ let quad = 0;
120
+ if (forwDelta[0] > 0) quad += 1;
121
+ if (forwDelta[1] > 0) quad += 2;
122
+ perQuad[quad].push(sample);
123
+ });
124
+
125
+ return { perQuad, aggregate };
126
+ }
127
+
128
+ function reduceSamples(samples: SamplePair[]): [number, number] {
129
+ let minRatio = Infinity;
130
+ let sumCos = 0;
131
+ let sumSin = 0;
132
+
133
+ samples.forEach((sample) => {
134
+ const { forw, bakw } = sample;
135
+ const forwNorm = Math.hypot(forw[0], forw[1]);
136
+ const bakwNorm = Math.hypot(bakw[0], bakw[1]);
137
+ if (bakwNorm === 0) return;
138
+
139
+ const ratio = forwNorm / bakwNorm;
140
+ const theta = Math.atan2(forw[0], forw[1]) - Math.atan2(bakw[0], bakw[1]);
141
+
142
+ minRatio = Math.min(minRatio, ratio);
143
+ sumCos += Math.cos(theta);
144
+ sumSin += Math.sin(theta);
145
+ });
146
+
147
+ if (!isFinite(minRatio)) {
148
+ return [1, 0];
149
+ }
150
+
151
+ return [minRatio, Math.atan2(sumSin, sumCos)];
152
+ }
153
+
154
+ function buildVertexRatio(
155
+ convexBuf: Record<string, ConvexEntry>,
156
+ centroid: { forw: Position; bakw: Position },
157
+ mode: "plain" | "birdeye",
158
+ ): Array<[number, number]> {
159
+ const { perQuad, aggregate } = collectSamples(convexBuf, centroid);
160
+ const hasAllQuadrants = perQuad.every((samples) => samples.length > 0);
161
+
162
+ const groups: SamplePair[][] = mode === "birdeye"
163
+ ? (hasAllQuadrants ? perQuad : [aggregate])
164
+ : [aggregate];
165
+
166
+ const vertexRatio = groups.map((samples) => reduceSamples(samples));
167
+
168
+ if (vertexRatio.length === 1) {
169
+ return [vertexRatio[0], vertexRatio[0], vertexRatio[0], vertexRatio[0]];
170
+ }
171
+
172
+ return vertexRatio;
173
+ }
174
+
175
+ function checkAndAdjustVertices(
176
+ vertices: VertexPosition[],
177
+ edgeNodes: EdgeRayBuckets,
178
+ centroid: { forw: Position; bakw: Position },
179
+ ): void {
180
+ const expandRatio = [1, 1, 1, 1];
181
+
182
+ for (let i = 0; i < 4; i++) {
183
+ const j = (i + 1) % 4;
184
+ const side = lineString([vertices[i].bakw, vertices[j].bakw]);
185
+
186
+ edgeNodes[i].map((node) => {
187
+ const line = lineString([centroid.bakw, node.bakw]);
188
+ const intersect = lineIntersect(side, line);
189
+
190
+ if (intersect.features.length > 0 && intersect.features[0].geometry) {
191
+ const intersectPt = intersect.features[0];
192
+ const distance = Math.sqrt(
193
+ Math.pow(node.bakw[0] - centroid.bakw[0], 2) +
194
+ Math.pow(node.bakw[1] - centroid.bakw[1], 2),
195
+ );
196
+ const intDistance = Math.sqrt(
197
+ Math.pow(
198
+ intersectPt.geometry.coordinates[0] - centroid.bakw[0],
199
+ 2,
200
+ ) +
201
+ Math.pow(
202
+ intersectPt.geometry.coordinates[1] - centroid.bakw[1],
203
+ 2,
204
+ ),
205
+ );
206
+ const ratio = distance / intDistance;
207
+
208
+ if (ratio > expandRatio[i]) expandRatio[i] = ratio;
209
+ if (ratio > expandRatio[j]) expandRatio[j] = ratio;
210
+ }
211
+ });
212
+ }
213
+
214
+ vertices.forEach((vertex, i) => {
215
+ const ratio = expandRatio[i];
216
+ const bakw: Position = [
217
+ (vertex.bakw[0] - centroid.bakw[0]) * ratio + centroid.bakw[0],
218
+ (vertex.bakw[1] - centroid.bakw[1]) * ratio + centroid.bakw[1],
219
+ ];
220
+ vertex.bakw = bakw;
221
+ });
222
+ }
223
+
224
+ function finalizeVertices(
225
+ vertexRatio: Array<[number, number]>,
226
+ bbox: Position[],
227
+ centroid: { forw: Position; bakw: Position },
228
+ edgeNodes: EdgeRayBuckets,
229
+ ): VertexPosition[] {
230
+ const vertices = vertexRatio.map((ratio, index) => {
231
+ const forVertex = bbox[index];
232
+ const forDelta = [
233
+ forVertex[0] - centroid.forw[0],
234
+ forVertex[1] - centroid.forw[1],
235
+ ];
236
+ const forDistance = Math.sqrt(
237
+ Math.pow(forDelta[0], 2) + Math.pow(forDelta[1], 2),
238
+ );
239
+ const bakDistance = forDistance / ratio[0];
240
+ const bakTheta = Math.atan2(forDelta[0], forDelta[1]) - ratio[1];
241
+ const bakw: Position = [
242
+ centroid.bakw[0] + bakDistance * Math.sin(bakTheta),
243
+ centroid.bakw[1] - bakDistance * Math.cos(bakTheta),
244
+ ];
245
+
246
+ return { forw: forVertex, bakw };
247
+ });
248
+
249
+ const swap = vertices[2];
250
+ vertices[2] = vertices[3];
251
+ vertices[3] = swap;
252
+
253
+ checkAndAdjustVertices(vertices, edgeNodes, centroid);
254
+
255
+ return vertices;
256
+ }
257
+
258
+ /**
259
+ * Calculate the standard four boundary vertices around the centroid.
260
+ */
261
+ export function calculatePlainVertices(
262
+ params: BoundaryVerticesParams,
263
+ ): VertexPosition[] {
264
+ const { convexBuf, centroid, bbox, minx, maxx, miny, maxy } = params;
265
+
266
+ const edgeNodes = buildEdgeBuckets(convexBuf, centroid, minx, maxx, miny, maxy);
267
+ const vertexRatio = buildVertexRatio(convexBuf, centroid, "plain");
268
+
269
+ return finalizeVertices(vertexRatio, bbox, centroid, edgeNodes);
270
+ }
271
+
272
+ /**
273
+ * Calculate boundary vertices for bird's-eye mode using quadrant-aware ratios.
274
+ */
275
+ export function calculateBirdeyeVertices(
276
+ params: BoundaryVerticesParams,
277
+ ): VertexPosition[] {
278
+ const { convexBuf, centroid, bbox, minx, maxx, miny, maxy } = params;
279
+ const edgeNodes = buildEdgeBuckets(convexBuf, centroid, minx, maxx, miny, maxy);
280
+ const vertexRatio = buildVertexRatio(convexBuf, centroid, "birdeye");
281
+
282
+ return finalizeVertices(vertexRatio, bbox, centroid, edgeNodes);
283
+ }