-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Envelope.java
396 lines (361 loc) · 10.3 KB
/
Envelope.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
/*
* 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;
import java.util.Arrays;
public class Envelope
{
private static final double MAXIMAL_ENVELOPE_SIDE_RATIO = 100;
protected final double[] min;
protected final double[] max;
/**
* Copy constructor
*/
public Envelope( Envelope e )
{
this( e.min, e.max );
}
/**
* General constructor for the n-dimensional case
*/
public Envelope( double[] min, double[] max )
{
this.min = min.clone();
this.max = max.clone();
if ( !isValid() )
{
throw new IllegalArgumentException( "Invalid envelope created " + toString() );
}
}
/**
* Special constructor for the 2D case
*/
public Envelope( double xmin, double xmax, double ymin, double ymax )
{
this( new double[] { xmin, ymin }, new double[] { xmax, ymax } );
}
/**
* @return a copy of the envelope where the ratio of smallest to largest side is not more than 1:100
*/
public Envelope withSideRatioNotTooSmall( )
{
double[] from = Arrays.copyOf( this.min, min.length );
double[] to = Arrays.copyOf( this.max, max.length );
double highestDiff = -Double.MAX_VALUE;
double[] diffs = new double[from.length];
for ( int i = 0; i < from.length; i++ )
{
diffs[i] = to[i] - from[i];
highestDiff = Math.max( highestDiff, diffs[i] );
}
final double mindiff = highestDiff / MAXIMAL_ENVELOPE_SIDE_RATIO;
for ( int i = 0; i < from.length; i++ )
{
if ( diffs[i] < mindiff )
{
to[i] = from[i] + mindiff;
}
}
return new Envelope( from, to );
}
public double[] getMin()
{
return min;
}
public double[] getMax()
{
return max;
}
public double getMin( int dimension )
{
return min[dimension];
}
public double getMax( int dimension )
{
return max[dimension];
}
public double getMinX()
{
return getMin(0);
}
public double getMaxX()
{
return getMax(0);
}
public double getMinY()
{
return getMin(1);
}
public double getMaxY()
{
return getMax(1);
}
public int getDimension()
{
return min.length;
}
/**
* Note that this doesn't exclude the envelope boundary.
* See JTS Envelope.
*/
public boolean contains( Envelope other )
{
return covers(other);
}
public boolean covers( Envelope other )
{
boolean covers = getDimension() == other.getDimension();
for ( int i = 0; i < min.length && covers; i++ )
{
covers = other.min[i] >= min[i] && other.max[i] <= max[i];
}
return covers;
}
public boolean intersects( Envelope other )
{
boolean intersects = getDimension() == other.getDimension();
for ( int i = 0; i < min.length && intersects; i++ )
{
intersects = other.min[i] <= max[i] && other.max[i] >= min[i];
}
return intersects;
}
public void expandToInclude( Envelope other )
{
if ( getDimension() != other.getDimension() )
{
throw new IllegalArgumentException( "Cannot join Envelopes with different dimensions: " + this.getDimension() + " != " + other.getDimension() );
}
else
{
for ( int i = 0; i < min.length; i++ )
{
if ( other.min[i] < min[i] )
{
min[i] = other.min[i];
}
if ( other.max[i] > max[i] )
{
max[i] = other.max[i];
}
}
}
}
@Override
public boolean equals( Object obj )
{
if ( obj instanceof Envelope )
{
Envelope other = (Envelope) obj;
if ( this.getDimension() != other.getDimension() )
{
return false;
}
for ( int i = 0; i < getDimension(); i++ )
{
if ( this.min[i] != other.getMin( i ) || this.max[i] != other.getMax( i ) )
{
return false;
}
}
return true;
}
else
{
return false;
}
}
@Override
public int hashCode()
{
int result = 1;
for ( double element : min )
{
long bits = Double.doubleToLongBits( element );
result = 31 * result + (int) (bits ^ (bits >>> 32));
}
for ( double element : max )
{
long bits = Double.doubleToLongBits( element );
result = 31 * result + (int) (bits ^ (bits >>> 32));
}
return result;
}
/**
* Return the distance between the two envelopes on one dimension. This can return negative values if the envelopes intersect on this dimension.
* @return distance between envelopes
*/
public double distance( Envelope other, int dimension )
{
if ( min[dimension] < other.min[dimension] )
{
return other.min[dimension] - max[dimension];
}
else
{
return min[dimension] - other.max[dimension];
}
}
/**
* Find the pythagorean distance between two envelopes
*/
public double distance( Envelope other )
{
if ( intersects(other) )
{
return 0;
}
double distance = 0.0;
for ( int i = 0; i < min.length; i++ )
{
double dist = distance(other, i);
if ( dist > 0 )
{
distance += dist * dist;
}
}
return Math.sqrt(distance);
}
/**
* @return getWidth(0) for special 2D case with the first dimension being x (width)
*/
public double getWidth()
{
return getWidth(0);
}
/**
* Return the width of the envelope at the specified dimension
* @return with of that dimension, ie. max[d] - min[d]
*/
public double getWidth( int dimension )
{
return max[dimension] - min[dimension];
}
/**
* Return the fractional widths of the envelope at all axes
*
* @param divisor the number of segments to divide by (a 2D envelope will be divided into quadrants using 2)
* @return double array of widths, ie. max[d] - min[d]
*/
public double[] getWidths( int divisor )
{
double[] widths = Arrays.copyOf(max, max.length);
for ( int d = 0; d < max.length; d++ )
{
widths[d] -= min[d];
widths[d] /= divisor;
}
return widths;
}
public double getArea()
{
double area = 1.0;
for ( int i = 0; i < min.length; i++ )
{
area *= max[i] - min[i];
}
return area;
}
public double overlap( Envelope other )
{
Envelope smallest = this.getArea() < other.getArea() ? this : other;
Envelope intersection = this.intersection(other);
return intersection == null ? 0.0 : smallest.isPoint() ? 1.0 : intersection.getArea() / smallest.getArea();
}
public boolean isPoint()
{
boolean ans = true;
for ( int i = 0; i < min.length && ans; i++ )
{
ans = min[i] == max[i];
}
return ans;
}
private boolean isValid()
{
boolean valid = min != null && max != null && min.length == max.length;
for ( int i = 0; valid && i < min.length; i++ )
{
valid = min[i] <= max[i];
}
return valid;
}
@Override
public String toString()
{
return "Envelope: min=" + makeString(min) + ", max=" + makeString(max);
}
private static String makeString( double[] vals )
{
StringBuilder sb = new StringBuilder();
if ( vals == null )
{
sb.append("null");
}
else
{
for ( double val : vals )
{
if ( sb.length() > 0 )
{
sb.append( "," );
}
else
{
sb.append( "(" );
}
sb.append( val );
}
if ( sb.length() > 0 )
{
sb.append( ")" );
}
}
return sb.toString();
}
public Envelope intersection( Envelope other )
{
if ( getDimension() == other.getDimension() )
{
double[] iMin = new double[this.min.length];
double[] iMax = new double[this.min.length];
Arrays.fill(iMin, Double.NaN);
Arrays.fill(iMax, Double.NaN);
boolean result = true;
for ( int i = 0; i < min.length; i++ )
{
if ( other.min[i] <= this.max[i] && other.max[i] >= this.min[i] )
{
iMin[i] = Math.max(this.min[i], other.min[i]);
iMax[i] = Math.min(this.max[i], other.max[i]);
}
else
{
result = false;
}
}
return result ? new Envelope( iMin, iMax ) : null;
}
else
{
throw new IllegalArgumentException(
"Cannot calculate intersection of Envelopes with different dimensions: " + this.getDimension() + " != " + other.getDimension() );
}
}
}