ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkLabelObject.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkLabelObject_h
00019 #define __itkLabelObject_h
00020 
00021 #include <deque>
00022 #include "itkLightObject.h"
00023 #include "itkLabelObjectLine.h"
00024 #include "itkWeakPointer.h"
00025 #include "itkObjectFactory.h"
00026 
00027 namespace itk
00028 {
00063 template< class TLabel, unsigned int VImageDimension >
00064 class ITK_EXPORT LabelObject:public LightObject
00065 {
00066 public:
00068   typedef LabelObject                Self;
00069   typedef LightObject                Superclass;
00070   typedef Self                       LabelObjectType;
00071   typedef SmartPointer< Self >       Pointer;
00072   typedef SmartPointer< const Self > ConstPointer;
00073   typedef WeakPointer< const Self >  ConstWeakPointer;
00074 
00076   itkNewMacro(Self);
00077 
00079   itkTypeMacro(LabelObject, LightObject);
00080 
00081   itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
00082 
00083   typedef Index< VImageDimension >           IndexType;
00084   typedef Offset< VImageDimension >          OffsetType;
00085   typedef TLabel                             LabelType;
00086   typedef LabelObjectLine< VImageDimension > LineType;
00087   typedef typename LineType::LengthType      LengthType;
00088   typedef unsigned int                       AttributeType;
00089   typedef itk::SizeValueType                 SizeValueType;
00090 
00091   itkStaticConstMacro(LABEL, AttributeType, 0);
00092 
00093   static AttributeType GetAttributeFromName(const std::string & s);
00094 
00095   static std::string GetNameFromAttribute(const AttributeType & a);
00096 
00100   const LabelType & GetLabel() const;
00101 
00102   void SetLabel(const LabelType & label);
00103 
00108   bool HasIndex(const IndexType & idx) const;
00109 
00114   void AddIndex(const IndexType & idx);
00115 
00121   bool RemoveIndex(const IndexType & idx );
00122 
00126   void AddLine(const IndexType & idx, const LengthType & length);
00127 
00131   void AddLine(const LineType & line);
00132 
00133   SizeValueType GetNumberOfLines() const;
00134 
00135   const LineType & GetLine(SizeValueType i) const;
00136 
00137   LineType & GetLine(SizeValueType i);
00138 
00146   SizeValueType Size() const;
00147 
00152   bool Empty() const;
00153 
00154   void Clear();
00155 
00160   IndexType GetIndex(SizeValueType i) const;
00161 
00163   virtual void CopyAttributesFrom(const Self *src);
00164 
00166   void CopyAllFrom(const Self *src);
00167 
00171   void Optimize();
00172 
00174   void Shift( OffsetType offset );
00175 
00180   class ConstLineIterator
00181   {
00182   public:
00183 
00184     ConstLineIterator() {}
00185 
00186     ConstLineIterator(const Self *lo)
00187     {
00188       m_Begin = lo->m_LineContainer.begin();
00189       m_End = lo->m_LineContainer.end();
00190       m_Iterator = m_Begin;
00191     }
00192 
00193     ConstLineIterator(const ConstLineIterator & iter)
00194     {
00195       m_Iterator = iter.m_Iterator;
00196       m_Begin = iter.m_Begin;
00197       m_End = iter.m_End;
00198     }
00199 
00200     ConstLineIterator & operator=(const ConstLineIterator & iter)
00201     {
00202       m_Iterator = iter.m_Iterator;
00203       m_Begin = iter.m_Begin;
00204       m_End = iter.m_End;
00205       return *this;
00206     }
00207 
00208     const LineType & GetLine() const
00209     {
00210       return *m_Iterator;
00211     }
00212 
00213     ConstLineIterator operator++(int)
00214     {
00215       ConstLineIterator tmp = *this;
00216       ++(*this);
00217       return tmp;
00218     }
00219 
00220     ConstLineIterator & operator++()
00221     {
00222       ++m_Iterator;
00223       return *this;
00224     }
00225 
00226   bool operator==(const ConstLineIterator & iter) const
00227     {
00228     return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
00229     }
00230 
00231   bool operator!=(const ConstLineIterator & iter) const
00232     {
00233     return !( *this == iter );
00234     }
00235 
00236   void GoToBegin()
00237     {
00238       m_Iterator = m_Begin;
00239     }
00240 
00241     bool IsAtEnd() const
00242     {
00243       return m_Iterator == m_End;
00244     }
00245 
00246   private:
00247     typedef typename std::deque< LineType >            LineContainerType;
00248     typedef typename LineContainerType::const_iterator InternalIteratorType;
00249     InternalIteratorType m_Iterator;
00250     InternalIteratorType m_Begin;
00251     InternalIteratorType m_End;
00252   };
00253 
00258   class ConstIndexIterator
00259   {
00260   public:
00261 
00262     ConstIndexIterator():
00263       m_Iterator(),
00264       m_Begin(),
00265       m_End()
00266         {
00267         m_Index.Fill(0);
00268         }
00269 
00270     ConstIndexIterator(const Self *lo)
00271     {
00272       m_Begin = lo->m_LineContainer.begin();
00273       m_End = lo->m_LineContainer.end();
00274       GoToBegin();
00275     }
00276 
00277     ConstIndexIterator(const ConstIndexIterator & iter)
00278     {
00279       m_Iterator = iter.m_Iterator;
00280       m_Index = iter.m_Index;
00281       m_Begin = iter.m_Begin;
00282       m_End = iter.m_End;
00283     }
00284 
00285     ConstIndexIterator & operator=(const ConstIndexIterator & iter)
00286     {
00287       m_Iterator = iter.m_Iterator;
00288       m_Index = iter.m_Index;
00289       m_Begin = iter.m_Begin;
00290       m_End = iter.m_End;
00291       return *this;
00292     }
00293 
00294     const IndexType & GetIndex() const
00295     {
00296       return m_Index;
00297     }
00298 
00299     ConstIndexIterator & operator++()
00300     {
00301       m_Index[0]++;
00302       if( m_Index[0] >= m_Iterator->GetIndex()[0] + (OffsetValueType)m_Iterator->GetLength() )
00303         {
00304         // we've reached the end of the line - go to the next one
00305         ++m_Iterator;
00306         NextValidLine();
00307         }
00308       return *this;
00309     }
00310 
00311     ConstIndexIterator operator++(int)
00312     {
00313       ConstIndexIterator tmp = *this;
00314       ++(*this);
00315       return tmp;
00316     }
00317 
00318     bool operator==(const ConstIndexIterator & iter) const
00319     {
00320       return m_Index == iter.m_Index && m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
00321     }
00322 
00323     bool operator!=(const ConstIndexIterator & iter) const
00324     {
00325       return !( *this == iter );
00326     }
00327 
00328     void GoToBegin()
00329     {
00330       m_Iterator = m_Begin;
00331       m_Index.Fill(0);
00332       NextValidLine();
00333     }
00334 
00335     bool IsAtEnd() const
00336     {
00337       return m_Iterator == m_End;
00338     }
00339 
00340   private:
00341 
00342     typedef typename std::deque< LineType >            LineContainerType;
00343     typedef typename LineContainerType::const_iterator InternalIteratorType;
00344     void NextValidLine()
00345     {
00346       // search for the next valid position
00347       while( m_Iterator != m_End && m_Iterator->GetLength() == 0 )
00348         {
00349         ++m_Iterator;
00350         }
00351       if( m_Iterator != m_End )
00352         {
00353         m_Index = m_Iterator->GetIndex();
00354         }
00355     }
00356 
00357     InternalIteratorType m_Iterator;
00358     InternalIteratorType m_Begin;
00359     InternalIteratorType m_End;
00360     IndexType            m_Index;
00361   };
00362 
00363 protected:
00364   LabelObject();
00365   void PrintSelf(std::ostream & os, Indent indent) const;
00366 
00367 private:
00368   LabelObject(const Self &);    //purposely not implemented
00369   void operator=(const Self &); //purposely not implemented
00370 
00371   typedef typename std::deque< LineType >    LineContainerType;
00372 
00373   LineContainerType m_LineContainer;
00374   LabelType         m_Label;
00375 };
00376 } // end namespace itk
00377 
00378 #ifndef ITK_MANUAL_INSTANTIATION
00379 #include "itkLabelObject.hxx"
00380 #endif
00381 
00382 #endif
00383