This file is indexed.

/usr/include/ITK-4.5/itkKdTree.h is in libinsighttoolkit4-dev 4.5.0-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef __itkKdTree_h
#define __itkKdTree_h

#include <queue>
#include <vector>

#include "itkPoint.h"
#include "itkSize.h"
#include "itkObject.h"
#include "itkArray.h"

#include "itkSubsample.h"

#include "itkEuclideanDistanceMetric.h"

namespace itk
{
namespace Statistics
{
/** \class KdTreeNode
 *  \brief This class defines the interface of its derived classes.
 *
 * The methods defined in this class are a superset of the methods
 * defined in its subclases. Therefore, the subclasses implements only
 * part of the methods. The template argument, TSample, can be any
 * subclass of the Sample class.
 *
 * There are two categories for the subclasses, terminal and nonterminal
 * nodes. The terminal nodes stores the instance identifiers beloging to
 * them, while the nonterminal nodes don't. Therefore, the
 * AddInstanceIdentifier and the GetInstanceIdentifier have meaning only
 * with the terminal ones. The terminal nodes don't have any child (left
 * or right). For terminal nodes, the GetParameters method is void.
 *
 * <b>Recent API changes:</b>
 * The static const macro to get the length of a measurement vector,
 * \c MeasurementVectorSize  has been removed to allow the length of a measurement
 * vector to be specified at run time. The \c typedef for \c CentroidType has
 * been changed from Array to FixedArray.
 *
 * \sa KdTreeNonterminalNode, KdTreeWeightedCentroidNonterminalNode,
 * KdTreeTerminalNode
 * \ingroup ITKStatistics
 */
template<typename TSample>

struct KdTreeNode
  {
  /** type alias for itself */
  typedef KdTreeNode<TSample> Self;

  /** Measurement type, not the measurement vector type */
  typedef typename TSample::MeasurementType MeasurementType;

  /** Centroid type */
  typedef Array<double> CentroidType;

  /** Instance identifier type (index value type for the measurement
   * vector in a sample */
  typedef typename TSample::InstanceIdentifier InstanceIdentifier;

  /** Returns true if the node is a terminal node, that is a node that
   * doesn't have any child. */
  virtual bool IsTerminal() const = 0;

  /** Fills the partitionDimension (the dimension that was chosen to
   * split the measurement vectors belong to this node to the left and the
   * right child among k dimensions) and the partitionValue (the
   * measurement value on the partitionDimension divides the left and the
   * right child */
  virtual void GetParameters( unsigned int &, MeasurementType & ) const = 0;

  /** Returns the pointer to the left child of this node */
  virtual Self * Left() = 0;

  /** Returns the const pointer to the left child of this node */
  virtual const Self * Left() const = 0;

  /** Returns the pointer to the right child of this node */
  virtual Self * Right() = 0;

  /** Returns the const pointer to the right child of this node */
  virtual const Self * Right() const = 0;

  /**
   * Returs the number of measurement vectors under this node including
   * its children
   */
  virtual unsigned int Size() const = 0;

  /** Returns the vector sum of the all measurement vectors under this node */
  virtual void GetWeightedCentroid( CentroidType & ) = 0;

  /** Returns the centroid. weighted centroid divided by the size */
  virtual void GetCentroid( CentroidType & ) = 0;

  /** Retuns the instance identifier of the index-th measurement vector */
  virtual InstanceIdentifier GetInstanceIdentifier( InstanceIdentifier ) const = 0;

  /** Add an instance to this node */
  virtual void AddInstanceIdentifier( InstanceIdentifier ) = 0;

