@vizhub/quadstream 0.3.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 +6 -0
- package/README.md +28 -624
- 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/index.d.ts +5 -1
- package/dist/index.js +1 -0
- package/dist/loader.d.ts +1 -1
- package/dist/loader.js +1 -1
- 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 +3 -0
- package/dist/reconstruct.js +26 -3
- package/dist/session.d.ts +12 -9
- package/dist/session.js +24 -7
- package/dist/spherical-cull.js +15 -6
- package/dist/spherical-dataset.d.ts +13 -5
- package/dist/spherical-dataset.js +119 -41
- package/dist/spherical-geometry.d.ts +2 -0
- package/dist/spherical-geometry.js +11 -0
- package/dist/spherical-line.d.ts +29 -0
- package/dist/spherical-line.js +221 -0
- package/dist/spherical-preprocess.d.ts +6 -2
- package/dist/spherical-preprocess.js +34 -18
- package/dist/topology.d.ts +3 -0
- package/dist/topology.js +40 -9
- package/package.json +3 -2
package/bin/quadstream.mjs
CHANGED
|
@@ -148,7 +148,9 @@ async function main() {
|
|
|
148
148
|
"file-depth",
|
|
149
149
|
);
|
|
150
150
|
const bundle = exportPartitions(
|
|
151
|
-
buildQuadstream(asSource(json(await read(input))), {
|
|
151
|
+
await buildQuadstream(asSource(json(await read(input))), {
|
|
152
|
+
maxLevel: level,
|
|
153
|
+
}),
|
|
152
154
|
{ fileDepth: depth },
|
|
153
155
|
);
|
|
154
156
|
validateManifest(bundle.manifest);
|
|
@@ -175,7 +177,7 @@ async function main() {
|
|
|
175
177
|
if (cmd === "validate") {
|
|
176
178
|
if ((await lstat(input)).isDirectory()) {
|
|
177
179
|
const { m, session } = await load(input, read),
|
|
178
|
-
result = session.query({ level: m.maxLevel - 1 });
|
|
180
|
+
result = await session.query({ level: m.maxLevel - 1 });
|
|
179
181
|
if (!result.complete || result.omittedFeatures)
|
|
180
182
|
throw new Error("Incomplete full-detail reconstruction");
|
|
181
183
|
print({ valid: true, ...inspect(m) });
|
|
@@ -197,7 +199,7 @@ async function main() {
|
|
|
197
199
|
m.maxLevel - 1,
|
|
198
200
|
"level",
|
|
199
201
|
);
|
|
200
|
-
const result = session.query({ level, bounds });
|
|
202
|
+
const result = await session.query({ level, bounds });
|
|
201
203
|
if (!result.complete) throw new Error("Incomplete query result");
|
|
202
204
|
const data =
|
|
203
205
|
format === "topojson"
|
|
@@ -214,12 +216,12 @@ async function main() {
|
|
|
214
216
|
runs = [];
|
|
215
217
|
for (let i = 0; i < iterations; i++) {
|
|
216
218
|
const start = performance.now(),
|
|
217
|
-
dataset = buildQuadstream(data, { maxLevel: level }),
|
|
219
|
+
dataset = await buildQuadstream(data, { maxLevel: level }),
|
|
218
220
|
built = performance.now(),
|
|
219
221
|
bundle = exportPartitions(dataset, { fileDepth: Math.min(4, level) }),
|
|
220
222
|
packed = performance.now();
|
|
221
|
-
const coarse = dataset.query({ level: 0 }),
|
|
222
|
-
full = dataset.query({ level: level - 1 }),
|
|
223
|
+
const coarse = await dataset.query({ level: 0 }),
|
|
224
|
+
full = await dataset.query({ level: level - 1 }),
|
|
223
225
|
done = performance.now();
|
|
224
226
|
runs.push({
|
|
225
227
|
buildMs: built - start,
|
package/dist/blg.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Work } from "./cooperative.js";
|
|
1
2
|
/** Ordered min-Cartesian tree over vertex priorities; inorder restores exact input order. */
|
|
2
3
|
export interface BlgNode {
|
|
3
4
|
position: number;
|
|
@@ -12,6 +13,8 @@ export interface BlgTree {
|
|
|
12
13
|
}
|
|
13
14
|
export declare function stableBlgRandom(key: number): number;
|
|
14
15
|
export declare function buildBlgTree(levels: readonly number[], randomValues?: readonly number[]): BlgTree;
|
|
16
|
+
export declare function buildBlgTreeSteps(levels: readonly number[], randomValues?: readonly number[]): Work<BlgTree>;
|
|
15
17
|
/** Iterative inorder traversal: no recursion depth limitation. */
|
|
16
18
|
export declare function selectBlgPositions(tree: BlgTree, maxLevel: number): number[];
|
|
19
|
+
export declare function selectBlgPositionSteps(tree: BlgTree, maxLevel: number): Work<number[]>;
|
|
17
20
|
export declare function assertBlgInvariants(tree: BlgTree): void;
|
package/dist/blg.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { runSync } from "./cooperative.js";
|
|
1
2
|
export function stableBlgRandom(key) {
|
|
2
3
|
let hash = Math.imul(key + 1, 0x9e3779b1);
|
|
3
4
|
hash ^= hash >>> 16;
|
|
@@ -6,10 +7,15 @@ export function stableBlgRandom(key) {
|
|
|
6
7
|
return (hash >>> 0) / 0x100000000;
|
|
7
8
|
}
|
|
8
9
|
export function buildBlgTree(levels, randomValues) {
|
|
10
|
+
return runSync(buildBlgTreeSteps(levels, randomValues));
|
|
11
|
+
}
|
|
12
|
+
export function* buildBlgTreeSteps(levels, randomValues) {
|
|
9
13
|
if (randomValues && randomValues.length !== levels.length)
|
|
10
14
|
throw new Error("BLG random values must match priorities");
|
|
11
15
|
const stack = [];
|
|
12
16
|
for (let position = 0; position < levels.length; position++) {
|
|
17
|
+
if (position % 512 === 0)
|
|
18
|
+
yield undefined;
|
|
13
19
|
const level = levels[position];
|
|
14
20
|
if (!Number.isFinite(level) || level < 0)
|
|
15
21
|
throw new Error("BLG priorities must be nonnegative and finite");
|
|
@@ -32,6 +38,9 @@ export function buildBlgTree(levels, randomValues) {
|
|
|
32
38
|
}
|
|
33
39
|
/** Iterative inorder traversal: no recursion depth limitation. */
|
|
34
40
|
export function selectBlgPositions(tree, maxLevel) {
|
|
41
|
+
return runSync(selectBlgPositionSteps(tree, maxLevel));
|
|
42
|
+
}
|
|
43
|
+
export function* selectBlgPositionSteps(tree, maxLevel) {
|
|
35
44
|
if (!Number.isFinite(maxLevel) && maxLevel !== Infinity)
|
|
36
45
|
throw new Error("Invalid selection level");
|
|
37
46
|
if (maxLevel < 0)
|
|
@@ -40,14 +49,18 @@ export function selectBlgPositions(tree, maxLevel) {
|
|
|
40
49
|
maxLevel = Number.MAX_SAFE_INTEGER;
|
|
41
50
|
const wholeLevel = Math.floor(maxLevel), fraction = maxLevel - wholeLevel, partialLevel = wholeLevel + 1, traversalLevel = fraction === 0 ? wholeLevel : partialLevel;
|
|
42
51
|
const selected = [], stack = [];
|
|
43
|
-
let current = tree.root;
|
|
52
|
+
let current = tree.root, steps = 0;
|
|
44
53
|
while (current || stack.length) {
|
|
45
54
|
while (current && current.level <= traversalLevel) {
|
|
55
|
+
if (++steps % 512 === 0)
|
|
56
|
+
yield undefined;
|
|
46
57
|
stack.push(current);
|
|
47
58
|
current = current.left;
|
|
48
59
|
}
|
|
49
60
|
if (!stack.length)
|
|
50
61
|
break;
|
|
62
|
+
if (++steps % 512 === 0)
|
|
63
|
+
yield undefined;
|
|
51
64
|
const node = stack.pop();
|
|
52
65
|
if (node.level <= wholeLevel ||
|
|
53
66
|
(fraction > 0 &&
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Generator checkpoints are cooperative, not hard real-time deadlines. */
|
|
2
|
+
export interface CooperativeOptions {
|
|
3
|
+
signal?: AbortSignal;
|
|
4
|
+
sliceMs?: number;
|
|
5
|
+
yieldTurn?: () => Promise<void>;
|
|
6
|
+
/** Observed compute slice duration, excluding task-yield wait time. */
|
|
7
|
+
onSlice?: (elapsedMs: number) => void;
|
|
8
|
+
onProgress?: (progress: {
|
|
9
|
+
stage: string;
|
|
10
|
+
completed?: number;
|
|
11
|
+
total?: number;
|
|
12
|
+
}) => void;
|
|
13
|
+
}
|
|
14
|
+
export type WorkProgress = {
|
|
15
|
+
stage: string;
|
|
16
|
+
completed?: number;
|
|
17
|
+
total?: number;
|
|
18
|
+
};
|
|
19
|
+
export type Work<T> = Generator<WorkProgress | undefined, T, void>;
|
|
20
|
+
export declare function runSync<T>(work: Work<T>): T;
|
|
21
|
+
export declare function runCooperative<T>(work: Work<T>, { signal, sliceMs, yieldTurn, onProgress, onSlice, }?: CooperativeOptions): Promise<T>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function runSync(work) {
|
|
2
|
+
let step = work.next();
|
|
3
|
+
while (!step.done)
|
|
4
|
+
step = work.next();
|
|
5
|
+
return step.value;
|
|
6
|
+
}
|
|
7
|
+
export async function runCooperative(work, { signal, sliceMs = 4, yieldTurn = () => new Promise((resolve) => setTimeout(resolve, 0)), onProgress, onSlice, } = {}) {
|
|
8
|
+
if (!Number.isFinite(sliceMs) || sliceMs < 0)
|
|
9
|
+
throw new RangeError("Invalid slice budget");
|
|
10
|
+
let start = performance.now(), count = 0;
|
|
11
|
+
try {
|
|
12
|
+
for (;;) {
|
|
13
|
+
signal?.throwIfAborted();
|
|
14
|
+
const step = work.next();
|
|
15
|
+
if (step.done) {
|
|
16
|
+
onSlice?.(performance.now() - start);
|
|
17
|
+
signal?.throwIfAborted();
|
|
18
|
+
return step.value;
|
|
19
|
+
}
|
|
20
|
+
if (step.value)
|
|
21
|
+
onProgress?.(step.value);
|
|
22
|
+
if (++count >= 1024 || performance.now() - start >= sliceMs) {
|
|
23
|
+
onSlice?.(performance.now() - start);
|
|
24
|
+
await yieldTurn();
|
|
25
|
+
start = performance.now();
|
|
26
|
+
count = 0;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
work.return(undefined);
|
|
32
|
+
}
|
|
33
|
+
}
|
package/dist/dataset.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type CooperativeOptions } from "./cooperative.js";
|
|
1
2
|
import type { FeatureCollection, QueryOptions, QueryResult, SparseQuadtree, Topology } from "./types.js";
|
|
2
3
|
import type { BlgTree } from "./blg.js";
|
|
3
4
|
export declare class QuadstreamDataset {
|
|
@@ -6,11 +7,14 @@ export declare class QuadstreamDataset {
|
|
|
6
7
|
readonly arcTrees: readonly BlgTree[];
|
|
7
8
|
private readonly featureIndex;
|
|
8
9
|
private readonly validityCache;
|
|
9
|
-
constructor(
|
|
10
|
+
private constructor();
|
|
11
|
+
static create(input: FeatureCollection, options?: {
|
|
10
12
|
maxLevel?: number;
|
|
11
|
-
})
|
|
12
|
-
query(options?: QueryOptions): QueryResult
|
|
13
|
+
} & CooperativeOptions): Promise<QuadstreamDataset>;
|
|
14
|
+
query(options?: QueryOptions & CooperativeOptions): Promise<QueryResult>;
|
|
15
|
+
private querySteps;
|
|
13
16
|
}
|
|
17
|
+
/** Cooperative construction using the same topology and priority algorithms. */
|
|
14
18
|
export declare function buildQuadstream(input: FeatureCollection, options?: {
|
|
15
19
|
maxLevel?: number;
|
|
16
|
-
}): QuadstreamDataset
|
|
20
|
+
} & CooperativeOptions): Promise<QuadstreamDataset>;
|
package/dist/dataset.js
CHANGED
|
@@ -1,21 +1,46 @@
|
|
|
1
|
+
import { runSync, runCooperative, } from "./cooperative.js";
|
|
1
2
|
import { assertBounds } from "./geometry.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
3
|
+
import { buildPlanarTopologySteps } from "./topology.js";
|
|
4
|
+
import { buildSparseQuadtreeSteps } from "./quadtree.js";
|
|
4
5
|
import { createSpatialIndex } from "./spatial-index.js";
|
|
5
|
-
import {
|
|
6
|
+
import { buildArcTreeSteps, reconstructFeatureSteps } from "./reconstruct.js";
|
|
7
|
+
function* preparePlanar(input, maxLevel) {
|
|
8
|
+
yield { stage: "Construct topology" };
|
|
9
|
+
const topology = yield* buildPlanarTopologySteps(input);
|
|
10
|
+
const quadtree = yield* buildSparseQuadtreeSteps(topology.vertices, topology.bounds, maxLevel);
|
|
11
|
+
const arcTrees = yield* buildArcTreeSteps(topology.arcs, quadtree.levelByVertexId, topology.vertices.map((v) => v.critical));
|
|
12
|
+
return { topology, quadtree, arcTrees };
|
|
13
|
+
}
|
|
6
14
|
export class QuadstreamDataset {
|
|
7
15
|
topology;
|
|
8
16
|
quadtree;
|
|
9
17
|
arcTrees;
|
|
10
18
|
featureIndex;
|
|
11
19
|
validityCache = new Map();
|
|
12
|
-
constructor(input, { maxLevel = 20 } = {}) {
|
|
13
|
-
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
20
|
+
constructor(input, { maxLevel = 20 } = {}, prepared) {
|
|
21
|
+
const state = prepared ?? runSync(preparePlanar(input, maxLevel));
|
|
22
|
+
this.topology = state.topology;
|
|
23
|
+
this.quadtree = state.quadtree;
|
|
24
|
+
this.arcTrees = state.arcTrees;
|
|
16
25
|
this.featureIndex = createSpatialIndex(this.topology.features.map((f, i) => ({ bounds: f.bounds, value: i })));
|
|
17
26
|
}
|
|
27
|
+
static async create(input, options = {}) {
|
|
28
|
+
const prepared = await runCooperative(preparePlanar(input, options.maxLevel ?? 20), options);
|
|
29
|
+
options.signal?.throwIfAborted();
|
|
30
|
+
return new QuadstreamDataset(input, options, prepared);
|
|
31
|
+
}
|
|
32
|
+
/** @internal Synchronous reference for algorithm parity tests. */
|
|
33
|
+
static createSync(input, options) {
|
|
34
|
+
return new QuadstreamDataset(input, options);
|
|
35
|
+
}
|
|
36
|
+
/** @internal Synchronous reference for algorithm parity tests. */
|
|
37
|
+
querySync(options = {}) {
|
|
38
|
+
return runSync(this.querySteps(options));
|
|
39
|
+
}
|
|
18
40
|
query(options = {}) {
|
|
41
|
+
return runCooperative(this.querySteps(options), options);
|
|
42
|
+
}
|
|
43
|
+
*querySteps(options) {
|
|
19
44
|
const level = options.level ?? this.quadtree.maxLevel - 1;
|
|
20
45
|
if (!Number.isFinite(level) || level < 0 || level >= this.quadtree.maxLevel)
|
|
21
46
|
throw new Error("Invalid query level");
|
|
@@ -36,16 +61,23 @@ export class QuadstreamDataset {
|
|
|
36
61
|
else {
|
|
37
62
|
// Viewport-independent decision: shared boundaries must render consistently
|
|
38
63
|
// even when two viewports select different subsets of features.
|
|
39
|
-
|
|
64
|
+
for (const f of this.topology.features) {
|
|
65
|
+
const result = yield* reconstructFeatureSteps(f, this.topology.arcs, this.arcTrees, level, coords);
|
|
66
|
+
if (!result.valid) {
|
|
67
|
+
globallyInvalid = true;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
40
71
|
this.validityCache.set(level, !globallyInvalid);
|
|
41
72
|
}
|
|
42
73
|
}
|
|
43
74
|
let total = 0;
|
|
44
|
-
const results =
|
|
45
|
-
|
|
75
|
+
const results = [];
|
|
76
|
+
for (const i of selected) {
|
|
77
|
+
const r = yield* reconstructFeatureSteps(this.topology.features[i], this.topology.arcs, this.arcTrees, globallyInvalid ? Infinity : level, coords);
|
|
46
78
|
total += r.selectedVertices;
|
|
47
|
-
|
|
48
|
-
}
|
|
79
|
+
results.push(r.feature);
|
|
80
|
+
}
|
|
49
81
|
return {
|
|
50
82
|
geojson: { type: "FeatureCollection", features: results },
|
|
51
83
|
level,
|
|
@@ -56,6 +88,11 @@ export class QuadstreamDataset {
|
|
|
56
88
|
};
|
|
57
89
|
}
|
|
58
90
|
}
|
|
59
|
-
|
|
60
|
-
|
|
91
|
+
/** @internal Synchronous reference for algorithm parity tests. */
|
|
92
|
+
export function buildQuadstreamSync(input, options) {
|
|
93
|
+
return QuadstreamDataset.createSync(input, options);
|
|
94
|
+
}
|
|
95
|
+
/** Cooperative construction using the same topology and priority algorithms. */
|
|
96
|
+
export async function buildQuadstream(input, options = {}) {
|
|
97
|
+
return QuadstreamDataset.create(input, options);
|
|
61
98
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export { buildBlgTree, selectBlgPositions, stableBlgRandom, assertBlgInvariants,
|
|
|
5
5
|
export { createSpatialIndex } from "./spatial-index.js";
|
|
6
6
|
export { exportPartitions, encodePartition, decodePartition, checksum, requiredPartitionKeys, partitionsWithVerticesInBounds, } from "./partition.js";
|
|
7
7
|
export { QuadstreamSession } from "./session.js";
|
|
8
|
+
export type { SessionQueryResult } from "./session.js";
|
|
8
9
|
export { QuadstreamLoader, fetchFromBundle } from "./loader.js";
|
|
9
10
|
export type { PartitionFetcher, LoaderOptions } from "./loader.js";
|
|
10
11
|
export { validPolygon, simpleClosedRing, simpleClosedRingReference, segmentsIntersect, pointInRing, intersects, boundsOf, } from "./geometry.js";
|
|
@@ -19,7 +20,7 @@ export type { MapLibreLike, MapLibreAdapterOptions, } from "./maplibre-adapter.j
|
|
|
19
20
|
export { canonicalLonLat, lonLatVector, angularDistance, validGreatCirclePath, sphericalCellAddress, buildSphericalPriorities, sphericalChart, projectSpherical, sphericalPolygonSignature, validSphericalPolygon, } from "./spherical-geometry.js";
|
|
20
21
|
export type { UnitVector, CubeFace, SphericalCell, SphericalPriorities, SphericalChart, SphericalPolygonSignature, } from "./spherical-geometry.js";
|
|
21
22
|
export { SphericalQuadstreamDataset, buildSphericalQuadstream, importSphericalTopoJSON, } from "./spherical-dataset.js";
|
|
22
|
-
export type { SphericalQueryOptions } from "./spherical-dataset.js";
|
|
23
|
+
export type { SphericalBuildOptions, SphericalQueryOptions, } from "./spherical-dataset.js";
|
|
23
24
|
export { computeFeatureCap, computeFeatureCaps, isCapVisible, filterSphericalFeatures, } from "./spherical-cull.js";
|
|
24
25
|
export type { SphericalCap, SphericalViewCone } from "./spherical-cull.js";
|
|
25
26
|
export { clipWindow, clipToWindow } from "./spherical-clip.js";
|
|
@@ -34,3 +35,6 @@ export { packGeometry, unpackGeometry } from "./geometry-wire.js";
|
|
|
34
35
|
export type { PackedGeometry } from "./geometry-wire.js";
|
|
35
36
|
export { createSphericalViewClipper } from "./spherical-view-clipper.js";
|
|
36
37
|
export type { SphericalViewClipperOptions, CachedSphericalClipResult, } from "./spherical-view-clipper.js";
|
|
38
|
+
export { createSphericalLineClipper, clipLineToCone, edgeIntersectsCone, } from "./spherical-line.js";
|
|
39
|
+
export type { SphericalLineStats, SphericalLineResult, } from "./spherical-line.js";
|
|
40
|
+
export type { CooperativeOptions, WorkProgress } from "./cooperative.js";
|
package/dist/index.js
CHANGED
|
@@ -20,3 +20,4 @@ export { createLatestGeometry, improvesLod } from "./lod-scheduler.js";
|
|
|
20
20
|
export { sphericalConnectedComponents, buildRobustSphericalGroups, } from "./spherical-robust-dataset.js";
|
|
21
21
|
export { packGeometry, unpackGeometry } from "./geometry-wire.js";
|
|
22
22
|
export { createSphericalViewClipper } from "./spherical-view-clipper.js";
|
|
23
|
+
export { createSphericalLineClipper, clipLineToCone, edgeIntersectsCone, } from "./spherical-line.js";
|
package/dist/loader.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export declare class QuadstreamLoader {
|
|
|
27
27
|
signal?: AbortSignal;
|
|
28
28
|
}): Promise<void>;
|
|
29
29
|
/** Supersede the preceding latest-view request without clearing valid stored geometry. */
|
|
30
|
-
loadLatest(query?: QueryOptions): Promise<ReturnType<QuadstreamSession["query"]
|
|
30
|
+
loadLatest(query?: QueryOptions): Promise<Awaited<ReturnType<QuadstreamSession["query"]>>>;
|
|
31
31
|
cancelLatest(): void;
|
|
32
32
|
}
|
|
33
33
|
/** In-memory adapter for examples and deterministic end-to-end tests. */
|
package/dist/loader.js
CHANGED
|
@@ -116,7 +116,7 @@ export class QuadstreamLoader {
|
|
|
116
116
|
await this.ensureLevel(query.level ?? this.session.manifest.maxLevel - 1, { signal: controller.signal });
|
|
117
117
|
if (controller.signal.aborted)
|
|
118
118
|
throw abortError();
|
|
119
|
-
return this.session.query(query);
|
|
119
|
+
return await this.session.query({ ...query, signal: controller.signal });
|
|
120
120
|
}
|
|
121
121
|
finally {
|
|
122
122
|
if (this.latestRequest === controller)
|
|
@@ -3,7 +3,7 @@ import type { QuadstreamSession } from "./session.js";
|
|
|
3
3
|
import type { QueryOptions } from "./types.js";
|
|
4
4
|
export type MapQuery = Omit<QueryOptions, "validate">;
|
|
5
5
|
export interface MapFrame {
|
|
6
|
-
result: ReturnType<QuadstreamSession["query"]
|
|
6
|
+
result: Awaited<ReturnType<QuadstreamSession["query"]>>;
|
|
7
7
|
targetLevel: number;
|
|
8
8
|
final: boolean;
|
|
9
9
|
loadedPartitions: number;
|
package/dist/progressive-map.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { assertBounds } from "./geometry.js";
|
|
1
2
|
function aborted() {
|
|
2
3
|
const e = new Error("Map request cancelled");
|
|
3
4
|
e.name = "AbortError";
|
|
@@ -71,9 +72,10 @@ export class ProgressiveMap {
|
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
};
|
|
74
|
-
const emit = (level, final) => {
|
|
75
|
+
const emit = async (level, final) => {
|
|
76
|
+
check();
|
|
77
|
+
const result = await session.query({ level, bounds, signal });
|
|
75
78
|
check();
|
|
76
|
-
const result = session.query({ level, bounds });
|
|
77
79
|
if (final && !result.complete)
|
|
78
80
|
throw new Error("Incomplete final geometry");
|
|
79
81
|
onFrame({
|
|
@@ -87,21 +89,23 @@ export class ProgressiveMap {
|
|
|
87
89
|
};
|
|
88
90
|
try {
|
|
89
91
|
session.missingKeys(target);
|
|
90
|
-
|
|
92
|
+
if (bounds)
|
|
93
|
+
assertBounds(bounds); // validate bounds before fetching
|
|
91
94
|
for (const level of [...new Set([0, Math.floor(target / 2), target])]) {
|
|
92
95
|
await ensure(level);
|
|
93
|
-
const global = session.query({ level });
|
|
96
|
+
const global = await session.query({ level, signal });
|
|
97
|
+
check();
|
|
94
98
|
if (level === target) {
|
|
95
99
|
if (!global.complete) {
|
|
96
|
-
emit(level, false);
|
|
100
|
+
await emit(level, false);
|
|
97
101
|
await ensure(session.manifest.maxLevel - 1);
|
|
98
|
-
if (!session.query({ level }).complete)
|
|
102
|
+
if (!(await session.query({ level, signal })).complete)
|
|
99
103
|
throw new Error("Incomplete global geometry");
|
|
100
104
|
}
|
|
101
|
-
emit(level, true);
|
|
105
|
+
await emit(level, true);
|
|
102
106
|
}
|
|
103
107
|
else
|
|
104
|
-
emit(level, false);
|
|
108
|
+
await emit(level, false);
|
|
105
109
|
}
|
|
106
110
|
}
|
|
107
111
|
finally {
|
package/dist/quadtree.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Work } from "./cooperative.js";
|
|
1
2
|
import type { Bounds, SparseQuadtree, Vertex } from "./types.js";
|
|
2
3
|
export declare function cellKey(level: number, i: number, j: number): string;
|
|
3
4
|
export declare function cellAddress(x: number, y: number, bounds: Bounds, level: number): {
|
|
@@ -6,3 +7,4 @@ export declare function cellAddress(x: number, y: number, bounds: Bounds, level:
|
|
|
6
7
|
};
|
|
7
8
|
/** Assign each distinct vertex to the first unoccupied quadtree cell, coarse to fine. */
|
|
8
9
|
export declare function buildSparseQuadtree(vertices: readonly Vertex[], bounds: Bounds, maxLevel?: number): SparseQuadtree;
|
|
10
|
+
export declare function buildSparseQuadtreeSteps(vertices: readonly Vertex[], bounds: Bounds, maxLevel?: number): Work<SparseQuadtree>;
|
package/dist/quadtree.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { runSync } from "./cooperative.js";
|
|
1
2
|
import { assertBounds } from "./geometry.js";
|
|
2
3
|
export function cellKey(level, i, j) {
|
|
3
4
|
return `${level}_${i}_${j}`;
|
|
@@ -22,6 +23,9 @@ export function cellAddress(x, y, bounds, level) {
|
|
|
22
23
|
}
|
|
23
24
|
/** Assign each distinct vertex to the first unoccupied quadtree cell, coarse to fine. */
|
|
24
25
|
export function buildSparseQuadtree(vertices, bounds, maxLevel = 20) {
|
|
26
|
+
return runSync(buildSparseQuadtreeSteps(vertices, bounds, maxLevel));
|
|
27
|
+
}
|
|
28
|
+
export function* buildSparseQuadtreeSteps(vertices, bounds, maxLevel = 20) {
|
|
25
29
|
assertBounds(bounds);
|
|
26
30
|
if (!Number.isInteger(maxLevel) || maxLevel < 1 || maxLevel > 26)
|
|
27
31
|
throw new Error("maxLevel must be 1..26");
|
|
@@ -30,7 +34,15 @@ export function buildSparseQuadtree(vertices, bounds, maxLevel = 20) {
|
|
|
30
34
|
a.y - b.y ||
|
|
31
35
|
a.id - b.id);
|
|
32
36
|
const occupied = new Set(), cells = [], levelByVertexId = [];
|
|
37
|
+
let processed = 0;
|
|
33
38
|
for (const v of sorted) {
|
|
39
|
+
if (processed % 512 === 0)
|
|
40
|
+
yield {
|
|
41
|
+
stage: "Planar priorities",
|
|
42
|
+
completed: processed,
|
|
43
|
+
total: sorted.length,
|
|
44
|
+
};
|
|
45
|
+
processed++;
|
|
34
46
|
let inserted = false;
|
|
35
47
|
for (let level = 0; level < maxLevel; level++) {
|
|
36
48
|
const { i, j } = cellAddress(v.x, v.y, bounds, level), key = cellKey(level, i, j);
|
package/dist/reconstruct.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Work } from "./cooperative.js";
|
|
1
2
|
import { type BlgTree } from "./blg.js";
|
|
2
3
|
import type { Arc, Feature, Position, TopologyFeature } from "./types.js";
|
|
3
4
|
export interface CoordinateLookup {
|
|
@@ -24,4 +25,6 @@ export interface ReconstructOptions {
|
|
|
24
25
|
hiddenPaths?: ReadonlySet<number>;
|
|
25
26
|
}
|
|
26
27
|
export declare function buildArcTrees(arcs: readonly Arc[], levels: readonly number[], critical?: readonly boolean[]): BlgTree[];
|
|
28
|
+
export declare function buildArcTreeSteps(arcs: readonly Arc[], levels: readonly number[], critical?: readonly boolean[]): Work<BlgTree[]>;
|
|
27
29
|
export declare function reconstructFeature(f: TopologyFeature, arcs: readonly Arc[], trees: readonly BlgTree[], level: number | ((arcId: number) => number), lookup: CoordinateLookup, _includeMissing?: boolean, planarValidity?: boolean, options?: ReconstructOptions): ReconstructionResult;
|
|
30
|
+
export declare function reconstructFeatureSteps(f: TopologyFeature, arcs: readonly Arc[], trees: readonly BlgTree[], level: number | ((arcId: number) => number), lookup: CoordinateLookup, planarValidity?: boolean, options?: ReconstructOptions): Work<ReconstructionResult>;
|
package/dist/reconstruct.js
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
|
+
import { runSync } from "./cooperative.js";
|
|
1
2
|
import { validPolygon } from "./geometry.js";
|
|
2
|
-
import {
|
|
3
|
+
import { buildBlgTreeSteps, selectBlgPositionSteps, stableBlgRandom, } from "./blg.js";
|
|
3
4
|
export function buildArcTrees(arcs, levels, critical = []) {
|
|
4
|
-
return
|
|
5
|
+
return runSync(buildArcTreeSteps(arcs, levels, critical));
|
|
6
|
+
}
|
|
7
|
+
export function* buildArcTreeSteps(arcs, levels, critical = []) {
|
|
8
|
+
const trees = [];
|
|
9
|
+
for (const arc of arcs) {
|
|
10
|
+
if (trees.length % 128 === 0)
|
|
11
|
+
yield {
|
|
12
|
+
stage: "Build BLG trees",
|
|
13
|
+
completed: trees.length,
|
|
14
|
+
total: arcs.length,
|
|
15
|
+
};
|
|
16
|
+
trees.push(yield* buildBlgTreeSteps(arc.vertexIds.map((id, i) => i === 0 || i === arc.vertexIds.length - 1 ? 0 : levels[id]), arc.vertexIds.map((id) => (critical[id] ? 1 : stableBlgRandom(id)))));
|
|
17
|
+
}
|
|
18
|
+
return trees;
|
|
5
19
|
}
|
|
6
20
|
/** True when fewer than 3 distinct positions precede the closing vertex. */
|
|
7
21
|
function distinctBelow3(coords) {
|
|
@@ -24,6 +38,9 @@ function distinctBelow3(coords) {
|
|
|
24
38
|
export function reconstructFeature(f, arcs, trees, level, lookup,
|
|
25
39
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
26
40
|
_includeMissing = false, planarValidity = true, options = {}) {
|
|
41
|
+
return runSync(reconstructFeatureSteps(f, arcs, trees, level, lookup, planarValidity, options));
|
|
42
|
+
}
|
|
43
|
+
export function* reconstructFeatureSteps(f, arcs, trees, level, lookup, planarValidity = true, options = {}) {
|
|
27
44
|
let missing = false, selectedVertices = 0;
|
|
28
45
|
const dropCollapsed = options.collapsed === "drop";
|
|
29
46
|
const hidden = new Set(options.hiddenPaths);
|
|
@@ -38,6 +55,8 @@ _includeMissing = false, planarValidity = true, options = {}) {
|
|
|
38
55
|
}
|
|
39
56
|
const paths = [];
|
|
40
57
|
for (let pathIndex = 0; pathIndex < f.paths.length; pathIndex++) {
|
|
58
|
+
if (pathIndex % 32 === 0)
|
|
59
|
+
yield undefined;
|
|
41
60
|
const path = f.paths[pathIndex];
|
|
42
61
|
if (hidden.has(pathIndex) || hidden.has(shellOf[pathIndex] ?? -1)) {
|
|
43
62
|
hidden.add(pathIndex);
|
|
@@ -48,9 +67,11 @@ _includeMissing = false, planarValidity = true, options = {}) {
|
|
|
48
67
|
let pathSelectedVertices = 0;
|
|
49
68
|
for (const ref of path.arcs) {
|
|
50
69
|
const arcId = Math.abs(ref) - 1, arc = arcs[arcId], tree = trees[arcId];
|
|
51
|
-
const positions =
|
|
70
|
+
const positions = yield* selectBlgPositionSteps(tree, typeof level === "number" ? level : level(arcId));
|
|
52
71
|
const last = positions.length - 1;
|
|
53
72
|
for (let i = 0; i < positions.length; i++) {
|
|
73
|
+
if (i % 512 === 0)
|
|
74
|
+
yield undefined;
|
|
54
75
|
const id = arc.vertexIds[positions[ref < 0 ? last - i : i]], xy = lookup(id);
|
|
55
76
|
if (!xy) {
|
|
56
77
|
missing = true;
|
|
@@ -79,6 +100,8 @@ _includeMissing = false, planarValidity = true, options = {}) {
|
|
|
79
100
|
for (const ref of path.arcs) {
|
|
80
101
|
const ids = arcs[Math.abs(ref) - 1].vertexIds;
|
|
81
102
|
for (let k = 0; k < ids.length; k++) {
|
|
103
|
+
if (k % 512 === 0)
|
|
104
|
+
yield undefined;
|
|
82
105
|
const id = ids[ref < 0 ? ids.length - 1 - k : k];
|
|
83
106
|
if (id === lastId)
|
|
84
107
|
continue;
|
package/dist/session.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type CooperativeOptions } from "./cooperative.js";
|
|
1
2
|
import type { FeatureCollection, QuadstreamManifest, QueryOptions } from "./types.js";
|
|
2
3
|
/** Progressive session: validates every partition before changing observable state. */
|
|
3
4
|
export declare class QuadstreamSession {
|
|
@@ -12,13 +13,15 @@ export declare class QuadstreamSession {
|
|
|
12
13
|
load(key: string, bytes: Uint8Array): boolean;
|
|
13
14
|
get loadedKeys(): string[];
|
|
14
15
|
missingKeys(level: number): string[];
|
|
15
|
-
query(options?: QueryOptions):
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
query(options?: QueryOptions & CooperativeOptions): Promise<SessionQueryResult>;
|
|
17
|
+
private querySteps;
|
|
18
|
+
}
|
|
19
|
+
export interface SessionQueryResult {
|
|
20
|
+
geojson: FeatureCollection;
|
|
21
|
+
complete: boolean;
|
|
22
|
+
level: number;
|
|
23
|
+
missingKeys: string[];
|
|
24
|
+
omittedFeatures: number;
|
|
25
|
+
selectedVertices: number;
|
|
26
|
+
usedFallback: boolean;
|
|
24
27
|
}
|
package/dist/session.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { runSync, runCooperative, } from "./cooperative.js";
|
|
1
2
|
import { assertBounds } from "./geometry.js";
|
|
2
|
-
import { buildArcTrees,
|
|
3
|
+
import { buildArcTrees, reconstructFeatureSteps } from "./reconstruct.js";
|
|
3
4
|
import { createSpatialIndex } from "./spatial-index.js";
|
|
4
5
|
import { decodePartition, checksum, requiredPartitionKeys, } from "./partition.js";
|
|
5
6
|
/** Progressive session: validates every partition before changing observable state. */
|
|
@@ -66,7 +67,14 @@ export class QuadstreamSession {
|
|
|
66
67
|
missingKeys(level) {
|
|
67
68
|
return requiredPartitionKeys(this.manifest, level).filter((k) => !this.loaded.has(k));
|
|
68
69
|
}
|
|
70
|
+
/** @internal Synchronous reference for algorithm parity tests. */
|
|
71
|
+
querySync(options = {}) {
|
|
72
|
+
return runSync(this.querySteps(options, false));
|
|
73
|
+
}
|
|
69
74
|
query(options = {}) {
|
|
75
|
+
return runCooperative(this.querySteps(options, true), options);
|
|
76
|
+
}
|
|
77
|
+
*querySteps(options, snapshot) {
|
|
70
78
|
const level = options.level ?? this.manifest.maxLevel - 1;
|
|
71
79
|
const missingKeys = this.missingKeys(level);
|
|
72
80
|
if (options.bounds)
|
|
@@ -75,24 +83,33 @@ export class QuadstreamSession {
|
|
|
75
83
|
? this.featureIndex.query(options.bounds)
|
|
76
84
|
: this.manifest.features.map((_, i) => i);
|
|
77
85
|
const allLoaded = this.loaded.size === this.manifest.partitions.length;
|
|
86
|
+
// A yielding query observes the vertices available at its start, even if
|
|
87
|
+
// another request ingests partitions before reconstruction completes.
|
|
88
|
+
const vertices = snapshot && !allLoaded ? new Map(this.vertices) : this.vertices;
|
|
78
89
|
let globallyInvalid = false;
|
|
79
90
|
if (allLoaded && options.validate !== false) {
|
|
80
91
|
const cached = this.validityCache.get(level);
|
|
81
92
|
if (cached !== undefined)
|
|
82
93
|
globallyInvalid = !cached;
|
|
83
94
|
else {
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
95
|
+
for (const f of this.manifest.features) {
|
|
96
|
+
const result = yield* reconstructFeatureSteps(f, this.manifest.arcs, this.tree, level, (id) => {
|
|
97
|
+
const v = vertices.get(id);
|
|
98
|
+
return v ? [v.x, v.y] : undefined;
|
|
99
|
+
});
|
|
100
|
+
if (!result.valid) {
|
|
101
|
+
globallyInvalid = true;
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
88
105
|
this.validityCache.set(level, !globallyInvalid);
|
|
89
106
|
}
|
|
90
107
|
}
|
|
91
108
|
let omittedFeatures = 0, selectedVertices = 0;
|
|
92
109
|
const features = [];
|
|
93
110
|
for (const i of ids) {
|
|
94
|
-
const result =
|
|
95
|
-
const v =
|
|
111
|
+
const result = yield* reconstructFeatureSteps(this.manifest.features[i], this.manifest.arcs, this.tree, globallyInvalid ? Infinity : level, (id) => {
|
|
112
|
+
const v = vertices.get(id);
|
|
96
113
|
return v ? [v.x, v.y] : undefined;
|
|
97
114
|
});
|
|
98
115
|
if (!result.valid || result.missing) {
|
package/dist/spherical-cull.js
CHANGED
|
@@ -18,6 +18,15 @@ function forEachRingCoord(geometry, fn) {
|
|
|
18
18
|
for (const pt of ring)
|
|
19
19
|
fn(pt);
|
|
20
20
|
}
|
|
21
|
+
else if (geometry.type === "LineString") {
|
|
22
|
+
for (const pt of geometry.coordinates)
|
|
23
|
+
fn(pt);
|
|
24
|
+
}
|
|
25
|
+
else if (geometry.type === "MultiLineString") {
|
|
26
|
+
for (const line of geometry.coordinates)
|
|
27
|
+
for (const pt of line)
|
|
28
|
+
fn(pt);
|
|
29
|
+
}
|
|
21
30
|
}
|
|
22
31
|
// Scratch unit vectors let the cap use one trig pass instead of two.
|
|
23
32
|
let scratch = new Float64Array(3 * 4096);
|
|
@@ -38,12 +47,6 @@ export function computeFeatureCap(feature) {
|
|
|
38
47
|
sz += z;
|
|
39
48
|
count++;
|
|
40
49
|
});
|
|
41
|
-
// The original algorithm only visits polygon rings. Non-polygons must
|
|
42
|
-
// remain visible rather than being treated as a point at (0, 0).
|
|
43
|
-
if (!count &&
|
|
44
|
-
feature.geometry.type !== "Polygon" &&
|
|
45
|
-
feature.geometry.type !== "MultiPolygon")
|
|
46
|
-
return { cx: 1, cy: 0, cz: 0, radiusRad: Math.PI };
|
|
47
50
|
if (!count)
|
|
48
51
|
return { cx: 1, cy: 0, cz: 0, radiusRad: 0 };
|
|
49
52
|
const len = Math.hypot(sx, sy, sz);
|
|
@@ -59,6 +62,12 @@ export function computeFeatureCap(feature) {
|
|
|
59
62
|
minDot = dot;
|
|
60
63
|
}
|
|
61
64
|
const maxAngle = Math.acos(Math.max(-1, Math.min(1, minDot)));
|
|
65
|
+
// A cap below a hemisphere contains every minor arc between its vertices.
|
|
66
|
+
// Larger line caps are not geodesically convex: retain those conservatively.
|
|
67
|
+
if ((feature.geometry.type === "LineString" ||
|
|
68
|
+
feature.geometry.type === "MultiLineString") &&
|
|
69
|
+
maxAngle + CAP_MARGIN_RAD >= Math.PI / 2)
|
|
70
|
+
return { cx, cy, cz, radiusRad: Math.PI };
|
|
62
71
|
return {
|
|
63
72
|
cx,
|
|
64
73
|
cy,
|