-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
361 lines (334 loc) · 8.14 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
let XYZToFaceUV = function(xyz) {
let largestAbsComponent = function(xyz) {
let temp = [Math.abs(xyz[0]), Math.abs(xyz[1]), Math.abs(xyz[2])];
if (temp[0] > temp[1]) {
if (temp[0] > temp[2]) {
return 0;
} else {
return 2;
}
} else {
if (temp[1] > temp[2]) {
return 1;
} else {
return 2;
}
}
};
let faceXYZToUV = function(face, xyz) {
switch (face) {
case 0:
return [
xyz[1]/xyz[0],
xyz[2]/xyz[0],
];
case 1:
return [
-xyz[0]/xyz[1],
xyz[2]/xyz[1],
];
case 2:
return [
-xyz[0]/xyz[2],
-xyz[1]/xyz[2],
];
case 3:
return [
xyz[2]/xyz[0],
xyz[1]/xyz[0],
];
case 4:
return [
xyz[2]/xyz[1],
-xyz[0]/xyz[1],
];
case 5:
return [
-xyz[1]/xyz[2],
-xyz[0]/xyz[2],
];
default:
throw Error(`Invalid face`);
}
};
let face = largestAbsComponent(xyz);
if (xyz[face] < 0) {
face += 3;
}
let uv = faceXYZToUV(face, xyz);
return [face, uv];
};
let FaceUVToXYZ = function(face, uv) {
let u = uv[0];
let v = uv[1];
switch (face) {
case 0:
return [1, u, v];
case 1:
return [-u, 1, v];
case 2:
return [-u, -v, 1];
case 3:
return [-1, -v, -u];
case 4:
return [v, -1, -u];
case 5:
return [v, u, -1];
default:
throw Error(`Invalid face`);
}
};
let STToUV = function(st) {
let singleSTtoUV = function(st) {
if (st >= 0.5) {
return (1/3.0) * (4*st*st - 1);
} else {
return (1/3.0) * (1 - (4*(1-st)*(1-st)));
}
};
return [
singleSTtoUV(st[0]),
singleSTtoUV(st[1]),
];
};
let UVToST = function(uv) {
let singleUVtoST = function(uv) {
if (uv >= 0) {
return 0.5 * Math.sqrt(1 + 3*uv);
} else {
return 1 - 0.5 * Math.sqrt(1 - 3*uv);
}
};
return [
singleUVtoST(uv[0]),
singleUVtoST(uv[1])
];
};
let STToIJ = function(st, order) {
let maxSize = (1<<order);
let singleSTtoIJ = function(st) {
let ij = Math.floor(st * maxSize);
return Math.max(0, Math.min(maxSize - 1, ij));
};
return [singleSTtoIJ(st[0]), singleSTtoIJ(st[1])];
};
let IJToST = function(ij, order, offsets) {
let maxSize = (1<<order);
return [
(ij[0] + offsets[0]) / maxSize,
(ij[1] + offsets[1]) / maxSize
];
};
export let S2LatLng = class {
constructor() {
this.lat = 0;
this.lng = 0;
}
static from(latitude, longitude, wrap = true) {
if (wrap) {
latitude = Math.max(Math.min(latitude, 90), -90); // clamp latitude into -90..90
longitude = (longitude + 180) % 360 + ((longitude < -180 || longitude == 180) ? 180 : -180); // wrap longtitude into -180..180
}
let self = new this;
self.lat = latitude;
self.lng = longitude;
return self;
}
static fromInteger(id) {
return S2Cell.fromInteger(id).toLatLng();
}
static _fromXYZ(xyz) {
let RAD_TO_DEG = 180 / Math.PI;
let lat = Math.atan2(xyz[2], Math.sqrt(xyz[0]*xyz[0]+xyz[1]*xyz[1]));
let lng = Math.atan2(xyz[1], xyz[0]);
return this.from(lat*RAD_TO_DEG, lng*RAD_TO_DEG);
}
_toXYZ() {
/* http://stackoverflow.com/questions/8981943/lat-long-to-x-y-z-position-in-js-not-working */
let DEG_TO_RAD = Math.PI / 180;
let phi = this.lat*DEG_TO_RAD;
let theta = this.lng*DEG_TO_RAD;
let cosphi = Math.cos(phi);
return [
Math.cos(theta)*cosphi,
Math.sin(theta)*cosphi,
Math.sin(phi),
];
}
toInteger(level) {
return S2Cell.fromLatLng(this, level).toInteger();
}
getNeighbors(level) {
return S2Cell.fromLatLng(this, level).getNeighbors();
}
compareTo(other) {
return this.toInteger() - other.toInteger();
}
};
let S2Point = class {
constructor() {
this.x = 0;
this.y = 0;
}
flip() {
let x = this.x;
this.x = this.y
this.y = x;
}
};
let MAX_LEVEL = 30;
export let S2Cell = class {
constructor() {
this.face = 0;
this.ij = [0, 0];
this.level = MAX_LEVEL;
}
static from(face, ij, level = MAX_LEVEL) {
level = Math.min(Math.max(level, 1), MAX_LEVEL);
let self = new this;
self.face = face;
self.ij = ij;
self.level = level;
return self;
}
static fromLatLng(latLng, level = MAX_LEVEL) {
let xyz = latLng._toXYZ();
let faceuv = XYZToFaceUV(xyz);
let st = UVToST(faceuv[1]);
let ij = STToIJ(st, level);
return this.from(faceuv[0], ij, level);
}
static fromInteger(cellId) {
cellId = BigInt(cellId);
let rotateAndFlipQuadrant = function(n, point, rx, ry) {
if (ry == 0) {
if (rx == 1) {
point.x = n - 1 - point.x;
point.y = n - 1 - point.y;
}
point.flip();
}
}
let face = cellId >> 61n;
let positions = BigInt.asUintN(61, cellId);
let maxLevel = MAX_LEVEL;
let point = new S2Point;
while ((positions & 1n) == 0n) {
positions >>= 2n; // remove zero padding
maxLevel -= 1;
}
positions >>= 1n; // remove 1-bit lsb marker
for (let level = 0; level < maxLevel; level += 1) {
let position = positions & 3n;
let rx = 0;
let ry = 0;
if (position == 1n) {
ry = 1;
} else if (position == 2n) {
rx = 1;
ry = 1;
} else if (position == 3n) {
rx = 1;
}
let value = Math.pow(2, level);
rotateAndFlipQuadrant(value, point, rx, ry);
point.x += value * rx;
point.y += value * ry;
positions >>= 2n;
}
if (face % 2n == 1) {
point.flip();
}
return this.from(Number(face), [point.x, point.y], maxLevel);
}
toLatLng() {
let st = IJToST(this.ij, this.level, [0.5, 0.5]);
let uv = STToUV(st);
let xyz = FaceUVToXYZ(this.face, uv);
return S2LatLng._fromXYZ(xyz);
}
*getCornerLatLngs() {
let offsets = [
[0.0, 0.0],
[0.0, 1.0],
[1.0, 1.0],
[1.0, 0.0]
];
for (let offset of offsets) {
let st = IJToST(this.ij, this.level, offset);
let uv = STToUV(st);
let xyz = FaceUVToXYZ(this.face, uv);
yield S2LatLng._fromXYZ(xyz);
}
}
/* hilbert space-filling curve based on http://blog.notdot.net/2009/11/Damn-Cool-Algorithms-Spatial-indexing-with-Quadtrees-and-Hilbert-Curves */
static _pointToHilbertQuadList(point, order = MAX_LEVEL, face = 0) {
let hilbertMap = {
a: [ [0n, `d`], [1n, `a`], [3n, `b`], [2n, `a`] ],
b: [ [2n, `b`], [1n, `b`], [3n, `a`], [0n, `c`] ],
c: [ [2n, `c`], [3n, `d`], [1n, `c`], [0n, `b`] ],
d: [ [0n, `a`], [3n, `c`], [1n, `d`], [2n, `d`] ],
};
let currentSquare = (face % 2) ? `d` : `a`;
let positions = 0n;
let shift = 64n;
let [x, y] = point;
for (let index = order-1; index >= 0; index -= 1) {
let mask = 1<<index;
let quadX = x&mask ? 1 : 0;
let quadY = y&mask ? 1 : 0;
let [position, nextSquare] = hilbertMap[currentSquare][quadX*2 + quadY];
currentSquare = nextSquare;
shift -= 2n;
positions |= position << shift;
}
return positions;
}
toInteger(_step = 0) {
let quads = S2Cell._pointToHilbertQuadList(this.ij, this.level, this.face);
if (_step) {
let shift = BigInt(64 - this.level*2);
quads = ((quads >> shift) + BigInt(_step)) << shift;
}
return (BigInt(this.face) << 61n) | (quads >> 3n) | (1n << BigInt(60 - this.level*2));
}
*getNeighbors() {
let fromFaceIJWrap = function(face, ij, level) {
let maxSize = 1 << level;
if (ij[0] >= 0 && ij[1] >= 0 && ij[0] < maxSize && ij[1] < maxSize) {
// no wrapping out of bounds
return S2Cell.from(face, ij, level);
} else {
// the new i,j are out of range.
// with the assumption that they're only a little past the borders we can just take the points as just beyond the cube face, project to XYZ, then re-create FaceUV from the XYZ vector
let st = IJToST(ij, level, [0.5, 0.5]);
let uv = STToUV(st);
let xyz = FaceUVToXYZ(face, uv);
let faceuv = XYZToFaceUV(xyz);
face = faceuv[0];
uv = faceuv[1];
st = UVToST(uv);
ij = STToIJ(st,level);
return S2Cell.from(face, ij, level);
}
};
let {face, ij: [i, j], level} = this.face;
yield fromFaceIJWrap(face, [i-1, j], level); // left
yield fromFaceIJWrap(face, [i, j-1], level); // down
yield fromFaceIJWrap(face, [i+1, j], level); // right
yield fromFaceIJWrap(face, [i, j+1], level); // up
}
move(step) {
return S2Cell.fromInteger(this.toInteger(step));
}
includes(other) {
if (other instanceof S2LatLng) {
other = S2Cell.fromLatLng(other);
}
let shift = BigInt(61 - this.level*2);
return (this.toInteger() >> shift) == (other.toInteger() >> shift);
}
compareTo(other) {
return this.toInteger() - other.toInteger();
}
};