nodes2ts 3.0.0 → 4.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.
Files changed (80) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +102 -15
  3. package/dist/export.cjs +5331 -0
  4. package/dist/export.cjs.map +1 -0
  5. package/dist/export.d.cts +1838 -0
  6. package/dist/export.d.ts +1828 -20
  7. package/dist/export.js +5280 -62
  8. package/dist/export.js.map +1 -1
  9. package/package.json +34 -55
  10. package/.eslintignore +0 -3
  11. package/.eslintrc.cjs +0 -11
  12. package/.github/workflows/lint.js.yml +0 -22
  13. package/.github/workflows/node.js.yml +0 -22
  14. package/.mocharc.js +0 -7
  15. package/dist/Interval.d.ts +0 -24
  16. package/dist/Interval.js +0 -23
  17. package/dist/Interval.js.map +0 -1
  18. package/dist/MutableInteger.d.ts +0 -4
  19. package/dist/MutableInteger.js +0 -10
  20. package/dist/MutableInteger.js.map +0 -1
  21. package/dist/Platform.d.ts +0 -15
  22. package/dist/Platform.js +0 -53
  23. package/dist/Platform.js.map +0 -1
  24. package/dist/R1Interval.d.ts +0 -76
  25. package/dist/R1Interval.js +0 -158
  26. package/dist/R1Interval.js.map +0 -1
  27. package/dist/R2Vector.d.ts +0 -34
  28. package/dist/R2Vector.js +0 -131
  29. package/dist/R2Vector.js.map +0 -1
  30. package/dist/S1Angle.d.ts +0 -60
  31. package/dist/S1Angle.js +0 -133
  32. package/dist/S1Angle.js.map +0 -1
  33. package/dist/S1ChordAngle.d.ts +0 -166
  34. package/dist/S1ChordAngle.js +0 -318
  35. package/dist/S1ChordAngle.js.map +0 -1
  36. package/dist/S1Interval.d.ts +0 -109
  37. package/dist/S1Interval.js +0 -398
  38. package/dist/S1Interval.js.map +0 -1
  39. package/dist/S2.d.ts +0 -96
  40. package/dist/S2.js +0 -231
  41. package/dist/S2.js.map +0 -1
  42. package/dist/S2Cap.d.ts +0 -122
  43. package/dist/S2Cap.js +0 -399
  44. package/dist/S2Cap.js.map +0 -1
  45. package/dist/S2Cell.d.ts +0 -106
  46. package/dist/S2Cell.js +0 -358
  47. package/dist/S2Cell.js.map +0 -1
  48. package/dist/S2CellId.d.ts +0 -290
  49. package/dist/S2CellId.js +0 -983
  50. package/dist/S2CellId.js.map +0 -1
  51. package/dist/S2CellUnion.d.ts +0 -179
  52. package/dist/S2CellUnion.js +0 -523
  53. package/dist/S2CellUnion.js.map +0 -1
  54. package/dist/S2EdgeUtil.d.ts +0 -17
  55. package/dist/S2EdgeUtil.js +0 -785
  56. package/dist/S2EdgeUtil.js.map +0 -1
  57. package/dist/S2LatLng.d.ts +0 -77
  58. package/dist/S2LatLng.js +0 -186
  59. package/dist/S2LatLng.js.map +0 -1
  60. package/dist/S2LatLngRect.d.ts +0 -190
  61. package/dist/S2LatLngRect.js +0 -624
  62. package/dist/S2LatLngRect.js.map +0 -1
  63. package/dist/S2Metric.d.ts +0 -38
  64. package/dist/S2Metric.js +0 -79
  65. package/dist/S2Metric.js.map +0 -1
  66. package/dist/S2Point.d.ts +0 -78
  67. package/dist/S2Point.js +0 -250
  68. package/dist/S2Point.js.map +0 -1
  69. package/dist/S2Projections.d.ts +0 -102
  70. package/dist/S2Projections.js +0 -331
  71. package/dist/S2Projections.js.map +0 -1
  72. package/dist/S2Region.d.ts +0 -31
  73. package/dist/S2Region.js +0 -18
  74. package/dist/S2Region.js.map +0 -1
  75. package/dist/S2RegionCoverer.d.ts +0 -186
  76. package/dist/S2RegionCoverer.js +0 -471
  77. package/dist/S2RegionCoverer.js.map +0 -1
  78. package/dist/utils/preconditions.d.ts +0 -2
  79. package/dist/utils/preconditions.js +0 -16
  80. package/dist/utils/preconditions.js.map +0 -1
