ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkKdTree.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkKdTree_h
19 #define itkKdTree_h
20 
21 #include <queue>
22 #include <vector>
23 
24 #include "itkPoint.h"
25 #include "itkSize.h"
26 #include "itkObject.h"
27 #include "itkArray.h"
28 
29 #include "itkSubsample.h"
30 
32 
33 namespace itk
34 {
35 namespace Statistics
36 {
62 template<typename TSample>
63 
64 struct ITK_TEMPLATE_EXPORT KdTreeNode
65  {
68 
70  typedef typename TSample::MeasurementType MeasurementType;
71 
74 
77  typedef typename TSample::InstanceIdentifier InstanceIdentifier;
78 
81  virtual bool IsTerminal() const = 0;
82 
88  virtual void GetParameters( unsigned int &, MeasurementType & ) const = 0;
89 
91  virtual Self * Left() = 0;
92 
94  virtual const Self * Left() const = 0;
95 
97  virtual Self * Right() = 0;
98 
100  virtual const Self * Right() const = 0;
101 
106  virtual unsigned int Size() const = 0;
107 
109  virtual void GetWeightedCentroid( CentroidType & ) = 0;
110 
112  virtual void GetCentroid( CentroidType & ) = 0;
113 
115  virtual InstanceIdentifier GetInstanceIdentifier( InstanceIdentifier ) const = 0;
116 
118  virtual void AddInstanceIdentifier( InstanceIdentifier ) = 0;
119 
121  virtual ~KdTreeNode() {} // needed to subclasses will actually be deleted
122 }; // end of class
123 
136 template<typename TSample>
137 
138 struct ITK_TEMPLATE_EXPORT KdTreeNonterminalNode:public KdTreeNode<TSample>
139  {
144 
146  Superclass * );
147 
149 
150  virtual bool IsTerminal() const
151  {
152  return false;
153  }
154 
155  void GetParameters( unsigned int &, MeasurementType & ) const;
156 
159  {
160  return m_Left;
161  }
162 
165  {
166  return m_Right;
167  }
168 
170  const Superclass * Left() const
171  {
172  return m_Left;
173  }
174 
176  const Superclass * Right() const
177  {
178  return m_Right;
179  }
180 
185  unsigned int Size() const
186  {
187  return 0;
188  }
189 
195 
201 
208  {
209  return this->m_InstanceIdentifier;
210  }
211 
216  {
217  this->m_InstanceIdentifier = valueId;
218  }
219 
220 private:
221 
222  unsigned int m_PartitionDimension;
227 }; // end of class
228 
244 template<typename TSample>
245 struct ITK_TEMPLATE_EXPORT KdTreeWeightedCentroidNonterminalNode:public KdTreeNode<TSample>
246  {
251  typedef typename TSample::MeasurementVectorSizeType MeasurementVectorSizeType;
252 
254  Superclass *, Superclass *, CentroidType &, unsigned int );
255 
257 
259  virtual bool IsTerminal() const
260  {
261  return false;
262  }
263 
265  void GetParameters( unsigned int &, MeasurementType & ) const;
266 
269  {
270  return m_MeasurementVectorSize;
271  }
272 
275  {
276  return m_Left;
277  }
278 
281  {
282  return m_Right;
283  }
284 
286  const Superclass * Left() const
287  {
288  return m_Left;
289  }
290 
292  const Superclass * Right() const
293  {
294  return m_Right;
295  }
296 
298  unsigned int Size() const
299  {
300  return m_Size;
301  }
302 
307  {
308  centroid = m_WeightedCentroid;
309  }
310 
314  void GetCentroid(CentroidType & centroid)
315  {
316  centroid = m_Centroid;
317  }
318 
325  {
326  return this->m_InstanceIdentifier;
327  }
328 
333  {
334  this->m_InstanceIdentifier = valueId;
335  }
336 
337 private:
339  unsigned int m_PartitionDimension;
344  unsigned int m_Size;
347 }; // end of class
348 
361 template<typename TSample>
362 struct ITK_TEMPLATE_EXPORT KdTreeTerminalNode:public KdTreeNode<TSample>
363  {
368 
370 
372  {
373  this->m_InstanceIdentifiers.clear();
374  }
375 
377  bool IsTerminal() const
378  {
379  return true;
380  }
381 
383  void GetParameters( unsigned int &, MeasurementType & ) const {}
384 
387  {
388  return ITK_NULLPTR;
389  }
390 
393  {
394  return ITK_NULLPTR;
395  }
396 
398  const Superclass * Left() const
399  {
400  return ITK_NULLPTR;
401  }
402 
404  const Superclass * Right() const
405  {
406  return ITK_NULLPTR;
407  }
408 
410  unsigned int Size() const
411  {
412  return static_cast< unsigned int >( m_InstanceIdentifiers.size() );
413  }
414 
420 
426 
433  {
434  return m_InstanceIdentifiers[index];
435  }
436 
441  {
442  m_InstanceIdentifiers.push_back( id );
443  }
444 
445 private:
446  std::vector< InstanceIdentifier > m_InstanceIdentifiers;
447 }; // end of class
448 
482 template<typename TSample>
483 class ITK_TEMPLATE_EXPORT KdTree:public Object
484 {
485 public:
487  typedef KdTree Self;
491 
493  itkTypeMacro(KdTree, Object);
494 
496  itkNewMacro(Self);
497 
499  typedef TSample SampleType;
500  typedef typename TSample::MeasurementVectorType MeasurementVectorType;
501  typedef typename TSample::MeasurementType MeasurementType;
502  typedef typename TSample::InstanceIdentifier InstanceIdentifier;
503  typedef typename TSample::AbsoluteFrequencyType AbsoluteFrequencyType;
504 
505  typedef unsigned int MeasurementVectorSizeType;
506 
509  itkGetConstMacro( MeasurementVectorSize, MeasurementVectorSizeType );
510 
513 
516 
520  typedef std::pair< InstanceIdentifier, double > NeighborType;
521 
522  typedef std::vector< InstanceIdentifier > InstanceIdentifierVectorType;
523 
535  {
536  public:
538  NearestNeighbors( std::vector<double> & cache_vector) : m_FarthestNeighborIndex(0), m_Distances(cache_vector) {}
539 
542 
545  void resize( unsigned int k )
546  {
547  m_Identifiers.clear();
548  m_Identifiers.resize( k, NumericTraits< IdentifierType >::max() );
549  m_Distances.clear();
550  m_Distances.resize( k, NumericTraits<double>::max() );
551  m_FarthestNeighborIndex = 0;
552  }
554 
557  {
558  return m_Distances[m_FarthestNeighborIndex];
559  }
560 
563  void ReplaceFarthestNeighbor( InstanceIdentifier id, double distance )
564  {
565  m_Identifiers[m_FarthestNeighborIndex] = id;
566  m_Distances[m_FarthestNeighborIndex] = distance;
567  double farthestDistance = NumericTraits<double>::min();
568  const unsigned int size = static_cast< unsigned int >( m_Distances.size() );
569  for ( unsigned int i = 0; i < size; i++ )
570  {
571  if ( m_Distances[i] > farthestDistance )
572  {
573  farthestDistance = m_Distances[i];
574  m_FarthestNeighborIndex = i;
575  }
576  }
577  }
579 
582  {
583  return m_Identifiers;
584  }
585 
588  InstanceIdentifier GetNeighbor(unsigned int index) const
589  {
590  return m_Identifiers[index];
591  }
592 
593  private:
594  NearestNeighbors() ITK_DELETED_FUNCTION;
596  unsigned int m_FarthestNeighborIndex;
597 
600 
604  std::vector<double> & m_Distances;
605  };
606 
609  void SetBucketSize( unsigned int );
610 
613  void SetSample( const TSample * );
614 
616  const TSample * GetSample() const
617  {
618  return m_Sample;
619  }
620 
622  {
623  return m_Sample->Size();
624  }
625 
631  {
632  return m_EmptyTerminalNode;
633  }
634 
638  {
639  if ( this->m_Root )
640  {
641  this->DeleteNode( this->m_Root );
642  }
643  this->m_Root = root;
644  }
646 
649  {
650  return m_Root;
651  }
652 
656  const
657  {
658  return m_Sample->GetMeasurementVector( id );
659  }
660 
664  {
665  return m_Sample->GetFrequency( id );
666  }
667 
670  {
671  return m_DistanceMetric.GetPointer();
672  }
673 
675  void Search( const MeasurementVectorType &, unsigned int,
676  InstanceIdentifierVectorType & ) const;
677 
681  void Search( const MeasurementVectorType &, unsigned int,
682  InstanceIdentifierVectorType &, std::vector<double> & ) const;
683 
685  void Search( const MeasurementVectorType &, double,
686  InstanceIdentifierVectorType & ) const;
687 
693  bool BallWithinBounds( const MeasurementVectorType &,
694  MeasurementVectorType &, MeasurementVectorType &, double ) const;
695 
699  bool BoundsOverlapBall( const MeasurementVectorType &,
700  MeasurementVectorType &, MeasurementVectorType &, double) const;
701 
703  void DeleteNode( KdTreeNodeType * );
704 
706  void PrintTree( std::ostream & ) const;
707 
709  void PrintTree( KdTreeNodeType *, unsigned int, unsigned int,
710  std::ostream & os = std::cout ) const;
711 
714  void PlotTree( std::ostream & os ) const;
715 
717  void PlotTree( KdTreeNodeType *node, std::ostream & os = std::cout ) const;
718 
719  typedef typename TSample::Iterator Iterator;
720  typedef typename TSample::ConstIterator ConstIterator;
721 
722 protected:
724  KdTree();
725 
727  virtual ~KdTree() ITK_OVERRIDE;
728 
729  virtual void PrintSelf( std::ostream & os, Indent indent ) const ITK_OVERRIDE;
730 
732  int NearestNeighborSearchLoop( const KdTreeNodeType *,
733  const MeasurementVectorType &, MeasurementVectorType &,
734  MeasurementVectorType &, NearestNeighbors & ) const;
735 
737  int SearchLoop( const KdTreeNodeType *, const MeasurementVectorType &,
738  double, MeasurementVectorType &, MeasurementVectorType &,
740 
741 private:
742  ITK_DISALLOW_COPY_AND_ASSIGN(KdTree);
743 
745  const TSample *m_Sample;
746 
748  int m_BucketSize;
749 
751  KdTreeNodeType *m_Root;
752 
754  KdTreeNodeType *m_EmptyTerminalNode;
755 
757  typename DistanceMetricType::Pointer m_DistanceMetric;
758 
760  MeasurementVectorSizeType m_MeasurementVectorSize;
761 }; // end of class
762 } // end of namespace Statistics
763 } // end of namespace itk
764 
765 #ifndef ITK_MANUAL_INSTANTIATION
766 #include "itkKdTree.hxx"
767 #endif
768 
769 #endif
void GetWeightedCentroid(CentroidType &)
Definition: itkKdTree.h:419
const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const
Definition: itkKdTree.h:655
void GetCentroid(CentroidType &)
Definition: itkKdTree.h:425
KdTreeNode< TSample > Self
Definition: itkKdTree.h:67
TSample::ConstIterator ConstIterator
Definition: itkKdTree.h:720
Light weight base class for most itk classes.
InstanceIdentifier GetInstanceIdentifier(InstanceIdentifier index) const
Definition: itkKdTree.h:432
KdTreeNodeType * GetEmptyTerminalNode()
Definition: itkKdTree.h:630
Superclass::InstanceIdentifier InstanceIdentifier
Definition: itkKdTree.h:250
KdTreeNodeType * GetRoot()
Definition: itkKdTree.h:648
This class defines the interface of its derived classes.
Definition: itkKdTree.h:64
This is a subclass of the KdTreeNode.
Definition: itkKdTree.h:138
const Superclass * Left() const
Definition: itkKdTree.h:170
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
void AddInstanceIdentifier(InstanceIdentifier valueId)
Definition: itkKdTree.h:215
const Superclass * Right() const
Definition: itkKdTree.h:176
TSample::MeasurementVectorType MeasurementVectorType
Definition: itkKdTree.h:500
void ReplaceFarthestNeighbor(InstanceIdentifier id, double distance)
Definition: itkKdTree.h:563
TSample::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkKdTree.h:503
void GetWeightedCentroid(CentroidType &)
Definition: itkKdTree.h:194
TSample::InstanceIdentifier InstanceIdentifier
Definition: itkKdTree.h:77
void GetWeightedCentroid(CentroidType &centroid)
Definition: itkKdTree.h:306
TSample::MeasurementType MeasurementType
Definition: itkKdTree.h:501
SizeValueType Size() const
Definition: itkKdTree.h:621
EuclideanDistanceMetric< MeasurementVectorType > DistanceMetricType
Definition: itkKdTree.h:509
InstanceIdentifier GetInstanceIdentifier(InstanceIdentifier) const
Definition: itkKdTree.h:324
unsigned long SizeValueType
Definition: itkIntTypes.h:143
const Superclass * Left() const
Definition: itkKdTree.h:398
Superclass::CentroidType CentroidType
Definition: itkKdTree.h:142
KdTreeNode< TSample > KdTreeNodeType
Definition: itkKdTree.h:515
void GetCentroid(CentroidType &)
Definition: itkKdTree.h:200
InstanceIdentifier m_InstanceIdentifier
Definition: itkKdTree.h:224
virtual bool IsTerminal() const
Definition: itkKdTree.h:150
data structure for storing k-nearest neighbor search result (k number of Neighbors) ...
Definition: itkKdTree.h:534
This class is the node that doesn&#39;t have any child node. The IsTerminal method returns true for this ...
Definition: itkKdTree.h:362
TSample::Iterator Iterator
Definition: itkKdTree.h:719
unsigned int Size() const
Definition: itkKdTree.h:410
const InstanceIdentifierVectorType & GetNeighbors() const
Definition: itkKdTree.h:581
InstanceIdentifier GetInstanceIdentifier(InstanceIdentifier) const
Definition: itkKdTree.h:207
DistanceMetricType * GetDistanceMetric()
Definition: itkKdTree.h:669
void SetRoot(KdTreeNodeType *root)
Definition: itkKdTree.h:637
void AddInstanceIdentifier(InstanceIdentifier id)
Definition: itkKdTree.h:440
This is a subclass of the KdTreeNode.
Definition: itkKdTree.h:245
Superclass::InstanceIdentifier InstanceIdentifier
Definition: itkKdTree.h:367
MeasurementVectorSizeType GetMeasurementVectorSize() const
Definition: itkKdTree.h:268
AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const
Definition: itkKdTree.h:663
TSample::InstanceIdentifier InstanceIdentifier
Definition: itkKdTree.h:502
const Superclass * Right() const
Definition: itkKdTree.h:404
Array< double > CentroidType
Definition: itkKdTree.h:73
std::pair< InstanceIdentifier, double > NeighborType
Definition: itkKdTree.h:520
std::vector< InstanceIdentifier > InstanceIdentifierVectorType
Definition: itkKdTree.h:522
Superclass::MeasurementType MeasurementType
Definition: itkKdTree.h:365
InstanceIdentifier GetNeighbor(unsigned int index) const
Definition: itkKdTree.h:588
Superclass::CentroidType CentroidType
Definition: itkKdTree.h:366
unsigned int MeasurementVectorSizeType
Definition: itkKdTree.h:505
std::vector< InstanceIdentifier > m_InstanceIdentifiers
Definition: itkKdTree.h:446
static ITK_CONSTEXPR_FUNC T min(const T &)
KdTreeNode< TSample > Superclass
Definition: itkKdTree.h:364
TSample::MeasurementVectorSizeType MeasurementVectorSizeType
Definition: itkKdTree.h:251
void GetParameters(unsigned int &, MeasurementType &) const
Definition: itkKdTree.h:383
Control indentation during Print() invocation.
Definition: itkIndent.h:49
KdTreeNode< TSample > Superclass
Definition: itkKdTree.h:140
Define additional traits for native types such as int or float.
void AddInstanceIdentifier(InstanceIdentifier valueId)
Definition: itkKdTree.h:332
Base class for most ITK classes.
Definition: itkObject.h:59
SmartPointer< Self > Pointer
Definition: itkKdTree.h:489
NearestNeighbors(std::vector< double > &cache_vector)
Definition: itkKdTree.h:538
Superclass::MeasurementType MeasurementType
Definition: itkKdTree.h:141
This class provides methods for k-nearest neighbor search and related data structures for a k-d tree...
Definition: itkKdTree.h:483
Superclass::InstanceIdentifier InstanceIdentifier
Definition: itkKdTree.h:143
SmartPointer< const Self > ConstPointer
Definition: itkKdTree.h:490
TSample::MeasurementType MeasurementType
Definition: itkKdTree.h:70