00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: itkLeafTreeIterator.h,v $ 00005 Language: C++ 00006 Date: $Date: 2004/12/11 20:29:18 $ 00007 Version: $Revision: 1.3 $ 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 TreeIteratorBase<TTreeType> Superclass; 00032 typedef TTreeType TreeType; 00033 typedef typename TreeType::ValueType ValueType; 00034 typedef TreeNode<ValueType> TreeNodeType; 00035 00037 LeafTreeIterator( const TreeType* tree ); 00038 00040 LeafTreeIterator( TreeType* tree ); 00041 00043 virtual ~LeafTreeIterator(); 00044 00046 int GetType() const; 00047 00049 TreeIteratorBase<TTreeType>* Clone(); 00050 00051 protected: 00052 00054 const ValueType& Next(); 00055 00057 bool HasNext() const; 00058 00059 private: 00060 00062 const TreeNodeType* FindNextNode() const; 00063 00064 }; 00065 00067 template <class TTreeType> 00068 LeafTreeIterator<TTreeType>::LeafTreeIterator( const TTreeType* tree ) 00069 :TreeIteratorBase<TTreeType>(tree,NULL) 00070 { 00071 this->m_Begin = const_cast<TreeNodeType* >(this->FindNextNode()); // Position the iterator to the first leaf; 00072 } 00073 00075 template <class TTreeType> 00076 LeafTreeIterator<TTreeType>::LeafTreeIterator( TTreeType* tree ) 00077 :TreeIteratorBase<TTreeType>(tree,NULL) 00078 { 00079 this->m_Begin = const_cast<TreeNodeType* >(this->FindNextNode()); // Position the iterator to the first leaf; 00080 } 00081 00083 template <class TTreeType> 00084 LeafTreeIterator<TTreeType>::~LeafTreeIterator() 00085 { 00086 } 00087 00089 template <class TTreeType> 00090 int LeafTreeIterator<TTreeType>::GetType() const 00091 { 00092 return TreeIteratorBase<TTreeType>::LEAF; 00093 } 00094 00096 template <class TTreeType> 00097 bool LeafTreeIterator<TTreeType>::HasNext() const 00098 { 00099 if(this->m_Position == NULL) 00100 { 00101 return false; 00102 } 00103 if ( const_cast<TreeNodeType* >(FindNextNode()) != NULL ) 00104 { 00105 return true; 00106 } 00107 return false; 00108 } 00110 00112 template <class TTreeType> 00113 const typename LeafTreeIterator<TTreeType>::ValueType& 00114 LeafTreeIterator<TTreeType>::Next() 00115 { 00116 this->m_Position = const_cast<TreeNodeType* >(FindNextNode()); 00117 return this->m_Position->Get(); 00118 } 00120 00122 template <class TTreeType> 00123 const typename LeafTreeIterator<TTreeType>::TreeNodeType* 00124 LeafTreeIterator<TTreeType>::FindNextNode() const 00125 { 00126 PreOrderTreeIterator<TTreeType> it(this->m_Tree,this->m_Position); 00127 ++it; // go next 00128 if(it.IsAtEnd()) 00129 { 00130 return NULL; 00131 } 00133 00134 if(!it.HasChild()) 00135 { 00136 return it.GetNode(); 00137 } 00138 00139 while( !it.IsAtEnd() ) 00140 { 00141 if(!it.HasChild()) 00142 { 00143 return it.GetNode(); 00144 } 00145 ++it; 00146 } 00147 00148 return NULL; 00149 } 00150 00152 template <class TTreeType> 00153 TreeIteratorBase<TTreeType>* LeafTreeIterator<TTreeType>::Clone() 00154 { 00155 LeafTreeIterator<TTreeType>* clone = new LeafTreeIterator<TTreeType>( this->m_Tree ); 00156 *clone = *this; 00157 return clone; 00158 } 00160 00161 } // end namespace itk 00162 00163 #endif 00164