00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: itkLeafTreeIterator.h,v $ 00005 Language: C++ 00006 Date: $Date: 2008-01-29 15:27:42 $ 00007 Version: $Revision: 1.5 $ 00008 00009 Copyright (c) Insight Software Consortium. All rights reserved. 00010 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 #ifndef __itkLeafTreeIterator_h 00018 #define __itkLeafTreeIterator_h 00019 00020 #include <itkTreeIteratorBase.h> 00021 #include <itkPreOrderTreeIterator.h> 00022 00023 namespace itk{ 00024 00025 template <class TTreeType> 00026 class LeafTreeIterator : public TreeIteratorBase<TTreeType> 00027 { 00028 public: 00029 00031 typedef LeafTreeIterator Self; 00032 typedef TreeIteratorBase<TTreeType> Superclass; 00033 typedef TTreeType TreeType; 00034 typedef typename TreeType::ValueType ValueType; 00035 typedef TreeNode<ValueType> TreeNodeType; 00036 00038 LeafTreeIterator( const TreeType* tree ); 00039 00041 LeafTreeIterator( TreeType* tree ); 00042 00044 virtual ~LeafTreeIterator(); 00045 00047 int GetType() const; 00048 00050 TreeIteratorBase<TTreeType>* Clone(); 00051 00052 protected: 00053 00055 const ValueType& Next(); 00056 00058 bool HasNext() const; 00059 00060 private: 00061 00063 const TreeNodeType* FindNextNode() const; 00064 00065 }; 00066 00068 template <class TTreeType> 00069 LeafTreeIterator<TTreeType>::LeafTreeIterator( const TTreeType* tree ) 00070 :TreeIteratorBase<TTreeType>(tree,NULL) 00071 { 00072 this->m_Begin = const_cast<TreeNodeType* >(this->FindNextNode()); // Position the iterator to the first leaf; 00073 } 00074 00076 template <class TTreeType> 00077 LeafTreeIterator<TTreeType>::LeafTreeIterator( TTreeType* tree ) 00078 :TreeIteratorBase<TTreeType>(tree,NULL) 00079 { 00080 this->m_Begin = const_cast<TreeNodeType* >(this->FindNextNode()); // Position the iterator to the first leaf; 00081 } 00082 00084 template <class TTreeType> 00085 LeafTreeIterator<TTreeType>::~LeafTreeIterator() 00086 { 00087 } 00088 00090 template <class TTreeType> 00091 int LeafTreeIterator<TTreeType>::GetType() const 00092 { 00093 return TreeIteratorBase<TTreeType>::LEAF; 00094 } 00095 00097 template <class TTreeType> 00098 bool LeafTreeIterator<TTreeType>::HasNext() const 00099 { 00100 if(this->m_Position == NULL) 00101 { 00102 return false; 00103 } 00104 if ( const_cast<TreeNodeType* >(FindNextNode()) != NULL ) 00105 { 00106 return true; 00107 } 00108 return false; 00109 } 00111 00113 template <class TTreeType> 00114 const typename LeafTreeIterator<TTreeType>::ValueType& 00115 LeafTreeIterator<TTreeType>::Next() 00116 { 00117 this->m_Position = const_cast<TreeNodeType* >(FindNextNode()); 00118 return this->m_Position->Get(); 00119 } 00121 00123 template <class TTreeType> 00124 const typename LeafTreeIterator<TTreeType>::TreeNodeType* 00125 LeafTreeIterator<TTreeType>::FindNextNode() const 00126 { 00127 PreOrderTreeIterator<TTreeType> it(this->m_Tree,this->m_Position); 00128 ++it; // go next 00129 if(it.IsAtEnd()) 00130 { 00131 return NULL; 00132 } 00134 00135 if(!it.HasChild()) 00136 { 00137 return it.GetNode(); 00138 } 00139 00140 while( !it.IsAtEnd() ) 00141 { 00142 if(!it.HasChild()) 00143 { 00144 return it.GetNode(); 00145 } 00146 ++it; 00147 } 00148 00149 return NULL; 00150 } 00151 00153 template <class TTreeType> 00154 TreeIteratorBase<TTreeType>* LeafTreeIterator<TTreeType>::Clone() 00155 { 00156 LeafTreeIterator<TTreeType>* clone = new LeafTreeIterator<TTreeType>( this->m_Tree ); 00157 *clone = *this; 00158 return clone; 00159 } 00161 00162 } // end namespace itk 00163 00164 #endif 00165