  /** Destructor */
  virtual ~KdTreeNode() {}  // needed to subclasses will actually be deleted
}; // end of class

/** \class KdTreeNonterminalNode
 *  \brief This is a subclass of the KdTreeNode.
 *
 * KdTreeNonterminalNode doesn't store the information related with the
 * centroids. Therefore, the GetWeightedCentroid and the GetCentroid
 * methods are void. This class should have the left and the right
 * children. If we have a sample and want to generate a KdTree without
 * the centroid related information, we can use the KdTreeGenerator.
 *
 * \sa KdTreeNode, KdTreeWeightedCentroidNonterminalNode, KdTreeGenerator
 * \ingroup ITKStatistics
 */
template<typename TSample>

struct KdTreeNonterminalNode:public KdTreeNode<TSample>
  {
  typedef KdTreeNode<TSample>                     Superclass;
  typedef typename Superclass::MeasurementType    MeasurementType;
  typedef typename Superclass::CentroidType       CentroidType;
  typedef typename Superclass::InstanceIdentifier InstanceIdentifier;

  KdTreeNonterminalNode( unsigned int, MeasurementType, Superclass *,
    Superclass * );

  virtual ~KdTreeNonterminalNode() {}

  virtual bool IsTerminal() const
  {
    return false;
  }

  void GetParameters( unsigned int &, MeasurementType & ) const;

  /** Returns the pointer to the left child of this node */
  Superclass * Left()
  {
    return m_Left;
  }

  /** Returns the pointer to the right child of this node */
  Superclass * Right()
  {
    return m_Right;
  }

  /** Returns the const pointer to the left child of this node */
  const Superclass * Left() const
  {
    return m_Left;
  }

  /** Returns the const pointer to the right child of this node */
  const Superclass * Right() const
  {
    return m_Right;
  }

  /**
   * Returs the number of measurement vectors under this node including
   * its children
   */
  unsigned int Size() const
  {
    return 0;
  }

  /**
   * Returns the vector sum of the all measurement vectors under this node.
   * Do nothing for this class.
   */
  void GetWeightedCentroid( CentroidType & ) {}

  /**
   * Returns the centroid. weighted centroid divided by the size. Do nothing for
   * this class.
   */
  void GetCentroid( CentroidType & ) {}

  /**
   * Returns the identifier of the only MeasurementVector associated with
   * this node in the tree. This MeasurementVector will be used later during
   * the distance computation when querying the tree.
   */
  InstanceIdentifier GetInstanceIdentifier( InstanceIdentifier ) const
  {
    return this->m_InstanceIdentifier;
  }

  /**
   * Set the identifier of the node.
   */
  void AddInstanceIdentifier( InstanceIdentifier valueId )
  {
    this->m_InstanceIdentifier = valueId;
  }

private:

  unsigned int           m_PartitionDimension;
  MeasurementType        m_PartitionValue;
  InstanceIdentifier     m_InstanceIdentifier;
  Superclass            *m_Left;
  Superclass            *m_Right;
};  // end of class

/** \class KdTreeWeightedCentroidNonterminalNode
 *  \brief This is a subclass of the KdTreeNode.
 *
 * KdTreeNonterminalNode does have the information related with the
 * centroids. Therefore, the GetWeightedCentroid and the GetCentroid
 * methods returns meaningful values. This class should have the left
 * and right children. If we have a sample and want to generate a KdTree
 * with the centroid related information, we can use the
 * WeightedCentroidKdTreeGenerator. The centroid, the weighted
 * centroid, and the size (the number of measurement vectors) can be
 * used to accelate the k-means estimation.
 *
 * \sa KdTreeNode, KdTreeNonterminalNode, WeightedCentroidKdTreeGenerator
 * \ingroup ITKStatistics
 */
template<typename TSample>
struct KdTreeWeightedCentroidNonterminalNode:public KdTreeNode<TSample>
  {
  typedef KdTreeNode<TSample>                         Superclass;
  typedef typename Superclass::MeasurementType        MeasurementType;
  typedef typename Superclass::CentroidType           CentroidType;
  typedef typename Superclass::InstanceIdentifier     InstanceIdentifier;
  typedef typename TSample::MeasurementVectorSizeType MeasurementVectorSizeType;

  KdTreeWeightedCentroidNonterminalNode( unsigned int, MeasurementType,
    Superclass *, Superclass *, CentroidType &, unsigned int );

