@vizhub/quadstream 0.2.0 → 0.3.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 +2 -0
- package/README.md +366 -44
- 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 +15 -1
- package/dist/index.js +8 -1
- package/dist/lod-scheduler.d.ts +15 -0
- package/dist/lod-scheduler.js +85 -0
- package/dist/reconstruct.d.ts +15 -1
- package/dist/reconstruct.js +68 -29
- 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 +93 -0
- package/dist/spherical-dataset.d.ts +30 -6
- package/dist/spherical-dataset.js +129 -108
- package/dist/spherical-geometry.js +76 -44
- package/dist/spherical-preprocess.d.ts +19 -0
- package/dist/spherical-preprocess.js +144 -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/types.d.ts +5 -1
- package/package.json +11 -7
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { buildBlgTree } from "./blg.js";
|
|
2
|
+
import { validGreatCirclePath, validSphericalPolygon, } from "./spherical-geometry.js";
|
|
3
|
+
/**
|
|
4
|
+
* Build a nested hierarchy by removing vertices from valid full detail.
|
|
5
|
+
* Batch at conservative tenth-LOD boundaries; when a batch fails, bisect it
|
|
6
|
+
* until we identify individually unsafe removals. Rejected vertices remain
|
|
7
|
+
* at level zero. Every accepted transition is validated in its actual context.
|
|
8
|
+
*
|
|
9
|
+
* The source quadtree priorities and ring visibility thresholds are not
|
|
10
|
+
* changed. A shared vertex has one removal decision across all of its owners.
|
|
11
|
+
* This uses the existing per-polygon/line reference validator, not a global
|
|
12
|
+
* intersection arrangement for unrelated boundaries.
|
|
13
|
+
*/
|
|
14
|
+
export function buildTopologySafeArcTrees(topology, nominalActivation, signatures) {
|
|
15
|
+
const { vertices, arcs, features } = topology;
|
|
16
|
+
const positions = vertices.map(({ x, y }) => [x, y]);
|
|
17
|
+
const anchors = new Uint8Array(vertices.length);
|
|
18
|
+
for (const arc of arcs) {
|
|
19
|
+
anchors[arc.vertexIds[0]] = 1;
|
|
20
|
+
anchors[arc.vertexIds.at(-1)] = 1;
|
|
21
|
+
}
|
|
22
|
+
const groups = [];
|
|
23
|
+
const owners = Array.from({ length: vertices.length }, () => []);
|
|
24
|
+
for (let fi = 0; fi < features.length; fi++) {
|
|
25
|
+
const feature = features[fi];
|
|
26
|
+
const paths = feature.paths.map((path) => {
|
|
27
|
+
const ids = [];
|
|
28
|
+
for (const ref of path.arcs) {
|
|
29
|
+
const arc = arcs[Math.abs(ref) - 1].vertexIds;
|
|
30
|
+
for (let i = 0; i < arc.length; i++) {
|
|
31
|
+
const id = arc[ref < 0 ? arc.length - 1 - i : i];
|
|
32
|
+
if (ids.at(-1) !== id)
|
|
33
|
+
ids.push(id);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (path.closed && ids[0] !== ids.at(-1))
|
|
37
|
+
ids.push(ids[0]);
|
|
38
|
+
return ids;
|
|
39
|
+
});
|
|
40
|
+
for (let gi = 0; gi < feature.groups.length; gi++) {
|
|
41
|
+
const group = feature.groups[gi].map((pi) => paths[pi]);
|
|
42
|
+
const id = groups.length;
|
|
43
|
+
groups.push({ paths: group, signature: signatures[fi][gi] });
|
|
44
|
+
const unique = new Set();
|
|
45
|
+
for (const path of group)
|
|
46
|
+
for (const vertex of path)
|
|
47
|
+
if (!anchors[vertex])
|
|
48
|
+
unique.add(vertex);
|
|
49
|
+
for (const vertex of unique)
|
|
50
|
+
owners[vertex].push(id);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const active = new Uint8Array(vertices.length).fill(1);
|
|
54
|
+
const activationLevels = new Array(vertices.length).fill(0);
|
|
55
|
+
const batches = new Map();
|
|
56
|
+
for (const vertex of vertices) {
|
|
57
|
+
if (anchors[vertex.id])
|
|
58
|
+
continue;
|
|
59
|
+
// Round downward: preprocessing may retain detail earlier, never later
|
|
60
|
+
// than the nominal quadtree threshold.
|
|
61
|
+
const tick = Math.floor(nominalActivation[vertex.id] * 10);
|
|
62
|
+
if (tick === 0)
|
|
63
|
+
continue;
|
|
64
|
+
if (!batches.has(tick))
|
|
65
|
+
batches.set(tick, []);
|
|
66
|
+
batches.get(tick).push(vertex.id);
|
|
67
|
+
}
|
|
68
|
+
let retainedVertices = 0;
|
|
69
|
+
function validGroup(id) {
|
|
70
|
+
const { paths, signature } = groups[id];
|
|
71
|
+
const coordinates = paths.map((path) => path
|
|
72
|
+
.filter((vertex) => active[vertex])
|
|
73
|
+
.map((vertex) => positions[vertex]));
|
|
74
|
+
return signature
|
|
75
|
+
? validSphericalPolygon(coordinates, signature)
|
|
76
|
+
: coordinates.every(validGreatCirclePath);
|
|
77
|
+
}
|
|
78
|
+
function remove(batch, tick) {
|
|
79
|
+
const affected = new Set();
|
|
80
|
+
for (const id of batch) {
|
|
81
|
+
active[id] = 0;
|
|
82
|
+
for (const owner of owners[id])
|
|
83
|
+
affected.add(owner);
|
|
84
|
+
}
|
|
85
|
+
let valid = true;
|
|
86
|
+
for (const group of affected)
|
|
87
|
+
if (!validGroup(group)) {
|
|
88
|
+
valid = false;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
if (valid) {
|
|
92
|
+
for (const id of batch)
|
|
93
|
+
activationLevels[id] = tick / 10;
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
for (const id of batch)
|
|
97
|
+
active[id] = 1;
|
|
98
|
+
if (batch.length === 1) {
|
|
99
|
+
retainedVertices++;
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const middle = Math.floor(batch.length / 2);
|
|
103
|
+
remove(batch.slice(0, middle), tick);
|
|
104
|
+
remove(batch.slice(middle), tick);
|
|
105
|
+
}
|
|
106
|
+
// Independent candidate components avoid revalidating unrelated countries
|
|
107
|
+
// when a local batch needs bisection. Group owners are shared across arcs.
|
|
108
|
+
for (const tick of [...batches.keys()].sort((a, b) => b - a)) {
|
|
109
|
+
const batch = batches.get(tick);
|
|
110
|
+
const parent = new Map(batch.map((id) => [id, id]));
|
|
111
|
+
const firstByGroup = new Map();
|
|
112
|
+
const root = (id) => {
|
|
113
|
+
while (parent.get(id) !== id) {
|
|
114
|
+
const next = parent.get(id);
|
|
115
|
+
parent.set(id, parent.get(next));
|
|
116
|
+
id = parent.get(id);
|
|
117
|
+
}
|
|
118
|
+
return id;
|
|
119
|
+
};
|
|
120
|
+
for (const id of batch)
|
|
121
|
+
for (const owner of owners[id]) {
|
|
122
|
+
const first = firstByGroup.get(owner);
|
|
123
|
+
if (first === undefined)
|
|
124
|
+
firstByGroup.set(owner, id);
|
|
125
|
+
else
|
|
126
|
+
parent.set(root(id), root(first));
|
|
127
|
+
}
|
|
128
|
+
const components = new Map();
|
|
129
|
+
for (const id of batch) {
|
|
130
|
+
const key = root(id);
|
|
131
|
+
if (!components.has(key))
|
|
132
|
+
components.set(key, []);
|
|
133
|
+
components.get(key).push(id);
|
|
134
|
+
}
|
|
135
|
+
for (const component of components.values())
|
|
136
|
+
remove(component, tick);
|
|
137
|
+
}
|
|
138
|
+
// Encode the corrected threshold in the existing integer-level/fractional
|
|
139
|
+
// BLG representation. Use the same subtraction as selectBlgPositions.
|
|
140
|
+
const levels = activationLevels.map(Math.ceil);
|
|
141
|
+
const random = activationLevels.map((level) => Number.isInteger(level) ? 0 : 1 - (level - Math.floor(level)));
|
|
142
|
+
const trees = arcs.map((arc) => buildBlgTree(arc.vertexIds.map((id) => levels[id]), arc.vertexIds.map((id) => random[id])));
|
|
143
|
+
return { trees, activationLevels, retainedVertices };
|
|
144
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { canonicalLonLat, projectSpherical, sphericalChart, } from "./spherical-geometry.js";
|
|
2
|
+
import { simpleClosedRing } from "./geometry.js";
|
|
3
|
+
const DEG = 180 / Math.PI, MAX_RING = 5000, MAX_PASSES = 16;
|
|
4
|
+
const orient = (a, b, c) => (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
|
|
5
|
+
// First pair of non-adjacent edges that properly cross (O(n^2), only run on
|
|
6
|
+
// rings the validator already rejected).
|
|
7
|
+
function firstCrossing(xy) {
|
|
8
|
+
const n = xy.length - 1;
|
|
9
|
+
for (let i = 0; i < n; i++)
|
|
10
|
+
for (let j = i + 2; j < n; j++) {
|
|
11
|
+
if (i === 0 && j === n - 1)
|
|
12
|
+
continue;
|
|
13
|
+
const a = xy[i], b = xy[i + 1], c = xy[j], d = xy[j + 1], d1 = orient(a, b, c), d2 = orient(a, b, d), d3 = orient(c, d, a), d4 = orient(c, d, b);
|
|
14
|
+
if (d1 * d2 < 0 && d3 * d4 < 0) {
|
|
15
|
+
const t = d3 / (d3 - d4);
|
|
16
|
+
return {
|
|
17
|
+
i,
|
|
18
|
+
j,
|
|
19
|
+
x: [a[0] + t * (b[0] - a[0]), a[1] + t * (b[1] - a[1])],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
function unproject([x, y], chart) {
|
|
26
|
+
const v = [0, 1, 2].map((k) => chart.center[k] + x * chart.east[k] + y * chart.north[k]);
|
|
27
|
+
const len = Math.hypot(...v);
|
|
28
|
+
return canonicalLonLat([
|
|
29
|
+
Math.atan2(v[1], v[0]) * DEG,
|
|
30
|
+
Math.asin(Math.max(-1, Math.min(1, v[2] / len))) * DEG,
|
|
31
|
+
]);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Removes self-intersection loops from a closed lon/lat ring by cutting at
|
|
35
|
+
* each crossing and discarding the loop with fewer vertices. Returns the
|
|
36
|
+
* repaired ring, or null when the ring is not repairable this way.
|
|
37
|
+
*/
|
|
38
|
+
export function repairRing(ring, chart = null, { maxRing = MAX_RING, maxPasses = MAX_PASSES } = {}) {
|
|
39
|
+
if (ring.length < 4 || ring.length > maxRing)
|
|
40
|
+
return null;
|
|
41
|
+
let pts = ring.slice(0, -1);
|
|
42
|
+
try {
|
|
43
|
+
const activeChart = chart ?? sphericalChart(ring);
|
|
44
|
+
for (let pass = 0; pass < maxPasses; pass++) {
|
|
45
|
+
const closed = [...pts, pts[0]], xy = closed.map((p) => projectSpherical(p, activeChart));
|
|
46
|
+
if (simpleClosedRing(xy))
|
|
47
|
+
return closed;
|
|
48
|
+
const hit = firstCrossing(xy);
|
|
49
|
+
if (!hit)
|
|
50
|
+
return null;
|
|
51
|
+
const x = unproject(hit.x, activeChart), inner = pts.slice(hit.i + 1, hit.j + 1);
|
|
52
|
+
pts =
|
|
53
|
+
inner.length <= pts.length - inner.length
|
|
54
|
+
? [...pts.slice(0, hit.i + 1), x, ...pts.slice(hit.j + 1)]
|
|
55
|
+
: [x, ...inner];
|
|
56
|
+
if (pts.length < 3)
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
/** Repairs every ring of a Polygon/MultiPolygon; reports whether it changed. */
|
|
66
|
+
export function repairGeometry(geometry, options = {}) {
|
|
67
|
+
let changed = false;
|
|
68
|
+
const fix = (poly) => {
|
|
69
|
+
let chart = null;
|
|
70
|
+
try {
|
|
71
|
+
chart = sphericalChart(poly[0]);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return poly;
|
|
75
|
+
}
|
|
76
|
+
return poly.map((ring) => {
|
|
77
|
+
const xy = (() => {
|
|
78
|
+
try {
|
|
79
|
+
return ring.map((p) => projectSpherical(p, chart));
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
})();
|
|
85
|
+
if (!xy || simpleClosedRing(xy))
|
|
86
|
+
return ring;
|
|
87
|
+
const fixed = repairRing(ring, chart, options);
|
|
88
|
+
if (!fixed)
|
|
89
|
+
return ring;
|
|
90
|
+
changed = true;
|
|
91
|
+
return fixed;
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
const next = geometry.type === "Polygon"
|
|
95
|
+
? { ...geometry, coordinates: fix(geometry.coordinates) }
|
|
96
|
+
: geometry.type === "MultiPolygon"
|
|
97
|
+
? { ...geometry, coordinates: geometry.coordinates.map(fix) }
|
|
98
|
+
: geometry;
|
|
99
|
+
return { geometry: next, changed };
|
|
100
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Feature, Geometry, FeatureCollection } from "./types.js";
|
|
2
|
+
/** Group exact-coordinate-connected features, including shared line vertices. */
|
|
3
|
+
export declare function sphericalConnectedComponents(features: readonly Feature[]): number[][];
|
|
4
|
+
export type SphericalBuildGroup<T> = {
|
|
5
|
+
indices: number[];
|
|
6
|
+
engine: T;
|
|
7
|
+
error?: never;
|
|
8
|
+
} | {
|
|
9
|
+
indices: number[];
|
|
10
|
+
engine?: never;
|
|
11
|
+
error: string;
|
|
12
|
+
};
|
|
13
|
+
export interface RobustSphericalOptions<T> {
|
|
14
|
+
build: (indices: number[], working: readonly Feature[]) => SphericalBuildGroup<T>;
|
|
15
|
+
repair?: (geometry: Geometry) => {
|
|
16
|
+
geometry: Geometry;
|
|
17
|
+
changed: boolean;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/** Recover independently connected components after a failed global build.
|
|
21
|
+
* Failed groups retain their source geometry at the caller; no implicit repair. */
|
|
22
|
+
export declare function buildRobustSphericalGroups<T>(source: FeatureCollection, options: RobustSphericalOptions<T>): {
|
|
23
|
+
groups: SphericalBuildGroup<T>[];
|
|
24
|
+
working: Feature[];
|
|
25
|
+
unavailable: number;
|
|
26
|
+
reasons: string[];
|
|
27
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { canonicalLonLat } from "./spherical-geometry.js";
|
|
2
|
+
const paths = (g) => g.type === "Polygon"
|
|
3
|
+
? g.coordinates
|
|
4
|
+
: g.type === "MultiPolygon"
|
|
5
|
+
? g.coordinates.flat()
|
|
6
|
+
: g.type === "LineString"
|
|
7
|
+
? [g.coordinates]
|
|
8
|
+
: g.coordinates;
|
|
9
|
+
/** Group exact-coordinate-connected features, including shared line vertices. */
|
|
10
|
+
export function sphericalConnectedComponents(features) {
|
|
11
|
+
const parent = features.map((_, i) => i), owner = new Map();
|
|
12
|
+
const root = (index) => {
|
|
13
|
+
let i = index;
|
|
14
|
+
while (parent[i] !== i) {
|
|
15
|
+
parent[i] = parent[parent[i]];
|
|
16
|
+
i = parent[i];
|
|
17
|
+
}
|
|
18
|
+
return i;
|
|
19
|
+
};
|
|
20
|
+
for (const [i, feature] of features.entries()) {
|
|
21
|
+
for (const ring of paths(feature.geometry))
|
|
22
|
+
for (const vertex of ring) {
|
|
23
|
+
const key = JSON.stringify(canonicalLonLat(vertex));
|
|
24
|
+
const previous = owner.get(key);
|
|
25
|
+
if (previous === undefined)
|
|
26
|
+
owner.set(key, i);
|
|
27
|
+
else {
|
|
28
|
+
const a = root(i), b = root(previous);
|
|
29
|
+
if (a !== b)
|
|
30
|
+
parent[a] = b;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const groups = new Map();
|
|
35
|
+
for (const i of features.keys()) {
|
|
36
|
+
const k = root(i);
|
|
37
|
+
if (!groups.has(k))
|
|
38
|
+
groups.set(k, []);
|
|
39
|
+
groups.get(k).push(i);
|
|
40
|
+
}
|
|
41
|
+
return [...groups.values()];
|
|
42
|
+
}
|
|
43
|
+
/** Recover independently connected components after a failed global build.
|
|
44
|
+
* Failed groups retain their source geometry at the caller; no implicit repair. */
|
|
45
|
+
export function buildRobustSphericalGroups(source, options) {
|
|
46
|
+
const features = source.features, work = [...features], all = features.map((_, i) => i);
|
|
47
|
+
const build = (indices) => options.build(indices, work);
|
|
48
|
+
let groups = [build(all)];
|
|
49
|
+
if (groups[0]?.error && features.length > 1) {
|
|
50
|
+
try {
|
|
51
|
+
const singletonGroups = all.map((i) => {
|
|
52
|
+
let g = build([i]);
|
|
53
|
+
if (g.error && options.repair) {
|
|
54
|
+
const original = features[i], fixed = options.repair(original.geometry);
|
|
55
|
+
if (fixed.changed) {
|
|
56
|
+
work[i] = { ...original, geometry: fixed.geometry };
|
|
57
|
+
const retry = build([i]);
|
|
58
|
+
if (retry.error)
|
|
59
|
+
work[i] = original;
|
|
60
|
+
else
|
|
61
|
+
g = retry;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return g;
|
|
65
|
+
});
|
|
66
|
+
const invalid = singletonGroups.filter((g) => g.error);
|
|
67
|
+
const valid = all.filter((i) => !singletonGroups[i].error);
|
|
68
|
+
groups = [...(valid.length ? [build(valid)] : []), ...invalid];
|
|
69
|
+
// Regroup when a rejected singleton shares a vertex with a valid group.
|
|
70
|
+
const connected = sphericalConnectedComponents(features);
|
|
71
|
+
const failed = new Set(invalid.flatMap((g) => g.indices));
|
|
72
|
+
if (groups.some((g) => g.error && g.indices.length > 1) ||
|
|
73
|
+
connected.some((c) => c.some((i) => failed.has(i)) && c.some((i) => !failed.has(i)))) {
|
|
74
|
+
groups = connected.map(build);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
/* Keep the last complete grouping if recovery fails. */
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
groups,
|
|
83
|
+
working: work,
|
|
84
|
+
unavailable: groups
|
|
85
|
+
.filter((g) => g.error)
|
|
86
|
+
.reduce((n, g) => n + g.indices.length, 0),
|
|
87
|
+
reasons: [...new Set(groups.flatMap((g) => (g.error ? [g.error] : [])))],
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { SphericalClipStats } from "./spherical-clip.js";
|
|
2
|
+
import type { SphericalViewCone, SphericalCap } from "./spherical-cull.js";
|
|
3
|
+
import type { FeatureCollection } from "./types.js";
|
|
4
|
+
export interface SphericalViewClipperOptions {
|
|
5
|
+
margin?: number;
|
|
6
|
+
radiusScale?: number;
|
|
7
|
+
maxRadius?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface CachedSphericalClipResult {
|
|
10
|
+
geojson: FeatureCollection;
|
|
11
|
+
stats: SphericalClipStats & {
|
|
12
|
+
mode: "clip" | "cull";
|
|
13
|
+
rebuilt: boolean;
|
|
14
|
+
sourceVertices: number;
|
|
15
|
+
sourceFeatures: number;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** Inputs are immutable by convention. Change identity or call invalidate after edits.
|
|
19
|
+
* Supplied caps must conservatively enclose their corresponding frame features. */
|
|
20
|
+
export declare function createSphericalViewClipper({ margin, radiusScale, maxRadius, }?: SphericalViewClipperOptions): ((geojson: FeatureCollection, cone: SphericalViewCone, suppliedCaps?: readonly SphericalCap[]) => CachedSphericalClipResult) & {
|
|
21
|
+
invalidate: () => void;
|
|
22
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { clipWindow, clipFeatureCollection, buildFeatureInfo, countVertices, } from "./spherical-clip.js";
|
|
2
|
+
import { computeFeatureCaps, filterSphericalFeatures, } from "./spherical-cull.js";
|
|
3
|
+
/** Inputs are immutable by convention. Change identity or call invalidate after edits.
|
|
4
|
+
* Supplied caps must conservatively enclose their corresponding frame features. */
|
|
5
|
+
export function createSphericalViewClipper({ margin = 0, radiusScale = 1.2, maxRadius = Math.PI / 2 - 0.12, } = {}) {
|
|
6
|
+
if (!Number.isFinite(margin) ||
|
|
7
|
+
margin < 0 ||
|
|
8
|
+
!Number.isFinite(radiusScale) ||
|
|
9
|
+
radiusScale < 1 ||
|
|
10
|
+
!Number.isFinite(maxRadius) ||
|
|
11
|
+
maxRadius <= 0 ||
|
|
12
|
+
maxRadius >= Math.PI / 2)
|
|
13
|
+
throw new Error("Invalid spherical clip options");
|
|
14
|
+
let frame, capIdentity, caps, infos = [], sourceVertices = 0;
|
|
15
|
+
let cached;
|
|
16
|
+
const invalidate = () => {
|
|
17
|
+
frame = undefined;
|
|
18
|
+
capIdentity = undefined;
|
|
19
|
+
caps = undefined;
|
|
20
|
+
infos = [];
|
|
21
|
+
cached = undefined;
|
|
22
|
+
sourceVertices = 0;
|
|
23
|
+
};
|
|
24
|
+
function clip(geojson, cone, suppliedCaps) {
|
|
25
|
+
if (!Number.isFinite(cone.viewRadius) ||
|
|
26
|
+
cone.viewRadius < 0 ||
|
|
27
|
+
cone.viewRadius > Math.PI ||
|
|
28
|
+
Math.abs(Math.hypot(cone.vx, cone.vy, cone.vz) - 1) > 1e-9 ||
|
|
29
|
+
![cone.vx, cone.vy, cone.vz].every(Number.isFinite))
|
|
30
|
+
throw new Error("Invalid spherical view cone");
|
|
31
|
+
if (suppliedCaps && suppliedCaps.length !== geojson.features.length)
|
|
32
|
+
throw new Error("Spherical caps must match feature count");
|
|
33
|
+
if (frame !== geojson || capIdentity !== suppliedCaps) {
|
|
34
|
+
invalidate();
|
|
35
|
+
frame = geojson;
|
|
36
|
+
capIdentity = suppliedCaps;
|
|
37
|
+
caps = suppliedCaps ?? computeFeatureCaps(geojson);
|
|
38
|
+
sourceVertices = countVertices(geojson);
|
|
39
|
+
}
|
|
40
|
+
const sourceFeatures = geojson.features.length;
|
|
41
|
+
if (!sourceFeatures ||
|
|
42
|
+
cone.viewRadius + margin > maxRadius ||
|
|
43
|
+
cone.viewRadius + margin === 0) {
|
|
44
|
+
cached = undefined;
|
|
45
|
+
const visible = filterSphericalFeatures(geojson, cone, caps);
|
|
46
|
+
return {
|
|
47
|
+
geojson: visible,
|
|
48
|
+
stats: {
|
|
49
|
+
mode: "cull",
|
|
50
|
+
rebuilt: true,
|
|
51
|
+
sourceFeatures,
|
|
52
|
+
sourceVertices,
|
|
53
|
+
features: visible.features.length,
|
|
54
|
+
vertices: countVertices(visible),
|
|
55
|
+
rings: { kept: 0, clipped: 0, wrapped: 0, dropped: 0 },
|
|
56
|
+
stream: { classified: 0, inside: 0, outside: 0 },
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (cached) {
|
|
61
|
+
const w = cached.window;
|
|
62
|
+
const angle = Math.acos(Math.max(-1, Math.min(1, w.cx * cone.vx + w.cy * cone.vy + w.cz * cone.vz)));
|
|
63
|
+
if (angle + cone.viewRadius + margin <= w.radius)
|
|
64
|
+
return {
|
|
65
|
+
geojson: cached.result.geojson,
|
|
66
|
+
stats: { ...cached.result.stats, rebuilt: false },
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const window = clipWindow(cone.vx, cone.vy, cone.vz, Math.min(maxRadius, cone.viewRadius * radiusScale + margin));
|
|
70
|
+
const { geojson: out, stats } = clipFeatureCollection(geojson, window, (i, f) => (infos[i] ??= buildFeatureInfo(f, caps[i])));
|
|
71
|
+
const result = {
|
|
72
|
+
geojson: out,
|
|
73
|
+
stats: {
|
|
74
|
+
...stats,
|
|
75
|
+
mode: "clip",
|
|
76
|
+
rebuilt: true,
|
|
77
|
+
sourceFeatures,
|
|
78
|
+
sourceVertices,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
cached = { window, result };
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
return Object.assign(clip, { invalidate });
|
|
85
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -79,8 +79,12 @@ export interface QueryResult {
|
|
|
79
79
|
/** Whether the requested reconstruction passed the dataset's validity checks. */
|
|
80
80
|
valid: boolean;
|
|
81
81
|
usedFallback: boolean;
|
|
82
|
-
/**
|
|
82
|
+
/** Deprecated spherical diagnostic: always zero for preprocessed hierarchies. */
|
|
83
83
|
promotedArcs?: number;
|
|
84
|
+
/** Spherical queries with the "drop" policy: polygons hidden by the tree. */
|
|
85
|
+
hiddenPolygons?: number;
|
|
86
|
+
/** Spherical queries with the "drop" policy: closed rings (shells and holes) omitted. */
|
|
87
|
+
hiddenRings?: number;
|
|
84
88
|
selectedFeatures: number;
|
|
85
89
|
selectedVertices: number;
|
|
86
90
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vizhub/quadstream",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Deterministic, topology-aware progressive geographic geometry engine",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -21,6 +21,8 @@
|
|
|
21
21
|
"bin"
|
|
22
22
|
],
|
|
23
23
|
"scripts": {
|
|
24
|
+
"dev": "npm run build && node examples/progressive/server.mjs --globe",
|
|
25
|
+
"dev:restart": "node scripts/dev-restart.mjs",
|
|
24
26
|
"build": "tsc -p tsconfig.json",
|
|
25
27
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
26
28
|
"prettier": "prettier --write .",
|
|
@@ -35,16 +37,18 @@
|
|
|
35
37
|
"test:package": "node scripts/test-package.mjs"
|
|
36
38
|
},
|
|
37
39
|
"devDependencies": {
|
|
38
|
-
"typescript": "^6.0.3",
|
|
39
|
-
"prettier": "3.10.1",
|
|
40
|
-
"eslint": "10.15.0",
|
|
41
40
|
"@eslint/js": "10.0.1",
|
|
42
|
-
"typescript-eslint": "8.74.0",
|
|
43
|
-
"globals": "17.14.0",
|
|
44
|
-
"maplibre-gl": "6.19.0",
|
|
45
41
|
"@playwright/test": "1.65.1",
|
|
46
42
|
"d3": "7.9.2",
|
|
43
|
+
"eslint": "10.16.0",
|
|
44
|
+
"globals": "17.14.0",
|
|
45
|
+
"maplibre-gl": "6.20.0",
|
|
46
|
+
"prettier": "3.10.1",
|
|
47
47
|
"topojson-client": "3.1.0",
|
|
48
|
+
"typescript": "^6.0.3",
|
|
49
|
+
"typescript-eslint": "8.74.0",
|
|
50
|
+
"us-atlas": "3.0.1",
|
|
51
|
+
"visionscarto-world-atlas": "1.0.0",
|
|
48
52
|
"world-atlas": "2.0.2"
|
|
49
53
|
},
|
|
50
54
|
"bin": {
|