~ubuntu-branches/ubuntu/precise/insighttoolkit/precise

« back to all changes in this revision

Viewing changes to Code/Numerics/Statistics/itkKdTree.h

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2008-12-19 20:16:49 UTC
  • mfrom: (1.2.1 upstream) (4.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20081219201649-drt97guwl2ryt0cn

* New upstream version.
  - patches/nifti-versioning.patch: Remove.  Applied upstream.
  - control:
  - rules: Update version numbers, package names.

* control: Build-depend on uuid-dev (gdcm uses it).

* copyright: Update download URL.

* rules: Adhere to parallel=N in DEB_BUILD_OPTIONS by setting MAKEFLAGS.

* compat: Set to 7.
* control: Update build-dep on debhelper to version >= 7.

* CMakeCache.txt.debian: Set CMAKE_BUILD_TYPE to "RELEASE" so that we
  build with -O3 (not -O2), necessary to optimize the templated code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
  Program:   Insight Segmentation & Registration Toolkit
4
4
  Module:    $RCSfile: itkKdTree.h,v $
5
5
  Language:  C++
6
 
  Date:      $Date: 2005-07-26 15:54:57 $
7
 
  Version:   $Revision: 1.24 $
 
6
  Date:      $Date: 2008-04-26 00:08:19 $
 
7
  Version:   $Revision: 1.27 $
8
8
 
9
9
  Copyright (c) Insight Software Consortium. All rights reserved.
10
10
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
11
 
12
 
     This software is distributed WITHOUT ANY WARRANTY; without even 
13
 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 
12
     This software is distributed WITHOUT ANY WARRANTY; without even
 
13
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14
14
     PURPOSE.  See the above copyright notices for more information.
15
15
 
16
16
=========================================================================*/
32
32
 
33
33
#include "itkEuclideanDistance.h"
34
34
 
35
 
namespace itk{ 
 
35
namespace itk{
36
36
namespace Statistics{
37
37
 
38
 
/** \class KdTreeNode 
 
38
/** \class KdTreeNode
39
39
 *  \brief This class defines the interface of its derived classes.
40
 
 * 
 
40
 *
41
41
 * The methods defined in this class are a superset of the methods
42
42
 * defined in its subclases. Therefore, the subclasses implements only
43
43
 * part of the methods. The template argument, TSample, can be any
57
57
 * been changed from Array to FixedArray.
58
58
 *
59
59
 * \sa KdTreeNonterminalNode, KdTreeWeightedCentroidNonterminalNode,
60
 
 * KdTreeTerminalNode 
 
60
 * KdTreeTerminalNode
61
61
 */
62
62
template< class TSample >
63
63
struct KdTreeNode
64
64
{
65
65
  /** type alias for itself */
66
 
  typedef KdTreeNode< TSample> Self ;
67
 
  
 
66
  typedef KdTreeNode< TSample> Self;
 
67
 
68
68
  /** Measurement type, not the measurement vector type */
69
 
  typedef typename TSample::MeasurementType MeasurementType ;
70
 
  
 
69
  typedef typename TSample::MeasurementType MeasurementType;
 
70
 
71
71
  /** Centroid type */
72
72
  typedef Array< double > CentroidType;
73
 
  
 
73
 
74
74
  /** Instance identifier type (index value type for the measurement
75
75
   * vector in a sample */
76
 
  typedef typename TSample::InstanceIdentifier InstanceIdentifier ;
 
76
  typedef typename TSample::InstanceIdentifier InstanceIdentifier;
77
77
 
78
78
  /** Returns true if the node is a terminal node, that is a node that
79
79
   * doesn't have any child. */
80
 
  virtual bool IsTerminal() const = 0 ;
 
80
  virtual bool IsTerminal() const = 0;
81
81
 
82
82
  /** Fills the partitionDimension (the dimension that was chosen to
83
83
   * split the measurement vectors belong to this node to the left and the
84
84
   * right child among k dimensions) and the partitionValue (the
85
85
   * measurement value on the partitionDimension divides the left and the
86
86
   * right child */
87
 
  virtual void GetParameters(unsigned int &partitionDimension, 
88
 
                             MeasurementType &partitionValue) const = 0 ;  
 
87
  virtual void GetParameters(unsigned int &partitionDimension,
 
88
                             MeasurementType &partitionValue) const = 0;
89
89
 
90
90
  /** Returns the pointer to the left child of this node */
91
 
  virtual       Self* Left()       = 0 ;
92
 
  virtual const Self* Left() const = 0 ;
 
91
  virtual       Self* Left()       = 0;
 
92
  virtual const Self* Left() const = 0;
93
93
 
94
94
  /** Returns the pointer to the right child of this node */
95
 
  virtual       Self* Right()       = 0 ;
96
 
  virtual const Self* Right() const = 0 ;
 
95
  virtual       Self* Right()       = 0;
 
96
  virtual const Self* Right() const = 0;
97
97
 
98
98
  /** Returs the number of measurement vectors under this node including
99
99
   * its children */
100
 
  virtual unsigned int Size() const = 0 ;
 
100
  virtual unsigned int Size() const = 0;
101
101
 
102
102
  /** Returns the vector sum of the all measurement vectors under this node */
103
 
  virtual void GetWeightedCentroid(CentroidType &centroid) = 0 ;
 
103
  virtual void GetWeightedCentroid(CentroidType &centroid) = 0;
104
104
 
105
105
  /** Returns the centroid. weighted centroid divided by the size */
106
 
  virtual void GetCentroid(CentroidType &centroid) = 0 ;
 
106
  virtual void GetCentroid(CentroidType &centroid) = 0;
107
107
 
108
108
  /** Retuns the instance identifier of the index-th measurement vector */
109
 
  virtual InstanceIdentifier GetInstanceIdentifier(size_t index) const = 0 ;
 
109
  virtual InstanceIdentifier GetInstanceIdentifier(size_t index) const = 0;
110
110
 
111
111
  /** Add an instance to this node */
112
 
  virtual void AddInstanceIdentifier(InstanceIdentifier id) = 0 ;
 
112
  virtual void AddInstanceIdentifier(InstanceIdentifier id) = 0;
113
113
 
114
114
  /** Destructor */
115
115
  virtual ~KdTreeNode() {}; // needed to subclasses will actually be deleted
116
 
} ; // end of class
 
116
}; // end of class
117
117
 
118
118
/** \class KdTreeNonterminalNode
119
 
 *  \brief This is a subclass of the KdTreeNode. 
 
119
 *  \brief This is a subclass of the KdTreeNode.
120
120
 *
121
121
 * KdTreeNonterminalNode doesn't store the information related with the
122
122
 * centroids. Therefore, the GetWeightedCentroid and the GetCentroid
123
123
 * methods are void. This class should have the left and the right
124
124
 * children. If we have a sample and want to generate a KdTree without
125
125
 * the centroid related information, we can use the KdTreeGenerator.
126
 
 * 
 
126
 *
127
127
 * \sa KdTreeNode, KdTreeWeightedCentroidNonterminalNode, KdTreeGenerator
128
128
 */
129
129
template< class TSample >
130
130
struct KdTreeNonterminalNode: public KdTreeNode< TSample >
131
131
{
132
 
  typedef KdTreeNode< TSample > Superclass ;
133
 
  typedef typename Superclass::MeasurementType MeasurementType ;
134
 
  typedef typename Superclass::CentroidType CentroidType ;
135
 
  typedef typename Superclass::InstanceIdentifier InstanceIdentifier ;
 
132
  typedef KdTreeNode< TSample > Superclass;
 
133
  typedef typename Superclass::MeasurementType MeasurementType;
 
134
  typedef typename Superclass::CentroidType CentroidType;
 
135
  typedef typename Superclass::InstanceIdentifier InstanceIdentifier;
136
136
 
137
137
  KdTreeNonterminalNode(unsigned int partitionDimension,
138
138
                        MeasurementType partitionValue,
139
139
                        Superclass* left,
140
 
                        Superclass* right) ;
 
140
                        Superclass* right);
141
141
 
142
142
  virtual ~KdTreeNonterminalNode() {}
143
 
  
 
143
 
144
144
  virtual bool IsTerminal() const
145
 
  { return false ; }
 
145
  { return false; }
146
146
 
147
 
  void GetParameters(unsigned int &partitionDimension, 
 
147
  void GetParameters(unsigned int &partitionDimension,
148
148
                     MeasurementType &partitionValue) const;
149
149
 
150
 
  Superclass* Left() 
151
 
  { return m_Left ; }
 
150
  Superclass* Left()
 
151
  { return m_Left; }
152
152
 
153
 
  Superclass* Right() 
154
 
  { return m_Right ; }
 
153
  Superclass* Right()
 
154
  { return m_Right; }
155
155
 
156
156
  const Superclass* Left() const
157
 
  { return m_Left ; }
 
157
  { return m_Left; }
158
158
 
159
159
  const Superclass* Right() const
160
 
  { return m_Right ; }
 
160
  { return m_Right; }
161
161
 
162
162
  unsigned int Size() const
163
 
  { return 0 ; }
164
 
 
165
 
  void GetWeightedCentroid(CentroidType &)
166
 
  { /* do nothing */ } 
167
 
 
168
 
  void GetCentroid(CentroidType &)
169
 
  { /* do nothing */ }
170
 
 
 
163
  { return 0; }
 
164
 
 
165
  void GetWeightedCentroid( CentroidType & )
 
166
  { /* do nothing */ }
 
167
 
 
168
  void GetCentroid( CentroidType & )
 
169
  { /* do nothing */ }
 
170
 
 
171
  // Returns the identifier of the only MeasurementVector associated with 
 
172
  // this node in the tree. This MeasurementVector will be used later during
 
173
  // the distance computation when querying the tree.
171
174
  InstanceIdentifier GetInstanceIdentifier(size_t) const
172
 
  { return 0 ; }
 
175
  { return this->m_InstanceIdentifier; }
173
176
 
174
 
  void AddInstanceIdentifier(InstanceIdentifier) {}
 
177
  void AddInstanceIdentifier(InstanceIdentifier valueId) 
 
178
  { this->m_InstanceIdentifier = valueId; }
175
179
 
176
180
private:
177
 
  unsigned int m_PartitionDimension ;
178
 
  MeasurementType m_PartitionValue ;
179
 
  Superclass* m_Left ;
180
 
  Superclass* m_Right ;
181
 
} ; // end of class
 
181
  unsigned int          m_PartitionDimension;
 
182
  MeasurementType       m_PartitionValue;
 
183
  InstanceIdentifier    m_InstanceIdentifier;
 
184
  Superclass* m_Left;
 
185
  Superclass* m_Right;
 
186
}; // end of class
182
187
 
183
188
/** \class KdTreeWeightedCentroidNonterminalNode
184
 
 *  \brief This is a subclass of the KdTreeNode. 
185
 
 * 
 
189
 *  \brief This is a subclass of the KdTreeNode.
 
190
 *
186
191
 * KdTreeNonterminalNode does have the information related with the
187
192
 * centroids. Therefore, the GetWeightedCentroid and the GetCentroid
188
193
 * methods returns meaningful values. This class should have the left
191
196
 * WeightedCentroidKdTreeGenerator. The centroid, the weighted
192
197
 * centroid, and the size (the number of measurement vectors) can be
193
198
 * used to accelate the k-means estimation.
194
 
 * 
 
199
 *
195
200
 * \sa KdTreeNode, KdTreeNonterminalNode, WeightedCentroidKdTreeGenerator
196
201
 */
197
202
template< class TSample >
198
203
struct KdTreeWeightedCentroidNonterminalNode: public KdTreeNode< TSample >
199
204
{
200
 
  typedef KdTreeNode< TSample > Superclass ;
201
 
  typedef typename Superclass::MeasurementType MeasurementType ;
202
 
  typedef typename Superclass::CentroidType CentroidType ;
203
 
  typedef typename Superclass::InstanceIdentifier InstanceIdentifier ;
 
205
  typedef KdTreeNode< TSample > Superclass;
 
206
  typedef typename Superclass::MeasurementType MeasurementType;
 
207
  typedef typename Superclass::CentroidType CentroidType;
 
208
  typedef typename Superclass::InstanceIdentifier InstanceIdentifier;
204
209
  typedef typename TSample::MeasurementVectorSizeType MeasurementVectorSizeType;
205
210
 
206
211
  KdTreeWeightedCentroidNonterminalNode(unsigned int partitionDimension,
208
213
                                         Superclass* left,
209
214
                                         Superclass* right,
210
215
                                         CentroidType &centroid,
211
 
                                         unsigned int size) ;
 
216
                                         unsigned int size);
212
217
  virtual ~KdTreeWeightedCentroidNonterminalNode() {}
213
218
 
214
219
  virtual bool IsTerminal() const
215
 
  { return false ; }
 
220
  { return false; }
216
221
 
217
 
  void GetParameters(unsigned int &partitionDimension, 
218
 
                     MeasurementType &partitionValue) const ;
 
222
  void GetParameters(unsigned int &partitionDimension,
 
223
                     MeasurementType &partitionValue) const;
219
224
 
220
225
  /** Return the length of a measurement vector */
221
226
  MeasurementVectorSizeType GetMeasurementVectorSize() const
223
228
    return m_MeasurementVectorSize;
224
229
    }
225
230
 
226
 
  Superclass* Left() 
227
 
  { return m_Left ; }
 
231
  Superclass* Left()
 
232
  { return m_Left; }
228
233
 
229
 
  Superclass* Right() 
230
 
  { return m_Right ; }
 
234
  Superclass* Right()
 
235
  { return m_Right; }
231
236
 
232
237
 
233
238
  const Superclass* Left() const
234
 
  { return m_Left ; }
 
239
  { return m_Left; }
235
240
 
236
241
  const Superclass* Right() const
237
 
  { return m_Right ; }
 
242
  { return m_Right; }
238
243
 
239
244
  unsigned int Size() const
240
 
  { return m_Size ; }
 
245
  { return m_Size; }
241
246
 
242
247
  void GetWeightedCentroid(CentroidType &centroid)
243
 
  { centroid = m_WeightedCentroid ; }
 
248
  { centroid = m_WeightedCentroid; }
244
249
 
245
250
  void GetCentroid(CentroidType &centroid)
246
 
  { centroid = m_Centroid ; }
 
251
  { centroid = m_Centroid; }
247
252
 
248
253
  InstanceIdentifier GetInstanceIdentifier(size_t) const
249
 
  { return 0 ; }
 
254
  { return this->m_InstanceIdentifier; }
250
255
 
251
 
  void AddInstanceIdentifier(InstanceIdentifier) {}
 
256
  void AddInstanceIdentifier(InstanceIdentifier valueId) 
 
257
  { this->m_InstanceIdentifier = valueId; }
252
258
 
253
259
private:
254
 
  MeasurementVectorSizeType m_MeasurementVectorSize;
255
 
  unsigned int m_PartitionDimension ;
256
 
  MeasurementType m_PartitionValue ;
257
 
  CentroidType m_WeightedCentroid ;
258
 
  CentroidType m_Centroid ;
259
 
  unsigned int m_Size ;
260
 
  Superclass* m_Left ;
261
 
  Superclass* m_Right ;
262
 
} ; // end of class
263
 
 
264
 
 
265
 