  virtual ~KdTreeWeightedCentroidNonterminalNode() {}

  /** Not a terminal node. */
  virtual bool IsTerminal() const
  {
    return false;
  }

  /** Return the parameters of the node. */
  void GetParameters( unsigned int &, MeasurementType & ) const;

  /** Return the length of a measurement vector */
  MeasurementVectorSizeType GetMeasurementVectorSize() const
  {
    return m_MeasurementVectorSize;
  }

  /** Return the left tree pointer. */
  Superclass * Left()
  {
    return m_Left;
  }

  /** Return the right tree pointer. */
  Superclass * Right()
  {
    return m_Right;
  }

  /** Return the left tree const pointer. */
  const Superclass * Left() const
  {
    return m_Left;
  }

  /** Return the right tree const pointer. */
  const Superclass * Right() const
  {
    return m_Right;
  }

  /** Return the size of the node. */
  unsigned int Size() const
  {
    return m_Size;
  }

  /**
   * Returns the vector sum of the all measurement vectors under this node.
   */
  void GetWeightedCentroid(CentroidType & centroid)
  {
    centroid = m_WeightedCentroid;
  }

  /**
   * Returns the centroid. weighted centroid divided by the size.
   */
  void GetCentroid(CentroidType & centroid)
  {
    centroid = m_Centroid;
  }

  /**
   * Returns the identifier of the only MeasurementVector associated with
   * this node in the tree. This MeasurementVector will be used later during
   * the distance computation when querying the tree.
   */
  InstanceIdentifier GetInstanceIdentifier(InstanceIdentifier) const
  {
    return this->m_InstanceIdentifier;
  }

  /**
   * Set the identifier of the node.
   */
  void AddInstanceIdentifier(InstanceIdentifier valueId)
  {
    this->m_InstanceIdentifier = valueId;
  }

private:
  MeasurementVectorSizeType     m_MeasurementVectorSize;
  unsigned int                  m_PartitionDimension;
  MeasurementType               m_PartitionValue;
  CentroidType                  m_WeightedCentroid;
  CentroidType                  m_Centroid;
  InstanceIdentifier            m_InstanceIdentifier;
  unsigned int                  m_Size;
  Superclass                   *m_Left;
  Superclass                   *m_Right;
};  // end of class

/** \class KdTreeTerminalNode
 *  \brief This class is the node that doesn't have any child node. The
 *  IsTerminal method returns true for this class. This class stores the
 *  instance identifiers belonging to this node, while the nonterminal
 *  nodes do not store them. The AddInstanceIdentifier and
 *  GetInstanceIdentifier are storing and retrieving the instance
 *  identifiers belonging to this node.
 *
 * \sa KdTreeNode, KdTreeNonterminalNode,
 * KdTreeWeightedCentroidNonterminalNode
 * \ingroup ITKStatistics
 */
template<typename TSample>
struct KdTreeTerminalNode:public KdTreeNode<TSample>
  {
  typedef KdTreeNode<TSample>                     Superclass;
  typedef typename Superclass::MeasurementType    MeasurementType;
  typedef typename Superclass::CentroidType       CentroidType;
  typedef typename Superclass::InstanceIdentifier InstanceIdentifier;

  KdTreeTerminalNode() {}

  virtual ~KdTreeTerminalNode()
  {
    this->m_InstanceIdentifiers.clear();
  }

  /** A terminal node. */
  bool IsTerminal() const
  {
    return true;
  }

  /** Return the parameters of the node. */
  void GetParameters( unsigned int &, MeasurementType & ) const {}

  /** Return the left tree pointer. Null for terminal nodes. */
  Superclass * Left()
  {
    return 0;
  }

  /** Return the right tree pointer. Null for terminal nodes. */
  Superclass * Right()
  {
    return 0;
  }

  /** Return the left tree const pointer. Null for terminal nodes. */
  const Superclass * Left() const
  {
    return 0;
  }

