@loaders.gl/3d-tiles 3.4.0-alpha.1 → 3.4.0-alpha.2

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.
Files changed (96) hide show
  1. package/dist/cesium-ion-loader.d.ts.map +1 -1
  2. package/dist/cesium-ion-loader.js +1 -0
  3. package/dist/dist.min.js +2337 -284
  4. package/dist/es5/cesium-ion-loader.js.map +1 -1
  5. package/dist/es5/lib/parsers/helpers/parse-3d-implicit-tiles.js +61 -18
  6. package/dist/es5/lib/parsers/helpers/parse-3d-implicit-tiles.js.map +1 -1
  7. package/dist/es5/lib/parsers/parse-3d-tile-header.js +17 -6
  8. package/dist/es5/lib/parsers/parse-3d-tile-header.js.map +1 -1
  9. package/dist/es5/lib/utils/obb/s2-corners-to-obb.js +32 -0
  10. package/dist/es5/lib/utils/obb/s2-corners-to-obb.js.map +1 -0
  11. package/dist/es5/lib/utils/s2/converters/s2-to-boundary.js +47 -0
  12. package/dist/es5/lib/utils/s2/converters/s2-to-boundary.js.map +1 -0
  13. package/dist/es5/lib/utils/s2/converters/s2-to-obb-points.js +32 -0
  14. package/dist/es5/lib/utils/s2/converters/s2-to-obb-points.js.map +1 -0
  15. package/dist/es5/lib/utils/s2/converters/s2-to-region.js +54 -0
  16. package/dist/es5/lib/utils/s2/converters/s2-to-region.js.map +1 -0
  17. package/dist/es5/lib/utils/s2/index.js +90 -0
  18. package/dist/es5/lib/utils/s2/index.js.map +1 -0
  19. package/dist/es5/lib/utils/s2/s2-geometry-functions.js +21 -0
  20. package/dist/es5/lib/utils/s2/s2-geometry-functions.js.map +1 -0
  21. package/dist/es5/lib/utils/s2/s2-token-functions.js +46 -0
  22. package/dist/es5/lib/utils/s2/s2-token-functions.js.map +1 -0
  23. package/dist/es5/lib/utils/s2/s2geometry/s2-cell-utils.js +24 -0
  24. package/dist/es5/lib/utils/s2/s2geometry/s2-cell-utils.js.map +1 -0
  25. package/dist/es5/lib/utils/s2/s2geometry/s2-geometry.js +217 -0
  26. package/dist/es5/lib/utils/s2/s2geometry/s2-geometry.js.map +1 -0
  27. package/dist/es5/lib/utils/version.js +1 -1
  28. package/dist/esm/cesium-ion-loader.js.map +1 -1
  29. package/dist/esm/lib/parsers/helpers/parse-3d-implicit-tiles.js +55 -11
  30. package/dist/esm/lib/parsers/helpers/parse-3d-implicit-tiles.js.map +1 -1
  31. package/dist/esm/lib/parsers/parse-3d-tile-header.js +14 -2
  32. package/dist/esm/lib/parsers/parse-3d-tile-header.js.map +1 -1
  33. package/dist/esm/lib/utils/obb/s2-corners-to-obb.js +25 -0
  34. package/dist/esm/lib/utils/obb/s2-corners-to-obb.js.map +1 -0
  35. package/dist/esm/lib/utils/s2/converters/s2-to-boundary.js +43 -0
  36. package/dist/esm/lib/utils/s2/converters/s2-to-boundary.js.map +1 -0
  37. package/dist/esm/lib/utils/s2/converters/s2-to-obb-points.js +26 -0
  38. package/dist/esm/lib/utils/s2/converters/s2-to-obb-points.js.map +1 -0
  39. package/dist/esm/lib/utils/s2/converters/s2-to-region.js +45 -0
  40. package/dist/esm/lib/utils/s2/converters/s2-to-region.js.map +1 -0
  41. package/dist/esm/lib/utils/s2/index.js +9 -0
  42. package/dist/esm/lib/utils/s2/index.js.map +1 -0
  43. package/dist/esm/lib/utils/s2/s2-geometry-functions.js +16 -0
  44. package/dist/esm/lib/utils/s2/s2-geometry-functions.js.map +1 -0
  45. package/dist/esm/lib/utils/s2/s2-token-functions.js +38 -0
  46. package/dist/esm/lib/utils/s2/s2-token-functions.js.map +1 -0
  47. package/dist/esm/lib/utils/s2/s2geometry/s2-cell-utils.js +18 -0
  48. package/dist/esm/lib/utils/s2/s2geometry/s2-cell-utils.js.map +1 -0
  49. package/dist/esm/lib/utils/s2/s2geometry/s2-geometry.js +198 -0
  50. package/dist/esm/lib/utils/s2/s2geometry/s2-geometry.js.map +1 -0
  51. package/dist/esm/lib/utils/version.js +1 -1
  52. package/dist/lib/parsers/helpers/parse-3d-implicit-tiles.d.ts +11 -0
  53. package/dist/lib/parsers/helpers/parse-3d-implicit-tiles.d.ts.map +1 -1
  54. package/dist/lib/parsers/helpers/parse-3d-implicit-tiles.js +60 -11
  55. package/dist/lib/parsers/parse-3d-tile-header.d.ts.map +1 -1
  56. package/dist/lib/parsers/parse-3d-tile-header.js +13 -1
  57. package/dist/lib/utils/obb/s2-corners-to-obb.d.ts +15 -0
  58. package/dist/lib/utils/obb/s2-corners-to-obb.d.ts.map +1 -0
  59. package/dist/lib/utils/obb/s2-corners-to-obb.js +37 -0
  60. package/dist/lib/utils/s2/converters/s2-to-boundary.d.ts +9 -0
  61. package/dist/lib/utils/s2/converters/s2-to-boundary.d.ts.map +1 -0
  62. package/dist/lib/utils/s2/converters/s2-to-boundary.js +61 -0
  63. package/dist/lib/utils/s2/converters/s2-to-obb-points.d.ts +15 -0
  64. package/dist/lib/utils/s2/converters/s2-to-obb-points.d.ts.map +1 -0
  65. package/dist/lib/utils/s2/converters/s2-to-obb-points.js +36 -0
  66. package/dist/lib/utils/s2/converters/s2-to-region.d.ts +13 -0
  67. package/dist/lib/utils/s2/converters/s2-to-region.d.ts.map +1 -0
  68. package/dist/lib/utils/s2/converters/s2-to-region.js +59 -0
  69. package/dist/lib/utils/s2/index.d.ts +8 -0
  70. package/dist/lib/utils/s2/index.d.ts.map +1 -0
  71. package/dist/lib/utils/s2/index.js +23 -0
  72. package/dist/lib/utils/s2/s2-geometry-functions.d.ts +14 -0
  73. package/dist/lib/utils/s2/s2-geometry-functions.d.ts.map +1 -0
  74. package/dist/lib/utils/s2/s2-geometry-functions.js +29 -0
  75. package/dist/lib/utils/s2/s2-token-functions.d.ts +23 -0
  76. package/dist/lib/utils/s2/s2-token-functions.d.ts.map +1 -0
  77. package/dist/lib/utils/s2/s2-token-functions.js +68 -0
  78. package/dist/lib/utils/s2/s2geometry/s2-cell-utils.d.ts +14 -0
  79. package/dist/lib/utils/s2/s2geometry/s2-cell-utils.d.ts.map +1 -0
  80. package/dist/lib/utils/s2/s2geometry/s2-cell-utils.js +32 -0
  81. package/dist/lib/utils/s2/s2geometry/s2-geometry.d.ts +52 -0
  82. package/dist/lib/utils/s2/s2geometry/s2-geometry.d.ts.map +1 -0
  83. package/dist/lib/utils/s2/s2geometry/s2-geometry.js +260 -0
  84. package/package.json +9 -8
  85. package/src/cesium-ion-loader.ts +1 -0
  86. package/src/lib/parsers/helpers/parse-3d-implicit-tiles.ts +102 -13
  87. package/src/lib/parsers/parse-3d-tile-header.ts +17 -1
  88. package/src/lib/utils/obb/s2-corners-to-obb.ts +51 -0
  89. package/src/lib/utils/s2/converters/s2-to-boundary.ts +67 -0
  90. package/src/lib/utils/s2/converters/s2-to-obb-points.ts +46 -0
  91. package/src/lib/utils/s2/converters/s2-to-region.ts +69 -0
  92. package/src/lib/utils/s2/index.ts +17 -0
  93. package/src/lib/utils/s2/s2-geometry-functions.ts +28 -0
  94. package/src/lib/utils/s2/s2-token-functions.ts +67 -0
  95. package/src/lib/utils/s2/s2geometry/s2-cell-utils.ts +33 -0
  96. package/src/lib/utils/s2/s2geometry/s2-geometry.ts +296 -0