/** \class KdTreeTerminalNode 
 
260
  MeasurementVectorSizeType   m_MeasurementVectorSize;
 
261
  unsigned int                m_PartitionDimension;
 
262
  MeasurementType             m_PartitionValue;
 
263
  CentroidType                m_WeightedCentroid;
 
264
  CentroidType                m_Centroid;
 
265
  InstanceIdentifier          m_InstanceIdentifier;
 
266
  unsigned int                m_Size;
 
267
  Superclass*                 m_Left;
 
268
  Superclass*                 m_Right;
 
269
}; // end of class
 
270
 
 
271
 
 
272
/** \class KdTreeTerminalNode
266
273
 *  \brief This class is the node that doesn't have any child node. The
267
274
 *  IsTerminal method returns true for this class. This class stores the
268
275
 *  instance identifiers belonging to this node, while the nonterminal
269
276
 *  nodes do not store them. The AddInstanceIdentifier and
270
277
 *  GetInstanceIdentifier are storing and retrieving the instance
271
278
 *  identifiers belonging to this node.
272
 
 * 
 
279
 *
273
280
 * \sa KdTreeNode, KdTreeNonterminalNode,
274
281
 * KdTreeWeightedCentroidNonterminalNode
275
282
 */
276
283
template< class TSample >
277
284
struct KdTreeTerminalNode: public KdTreeNode< TSample >
278
285
{
279
 
  typedef KdTreeNode< TSample > Superclass ;
280
 
  typedef typename Superclass::MeasurementType MeasurementType ;
281
 
  typedef typename Superclass::CentroidType CentroidType ;
282
 
  typedef typename Superclass::InstanceIdentifier InstanceIdentifier ;
 
286
  typedef KdTreeNode< TSample > Superclass;
 
287
  typedef typename Superclass::MeasurementType MeasurementType;
 
288
  typedef typename Superclass::CentroidType CentroidType;
 
289
  typedef typename Superclass::InstanceIdentifier InstanceIdentifier;
283
290
 
284
291
  KdTreeTerminalNode() {}
285
292
 
286
293
  virtual ~KdTreeTerminalNode() {}
287
294
 
288
295
  bool IsTerminal() const
289
 
  { return true ; }
 
296
  { return true; }
290
297
 
291
 
  void GetParameters(unsigned int &, 
 
298
  void GetParameters(unsigned int &,
292
299
                     MeasurementType &) const {}
293
300
 
294
 
  Superclass* Left() 
295
 
  { return 0 ; }
 
301
  Superclass* Left()
 
302
  { return 0; }
296
303
 
297
 
  Superclass* Right() 
298
 
  { return 0 ; }
 
304
  Superclass* Right()
 
305
  { return 0; }
299
306
 
300
307
 
301
308
  const Superclass* Left() const
302
 
  { return 0 ; }
 
309
  { return 0; }
303
310
 
304
311
  const Superclass* Right() const
305
 
  { return 0 ; }
 
312
  { return 0; }
306
313
 
307
314
  unsigned int Size() const
308
315
  { return static_cast<unsigned int>( m_InstanceIdentifiers.size() ); }
309
316
 
310
317
  void GetWeightedCentroid(CentroidType &)
311
 
  { /* do nothing */ } 
 
318
  { /* do nothing */ }
312
319
 
313
320
  void GetCentroid(CentroidType &)
314
 
  { /* do nothing */ } 
 
321
  { /* do nothing */ }
315
322
 
316
323
  InstanceIdentifier GetInstanceIdentifier(size_t index) const
317
 
  { return m_InstanceIdentifiers[index] ; }
 
324
  { return m_InstanceIdentifiers[index]; }
318
325
 
319
 
  void AddInstanceIdentifier(InstanceIdentifier id) 
320
 
  { m_InstanceIdentifiers.push_back(id) ;}
 
326
  void AddInstanceIdentifier(InstanceIdentifier id)
 
327
  { m_InstanceIdentifiers.push_back(id);}
321
328
 
322
329
private:
323
 
  std::vector< InstanceIdentifier > m_InstanceIdentifiers ;
324
 
} ; // end of class
 
330
  std::vector< InstanceIdentifier > m_InstanceIdentifiers;
 
331
}; // end of class
325
332
 
