ITK  5.0.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  using MeasurementType = typename TSample::MeasurementType;
71 
74 
77  using InstanceIdentifier = typename TSample::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() = default; // 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 
148  ~KdTreeNonterminalNode() override = default;
149 
150  bool IsTerminal() const override
151  {
152  return false;
153  }
154 
155  void GetParameters( unsigned int &, MeasurementType & ) const override;
156 
158  Superclass * Left() override
159  {
160  return m_Left;
161  }
162 
164  Superclass * Right() override
165  {
166  return m_Right;
167  }
168 
170  const Superclass * Left() const override
171  {
172  return m_Left;
173  }
174 
176  const Superclass * Right() const override
177  {
178  return m_Right;
179  }
180 
185  unsigned int Size() const override
186  {
187  return 0;
188  }
189 
194  void GetWeightedCentroid( CentroidType & ) override {}
195 
200  void GetCentroid( CentroidType & ) override {}
201 
208  {
209  return this->m_InstanceIdentifier;
210  }
211 
215  void AddInstanceIdentifier( InstanceIdentifier valueId ) override
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  using MeasurementVectorSizeType = typename TSample::MeasurementVectorSizeType;
252 
254  Superclass *, Superclass *, CentroidType &, unsigned int );
255 
256  ~KdTreeWeightedCentroidNonterminalNode() override = default;
257 
259  bool IsTerminal() const override
260  {
261  return false;
262  }
263 
265  void GetParameters( unsigned int &, MeasurementType & ) const override;
266 
269  {
270  return m_MeasurementVectorSize;
271  }
272 
274  Superclass * Left() override
275  {
276  return m_Left;
277  }
278 
280  Superclass * Right() override
281  {
282  return m_Right;
283  }
284 
286  const Superclass * Left() const override
287  {
288  return m_Left;
289  }
290 
292  const Superclass * Right() const override
293  {
294  return m_Right;
295  }
296 
298  unsigned int Size() const override
299  {
300  return m_Size;
301  }
302 
306  void GetWeightedCentroid(CentroidType & centroid) override
307  {
308  centroid = m_WeightedCentroid;
309  }
310 
314  void GetCentroid(CentroidType & centroid) override
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 
369  KdTreeTerminalNode() = default;
370 
372  {
373  this->m_InstanceIdentifiers.clear();
374  }
375 
377  bool IsTerminal() const override
378  {
379  return true;
380  }
381 
383  void GetParameters( unsigned int &, MeasurementType & ) const override {}
384 
386  Superclass * Left() override
387  {
388  return nullptr;
389  }
390 
392  Superclass * Right() override
393  {
394  return nullptr;
395  }
396 
398  const Superclass * Left() const override
399  {
400  return nullptr;
401  }
402 
404  const Superclass * Right() const override
405  {
406  return nullptr;
407  }
408 
410  unsigned int Size() const override
411  {
412  return static_cast< unsigned int >( m_InstanceIdentifiers.size() );
413  }
414 
419  void GetWeightedCentroid( CentroidType & ) override {}
420 
425  void GetCentroid( CentroidType & ) override {}
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:
486  ITK_DISALLOW_COPY_AND_ASSIGN(KdTree);
487 
489  using Self = KdTree;
493 
495  itkTypeMacro(KdTree, Object);
496 
498  itkNewMacro(Self);
499 
501  using SampleType = TSample;
502  using MeasurementVectorType = typename TSample::MeasurementVectorType;
503  using MeasurementType = typename TSample::MeasurementType;
504  using InstanceIdentifier = typename TSample::InstanceIdentifier;
505  using AbsoluteFrequencyType = typename TSample::AbsoluteFrequencyType;
506 
507  using MeasurementVectorSizeType = unsigned int;
508 
511  itkGetConstMacro( MeasurementVectorSize, MeasurementVectorSizeType );
512 
515 
518 
522  using NeighborType = std::pair< InstanceIdentifier, double >;
523 
524  using InstanceIdentifierVectorType = std::vector< InstanceIdentifier >;
525 
537  {
538  public:
540  NearestNeighbors( std::vector<double> & cache_vector) : m_FarthestNeighborIndex(0), m_Distances(cache_vector) {}
541 
543  ~NearestNeighbors() = default;
544 
547  void resize( unsigned int k )
548  {
549  m_Identifiers.clear();
550  m_Identifiers.resize( k, NumericTraits< IdentifierType >::max() );
551  m_Distances.clear();
552  m_Distances.resize( k, NumericTraits<double>::max() );
553  m_FarthestNeighborIndex = 0;
554  }
556 
559  {
560  return m_Distances[m_FarthestNeighborIndex];
561  }
562 
565  void ReplaceFarthestNeighbor( InstanceIdentifier id, double distance )
566  {
567  m_Identifiers[m_FarthestNeighborIndex] = id;
568  m_Distances[m_FarthestNeighborIndex] = distance;
569  double farthestDistance = NumericTraits<double>::min();
570  const auto size = static_cast< unsigned int >( m_Distances.size() );
571  for ( unsigned int i = 0; i < size; i++ )
572  {
573  if ( m_Distances[i] > farthestDistance )
574  {
575  farthestDistance = m_Distances[i];
576  m_FarthestNeighborIndex = i;
577  }
578  }
579  }
581 
584  {
585  return m_Identifiers;
586  }
587 
590  InstanceIdentifier GetNeighbor(unsigned int index) const
591  {
592  return m_Identifiers[index];
593  }
594 
595  private:
596  NearestNeighbors() = delete;
599 
602 
606  std::vector<double> & m_Distances;
607  };
608 
611  void SetBucketSize( unsigned int );
612 
615  void SetSample( const TSample * );
616 
618  const TSample * GetSample() const
619  {
620  return m_Sample;
621  }
622 
624  {
625  return m_Sample->Size();
626  }
627 
633  {
634  return m_EmptyTerminalNode;
635  }
636 
640  {
641  if ( this->m_Root )
642  {
643  this->DeleteNode( this->m_Root );
644  }
645  this->m_Root = root;
646  }
648 
651  {
652  return m_Root;
653  }
654 
658  const
659  {
660  return m_Sample->GetMeasurementVector( id );
661  }
662 
666  {
667  return m_Sample->GetFrequency( id );
668  }
669 
672  {
673  return m_DistanceMetric.GetPointer();
674  }
675 
677  void Search( const MeasurementVectorType &, unsigned int,
678  InstanceIdentifierVectorType & ) const;
679 
683  void Search( const MeasurementVectorType &, unsigned int,
684  InstanceIdentifierVectorType &, std::vector<double> & ) const;
685 
687  void Search( const MeasurementVectorType &, double,
688  InstanceIdentifierVectorType & ) const;
689 
695  bool BallWithinBounds( const MeasurementVectorType &,
696  MeasurementVectorType &, MeasurementVectorType &, double ) const;
697 
701  bool BoundsOverlapBall( const MeasurementVectorType &,
702  MeasurementVectorType &, MeasurementVectorType &, double) const;
703 
705  void DeleteNode( KdTreeNodeType * );
706 
708  void PrintTree( std::ostream & ) const;
709 
711  void PrintTree( KdTreeNodeType *, unsigned int, unsigned int,
712  std::ostream & os = std::cout ) const;
713 
716  void PlotTree( std::ostream & os ) const;
717 
719  void PlotTree( KdTreeNodeType *node, std::ostream & os = std::cout ) const;
720 
721  using Iterator = typename TSample::Iterator;
722  using ConstIterator = typename TSample::ConstIterator;
723 
724 protected:
726  KdTree();
727 
729  ~KdTree() override;
730 
731  void PrintSelf( std::ostream & os, Indent indent ) const override;
732 
734  int NearestNeighborSearchLoop( const KdTreeNodeType *,
737 
739  int SearchLoop( const KdTreeNodeType *, const MeasurementVectorType &,
742 
743 private:
745  const TSample *m_Sample;
746 
749 
752 
755 
758 
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
const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const
Definition: itkKdTree.h:657
Superclass * Left() override
Definition: itkKdTree.h:158
Light weight base class for most itk classes.
KdTreeNodeType * GetEmptyTerminalNode()
Definition: itkKdTree.h:632
KdTreeNodeType * m_Root
Definition: itkKdTree.h:751
typename TSample::MeasurementType MeasurementType
Definition: itkKdTree.h:70
typename TSample::MeasurementType MeasurementType
Definition: itkKdTree.h:503
KdTreeNodeType * GetRoot()
Definition: itkKdTree.h:650
This class defines the interface of its derived classes.
Definition: itkKdTree.h:64
This is a subclass of the KdTreeNode.
Definition: itkKdTree.h:138
Define numeric traits for std::vector.
unsigned long SizeValueType
Definition: itkIntTypes.h:83
void ReplaceFarthestNeighbor(InstanceIdentifier id, double distance)
Definition: itkKdTree.h:565
void GetWeightedCentroid(CentroidType &centroid) override
Definition: itkKdTree.h:306
std::pair< InstanceIdentifier, double > NeighborType
Definition: itkKdTree.h:522
unsigned int Size() const override
Definition: itkKdTree.h:185
void GetCentroid(CentroidType &centroid) override
Definition: itkKdTree.h:314
typename TSample::MeasurementVectorType MeasurementVectorType
Definition: itkKdTree.h:502
SizeValueType Size() const
Definition: itkKdTree.h:623
const Superclass * Right() const override
Definition: itkKdTree.h:292
const Superclass * Left() const override
Definition: itkKdTree.h:170
std::vector< InstanceIdentifier > InstanceIdentifierVectorType
Definition: itkKdTree.h:524
InstanceIdentifier m_InstanceIdentifier
Definition: itkKdTree.h:224
data structure for storing k-nearest neighbor search result (k number of Neighbors) ...
Definition: itkKdTree.h:536
This class is the node that doesn&#39;t have any child node. The IsTerminal method returns true for this ...
Definition: itkKdTree.h:362
unsigned int Size() const override
Definition: itkKdTree.h:410
bool IsTerminal() const override
Definition: itkKdTree.h:377
void GetCentroid(CentroidType &) override
Definition: itkKdTree.h:200
const InstanceIdentifierVectorType & GetNeighbors() const
Definition: itkKdTree.h:583
const TSample * m_Sample
Definition: itkKdTree.h:745
DistanceMetricType * GetDistanceMetric()
Definition: itkKdTree.h:671
void GetWeightedCentroid(CentroidType &) override
Definition: itkKdTree.h:419
void SetRoot(KdTreeNodeType *root)
Definition: itkKdTree.h:639
typename TSample::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkKdTree.h:505
const Superclass * Left() const override
Definition: itkKdTree.h:286
Superclass * Right() override
Definition: itkKdTree.h:164
This is a subclass of the KdTreeNode.
Definition: itkKdTree.h:245
InstanceIdentifier GetInstanceIdentifier(InstanceIdentifier) const override
Definition: itkKdTree.h:324
KdTreeNodeType * m_EmptyTerminalNode
Definition: itkKdTree.h:754
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
Superclass * Right() override
Definition: itkKdTree.h:392
const Superclass * Left() const override
Definition: itkKdTree.h:398
MeasurementVectorSizeType GetMeasurementVectorSize() const
Definition: itkKdTree.h:268
AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const
Definition: itkKdTree.h:665
unsigned int MeasurementVectorSizeType
Definition: itkKdTree.h:507
const Superclass * Right() const override
Definition: itkKdTree.h:176
typename TSample::InstanceIdentifier InstanceIdentifier
Definition: itkKdTree.h:77
DistanceMetricType::Pointer m_DistanceMetric
Definition: itkKdTree.h:757
void GetCentroid(CentroidType &) override
Definition: itkKdTree.h:425
InstanceIdentifier GetNeighbor(unsigned int index) const
Definition: itkKdTree.h:590
std::vector< double > & m_Distances
Definition: itkKdTree.h:606
std::vector< InstanceIdentifier > m_InstanceIdentifiers
Definition: itkKdTree.h:446
void AddInstanceIdentifier(InstanceIdentifier valueId) override
Definition: itkKdTree.h:332
void AddInstanceIdentifier(InstanceIdentifier valueId) override
Definition: itkKdTree.h:215
InstanceIdentifier GetInstanceIdentifier(InstanceIdentifier index) const override
Definition: itkKdTree.h:432
void AddInstanceIdentifier(InstanceIdentifier id) override
Definition: itkKdTree.h:440
const Superclass * Right() const override
Definition: itkKdTree.h:404
void GetParameters(unsigned int &, MeasurementType &) const override
Definition: itkKdTree.h:383
Control indentation during Print() invocation.
Definition: itkIndent.h:49
typename TSample::InstanceIdentifier InstanceIdentifier
Definition: itkKdTree.h:504
bool IsTerminal() const override
Definition: itkKdTree.h:150
typename TSample::Iterator Iterator
Definition: itkKdTree.h:721
InstanceIdentifierVectorType m_Identifiers
Definition: itkKdTree.h:601
MeasurementVectorSizeType m_MeasurementVectorSize
Definition: itkKdTree.h:760
const TSample * GetSample() const
Definition: itkKdTree.h:618
Base class for most ITK classes.
Definition: itkObject.h:60
Superclass * Left() override
Definition: itkKdTree.h:386
NearestNeighbors(std::vector< double > &cache_vector)
Definition: itkKdTree.h:540
This class provides methods for k-nearest neighbor search and related data structures for a k-d tree...
Definition: itkKdTree.h:483
typename TSample::ConstIterator ConstIterator
Definition: itkKdTree.h:722
typename TSample::MeasurementVectorSizeType MeasurementVectorSizeType
Definition: itkKdTree.h:251
void GetWeightedCentroid(CentroidType &) override
Definition: itkKdTree.h:194
InstanceIdentifier GetInstanceIdentifier(InstanceIdentifier) const override
Definition: itkKdTree.h:207