@vizhub/quadstream 0.2.0 → 1.0.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/CHANGELOG.md +8 -0
- package/README.md +49 -323
- package/bin/quadstream.mjs +8 -6
- package/dist/blg.d.ts +3 -0
- package/dist/blg.js +14 -1
- package/dist/cooperative.d.ts +21 -0
- package/dist/cooperative.js +33 -0
- package/dist/dataset.d.ts +8 -4
- package/dist/dataset.js +51 -14
- package/dist/geometry-wire.d.ts +8 -0
- package/dist/geometry-wire.js +76 -0
- package/dist/geometry.d.ts +3 -1
- package/dist/geometry.js +115 -5
- package/dist/index.d.ts +20 -2
- package/dist/index.js +9 -1
- package/dist/loader.d.ts +1 -1
- package/dist/loader.js +1 -1
- package/dist/lod-scheduler.d.ts +15 -0
- package/dist/lod-scheduler.js +85 -0
- package/dist/progressive-map.d.ts +1 -1
- package/dist/progressive-map.js +12 -8
- package/dist/quadtree.d.ts +2 -0
- package/dist/quadtree.js +12 -0
- package/dist/reconstruct.d.ts +18 -1
- package/dist/reconstruct.js +94 -32
- package/dist/session.d.ts +12 -9
- package/dist/session.js +24 -7
- package/dist/spherical-clip.d.ts +95 -0
- package/dist/spherical-clip.js +557 -0
- package/dist/spherical-cull.d.ts +25 -0
- package/dist/spherical-cull.js +102 -0
- package/dist/spherical-dataset.d.ts +41 -9
- package/dist/spherical-dataset.js +221 -122
- package/dist/spherical-geometry.d.ts +2 -0
- package/dist/spherical-geometry.js +87 -44
- package/dist/spherical-line.d.ts +29 -0
- package/dist/spherical-line.js +221 -0
- package/dist/spherical-preprocess.d.ts +23 -0
- package/dist/spherical-preprocess.js +160 -0
- package/dist/spherical-repair.d.ts +17 -0
- package/dist/spherical-repair.js +100 -0
- package/dist/spherical-robust-dataset.d.ts +27 -0
- package/dist/spherical-robust-dataset.js +89 -0
- package/dist/spherical-view-clipper.d.ts +22 -0
- package/dist/spherical-view-clipper.js +85 -0
- package/dist/topology.d.ts +3 -0
- package/dist/topology.js +40 -9
- package/dist/types.d.ts +5 -1
- package/package.json +12 -7
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { runSync } from "./cooperative.js";
|
|
1
2
|
import { simpleClosedRing, validPolygon, signedArea } from "./geometry.js";
|
|
2
3
|
const rad = Math.PI / 180;
|
|
3
4
|
const eps = 1e-10;
|
|
@@ -28,8 +29,8 @@ export function canonicalLonLat(value) {
|
|
|
28
29
|
lat === 0 ? 0 : lat,
|
|
29
30
|
];
|
|
30
31
|
}
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
/** Vector of an already canonical (validated, seam-aliased) longitude/latitude. */
|
|
33
|
+
function canonicalVector(lon, lat) {
|
|
33
34
|
if (Math.abs(lat) === 90)
|
|
34
35
|
return [0, 0, Math.sign(lat)];
|
|
35
36
|
const c = Math.cos(lat * rad), z = Math.sin(lat * rad);
|
|
@@ -48,77 +49,115 @@ export function lonLatVector(p) {
|
|
|
48
49
|
return [c * Math.cos(lon * rad), c * Math.sin(lon * rad), z];
|
|
49
50
|
}
|
|
50
51
|
}
|
|
52
|
+
export function lonLatVector(p) {
|
|
53
|
+
const [lon, lat] = canonicalLonLat(p);
|
|
54
|
+
return canonicalVector(lon, lat);
|
|
55
|
+
}
|
|
51
56
|
export function angularDistance(a, b) {
|
|
52
57
|
const u = lonLatVector(a), v = lonLatVector(b);
|
|
53
58
|
return Math.atan2(norm(cross(u, v)), dot(u, v));
|
|
54
59
|
}
|
|
55
|
-
|
|
56
|
-
if (
|
|
60
|
+
function validVectorPath(vectors) {
|
|
61
|
+
if (vectors.length < 2)
|
|
57
62
|
return false;
|
|
58
|
-
for (let i = 1; i <
|
|
59
|
-
const
|
|
63
|
+
for (let i = 1; i < vectors.length; i++) {
|
|
64
|
+
const u = vectors[i - 1], v = vectors[i], d = Math.atan2(norm(cross(u, v)), dot(u, v));
|
|
60
65
|
if (d <= eps || Math.PI - d <= eps)
|
|
61
66
|
return false;
|
|
62
67
|
}
|
|
63
68
|
return true;
|
|
64
69
|
}
|
|
65
|
-
export function
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
export function validGreatCirclePath(path) {
|
|
71
|
+
if (path.length < 2)
|
|
72
|
+
return false;
|
|
73
|
+
return validVectorPath(path.map(lonLatVector));
|
|
74
|
+
}
|
|
75
|
+
const FACES = ["+X", "-X", "+Y", "-Y", "+Z", "-Z"];
|
|
76
|
+
function faceCoords([x, y, z]) {
|
|
69
77
|
// Fixed axis order breaks exact and roundoff-sized seam ties.
|
|
70
78
|
const m = Math.max(Math.abs(x), Math.abs(y), Math.abs(z));
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
u
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
79
|
+
if (m - Math.abs(x) <= 1e-15)
|
|
80
|
+
return { face: x >= 0 ? 0 : 1, u: y / Math.abs(x), v: z / Math.abs(x) };
|
|
81
|
+
if (m - Math.abs(y) <= 1e-15)
|
|
82
|
+
return { face: y >= 0 ? 2 : 3, u: x / Math.abs(y), v: z / Math.abs(y) };
|
|
83
|
+
return { face: z >= 0 ? 4 : 5, u: x / Math.abs(z), v: y / Math.abs(z) };
|
|
84
|
+
}
|
|
85
|
+
function cellIndex(t, side) {
|
|
86
|
+
return Math.max(0, Math.min(side - 1, Math.floor(((t + 1) * side) / 2)));
|
|
87
|
+
}
|
|
88
|
+
function assertLevel(level) {
|
|
89
|
+
if (!Number.isInteger(level) || level < 0 || level > 25)
|
|
90
|
+
throw new Error("Spherical level must be 0..25");
|
|
91
|
+
}
|
|
92
|
+
export function sphericalCellAddress(p, level) {
|
|
93
|
+
assertLevel(level);
|
|
94
|
+
const { face, u, v } = faceCoords(lonLatVector(p)), side = 2 ** level, i = cellIndex(u, side), j = cellIndex(v, side), name = FACES[face];
|
|
95
|
+
return {
|
|
96
|
+
face: name,
|
|
97
|
+
level,
|
|
98
|
+
i,
|
|
99
|
+
j,
|
|
100
|
+
key: name + ":" + level + ":" + i + ":" + j,
|
|
101
|
+
};
|
|
90
102
|
}
|
|
103
|
+
// Face and indices pack into one exact double: 6 * 2^50 < 2^53.
|
|
104
|
+
const SIDE_MAX = 2 ** 25;
|
|
105
|
+
const packCell = (face, i, j) => (face * SIDE_MAX + i) * SIDE_MAX + j;
|
|
91
106
|
export function buildSphericalPriorities(vertices, maxLevel = 20) {
|
|
107
|
+
return runSync(buildSphericalPrioritiesSteps(vertices, maxLevel));
|
|
108
|
+
}
|
|
109
|
+
export function* buildSphericalPrioritiesSteps(vertices, maxLevel = 20) {
|
|
92
110
|
if (!Number.isInteger(maxLevel) || maxLevel < 1 || maxLevel > 26)
|
|
93
111
|
throw new Error("maxLevel must be 1..26");
|
|
94
|
-
const seen = new Set(), ids = new
|
|
112
|
+
const seen = new Set(), ids = new Uint8Array(vertices.length);
|
|
95
113
|
const sorted = vertices
|
|
96
114
|
.map((v) => {
|
|
97
115
|
const [x, y] = canonicalLonLat([v.x, v.y]);
|
|
98
|
-
const k =
|
|
116
|
+
const k = x + "," + y;
|
|
99
117
|
if (seen.has(k) ||
|
|
100
118
|
!Number.isSafeInteger(v.id) ||
|
|
101
119
|
v.id < 0 ||
|
|
102
120
|
v.id >= vertices.length ||
|
|
103
|
-
ids
|
|
121
|
+
ids[v.id])
|
|
104
122
|
throw new Error("Expected distinct canonical vertices and dense IDs");
|
|
105
123
|
seen.add(k);
|
|
106
|
-
ids
|
|
107
|
-
return {
|
|
124
|
+
ids[v.id] = 1;
|
|
125
|
+
return {
|
|
126
|
+
id: v.id,
|
|
127
|
+
critical: v.critical,
|
|
128
|
+
x,
|
|
129
|
+
y,
|
|
130
|
+
face: faceCoords(canonicalVector(x, y)),
|
|
131
|
+
};
|
|
108
132
|
})
|
|
109
133
|
.sort((a, b) => Number(b.critical) - Number(a.critical) ||
|
|
110
134
|
a.x - b.x ||
|
|
111
135
|
a.y - b.y ||
|
|
112
136
|
a.id - b.id);
|
|
113
|
-
const occupied = new Set(), cells = [], levelByVertexId = [];
|
|
137
|
+
const occupied = Array.from({ length: maxLevel }, () => new Set()), sides = Array.from({ length: maxLevel }, (_, level) => 2 ** level), cells = [], levelByVertexId = [];
|
|
138
|
+
let processed = 0;
|
|
114
139
|
for (const v of sorted) {
|
|
140
|
+
if (processed++ % 512 === 0)
|
|
141
|
+
yield {
|
|
142
|
+
stage: "Spherical priorities",
|
|
143
|
+
completed: processed - 1,
|
|
144
|
+
total: sorted.length,
|
|
145
|
+
};
|
|
115
146
|
let found = false;
|
|
116
147
|
for (let level = 0; level < maxLevel; level++) {
|
|
117
|
-
const
|
|
118
|
-
if (occupied.has(
|
|
148
|
+
const side = sides[level], i = cellIndex(v.face.u, side), j = cellIndex(v.face.v, side), packed = packCell(v.face.face, i, j);
|
|
149
|
+
if (occupied[level].has(packed))
|
|
119
150
|
continue;
|
|
120
|
-
occupied.add(
|
|
121
|
-
|
|
151
|
+
occupied[level].add(packed);
|
|
152
|
+
const face = FACES[v.face.face];
|
|
153
|
+
cells.push({
|
|
154
|
+
vertexId: v.id,
|
|
155
|
+
face,
|
|
156
|
+
level,
|
|
157
|
+
i,
|
|
158
|
+
j,
|
|
159
|
+
key: face + ":" + level + ":" + i + ":" + j,
|
|
160
|
+
});
|
|
122
161
|
levelByVertexId[v.id] = level;
|
|
123
162
|
found = true;
|
|
124
163
|
break;
|
|
@@ -146,18 +185,22 @@ export function sphericalChart(ring) {
|
|
|
146
185
|
return { center, east, north };
|
|
147
186
|
}
|
|
148
187
|
export function projectSpherical(p, chart) {
|
|
149
|
-
|
|
188
|
+
return projectVector(lonLatVector(p), chart);
|
|
189
|
+
}
|
|
190
|
+
function projectVector(v, chart) {
|
|
191
|
+
const d = dot(v, chart.center);
|
|
150
192
|
if (d <= eps)
|
|
151
193
|
throw new Error("Position outside supported open-hemisphere chart");
|
|
152
194
|
return [dot(v, chart.east) / d, dot(v, chart.north) / d];
|
|
153
195
|
}
|
|
154
196
|
function ringOnChart(ring, chart) {
|
|
155
|
-
const p = ring.map(canonicalLonLat);
|
|
197
|
+
const p = ring.map(canonicalLonLat), vectors = p.map(([lon, lat]) => canonicalVector(lon, lat));
|
|
156
198
|
if (p.length < 4 ||
|
|
157
|
-
|
|
158
|
-
|
|
199
|
+
p[0][0] !== p[p.length - 1][0] ||
|
|
200
|
+
p[0][1] !== p[p.length - 1][1] ||
|
|
201
|
+
!validVectorPath(vectors))
|
|
159
202
|
throw new Error("Invalid spherical ring");
|
|
160
|
-
const xy =
|
|
203
|
+
const xy = vectors.map((v) => projectVector(v, chart));
|
|
161
204
|
if (!simpleClosedRing(xy) || Math.abs(signedArea(xy)) <= eps * eps)
|
|
162
205
|
throw new Error("Degenerate or self-intersecting spherical ring");
|
|
163
206
|
// Reject overlapping adjacent great-circle edges (including numerical near-overlap).
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Geometry, Position } from "./types.js";
|
|
2
|
+
import type { UnitVector } from "./spherical-geometry.js";
|
|
3
|
+
import type { SphericalViewCone } from "./spherical-cull.js";
|
|
4
|
+
type LineGeometry = Extract<Geometry, {
|
|
5
|
+
type: "LineString" | "MultiLineString";
|
|
6
|
+
}>;
|
|
7
|
+
export declare function edgeIntersectsCone(a: UnitVector, b: UnitVector, cone: SphericalViewCone): boolean;
|
|
8
|
+
export interface SphericalLineStats {
|
|
9
|
+
sourceVertices: number;
|
|
10
|
+
submittedVertices: number;
|
|
11
|
+
sourceSegments: number;
|
|
12
|
+
rejectedSegments: number;
|
|
13
|
+
clippedSegments: number;
|
|
14
|
+
visitedNodes: number;
|
|
15
|
+
rejectedLines: number;
|
|
16
|
+
cacheHit: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface SphericalLineResult {
|
|
19
|
+
geometry: {
|
|
20
|
+
type: "MultiLineString";
|
|
21
|
+
coordinates: Position[][];
|
|
22
|
+
};
|
|
23
|
+
stats: SphericalLineStats;
|
|
24
|
+
}
|
|
25
|
+
export declare function createSphericalLineClipper({ preserveSegments }?: {
|
|
26
|
+
preserveSegments?: boolean | undefined;
|
|
27
|
+
}): (geometry: LineGeometry, cone: SphericalViewCone) => SphericalLineResult;
|
|
28
|
+
export declare function clipLineToCone(coordinates: Position[], cone: SphericalViewCone): Position[][];
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { isCapVisible, lonLatToVec } from "./spherical-cull.js";
|
|
2
|
+
const EPS = 1e-12;
|
|
3
|
+
const TAU = 2 * Math.PI;
|
|
4
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
5
|
+
const clamp = (x) => Math.max(-1, Math.min(1, x));
|
|
6
|
+
const cross = (a, b) => [
|
|
7
|
+
a[1] * b[2] - a[2] * b[1],
|
|
8
|
+
a[2] * b[0] - a[0] * b[2],
|
|
9
|
+
a[0] * b[1] - a[1] * b[0],
|
|
10
|
+
];
|
|
11
|
+
const angle = (a, b) => Math.atan2(Math.hypot(...cross(a, b)), dot(a, b));
|
|
12
|
+
const unit = (a) => {
|
|
13
|
+
const length = Math.hypot(...a);
|
|
14
|
+
return [a[0] / length, a[1] / length, a[2] / length];
|
|
15
|
+
};
|
|
16
|
+
const center = (cap) => [cap.cx, cap.cy, cap.cz];
|
|
17
|
+
const capAt = (v, radiusRad) => ({
|
|
18
|
+
cx: v[0],
|
|
19
|
+
cy: v[1],
|
|
20
|
+
cz: v[2],
|
|
21
|
+
radiusRad: Math.min(Math.PI, radiusRad + EPS),
|
|
22
|
+
});
|
|
23
|
+
const fullCap = () => capAt([1, 0, 0], Math.PI);
|
|
24
|
+
const view = (cone) => [
|
|
25
|
+
cone.vx,
|
|
26
|
+
cone.vy,
|
|
27
|
+
cone.vz,
|
|
28
|
+
];
|
|
29
|
+
const visibleCap = (cap, cone) => isCapVisible(cap, { ...cone, viewRadius: cone.viewRadius + EPS });
|
|
30
|
+
const contained = (cap, cone) => angle(center(cap), view(cone)) + cap.radiusRad <= cone.viewRadius - EPS;
|
|
31
|
+
// The minor great-circle arc can cross the cone even if both endpoints are outside.
|
|
32
|
+
// Near-antipodal arcs have no uniquely stable tangent: keep them for D3.
|
|
33
|
+
function arcInterval(a, b, cone) {
|
|
34
|
+
const normal = cross(a, b);
|
|
35
|
+
const length = angle(a, b);
|
|
36
|
+
if (Math.hypot(...normal) < EPS) {
|
|
37
|
+
if (dot(a, b) < 0 || dot(a, view(cone)) >= Math.cos(cone.viewRadius) - EPS)
|
|
38
|
+
return { start: 0, end: length, length };
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const tangent = cross(unit(normal), a);
|
|
42
|
+
const x = dot(a, view(cone)), y = dot(tangent, view(cone));
|
|
43
|
+
const amplitude = Math.hypot(x, y);
|
|
44
|
+
const threshold = Math.cos(cone.viewRadius) - EPS;
|
|
45
|
+
if (amplitude < EPS)
|
|
46
|
+
return threshold <= EPS ? { start: 0, end: length, length, tangent } : null;
|
|
47
|
+
if (threshold > amplitude)
|
|
48
|
+
return null;
|
|
49
|
+
if (threshold <= -amplitude)
|
|
50
|
+
return { start: 0, end: length, length, tangent };
|
|
51
|
+
const phase = Math.atan2(y, x), half = Math.acos(clamp(threshold / amplitude));
|
|
52
|
+
// A view cone is at most a hemisphere. Its intersection with a minor arc
|
|
53
|
+
// is connected (up to numerical tolerance at the hemisphere boundary).
|
|
54
|
+
let start = Infinity, end = -Infinity;
|
|
55
|
+
for (const shift of [-TAU, 0, TAU]) {
|
|
56
|
+
const lo = Math.max(0, phase + shift - half);
|
|
57
|
+
const hi = Math.min(length, phase + shift + half);
|
|
58
|
+
if (lo <= hi) {
|
|
59
|
+
start = Math.min(start, lo);
|
|
60
|
+
end = Math.max(end, hi);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return start <= end ? { start, end, length, tangent } : null;
|
|
64
|
+
}
|
|
65
|
+
export function edgeIntersectsCone(a, b, cone) {
|
|
66
|
+
return arcInterval(a, b, cone) !== null;
|
|
67
|
+
}
|
|
68
|
+
function pointAt(a, tangent, t) {
|
|
69
|
+
const x = a[0] * Math.cos(t) + tangent[0] * Math.sin(t);
|
|
70
|
+
const y = a[1] * Math.cos(t) + tangent[1] * Math.sin(t);
|
|
71
|
+
const z = a[2] * Math.cos(t) + tangent[2] * Math.sin(t);
|
|
72
|
+
return [
|
|
73
|
+
(Math.atan2(y, x) * 180) / Math.PI,
|
|
74
|
+
(Math.atan2(z, Math.hypot(x, y)) * 180) / Math.PI,
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
function mergeCaps(a, b) {
|
|
78
|
+
if (a.radiusRad >= Math.PI || b.radiusRad >= Math.PI)
|
|
79
|
+
return fullCap();
|
|
80
|
+
const av = center(a), bv = center(b);
|
|
81
|
+
const sum = [av[0] + bv[0], av[1] + bv[1], av[2] + bv[2]];
|
|
82
|
+
if (Math.hypot(...sum) < EPS)
|
|
83
|
+
return fullCap();
|
|
84
|
+
const v = unit(sum);
|
|
85
|
+
return capAt(v, Math.max(angle(v, av) + a.radiusRad, angle(v, bv) + b.radiusRad));
|
|
86
|
+
}
|
|
87
|
+
function leafCap(vectors, start, end) {
|
|
88
|
+
const v = vectors[start];
|
|
89
|
+
// A cap smaller than a hemisphere is geodesically convex. Otherwise
|
|
90
|
+
// fail open: endpoint containment alone cannot bound a great-circle arc.
|
|
91
|
+
let radius = 0;
|
|
92
|
+
for (let i = start + 1; i <= end; i++)
|
|
93
|
+
radius = Math.max(radius, angle(v, vectors[i]));
|
|
94
|
+
return radius < Math.PI / 2 - EPS ? capAt(v, radius) : fullCap();
|
|
95
|
+
}
|
|
96
|
+
function buildNode(vectors, start, end) {
|
|
97
|
+
if (end - start <= 32)
|
|
98
|
+
return { start, end, cap: leafCap(vectors, start, end) };
|
|
99
|
+
const mid = (start + end) >>> 1;
|
|
100
|
+
const left = buildNode(vectors, start, mid), right = buildNode(vectors, mid, end);
|
|
101
|
+
return { start, end, left, right, cap: mergeCaps(left.cap, right.cap) };
|
|
102
|
+
}
|
|
103
|
+
// Geometry frames are immutable. Weak keys permit eviction with the LOD cache.
|
|
104
|
+
// Only camera-independent vectors and cap trees persist across view changes.
|
|
105
|
+
export function createSphericalLineClipper({ preserveSegments = false } = {}) {
|
|
106
|
+
const indexes = new WeakMap();
|
|
107
|
+
return (geometry, cone) => {
|
|
108
|
+
let index = indexes.get(geometry);
|
|
109
|
+
if (!index) {
|
|
110
|
+
const lines = (geometry.type === "LineString"
|
|
111
|
+
? [geometry.coordinates]
|
|
112
|
+
: geometry.coordinates)
|
|
113
|
+
.filter((points) => points.length >= 2)
|
|
114
|
+
.map((points) => {
|
|
115
|
+
const vectors = points.map(([lon, lat]) => lonLatToVec(lon, lat));
|
|
116
|
+
return {
|
|
117
|
+
points,
|
|
118
|
+
vectors,
|
|
119
|
+
root: buildNode(vectors, 0, vectors.length - 1),
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
index = {
|
|
123
|
+
lines,
|
|
124
|
+
sourceVertices: lines.reduce((n, line) => n + line.points.length, 0),
|
|
125
|
+
sourceSegments: lines.reduce((n, line) => n + line.points.length - 1, 0),
|
|
126
|
+
};
|
|
127
|
+
indexes.set(geometry, index);
|
|
128
|
+
}
|
|
129
|
+
const prev = index.lastCone;
|
|
130
|
+
if (prev &&
|
|
131
|
+
index.lastResult &&
|
|
132
|
+
prev.vx === cone.vx &&
|
|
133
|
+
prev.vy === cone.vy &&
|
|
134
|
+
prev.vz === cone.vz &&
|
|
135
|
+
prev.viewRadius === cone.viewRadius)
|
|
136
|
+
return {
|
|
137
|
+
...index.lastResult,
|
|
138
|
+
stats: { ...index.lastResult.stats, cacheHit: true },
|
|
139
|
+
};
|
|
140
|
+
const stats = {
|
|
141
|
+
sourceVertices: index.sourceVertices,
|
|
142
|
+
sourceSegments: index.sourceSegments,
|
|
143
|
+
submittedVertices: 0,
|
|
144
|
+
rejectedSegments: 0,
|
|
145
|
+
clippedSegments: 0,
|
|
146
|
+
visitedNodes: 0,
|
|
147
|
+
rejectedLines: 0,
|
|
148
|
+
cacheHit: false,
|
|
149
|
+
};
|
|
150
|
+
const coordinates = [];
|
|
151
|
+
for (const { points, vectors, root } of index.lines) {
|
|
152
|
+
let current;
|
|
153
|
+
const append = (part) => {
|
|
154
|
+
if (current && current.at(-1) === part[0]) {
|
|
155
|
+
for (let i = 1; i < part.length; i++)
|
|
156
|
+
current.push(part[i]);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
current = part.slice();
|
|
160
|
+
coordinates.push(current);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
const visit = (node) => {
|
|
164
|
+
stats.visitedNodes++;
|
|
165
|
+
if (!visibleCap(node.cap, cone)) {
|
|
166
|
+
stats.rejectedSegments += node.end - node.start;
|
|
167
|
+
if (node === root)
|
|
168
|
+
stats.rejectedLines++;
|
|
169
|
+
current = undefined;
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (contained(node.cap, cone)) {
|
|
173
|
+
append(points.slice(node.start, node.end + 1));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (node.left && node.right) {
|
|
177
|
+
visit(node.left);
|
|
178
|
+
visit(node.right);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
for (let i = node.start; i < node.end; i++) {
|
|
182
|
+
const a = vectors[i], b = vectors[i + 1];
|
|
183
|
+
const interval = arcInterval(a, b, cone);
|
|
184
|
+
if (!interval) {
|
|
185
|
+
stats.rejectedSegments++;
|
|
186
|
+
current = undefined;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
// Rendering consumers need the original endpoints: changing them
|
|
190
|
+
// changes D3's adaptive resampling and the rasterized stroke.
|
|
191
|
+
if (preserveSegments) {
|
|
192
|
+
append([points[i], points[i + 1]]);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
const { start, end, length, tangent } = interval;
|
|
196
|
+
const atStart = start <= EPS, atEnd = length - end <= EPS;
|
|
197
|
+
if (!atStart || !atEnd)
|
|
198
|
+
stats.clippedSegments++;
|
|
199
|
+
const first = atStart || !tangent ? points[i] : pointAt(a, tangent, start);
|
|
200
|
+
const last = atEnd || !tangent ? points[i + 1] : pointAt(a, tangent, end);
|
|
201
|
+
append([first, last]);
|
|
202
|
+
if (!atEnd)
|
|
203
|
+
current = undefined;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
visit(root);
|
|
207
|
+
}
|
|
208
|
+
stats.submittedVertices = coordinates.reduce((n, line) => n + line.length, 0);
|
|
209
|
+
const result = {
|
|
210
|
+
geometry: { type: "MultiLineString", coordinates },
|
|
211
|
+
stats,
|
|
212
|
+
};
|
|
213
|
+
index.lastCone = { ...cone };
|
|
214
|
+
index.lastResult = result;
|
|
215
|
+
return result;
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
export function clipLineToCone(coordinates, cone) {
|
|
219
|
+
return createSphericalLineClipper()({ type: "LineString", coordinates }, cone)
|
|
220
|
+
.geometry.coordinates;
|
|
221
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Work } from "./cooperative.js";
|
|
2
|
+
import { type BlgTree } from "./blg.js";
|
|
3
|
+
import { type SphericalPolygonSignature } from "./spherical-geometry.js";
|
|
4
|
+
import type { Topology } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Build a nested hierarchy by removing vertices from valid full detail.
|
|
7
|
+
* Batch at conservative tenth-LOD boundaries; when a batch fails, bisect it
|
|
8
|
+
* until we identify individually unsafe removals. Rejected vertices remain
|
|
9
|
+
* at level zero. Every accepted transition is validated in its actual context.
|
|
10
|
+
*
|
|
11
|
+
* The source quadtree priorities and ring visibility thresholds are not
|
|
12
|
+
* changed. A shared vertex has one removal decision across all of its owners.
|
|
13
|
+
* This uses the existing per-polygon/line reference validator, not a global
|
|
14
|
+
* intersection arrangement for unrelated boundaries.
|
|
15
|
+
*/
|
|
16
|
+
interface PreparedArcTrees {
|
|
17
|
+
trees: BlgTree[];
|
|
18
|
+
activationLevels: number[];
|
|
19
|
+
retainedVertices: number;
|
|
20
|
+
}
|
|
21
|
+
export declare function buildTopologySafeArcTrees(topology: Topology, nominalActivation: readonly number[], signatures: readonly SphericalPolygonSignature[][]): PreparedArcTrees;
|
|
22
|
+
export declare function buildTopologySafeArcTreeSteps(topology: Topology, nominalActivation: readonly number[], signatures: readonly SphericalPolygonSignature[][]): Work<PreparedArcTrees>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { runSync } from "./cooperative.js";
|
|
2
|
+
import { buildBlgTreeSteps } from "./blg.js";
|
|
3
|
+
import { validGreatCirclePath, validSphericalPolygon, } from "./spherical-geometry.js";
|
|
4
|
+
export function buildTopologySafeArcTrees(topology, nominalActivation, signatures) {
|
|
5
|
+
return runSync(buildTopologySafeArcTreeSteps(topology, nominalActivation, signatures));
|
|
6
|
+
}
|
|
7
|
+
export function* buildTopologySafeArcTreeSteps(topology, nominalActivation, signatures) {
|
|
8
|
+
const { vertices, arcs, features } = topology;
|
|
9
|
+
const positions = vertices.map(({ x, y }) => [x, y]);
|
|
10
|
+
const anchors = new Uint8Array(vertices.length);
|
|
11
|
+
for (const arc of arcs) {
|
|
12
|
+
anchors[arc.vertexIds[0]] = 1;
|
|
13
|
+
anchors[arc.vertexIds.at(-1)] = 1;
|
|
14
|
+
}
|
|
15
|
+
const groups = [];
|
|
16
|
+
const owners = Array.from({ length: vertices.length }, () => []);
|
|
17
|
+
for (let fi = 0; fi < features.length; fi++) {
|
|
18
|
+
if (fi % 32 === 0)
|
|
19
|
+
yield {
|
|
20
|
+
stage: "Validation groups",
|
|
21
|
+
completed: fi,
|
|
22
|
+
total: features.length,
|
|
23
|
+
};
|
|
24
|
+
const feature = features[fi];
|
|
25
|
+
const paths = feature.paths.map((path) => {
|
|
26
|
+
const ids = [];
|
|
27
|
+
for (const ref of path.arcs) {
|
|
28
|
+
const arc = arcs[Math.abs(ref) - 1].vertexIds;
|
|
29
|
+
for (let i = 0; i < arc.length; i++) {
|
|
30
|
+
const id = arc[ref < 0 ? arc.length - 1 - i : i];
|
|
31
|
+
if (ids.at(-1) !== id)
|
|
32
|
+
ids.push(id);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (path.closed && ids[0] !== ids.at(-1))
|
|
36
|
+
ids.push(ids[0]);
|
|
37
|
+
return ids;
|
|
38
|
+
});
|
|
39
|
+
for (let gi = 0; gi < feature.groups.length; gi++) {
|
|
40
|
+
const group = feature.groups[gi].map((pi) => paths[pi]);
|
|
41
|
+
const id = groups.length;
|
|
42
|
+
groups.push({ paths: group, signature: signatures[fi][gi] });
|
|
43
|
+
const unique = new Set();
|
|
44
|
+
for (const path of group)
|
|
45
|
+
for (const vertex of path)
|
|
46
|
+
if (!anchors[vertex])
|
|
47
|
+
unique.add(vertex);
|
|
48
|
+
for (const vertex of unique)
|
|
49
|
+
owners[vertex].push(id);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const active = new Uint8Array(vertices.length).fill(1);
|
|
53
|
+
const activationLevels = new Array(vertices.length).fill(0);
|
|
54
|
+
const batches = new Map();
|
|
55
|
+
for (const vertex of vertices) {
|
|
56
|
+
if (anchors[vertex.id])
|
|
57
|
+
continue;
|
|
58
|
+
// Round downward: preprocessing may retain detail earlier, never later
|
|
59
|
+
// than the nominal quadtree threshold.
|
|
60
|
+
const tick = Math.floor(nominalActivation[vertex.id] * 10);
|
|
61
|
+
if (tick === 0)
|
|
62
|
+
continue;
|
|
63
|
+
if (!batches.has(tick))
|
|
64
|
+
batches.set(tick, []);
|
|
65
|
+
batches.get(tick).push(vertex.id);
|
|
66
|
+
}
|
|
67
|
+
let retainedVertices = 0;
|
|
68
|
+
function validGroup(id) {
|
|
69
|
+
const { paths, signature } = groups[id];
|
|
70
|
+
const coordinates = paths.map((path) => path
|
|
71
|
+
.filter((vertex) => active[vertex])
|
|
72
|
+
.map((vertex) => positions[vertex]));
|
|
73
|
+
return signature
|
|
74
|
+
? validSphericalPolygon(coordinates, signature)
|
|
75
|
+
: coordinates.every(validGreatCirclePath);
|
|
76
|
+
}
|
|
77
|
+
function* remove(batch, tick) {
|
|
78
|
+
const affected = new Set();
|
|
79
|
+
for (const id of batch) {
|
|
80
|
+
active[id] = 0;
|
|
81
|
+
for (const owner of owners[id])
|
|
82
|
+
affected.add(owner);
|
|
83
|
+
}
|
|
84
|
+
let valid = true;
|
|
85
|
+
for (const group of affected) {
|
|
86
|
+
yield undefined;
|
|
87
|
+
if (!validGroup(group)) {
|
|
88
|
+
valid = false;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (valid) {
|
|
93
|
+
for (const id of batch)
|
|
94
|
+
activationLevels[id] = tick / 10;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
for (const id of batch)
|
|
98
|
+
active[id] = 1;
|
|
99
|
+
if (batch.length === 1) {
|
|
100
|
+
retainedVertices++;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const middle = Math.floor(batch.length / 2);
|
|
104
|
+
yield* remove(batch.slice(0, middle), tick);
|
|
105
|
+
yield* remove(batch.slice(middle), tick);
|
|
106
|
+
}
|
|
107
|
+
// Independent candidate components avoid revalidating unrelated countries
|
|
108
|
+
// when a local batch needs bisection. Group owners are shared across arcs.
|
|
109
|
+
let completed = 0;
|
|
110
|
+
for (const tick of [...batches.keys()].sort((a, b) => b - a)) {
|
|
111
|
+
yield {
|
|
112
|
+
stage: "Validate simplification",
|
|
113
|
+
completed: completed++,
|
|
114
|
+
total: batches.size,
|
|
115
|
+
};
|
|
116
|
+
const batch = batches.get(tick);
|
|
117
|
+
const parent = new Map(batch.map((id) => [id, id]));
|
|
118
|
+
const firstByGroup = new Map();
|
|
119
|
+
const root = (id) => {
|
|
120
|
+
while (parent.get(id) !== id) {
|
|
121
|
+
const next = parent.get(id);
|
|
122
|
+
parent.set(id, parent.get(next));
|
|
123
|
+
id = parent.get(id);
|
|
124
|
+
}
|
|
125
|
+
return id;
|
|
126
|
+
};
|
|
127
|
+
for (const id of batch)
|
|
128
|
+
for (const owner of owners[id]) {
|
|
129
|
+
const first = firstByGroup.get(owner);
|
|
130
|
+
if (first === undefined)
|
|
131
|
+
firstByGroup.set(owner, id);
|
|
132
|
+
else
|
|
133
|
+
parent.set(root(id), root(first));
|
|
134
|
+
}
|
|
135
|
+
const components = new Map();
|
|
136
|
+
for (const id of batch) {
|
|
137
|
+
const key = root(id);
|
|
138
|
+
if (!components.has(key))
|
|
139
|
+
components.set(key, []);
|
|
140
|
+
components.get(key).push(id);
|
|
141
|
+
}
|
|
142
|
+
for (const component of components.values())
|
|
143
|
+
yield* remove(component, tick);
|
|
144
|
+
}
|
|
145
|
+
// Encode the corrected threshold in the existing integer-level/fractional
|
|
146
|
+
// BLG representation. Use the same subtraction as selectBlgPositions.
|
|
147
|
+
const levels = activationLevels.map(Math.ceil);
|
|
148
|
+
const random = activationLevels.map((level) => Number.isInteger(level) ? 0 : 1 - (level - Math.floor(level)));
|
|
149
|
+
const trees = [];
|
|
150
|
+
for (const arc of arcs) {
|
|
151
|
+
if (trees.length % 128 === 0)
|
|
152
|
+
yield {
|
|
153
|
+
stage: "Build BLG trees",
|
|
154
|
+
completed: trees.length,
|
|
155
|
+
total: arcs.length,
|
|
156
|
+
};
|
|
157
|
+
trees.push(yield* buildBlgTreeSteps(arc.vertexIds.map((id) => levels[id]), arc.vertexIds.map((id) => random[id])));
|
|
158
|
+
}
|
|
159
|
+
return { trees, activationLevels, retainedVertices };
|
|
160
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Geometry, Position } from "./types.js";
|
|
2
|
+
import type { SphericalChart } from "./spherical-geometry.js";
|
|
3
|
+
/**
|
|
4
|
+
* Removes self-intersection loops from a closed lon/lat ring by cutting at
|
|
5
|
+
* each crossing and discarding the loop with fewer vertices. Returns the
|
|
6
|
+
* repaired ring, or null when the ring is not repairable this way.
|
|
7
|
+
*/
|
|
8
|
+
export declare function repairRing(ring: readonly Position[], chart?: SphericalChart | null, { maxRing, maxPasses }?: SphericalRepairOptions): Position[] | null;
|
|
9
|
+
/** Repairs every ring of a Polygon/MultiPolygon; reports whether it changed. */
|
|
10
|
+
export declare function repairGeometry(geometry: Geometry, options?: SphericalRepairOptions): {
|
|
11
|
+
geometry: Geometry;
|
|
12
|
+
changed: boolean;
|
|
13
|
+
};
|
|
14
|
+
export interface SphericalRepairOptions {
|
|
15
|
+
maxRing?: number;
|
|
16
|
+
maxPasses?: number;
|
|
17
|
+
}
|