326
 
/** \class KdTree 
 
333
/** \class KdTree
327
334
 *  \brief This class provides methods for k-nearest neighbor search and
328
 
 *  related data structures for a k-d tree. 
 
335
 *  related data structures for a k-d tree.
329
336
 *
330
337
 * An object of this class stores instance identifiers in a k-d tree
331
338
 * that is a binary tree with childrens split along a dimension among
337
344
 * nature and in implementation. This implementation doesn't support
338
345
 * dynamic insert and delete operations for the tree. Instead, we can
339
346
 * use the KdTreeGenerator or WeightedCentroidKdTreeGenerator to
340
 
 * generate a static KdTree object. 
 
347
 * generate a static KdTree object.
341
348
 *
342
349
 * To search k-nearest neighbor, call the Search method with the query
343
350
 * point in a k-d space and the number of nearest neighbors. The
344
351
 * GetSearchResult method returns a pointer to a NearestNeighbors object
345
352
 * with k-nearest neighbors.
346
 
 * 
 
353
 *
347
354
 * <b>Recent API changes:</b>
348
355
 * The static const macro to get the length of a measurement vector,
349
356
 * 'MeasurementVectorSize'  has been removed to allow the length of a measurement
350
 
 * vector to be specified at run time. Please use the function 
 
357
 * vector to be specified at run time. Please use the function
351
358
 * GetMeasurementVectorSize() instead.
352
359
 
353
360
 * \sa KdTreeNode, KdTreeNonterminalNode,
360
367
{
361
368
public:
362
369
  /** Standard class typedefs */
