/
KDTree.java
322 lines (281 loc) · 8.33 KB
/
KDTree.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
/*-
* #%L
* ImgLib2: a general-purpose, multidimensional image processing library.
* %%
* Copyright (C) 2009 - 2024 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
* Jean-Yves Tinevez and Michael Zinsmaier.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package net.imglib2;
import java.util.List;
import net.imglib2.converter.AbstractConvertedIterableRealInterval;
import net.imglib2.converter.AbstractConvertedRealCursor;
import net.imglib2.kdtree.KDTreeData;
import net.imglib2.kdtree.KDTreeImpl;
public class KDTree< T > implements EuclideanSpace, IterableRealInterval< T >
{
private final KDTreeData< T > treeData;
final KDTreeImpl impl;
/**
* Access to underlying data for serialization.
*/
public KDTreeData< T > treeData()
{
return treeData;
}
/**
* Access to pure coordinate kD Tree implementation.
*/
public KDTreeImpl impl()
{
return impl;
}
/**
* Construct a KDTree from the elements in the given list.
*
* <p>
* Note that the constructor can be called with the same list for both
* {@code values == positions} if {@code T extends RealLocalizable}.
* </p>
*
* @param values
* a list of values
* @param positions
* a list of positions corresponding to the values
*/
public < L extends RealLocalizable > KDTree( final List< T > values, final List< L > positions )
{
this( verifySize( values, positions ), values, positions );
}
private static int verifySize( final List< ? > values, final List< ? > positions )
{
if ( values.size() != positions.size() )
throw new IllegalArgumentException( "The list of values and the list of positions provided to KDTree should have the same size." );
if ( positions.isEmpty() )
throw new IllegalArgumentException( "List of positions is empty. At least one point is requires to construct a KDTree." );
return values.size();
}
/**
* Construct a KDTree from the elements of the given
* {@link IterableRealInterval}.
*
* @param interval
* elements in the tree are obtained by iterating this
*/
public KDTree( final IterableRealInterval< T > interval )
{
this( verifySize( interval ), interval, positionsIterable( interval ) );
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private static int verifySize( final IterableRealInterval< ? > interval )
{
final long size = interval.size();
if ( size > MAX_ARRAY_SIZE )
throw new IllegalArgumentException( "Interval contains too many points to store in KDTree" );
else if ( size <= 0 )
throw new IllegalArgumentException( "Interval is empty. At least one point is requires to construct a KDTree." );
return ( int ) size;
}
private static < A > Iterable< RealLocalizable > positionsIterable( IterableRealInterval< A > sourceInterval )
{
return new AbstractConvertedIterableRealInterval< A, RealLocalizable >( sourceInterval )
{
class Cursor extends AbstractConvertedRealCursor< A, RealLocalizable >
{
Cursor( final RealCursor< A > source )
{
super( source );
}
@Override
public RealLocalizable get()
{
return source;
}
@Override
public Cursor copy()
{
return new Cursor( source.copy() );
}
@Override
public RealLocalizable getType()
{
return source;
}
}
@Override
public AbstractConvertedRealCursor< A, RealLocalizable > cursor()
{
return new Cursor( sourceInterval.cursor() );
}
@Override
public AbstractConvertedRealCursor< A, RealLocalizable > localizingCursor()
{
return new Cursor( sourceInterval.localizingCursor() );
}
};
}
public < L extends RealLocalizable > KDTree( final int numPoints, final Iterable< T > values, final Iterable< L > positions )
{
// TODO make storeValuesAsNativeImg a parameter
this( KDTreeData.create( numPoints, values, positions, true ) );
}
// construct with pre-built data, e.g., from deserialization
public KDTree( final KDTreeData< T > data )
{
treeData = data;
impl = new KDTreeImpl( treeData.positions() );
}
/**
* Get the root node.
*
* @return the root node.
*
* @deprecated {@link KDTreeNode} is now a re-usable proxy (like {@code NativeType}).
* To work with existing code, {@link KDTreeNode#left()}, {@link
* KDTreeNode#right()}, {@link KDTree#getRoot()} etc create new objects in each
* call, instead of re-using existing proxies.
* Code using that should be rewritten to reuse proxies, if possible.
*/
@Deprecated
public KDTreeNode< T > getRoot()
{
return new KDTreeNode<>( this ).setNodeIndex( impl.root() );
}
@Override
public T getType()
{
return treeData.getType();
}
@Override
public int numDimensions()
{
return impl.numDimensions();
}
@Override
public double realMin( final int d )
{
return treeData.boundingBox().realMin( d );
}
@Override
public double realMax( final int d )
{
return treeData.boundingBox().realMax( d );
}
@Override
public KDTreeCursor cursor()
{
return new KDTreeCursor();
}
public final class KDTreeCursor extends KDTreeNode< T > implements RealCursor< T >
{
KDTreeCursor()
{
super( KDTree.this );
reset();
}
@Override
public void fwd()
{
setNodeIndex( nodeIndex() + 1 );
}
@Override
public void reset()
{
setNodeIndex( -1 );
}
@Override
public boolean hasNext()
{
return nodeIndex() < impl.size() - 1;
}
@Override
public T getType()
{
return treeData.getType();
}
@Override
public KDTreeCursor copy()
{
final KDTreeCursor copy = new KDTreeCursor();
copy.setNodeIndex( nodeIndex() );
return copy;
}
}
@Override
public KDTreeCursor localizingCursor()
{
return cursor();
}
@Override
public KDTreeCursor iterator()
{
return cursor();
}
@Override
public long size()
{
return impl.size();
}
@Override
public Object iterationOrder()
{
return this; // iteration order is only compatible with ourselves
}
@Override
public String toString()
{
return toString( impl.root(), "", createNode() );
}
private String toString( final int node, final String indent, final KDTreeNode< T > ref )
{
if ( node < 0 )
return "";
return indent + "- " + ref.setNodeIndex( node ).toString() + "\n"
+ toString( impl.left( node ), indent + " ", ref )
+ toString( impl.right( node ), indent + " ", ref );
}
/**
* Create a re-usable {@link KDTreeNode} proxy linked to this tree.
* {@link KDTreeNode#setNodeIndex(int)} can be used to point the proxy to a
* particular node in the tree.
*/
public KDTreeNode< T > createNode()
{
return new KDTreeNode<>( this );
}
KDTreeNode< T > left( final KDTreeNode< T > parent )
{
final int c = impl.left( parent.nodeIndex() );
return c < 0 ? null : createNode().setNodeIndex( c );
}
KDTreeNode< T > right( final KDTreeNode< T > parent )
{
final int c = impl.right( parent.nodeIndex() );
return c < 0 ? null : createNode().setNodeIndex( c );
}
}