ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkLabelMap.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 __itkLabelMap_h
00019 #define __itkLabelMap_h
00020 
00021 #include "itkImageBase.h"
00022 #include "itkWeakPointer.h"
00023 #include <map>
00024 
00025 namespace itk
00026 {
00069 template< class TLabelObject >
00070 class ITK_EXPORT LabelMap:public ImageBase< ::itk::GetImageDimension< TLabelObject >::ImageDimension >
00071 {
00072 public:
00074   typedef LabelMap                                                              Self;
00075   typedef ImageBase< ::itk::GetImageDimension< TLabelObject >::ImageDimension > Superclass;
00076   typedef SmartPointer< Self >                                                  Pointer;
00077   typedef SmartPointer< const Self >                                            ConstPointer;
00078   typedef WeakPointer< const Self >                                             ConstWeakPointer;
00079 
00081   itkNewMacro(Self);
00082 
00084   itkTypeMacro(LabelMap, ImageBase);
00085 
00086   typedef TLabelObject LabelObjectType;
00087 
00088   typedef typename LabelObjectType::Pointer LabelObjectPointerType;
00089 
00090   typedef typename Superclass::SizeValueType SizeValueType;
00091   typedef SizeValueType                      LengthType;
00092 
00097   itkStaticConstMacro(ImageDimension, unsigned int, LabelObjectType::ImageDimension);
00098 
00100   typedef typename LabelObjectType::LabelType LabelType;
00101   typedef LabelType                           PixelType;
00102 
00104   typedef std::vector< LabelType >              LabelVectorType;
00105   typedef std::vector< LabelObjectPointerType > LabelObjectVectorType;
00106 
00108   typedef typename Superclass::IndexType IndexType;
00109 
00111   typedef typename Superclass::OffsetType OffsetType;
00112 
00114   typedef typename Superclass::SizeType SizeType;
00115 
00117   typedef typename Superclass::DirectionType DirectionType;
00118 
00121   typedef typename Superclass::RegionType RegionType;
00122 
00125   typedef typename Superclass::SpacingType SpacingType;
00126 
00129   typedef typename Superclass::PointType PointType;
00130 
00132   typedef typename Superclass::OffsetValueType OffsetValueType;
00133 
00136   virtual void Initialize();
00137 
00139   virtual void Allocate();
00140 
00141   virtual void Graft(const DataObject *data);
00142 
00148   LabelObjectType * GetLabelObject(const LabelType & label);
00149   const LabelObjectType * GetLabelObject(const LabelType & label) const;
00151 
00157   bool HasLabel(const LabelType label) const;
00158 
00165   LabelObjectType * GetNthLabelObject(const SizeValueType & pos);
00166   const LabelObjectType * GetNthLabelObject(const SizeValueType & pos) const;
00168 
00176   const LabelType & GetPixel(const IndexType & idx) const;
00177 
00187   void SetPixel(const IndexType & idx, const LabelType & label);
00188 
00196   void AddPixel(const IndexType & idx, const LabelType & label);
00197 
00202   void RemovePixel(const IndexType & idx, const LabelType & label);
00203 
00211   void SetLine(const IndexType & idx, const LengthType & length, const LabelType & label);
00212 
00218   LabelObjectType * GetLabelObject(const IndexType & idx) const;
00219 
00224   void AddLabelObject(LabelObjectType *labelObject);
00225 
00230   void PushLabelObject(LabelObjectType *labelObject);
00231 
00235   void RemoveLabelObject(LabelObjectType *labelObject);
00236 
00240   void RemoveLabel(const LabelType & label);
00241 
00245   void ClearLabels();
00246 
00250   typename Self::SizeValueType GetNumberOfLabelObjects() const;
00251 
00255   LabelVectorType GetLabels() const;
00256 
00260   LabelObjectVectorType GetLabelObjects() const;
00261 
00265   itkGetConstMacro(BackgroundValue, LabelType);
00266   itkSetMacro(BackgroundValue, LabelType);
00268 
00273   void PrintLabelObjects(std::ostream & os) const;
00274 
00275   void PrintLabelObjects() const
00276   {
00277     this->PrintLabelObjects(std::cerr);
00278   }
00279 
00283   void Optimize();
00284 
00289   class ConstIterator
00290   {
00291   public:
00292 
00293     ConstIterator() {}
00294 
00295     ConstIterator(const Self *lm)
00296     {
00297       m_Begin = lm->m_LabelObjectContainer.begin();
00298       m_End = lm->m_LabelObjectContainer.end();
00299       m_Iterator = m_Begin;
00300     }
00301 
00302     ConstIterator(const ConstIterator & iter)
00303     {
00304       m_Iterator = iter.m_Iterator;
00305       m_Begin = iter.m_Begin;
00306       m_End = iter.m_End;
00307     }
00308 
00309     ConstIterator & operator=(const ConstIterator & iter)
00310     {
00311       m_Iterator = iter.m_Iterator;
00312       m_Begin = iter.m_Begin;
00313       m_End = iter.m_End;
00314       return *this;
00315     }
00316 
00317     const LabelObjectType * GetLabelObject() const
00318     {
00319       return m_Iterator->second;
00320     }
00321 
00322     const LabelType & GetLabel() const
00323     {
00324       return m_Iterator->first;
00325     }
00326 
00327     ConstIterator operator++(int)
00328     {
00329       ConstIterator tmp = *this;
00330       ++(*this);
00331       return tmp;
00332     }
00333 
00334     ConstIterator & operator++()
00335     {
00336       ++m_Iterator;
00337       return *this;
00338     }
00339 
00340   bool operator==(const ConstIterator & iter) const
00341     {
00342     return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
00343     }
00344 
00345   bool operator!=(const ConstIterator & iter) const
00346     {
00347     return !( *this == iter );
00348     }
00349 
00350   void GoToBegin()
00351     {
00352       m_Iterator = m_Begin;
00353     }
00354 
00355     bool IsAtEnd() const
00356     {
00357       return m_Iterator == m_End;
00358     }
00359 
00360   private:
00361     typedef typename std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType;
00362     InternalIteratorType m_Iterator;
00363     InternalIteratorType m_Begin;
00364     InternalIteratorType m_End;
00365   };
00366 
00371   class Iterator
00372   {
00373   public:
00374 
00375     Iterator() {}
00376 
00377     Iterator(Self *lm)
00378     {
00379       m_Begin = lm->m_LabelObjectContainer.begin();
00380       m_End = lm->m_LabelObjectContainer.end();
00381       m_Iterator = m_Begin;
00382     }
00383 
00384     Iterator(const Iterator & iter)
00385     {
00386       m_Iterator = iter.m_Iterator;
00387       m_Begin = iter.m_Begin;
00388       m_End = iter.m_End;
00389     }
00390 
00391     Iterator & operator=(const Iterator & iter)
00392     {
00393       m_Iterator = iter.m_Iterator;
00394       m_Begin = iter.m_Begin;
00395       m_End = iter.m_End;
00396       return *this;
00397     }
00398 
00399     LabelObjectType * GetLabelObject()
00400     {
00401       return m_Iterator->second;
00402     }
00403 
00404     const LabelType & GetLabel() const
00405     {
00406       return m_Iterator->first;
00407     }
00408 
00409     Iterator operator++(int)
00410     {
00411       Iterator tmp = *this;
00412       ++(*this);
00413       return tmp;
00414     }
00415 
00416     Iterator & operator++()
00417     {
00418       ++m_Iterator;
00419       return *this;
00420     }
00421 
00422   bool operator==(const Iterator & iter) const
00423     {
00424     return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
00425     }
00426 
00427   bool operator!=(const Iterator & iter) const
00428     {
00429     return !( *this == iter );
00430     }
00431 
00432   void GoToBegin()
00433     {
00434       m_Iterator = m_Begin;
00435     }
00436 
00437     bool IsAtEnd() const
00438     {
00439       return m_Iterator == m_End;
00440     }
00441 
00442   private:
00443     typedef typename std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType;
00444     InternalIteratorType m_Iterator;
00445     InternalIteratorType m_Begin;
00446     InternalIteratorType m_End;
00447 
00448     friend class LabelMap;
00449   };
00450 
00451 protected:
00452   LabelMap();
00453   virtual ~LabelMap() {}
00454   void PrintSelf(std::ostream & os, Indent indent) const;
00455 
00456 private:
00457   LabelMap(const Self &);       //purposely not implemented
00458   void operator=(const Self &); //purposely not implemented
00459 
00461   typedef std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType;
00462   typedef typename LabelObjectContainerType::iterator   LabelObjectContainerIterator;
00463   typedef typename LabelObjectContainerType::const_iterator
00464                                                         LabelObjectContainerConstIterator;
00465 
00466   LabelObjectContainerType m_LabelObjectContainer;
00467   LabelType                m_BackgroundValue;
00468 
00469   void AddPixel( const LabelObjectContainerIterator& it,
00470                  const IndexType& idx,
00471                  const LabelType& iLabel );
00472 
00473   void RemovePixel( const LabelObjectContainerIterator& it,
00474                     const IndexType& idx,
00475                     bool iEmitModifiedEvent );
00476 };
00477 } // end namespace itk
00478 
00479 #ifndef ITK_MANUAL_INSTANTIATION
00480 #include "itkLabelMap.hxx"
00481 #endif
00482 
00483 #endif
00484