363
 
  typedef KdTree Self ;
364
 
  typedef Object Superclass ;
 
370
  typedef KdTree Self;
 
371
  typedef Object Superclass;
365
372
  typedef SmartPointer<Self> Pointer;
366
373
  typedef SmartPointer<const Self> ConstPointer;
367
374
 
369
376
  itkTypeMacro(KdTree, Object);
370
377
 
371
378
  /** Method for creation through the object factory. */
372
 
  itkNewMacro(Self) ;
 
379
  itkNewMacro(Self);
373
380
 
374
 
  /** typedef alias for the source data container */ 
375
 
  typedef TSample SampleType ;
376
 
  typedef typename TSample::MeasurementVectorType MeasurementVectorType ;
377
 
  typedef typename TSample::MeasurementType MeasurementType ;
378
 
  typedef typename TSample::InstanceIdentifier InstanceIdentifier ;
379
 
  typedef typename TSample::FrequencyType FrequencyType ;
 
381
  /** typedef alias for the source data container */
 
382
  typedef TSample SampleType;
 
383
  typedef typename TSample::MeasurementVectorType MeasurementVectorType;
 
384
  typedef typename TSample::MeasurementType MeasurementType;
 
385
  typedef typename TSample::InstanceIdentifier InstanceIdentifier;
 
