-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SpaceFillingCurve.java
476 lines (411 loc) · 13.5 KB
/
SpaceFillingCurve.java
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gis.spatial.index.curves;
import java.util.ArrayList;
import java.util.List;
import org.neo4j.gis.spatial.index.Envelope;
public abstract class SpaceFillingCurve
{
/**
* Description of the space filling curve structure
*/
abstract static class CurveRule
{
final int dimension;
final int[] npointValues;
CurveRule( int dimension, int[] npointValues )
{
this.dimension = dimension;
this.npointValues = npointValues;
assert npointValues.length == length();
}
final int length()
{
return (int) Math.pow( 2, dimension );
}
int npointForIndex( int derivedIndex )
{
return npointValues[derivedIndex];
}
int indexForNPoint( int npoint )
{
for ( int index = 0; index < npointValues.length; index++ )
{
if ( npointValues[index] == npoint )
{
return index;
}
}
return -1;
}
abstract CurveRule childAt( int npoint );
}
private final Envelope range;
private final int nbrDim;
private final int maxLevel;
private final long width;
private final long valueWidth;
private final int quadFactor;
private final long initialNormMask;
private double[] scalingFactor;
SpaceFillingCurve( Envelope range, int maxLevel )
{
this.range = range;
this.nbrDim = range.getDimension();
this.maxLevel = maxLevel;
if ( maxLevel < 1 )
{
throw new IllegalArgumentException( "Hilbert index needs at least one level" );
}
if ( range.getDimension() > 3 )
{
throw new IllegalArgumentException( "Hilbert index does not yet support more than 3 dimensions" );
}
this.width = (long) Math.pow( 2, maxLevel );
this.scalingFactor = new double[nbrDim];
for ( int dim = 0; dim < nbrDim; dim++ )
{
scalingFactor[dim] = this.width / range.getWidth( dim );
}
this.valueWidth = (long) Math.pow( 2, maxLevel * nbrDim );
this.initialNormMask = (long) (Math.pow( 2, nbrDim ) - 1) << (maxLevel - 1) * nbrDim;
this.quadFactor = (int) Math.pow( 2, nbrDim );
}
public int getMaxLevel()
{
return maxLevel;
}
public long getWidth()
{
return width;
}
public long getValueWidth()
{
return valueWidth;
}
public double getTileWidth( int dimension, int level )
{
return range.getWidth( dimension ) / Math.pow( 2, level );
}
public Envelope getRange()
{
return range;
}
protected abstract CurveRule rootCurve();
/**
* Given a coordinate in multiple dimensions, calculate its derived key for maxLevel
*/
public Long derivedValueFor( double[] coord )
{
return derivedValueFor( coord, maxLevel );
}
/**
* Given a coordinate in multiple dimensions, calculate its derived key for given level
*/
private Long derivedValueFor( double[] coord, int level )
{
assertValidLevel( level );
long[] normalizedValues = getNormalizedCoord( coord );
long derivedValue = 0;
long mask = 1L << (maxLevel - 1);
// The starting curve depends on the dimensions
CurveRule currentCurve = rootCurve();
for ( int i = 1; i <= maxLevel; i++ )
{
int bitIndex = maxLevel - i;
int npoint = 0;
for ( long val : normalizedValues )
{
npoint = npoint << 1 | (int) ((val & mask) >> bitIndex);
}
int derivedIndex = currentCurve.indexForNPoint( npoint );
derivedValue = (derivedValue << nbrDim) | derivedIndex;
mask = mask >> 1;
currentCurve = currentCurve.childAt( derivedIndex );
}
if ( level < maxLevel )
{
derivedValue = derivedValue << (nbrDim * maxLevel - level);
}
return derivedValue;
}
/**
* Given a derived key, find the center coordinate of the corresponding tile at maxLevel
*/
public double[] centerPointFor( long derivedValue )
{
return centerPointFor( derivedValue, maxLevel );
}
/**
* Given a derived key, find the center coordinate of the corresponding tile at given level
*/
private double[] centerPointFor( long derivedValue, int level )
{
long[] normalizedCoord = normalizedCoordinateFor( derivedValue, level );
return getDoubleCoord( normalizedCoord, level );
}
/**
* Given a derived key, find the normalized coordinate it corresponds to on a specific level
*/
long[] normalizedCoordinateFor( long derivedValue, int level )
{
assertValidLevel( level );
long mask = initialNormMask;
long[] coordinate = new long[nbrDim];
// First level is a single curveUp
CurveRule currentCurve = rootCurve();
for ( int i = 1; i <= level; i++ )
{
int bitIndex = maxLevel - i;
int derivedIndex = (int) ((derivedValue & mask) >> bitIndex * nbrDim);
int npoint = currentCurve.npointForIndex( derivedIndex );
int[] bitValues = bitValues( npoint );
for ( int dim = 0; dim < nbrDim; dim++ )
{
coordinate[dim] = coordinate[dim] << 1 | bitValues[dim];
}
mask = mask >> nbrDim;
currentCurve = currentCurve.childAt( derivedIndex );
}
if ( level < maxLevel )
{
for ( int dim = 0; dim < nbrDim; dim++ )
{
coordinate[dim] = coordinate[dim] << maxLevel - level;
}
}
return coordinate;
}
/**
* Given an envelope, find a collection of LongRange of tiles intersecting it on maxLevel and merge adjacent ones
*/
public List<LongRange> getTilesIntersectingEnvelope( Envelope referenceEnvelope )
{
SearchEnvelope search = new SearchEnvelope( referenceEnvelope );
ArrayList<LongRange> results = new ArrayList<>();
addTilesIntersectingEnvelopeAt( search, new SearchEnvelope( 0, this.getWidth(), nbrDim ), rootCurve(), 0, this.getValueWidth(), results );
return results;
}
private void addTilesIntersectingEnvelopeAt( SearchEnvelope search, SearchEnvelope currentExtent, CurveRule curve, long left, long right,
ArrayList<LongRange> results )
{
if ( right - left == 1 )
{
long[] coord = normalizedCoordinateFor( left, maxLevel );
if ( search.contains( coord ) )
{
LongRange current = (results.size() > 0) ? results.get( results.size() - 1 ) : null;
if ( current != null && current.max == left - 1 )
{
current.expandToMax( left );
}
else
{
current = new LongRange( left );
results.add( current );
}
}
}
else if ( search.intersects( currentExtent ) )
{
long width = (right - left) / quadFactor;
for ( int i = 0; i < quadFactor; i++ )
{
int npoint = curve.npointForIndex( i );
SearchEnvelope quadrant = currentExtent.quadrant( bitValues( npoint ) );
addTilesIntersectingEnvelopeAt( search, quadrant, curve.childAt( i ), left + i * width, left + (i + 1) * width, results );
}
}
}
/**
* Bit index describing the in which quadrant an npoint corresponds to
*/
private int[] bitValues( int npoint )
{
int[] bitValues = new int[nbrDim];
for ( int dim = 0; dim < nbrDim; dim++ )
{
int shift = nbrDim - dim - 1;
bitValues[dim] = (npoint & (1 << shift)) >> shift;
}
return bitValues;
}
/**
* Given a coordinate, find the corresponding normalized coordinate
*/
private long[] getNormalizedCoord( double[] coord )
{
long[] normalizedCoord = new long[nbrDim];
for ( int dim = 0; dim < nbrDim; dim++ )
{
double value = clamp( coord[dim], range.getMin( dim ), range.getMax( dim ) );
if ( value == range.getMax( dim ) )
{
normalizedCoord[dim] = valueWidth - 1;
}
else
{
normalizedCoord[dim] = (long) ((value - range.getMin( dim )) * scalingFactor[dim]);
}
}
return normalizedCoord;
}
/**
* Given a normalized coordinate, find the center coordinate of that tile on the given level
*/
private double[] getDoubleCoord( long[] normalizedCoord, int level )
{
double[] coord = new double[nbrDim];
for ( int dim = 0; dim < nbrDim; dim++ )
{
double coordinate = ((double) normalizedCoord[dim]) / scalingFactor[dim] + range.getMin( dim ) + getTileWidth( dim, level ) / 2.0;
coord[dim] = clamp( coordinate, range.getMin( dim ), range.getMax( dim ) );
}
return coord;
}
private double clamp( double val, double min, double max )
{
if ( val <= min )
{
return min;
}
if ( val >= max )
{
return max;
}
return val;
}
/**
* Assert that a given level is valid
*/
private void assertValidLevel( int level )
{
if ( level > maxLevel )
{
throw new IllegalArgumentException( "Level " + level + " greater than max-level " + maxLevel );
}
}
/**
* Class for ranges of tiles
*/
public static class LongRange
{
public final long min;
public long max;
LongRange( long value )
{
this( value, value );
}
LongRange( long min, long max )
{
this.min = min;
this.max = max;
}
void expandToMax( long other )
{
this.max = other;
}
@Override
public boolean equals( Object other )
{
return (other instanceof LongRange) && this.equals( (LongRange) other );
}
public boolean equals( LongRange other )
{
return this.min == other.min && this.max == other.max;
}
@Override
public int hashCode()
{
return (int) (this.min << 16 + this.max);
}
@Override
public String toString()
{
return "LongRange(" + min + "," + max + ")";
}
}
/**
* N-dimensional searchEnvelope
*/
private class SearchEnvelope
{
long[] min;
long[] max;
int nbrDim;
private SearchEnvelope( Envelope referenceEnvelope )
{
this.min = getNormalizedCoord( referenceEnvelope.getMin() );
this.max = getNormalizedCoord( referenceEnvelope.getMax() );
this.nbrDim = referenceEnvelope.getDimension();
}
private SearchEnvelope( long[] min, long[] max )
{
this.min = min;
this.max = max;
this.nbrDim = min.length;
}
private SearchEnvelope( long min, long max, int nbrDim )
{
this.nbrDim = nbrDim;
this.min = new long[nbrDim];
this.max = new long[nbrDim];
for ( int dim = 0; dim < nbrDim; dim++ )
{
this.min[dim] = min;
this.max[dim] = max;
}
}
private SearchEnvelope quadrant( int[] quadNbrs )
{
long[] newMin = new long[nbrDim];
long[] newMax = new long[nbrDim];
for ( int dim = 0; dim < nbrDim; dim++ )
{
long extent = (max[dim] - min[dim]) / 2;
newMin[dim] = this.min[dim] + quadNbrs[dim] * extent;
newMax[dim] = this.min[dim] + (quadNbrs[dim] + 1) * extent;
}
return new SearchEnvelope( newMin, newMax );
}
private boolean contains( long[] coord )
{
for ( int dim = 0; dim < nbrDim; dim++ )
{
if ( coord[dim] < min[dim] || coord[dim] > max[dim] )
{
return false;
}
}
return true;
}
private boolean intersects( SearchEnvelope other )
{
for ( int dim = 0; dim < nbrDim; dim++ )
{
if ( max[dim] < other.min[dim] || other.max[dim] < min[dim] )
{
return false;
}
}
return true;
}
}
}