package/dist/S2CellId.js DELETED
@@ -1,983 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright 2005 Google Inc.
4
- *
5
- * Licensed under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License.
7
- * You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an "AS IS" BASIS,
13
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- * See the License for the specific language governing permissions and
15
- * limitations under the License.
16
- */
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.S2CellId = void 0;
19
- //const Long = require("long");
20
- const Long = require("long");
21
- const S2Point_1 = require("./S2Point");
22
- const R2Vector_1 = require("./R2Vector");
23
- const S2_1 = require("./S2");
24
- const S2LatLng_1 = require("./S2LatLng");
25
- const S2Projections_1 = require("./S2Projections");
26
- const parseHex = function parseHex(str) {
27
- return Long.fromString(str, false, 16);
28
- };
29
- /**
30
- * An S2CellId is a 64-bit unsigned integer that uniquely identifies a cell in
31
- * the S2 cell decomposition. It has the following format:
32
- *
33
- * <pre>
34
- * id = [face][face_pos]
35
- * </pre>
36
- *
37
- * face: a 3-bit number (range 0..5) encoding the cube face.
38
- *
39
- * face_pos: a 61-bit number encoding the position of the center of this cell
40
- * along the Hilbert curve over this face (see the Wiki pages for details).
41
- *
42
- * Sequentially increasing cell ids follow a continuous space-filling curve over
43
- * the entire sphere. They have the following properties:
44
- * - The id of a cell at level k consists of a 3-bit face number followed by k
45
- * bit pairs that recursively select one of the four children of each cell. The
46
- * next bit is always 1, and all other bits are 0. Therefore, the level of a
47
- * cell is determined by the position of its lowest-numbered bit that is turned
48
- * on (for a cell at level k, this position is 2 * (MAX_LEVEL - k).)
49
- * - The id of a parent cell is at the midpoint of the range of ids spanned by
50
- * its children (or by its descendants at any level).
51
- *
52
- * Leaf cells are often used to represent points on the unit sphere, and this
53
- * class provides methods for converting directly between these two
54
- * representations. For cells that represent 2D regions rather than discrete
55
- * point, it is better to use the S2Cell class.
56
- *
57
- *
58
- */
59
- class S2CellId {
60
- constructor(id) {
61
- if (typeof (id) === 'string') {
62
- this.id = Long.fromString(id);
63
- }
64
- else {
65
- this.id = id;
66
- }
67
- }
68
- /** Which cube face this cell belongs to, in the range 0..5. */
69
- get face() {
70
- return this.id.shiftRightUnsigned(S2CellId.POS_BITS).toInt();
71
- }
72
- /** Return the lowest-numbered bit that is on for cells at the given level. */
73
- lowestOnBit() {
74
- return S2CellId.lowestOnBit(this.id);
75
- }
76
- static lowestOnBit(id) {
77
- return id.and(id.negate());
78
- }
79
- /** The default constructor returns an invalid cell id. */
80
- static none() {
81
- return new S2CellId(new Long(0));
82
- }
83
- /**
84
- * Returns an invalid cell id guaranteed to be larger than any valid cell id.
85
- * Useful for creating indexes.
86
- */
87
- static sentinel() {
88
- return new S2CellId(S2CellId.MAX_UNSIGNED); // -1
89
- }
90
- getBits1(i, j, k, bits) {
91
- const nbits = (k == 7) ? (S2CellId.MAX_LEVEL - 7 * S2CellId.LOOKUP_BITS) : S2CellId.LOOKUP_BITS;
92
- bits += (this.id
93
- .shiftRightUnsigned((k * 2 * S2CellId.LOOKUP_BITS + 1))
94
- .getLowBitsUnsigned()
95
- & ((1 << (2 * nbits)) - 1)) << 2;
96
- /*
97
- * System.out.println("id is: " + id_); System.out.println("bits is " +
98
- * bits); System.out.println("lookup_ij[bits] is " + lookup_ij[bits]);
99
- */
100
- bits = S2CellId.LOOKUP_IJ[bits];
101
- i.val = i.val + ((bits >> (S2CellId.LOOKUP_BITS + 2)) << (k * S2CellId.LOOKUP_BITS));
102
- // i.setValue(i.intValue() + ((bits >> (LOOKUP_BITS + 2)) << (k * LOOKUP_BITS)));
103
- /*
104
- * System.out.println("left is " + ((bits >> 2) & ((1 << kLookupBits) -
105
- * 1))); System.out.println("right is " + (k * kLookupBits));
106
- * System.out.println("j is: " + j.intValue()); System.out.println("addition
107
- * is: " + ((((bits >> 2) & ((1 << kLookupBits) - 1))) << (k *
108
- * kLookupBits)));
109
- */
110
- j.val = j.val + ((((bits >> 2) & ((1 << S2CellId.LOOKUP_BITS) - 1))) << (k * S2CellId.LOOKUP_BITS));
111
- bits &= (S2_1.S2.SWAP_MASK | S2_1.S2.INVERT_MASK);
112
- return bits;
113
- }
114
- static lowestOnBitForLevel(level) {
115
- return new Long(1).shiftLeft(2 * (S2CellId.MAX_LEVEL - level));
116
- }
117
- /**
118
- * @deprecated use `toIJOrientation` instead
119
- * Return the (face, i, j) coordinates for the leaf cell corresponding to this
120
- * cell id. Since cells are represented by the Hilbert curve position at the
121
- * center of the cell, the returned (i,j) for non-leaf cells will be a leaf
122
- * cell adjacent to the cell center. If "orientation" is non-NULL, also return
123
- * the Hilbert curve orientation for the current cell.
124
- */
125
- toFaceIJOrientation(pi, pj, orientation) {
126
- // System.out.println("Entering toFaceIjorientation");
127
- const face = this.face;
128
- let bits = (face & S2_1.S2.SWAP_MASK);
129
- // System.out.println("face = " + face + " bits = " + bits);
130
- // Each iteration maps 8 bits of the Hilbert curve position into
131
- // 4 bits of "i" and "j". The lookup table transforms a key of the
132
- // form "ppppppppoo" to a value of the form "iiiijjjjoo", where the
133
- // letters [ijpo] represents bits of "i", "j", the Hilbert curve
134
- // position, and the Hilbert curve orientation respectively.
135
- //
136
- // On the first iteration we need to be careful to clear out the bits
137
- // representing the cube face.
138
- for (let k = 7; k >= 0; --k) {
139
- bits = this.getBits1(pi, pj, k, bits);
140
- // System.out.println("pi = " + pi + " pj= " + pj + " bits = " + bits);
141
- }
142
- if (orientation != null) {
143
- // The position of a non-leaf cell at level "n" consists of a prefix of
144
- // 2*n bits that identifies the cell, followed by a suffix of
145
- // 2*(MAX_LEVEL-n)+1 bits of the form 10*. If n==MAX_LEVEL, the suffix is
146
- // just "1" and has no effect. Otherwise, it consists of "10", followed
147
- // by (MAX_LEVEL-n-1) repetitions of "00", followed by "0". The "10" has
148
- // no effect, while each occurrence of "00" has the effect of reversing
149
- // the kSwapMask bit.
150
- // assert (S2.POS_TO_ORIENTATION[2] == 0);
151
- // assert (S2.POS_TO_ORIENTATION[0] == S2.SWAP_MASK);
152
- if ((Long.fromString('0x1111111111111110', true, 16).and(this.lowestOnBit()).notEquals(0))) {
153
- bits ^= S2_1.S2.SWAP_MASK;
154
- }
155
- orientation.val = bits;
156
- }
157
- return face;
158
- }
159
- toIJOrientation() {
160
- const face = this.face;
161
- let bits = (face & S2_1.S2.SWAP_MASK);
162
- // System.out.println("face = " + face + " bits = " + bits);
163
- // Each iteration maps 8 bits of the Hilbert curve position into
164
- // 4 bits of "i" and "j". The lookup table transforms a key of the
165
- // form "ppppppppoo" to a value of the form "iiiijjjjoo", where the
166
- // letters [ijpo] represents bits of "i", "j", the Hilbert curve
167
- // position, and the Hilbert curve orientation respectively.
168
- //
169
- // On the first iteration we need to be careful to clear out the bits
170
- // representing the cube face.
171
- let i = 0;
172
- let j = 0;
173
- for (let k = 7; k >= 0; --k) {
174
- const nbits = (k == 7) ? (S2CellId.MAX_LEVEL - 7 * S2CellId.LOOKUP_BITS) : S2CellId.LOOKUP_BITS;
175
- bits += (this.id
176
- .shiftRightUnsigned((k * 2 * S2CellId.LOOKUP_BITS + 1))
177
- .getLowBitsUnsigned()
178
- & ((1 << (2 * nbits)) - 1)) << 2;
179
- /*
180
- * System.out.println("id is: " + id_); System.out.println("bits is " +
181
- * bits); System.out.println("lookup_ij[bits] is " + lookup_ij[bits]);
182
- */
183
- bits = S2CellId.LOOKUP_IJ[bits];
184
- i = i + ((bits >> (S2CellId.LOOKUP_BITS + 2)) << (k * S2CellId.LOOKUP_BITS));
185
- // i.setValue(i.intValue() + ((bits >> (LOOKUP_BITS + 2)) << (k * LOOKUP_BITS)));
186
- /*
187
- * System.out.println("left is " + ((bits >> 2) & ((1 << kLookupBits) -
188
- * 1))); System.out.println("right is " + (k * kLookupBits));
189
- * System.out.println("j is: " + j.intValue()); System.out.println("addition
190
- * is: " + ((((bits >> 2) & ((1 << kLookupBits) - 1))) << (k *
191
- * kLookupBits)));
192
- */
193
- j = j + ((((bits >> 2) & ((1 << S2CellId.LOOKUP_BITS) - 1))) << (k * S2CellId.LOOKUP_BITS));
194
- bits &= (S2_1.S2.SWAP_MASK | S2_1.S2.INVERT_MASK);
195
- }
196
- if ((Long.fromString('0x1111111111111110', true, 16).and(this.lowestOnBit()).notEquals(0))) {
197
- bits ^= S2_1.S2.SWAP_MASK;
198
- }
199
- const orientation = bits;
200
- return Long.fromInt(i).shiftLeft(S2CellId.I_SHIFT).or(Long.fromInt(j).shiftLeft(S2CellId.J_SHIFT)).or(orientation);
201
- }
202
- getI() {
203
- return S2CellId.getI(this.toIJOrientation());
204
- }
205
- static getI(ijo) {
206
- return ijo.shiftRightUnsigned(this.I_SHIFT).toInt();
207
- }
208
- getJ() {
209
- return S2CellId.getJ(this.toIJOrientation());
210
- }
211
- static getJ(ijo) {
212
- return ijo.shiftRightUnsigned(this.J_SHIFT).and(S2CellId.J_MASK).toInt();
213
- }
214
- static getOrientation(ijo) {
215
- return ijo.and(S2CellId.ORIENTATION_MASK).toInt();
216
- }
217
- /**
218
- * Return true if this is a leaf cell (more efficient than checking whether
219
- * level() == MAX_LEVEL).
220
- */
221
- isLeaf() {
222
- return this.id.and(1).getLowBits() != 0;
223
- }
224
- /**
225
- * Return the cell at the previous level or at the given level (which must be
226
- * less than or equal to the current level).
227
- */
228
- parentL(level) {
229
- // assert (isValid() && level >= 0 && level <= this.level());
230
- const newLsb = S2CellId.lowestOnBitForLevel(level);
231
- return new S2CellId(this.id.and(newLsb.negate()).or(newLsb));
232
- // return new S2CellId((id & -newLsb) | newLsb);
233
- }
234
- parent() {
235
- // assert (isValid() && level() > 0);
236
- const newLsb = this.lowestOnBit().shiftLeft(2);
237
- // return new S2CellId((id & -newLsb) | newLsb);
238
- return new S2CellId(this.id.and(newLsb.negate()).or(newLsb));
239
- }
240
- /**
241
- * Return a cell given its face (range 0..5), 61-bit Hilbert curve position
242
- * within that face, and level (range 0..MAX_LEVEL). The given position will
243
- * be modified to correspond to the Hilbert curve position at the center of
244
- * the returned cell. This is a static function rather than a constructor in
245
- * order to give names to the arguments.
246
- */
247
- static fromFacePosLevel(face, pos, level) {
248
- // equivalent to pos | 1
249
- return new S2CellId(new Long(face)
250
- .shiftLeft(S2CellId.POS_BITS)
251
- .add(pos.or(1))).parentL(level);
252
- // return new S2CellId((((long) face) << POS_BITS) + (pos | 1)).parent(level);
253
- }
254
- static fromFace(face) {
255
- return new S2CellId(S2CellId.fromFaceAsLong(face));
256
- }
257
- // /**
258
- // * Return the leaf cell containing the given point (a direction vector, not
259
- // * necessarily unit length).
260
- // */
261
- static fromPoint(p) {
262
- const face = S2Projections_1.S2Projections.xyzToFaceP(p);
263
- const t = S2Projections_1.S2Projections.faceToUvTransform(face);
264
- const i = S2Projections_1.S2Projections.stToIj(R2Vector_1.R2Vector.singleUVToST(t.xyzToU(p.x, p.y, p.z)));
265
- const j = S2Projections_1.S2Projections.stToIj(R2Vector_1.R2Vector.singleUVToST(t.xyzToV(p.x, p.y, p.z)));
266
- return this.fromFaceIJ(face, i, j);
267
- }
268
- //
269
- //
270
- // /** Return the leaf cell containing the given S2LatLng. */
271
- // public static S2CellId fromLatLng(S2LatLng ll) {
272
- // return fromPoint(ll.toPoint());
273
- // }
274
- getCenterUV() {
275
- const center = this.getCenterSiTi();
276
- return new R2Vector_1.R2Vector(R2Vector_1.R2Vector.singleStTOUV(S2Projections_1.S2Projections.siTiToSt(S2CellId.getSi(center))), R2Vector_1.R2Vector.singleStTOUV(S2Projections_1.S2Projections.siTiToSt(S2CellId.getTi(center))));
277
- }
278
- toPoint() {
279
- return S2Point_1.S2Point.normalize(this.toPointRaw());
280
- }
281
- getCenterSiTi() {
282
- const ijo = this.toIJOrientation();
283
- const i = S2CellId.getI(ijo);
284
- const j = S2CellId.getJ(ijo);
285
- const delta = this.isLeaf() ? 1 : ((((new Long(i).getLowBits() ^ ((this.id.getLowBits()) >>> 2)) & 1) != 0) ? 2 : 0);
286
- return Long.fromInt(2 * i + delta).shiftLeft(S2CellId.SI_SHIFT).or(S2CellId.TI_MASK.and(2 * j + delta));
287
- }
288
- static getSi(center) {
289
- return center.shiftRight(S2CellId.SI_SHIFT).toInt();
290
- }
291
- static getTi(center) {
292
- return center.toInt();
293
- }
294
- /**
295
- * Return the direction vector corresponding to the center of the given cell.
296
- * The vector returned by ToPointRaw is not necessarily unit length.
297
- */
298
- toPointRaw() {
299
- const center = this.getCenterSiTi();
300
- return S2Projections_1.S2Projections.faceSiTiToXYZ(this.face, S2CellId.getSi(center), S2CellId.getTi(center));
301
- }
302
- /** Return the S2LatLng corresponding to the center of the given cell. */
303
- toLatLng() {
304
- return S2LatLng_1.S2LatLng.fromPoint(this.toPointRaw());
305
- }
306
- /** Return true if id() represents a valid cell. */
307
- isValid() {
308
- return this.face < S2CellId.NUM_FACES && ((this.lowestOnBit().and(Long.fromString('0x1555555555555555', false, 16)).notEquals(0)));
309
- // return this.face() < NUM_FACES && ((lowestOnBit() & (0x1555555555555555L)) != 0);
310
- }
311
- /**
312
- * The position of the cell center along the Hilbert curve over this face, in
313
- * the range 0..(2**kPosBits-1).
314
- */
315
- pos() {
316
- return this.id.and(S2CellId.MAX_UNSIGNED.shiftRightUnsigned(S2CellId.FACE_BITS));
317
- // return (id & (-1L >>> FACE_BITS));
318
- }
319
- /** Return the subdivision level of the cell (range 0..MAX_LEVEL). */
320
- level() {
321
- // Fast path for leaf cells.
322
- if (this.isLeaf()) {
323
- return S2CellId.MAX_LEVEL;
324
- }
325
- let x = this.id.getLowBits();
326
- let level = -1;
327
- if (x != 0) {
328
- level += 16;
329
- }
330
- else {
331
- x = this.id.shiftRightUnsigned(32).getLowBits();
332
- // (int) (id >>> 32);
333
- }
334
- // We only need to look at even-numbered bits to determine the
335
- // level of a valid cell id.
336
- x &= -x; // Get lowest bit.
337
- if ((x & 0x00005555) != 0) {
338
- level += 8;
339
- }
340
- if ((x & 0x00550055) != 0) {
341
- level += 4;
342
- }
343
- if ((x & 0x05050505) != 0) {
344
- level += 2;
345
- }
346
- if ((x & 0x11111111) != 0) {
347
- level += 1;
348
- }
349
- // assert (level >= 0 && level <= MAX_LEVEL);
350
- return level;
351
- }
352
- getSizeIJ() {
353
- return S2CellId.getSizeIJ(this.level());
354
- }
355
- static getSizeIJ(level) {
356
- return 1 << (S2_1.S2.MAX_LEVEL - level);
357
- }
358
- getSizeST() {
359
- return S2CellId.getSizeST(this.level());
360
- }
361
- static getSizeST(level) {
362
- return S2Projections_1.S2Projections.ijToStMin(S2CellId.getSizeIJ(level));
363
- }
364
- /**
365
- * Return true if this is a top-level face cell (more efficient than checking
366
- * whether level() == 0).
367
- */
368
- isFace() {
369
- return this.level() === 0;
370
- // return (id & (lowestOnBitForLevel(0) - 1)) == 0;
371
- }
372
- /**
373
- * Return the child position (0..3) of this cell's ancestor at the given
374
- * level, relative to its parent. The argument should be in the range
375
- * 1..MAX_LEVEL. For example, child_position(1) returns the position of this
376
- * cell's level-1 ancestor within its top-level face cell.
377
- */
378
- childPosition(level) {
379
- return this.id.shiftRight((2 * (S2CellId.MAX_LEVEL - level) + 1)).and(3).getLowBits();
380
- // return (int) (id >>> (2 * (MAX_LEVEL - level) + 1)) & 3;
381
- }
382
- // Methods that return the range of cell ids that are contained
383
- // within this cell (including itself). The range is *inclusive*
384
- // (i.e. test using >= and <=) and the return values of both
385
- // methods are valid leaf cell ids.
386
- //
387
- // These methods should not be used for iteration. If you want to
388
- // iterate through all the leaf cells, call child_begin(MAX_LEVEL) and
389
- // child_end(MAX_LEVEL) instead.
390
- //
391
- // It would in fact be error-prone to define a range_end() method,
392
- // because (range_max().id() + 1) is not always a valid cell id, and the
393
- // iterator would need to be tested using "<" rather that the usual "!=".
394
- rangeMin() {
395
- return new S2CellId(this.id.sub(this.lowestOnBit().sub(1)));
396
- // return new S2CellId(id - (lowestOnBit() - 1));
397
- }
398
- rangeMax() {
399
- return new S2CellId(this.id.add(this.lowestOnBit().sub(1)));
400
- // return new S2CellId(id + (lowestOnBit() - 1));
401
- }
402
- //
403
- //
404
- /** Return true if the given cell is contained within this one. */
405
- contains(other) {
406
- // assert (isValid() && other.isValid());
407
- return other.greaterOrEquals(this.rangeMin()) && other.lessOrEquals(this.rangeMax());
408
- }
409
- /** Return true if the given cell intersects this one. */
410
- intersects(other) {
411
- // assert (isValid() && other.isValid());
412
- return other.rangeMin().lessOrEquals(this.rangeMax())
413
- && other.rangeMax().greaterOrEquals(this.rangeMin());
414
- }
415
- childBegin() {
416
- // assert (isValid() && level() < MAX_LEVEL);
417
- return new S2CellId(S2CellId.childBeginAsLong(this.id));
418
- }
419
- childBeginL(level) {
420
- // assert (isValid() && level >= this.level() && level <= MAX_LEVEL);
421
- return new S2CellId(S2CellId.childBeginAsLongL(this.id, level));
422
- }
423
- childEnd() {
424
- // assert (isValid() && level() < MAX_LEVEL);
425
- return new S2CellId(S2CellId.childEndAsLong(this.id));
426
- }
427
- childEndL(level) {
428
- // assert (isValid() && level >= this.level() && level <= MAX_LEVEL);
429
- return new S2CellId(S2CellId.childEndAsLongL(this.id, level));
430
- }
431
- static childBeginAsLong(id) {
432
- const oldLsb = S2CellId.lowestOnBit(id);
433
- return id.subtract(oldLsb).add(oldLsb.shiftRightUnsigned(2));
434
- }
435
- static childBeginAsLongL(id, level) {
436
- return id.subtract(S2CellId.lowestOnBit(id)).add(S2CellId.lowestOnBitForLevel(level));
437
- }
438
- static childEndAsLong(id) {
439
- const oldLsb = S2CellId.lowestOnBit(id);
440
- return id.add(oldLsb).add(oldLsb.shiftRightUnsigned(2));
441
- }
442
- static childEndAsLongL(id, level) {
443
- return id.add(S2CellId.lowestOnBit(id)).add(S2CellId.lowestOnBitForLevel(level));
444
- }
445
- static fromFaceAsLong(face) {
446
- return Long.fromInt(face).shiftLeft(S2CellId.POS_BITS).add(S2CellId.lowestOnBitForLevel(0));
447
- }
448
- //
449
- // Iterator-style methods for traversing the immediate children of a cell or
450
- // all of the children at a given level (greater than or equal to the current
451
- // level). Note that the end value is exclusive, just like standard STL
452
- // iterators, and may not even be a valid cell id. You should iterate using
453
- // code like this:
454
- //
455
- // for(S2CellId c = id.childBegin(); !c.equals(id.childEnd()); c = c.next())
456
- // ...
457
- //
458
- // The convention for advancing the iterator is "c = c.next()", so be sure
459
- // to use 'equals()' in the loop guard, or compare 64-bit cell id's,
460
- // rather than "c != id.childEnd()".
461
- /**
462
- * Return the next cell at the same level along the Hilbert curve. Works
463
- * correctly when advancing from one face to the next, but does *not* wrap
464
- * around from the last face to the first or vice versa.
465
- */
466
- next() {
467
- return new S2CellId(this.id.add(this.lowestOnBit().shiftLeft(1)));
468
- // return new S2CellId(id + (lowestOnBit() << 1));
469
- }
470
- /**
471
- * Return the previous cell at the same level along the Hilbert curve. Works
472
- * correctly when advancing from one face to the next, but does *not* wrap
473
- * around from the last face to the first or vice versa.
474
- */
475
- prev() {
476
- return new S2CellId(this.id.sub(this.lowestOnBit().shiftLeft(1)));
477
- // return new S2CellId(id - (lowestOnBit() << 1));
478
- }
479
- /**
480
- * Like next(), but wraps around from the last face to the first and vice
481
- * versa. Should *not* be used for iteration in conjunction with
482
- * child_begin(), child_end(), Begin(), or End().
483
- */
484
- nextWrap() {
485
- const n = this.next();
486
- if (S2CellId.unsignedLongLessThan(n.id, S2CellId.WRAP_OFFSET)) {
487
- return n;
488
- }
489
- return new S2CellId(n.id.sub(S2CellId.WRAP_OFFSET));
490
- // return new S2CellId(n.id - WRAP_OFFSET);
491
- }
492
- /**
493
- * Like prev(), but wraps around from the last face to the first and vice
494
- * versa. Should *not* be used for iteration in conjunction with
495
- * child_begin(), child_end(), Begin(), or End().
496
- */
497
- prevWrap() {
498
- const p = this.prev();
499
- if (p.id.lessThan(S2CellId.WRAP_OFFSET)) {
500
- return p;
501
- }
502
- return new S2CellId(p.id.add(S2CellId.WRAP_OFFSET));
503
- }
504
- static begin(level) {
505
- return S2CellId.fromFacePosLevel(0, new Long(0), 0).childBeginL(level);
506
- }
507
- static end(level) {
508
- return S2CellId.fromFacePosLevel(5, new Long(0), 0).childEndL(level);
509
- }
510
- /**
511
- * Decodes the cell id from a compact text string suitable for display or
512
- * indexing. Cells at lower levels (i.e. larger cells) are encoded into
513
- * fewer characters. The maximum token length is 16.
514
- *
515
- * @param token the token to decode
516
- * @return the S2CellId for that token
517
- * @throws NumberFormatException if the token is not formatted correctly
518
- */
519
- static fromToken(token) {
520
- if (token == null) {
521
- throw new Error("Null string in S2CellId.fromToken");
522
- }
523
- if (token.length == 0) {
524
- throw new Error("Empty string in S2CellId.fromToken");
525
- }
526
- if (token.length > 16 || "X" == token) {
527
- return S2CellId.none();
528
- }
529
- let value = new Long(0);
530
- for (let pos = 0; pos < 16; pos++) {
531
- let digit = new Long(0);
532
- if (pos < token.length) {
533
- digit = Long.fromString(token[pos], true, 16);
534
- if (digit.equals(-1)) {
535
- throw new Error(token);
536
- }
537
- if (S2CellId.overflowInParse(value, digit.toNumber())) {
538
- throw new Error("Too large for unsigned long: " + token);
539
- }
540
- }
541
- value = value.mul(16).add(digit);
542
- // (value * 16) + digit;
543
- }
544
- return new S2CellId(value);
545
- }
546
- /**
547
- * Encodes the cell id to compact text strings suitable for display or indexing.
548
- * Cells at lower levels (i.e. larger cells) are encoded into fewer characters.
549
- * The maximum token length is 16.
550
- *
551
- * Simple implementation: convert the id to hex and strip trailing zeros. We
552
- * could use base-32 or base-64, but assuming the cells used for indexing
553
- * regions are at least 100 meters across (level 16 or less), the savings
554
- * would be at most 3 bytes (9 bytes hex vs. 6 bytes base-64).
555
- *
556
- * @return the encoded cell id
557
- */
558
- toToken() {
559
- if (this.id.equals(0)) {
560
- return "X";
561
- }
562
- const hex = this.id.toUnsigned().toString(16);
563
- // Long.toHexString(id).toLowerCase(Locale.ENGLISH);
564
- let sb = '';
565
- for (let i = hex.length; i < 16; i++) {
566
- sb += '0';
567
- // sb.append('0');
568
- }
569
- sb += hex;
570
- // sb.append(hex);
571
- for (let len = 16; len > 0; len--) {
572
- if (sb[len - 1] != '0') {
573
- return sb.substring(0, len);
574
- }
575
- }
576
- throw new Error("Shouldn't make it here");
577
- }
578
- /**
579
- * Returns true if (current * radix) + digit is a number too large to be
580
- * represented by an unsigned long. This is useful for detecting overflow
581
- * while parsing a string representation of a number.
582
- * Does not verify whether supplied radix is valid, passing an invalid radix
583
- * will give undefined results or an ArrayIndexOutOfBoundsException.
584
- */
585
- static overflowInParse(current, digit, radix = 10) {
586
- if (current.greaterThanOrEqual(0)) {
587
- if (current.lessThan(S2CellId.maxValueDivs[radix])) {
588
- return false;
589
- }
590
- if (current.greaterThan(S2CellId.maxValueDivs[radix])) {
591
- return true;
592
- }
593
- // current == maxValueDivs[radix]
594
- return (digit > S2CellId.maxValueMods[radix]);
595
- }
596
- // current < 0: high bit is set
597
- return true;
598
- }
599
- /**
600
- * Return the four cells that are adjacent across the cell's four edges.
601
- * Neighbors are returned in the order defined by S2Cell::GetEdge. All
602
- * neighbors are guaranteed to be distinct.
603
- */
604
- getEdgeNeighbors() {
605
- const level = this.level();
606
- const size = this.getSizeIJ();
607
- const face = this.face;
608
- const ijo = this.toIJOrientation();
609
- const i = S2CellId.getI(ijo);
610
- const j = S2CellId.getJ(ijo);
611
- const neighbors = [];
612
- // Edges 0, 1, 2, 3 are in the S, E, N, W directions.
613
- neighbors.push(S2CellId.fromFaceIJSame(face, i, j - size, j - size >= 0).parentL(level));
614
- neighbors.push(S2CellId.fromFaceIJSame(face, i + size, j, i + size < S2CellId.MAX_SIZE).parentL(level));
615
- neighbors.push(S2CellId.fromFaceIJSame(face, i, j + size, j + size < S2CellId.MAX_SIZE).parentL(level));
616
- neighbors.push(S2CellId.fromFaceIJSame(face, i - size, j, i - size >= 0).parentL(level));
617
- return neighbors;
618
- }
619
- /**
620
- * Return the neighbors of closest vertex to this cell at the given level, by
621
- * appending them to "output". Normally there are four neighbors, but the
622
- * closest vertex may only have three neighbors if it is one of the 8 cube
623
- * vertices.
624
- *
625
- * Requires: level < this.evel(), so that we can determine which vertex is
626
- * closest (in particular, level == MAX_LEVEL is not allowed).
627
- */
628
- getVertexNeighbors(level) {
629
- // "level" must be strictly less than this cell's level so that we can
630
- // determine which vertex this cell is closest to.
631
- // assert (level < this.level());
632
- const ijo = this.toIJOrientation();
633
- const i = S2CellId.getI(ijo);
634
- const j = S2CellId.getJ(ijo);
635
- // Determine the i- and j-offsets to the closest neighboring cell in each
636
- // direction. This involves looking at the next bit of "i" and "j" to
637
- // determine which quadrant of this->parent(level) this cell lies in.
638
- const halfsize = S2CellId.getSizeIJ(level + 1);
639
- const size = halfsize << 1;
640
- let isame, jsame;
641
- let ioffset, joffset;
642
- if ((i & halfsize) != 0) {
643
- ioffset = size;
644
- isame = (i + size) < S2CellId.MAX_SIZE;
645
- }
646
- else {
647
- ioffset = -size;
648
- isame = (i - size) >= 0;
649
- }
650
- if ((j & halfsize) != 0) {
651
- joffset = size;
652
- jsame = (j + size) < S2CellId.MAX_SIZE;
653
- }
654
- else {
655
- joffset = -size;
656
- jsame = (j - size) >= 0;
657
- }
658
- const toRet = [];
659
- const face = this.face;
660
- toRet.push(this.parentL(level));
661
- toRet.push(S2CellId.fromFaceIJSame(face, i + ioffset, j, isame).parentL(level));
662
- toRet.push(S2CellId.fromFaceIJSame(face, i, j + joffset, jsame).parentL(level));
663
- if (isame || jsame) {
664
- toRet.push(S2CellId.fromFaceIJSame(face, i + ioffset, j + joffset, isame && jsame).parentL(level));
665
- }
666
- return toRet;
667
- }
668
- /**
669
- * Append all neighbors of this cell at the given level to "output". Two cells
670
- * X and Y are neighbors if their boundaries intersect but their interiors do
671
- * not. In particular, two cells that intersect at a single point are
672
- * neighbors.
673
- *
674
- * Requires: nbr_level >= this->level(). Note that for cells adjacent to a
675
- * face vertex, the same neighbor may be appended more than once.
676
- */
677
- getAllNeighbors(nbrLevel) {
678
- const ijo = this.toIJOrientation();
679
- // Find the coordinates of the lower left-hand leaf cell. We need to
680
- // normalize (i,j) to a known position within the cell because nbrLevel
681
- // may be larger than this cell's level.
682
- const size = this.getSizeIJ();
683
- const face = this.face;
684
- const i = S2CellId.getI(ijo) & -size;
685
- const j = S2CellId.getJ(ijo) & -size;
686
- const nbrSize = S2CellId.getSizeIJ(nbrLevel);
687
- // assert (nbrSize <= size);
688
- const output = [];
689
- // We compute the N-S, E-W, and diagonal neighbors in one pass.
690
- // The loop test is at the end of the loop to avoid 32-bit overflow.
691
- for (let k = -nbrSize;; k += nbrSize) {
692
- let sameFace;
693
- if (k < 0) {
694
- sameFace = (j + k >= 0);
695
- }
696
- else if (k >= size) {
697
- sameFace = (j + k < S2CellId.MAX_SIZE);
698
- }
699
- else {
700
- sameFace = true;
701
- // North and South neighbors.
702
- output.push(S2CellId.fromFaceIJSame(face, i + k, j - nbrSize, j - size >= 0).parentL(nbrLevel));
703
- output.push(S2CellId.fromFaceIJSame(face, i + k, j + size, j + size < S2CellId.MAX_SIZE).parentL(nbrLevel));
704
- }
705
- // East, West, and Diagonal neighbors.
706
- output.push(S2CellId.fromFaceIJSame(face, i - nbrSize, j + k, sameFace && i - size >= 0).parentL(nbrLevel));
707
- output.push(S2CellId.fromFaceIJSame(face, i + size, j + k, sameFace && i + size < S2CellId.MAX_SIZE).parentL(nbrLevel));
708
- if (k >= size) {
709
- break;
710
- }
711
- }
712
- return output;
713
- }
714
- // ///////////////////////////////////////////////////////////////////
715
- // Low-level methods.
716
- /**
717
- * Return a leaf cell given its cube face (range 0..5) and i- and
718
- * j-coordinates (see s2.h).
719
- */
720
- static fromFaceIJ(face, i, j) {
721
- // Optimization notes:
722
- // - Non-overlapping bit fields can be combined with either "+" or "|".
723
- // Generally "+" seems to produce better code, but not always.
724
- // gcc doesn't have very good code generation for 64-bit operations.
725
- // We optimize this by computing the result as two 32-bit integers
726
- // and combining them at the end. Declaring the result as an array
727
- // rather than local variables helps the compiler to do a better job
728
- // of register allocation as well. Note that the two 32-bits halves
729
- // get shifted one bit to the left when they are combined.
730
- const faceL = new Long(face);
731
- const n = [new Long(0), faceL.shiftLeft(S2CellId.POS_BITS - 33)];
732
- // Alternating faces have opposite Hilbert curve orientations; this
733
- // is necessary in order for all faces to have a right-handed
734
- // coordinate system.
735
- let bits = faceL.and(S2CellId.SWAP_MASK);
736
- // Each iteration maps 4 bits of "i" and "j" into 8 bits of the Hilbert
737
- // curve position. The lookup table transforms a 10-bit key of the form
738
- // "iiiijjjjoo" to a 10-bit value of the form "ppppppppoo", where the
739
- // letters [ijpo] denote bits of "i", "j", Hilbert curve position, and
740
- // Hilbert curve orientation respectively.
741
- for (let k = 7; k >= 0; --k) {
742
- bits = S2CellId.getBits(n, i, j, k, bits);
743
- }
744
- // S2CellId s = new S2CellId((((n[1] << 32) + n[0]) << 1) + 1);
745
- return new S2CellId(n[1].shiftLeft(32)
746
- .add(n[0])
747
- .shiftLeft(1)
748
- .add(1));
749
- }
750
- static getBits(n, i, j, k, bits) {
751
- const mask = new Long(1).shiftLeft(S2CellId.LOOKUP_BITS).sub(1);
752
- bits = bits.add(new Long(i)
753
- .shiftRight(k * S2CellId.LOOKUP_BITS)
754
- .and(mask)
755
- .shiftLeft(S2CellId.LOOKUP_BITS + 2));
756
- // bits += (((i >> (k * LOOKUP_BITS)) & mask) << (LOOKUP_BITS + 2));
757
- bits = bits.add(new Long(j)
758
- .shiftRight(k * S2CellId.LOOKUP_BITS)
759
- .and(mask)
760
- .shiftLeft(2));
761
- // bits += (((j >> (k * LOOKUP_BITS)) & mask) << 2);
762
- bits = S2CellId.LOOKUP_POS[bits.toNumber()];
763
- n[k >> 2] = n[k >> 2].or(bits.shiftRight(2).shiftLeft((k & 3) * 2 * S2CellId.LOOKUP_BITS));
764
- // n[k >> 2] |= ((((long) bits) >> 2) << ((k & 3) * 2 * LOOKUP_BITS));
765
- return bits.and(S2CellId.SWAP_MASK | S2CellId.INVERT_MASK);
766
- }
767
- /**
768
- * Return the i- or j-index of the leaf cell containing the given s- or
769
- * t-value.
770
- */
771
- static stToIJ(s) {
772
- // Converting from floating-point to integers via static_cast is very slow
773
- // on Intel processors because it requires changing the rounding mode.
774
- // Rounding to the nearest integer using FastIntRound() is much faster.
775
- const m = S2CellId.MAX_SIZE / 2; // scaling multiplier
776
- return Math.max(0, Math.min(m * 2 - 1, Math.round(m * s + m - 0.5)));
777
- // return Math.max(0, Math.min(2 * m - 1, Math.round(m * s + (m - 0.5))));
778
- // return (int) Math.max(0, Math.min(2 * m - 1, Math.round(m * s + (m - 0.5))));
779
- }
780
- /**
781
- * Given (i, j) coordinates that may be out of bounds, normalize them by
782
- * returning the corresponding neighbor cell on an adjacent face.
783
- */
784
- static fromFaceIJWrap(face, i, j) {
785
- // Convert i and j to the coordinates of a leaf cell just beyond the
786
- // boundary of this face. This prevents 32-bit overflow in the case
787
- // of finding the neighbors of a face cell, and also means that we
788
- // don't need to worry about the distinction between (s,t) and (u,v).
789
- i = Math.max(-1, Math.min(S2CellId.MAX_SIZE, i));
790
- j = Math.max(-1, Math.min(S2CellId.MAX_SIZE, j));
791
- // Find the (s,t) coordinates corresponding to (i,j). At least one
792
- // of these coordinates will be just outside the range [0, 1].
793
- const kScale = 1 / S2CellId.MAX_SIZE;
794
- const s = kScale * (new Long(i).shiftLeft(1).add(1).sub(S2CellId.MAX_SIZE).toInt());
795
- const t = kScale * (new Long(j).shiftLeft(1).add(1).sub(S2CellId.MAX_SIZE).toInt());
796
- // Find the leaf cell coordinates on the adjacent face, and convert
797
- // them to a cell id at the appropriate level.
798
- const p = new R2Vector_1.R2Vector(s, t).toPoint(face);
799
- face = p.toFace();
800
- // face = S2Projections.xyzToFace(p);
801
- const st = p.toR2Vector(face);
802
- // R2Vector st = S2Projections.validFaceXyzToUv(face, p);
803
- return S2CellId.fromFaceIJ(face, S2CellId.stToIJ(st.x), S2CellId.stToIJ(st.y));
804
- }
805
- /**
806
- * Public helper function that calls FromFaceIJ if sameFace is true, or
807
- * FromFaceIJWrap if sameFace is false.
808
- */
809
- static fromFaceIJSame(face, i, j, sameFace) {
810
- if (sameFace) {
811
- return S2CellId.fromFaceIJ(face, i, j);
812
- }
813
- else {
814
- return S2CellId.fromFaceIJWrap(face, i, j);
815
- }
816
- }
817
- /**
818
- * Returns true if x1 < x2, when both values are treated as unsigned.
819
- */
820
- static unsignedLongLessThan(x1, x2) {
821
- return x1.toUnsigned().lessThan(x2.toUnsigned());
822
- // return (x1 + Long.MIN_VALUE) < (x2 + Long.MIN_VALUE);
823
- }
824
- /**
825
- * Returns true if x1 > x2, when both values are treated as unsigned.
826
- */
827
- static unsignedLongGreaterThan(x1, x2) {
828
- return x1.toUnsigned().greaterThan(x2.toUnsigned());
829
- // return (x1 + Long.MIN_VALUE) > (x2 + Long.MIN_VALUE);
830
- }
831
- lessThan(x) {
832
- return S2CellId.unsignedLongLessThan(this.id, x.id);
833
- }
834
- greaterThan(x) {
835
- return S2CellId.unsignedLongGreaterThan(this.id, x.id);
836
- }
837
- lessOrEquals(x) {
838
- return S2CellId.unsignedLongLessThan(this.id, x.id) || this.id.equals(x.id);
839
- }
840
- greaterOrEquals(x) {
841
- return S2CellId.unsignedLongGreaterThan(this.id, x.id) || this.id.equals(x.id);
842
- }
843
- toString() {
844
- return "(face=" + this.face + ", pos=" + this.pos().toString(16) + ", level="
845
- + this.level() + ")";
846
- }
847
- compareTo(that) {
848
- return S2CellId.unsignedLongLessThan(this.id, that.id) ? -1 :
849
- S2CellId.unsignedLongGreaterThan(this.id, that.id) ? 1 : 0;
850
- }
851
- equals(that) {
852
- return this.compareTo(that) === 0;
853
- }
854
- /**
855
- * Returns the position of the id within the given list or a negative value with
856
- * the position of the index wher eit should be entered if the id was present
857
- */
858
- static binarySearch(ids, _id, low = 0) {
859
- let id;
860
- if (_id instanceof S2CellId) {
861
- id = _id;
862
- }
863
- else if (_id instanceof Long) {
864
- id = new S2CellId(_id);
865
- }
866
- let high = ids.length - 1;
867
- while (low <= high) {
868
- const mid = (low + high) >>> 1;
869
- const midVal = ids[mid];
870
- const cmp = midVal.compareTo(id);
871
- if (cmp < 0)
872
- low = mid + 1;
873
- else if (cmp > 0)
874
- high = mid - 1;
875
- else
876
- return mid; // key found
877
- }
878
- return -(low + 1); // key not found
879
- }
880
- static indexedBinarySearch(ids, id, low = 0) {
881
- const toRet = this.binarySearch(ids, id, low);
882
- if (toRet >= 0) {
883
- return toRet;
884
- }
885
- else {
886
- return -(toRet + 1);
887
- }
888
- }
889
- }
890
- exports.S2CellId = S2CellId;
891
- // Although only 60 bits are needed to represent the index of a leaf
892
- // cell, we need an extra bit in order to represent the position of
893
- // the center of the leaf cell along the Hilbert curve.
894
- S2CellId.FACE_BITS = 3;
895
- S2CellId.NUM_FACES = 6;
896
- S2CellId.MAX_LEVEL = 30; // Valid levels: 0..MAX_LEVEL
897
- S2CellId.POS_BITS = 2 * S2CellId.MAX_LEVEL + 1;
898
- S2CellId.MAX_SIZE = 1 << S2CellId.MAX_LEVEL;
899
- //
900
- // calculated as 0xffffffffffffffff / radix
901
- S2CellId.maxValueDivs = [new Long(0), new Long(0),
902
- parseHex('9223372036854775807'), parseHex('6148914691236517205'), parseHex('4611686018427387903'),
903
- parseHex('3689348814741910323'), parseHex('3074457345618258602'), parseHex('2635249153387078802'),
904
- parseHex('2305843009213693951'), parseHex('2049638230412172401'), parseHex('1844674407370955161'),
905
- parseHex('1676976733973595601'), parseHex('1537228672809129301'), parseHex('1418980313362273201'),
906
- parseHex('1317624576693539401'), parseHex('1229782938247303441'), parseHex('1152921504606846975'),
907
- parseHex('1085102592571150095'), parseHex('1024819115206086200'), parseHex('970881267037344821'),
908
- parseHex('922337203685477580'), parseHex('878416384462359600'), parseHex('838488366986797800'),
909
- parseHex('802032351030850070'), parseHex('768614336404564650'), parseHex('737869762948382064'),
910
- parseHex('709490156681136600'), parseHex('683212743470724133'), parseHex('658812288346769700'),
911
- parseHex('636094623231363848'), parseHex('614891469123651720'), parseHex('595056260442243600'),
912
- parseHex('576460752303423487'), parseHex('558992244657865200'), parseHex('542551296285575047'),
913
- parseHex('527049830677415760'), parseHex('512409557603043100')]; // 35-36
914
- // calculated as 0xffffffffffffffff % radix
915
- S2CellId.maxValueMods = [0, 0,
916
- 1, 0, 3, 0, 3, 1, 7, 6, 5, 4, 3, 2, 1, 0, 15, 0, 15, 16, 15, 15,
917
- 15, 5, 15, 15, 15, 24, 15, 23, 15, 15, 31, 15, 17, 15, 15]; // 22-36
918
- // Constant related to unsigned long's
919
- // '18446744073709551615'
920
- // Long.fromString('0xffffffffffffffff', true, 16).toString()
921
- // new Decimal(2).pow(64).sub(1);
922
- S2CellId.MAX_UNSIGNED = Long.fromString('0xffffffffffffffff', true, 16);
923
- // The following lookup tables are used to convert efficiently between an
924
- // (i,j) cell index and the corresponding position along the Hilbert curve.
925
- // "lookup_pos" maps 4 bits of "i", 4 bits of "j", and 2 bits representing the
926
- // orientation of the current cell into 8 bits representing the order in which
927
- // that subcell is visited by the Hilbert curve, plus 2 bits indicating the
928
- // new orientation of the Hilbert curve within that subcell. (Cell
929
- // orientations are represented as combination of kSwapMask and kInvertMask.)
930
- //
931
- // "lookup_ij" is an inverted table used for mapping in the opposite
932
- // direction.
933
- //
934
- // We also experimented with looking up 16 bits at a time (14 bits of position
935
- // plus 2 of orientation) but found that smaller lookup tables gave better
936
- // performance. (2KB fits easily in the primary cache.)
937
- // Values for these constants are *declared* in the *.h file. Even though
938
- // the declaration specifies a value for the constant, that declaration
939
- // is not a *definition* of storage for the value. Because the values are
940
- // supplied in the declaration, we don't need the values here. Failing to
941
- // define storage causes link errors for any code that tries to take the
942
- // address of one of these values.
943
- S2CellId.LOOKUP_BITS = 4;
944
- S2CellId.SWAP_MASK = 0x01;
945
- S2CellId.INVERT_MASK = 0x02;
946
- S2CellId.I_SHIFT = 33;
947
- S2CellId.J_SHIFT = 2;
948
- S2CellId.J_MASK = Long.fromInt(1).shiftLeft(31).subtract(1);
949
- S2CellId.SI_SHIFT = 32;
950
- S2CellId.ORIENTATION_MASK = Long.fromInt(1).shiftLeft(2).subtract(1);
951
- S2CellId.TI_MASK = Long.fromInt(1).shiftLeft(32).subtract(1);
952
- S2CellId.LOOKUP_POS = [];
953
- S2CellId.LOOKUP_IJ = [];
954
- /**
955
- * This is the offset required to wrap around from the beginning of the
956
- * Hilbert curve to the end or vice versa; see next_wrap() and prev_wrap().
957
- */
958
- S2CellId.WRAP_OFFSET = new Long(S2CellId.NUM_FACES).shiftLeft(S2CellId.POS_BITS);
959
- function initLookupCell(level, i, j, origOrientation, pos, orientation) {
960
- if (level == S2CellId.LOOKUP_BITS) {
961
- const ij = (i << S2CellId.LOOKUP_BITS) + j;
962
- S2CellId.LOOKUP_POS[(ij << 2) + origOrientation] = pos.shiftLeft(2).add(orientation);
963
- S2CellId.LOOKUP_IJ[pos.shiftLeft(2).add(origOrientation).toNumber()] = (ij << 2) + orientation;
964
- // new Long((ij << 2)).add(orientation);
965
- }
966
- else {
967
- level++;
968
- i <<= 1;
969
- j <<= 1;
970
- pos = pos.shiftLeft(2);
971
- // Initialize each sub-cell recursively.
972
- for (let subPos = 0; subPos < 4; subPos++) {
973
- const ij = S2_1.S2.POS_TO_IJ[orientation][subPos];
974
- const orientationMask = S2_1.S2.POS_TO_ORIENTATION[subPos];
975
- initLookupCell(level, i + (ij >>> 1), j + (ij & 1), origOrientation, pos.add(subPos), orientation ^ orientationMask);
976
- }
977
- }
978
- }
979
- initLookupCell(0, 0, 0, 0, new Long(0), 0);
980
- initLookupCell(0, 0, 0, S2_1.S2.SWAP_MASK, new Long(0), S2_1.S2.SWAP_MASK);
981
- initLookupCell(0, 0, 0, S2_1.S2.INVERT_MASK, new Long(0), S2_1.S2.INVERT_MASK);
982
- initLookupCell(0, 0, 0, S2_1.S2.SWAP_MASK | S2_1.S2.INVERT_MASK, new Long(0), S2_1.S2.SWAP_MASK | S2_1.S2.INVERT_MASK);
983
- //# sourceMappingURL=S2CellId.js.map