386
  typedef typename TSample::FrequencyType FrequencyType;
380
387
 
381
388
  typedef unsigned int                    MeasurementVectorSizeType;
382
389
 
385
392
  itkGetConstMacro( MeasurementVectorSize, MeasurementVectorSizeType );
386
393
 
387
394
  /** DistanceMetric type for the distance calculation and comparison */
388
 
  typedef EuclideanDistance< MeasurementVectorType > DistanceMetricType ;
 
395
  typedef EuclideanDistance< MeasurementVectorType > DistanceMetricType;
389
396
 
390
397
  /** Node type of the KdTree */
391
 
  typedef KdTreeNode< TSample > KdTreeNodeType ;
 
398
  typedef KdTreeNode< TSample > KdTreeNodeType;
392
399
 
393
400
  /** Neighbor type. The first element of the std::pair is the instance
394
401
   * identifier and the second one is the distance between the measurement
395
402
   * vector identified by the first element and the query point. */
396
 
  typedef std::pair< InstanceIdentifier, double > NeighborType ;
 
403
  typedef std::pair< InstanceIdentifier, double > NeighborType;
397
404
 
398
 
  typedef std::vector< InstanceIdentifier > InstanceIdentifierVectorType ;
 
405
  typedef std::vector< InstanceIdentifier > InstanceIdentifierVectorType;
399
406
 
400
407
  /** \class NearestNeighbors
401
408
   * \brief data structure for storing k-nearest neighbor search result
402
409
   * (k number of Neighbors)
403
 
   * 
 
410
   *
404
411
   * This class stores the instance identifiers and the distance values
405
412
   * of k-nearest neighbors. We can also query the farthest neighbor's
406
413
   * distance from the query point using the GetLargestDistance
407
 
   * method. 
 
414
   * method.
408
415
   */
409
416
  class NearestNeighbors