  /** Return the right tree const pointer. Null for terminal nodes. */
  const Superclass * Right() const
  {
    return 0;
  }

  /** Return the size of the node. */
  unsigned int Size() const
  {
    return static_cast< unsigned int >( m_InstanceIdentifiers.size() );
  }

  /**
   * Returns the vector sum of the all measurement vectors under this node.
   * Do nothing for this case.
   */
  void GetWeightedCentroid( CentroidType & ) {}

  /**
   * Returns the centroid. weighted centroid divided by the size.  Do nothing
   * for this case.
   */
  void GetCentroid( CentroidType & ) {}

  /**
   * Returns the identifier of the only MeasurementVector associated with
   * this node in the tree. This MeasurementVector will be used later during
   * the distance computation when querying the tree.
   */
  InstanceIdentifier GetInstanceIdentifier( InstanceIdentifier index ) const
  {
    return m_InstanceIdentifiers[index];
  }

  /**
   * Set the identifier of the node.
   */
  void AddInstanceIdentifier( InstanceIdentifier id )
  {
    m_InstanceIdentifiers.push_back( id );
  }

private:
  std::vector< InstanceIdentifier > m_InstanceIdentifiers;
};  // end of class

/** \class KdTree
 *  \brief This class provides methods for k-nearest neighbor search and
 *  related data structures for a k-d tree.
 *
 * An object of this class stores instance identifiers in a k-d tree
 * that is a binary tree with childrens split along a dimension among
 * k-dimensions. The dimension of the split (or partition) is determined
 * for each nonterminal node that has two children. The split process is
 * terminated when the node has no children (when the number of
 * measurement vectors is less than or equal to the size set by the
 * SetBucketSize. That is The split process is a recursive process in
 * nature and in implementation. This implementation doesn't support
 * dynamic insert and delete operations for the tree. Instead, we can
 * use the KdTreeGenerator or WeightedCentroidKdTreeGenerator to
 * generate a static KdTree object.
 *
 * To search k-nearest neighbor, call the Search method with the query
 * point in a k-d space and the number of nearest neighbors. The
 * GetSearchResult method returns a pointer to a NearestNeighbors object
 * with k-nearest neighbors.
 *
 * <b>Recent API changes:</b>
 * The static const macro to get the length of a measurement vector,
 * 'MeasurementVectorSize'  has been removed to allow the length of a measurement
 * vector to be specified at run time. Please use the function
 * GetMeasurementVectorSize() instead.

 * \sa KdTreeNode, KdTreeNonterminalNode,
 * KdTreeWeightedCentroidNonterminalNode, KdTreeTerminalNode,
 * KdTreeGenerator, WeightedCentroidKdTreeNode
 * \ingroup ITKStatistics
 */

template<typename TSample>
class KdTree:public Object
{
public:
  /** Standard class typedefs */
  typedef KdTree                     Self;
  typedef Object                     Superclass;
  typedef SmartPointer< Self >       Pointer;
  typedef SmartPointer< const Self > ConstPointer;

  /** Run-time type information (and related methods) */
  itkTypeMacro(KdTree, Object);

  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** typedef alias for the source data container */
  typedef TSample                                 SampleType;
  typedef typename TSample::MeasurementVectorType MeasurementVectorType;
  typedef typename TSample::MeasurementType       MeasurementType;
  typedef typename TSample::InstanceIdentifier    InstanceIdentifier;
  typedef typename TSample::AbsoluteFrequencyType AbsoluteFrequencyType;

  typedef unsigned int MeasurementVectorSizeType;

  /** Get Macro to get the length of a measurement vector in the KdTree.
   * The length is obtained from the input sample. */
  itkGetConstMacro( MeasurementVectorSize, MeasurementVectorSizeType );

  /** DistanceMetric type for the distance calculation and comparison */
  typedef EuclideanDistanceMetric< MeasurementVectorType > DistanceMetricType;

  /** Node type of the KdTree */
  typedef KdTreeNode<TSample> KdTreeNodeType;

