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 } 00109 00111 template <class TTreeType> 00112 const typename LeafTreeIterator<TTreeType>::ValueType& 00113 LeafTreeIterator<TTreeType>::Next() 00114 { 00115 this->m_Position = const_cast<TreeNodeType* >(FindNextNode()); 00116 return this->m_Position->Get(); 00117 } 00118 00120 template <class TTreeType> 00121 const typename LeafTreeIterator<TTreeType>::TreeNodeType* 00122 LeafTreeIterator<TTreeType>::FindNextNode() const 00123 { 00124 PreOrderTreeIterator<TTreeType> it(this->m_Tree,this->m_Position); 00125 ++it; // go next 00126 if(it.IsAtEnd()) 00127 { 00128 return NULL; 00129 } 00130 00131 if(!it.HasChild()) 00132 { 00133 return it.GetNode(); 00134 } 00135 00136 while( !it.IsAtEnd() ) 00137 { 00138 if(!it.HasChild()) 00139 { 00140 return it.GetNode(); 00141 } 00142 ++it; 00143 } 00144 00145 return NULL; 00146 } 00147 00149 template <class TTreeType> 00150 TreeIteratorBase<TTreeType>* LeafTreeIterator<TTreeType>::Clone() 00151 { 00152 LeafTreeIterator<TTreeType>* clone = new LeafTreeIterator<TTreeType>( this->m_Tree ); 00153 *clone = *this; 00154 return clone; 00155 } 00156 00157 } // end namespace itk 00158 00159 #endif