410
417
  {
411
418
  public:
412
419
    /** Constructor */
413
420
    NearestNeighbors() {}
414
 
    
 
421
 
415
422
    /** Destructor */
416
 
    ~NearestNeighbors() {} 
417
 
    
 
423
    ~NearestNeighbors() {}
 
424
 
418
425
    /** Initialize the internal instance identifier and distance holders
419
426
     * with the size, k */
420
427
    void resize(unsigned int k)
421
428
    {
422
 
      m_Identifiers.clear() ;
423
 
      m_Identifiers.resize(k, NumericTraits< unsigned long >::max()) ;
424
 
      m_Distances.clear() ;
425
 
      m_Distances.resize(k, NumericTraits< double >::max()) ;
426
 
      m_FarthestNeighborIndex = 0 ;
 
429
      m_Identifiers.clear();
 
430
      m_Identifiers.resize(k, NumericTraits< unsigned long >::max());
 
431
      m_Distances.clear();
 
432
      m_Distances.resize(k, NumericTraits< double >::max());
 
433
      m_FarthestNeighborIndex = 0;
427
434
    }
428
435
 
429
436
    /** Returns the distance of the farthest neighbor from the query point */
430
 
    double GetLargestDistance() 
431
 
    { return m_Distances[m_FarthestNeighborIndex] ; }
 
437
    double GetLargestDistance()
 
438
    { return m_Distances[m_FarthestNeighborIndex]; }
432
439
 
433
440
    /** Replaces the farthest neighbor's instance identifier and
434
441
     * distance value with the id and the distance */
435
 
    void ReplaceFarthestNeighbor(InstanceIdentifier id, double distance) 
 
442
    void ReplaceFarthestNeighbor(InstanceIdentifier id, double distance)
436
443
    {
437
 
      m_Identifiers[m_FarthestNeighborIndex] = id ;
438
 
      m_Distances[m_FarthestNeighborIndex] = distance ;
439
 
      double farthestDistance = NumericTraits< double >::min() ;
 
444
      m_Identifiers[m_FarthestNeighborIndex] = id;
 
445
      m_Distances[m_FarthestNeighborIndex] = distance;
 
446
      double farthestDistance = NumericTraits< double >::min();
440
447
      const unsigned int size = static_cast<unsigned int>( m_Distances.size() );
441
 
      for ( unsigned int i = 0 ; i < size; i++ )
 
448
      for ( unsigned int i = 0; i < size; i++ )
442
449
        {
443
450
        if ( m_Distances[i] > farthestDistance )
444
451
          {
445
 
          farthestDistance = m_Distances[i] ;
446
 
          m_FarthestNeighborIndex = i ;
 
452
          farthestDistance = m_Distances[i];
 
453
          m_FarthestNeighborIndex = i;
447
454
          }
448
455
        }
449
456
    }
450
457
 
451
458
    /** Returns the vector of k-neighbors' instance identifiers */
452
 
    InstanceIdentifierVectorType GetNeighbors()
453
 
    { return m_Identifiers ; }
 
459
    const InstanceIdentifierVectorType & GetNeighbors() const
 
460
    { return m_Identifiers; }
454
461
 
455
462
    /** Returns the instance identifier of the index-th neighbor among
456
463
     * k-neighbors */
457
 
    InstanceIdentifier GetNeighbor(unsigned int index)
458
 
    { return m_Identifiers[index] ; }
 
464
    InstanceIdentifier GetNeighbor(unsigned int index) const
 
465
    { return m_Identifiers[index]; }
459
466
 
460
467
    /** Returns the vector of k-neighbors' instance identifiers */
461
 
    std::vector< double >& GetDistances()
462
 
    { return m_Distances ; }
 
468
    const std::vector< double >& GetDistances() const
 
469
    { return m_Distances; }
463
470
 
464
471
  private:
465
472
    /** The index of the farthest neighbor among k-neighbors */
466
 
    unsigned int m_FarthestNeighborIndex ;
467
 
    
 
473
    unsigned int m_FarthestNeighborIndex;
 
474
 
468
475
    /** Storage for the instance identifiers of k-neighbors */
469
 
    InstanceIdentifierVectorType m_Identifiers ;
 
476
    InstanceIdentifierVectorType m_Identifiers;
470
477
 
471
478
    /** Storage for the distance values of k-neighbors from the query
472
479
     * point */
473
 
    std::vector< double > m_Distances ;
474
 
  } ;
 
480
    std::vector< double > m_Distances;
 
481
  };
475
482
 
476
483
  /** Sets the number of measurement vectors that can be stored in a
477
484
   * terminal node */
478
 
  void SetBucketSize(unsigned int size) ;
 
485
  void SetBucketSize(unsigned int size);
479
486
 
480
487
  /** Sets the input sample that provides the measurement vectors to the k-d
481
488
   * tree */
482
 
  void SetSample(const TSample* sample) ;
 
489
  void SetSample(const TSample* sample);
483
490
 
484
491
  /** Returns the pointer to the input sample */
485
492
  const TSample* GetSample() const
486
 
  { return m_Sample ; }
 
493
  { return m_Sample; }
487
494
 
488
495
  unsigned long Size() const
489
 
  { return m_Sample->Size() ; }
 
496
  { return m_Sample->Size(); }
490
497
 
491
498
  /** Returns the pointer to the empty terminal node. A KdTree object
492
499
   * has a single empty terminal node in memory. when the split process
493
500
   * has to create an empty terminal node, the single instance is reused
494
501
   * for this case */
495
502
  KdTreeNodeType* GetEmptyTerminalNode()
496
 
  { return m_EmptyTerminalNode ; } 
 
503
  { return m_EmptyTerminalNode; }
497
504
 
498
505
  /** Sets the root node of the KdTree that is a result of
499
506
   * KdTreeGenerator or WeightedCentroidKdTreeGenerator. */
500
 
  void SetRoot(KdTreeNodeType* root) 
501
 
  { m_Root = root ; } 
 
507
  void SetRoot(KdTreeNodeType* root)
 
508
  { m_Root = root; }
502
509
 
503
510
  /** Returns the pointer to the root node. */
504
 
  KdTreeNodeType* GetRoot() 
505
 
  { return m_Root ; } 
 
511
  KdTreeNodeType* GetRoot()
 
512
  { return m_Root; }
506
513
 