  /** Neighbor type. The first element of the std::pair is the instance
   * identifier and the second one is the distance between the measurement
   * vector identified by the first element and the query point. */
  typedef std::pair< InstanceIdentifier, double > NeighborType;

  typedef std::vector< InstanceIdentifier > InstanceIdentifierVectorType;

  /** \class NearestNeighbors
   * \brief data structure for storing k-nearest neighbor search result
   * (k number of Neighbors)
   *
   * This class stores the instance identifiers and the distance values
   * of k-nearest neighbors. We can also query the farthest neighbor's
   * distance from the query point using the GetLargestDistance
   * method.
   * \ingroup ITKStatistics
   */
  class NearestNeighbors
  {
  public:
    /** Constructor */
    NearestNeighbors() {}

    /** Destructor */
    ~NearestNeighbors() {}

    /** Initialize the internal instance identifier and distance holders
     * with the size, k */
    void resize( unsigned int k )
    {
      m_Identifiers.clear();
      m_Identifiers.resize( k, NumericTraits< IdentifierType >::max() );
      m_Distances.clear();
      m_Distances.resize( k, NumericTraits<double>::max() );
      m_FarthestNeighborIndex = 0;
    }

    /** Returns the distance of the farthest neighbor from the query point */
    double GetLargestDistance()
    {
      return m_Distances[m_FarthestNeighborIndex];
    }

    /** Replaces the farthest neighbor's instance identifier and
     * distance value with the id and the distance */
    void ReplaceFarthestNeighbor( InstanceIdentifier id, double distance )
    {
      m_Identifiers[m_FarthestNeighborIndex] = id;
      m_Distances[m_FarthestNeighborIndex] = distance;
      double farthestDistance = NumericTraits<double>::min();
      const unsigned int size = static_cast< unsigned int >( m_Distances.size() );
      for ( unsigned int i = 0; i < size; i++ )
        {
        if ( m_Distances[i] > farthestDistance )
          {
          farthestDistance = m_Distances[i];
          m_FarthestNeighborIndex = i;
          }
        }
    }

    /** Returns the vector of k-neighbors' instance identifiers */
    const InstanceIdentifierVectorType & GetNeighbors() const
    {
      return m_Identifiers;
    }

    /** Returns the instance identifier of the index-th neighbor among
     * k-neighbors */
    InstanceIdentifier GetNeighbor(unsigned int index) const
    {
      return m_Identifiers[index];
    }

    /** Returns the vector of k-neighbors' instance identifiers */
    const std::vector<double> & GetDistances() const
    {
      return m_Distances;
    }

  private:
    /** The index of the farthest neighbor among k-neighbors */
    unsigned int m_FarthestNeighborIndex;

    /** Storage for the instance identifiers of k-neighbors */
    InstanceIdentifierVectorType m_Identifiers;

    /** Storage for the distance values of k-neighbors from the query
     * point */
    std::vector<double> m_Distances;
  };

  /** Sets the number of measurement vectors that can be stored in a
   * terminal node */
  void SetBucketSize( unsigned int );

  /** Sets the input sample that provides the measurement vectors to the k-d
   * tree */
  void SetSample( const TSample * );

  /** Returns the pointer to the input sample */
  const TSample * GetSample() const
  {
    return m_Sample;
  }

  SizeValueType Size() const
  {
    return m_Sample->Size();
  }

  /** Returns the pointer to the empty terminal node. A KdTree object
   * has a single empty terminal node in memory. when the split process
   * has to create an empty terminal node, the single instance is reused
   * for this case */
  KdTreeNodeType * GetEmptyTerminalNode()
  {
    return m_EmptyTerminalNode;
  }

  /** Sets the root node of the KdTree that is a result of
   * KdTreeGenerator or WeightedCentroidKdTreeGenerator. */
  void SetRoot(KdTreeNodeType *root)
  {
    if ( this->m_Root )
      {
      this->DeleteNode( this->m_Root );
      }
    this->m_Root = root;
  }

