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