507
514
  /** Returns the measurement vector identified by the instance
508
515
   * identifier that is an identifier defiend for the input sample */
509
516
  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const
510
 
  { return m_Sample->GetMeasurementVector(id) ; }
 
517
  { return m_Sample->GetMeasurementVector(id); }
511
518
 
512
 
  /** Returns the frequency of the measurement vector identified by 
 
519
  /** Returns the frequency of the measurement vector identified by
513
520
   * the instance identifier */
514
521
  FrequencyType GetFrequency(InstanceIdentifier id) const
515
 
  { return m_Sample->GetFrequency( id ) ; }
 
522
  { return m_Sample->GetFrequency( id ); }
516
523
 
517
524
  /** Get the pointer to the distance metric. */
518
525
  DistanceMetricType* GetDistanceMetric()
519
 
  { return m_DistanceMetric.GetPointer() ; }
 
526
  { return m_DistanceMetric.GetPointer(); }
520
527
 
521
528
  /** Searches the k-nearest neighbors */
522
 
  void Search(MeasurementVectorType &query, 
523
 
              unsigned int k,
 
529
  void Search(const MeasurementVectorType &query,
 
530
              unsigned int numberOfNeighborsRequested,
524
531
              InstanceIdentifierVectorType& result) const;
525
532
 
526
533
  /** Searches the neighbors fallen into a hypersphere */
527
 
  void Search(MeasurementVectorType &query,
528
 
              double radius, 
 
534
  void Search(const MeasurementVectorType &query,
 
535
              double radius,
529
536
              InstanceIdentifierVectorType& result) const;
530
537
 
531
538
  /** Returns the number of measurement vectors that have been visited
532
539
   * to find the k-nearest neighbors. */
533
540
  int GetNumberOfVisits() const
534
 
  { return m_NumberOfVisits ; }
 
541
  { return m_NumberOfVisits; }
535
542
 
536
543
  /** Returns true if the intermediate k-nearest neighbors exist within
537
544
   * the the bounding box defined by the lowerBound and the
538
545
   * upperBound. Otherwise returns false. Returns false if the ball
539
546
   * defined by the distance between the query point and the farthest
540
547
   * neighbor touch the surface of the bounding box.*/
541
 
  bool BallWithinBounds(MeasurementVectorType &query, 
 
548
  bool BallWithinBounds(const MeasurementVectorType &query,
542
549
                        MeasurementVectorType &lowerBound,
543
550
                        MeasurementVectorType &upperBound,
544
 
                        double radius) const ;
 
551
                        double radius) const;
545
552
 
546
553
  /** Returns true if the ball defined by the distance between the query
547
554
   * point and the farthest neighbor overlaps with the bounding box
548
555
   * defined by the lower and the upper bounds.*/
549
 
  bool BoundsOverlapBall(MeasurementVectorType &query, 
 
556
  bool BoundsOverlapBall(const MeasurementVectorType &query,
550
557
                         MeasurementVectorType &lowerBound,
551
558
                         MeasurementVectorType &upperBound,
552
 
                         double radius) const ;
 
559
                         double radius) const;
553
560
 
554
561
  /** Deletes the node recursively */
555
 
  void DeleteNode(KdTreeNodeType *node) ;
556
 
 
557
 
  /** Prints out the tree information */
558
 
  void PrintTree(KdTreeNodeType *node, int level, 
559
 
                 unsigned int activeDimension) ;
560
 
 
561
 
  typedef typename TSample::Iterator Iterator ;
562
 
  typedef typename TSample::ConstIterator ConstIterator ;
 
562
  void DeleteNode(KdTreeNodeType *node);
 
563
 
 
564
  /** Prints out the tree information */
 
565
  void PrintTree( std::ostream & os ) const;
 
566
 
 
567
  /** Prints out the tree information */
 
568
  void PrintTree(KdTreeNodeType *node, unsigned int level,
 
569
                 unsigned int activeDimension,
 
570
                 std::ostream & os = std::cout ) const;
 
571
 
 
572
  /** Draw out the tree information to a ostream using 
 
573
   * the format of the Graphviz dot tool. */
 
574
  void PlotTree( std::ostream & os ) const;
 
575
 
 
576
  /** Prints out the tree information */
 
577
  void PlotTree(KdTreeNodeType *node, std::ostream & os = std::cout ) const;
 
578
 
 
579
 
 
580
  typedef typename TSample::Iterator Iterator;
 
581
  typedef typename TSample::ConstIterator ConstIterator;
563
582
 
564
583
  Iterator Begin()
565
584
  {
566
 
    typename TSample::ConstIterator iter = m_Sample->Begin() ;
567
 
    return iter; 
 
585
    typename TSample::ConstIterator iter = m_Sample->Begin();
 
586
    return iter;
568
587
  }
569
588
 
570
 
  Iterator End() 
 
589
  Iterator End()
571
590
  {
572
 
    Iterator iter = m_Sample->End() ;
573
 
    return iter; 
 
591
    Iterator iter = m_Sample->End();
 
592
    return iter;
574
593
  }
575
594
 
576
595
  ConstIterator Begin() const
577
596
  {
578
 
    typename TSample::ConstIterator iter = m_Sample->Begin() ;
579
 
    return iter; 
 
597
    typename TSample::ConstIterator iter = m_Sample->Begin();
 
598
    return iter;
580
599
  }
581
600
 
582
601
  ConstIterator End() const
583
602
  {
584
 
    ConstIterator iter = m_Sample->End() ;
585
 
    return iter; 
 
603
    ConstIterator iter = m_Sample->End();
 
604
    return iter;
586
605
  }
587
606
 
588
 
 
589
607
protected:
590
608
  /** Constructor */
591
 
  KdTree() ;
592
 
  
 
609
  KdTree();
 
610
 
593
611
  /** Destructor: deletes the root node and the empty terminal node. */
594
 
  virtual ~KdTree() ;
595
 
 
596
 
  void PrintSelf(std::ostream& os, Indent indent) const ;
597
 
 
598
 
  /** search loop */ 
 
612
  virtual ~KdTree();
 
613
 
 
614
  void PrintSelf(std::ostream& os, Indent indent) const;
 
615
 
 
616
  /** search loop */
599
617
  int NearestNeighborSearchLoop(const KdTreeNodeType* node,
600
 
                                MeasurementVectorType &query,
 
618
                                const MeasurementVectorType &query,
601
619
                                MeasurementVectorType &lowerBound,
602
620
                                MeasurementVectorType &upperBound) const;
603
621
 
604
 
  /** search loop */ 
605
 
  int SearchLoop(const KdTreeNodeType* node, MeasurementVectorType &query,
 
622
  /** search loop */
 
623
  int SearchLoop(const KdTreeNodeType* node, const MeasurementVectorType &query,
606
624
                 MeasurementVectorType &lowerBound,
607
 
                 MeasurementVectorType &upperBound) const ;
 
625
                 MeasurementVectorType &upperBound) const;
608
626
private:
609
 
  KdTree(const Self&) ; //purposely not implemented
610
 
  void operator=(const Self&) ; //purposely not implemented
 
627
  KdTree(const Self&); //purposely not implemented
 
628
  void operator=(const Self&); //purposely not implemented
611
629
 
612
630
  /** Pointer to the input sample */
613
 
  const TSample* m_Sample ;
614
 
  
 
631
  const TSample* m_Sample;
 
632
 
615
633
  /** Number of measurement vectors can be stored in a terminal node. */
616
 
  int m_BucketSize ;
 
634
  int m_BucketSize;
617
635
 
618
636
  /** Pointer to the root node */
619
 
  KdTreeNodeType* m_Root ;
 
637
  KdTreeNodeType* m_Root;
620
638
 
621
639
  /** Pointer to the empty terminal node */
622
 
  KdTreeNodeType* m_EmptyTerminalNode ;
 
640
  KdTreeNodeType* m_EmptyTerminalNode;
623
641
 
624
642
  /** Distance metric smart pointer */
625
 
  typename DistanceMetricType::Pointer m_DistanceMetric ;
626
 
 
627
 
  mutable bool m_IsNearestNeighborSearch ;
628
 
 
629
 
  mutable double m_SearchRadius ;
630
 
 
631
 
  mutable InstanceIdentifierVectorType m_Neighbors ;
 
643
  typename DistanceMetricType::Pointer m_DistanceMetric;
 
644
 
 
645
  mutable bool m_IsNearestNeighborSearch;
 
646
 
 
647
  mutable double m_SearchRadius;
 
648
 
 
649
  mutable InstanceIdentifierVectorType m_Neighbors;
632
650
 
633
651
  /** k-nearest neighbors */
634
 
  mutable NearestNeighbors m_NearestNeighbors ;
 
652
  mutable NearestNeighbors m_NearestNeighbors;
635
653
 
636
654
  /** Temporary lower bound in the SearchLoop. */
637
 
  mutable MeasurementVectorType m_LowerBound ;
 
655
  mutable MeasurementVectorType m_LowerBound;
638
656
 
639
657
  /** Temporary upper bound in the SearchLoop. */
640
 
  mutable MeasurementVectorType m_UpperBound ;
 
658
  mutable MeasurementVectorType m_UpperBound;
641
659
 
642
 
  /** Number of measurment vectors to find k-nearest neighbors. */ 
643
 
  mutable int m_NumberOfVisits ;
 
660
  /** Number of measurment vectors to find k-nearest neighbors. */
 
661
  mutable int m_NumberOfVisits;
644
662
 
645
663
  /** Flag to stop the SearchLoop. */
646
 
  mutable bool m_StopSearch ;
 
664
  mutable bool m_StopSearch;
647
665
 
648
666
  /** Temporary neighbor */
649
 
  mutable NeighborType m_TempNeighbor ;
 
667
  mutable NeighborType m_TempNeighbor;
650
668
 
651
669
  /** Measurement vector size */
652
670
  MeasurementVectorSizeType m_MeasurementVectorSize;
653
 
} ; // end of class
 
671
}; // end of class
654
672
 
655
673
} // end of namespace Statistics
656
674
} // end of namespace itk
660
678
#endif
661
679
 
662
680
#endif
663
 
 
664
 
 
665