  /** Returns the pointer to the root node. */
  KdTreeNodeType * GetRoot()
  {
    return m_Root;
  }

  /** Returns the measurement vector identified by the instance
   * identifier that is an identifier defiend for the input sample */
  const MeasurementVectorType & GetMeasurementVector( InstanceIdentifier id )
    const
  {
    return m_Sample->GetMeasurementVector( id );
  }

  /** Returns the frequency of the measurement vector identified by
   * the instance identifier */
  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id ) const
  {
    return m_Sample->GetFrequency( id );
  }

  /** Get the pointer to the distance metric. */
  DistanceMetricType * GetDistanceMetric()
  {
    return m_DistanceMetric.GetPointer();
  }

  /** Searches the k-nearest neighbors */
  void Search( const MeasurementVectorType &, unsigned int,
    InstanceIdentifierVectorType & ) const;

  /** Searches the neighbors fallen into a hypersphere */
  void Search( const MeasurementVectorType &, double,
    InstanceIdentifierVectorType & ) const;

  /** Returns true if the intermediate k-nearest neighbors exist within
   * the the bounding box defined by the lowerBound and the
   * upperBound. Otherwise returns false. Returns false if the ball
   * defined by the distance between the query point and the farthest
   * neighbor touch the surface of the bounding box. */
  bool BallWithinBounds( const MeasurementVectorType &,
    MeasurementVectorType &, MeasurementVectorType &, double ) const;

  /** Returns true if the ball defined by the distance between the query
   * point and the farthest neighbor overlaps with the bounding box
   * defined by the lower and the upper bounds. */
  bool BoundsOverlapBall( const MeasurementVectorType &,
    MeasurementVectorType &, MeasurementVectorType &, double) const;

  /** Deletes the node recursively */
  void DeleteNode( KdTreeNodeType * );

  /** Prints out the tree information */
  void PrintTree( std::ostream & ) const;

  /** Prints out the tree information */
  void PrintTree( KdTreeNodeType *, unsigned int, unsigned int,
    std::ostream & os = std::cout ) const;

  /** Draw out the tree information to a ostream using
   * the format of the Graphviz dot tool. */
  void PlotTree( std::ostream & os ) const;

  /** Prints out the tree information */
  void PlotTree( KdTreeNodeType *node, std::ostream & os = std::cout ) const;

  typedef typename TSample::Iterator      Iterator;
  typedef typename TSample::ConstIterator ConstIterator;

protected:
  /** Constructor */
  KdTree();

  /** Destructor: deletes the root node and the empty terminal node. */
  virtual ~KdTree();

  void PrintSelf( std::ostream & os, Indent indent ) const;

  /** search loop */
  int NearestNeighborSearchLoop( const KdTreeNodeType *,
    const MeasurementVectorType &, MeasurementVectorType &,
    MeasurementVectorType &, NearestNeighbors & ) const;

  /** search loop */
  int SearchLoop( const KdTreeNodeType *, const MeasurementVectorType &,
    double, MeasurementVectorType &, MeasurementVectorType &,
    InstanceIdentifierVectorType & ) const;

private:
  KdTree( const Self & );         //purposely not implemented
  void operator=( const Self & ); //purposely not implemented

  /** Pointer to the input sample */
  const TSample *m_Sample;

  /** Number of measurement vectors can be stored in a terminal node. */
  int m_BucketSize;

  /** Pointer to the root node */
  KdTreeNodeType *m_Root;

  /** Pointer to the empty terminal node */
  KdTreeNodeType *m_EmptyTerminalNode;

  /** Distance metric smart pointer */
  typename DistanceMetricType::Pointer m_DistanceMetric;

  /** Measurement vector size */
  MeasurementVectorSizeType m_MeasurementVectorSize;
};  // end of class
} // end of namespace Statistics
} // end of namespace itk

#ifndef ITK_MANUAL_INSTANTIATION
#include "itkKdTree.hxx"
#endif

#endif