@@ -2,6 +2,11 @@ import type {Availability, BoundingVolume, Subtree} from '../../../types';
2
2
  import {Tile3DSubtreeLoader} from '../../../tile-3d-subtree-loader';
3
3
  import {load} from '@loaders.gl/core';
4
4
 
5
+ import {getS2CellIdFromToken, getS2ChildCellId, getS2TokenFromCellId} from '../../utils/s2/index';
6
+ import type {S2VolumeInfo} from '../../utils/obb/s2-corners-to-obb';
7
+ import {convertS2BoundingVolumetoOBB} from '../../utils/obb/s2-corners-to-obb';
8
+ import Long from 'long';
9
+
5
10
  const QUADTREE_DEVISION_COUNT = 4;
6
11
  const OCTREE_DEVISION_COUNT = 8;
7
12
 
@@ -10,6 +15,59 @@ const SUBDIVISION_COUNT_MAP = {
10
15
  OCTREE: OCTREE_DEVISION_COUNT
11
16
  };
12
17
 
18
+ /**
19
+ * S2VolumeBox is an extention of BoundingVolume of type "box"
20
+ */
21
+ export type S2VolumeBox = {
22
+ /** BoundingVolume of type "box" has the "box" field. S2VolumeBox contains it as well. */
23
+ box: number[];
24
+ /** s2VolumeInfo provides additional info about the box - specifically the token, min and max height */
25
+ s2VolumeInfo: S2VolumeInfo;
26
+ };
27
+
28
+ function getChildS2VolumeBox(
29
+ s2VolumeBox: S2VolumeBox | undefined,
30
+ index: number,
31
+ subdivisionScheme: string
32
+ ): S2VolumeBox | undefined {
33
+ if (s2VolumeBox?.box) {
34
+ // Check if the BoundingVolume is of type "box"
35
+ const cellId: Long = getS2CellIdFromToken(s2VolumeBox.s2VolumeInfo.token);
36
+ const childCellId = getS2ChildCellId(cellId, index);
37
+ const childToken = getS2TokenFromCellId(childCellId);
38
+
39
+ // Clone object. Note, s2VolumeInfo is a plain object that doesn't contain any nested object.
40
+ // So, we can use the Spread Operator to make a shallow copy of the object.
41
+ const s2ChildVolumeInfo: S2VolumeInfo = {...s2VolumeBox.s2VolumeInfo};
42
+ s2ChildVolumeInfo.token = childToken; // replace the token with the child's one
43
+
44
+ // In case of QUADTREE the sizeZ should NOT be changed!
45
+ // https://portal.ogc.org/files/102132
46
+ // A quadtree divides space only on the x and y dimensions.
47
+ // It divides each tile into 4 smaller tiles where the x and y dimensions are halved.
48
+ // The quadtree z minimum and maximum remain unchanged.
49
+ switch (subdivisionScheme) {
50
+ case 'OCTREE':
51
+ const s2VolumeInfo: S2VolumeInfo = s2VolumeBox.s2VolumeInfo;
52
+ const delta = s2VolumeInfo.maximumHeight - s2VolumeInfo.minimumHeight;
53
+ const sizeZ: number = delta / 2.0; // It's a next level (a child)
54
+ const midZ: number = s2VolumeInfo.minimumHeight + delta / 2.0;
55
+ s2VolumeInfo.minimumHeight = midZ - sizeZ;
56
+ s2VolumeInfo.maximumHeight = midZ + sizeZ;
57
+ break;
58
+ default:
59
+ break;
60
+ }
61
+ const box = convertS2BoundingVolumetoOBB(s2ChildVolumeInfo);
62
+ const childS2VolumeBox: S2VolumeBox = {
63
+ box,
64
+ s2VolumeInfo: s2ChildVolumeInfo
65
+ };
66
+ return childS2VolumeBox;
67
+ }
68
+ return undefined;
69
+ }
70
+
13
71
  /**
14
72
  * Recursively parse implicit tiles tree
15
73
  * Spec - https://github.com/CesiumGS/3d-tiles/tree/main/extensions/3DTILES_implicit_tiling
@@ -30,6 +88,7 @@ export async function parseImplicitTiles(params: {
30
88
  childIndex?: number;
31
89
  level?: number;
32
90
  globalData?: {level: number; mortonIndex: number; x: number; y: number; z: number};
91
+ s2VolumeBox?: S2VolumeBox;
33
92
  }) {
34
93
  const {
35
94
  options,
@@ -46,7 +105,8 @@ export async function parseImplicitTiles(params: {
46
105
  x: 0,
47
106
  y: 0,
48
107
  z: 0
49
- }
108
+ },
109
+ s2VolumeBox
50
110
  } = params;
51
111
  let {subtree, level = 0} = params;
52
112
  const {
@@ -129,19 +189,33 @@ export async function parseImplicitTiles(params: {
129
189
  const pData = {mortonIndex: childTileMortonIndex, x: childTileX, y: childTileY, z: childTileZ};
130
190
 
131
191
  for (let index = 0; index < childrenPerTile; index++) {
132
- const currentTile = await parseImplicitTiles({
192
+ const childS2VolumeBox: S2VolumeBox | undefined = getChildS2VolumeBox(
193
+ s2VolumeBox,
194
+ index,
195
+ subdivisionScheme
196
+ );
197
+
198
+ // Recursive calling...
199
+ const childTileParsed = await parseImplicitTiles({
133
200
  subtree,
134
201
  options,
135
202
  parentData: pData,
136
203
  childIndex: index,
137
204
  level: childTileLevel,
138
- globalData
205
+ globalData,
206
+ s2VolumeBox: childS2VolumeBox
139
207
  });
140
208
 
141
- if (currentTile.contentUrl || currentTile.children.length) {
209
+ if (childTileParsed.contentUrl || childTileParsed.children.length) {
142
210
  const globalLevel = lev + 1;
143
211
  const childCoordinates = {childTileX, childTileY, childTileZ};
144
- const formattedTile = formatTileData(currentTile, globalLevel, childCoordinates, options);
212
+ const formattedTile = formatTileData(
213
+ childTileParsed,
214
+ globalLevel,
215
+ childCoordinates,
216
+ options,
217
+ s2VolumeBox
218
+ );
145
219
  // @ts-ignore
146
220
  tile.children.push(formattedTile);
147
221
  }
@@ -174,7 +248,8 @@ function formatTileData(
174
248
  tile,
175
249
  level: number,
176
250
  childCoordinates: {childTileX: number; childTileY: number; childTileZ: number},
177
- options: any
251
+ options: any,
252
+ s2VolumeBox?: S2VolumeBox
178
253
  ) {
179
254
  const {
180
255
  basePath,
@@ -187,9 +262,14 @@ function formatTileData(
187
262
  } = options;
188
263
  const uri = tile.contentUrl && tile.contentUrl.replace(`${basePath}/`, '');
189
264
  const lodMetricValue = rootLodMetricValue / 2 ** level;
190
- const boundingVolume = calculateBoundingVolumeForChildTile(
265
+
266
+ const boundingVolume: BoundingVolume = s2VolumeBox?.box
267
+ ? {box: s2VolumeBox.box}
268
+ : rootBoundingVolume;
269
+
270
+ const boundingVolumeForChildTile = calculateBoundingVolumeForChildTile(
191
271
  level,
192
- rootBoundingVolume,
272
+ boundingVolume,
193
273
  childCoordinates
194
274
  );
195
275
 
@@ -204,7 +284,7 @@ function formatTileData(
204
284
  lodMetricValue,
205
285
  geometricError: lodMetricValue,
206
286
  transform: tile.transform,
207
- boundingVolume
287
+ boundingVolume: boundingVolumeForChildTile
208
288
  };
209
289
  }
210
290
 
@@ -219,7 +299,7 @@ function calculateBoundingVolumeForChildTile(
219
299
  level: number,
220
300
  rootBoundingVolume: BoundingVolume,
221
301
  childCoordinates: {childTileX: number; childTileY: number; childTileZ: number}
222
- ): BoundingVolume | null {
302
+ ): BoundingVolume {
223
303
  if (rootBoundingVolume.region) {
224
304
  const {childTileX, childTileY, childTileZ} = childCoordinates;
225
305
  const [west, south, east, north, minimumHeight, maximumHeight] = rootBoundingVolume.region;
@@ -227,6 +307,13 @@ function calculateBoundingVolumeForChildTile(
227
307
 
228
308
  const sizeX = (east - west) / boundingVolumesCount;
229
309
  const sizeY = (north - south) / boundingVolumesCount;
310
+
311
+ // TODO : Why is the subdivisionScheme not being checked here?
312
+
313
+ // In case of QUADTREE the sizeZ should NOT be changed!
314
+ // https://portal.ogc.org/files/102132
315
+ // A quadtree divides space only on the x and y dimensions. It divides each tile into 4 smaller tiles where the x and y dimensions are halved. The quadtree z minimum and maximum remain unchanged.
316
+
230
317
  const sizeZ = (maximumHeight - minimumHeight) / boundingVolumesCount;
231
318
 
232
319
  const [childWest, childEast] = [west + sizeX * childTileX, west + sizeX * (childTileX + 1)];
@@ -241,9 +328,11 @@ function calculateBoundingVolumeForChildTile(
241
328
  };
242
329
  }
243
330
 
244
- // eslint-disable-next-line no-console
245
- console.warn('Unsupported bounding volume type: ', rootBoundingVolume);
246
- return null;
331
+ if (rootBoundingVolume.box) {
332
+ return rootBoundingVolume;
333
+ }
334
+
335
+ throw new Error(`Unsupported bounding volume type ${rootBoundingVolume}`);
247
336
  }
248
337
 
249
338
  /**
@@ -3,7 +3,10 @@ import {Tile3DSubtreeLoader} from '../../tile-3d-subtree-loader';
3
3
  import {load} from '@loaders.gl/core';
4
4
  import {Tileset3D, LOD_METRIC_TYPE, TILE_REFINEMENT, TILE_TYPE, Tile3D} from '@loaders.gl/tiles';
5
5
  import {ImplicitTilingExtension, Subtree} from '../../types';
6
+ import type {S2VolumeBox} from './helpers/parse-3d-implicit-tiles';
6
7
  import {parseImplicitTiles, replaceContentUrlTemplate} from './helpers/parse-3d-implicit-tiles';
8
+ import type {S2VolumeInfo} from '../utils/obb/s2-corners-to-obb';
9
+ import {convertS2BoundingVolumetoOBB} from '../utils/obb/s2-corners-to-obb';
7
10
 
8
11
  function getTileType(tile) {
9
12
  if (!tile.contentUrl) {
@@ -141,6 +144,15 @@ export async function normalizeImplicitTileHeaders(
141
144
  const refine = tileset?.root?.refine;
142
145
  // @ts-ignore
143
146
  const rootLodMetricValue = tile.geometricError;
147
+
148
+ // Replace tile.boundingVolume with the the bounding volume specified by the extensions['3DTILES_bounding_volume_S2']
149
+ const s2VolumeInfo: S2VolumeInfo = tile.boundingVolume.extensions?.['3DTILES_bounding_volume_S2'];
150
+ if (s2VolumeInfo) {
151
+ const box = convertS2BoundingVolumetoOBB(s2VolumeInfo);
152
+ const s2VolumeBox: S2VolumeBox = {box, s2VolumeInfo};
153
+ tile.boundingVolume = s2VolumeBox;
154
+ }
155
+
144
156
  const rootBoundingVolume = tile.boundingVolume;
145
157
 
146
158
  const implicitOptions = {
@@ -177,7 +189,11 @@ export async function normalizeImplicitTileData(tile, rootSubtree: Subtree, opti
177
189
  tile.lodMetricValue = tile.geometricError;
178
190
  tile.transformMatrix = tile.transform;
179
191
 
180
- const {children, contentUrl} = await parseImplicitTiles({subtree: rootSubtree, options});
192
+ const {children, contentUrl} = await parseImplicitTiles({
193
+ subtree: rootSubtree,
194
+ options,
195
+ s2VolumeBox: tile
196
+ });
181
197
 
182
198
  if (contentUrl) {
183
199
  tile.contentUrl = contentUrl;
@@ -0,0 +1,51 @@
1
+ import {Vector3} from '@math.gl/core';
2
+ import {OrientedBoundingBox, makeOrientedBoundingBoxFromPoints} from '@math.gl/culling';
3
+
4
+ import type {S2HeightInfo} from '../../utils/s2/index';
5
+ import {getS2OrientedBoundingBoxCornerPoints} from '../../utils/s2/index';
6
+
7
+ import {getS2LngLat} from '../../utils/s2/index';
8
+ import {Ellipsoid} from '@math.gl/geospatial';
9
+
10
+ export type S2VolumeInfo = {
11
+ /** S2 key or token */
12
+ token: string;
13
+ /** minimum height in meters */
14
+ minimumHeight: number;
15
+ /** maximum height in meters */
16
+ maximumHeight: number;
17
+ };
18
+
19
+ /**
20
+ * Converts S2VolumeInfo to OrientedBoundingBox
21
+ * @param {S2VolumeInfo} s2VolumeInfo - s2 volume to convert
22
+ * @returns Oriented Bounding Box of type Box
23
+ */
24
+ export function convertS2BoundingVolumetoOBB(s2VolumeInfo: S2VolumeInfo): number[] {
25
+ const token: string = s2VolumeInfo.token;
26
+ const heightInfo: S2HeightInfo = {
27
+ minimumHeight: s2VolumeInfo.minimumHeight,
28
+ maximumHeight: s2VolumeInfo.maximumHeight
29
+ };
30
+
31
+ const corners: Vector3[] = getS2OrientedBoundingBoxCornerPoints(token, heightInfo);
32
+
33
+ // Add a point that doesn't allow the box dive under the Earth
34
+
35
+ const center = getS2LngLat(token);
36
+ const centerLng: number = center[0];
37
+ const centerLat: number = center[1];
38
+ const point = Ellipsoid.WGS84.cartographicToCartesian([
39
+ centerLng,
40
+ centerLat,
41
+ heightInfo.maximumHeight
42
+ ]);
43
+ const centerPointAdditional = new Vector3(point[0], point[1], point[2]);
44
+ corners.push(centerPointAdditional);
45
+
46
+ // corners should be an array of Vector3 (XYZ)
47
+ const obb: OrientedBoundingBox = makeOrientedBoundingBoxFromPoints(corners);
48
+ const box: number[] = [...obb.center, ...obb.halfAxes];
49
+
50
+ return box;
51
+ }
@@ -0,0 +1,67 @@
1
+ import type {S2Cell} from '../s2geometry/s2-geometry';
2
+ import {IJToST, STToUV, FaceUVToXYZ, XYZToLngLat} from '../s2geometry/s2-geometry';
3
+
4
+ const MAX_RESOLUTION = 100;
5
+
6
+ /**
7
+ * Get a polygon with corner coordinates for an S2 cell
8
+ * @param s2cell {S2Cell} S2 cell
9
+ * @return {Float64Array} - a simple polygon in flat array format: [lng0, lat0, lng1, lat1, ...]
10
+ * - the polygon is closed, i.e. last coordinate is a copy of the first coordinate
11
+ */
12
+ // eslint-disable-next-line max-statements
13
+ export function getS2BoundaryFlatFromS2Cell(s2cell: S2Cell): Float64Array {
14
+ const {face, ij, level} = s2cell;
15
+ const offsets = [
16
+ [0, 0],
17
+ [0, 1],
18
+ [1, 1],
19
+ [1, 0],
20
+ [0, 0]
21
+ ];
22
+
23
+ // The S2 cell edge is curved: http://s2geometry.io/
24
+ // This is more prominent at lower levels
25
+ // resolution is the number of segments to generate per edge.
26
+ // We exponentially reduce resolution as level increases so it doesn't affect perf
27
+ // when there are a large number of cells
28
+ const resolution = Math.max(1, Math.ceil(MAX_RESOLUTION * Math.pow(2, -level)));
29
+ const result = new Float64Array(4 * resolution * 2 + 2);
30
+ let ptIndex = 0;
31
+ let prevLng = 0;
32
+
33
+ for (let i = 0; i < 4; i++) {
34
+ const offset = offsets[i].slice(0) as [number, number];
35
+ const nextOffset = offsets[i + 1];
36
+ const stepI = (nextOffset[0] - offset[0]) / resolution;
37
+ const stepJ = (nextOffset[1] - offset[1]) / resolution;
38
+
39
+ for (let j = 0; j < resolution; j++) {
40
+ offset[0] += stepI;
41
+ offset[1] += stepJ;
42
+ // Cell can be represented by coordinates IJ, ST, UV, XYZ
43
+ // http://s2geometry.io/devguide/s2cell_hierarchy#coordinate-systems
44
+ const st = IJToST(ij, level, offset);
45
+ const uv = STToUV(st);
46
+ const xyz = FaceUVToXYZ(face, uv);
47
+ const lngLat = XYZToLngLat(xyz);
48
+
49
+ // Adjust longitude for Web Mercator projection
50
+
51
+ if (Math.abs(lngLat[1]) > 89.999) {
52
+ lngLat[0] = prevLng;
53
+ }
54
+
55
+ const deltaLng = lngLat[0] - prevLng;
56
+ lngLat[0] += deltaLng > 180 ? -360 : deltaLng < -180 ? 360 : 0;
57
+
58
+ result[ptIndex++] = lngLat[0];
59
+ result[ptIndex++] = lngLat[1];
60
+ prevLng = lngLat[0];
61
+ }
62
+ }
63
+ // close the loop
64
+ result[ptIndex++] = result[0];
65
+ result[ptIndex++] = result[1];
66
+ return result;
67
+ }
@@ -0,0 +1,46 @@
1
+ import {getS2Cell} from '../s2geometry/s2-cell-utils';
2
+ import {getS2Region} from './s2-to-region';
3
+ import {Vector3} from '@math.gl/core';
4
+
5
+ export type S2HeightInfo = {
6
+ minimumHeight: number;
7
+ maximumHeight: number;
8
+ };
9
+
10
+ /**
11
+ * Converts S2HeightInfo to corner points of an oriented bounding box
12
+ * Can be used to constuct an OrientedBoundingBox instance
13
+ * @param tokenOrKey {string} A string that is the cell's hex token or the Hilbert quad key (containing /)
14
+ * @param heightInfo {S2HeightInfo} min and max height of the box
15
+ * @returns corner points of the oriented bounding box
16
+ */
17
+ export function getS2OrientedBoundingBoxCornerPoints(
18
+ tokenOrKey: string, // This can be an S2 key or token
19
+ heightInfo?: S2HeightInfo
20
+ ): Vector3[] {
21
+ const min: number = heightInfo?.minimumHeight || 0;
22
+ const max: number = heightInfo?.maximumHeight || 0;
23
+
24
+ const s2cell = getS2Cell(tokenOrKey);
25
+ const region = getS2Region(s2cell);
26
+
27
+ // region lng/lat are in degrees
28
+ const W = region.west;
29
+ const S = region.south;
30
+ const E = region.east;
31
+ const N = region.north;
32
+
33
+ const points: Vector3[] = [];
34
+
35
+ points.push(new Vector3(W, N, min));
36
+ points.push(new Vector3(E, N, min));
37
+ points.push(new Vector3(E, S, min));
38
+ points.push(new Vector3(W, S, min));
39
+
40
+ points.push(new Vector3(W, N, max));
41
+ points.push(new Vector3(E, N, max));
42
+ points.push(new Vector3(E, S, max));
43
+ points.push(new Vector3(W, S, max));
44
+
45
+ return points;
46
+ }
@@ -0,0 +1,69 @@
1
+ import type {S2Cell} from '../s2geometry/s2-geometry';
2
+ import {getS2BoundaryFlatFromS2Cell} from './s2-to-boundary';
3
+ import {getS2Cell} from '../s2geometry/s2-cell-utils';
4
+
5
+ /**
6
+ * Converts S2 cell to the 2D region
7
+ * @param s2cell {S2Cell} S2 cell to convert to 2D region
8
+ * @returns 2D region as an object containing: west, north, east, south in degrees
9
+ */
10
+ export function getS2Region(s2cell: S2Cell): {
11
+ west: number;
12
+ east: number;
13
+ north: number;
14
+ south: number;
15
+ } {
16
+ let region;
17
+ if (s2cell.face === 2 || s2cell.face === 5) {
18
+ // let corners: Float64Array;
19
+ let corners: any = null;
20
+ let len = 0;
21
+ for (let i = 0; i < 4; i++) {
22
+ const key = `${s2cell.face}/${i}`;
23
+ const cell = getS2Cell(key);
24
+ const corns: Float64Array = getS2BoundaryFlatFromS2Cell(cell);
25
+ if (typeof corners === 'undefined' || corners === null)
26
+ corners = new Float64Array(4 * corns.length);
27
+ corners.set(corns, len);
28
+ len += corns.length;
29
+ }
30
+ region = get2DRegionFromS2Corners(corners);
31
+ } else {
32
+ const corners: Float64Array = getS2BoundaryFlatFromS2Cell(s2cell);
33
+ region = get2DRegionFromS2Corners(corners);
34
+ }
35
+ return region;
36
+ }
37
+
38
+ /**
39
+ * Converts the S2 cell defined by its corners to the 2D region
40
+ * @param corners {Float64Array} - a simple polygon in flat array format: [lng0, lat0, lng1, lat1, ...]
41
+ * @returns 2D region as an object containing: west, north, east, south in degrees
42
+ */
43
+ function get2DRegionFromS2Corners(corners: Float64Array): {
44
+ west: number;
45
+ east: number;
46
+ north: number;
47
+ south: number;
48
+ } {
49
+ if (corners.length % 2 !== 0) {
50
+ throw new Error('Invalid corners');
51
+ }
52
+ const longitudes: number[] = [];
53
+ const latitudes: number[] = [];
54
+ for (let i = 0; i < corners.length; i += 2) {
55
+ longitudes.push(corners[i]);
56
+ latitudes.push(corners[i + 1]);
57
+ }
58
+
59
+ longitudes.sort((a, b) => a - b);
60
+ latitudes.sort((a, b) => a - b);
61
+
62
+ // Return the region in degrees
63
+ return {
64
+ west: longitudes[0],
65
+ east: longitudes[longitudes.length - 1],
66
+ north: latitudes[latitudes.length - 1],
67
+ south: latitudes[0]
68
+ };
69
+ }
@@ -0,0 +1,17 @@
1
+ // math.gl MIT license
2
+
3
+ export {getS2CellIdFromToken, getS2TokenFromCellId, getS2ChildCellId} from './s2-token-functions';
4
+ export {getS2BoundaryFlat, getS2LngLat} from './s2-geometry-functions';
5
+
6
+ export {getS2Cell, getS2QuadKey} from './s2geometry/s2-cell-utils';
7
+ export {
8
+ getS2QuadkeyFromCellId,
9
+ getS2CellFromQuadKey,
10
+ getS2CellIdFromQuadkey,
11
+ getS2LngLatFromS2Cell
12
+ } from './s2geometry/s2-geometry';
13
+
14
+ export {getS2Region} from './converters/s2-to-region';
15
+
16
+ export type {S2HeightInfo} from './converters/s2-to-obb-points';
17
+ export {getS2OrientedBoundingBoxCornerPoints} from './converters/s2-to-obb-points';
@@ -0,0 +1,28 @@
1
+ // math.gl, MIT license
2
+
3
+ import {getS2BoundaryFlatFromS2Cell} from './converters/s2-to-boundary';
4
+ import {getS2LngLatFromS2Cell} from './s2geometry/s2-geometry';
5
+ import {getS2Cell} from './s2geometry/s2-cell-utils';
6
+
7
+ // GEOMETRY
8
+
9
+ /**
10
+ * Retrieve S2 geometry center
11
+ * @param s2Token {string} A string that is the cell's hex token
12
+ * @returns {[number, number]} Longitude and Latitude coordinates of the S2 cell's center
13
+ */
14
+ export function getS2LngLat(s2Token: string): [number, number] {
15
+ const s2cell = getS2Cell(s2Token);
16
+ return getS2LngLatFromS2Cell(s2cell);
17
+ }
18
+
19
+ /**
20
+ * Get a polygon with corner coordinates for an s2 cell
21
+ * @param tokenOrKey {string} A string that is the cell's hex token or the Hilbert quad key (containing /)
22
+ * @return {Float64Array} - a simple polygon in flat array format: [lng0, lat0, lng1, lat1, ...]
23
+ * - the polygon is closed, i.e. last coordinate is a copy of the first coordinate
24
+ */
25
+ export function getS2BoundaryFlat(tokenOrKey: string): Float64Array {
26
+ const s2cell = getS2Cell(tokenOrKey);
27
+ return getS2BoundaryFlatFromS2Cell(s2cell);
28
+ }
@@ -0,0 +1,67 @@
1
+ // loaders.gl, MIT license
2
+
3
+ import Long from 'long';
4
+
5
+ const MAXIMUM_TOKEN_LENGTH = 16;
6
+
7
+ /**
8
+ * Convert the S2 token to the S2 cell ID
9
+ * @param token {string} A string that is the cell's hex token. Zero cell ID is represented as 'X'.
10
+ * @returns {Long} Cell id that is a 64-bit encoding of a face and a Hilbert curve parameter on that face.
11
+ * See {@link https://github.com/google/s2-geometry-library-java/blob/c04b68bf3197a9c34082327eeb3aec7ab7c85da1/src/com/google/common/geometry/S2CellId.java#L439} for more information
12
+ */
13
+ export function getS2CellIdFromToken(token: string): Long {
14
+ if (token === 'X') {
15
+ token = '';
16
+ }
17
+ // pad token with zeros to make the length 16 that is defined in MAXIMUM_TOKEN_LENGTH
18
+ const paddedToken = token.padEnd(MAXIMUM_TOKEN_LENGTH, '0');
19
+ return Long.fromString(paddedToken, true, 16); // Hex base
20
+ }
21
+
22
+ /**
23
+ * Convert the S2 cell ID to the S2 token
24
+ * @param cellId {Long} A 64-bit encoding of a face and a Hilbert curve parameter on that face.
25
+ * @returns {string} A string that is the cell's hex token. Zero cell ID is represented as 'X'.
26
+ */
27
+ export function getS2TokenFromCellId(cellId: Long): string {
28
+ if (cellId.isZero()) {
29
+ return 'X';
30
+ }
31
+ let numZeroDigits = cellId.countTrailingZeros();
32
+
33
+ const remainder = numZeroDigits % 4;
34
+ numZeroDigits = (numZeroDigits - remainder) / 4;
35
+ const trailingZeroHexChars = numZeroDigits;
36
+ numZeroDigits *= 4;
37
+
38
+ const x = cellId.shiftRightUnsigned(numZeroDigits);
39
+ const hexString = x.toString(16).replace(/0+$/, '');
40
+ const zeroString = Array(17 - trailingZeroHexChars - hexString.length).join('0');
41
+ return zeroString + hexString;
42
+ }
43
+
44
+ /**
45
+ * Get one of four S2 cell's children.
46
+ * @param cellId {Long} A 64-bit encoding of a face and a Hilbert curve parameter on that face.
47
+ * The cell must NOT be a leaf one. So, the cell's level is in the range [0-29].
48
+ * @param index {number} Child index defines one of four S2 cell's children. Must be in the range [0-3].
49
+ * @returns The ID of the cell's child.
50
+ */
51
+ export function getS2ChildCellId(cellId: Long, index: number): Long {
52
+ // Shift sentinel bit 2 positions to the right.
53
+ const newLsb = lsb(cellId).shiftRightUnsigned(2);
54
+ // Insert child index before the sentinel bit.
55
+ const childCellId: Long = cellId.add(Long.fromNumber(2 * index + 1 - 4).multiply(newLsb));
56
+ return childCellId;
57
+ }
58
+
59
+ /**
60
+ * Return the lowest-numbered bit that is on for this cell id.
61
+ * @private
62
+ * @param cellId {Long} Cell id.
63
+ * @returns {Long} The lowest-numbered bit that is on for this cell id.
64
+ */
65
+ function lsb(cellId: Long): Long {
66
+ return cellId.and(cellId.not().add(1)); // eslint-disable-line
67
+ }
@@ -0,0 +1,33 @@
1
+ // math.gl, MIT license
2
+
3
+ import type {S2Cell} from './s2-geometry';
4
+ import {getS2CellFromQuadKey, getS2QuadkeyFromCellId} from './s2-geometry';
5
+ import {getS2CellIdFromToken} from '../s2-token-functions';
6
+
7
+ import Long from 'long';
8
+
9
+ /**
10
+ * Return the S2Cell from the cell's hex token or the Hilbert quad key
11
+ * @param tokenOrKey {string} A string that is the cell's hex token or the Hilbert quad key (containing /)
12
+ * @returns {@link S2Cell}
13
+ */
14
+ export function getS2Cell(tokenOrKey: string): S2Cell {
15
+ const key = getS2QuadKey(tokenOrKey);
16
+ const s2cell = getS2CellFromQuadKey(key);
17
+ return s2cell;
18
+ }
19
+
20
+ /**
21
+ * Get the underlying Hilbert quad key
22
+ * @param tokenOrKey {string} A string that is the cell's hex token or the Hilbert quad key (containing /)
23
+ * @returns Hilbert quad key
24
+ */
25
+ export function getS2QuadKey(tokenOrKey: string): string {
26
+ if (tokenOrKey.indexOf('/') > 0) {
27
+ // is Hilbert quad key
28
+ return tokenOrKey;
29
+ }
30
+ // is S2 cell's hex token
31
+ const id: Long = getS2CellIdFromToken(tokenOrKey);
32
+ return getS2QuadkeyFromCellId(id);
33
+ }