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