@maplat/tin 0.13.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +224 -224
- package/README.ja.md +220 -214
- package/README.md +221 -215
- package/dist/boundary-vertices.d.ts +33 -6
- package/dist/maplat_tin.js +2078 -1864
- package/dist/maplat_tin.umd.js +3 -3
- package/dist/tin.d.ts +16 -1
- package/dist/vertexutils.d.ts +6 -2
- package/dist/weight-buffer.d.ts +2 -0
- package/package.json +5 -5
- package/src/boundary-vertices.ts +557 -283
- package/src/constrained-tin.ts +84 -84
- package/src/edgebound/base.ts +65 -65
- package/src/edgebound/bitset.ts +109 -109
- package/src/edgebound/constrain.license.txt +15 -15
- package/src/edgebound/constrain.ts +498 -498
- package/src/edgebound/index.ts +10 -10
- package/src/index.ts +42 -42
- package/src/kinks.ts +1092 -1092
- package/src/searchutils.ts +102 -102
- package/src/strict-overlap.ts +461 -281
- package/src/tin.ts +821 -703
- package/src/types/cdt2d.d.ts +4 -4
- package/src/types/constrainautor.d.ts +6 -6
- package/src/types/kinks.d.ts +51 -51
- package/src/types/tin.d.ts +54 -54
- package/src/vertexutils.ts +92 -87
- package/src/weight-buffer.ts +114 -111
package/src/boundary-vertices.ts
CHANGED
|
@@ -1,283 +1,557 @@
|
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
forw:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
]
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
]
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
)
|
|
264
|
-
const
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
*
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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
|
+
/**
|
|
11
|
+
* Unified parameters for all boundary vertex computations (V2 and V3).
|
|
12
|
+
*
|
|
13
|
+
* `allGcps` must include **all** interior points: GCPs from `this.points` **and**
|
|
14
|
+
* edge intermediate nodes from `this.edgeNodes`. This is necessary so that
|
|
15
|
+
* `checkAndAdjustVerticesN` can guarantee that every constrained-edge vertex in
|
|
16
|
+
* bakw space lies inside the boundary polygon.
|
|
17
|
+
*/
|
|
18
|
+
export interface BoundaryVerticesParams {
|
|
19
|
+
convexBuf: Record<string, ConvexEntry>;
|
|
20
|
+
centroid: { forw: Position; bakw: Position };
|
|
21
|
+
/** GCPs + edge intermediate nodes */
|
|
22
|
+
allGcps: Array<{ forw: Position; bakw: Position }>;
|
|
23
|
+
minx: number;
|
|
24
|
+
maxx: number;
|
|
25
|
+
miny: number;
|
|
26
|
+
maxy: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ─── Internal types ──────────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
type SamplePair = {
|
|
32
|
+
forw: [number, number];
|
|
33
|
+
bakw: [number, number];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// ─── Ratio helpers ───────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
function collectSamples(
|
|
39
|
+
convexBuf: Record<string, ConvexEntry>,
|
|
40
|
+
centroid: { forw: Position; bakw: Position },
|
|
41
|
+
): { perQuad: SamplePair[][]; aggregate: SamplePair[] } {
|
|
42
|
+
const perQuad: SamplePair[][] = [[], [], [], []];
|
|
43
|
+
const aggregate: SamplePair[] = [];
|
|
44
|
+
|
|
45
|
+
Object.keys(convexBuf).forEach((key) => {
|
|
46
|
+
const item = convexBuf[key];
|
|
47
|
+
const forw = item.forw;
|
|
48
|
+
const bakw = item.bakw;
|
|
49
|
+
|
|
50
|
+
const forwDelta: [number, number] = [
|
|
51
|
+
forw[0] - centroid.forw[0],
|
|
52
|
+
forw[1] - centroid.forw[1],
|
|
53
|
+
];
|
|
54
|
+
const bakwDelta: [number, number] = [
|
|
55
|
+
bakw[0] - centroid.bakw[0],
|
|
56
|
+
centroid.bakw[1] - bakw[1],
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
const sample: SamplePair = { forw: forwDelta, bakw: bakwDelta };
|
|
60
|
+
aggregate.push(sample);
|
|
61
|
+
|
|
62
|
+
if (forwDelta[0] === 0 || forwDelta[1] === 0) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let quad = 0;
|
|
67
|
+
if (forwDelta[0] > 0) quad += 1;
|
|
68
|
+
if (forwDelta[1] > 0) quad += 2;
|
|
69
|
+
perQuad[quad].push(sample);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return { perQuad, aggregate };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function reduceSamples(samples: SamplePair[]): [number, number] {
|
|
76
|
+
let minRatio = Infinity;
|
|
77
|
+
let sumCos = 0;
|
|
78
|
+
let sumSin = 0;
|
|
79
|
+
|
|
80
|
+
samples.forEach((sample) => {
|
|
81
|
+
const { forw, bakw } = sample;
|
|
82
|
+
const forwNorm = Math.hypot(forw[0], forw[1]);
|
|
83
|
+
const bakwNorm = Math.hypot(bakw[0], bakw[1]);
|
|
84
|
+
if (bakwNorm === 0) return;
|
|
85
|
+
|
|
86
|
+
const ratio = forwNorm / bakwNorm;
|
|
87
|
+
const theta = Math.atan2(forw[0], forw[1]) - Math.atan2(bakw[0], bakw[1]);
|
|
88
|
+
|
|
89
|
+
minRatio = Math.min(minRatio, ratio);
|
|
90
|
+
sumCos += Math.cos(theta);
|
|
91
|
+
sumSin += Math.sin(theta);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
if (!isFinite(minRatio)) {
|
|
95
|
+
return [1, 0];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return [minRatio, Math.atan2(sumSin, sumCos)];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function buildVertexRatio(
|
|
102
|
+
convexBuf: Record<string, ConvexEntry>,
|
|
103
|
+
centroid: { forw: Position; bakw: Position },
|
|
104
|
+
mode: "plain" | "birdeye",
|
|
105
|
+
): Array<[number, number]> {
|
|
106
|
+
const { perQuad, aggregate } = collectSamples(convexBuf, centroid);
|
|
107
|
+
const hasAllQuadrants = perQuad.every((samples) => samples.length > 0);
|
|
108
|
+
|
|
109
|
+
const groups: SamplePair[][] = mode === "birdeye"
|
|
110
|
+
? (hasAllQuadrants ? perQuad : [aggregate])
|
|
111
|
+
: [aggregate];
|
|
112
|
+
|
|
113
|
+
const vertexRatio = groups.map((samples) => reduceSamples(samples));
|
|
114
|
+
|
|
115
|
+
if (vertexRatio.length === 1) {
|
|
116
|
+
return [vertexRatio[0], vertexRatio[0], vertexRatio[0], vertexRatio[0]];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return vertexRatio;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ─── Geometry helpers ────────────────────────────────────────────────────────
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Return quadrant index (0–3) of `forw` relative to `centroidForw`.
|
|
126
|
+
*
|
|
127
|
+
* Quadrant encoding:
|
|
128
|
+
* q0: Δx≤0, Δy≤0 q1: Δx>0, Δy≤0
|
|
129
|
+
* q2: Δx≤0, Δy>0 q3: Δx>0, Δy>0
|
|
130
|
+
*/
|
|
131
|
+
function forwQuadrant(forw: Position, centroidForw: Position): number {
|
|
132
|
+
let q = 0;
|
|
133
|
+
if (forw[0] > centroidForw[0]) q += 1;
|
|
134
|
+
if (forw[1] > centroidForw[1]) q += 2;
|
|
135
|
+
return q;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Compute the bakw position for a given forw position using the centroid and
|
|
140
|
+
* an aggregate [scale, rotation] ratio.
|
|
141
|
+
*/
|
|
142
|
+
function computeVertexBakwFromRatio(
|
|
143
|
+
forwPos: Position,
|
|
144
|
+
centroid: { forw: Position; bakw: Position },
|
|
145
|
+
ratio: [number, number],
|
|
146
|
+
): Position {
|
|
147
|
+
const forDelta = [
|
|
148
|
+
forwPos[0] - centroid.forw[0],
|
|
149
|
+
forwPos[1] - centroid.forw[1],
|
|
150
|
+
];
|
|
151
|
+
const forDistance = Math.sqrt(forDelta[0] ** 2 + forDelta[1] ** 2);
|
|
152
|
+
const bakDistance = forDistance / ratio[0];
|
|
153
|
+
const bakTheta = Math.atan2(forDelta[0], forDelta[1]) - ratio[1];
|
|
154
|
+
return [
|
|
155
|
+
centroid.bakw[0] + bakDistance * Math.sin(bakTheta),
|
|
156
|
+
centroid.bakw[1] - bakDistance * Math.cos(bakTheta),
|
|
157
|
+
];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Find the intersection of the ray from `centroid` through `rayDir` with the
|
|
162
|
+
* segment [segStart, segEnd]. Returns {t, point} where t > 0 is the parameter
|
|
163
|
+
* along the ray, or null if no valid intersection.
|
|
164
|
+
*/
|
|
165
|
+
function findRaySegmentIntersection(
|
|
166
|
+
centroid: Position,
|
|
167
|
+
rayDir: Position,
|
|
168
|
+
segStart: Position,
|
|
169
|
+
segEnd: Position,
|
|
170
|
+
): { t: number; point: Position } | null {
|
|
171
|
+
const dx = rayDir[0] - centroid[0];
|
|
172
|
+
const dy = rayDir[1] - centroid[1];
|
|
173
|
+
if (Math.abs(dx) < 1e-12 && Math.abs(dy) < 1e-12) return null;
|
|
174
|
+
|
|
175
|
+
const ex = segEnd[0] - segStart[0];
|
|
176
|
+
const ey = segEnd[1] - segStart[1];
|
|
177
|
+
const fx = segStart[0] - centroid[0];
|
|
178
|
+
const fy = segStart[1] - centroid[1];
|
|
179
|
+
|
|
180
|
+
const denom = dx * ey - dy * ex;
|
|
181
|
+
if (Math.abs(denom) < 1e-12) return null;
|
|
182
|
+
|
|
183
|
+
const t = (fx * ey - fy * ex) / denom;
|
|
184
|
+
const s = (fx * dy - fy * dx) / denom;
|
|
185
|
+
|
|
186
|
+
if (t <= 1e-10) return null;
|
|
187
|
+
if (s < -1e-10 || s > 1 + 1e-10) return null;
|
|
188
|
+
|
|
189
|
+
return { t, point: [centroid[0] + t * dx, centroid[1] + t * dy] };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Find the exit intersection of the ray from `centroid` through `rayDir` with
|
|
194
|
+
* the convex polygon formed by `corners` (angularly-sorted VertexPosition[]).
|
|
195
|
+
*/
|
|
196
|
+
function findRayQuadIntersection(
|
|
197
|
+
centroid: Position,
|
|
198
|
+
rayDir: Position,
|
|
199
|
+
corners: VertexPosition[],
|
|
200
|
+
): Position | null {
|
|
201
|
+
const N = corners.length;
|
|
202
|
+
let bestT = -Infinity;
|
|
203
|
+
let bestPoint: Position | null = null;
|
|
204
|
+
|
|
205
|
+
for (let i = 0; i < N; i++) {
|
|
206
|
+
const j = (i + 1) % N;
|
|
207
|
+
const result = findRaySegmentIntersection(
|
|
208
|
+
centroid, rayDir, corners[i].bakw, corners[j].bakw,
|
|
209
|
+
);
|
|
210
|
+
if (result && result.t > bestT) {
|
|
211
|
+
bestT = result.t;
|
|
212
|
+
bestPoint = result.point;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return bestPoint;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Convert a forw position to [0, 360) degrees using the atan2(Δx, Δy) convention. */
|
|
219
|
+
function toAngleDeg360(pos: Position, centroid: Position): number {
|
|
220
|
+
const rawRad = Math.atan2(pos[0] - centroid[0], pos[1] - centroid[1]);
|
|
221
|
+
const deg = rawRad * (180 / Math.PI);
|
|
222
|
+
return deg < 0 ? deg + 360 : deg;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Find the first intersection of the ray from `centroid` THROUGH `vertex` with
|
|
227
|
+
* the axis-aligned bbox [minx,maxx] × [miny,maxy].
|
|
228
|
+
*/
|
|
229
|
+
function findRayBboxIntersection(
|
|
230
|
+
centroid: Position,
|
|
231
|
+
vertex: Position,
|
|
232
|
+
minx: number,
|
|
233
|
+
maxx: number,
|
|
234
|
+
miny: number,
|
|
235
|
+
maxy: number,
|
|
236
|
+
): Position | null {
|
|
237
|
+
const dx = vertex[0] - centroid[0];
|
|
238
|
+
const dy = vertex[1] - centroid[1];
|
|
239
|
+
if (dx === 0 && dy === 0) return null;
|
|
240
|
+
|
|
241
|
+
const candidates: { t: number; x: number; y: number }[] = [];
|
|
242
|
+
|
|
243
|
+
if (dx !== 0) {
|
|
244
|
+
for (const bx of [minx, maxx]) {
|
|
245
|
+
const t = (bx - centroid[0]) / dx;
|
|
246
|
+
if (t > 0) {
|
|
247
|
+
const y = centroid[1] + t * dy;
|
|
248
|
+
if (y >= miny && y <= maxy) candidates.push({ t, x: bx, y });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (dy !== 0) {
|
|
253
|
+
for (const by of [miny, maxy]) {
|
|
254
|
+
const t = (by - centroid[1]) / dy;
|
|
255
|
+
if (t > 0) {
|
|
256
|
+
const x = centroid[0] + t * dx;
|
|
257
|
+
if (x >= minx && x <= maxx) candidates.push({ t, x, y: by });
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (candidates.length === 0) return null;
|
|
263
|
+
candidates.sort((a, b) => a.t - b.t);
|
|
264
|
+
const best = candidates[0];
|
|
265
|
+
return [best.x, best.y];
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Expand N boundary vertices radially from `centroid.bakw` so that every point
|
|
270
|
+
* in `allPoints` has its bakw position enclosed inside the polygon formed by
|
|
271
|
+
* `vertices`.
|
|
272
|
+
*
|
|
273
|
+
* For each point, tests all N bakw polygon sides to find where the ray
|
|
274
|
+
* [centroid.bakw → point.bakw] exits. If the point is outside that side, both
|
|
275
|
+
* endpoints of the side are scaled outward until the point is enclosed.
|
|
276
|
+
*/
|
|
277
|
+
function checkAndAdjustVerticesN(
|
|
278
|
+
vertices: VertexPosition[],
|
|
279
|
+
allPoints: Array<{ forw: Position; bakw: Position }>,
|
|
280
|
+
centroid: { forw: Position; bakw: Position },
|
|
281
|
+
): void {
|
|
282
|
+
const N = vertices.length;
|
|
283
|
+
const expandRatio = new Array<number>(N).fill(1);
|
|
284
|
+
|
|
285
|
+
for (const node of allPoints) {
|
|
286
|
+
for (let i = 0; i < N; i++) {
|
|
287
|
+
const j = (i + 1) % N;
|
|
288
|
+
const side = lineString([vertices[i].bakw, vertices[j].bakw]);
|
|
289
|
+
const line = lineString([centroid.bakw, node.bakw]);
|
|
290
|
+
const intersect = lineIntersect(side, line);
|
|
291
|
+
|
|
292
|
+
if (intersect.features.length > 0 && intersect.features[0].geometry) {
|
|
293
|
+
const intersectPt = intersect.features[0];
|
|
294
|
+
const distance = Math.sqrt(
|
|
295
|
+
Math.pow(node.bakw[0] - centroid.bakw[0], 2) +
|
|
296
|
+
Math.pow(node.bakw[1] - centroid.bakw[1], 2),
|
|
297
|
+
);
|
|
298
|
+
const intDistance = Math.sqrt(
|
|
299
|
+
Math.pow(intersectPt.geometry.coordinates[0] - centroid.bakw[0], 2) +
|
|
300
|
+
Math.pow(intersectPt.geometry.coordinates[1] - centroid.bakw[1], 2),
|
|
301
|
+
);
|
|
302
|
+
const ratio = distance / intDistance;
|
|
303
|
+
if (ratio > expandRatio[i]) expandRatio[i] = ratio;
|
|
304
|
+
if (ratio > expandRatio[j]) expandRatio[j] = ratio;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
vertices.forEach((vertex, i) => {
|
|
310
|
+
const ratio = expandRatio[i];
|
|
311
|
+
vertex.bakw = [
|
|
312
|
+
(vertex.bakw[0] - centroid.bakw[0]) * ratio + centroid.bakw[0],
|
|
313
|
+
(vertex.bakw[1] - centroid.bakw[1]) * ratio + centroid.bakw[1],
|
|
314
|
+
];
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// ─── Unified core ────────────────────────────────────────────────────────────
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Unified boundary vertex computation shared by V2 (4 corners) and V3 (4 corners
|
|
322
|
+
* + up to 32 edge vertices).
|
|
323
|
+
*
|
|
324
|
+
* ## Algorithm
|
|
325
|
+
*
|
|
326
|
+
* ### Phase 1 – Ratios
|
|
327
|
+
* Compute per-quadrant (birdeye) or aggregate (plain) [minScale, avgRotation]
|
|
328
|
+
* ratios from the convex hull of all GCPs.
|
|
329
|
+
*
|
|
330
|
+
* ### Phase 2 – 4 bbox corners
|
|
331
|
+
* Each bbox corner's bakw position is extrapolated using the ratio for its
|
|
332
|
+
* quadrant. `checkAndAdjustVerticesN` then expands corners radially so that
|
|
333
|
+
* **every point in `allGcps`** (GCPs + edge intermediate nodes) is enclosed.
|
|
334
|
+
*
|
|
335
|
+
* If `withEdgeVertices` is false the function returns here (V2 path).
|
|
336
|
+
*
|
|
337
|
+
* ### Phase 3 – 36 × 10° bins (V3 only)
|
|
338
|
+
* For each non-corner 10° bin the most-extreme interior point (GCP or edge
|
|
339
|
+
* node) in that angular range is used as a guide:
|
|
340
|
+
*
|
|
341
|
+
* - **bakw direction**: linearly interpolated rotation between the two
|
|
342
|
+
* bracketing corner bakw rotations → smooth, no discontinuity.
|
|
343
|
+
* - **bakw distance**: the guide point's actual bakw distance from centroid,
|
|
344
|
+
* scaled up by `forwEdge_dist / forwGuide_dist` (extrapolation to the bbox
|
|
345
|
+
* boundary). This gives non-collinear positions independent of the
|
|
346
|
+
* corner-quad sides and avoids degenerate (zero-area) triangles.
|
|
347
|
+
*
|
|
348
|
+
* Bins with no interior points fall back to the interpolated corner scale.
|
|
349
|
+
*
|
|
350
|
+
* ### Phase 4 – Sort + final adjust
|
|
351
|
+
* All vertices are sorted by forw angle. `checkAndAdjustVerticesN` is called
|
|
352
|
+
* again on the full set so that the final polygon correctly encloses every
|
|
353
|
+
* interior point.
|
|
354
|
+
*/
|
|
355
|
+
function calculateVerticesCore(
|
|
356
|
+
params: BoundaryVerticesParams,
|
|
357
|
+
mode: "plain" | "birdeye",
|
|
358
|
+
withEdgeVertices: boolean,
|
|
359
|
+
): VertexPosition[] {
|
|
360
|
+
const { convexBuf, centroid, allGcps, minx, maxx, miny, maxy } = params;
|
|
361
|
+
|
|
362
|
+
// ── Phase 1 ───────────────────────────────────────────────────────────────
|
|
363
|
+
const cornerRatios = buildVertexRatio(convexBuf, centroid, mode);
|
|
364
|
+
|
|
365
|
+
// ── Phase 2: 4 bbox corner vertices ───────────────────────────────────────
|
|
366
|
+
const bboxCornerForws: Position[] = [
|
|
367
|
+
[minx, miny],
|
|
368
|
+
[maxx, miny],
|
|
369
|
+
[maxx, maxy],
|
|
370
|
+
[minx, maxy],
|
|
371
|
+
];
|
|
372
|
+
const cornerVertices: VertexPosition[] = bboxCornerForws.map((forw) => ({
|
|
373
|
+
forw,
|
|
374
|
+
bakw: computeVertexBakwFromRatio(
|
|
375
|
+
forw,
|
|
376
|
+
centroid,
|
|
377
|
+
cornerRatios[forwQuadrant(forw, centroid.forw)],
|
|
378
|
+
),
|
|
379
|
+
}));
|
|
380
|
+
|
|
381
|
+
// Sort corners CCW by forw angle (atan2(Δx, Δy) convention)
|
|
382
|
+
cornerVertices.sort((a, b) =>
|
|
383
|
+
Math.atan2(a.forw[0] - centroid.forw[0], a.forw[1] - centroid.forw[1]) -
|
|
384
|
+
Math.atan2(b.forw[0] - centroid.forw[0], b.forw[1] - centroid.forw[1])
|
|
385
|
+
);
|
|
386
|
+
|
|
387
|
+
// Expand corners to enclose all GCPs + edge nodes in bakw space
|
|
388
|
+
checkAndAdjustVerticesN(cornerVertices, allGcps, centroid);
|
|
389
|
+
|
|
390
|
+
if (!withEdgeVertices) return cornerVertices;
|
|
391
|
+
|
|
392
|
+
// ── Phase 3 prep: interpolation data from adjusted corners ────────────────
|
|
393
|
+
//
|
|
394
|
+
// For each edge vertex we interpolate the forw→bakw rotation from the two
|
|
395
|
+
// bracketing corner bakw rotations. At a corner's exact forw direction the
|
|
396
|
+
// interpolated rotation equals that corner's actual bakw rotation, so the
|
|
397
|
+
// boundary is continuous everywhere.
|
|
398
|
+
//
|
|
399
|
+
// The bakw distance is determined by projecting the interpolated direction
|
|
400
|
+
// onto the **corner polygon** sides. This places every Phase 3 edge vertex
|
|
401
|
+
// exactly on the adjusted corner-polygon boundary, so the 36-vertex bakw
|
|
402
|
+
// polygon is a refinement of (not larger than) the 4-corner polygon.
|
|
403
|
+
const N = 4; // always 4 corners
|
|
404
|
+
const cornerForwAngles = cornerVertices.map((cv) =>
|
|
405
|
+
Math.atan2(cv.forw[0] - centroid.forw[0], cv.forw[1] - centroid.forw[1])
|
|
406
|
+
);
|
|
407
|
+
const cornerBakwThetas = cornerVertices.map((cv) =>
|
|
408
|
+
Math.atan2(
|
|
409
|
+
cv.bakw[0] - centroid.bakw[0],
|
|
410
|
+
-(cv.bakw[1] - centroid.bakw[1]),
|
|
411
|
+
)
|
|
412
|
+
);
|
|
413
|
+
/** Find the angular sector [corners[i], corners[j]) containing theta. */
|
|
414
|
+
function findSector(theta: number): { i: number; j: number; frac: number } {
|
|
415
|
+
for (let i = 0; i < N; i++) {
|
|
416
|
+
const j = (i + 1) % N;
|
|
417
|
+
const ai = cornerForwAngles[i];
|
|
418
|
+
const aj = i < N - 1 ? cornerForwAngles[j] : cornerForwAngles[j] + 2 * Math.PI;
|
|
419
|
+
let t = theta;
|
|
420
|
+
while (t < ai) t += 2 * Math.PI;
|
|
421
|
+
while (t >= ai + 2 * Math.PI) t -= 2 * Math.PI;
|
|
422
|
+
if (t >= ai && t < aj) {
|
|
423
|
+
return { i, j, frac: (t - ai) / (aj - ai) };
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return { i: 0, j: 1, frac: 0 };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Interpolate the bakw angle directly between adjacent corner bakw angles.
|
|
431
|
+
* This avoids the large rotation-delta problem that occurs when the forw→bakw
|
|
432
|
+
* rotation difference between adjacent corners exceeds 180°.
|
|
433
|
+
*/
|
|
434
|
+
function interpolateBakwAngle(theta: number): number {
|
|
435
|
+
const { i, j, frac } = findSector(theta);
|
|
436
|
+
const bi = cornerBakwThetas[i];
|
|
437
|
+
const bj = cornerBakwThetas[j];
|
|
438
|
+
let delta = bj - bi;
|
|
439
|
+
while (delta > Math.PI) delta -= 2 * Math.PI;
|
|
440
|
+
while (delta < -Math.PI) delta += 2 * Math.PI;
|
|
441
|
+
return bi + frac * delta;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// ── Phase 3: 36-bin edge vertices ─────────────────────────────────────────
|
|
445
|
+
const cornerBins = new Set<number>(
|
|
446
|
+
cornerVertices.map(
|
|
447
|
+
(cv) => Math.floor(toAngleDeg360(cv.forw, centroid.forw) / 10) % 36,
|
|
448
|
+
),
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
type GcpInfo = {
|
|
452
|
+
forw: Position;
|
|
453
|
+
bakw: Position;
|
|
454
|
+
angleDeg: number;
|
|
455
|
+
forwDist: number;
|
|
456
|
+
};
|
|
457
|
+
const gcpInfos: GcpInfo[] = allGcps.map((gcp) => ({
|
|
458
|
+
forw: gcp.forw,
|
|
459
|
+
bakw: gcp.bakw,
|
|
460
|
+
angleDeg: toAngleDeg360(gcp.forw, centroid.forw),
|
|
461
|
+
forwDist: Math.hypot(gcp.forw[0] - centroid.forw[0], gcp.forw[1] - centroid.forw[1]),
|
|
462
|
+
}));
|
|
463
|
+
|
|
464
|
+
const edgeVertices: VertexPosition[] = [];
|
|
465
|
+
|
|
466
|
+
for (let bin = 0; bin < 36; bin++) {
|
|
467
|
+
if (cornerBins.has(bin)) continue;
|
|
468
|
+
|
|
469
|
+
const binStart = bin * 10;
|
|
470
|
+
const inBin = gcpInfos.filter(
|
|
471
|
+
(g) => g.angleDeg >= binStart && g.angleDeg < binStart + 10,
|
|
472
|
+
);
|
|
473
|
+
|
|
474
|
+
let forwEdge: Position | null = null;
|
|
475
|
+
|
|
476
|
+
if (inBin.length > 0) {
|
|
477
|
+
// Most extreme interior point in this angular bin
|
|
478
|
+
const sel = inBin.reduce((best, g) => g.forwDist > best.forwDist ? g : best);
|
|
479
|
+
forwEdge = findRayBboxIntersection(centroid.forw, sel.forw, minx, maxx, miny, maxy);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
if (!forwEdge) {
|
|
483
|
+
const binCenterRad = ((binStart + 5) % 360) * (Math.PI / 180);
|
|
484
|
+
const fallbackDir: Position = [
|
|
485
|
+
centroid.forw[0] + Math.sin(binCenterRad),
|
|
486
|
+
centroid.forw[1] + Math.cos(binCenterRad),
|
|
487
|
+
];
|
|
488
|
+
forwEdge = findRayBboxIntersection(centroid.forw, fallbackDir, minx, maxx, miny, maxy);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if (!forwEdge) continue;
|
|
492
|
+
|
|
493
|
+
const forDelta = [forwEdge[0] - centroid.forw[0], forwEdge[1] - centroid.forw[1]];
|
|
494
|
+
const forwAngle = Math.atan2(forDelta[0], forDelta[1]);
|
|
495
|
+
const bakwTheta = interpolateBakwAngle(forwAngle);
|
|
496
|
+
const bakwDirPt: Position = [
|
|
497
|
+
centroid.bakw[0] + Math.sin(bakwTheta),
|
|
498
|
+
centroid.bakw[1] - Math.cos(bakwTheta),
|
|
499
|
+
];
|
|
500
|
+
const bakwEdge = findRayQuadIntersection(centroid.bakw, bakwDirPt, cornerVertices);
|
|
501
|
+
|
|
502
|
+
if (bakwEdge) {
|
|
503
|
+
edgeVertices.push({ forw: forwEdge, bakw: bakwEdge });
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// ── Phase 4: Combine, sort, final adjust ──────────────────────────────────
|
|
508
|
+
const allVertices: VertexPosition[] = [...cornerVertices, ...edgeVertices];
|
|
509
|
+
allVertices.sort((a, b) =>
|
|
510
|
+
Math.atan2(a.forw[0] - centroid.forw[0], a.forw[1] - centroid.forw[1]) -
|
|
511
|
+
Math.atan2(b.forw[0] - centroid.forw[0], b.forw[1] - centroid.forw[1])
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
// Final expansion: ensure the full polygon encloses all GCPs + edge nodes
|
|
515
|
+
// (some edge vertices may have been placed conservatively via extrapolation;
|
|
516
|
+
// this step corrects any remaining gaps).
|
|
517
|
+
checkAndAdjustVerticesN(allVertices, allGcps, centroid);
|
|
518
|
+
|
|
519
|
+
return allVertices;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// ─── Public API ──────────────────────────────────────────────────────────────
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Calculate boundary vertices in plain mode.
|
|
526
|
+
*
|
|
527
|
+
* @param params - Input parameters including GCPs, centroid, and bounding box.
|
|
528
|
+
* @param v3 - When true (V3 format), runs the full 36-bin edge vertex pass
|
|
529
|
+
* (Phase 3) in addition to the 4 bbox corners, producing up to 36 vertices.
|
|
530
|
+
* When false (V2 format), returns only the 4 bbox corners.
|
|
531
|
+
*
|
|
532
|
+
* Plain mode uses a single aggregate [scale, rotation] ratio from all GCPs.
|
|
533
|
+
*/
|
|
534
|
+
export function calculatePlainVertices(
|
|
535
|
+
params: BoundaryVerticesParams,
|
|
536
|
+
v3 = false,
|
|
537
|
+
): VertexPosition[] {
|
|
538
|
+
return calculateVerticesCore(params, "plain", v3);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Calculate boundary vertices in bird's-eye mode.
|
|
543
|
+
*
|
|
544
|
+
* @param params - Input parameters including GCPs, centroid, and bounding box.
|
|
545
|
+
* @param v3 - When true (V3 format), runs the full 36-bin edge vertex pass
|
|
546
|
+
* (Phase 3) in addition to the 4 bbox corners, producing up to 36 vertices.
|
|
547
|
+
* When false (V2 format), returns only the 4 bbox corners.
|
|
548
|
+
*
|
|
549
|
+
* Birdeye mode uses per-quadrant [scale, rotation] ratios to capture
|
|
550
|
+
* perspective distortion in the 4 corner positions.
|
|
551
|
+
*/
|
|
552
|
+
export function calculateBirdeyeVertices(
|
|
553
|
+
params: BoundaryVerticesParams,
|
|
554
|
+
v3 = false,
|
|
555
|
+
): VertexPosition[] {
|
|
556
|
+
return calculateVerticesCore(params, "birdeye", v3);
|
|
557
|
+
}
|