ITK  4.1.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 
00137   void SetRegions(const RegionType & region)
00138   {
00139     this->SetLargestPossibleRegion(region);
00140     this->SetBufferedRegion(region);
00141     this->SetRequestedRegion(region);
00142   }
00144 
00145   void SetRegions(const SizeType & size)
00146   {
00147     RegionType region; region.SetSize(size);
00148 
00149     this->SetLargestPossibleRegion(region);
00150     this->SetBufferedRegion(region);
00151     this->SetRequestedRegion(region);
00152   }
00153 
00156   virtual void Initialize();
00157 
00159   virtual void Allocate();
00160 
00161   virtual void Graft(const DataObject *data);
00162 
00168   LabelObjectType * GetLabelObject(const LabelType & label);
00169   const LabelObjectType * GetLabelObject(const LabelType & label) const;
00171 
00177   bool HasLabel(const LabelType label) const;
00178 
00185   LabelObjectType * GetNthLabelObject(const SizeValueType & pos);
00186   const LabelObjectType * GetNthLabelObject(const SizeValueType & pos) const;
00188 
00196   const LabelType & GetPixel(const IndexType & idx) const;
00197 
00207   void SetPixel(const IndexType & idx, const LabelType & label);
00208 
00216   void AddPixel(const IndexType & idx, const LabelType & label);
00217 
00222   void RemovePixel(const IndexType & idx, const LabelType & label);
00223 
00231   void SetLine(const IndexType & idx, const LengthType & length, const LabelType & label);
00232 
00238   LabelObjectType * GetLabelObject(const IndexType & idx) const;
00239 
00244   void AddLabelObject(LabelObjectType *labelObject);
00245 
00250   void PushLabelObject(LabelObjectType *labelObject);
00251 
00255   void RemoveLabelObject(LabelObjectType *labelObject);
00256 
00260   void RemoveLabel(const LabelType & label);
00261 
00265   void ClearLabels();
00266 
00270   typename Self::SizeValueType GetNumberOfLabelObjects() const;
00271 
00275   LabelVectorType GetLabels() const;
00276 
00280   LabelObjectVectorType GetLabelObjects() const;
00281 
00285   itkGetConstMacro(BackgroundValue, LabelType);
00286   itkSetMacro(BackgroundValue, LabelType);
00288 
00293   void PrintLabelObjects(std::ostream & os) const;
00294 
00295   void PrintLabelObjects() const
00296   {
00297     this->PrintLabelObjects(std::cerr);
00298   }
00299 
00303   void Optimize();
00304 
00309   class ConstIterator
00310   {
00311   public:
00312 
00313     ConstIterator() {}
00314 
00315     ConstIterator(const Self *lm)
00316     {
00317       m_Begin = lm->m_LabelObjectContainer.begin();
00318       m_End = lm->m_LabelObjectContainer.end();
00319       m_Iterator = m_Begin;
00320     }
00321 
00322     ConstIterator(const ConstIterator & iter)
00323     {
00324       m_Iterator = iter.m_Iterator;
00325       m_Begin = iter.m_Begin;
00326       m_End = iter.m_End;
00327     }
00328 
00329     ConstIterator & operator=(const ConstIterator & iter)
00330     {
00331       m_Iterator = iter.m_Iterator;
00332       m_Begin = iter.m_Begin;
00333       m_End = iter.m_End;
00334       return *this;
00335     }
00336 
00337     const LabelObjectType * GetLabelObject() const
00338     {
00339       return m_Iterator->second;
00340     }
00341 
00342     const LabelType & GetLabel() const
00343     {
00344       return m_Iterator->first;
00345     }
00346 
00347     ConstIterator operator++(int)
00348     {
00349       ConstIterator tmp = *this;
00350       ++(*this);
00351       return tmp;
00352     }
00353 
00354     ConstIterator & operator++()
00355     {
00356       ++m_Iterator;
00357       return *this;
00358     }
00359 
00360   bool operator==(const ConstIterator & iter) const
00361     {
00362     return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
00363     }
00364 
00365   bool operator!=(const ConstIterator & iter) const
00366     {
00367     return !( *this == iter );
00368     }
00369 
00370   void GoToBegin()
00371     {
00372       m_Iterator = m_Begin;
00373     }
00374 
00375     bool IsAtEnd() const
00376     {
00377       return m_Iterator == m_End;
00378     }
00379 
00380   private:
00381     typedef typename std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType;
00382     InternalIteratorType m_Iterator;
00383     InternalIteratorType m_Begin;
00384     InternalIteratorType m_End;
00385   };
00386 
00391   class Iterator
00392   {
00393   public:
00394 
00395     Iterator() {}
00396 
00397     Iterator(Self *lm)
00398     {
00399       m_Begin = lm->m_LabelObjectContainer.begin();
00400       m_End = lm->m_LabelObjectContainer.end();
00401       m_Iterator = m_Begin;
00402     }
00403 
00404     Iterator(const Iterator & iter)
00405     {
00406       m_Iterator = iter.m_Iterator;
00407       m_Begin = iter.m_Begin;
00408       m_End = iter.m_End;
00409     }
00410 
00411     Iterator & operator=(const Iterator & iter)
00412     {
00413       m_Iterator = iter.m_Iterator;
00414       m_Begin = iter.m_Begin;
00415       m_End = iter.m_End;
00416       return *this;
00417     }
00418 
00419     LabelObjectType * GetLabelObject()
00420     {
00421       return m_Iterator->second;
00422     }
00423 
00424     const LabelType & GetLabel() const
00425     {
00426       return m_Iterator->first;
00427     }
00428 
00429     Iterator operator++(int)
00430     {
00431       Iterator tmp = *this;
00432       ++(*this);
00433       return tmp;
00434     }
00435 
00436     Iterator & operator++()
00437     {
00438       ++m_Iterator;
00439       return *this;
00440     }
00441 
00442   bool operator==(const Iterator & iter) const
00443     {
00444     return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
00445     }
00446 
00447   bool operator!=(const Iterator & iter) const
00448     {
00449     return !( *this == iter );
00450     }
00451 
00452   void GoToBegin()
00453     {
00454       m_Iterator = m_Begin;
00455     }
00456 
00457     bool IsAtEnd() const
00458     {
00459       return m_Iterator == m_End;
00460     }
00461 
00462   private:
00463     typedef typename std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType;
00464     InternalIteratorType m_Iterator;
00465     InternalIteratorType m_Begin;
00466     InternalIteratorType m_End;
00467 
00468     friend class LabelMap;
00469   };
00470 
00471 protected:
00472   LabelMap();
00473   virtual ~LabelMap() {}
00474   void PrintSelf(std::ostream & os, Indent indent) const;
00475 
00476 private:
00477   LabelMap(const Self &);       //purposely not implemented
00478   void operator=(const Self &); //purposely not implemented
00479 
00481   typedef std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType;
00482   typedef typename LabelObjectContainerType::iterator   LabelObjectContainerIterator;
00483   typedef typename LabelObjectContainerType::const_iterator
00484                                                         LabelObjectContainerConstIterator;
00485 
00486   LabelObjectContainerType m_LabelObjectContainer;
00487   LabelType                m_BackgroundValue;
00488 
00489   void AddPixel( const LabelObjectContainerIterator& it,
00490                  const IndexType& idx,
00491                  const LabelType& iLabel );
00492 
00493   void RemovePixel( const LabelObjectContainerIterator& it,
00494                     const IndexType& idx,
00495                     bool iEmitModifiedEvent );
00496 };
00497 } // end namespace itk
00498 
00499 #ifndef ITK_MANUAL_INSTANTIATION
00500 #include "itkLabelMap.hxx"
00501 #endif
00502 
00503 #endif
00504