@spacexr/tiles 0.0.0-stage → 0.1.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 +201 -0
- package/README.md +57 -2
- package/dist/addressing/index.d.ts +16 -0
- package/dist/addressing/index.js +4 -0
- package/dist/addressing/index.js.map +1 -0
- package/dist/bake/index.d.ts +91 -0
- package/dist/bake/index.js +3 -0
- package/dist/bake/index.js.map +1 -0
- package/dist/celestial-dataset-CUIsplKO.d.ts +99 -0
- package/dist/chunk-2HLQXRDD.js +246 -0
- package/dist/chunk-2HLQXRDD.js.map +1 -0
- package/dist/chunk-6YNCRDEE.js +170 -0
- package/dist/chunk-6YNCRDEE.js.map +1 -0
- package/dist/chunk-7AWV2IEK.js +79 -0
- package/dist/chunk-7AWV2IEK.js.map +1 -0
- package/dist/chunk-ESLGXWQR.js +219 -0
- package/dist/chunk-ESLGXWQR.js.map +1 -0
- package/dist/chunk-FB57CZDK.js +312 -0
- package/dist/chunk-FB57CZDK.js.map +1 -0
- package/dist/chunk-GRTG4WNA.js +44 -0
- package/dist/chunk-GRTG4WNA.js.map +1 -0
- package/dist/chunk-HVYN4AIT.js +45 -0
- package/dist/chunk-HVYN4AIT.js.map +1 -0
- package/dist/chunk-IGNRXAW3.js +77 -0
- package/dist/chunk-IGNRXAW3.js.map +1 -0
- package/dist/chunk-QGTREKHS.js +145 -0
- package/dist/chunk-QGTREKHS.js.map +1 -0
- package/dist/chunk-SPDRAY37.js +56 -0
- package/dist/chunk-SPDRAY37.js.map +1 -0
- package/dist/chunk-TA4BEVHL.js +81 -0
- package/dist/chunk-TA4BEVHL.js.map +1 -0
- package/dist/chunk-TGOB5HT5.js +149 -0
- package/dist/chunk-TGOB5HT5.js.map +1 -0
- package/dist/chunk-X6ZJ2PDY.js +104 -0
- package/dist/chunk-X6ZJ2PDY.js.map +1 -0
- package/dist/chunk-XTY4HCPY.js +22 -0
- package/dist/chunk-XTY4HCPY.js.map +1 -0
- package/dist/chunk-Y66PNDG2.js +107 -0
- package/dist/chunk-Y66PNDG2.js.map +1 -0
- package/dist/chunk-YEACOJOC.js +51 -0
- package/dist/chunk-YEACOJOC.js.map +1 -0
- package/dist/cube/index.d.ts +160 -0
- package/dist/cube/index.js +9 -0
- package/dist/cube/index.js.map +1 -0
- package/dist/datasets/index.d.ts +75 -0
- package/dist/datasets/index.js +11 -0
- package/dist/datasets/index.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/mercator/index.d.ts +24 -0
- package/dist/mercator/index.js +8 -0
- package/dist/mercator/index.js.map +1 -0
- package/dist/metrics-HGbzqj6L.d.ts +45 -0
- package/dist/pyramid/index.d.ts +156 -0
- package/dist/pyramid/index.js +4 -0
- package/dist/pyramid/index.js.map +1 -0
- package/dist/raster-registration-CgBxrhDB.d.ts +19 -0
- package/dist/reference-surface-CsTtrNsc.d.ts +49 -0
- package/dist/reprojection/index.d.ts +255 -0
- package/dist/reprojection/index.js +6 -0
- package/dist/reprojection/index.js.map +1 -0
- package/dist/sources/index.d.ts +55 -0
- package/dist/sources/index.js +7 -0
- package/dist/sources/index.js.map +1 -0
- package/dist/surface/index.d.ts +15 -0
- package/dist/surface/index.js +4 -0
- package/dist/surface/index.js.map +1 -0
- package/dist/tile-D-jjIRRA.d.ts +53 -0
- package/dist/tile-address-BkfSftp6.d.ts +66 -0
- package/dist/tile-http-client-BHZOtGX7.d.ts +73 -0
- package/dist/tile-neighbors-LfvV7oiX.d.ts +48 -0
- package/dist/tile-surface-s9Zi6X9_.d.ts +40 -0
- package/dist/tile-topology-B6xHYqPb.d.ts +51 -0
- package/dist/web-mercator-dataset-Dmou8byO.d.ts +125 -0
- package/dist/web-tile-source-DrZDBIm6.d.ts +92 -0
- package/package.json +122 -5
- package/src/addressing/index.ts +8 -0
- package/src/addressing/metrics.ts +48 -0
- package/src/addressing/raster-registration.ts +22 -0
- package/src/addressing/tile-address.ts +130 -0
- package/src/addressing/tile-edge-rotation.ts +13 -0
- package/src/addressing/tile-neighbors.ts +73 -0
- package/src/addressing/tile-surface.ts +44 -0
- package/src/addressing/tile-topology.ts +86 -0
- package/src/addressing/tile.ts +73 -0
- package/src/bake/bake-pyramid.ts +106 -0
- package/src/bake/index.ts +2 -0
- package/src/bake/pyramid-levels.ts +63 -0
- package/src/cube/celestial-dataset.ts +182 -0
- package/src/cube/cube-face.ts +51 -0
- package/src/cube/cube-geometric-error.ts +30 -0
- package/src/cube/cube-mapping.ts +101 -0
- package/src/cube/cube-neighbors.ts +198 -0
- package/src/cube/cube-tile-surface.ts +30 -0
- package/src/cube/index.ts +6 -0
- package/src/datasets/index.ts +1 -0
- package/src/datasets/tile-dataset.ts +121 -0
- package/src/index.ts +9 -0
- package/src/mercator/index.ts +3 -0
- package/src/mercator/web-mercator-dataset.ts +215 -0
- package/src/mercator/web-mercator-tile-metrics.ts +119 -0
- package/src/mercator/web-mercator-tile-surface.ts +52 -0
- package/src/pyramid/index.ts +2 -0
- package/src/pyramid/resident-tiles.ts +81 -0
- package/src/pyramid/tile-pyramid.ts +285 -0
- package/src/reprojection/cube-resampling.ts +159 -0
- package/src/reprojection/index.ts +4 -0
- package/src/reprojection/source-projection.ts +196 -0
- package/src/reprojection/source-tile-matrix.ts +100 -0
- package/src/reprojection/wmts-capabilities.ts +112 -0
- package/src/sources/index.ts +4 -0
- package/src/sources/tile-http-client.ts +154 -0
- package/src/sources/tile-layer-changes.ts +88 -0
- package/src/sources/url-template.ts +96 -0
- package/src/sources/web-tile-source.ts +73 -0
- package/src/surface/directions.ts +27 -0
- package/src/surface/index.ts +2 -0
- package/src/surface/reference-surface.ts +88 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { isValidTileAddress, type ITileAddress } from "../addressing/tile-address";
|
|
2
|
+
import { TileHttpClient, type ITileHttpClientOptions, type TileFetchResult } from "../sources/tile-http-client";
|
|
3
|
+
import { expandTileUrlTemplate, validateTileUrlTemplate } from "../sources/url-template";
|
|
4
|
+
import { CubeTileSurface } from "./cube-tile-surface";
|
|
5
|
+
import { ReferenceSurface } from "../surface/reference-surface";
|
|
6
|
+
|
|
7
|
+
/** Scheme of the datasets of this version of the specification. */
|
|
8
|
+
export const CELESTIAL_CUBE_SCHEME = "spacexr-cube-equiangular/1";
|
|
9
|
+
|
|
10
|
+
/** A tile below which levels down to its own `maxLOD` are available, such as the polar caps of the Moon. */
|
|
11
|
+
export interface ICelestialDatasetSubtree {
|
|
12
|
+
readonly face: number;
|
|
13
|
+
readonly lod: number;
|
|
14
|
+
readonly x: number;
|
|
15
|
+
readonly y: number;
|
|
16
|
+
readonly maxLOD: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Descriptor of a dataset of a tile service; see `docs/celestial-tiling.md`. */
|
|
20
|
+
export interface ICelestialDataset {
|
|
21
|
+
/** This specification and its version: {@link CELESTIAL_CUBE_SCHEME}. */
|
|
22
|
+
readonly scheme: string;
|
|
23
|
+
/** Catalogue name of the body. */
|
|
24
|
+
readonly body: string;
|
|
25
|
+
/** Body-fixed frame and its version. */
|
|
26
|
+
readonly frame: string;
|
|
27
|
+
/** Radii `a`, `b`, `c` of the reference surface, in metres. */
|
|
28
|
+
readonly referenceRadii: readonly [number, number, number];
|
|
29
|
+
readonly content: "elevation" | "imagery";
|
|
30
|
+
/** Tile format, such as `terrarium` for radial heights or `png`, `jpeg`, `webp` for imagery. */
|
|
31
|
+
readonly encoding: string;
|
|
32
|
+
/** Cells per tile side: tiles hold `tileSize + 1` samples per side. */
|
|
33
|
+
readonly tileSize: number;
|
|
34
|
+
/** Levels available everywhere, from `minLOD` to `maxLOD`. */
|
|
35
|
+
readonly minLOD: number;
|
|
36
|
+
readonly maxLOD: number;
|
|
37
|
+
readonly subtrees: readonly ICelestialDatasetSubtree[];
|
|
38
|
+
/** URL template. */
|
|
39
|
+
readonly tiles: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function fail(message: string): never {
|
|
43
|
+
throw new RangeError(`Invalid celestial dataset: ${message}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function text(value: Record<string, unknown>, name: string): string {
|
|
47
|
+
const member = value[name];
|
|
48
|
+
if (typeof member !== "string" || member.length === 0) fail(`${name} must be a non-empty string.`);
|
|
49
|
+
return member;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function level(value: Record<string, unknown>, name: string): number {
|
|
53
|
+
const member = value[name];
|
|
54
|
+
if (!Number.isInteger(member) || (member as number) < 0 || (member as number) > 52) fail(`${name} must be a level from 0 to 52.`);
|
|
55
|
+
return member as number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Checks the JSON value of a dataset descriptor.
|
|
60
|
+
*
|
|
61
|
+
* @returns The descriptor, with an empty `subtrees` when it has none.
|
|
62
|
+
* @throws RangeError When a member is missing or invalid, naming it. A scheme of another version is rejected.
|
|
63
|
+
* @throws TileUrlTemplateError When the URL template uses an unknown variable.
|
|
64
|
+
*/
|
|
65
|
+
export function parseCelestialDataset(value: unknown): ICelestialDataset {
|
|
66
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) fail("the descriptor must be an object.");
|
|
67
|
+
const source = value as Record<string, unknown>;
|
|
68
|
+
const scheme = text(source, "scheme");
|
|
69
|
+
if (scheme !== CELESTIAL_CUBE_SCHEME) fail(`scheme ${scheme} is not supported, expected ${CELESTIAL_CUBE_SCHEME}.`);
|
|
70
|
+
const radii = source.referenceRadii;
|
|
71
|
+
if (!Array.isArray(radii) || radii.length !== 3 || radii.some((radius) => typeof radius !== "number" || !Number.isFinite(radius) || radius <= 0)) {
|
|
72
|
+
fail("referenceRadii must be three positive numbers of metres.");
|
|
73
|
+
}
|
|
74
|
+
const content = source.content;
|
|
75
|
+
if (content !== "elevation" && content !== "imagery") fail("content must be elevation or imagery.");
|
|
76
|
+
const tileSize = source.tileSize;
|
|
77
|
+
if (!Number.isInteger(tileSize) || (tileSize as number) < 1) fail("tileSize must be a positive integer.");
|
|
78
|
+
const minLOD = level(source, "minLOD");
|
|
79
|
+
const maxLOD = level(source, "maxLOD");
|
|
80
|
+
if (minLOD > maxLOD) fail("minLOD must not exceed maxLOD.");
|
|
81
|
+
const rawSubtrees = source.subtrees ?? [];
|
|
82
|
+
if (!Array.isArray(rawSubtrees)) fail("subtrees must be an array.");
|
|
83
|
+
const subtrees = rawSubtrees.map((raw: unknown): ICelestialDatasetSubtree => {
|
|
84
|
+
if (typeof raw !== "object" || raw === null) fail("each subtree must be an object.");
|
|
85
|
+
const subtree = raw as Record<string, unknown>;
|
|
86
|
+
const address = { face: subtree.face as number, lod: subtree.lod as number, x: subtree.x as number, y: subtree.y as number };
|
|
87
|
+
if (subtree.face === undefined || !isValidTileAddress(address)) fail("each subtree needs a valid face, lod, x and y.");
|
|
88
|
+
const subtreeMaxLOD = level(subtree, "maxLOD");
|
|
89
|
+
if (subtreeMaxLOD < address.lod) fail("the maxLOD of a subtree must reach at least its lod.");
|
|
90
|
+
return { face: address.face, lod: address.lod, x: address.x, y: address.y, maxLOD: subtreeMaxLOD };
|
|
91
|
+
});
|
|
92
|
+
const tiles = text(source, "tiles");
|
|
93
|
+
validateTileUrlTemplate(tiles);
|
|
94
|
+
return {
|
|
95
|
+
scheme,
|
|
96
|
+
body: text(source, "body"),
|
|
97
|
+
frame: text(source, "frame"),
|
|
98
|
+
referenceRadii: [radii[0] as number, radii[1] as number, radii[2] as number],
|
|
99
|
+
content,
|
|
100
|
+
encoding: text(source, "encoding"),
|
|
101
|
+
tileSize: tileSize as number,
|
|
102
|
+
minLOD,
|
|
103
|
+
maxLOD,
|
|
104
|
+
subtrees,
|
|
105
|
+
tiles,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Options of {@link CelestialTileSource}. */
|
|
110
|
+
export interface ICelestialTileSourceOptions {
|
|
111
|
+
/** Shared client, or options used to create a dedicated one. */
|
|
112
|
+
client?: TileHttpClient | ITileHttpClientOptions;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Tiles of a dataset of a tile service, over HTTP. It never requests a tile outside the declared availability, which
|
|
117
|
+
* resolves as absent, so that a sparse pyramid costs no failed request.
|
|
118
|
+
*/
|
|
119
|
+
export class CelestialTileSource {
|
|
120
|
+
public readonly dataset: ICelestialDataset;
|
|
121
|
+
/** Surface of the tiles of the dataset, on its reference surface. */
|
|
122
|
+
public readonly surface: CubeTileSurface;
|
|
123
|
+
/** Deepest level available anywhere. */
|
|
124
|
+
public readonly deepestLOD: number;
|
|
125
|
+
public readonly client: TileHttpClient;
|
|
126
|
+
/** Deepest level of the subtree rooted at each tile. */
|
|
127
|
+
private readonly subtreeDepths = new Map<string, number>();
|
|
128
|
+
|
|
129
|
+
/** @param dataset - The descriptor, or its JSON value, checked by {@link parseCelestialDataset}. */
|
|
130
|
+
public constructor(dataset: ICelestialDataset | unknown, options: ICelestialTileSourceOptions = {}) {
|
|
131
|
+
this.dataset = parseCelestialDataset(dataset);
|
|
132
|
+
const [a, b, c] = this.dataset.referenceRadii;
|
|
133
|
+
this.surface = new CubeTileSurface(new ReferenceSurface(a, b, c));
|
|
134
|
+
this.deepestLOD = this.dataset.subtrees.reduce((deepest, subtree) => Math.max(deepest, subtree.maxLOD), this.dataset.maxLOD);
|
|
135
|
+
this.client = options.client instanceof TileHttpClient ? options.client : new TileHttpClient(options.client);
|
|
136
|
+
for (const { face, lod, x, y, maxLOD } of this.dataset.subtrees) {
|
|
137
|
+
const root = `${face}/${lod}/${x}/${y}`;
|
|
138
|
+
this.subtreeDepths.set(root, Math.max(maxLOD, this.subtreeDepths.get(root) ?? 0));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Deepest level of the dataset at the place of a tile, at most the level of the tile: the level of the tile itself
|
|
144
|
+
* where the dataset declares it, of its deepest declared ancestor elsewhere.
|
|
145
|
+
*/
|
|
146
|
+
public servingLevel(address: ITileAddress): number {
|
|
147
|
+
const everywhere = Math.min(address.lod, this.dataset.maxLOD);
|
|
148
|
+
if (address.lod <= this.dataset.maxLOD || this.subtreeDepths.size === 0) return everywhere;
|
|
149
|
+
const face = address.face ?? 0;
|
|
150
|
+
let best = everywhere;
|
|
151
|
+
for (let lod = Math.min(address.lod, this.deepestLOD); lod >= 0; lod--) {
|
|
152
|
+
const shift = address.lod - lod;
|
|
153
|
+
const depth = this.subtreeDepths.get(`${face}/${lod}/${Math.floor(address.x / 2 ** shift)}/${Math.floor(address.y / 2 ** shift)}`);
|
|
154
|
+
if (depth !== undefined) best = Math.max(best, Math.min(depth, address.lod));
|
|
155
|
+
if (best === address.lod) break;
|
|
156
|
+
}
|
|
157
|
+
return best;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Whether the dataset declares a tile. */
|
|
161
|
+
public isAvailable(address: ITileAddress): boolean {
|
|
162
|
+
if (!isValidTileAddress(address)) return false;
|
|
163
|
+
if (address.lod < this.dataset.minLOD) return false;
|
|
164
|
+
return this.servingLevel(address) === address.lod;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Returns the URL of a tile, whether it is available or not. */
|
|
168
|
+
public urlOf(address: ITileAddress): string {
|
|
169
|
+
return expandTileUrlTemplate(this.dataset.tiles, address);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Fetches a tile; a tile outside the declared availability resolves as absent, like a 404, without any request.
|
|
174
|
+
*
|
|
175
|
+
* @throws RangeError When the address is invalid.
|
|
176
|
+
*/
|
|
177
|
+
public async fetchTile(address: ITileAddress, signal?: AbortSignal): Promise<TileFetchResult> {
|
|
178
|
+
const url = this.urlOf(address);
|
|
179
|
+
if (!this.isAvailable(address)) return { status: "absent", url, httpStatus: 404 };
|
|
180
|
+
return this.client.fetchTile(url, signal);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ICartesian3 } from "@spacexr/geodesy";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Face of the cube index of a celestial body. Equatorial faces are numbered eastward from the prime meridian; the
|
|
5
|
+
* polar faces follow. See the repository document `docs/celestial-tiling.md`.
|
|
6
|
+
*/
|
|
7
|
+
export enum CubeFace {
|
|
8
|
+
/** Centred on latitude 0, longitude 0, the `+X` axis. */
|
|
9
|
+
PositiveX = 0,
|
|
10
|
+
/** Centred on latitude 0, longitude 90 degrees east, the `+Y` axis. */
|
|
11
|
+
PositiveY = 1,
|
|
12
|
+
/** Centred on latitude 0, longitude 180 degrees, the `-X` axis. */
|
|
13
|
+
NegativeX = 2,
|
|
14
|
+
/** Centred on latitude 0, longitude 90 degrees west, the `-Y` axis. */
|
|
15
|
+
NegativeY = 3,
|
|
16
|
+
/** Centred on the north pole, the `+Z` axis. */
|
|
17
|
+
North = 4,
|
|
18
|
+
/** Centred on the south pole, the `-Z` axis. */
|
|
19
|
+
South = 5,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Number of faces of the cube index. */
|
|
23
|
+
export const CUBE_FACE_COUNT = 6;
|
|
24
|
+
|
|
25
|
+
/** Frame of a cube face: its centre and the directions in which its `u` and `v` coordinates grow. */
|
|
26
|
+
export interface ICubeFaceFrame {
|
|
27
|
+
readonly center: Readonly<ICartesian3>;
|
|
28
|
+
readonly u: Readonly<ICartesian3>;
|
|
29
|
+
readonly v: Readonly<ICartesian3>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const axis = (x: number, y: number, z: number): Readonly<ICartesian3> => Object.freeze({ x, y, z });
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Frames of the six faces, indexed by {@link CubeFace}. On equatorial faces `u` grows eastward and `v` southward; the
|
|
36
|
+
* polar faces continue face 0 without rotation. On every face `u x v = -center`: seen from outside the body, `u` runs
|
|
37
|
+
* to the right and `v` downward, like the columns and rows of an image.
|
|
38
|
+
*/
|
|
39
|
+
export const CUBE_FACE_FRAMES: readonly ICubeFaceFrame[] = Object.freeze([
|
|
40
|
+
{ center: axis(1, 0, 0), u: axis(0, 1, 0), v: axis(0, 0, -1) },
|
|
41
|
+
{ center: axis(0, 1, 0), u: axis(-1, 0, 0), v: axis(0, 0, -1) },
|
|
42
|
+
{ center: axis(-1, 0, 0), u: axis(0, -1, 0), v: axis(0, 0, -1) },
|
|
43
|
+
{ center: axis(0, -1, 0), u: axis(1, 0, 0), v: axis(0, 0, -1) },
|
|
44
|
+
{ center: axis(0, 0, 1), u: axis(0, 1, 0), v: axis(1, 0, 0) },
|
|
45
|
+
{ center: axis(0, 0, -1), u: axis(0, 1, 0), v: axis(-1, 0, 0) },
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
/** Returns `true` for an integer face of the cube index. */
|
|
49
|
+
export function isCubeFace(face: number): face is CubeFace {
|
|
50
|
+
return Number.isInteger(face) && face >= 0 && face < CUBE_FACE_COUNT;
|
|
51
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ITileAddress } from "../addressing/tile-address";
|
|
2
|
+
import type { ReferenceSurface } from "../surface/reference-surface";
|
|
3
|
+
import { cubeTileDirection } from "./cube-mapping";
|
|
4
|
+
|
|
5
|
+
const CORNERS: ReadonlyArray<readonly [number, number]> = [
|
|
6
|
+
[0, 0],
|
|
7
|
+
[1, 0],
|
|
8
|
+
[1, 1],
|
|
9
|
+
[0, 1],
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Geometric error of a terrain tile of the cube index: the longest of its four edges, measured as the chord between
|
|
14
|
+
* its corners on the reference surface, divided by the cells of a tile side. It follows the distortion of the cube
|
|
15
|
+
* instead of assuming one size per level.
|
|
16
|
+
*
|
|
17
|
+
* @param tileSize - Cells per tile side.
|
|
18
|
+
* @throws RangeError When the address is invalid or the tile size is not positive.
|
|
19
|
+
*/
|
|
20
|
+
export function cubeTileGeometricError(address: ITileAddress, surface: ReferenceSurface, tileSize: number): number {
|
|
21
|
+
if (!(tileSize > 0)) throw new RangeError("The tile size must be positive.");
|
|
22
|
+
const points = CORNERS.map(([s, t]) => surface.point(cubeTileDirection(address, s, t)));
|
|
23
|
+
let longest = 0;
|
|
24
|
+
for (let index = 0; index < 4; index++) {
|
|
25
|
+
const a = points[index]!;
|
|
26
|
+
const b = points[(index + 1) % 4]!;
|
|
27
|
+
longest = Math.max(longest, Math.hypot(a.x - b.x, a.y - b.y, a.z - b.z));
|
|
28
|
+
}
|
|
29
|
+
return longest / tileSize;
|
|
30
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { ICartesian3 } from "@spacexr/geodesy";
|
|
2
|
+
|
|
3
|
+
import { assertValidTileAddress, tileCount, type ITileAddress } from "../addressing/tile-address";
|
|
4
|
+
import { CUBE_FACE_FRAMES, CubeFace } from "./cube-face";
|
|
5
|
+
|
|
6
|
+
const QUARTER_TURN = Math.PI / 4;
|
|
7
|
+
|
|
8
|
+
/** Position on a face of the cube index: `u` and `v` in `[0, 1]`, from the north-west corner of the face. */
|
|
9
|
+
export interface ICubeFaceCoordinates {
|
|
10
|
+
face: CubeFace;
|
|
11
|
+
u: number;
|
|
12
|
+
v: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function normalize(x: number, y: number, z: number, target: ICartesian3): ICartesian3 {
|
|
16
|
+
const length = Math.hypot(x, y, z);
|
|
17
|
+
target.x = x / length;
|
|
18
|
+
target.y = y / length;
|
|
19
|
+
target.z = z / length;
|
|
20
|
+
return target;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Direction of a point of a face, with the equi-angular mapping: `normalize(C + tan(pi/4 (2u - 1)) U + tan(pi/4
|
|
25
|
+
* (2v - 1)) V)`. Coordinates outside `[0, 1]` extend the face plane, up to `-0.5` and `1.5` exclusive.
|
|
26
|
+
*
|
|
27
|
+
* @param face - Face of the cube index.
|
|
28
|
+
* @param u - Coordinate along the face `u` axis, 0 on its west edge.
|
|
29
|
+
* @param v - Coordinate along the face `v` axis, 0 on its north edge.
|
|
30
|
+
* @param target - Receives the unit direction.
|
|
31
|
+
*/
|
|
32
|
+
export function cubeFaceDirection(face: CubeFace, u: number, v: number, target: ICartesian3 = { x: 0, y: 0, z: 0 }): ICartesian3 {
|
|
33
|
+
const frame = CUBE_FACE_FRAMES[face];
|
|
34
|
+
if (!frame) throw new RangeError(`Cube face ${face} does not exist.`);
|
|
35
|
+
const a = Math.tan(QUARTER_TURN * (2 * u - 1));
|
|
36
|
+
const b = Math.tan(QUARTER_TURN * (2 * v - 1));
|
|
37
|
+
const { center: c, u: du, v: dv } = frame;
|
|
38
|
+
return normalize(c.x + a * du.x + b * dv.x, c.y + a * du.y + b * dv.y, c.z + a * du.z + b * dv.z, target);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Face of a direction: the face of its largest absolute component, its sign giving the face. Ties go to the first
|
|
43
|
+
* axis in the order `X`, `Y`, `Z`, so a direction on a cube edge belongs to an equatorial face.
|
|
44
|
+
*
|
|
45
|
+
* @param direction - Any non-zero vector; it does not need to be normalized.
|
|
46
|
+
* @throws RangeError When the direction is zero or not finite.
|
|
47
|
+
*/
|
|
48
|
+
export function cubeFaceOfDirection(direction: Readonly<ICartesian3>): CubeFace {
|
|
49
|
+
const { x, y, z } = direction;
|
|
50
|
+
const ax = Math.abs(x);
|
|
51
|
+
const ay = Math.abs(y);
|
|
52
|
+
const az = Math.abs(z);
|
|
53
|
+
if (!(ax + ay + az > 0) || !Number.isFinite(ax + ay + az)) throw new RangeError("A cube direction must be finite and non-zero.");
|
|
54
|
+
if (ax >= ay && ax >= az) return x > 0 ? CubeFace.PositiveX : CubeFace.NegativeX;
|
|
55
|
+
if (ay >= az) return y > 0 ? CubeFace.PositiveY : CubeFace.NegativeY;
|
|
56
|
+
return z > 0 ? CubeFace.North : CubeFace.South;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Face and face coordinates of a direction, the inverse of {@link cubeFaceDirection}.
|
|
61
|
+
*
|
|
62
|
+
* @param direction - Any non-zero vector; it does not need to be normalized.
|
|
63
|
+
* @throws RangeError When the direction is zero or not finite.
|
|
64
|
+
*/
|
|
65
|
+
export function cubeFaceCoordinates(direction: Readonly<ICartesian3>): ICubeFaceCoordinates {
|
|
66
|
+
const face = cubeFaceOfDirection(direction);
|
|
67
|
+
const { center, u, v } = CUBE_FACE_FRAMES[face]!;
|
|
68
|
+
const c = direction.x * center.x + direction.y * center.y + direction.z * center.z;
|
|
69
|
+
const a = (direction.x * u.x + direction.y * u.y + direction.z * u.z) / c;
|
|
70
|
+
const b = (direction.x * v.x + direction.y * v.y + direction.z * v.z) / c;
|
|
71
|
+
return { face, u: (Math.atan(a) / QUARTER_TURN + 1) / 2, v: (Math.atan(b) / QUARTER_TURN + 1) / 2 };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Tile of a direction at a level of detail: `floor(u * 2^lod)` and `floor(v * 2^lod)` on its face, clamped so that
|
|
76
|
+
* the east and south edges of the face belong to its last tiles.
|
|
77
|
+
*
|
|
78
|
+
* @throws RangeError When the direction is zero or not finite, or the level is invalid.
|
|
79
|
+
*/
|
|
80
|
+
export function cubeTileOfDirection(direction: Readonly<ICartesian3>, lod: number): ITileAddress {
|
|
81
|
+
const { face, u, v } = cubeFaceCoordinates(direction);
|
|
82
|
+
const count = tileCount(lod);
|
|
83
|
+
const address = { face, lod, x: Math.min(count - 1, Math.floor(u * count)), y: Math.min(count - 1, Math.floor(v * count)) };
|
|
84
|
+
assertValidTileAddress(address);
|
|
85
|
+
return address;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Direction of a point of a tile.
|
|
90
|
+
*
|
|
91
|
+
* @param address - Tile of the cube index; a missing face means face 0.
|
|
92
|
+
* @param s - Coordinate in the tile, 0 on its west edge and 1 on its east edge.
|
|
93
|
+
* @param t - Coordinate in the tile, 0 on its north edge and 1 on its south edge.
|
|
94
|
+
* @param target - Receives the unit direction.
|
|
95
|
+
* @throws RangeError When the address is invalid.
|
|
96
|
+
*/
|
|
97
|
+
export function cubeTileDirection(address: ITileAddress, s: number, t: number, target?: ICartesian3): ICartesian3 {
|
|
98
|
+
assertValidTileAddress(address);
|
|
99
|
+
const count = tileCount(address.lod);
|
|
100
|
+
return cubeFaceDirection(address.face ?? 0, (address.x + s) / count, (address.y + t) / count, target);
|
|
101
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { assertValidTileAddress, tileCount, type ITileAddress } from "../addressing/tile-address";
|
|
2
|
+
import { TILE_CORNER_EDGES, TILE_EDGE_NEIGHBORS, TileNeighbor, tileNeighborOffset, type TileCornerNeighbor, type TileEdgeNeighbor } from "../addressing/tile-neighbors";
|
|
3
|
+
import { oppositeTileEdge } from "../addressing/tile-edge-rotation";
|
|
4
|
+
import type { ITileAreaAcross, ITileTopology } from "../addressing/tile-topology";
|
|
5
|
+
import { CubeFace } from "./cube-face";
|
|
6
|
+
|
|
7
|
+
/** Edge of the neighbouring face met by an edge of a face. */
|
|
8
|
+
export interface ICubeEdgeJunction {
|
|
9
|
+
/** Neighbouring face. */
|
|
10
|
+
readonly face: CubeFace;
|
|
11
|
+
/** Edge of the neighbouring face. */
|
|
12
|
+
readonly edge: TileEdgeNeighbor;
|
|
13
|
+
/** `true` when the coordinate along the edge runs the other way on the neighbouring face. */
|
|
14
|
+
readonly reversed: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Quarter turns from the frame of the face to the frame of its neighbour: a direction `d` of the face is
|
|
17
|
+
* `turnTileEdge(d, turns)` on the neighbour. Equatorial faces join with 0.
|
|
18
|
+
*/
|
|
19
|
+
readonly turns: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const { N, E, S, W } = TileNeighbor;
|
|
23
|
+
/** Neighbouring face, its edge and the reversal, for the edges N, E, S, W of each face; see `docs/celestial-tiling.md`. */
|
|
24
|
+
const EDGE_TABLE: ReadonlyArray<
|
|
25
|
+
readonly [
|
|
26
|
+
readonly [CubeFace, TileEdgeNeighbor, boolean],
|
|
27
|
+
readonly [CubeFace, TileEdgeNeighbor, boolean],
|
|
28
|
+
readonly [CubeFace, TileEdgeNeighbor, boolean],
|
|
29
|
+
readonly [CubeFace, TileEdgeNeighbor, boolean],
|
|
30
|
+
]
|
|
31
|
+
> = [
|
|
32
|
+
[
|
|
33
|
+
[4, S, false],
|
|
34
|
+
[1, W, false],
|
|
35
|
+
[5, N, false],
|
|
36
|
+
[3, E, false],
|
|
37
|
+
],
|
|
38
|
+
[
|
|
39
|
+
[4, E, true],
|
|
40
|
+
[2, W, false],
|
|
41
|
+
[5, E, false],
|
|
42
|
+
[0, E, false],
|
|
43
|
+
],
|
|
44
|
+
[
|
|
45
|
+
[4, N, true],
|
|
46
|
+
[3, W, false],
|
|
47
|
+
[5, S, true],
|
|
48
|
+
[1, E, false],
|
|
49
|
+
],
|
|
50
|
+
[
|
|
51
|
+
[4, W, false],
|
|
52
|
+
[0, W, false],
|
|
53
|
+
[5, W, true],
|
|
54
|
+
[2, E, false],
|
|
55
|
+
],
|
|
56
|
+
[
|
|
57
|
+
[2, N, true],
|
|
58
|
+
[1, N, true],
|
|
59
|
+
[0, N, false],
|
|
60
|
+
[3, N, false],
|
|
61
|
+
],
|
|
62
|
+
[
|
|
63
|
+
[0, S, false],
|
|
64
|
+
[1, S, false],
|
|
65
|
+
[2, S, true],
|
|
66
|
+
[3, S, true],
|
|
67
|
+
],
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Junctions of the faces of the cube index, indexed by face then edge. The neighbour's edge is the opposite of the
|
|
72
|
+
* face's edge turned by `turns`.
|
|
73
|
+
*/
|
|
74
|
+
export const CUBE_FACE_EDGES: ReadonlyArray<Readonly<Record<TileEdgeNeighbor, ICubeEdgeJunction>>> = Object.freeze(
|
|
75
|
+
EDGE_TABLE.map((row) => {
|
|
76
|
+
const junctions = {} as Record<TileEdgeNeighbor, ICubeEdgeJunction>;
|
|
77
|
+
row.forEach(([face, edge, reversed], index) => {
|
|
78
|
+
const own = TILE_EDGE_NEIGHBORS[index]!;
|
|
79
|
+
const turns = (TILE_EDGE_NEIGHBORS.indexOf(oppositeTileEdge(edge)) - index + 4) % 4;
|
|
80
|
+
junctions[own] = Object.freeze({ face, edge, reversed, turns });
|
|
81
|
+
});
|
|
82
|
+
return Object.freeze(junctions);
|
|
83
|
+
})
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
/** Neighbour of a tile of the cube index, with the turns from the frame of the tile to the frame of the neighbour. */
|
|
87
|
+
export interface ICubeTileNeighbor {
|
|
88
|
+
readonly address: ITileAddress;
|
|
89
|
+
/** Quarter turns, as {@link ICubeEdgeJunction.turns}; 0 on the same face. */
|
|
90
|
+
readonly turns: number;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Tile across an edge of the face at position `k` along that edge, at a level of `count` tiles per axis. */
|
|
94
|
+
function acrossFaceEdge(face: CubeFace, edge: TileEdgeNeighbor, k: number, lod: number, count: number): ICubeTileNeighbor {
|
|
95
|
+
const junction = CUBE_FACE_EDGES[face]![edge];
|
|
96
|
+
const along = junction.reversed ? count - 1 - k : k;
|
|
97
|
+
let x = along;
|
|
98
|
+
let y = along;
|
|
99
|
+
if (junction.edge === TileNeighbor.N) y = 0;
|
|
100
|
+
else if (junction.edge === TileNeighbor.S) y = count - 1;
|
|
101
|
+
else if (junction.edge === TileNeighbor.W) x = 0;
|
|
102
|
+
else x = count - 1;
|
|
103
|
+
return { address: { face: junction.face, lod, x, y }, turns: junction.turns };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Neighbour of a tile of the cube index at the same level, across the faces when needed.
|
|
108
|
+
*
|
|
109
|
+
* A corner neighbour is reached by crossing one of its two edges. At the 8 vertices of the cube only 3 tiles meet:
|
|
110
|
+
* the corner neighbour there does not exist and the result is `undefined`.
|
|
111
|
+
*
|
|
112
|
+
* @param address - Tile of the cube index; a missing face means face 0.
|
|
113
|
+
* @param neighbor - Position around the tile; `C` returns the tile itself.
|
|
114
|
+
* @throws RangeError When the address is invalid.
|
|
115
|
+
*/
|
|
116
|
+
export function cubeTileNeighbor(address: ITileAddress, neighbor: TileNeighbor): ICubeTileNeighbor | undefined {
|
|
117
|
+
assertValidTileAddress(address);
|
|
118
|
+
const face: CubeFace = address.face ?? CubeFace.PositiveX;
|
|
119
|
+
const { lod } = address;
|
|
120
|
+
const count = tileCount(lod);
|
|
121
|
+
const [dx, dy] = tileNeighborOffset(neighbor);
|
|
122
|
+
const x = address.x + dx;
|
|
123
|
+
const y = address.y + dy;
|
|
124
|
+
const outX = x < 0 || x >= count;
|
|
125
|
+
const outY = y < 0 || y >= count;
|
|
126
|
+
if (outX && outY) return undefined;
|
|
127
|
+
if (!outX && !outY) return { address: { face, lod, x, y }, turns: 0 };
|
|
128
|
+
if (outY) return acrossFaceEdge(face, y < 0 ? TileNeighbor.N : TileNeighbor.S, x, lod, count);
|
|
129
|
+
return acrossFaceEdge(face, x < 0 ? TileNeighbor.W : TileNeighbor.E, y, lod, count);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Returns the nine neighbours in {@link TileNeighbor} order, the tile itself at `TileNeighbor.C`. */
|
|
133
|
+
export function cubeTileNeighbors(address: ITileAddress): Array<ICubeTileNeighbor | undefined> {
|
|
134
|
+
return Array.from({ length: 9 }, (_, neighbor) => cubeTileNeighbor(address, neighbor));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Number of tiles meeting at a corner of a tile: 3 at the vertices of the cube, 4 elsewhere.
|
|
139
|
+
*
|
|
140
|
+
* @throws RangeError When the address is invalid.
|
|
141
|
+
*/
|
|
142
|
+
export function cubeCornerTileCount(address: ITileAddress, corner: TileCornerNeighbor): 3 | 4 {
|
|
143
|
+
assertValidTileAddress(address);
|
|
144
|
+
const count = tileCount(address.lod);
|
|
145
|
+
const [vertical, horizontal] = TILE_CORNER_EDGES[corner];
|
|
146
|
+
const onRow = vertical === TileNeighbor.N ? address.y === 0 : address.y === count - 1;
|
|
147
|
+
const onColumn = horizontal === TileNeighbor.W ? address.x === 0 : address.x === count - 1;
|
|
148
|
+
return onRow && onColumn ? 3 : 4;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Corner of a tile that `turnedTileCoordinates` sends to `(0, 0)`, per number of quarter turns. */
|
|
152
|
+
const TURNED_ORIGIN: ReadonlyArray<readonly [number, number]> = [
|
|
153
|
+
[0, 0],
|
|
154
|
+
[0, 1],
|
|
155
|
+
[1, 1],
|
|
156
|
+
[1, 0],
|
|
157
|
+
];
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Area of a tile of the cube index in another tile, across at most one face edge; see {@link ITileAreaAcross}.
|
|
161
|
+
*
|
|
162
|
+
* The neighbouring face is unfolded around the edge the two faces share, a rigid motion, so the points of that edge
|
|
163
|
+
* land exactly on the same directions.
|
|
164
|
+
*
|
|
165
|
+
* @throws RangeError When an address is invalid or the tiles lie on opposite faces.
|
|
166
|
+
*/
|
|
167
|
+
export function cubeTileAreaIn(address: ITileAddress, other: ITileAddress): ITileAreaAcross {
|
|
168
|
+
assertValidTileAddress(address);
|
|
169
|
+
assertValidTileAddress(other);
|
|
170
|
+
const face: CubeFace = address.face ?? 0;
|
|
171
|
+
const otherFace: CubeFace = other.face ?? 0;
|
|
172
|
+
const scale = 2 ** (address.lod - other.lod);
|
|
173
|
+
const size = 1 / scale;
|
|
174
|
+
if (face === otherFace) return { west: address.x / scale - other.x, north: address.y / scale - other.y, size, turns: 0 };
|
|
175
|
+
|
|
176
|
+
const edge = TILE_EDGE_NEIGHBORS.find((candidate) => CUBE_FACE_EDGES[face]![candidate].face === otherFace);
|
|
177
|
+
if (edge === undefined) throw new RangeError(`Faces ${face} and ${otherFace} are opposite: their tiles cannot touch.`);
|
|
178
|
+
const junction = CUBE_FACE_EDGES[face]![edge];
|
|
179
|
+
// The corner of the tile at the origin of the turned frame.
|
|
180
|
+
const [cornerU, cornerV] = TURNED_ORIGIN[junction.turns]!;
|
|
181
|
+
const count = tileCount(address.lod);
|
|
182
|
+
const u = (address.x + cornerU) / count;
|
|
183
|
+
const v = (address.y + cornerV) / count;
|
|
184
|
+
const along = edge === TileNeighbor.N || edge === TileNeighbor.S ? u : v;
|
|
185
|
+
const beyond = edge === TileNeighbor.N ? -v : edge === TileNeighbor.S ? v - 1 : edge === TileNeighbor.W ? -u : u - 1;
|
|
186
|
+
const alongOther = junction.reversed ? 1 - along : along;
|
|
187
|
+
let otherU = alongOther;
|
|
188
|
+
let otherV = alongOther;
|
|
189
|
+
if (junction.edge === TileNeighbor.N) otherV = beyond;
|
|
190
|
+
else if (junction.edge === TileNeighbor.S) otherV = 1 - beyond;
|
|
191
|
+
else if (junction.edge === TileNeighbor.W) otherU = beyond;
|
|
192
|
+
else otherU = 1 - beyond;
|
|
193
|
+
const otherCount = tileCount(other.lod);
|
|
194
|
+
return { west: otherU * otherCount - other.x, north: otherV * otherCount - other.y, size, turns: junction.turns };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Topology of the cube index: neighbours across faces, turned, and corners of 3 tiles. */
|
|
198
|
+
export const CUBE_TILE_TOPOLOGY: ITileTopology = Object.freeze({ neighbor: cubeTileNeighbor, areaIn: cubeTileAreaIn });
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ICartesian3 } from "@spacexr/geodesy";
|
|
2
|
+
|
|
3
|
+
import { assertValidTileAddress, formatTileAddress, type ITileAddress } from "../addressing/tile-address";
|
|
4
|
+
import type { ITileShapeFrame, ITileSurface } from "../addressing/tile-surface";
|
|
5
|
+
import { cubeTileDirection } from "./cube-mapping";
|
|
6
|
+
import type { ReferenceSurface } from "../surface/reference-surface";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Tiles of the cube index on a reference surface. Heights are radial: every point lies on the ray of its direction.
|
|
10
|
+
*
|
|
11
|
+
* Each tile has its own shape. The same address on another face is the same shape on a sphere only, and is never in
|
|
12
|
+
* view together with the tile, so sharing would save nothing.
|
|
13
|
+
*/
|
|
14
|
+
export class CubeTileSurface implements ITileSurface {
|
|
15
|
+
/** @param reference - Reference surface of the heights. */
|
|
16
|
+
public constructor(public readonly reference: ReferenceSurface) {}
|
|
17
|
+
|
|
18
|
+
public point(address: ITileAddress, s: number, t: number, height = 0, target: ICartesian3 = { x: 0, y: 0, z: 0 }): ICartesian3 {
|
|
19
|
+
return this.reference.point(cubeTileDirection(address, s, t, target), height, target);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public up(address: ITileAddress, s: number, t: number, target?: ICartesian3): ICartesian3 {
|
|
23
|
+
return cubeTileDirection(address, s, t, target);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public shape(address: ITileAddress): ITileShapeFrame {
|
|
27
|
+
assertValidTileAddress(address);
|
|
28
|
+
return { key: formatTileAddress({ ...address, face: address.face ?? 0 }), rotation: 0 };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./tile-dataset";
|