ITK  4.8.0
Insight Segmentation and Registration Toolkit
itkLabelMap.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkLabelMap_h
19 #define itkLabelMap_h
20 
21 #include "itkImageBase.h"
22 #include "itkWeakPointer.h"
23 #include <map>
24 
25 namespace itk
26 {
69 template< typename TLabelObject >
70 class LabelMap:public ImageBase< TLabelObject::ImageDimension >
71 {
72 public:
74  typedef LabelMap Self;
79 
81  itkNewMacro(Self);
82 
84  itkTypeMacro(LabelMap, ImageBase);
85 
86  typedef TLabelObject LabelObjectType;
87 
88  typedef typename LabelObjectType::Pointer LabelObjectPointerType;
89 
92 
97  itkStaticConstMacro(ImageDimension, unsigned int, LabelObjectType::ImageDimension);
98 
100  typedef typename LabelObjectType::LabelType LabelType;
102 
104  typedef std::vector< LabelType > LabelVectorType;
105  typedef std::vector< LabelObjectPointerType > LabelObjectVectorType;
106 
109 
112 
114  typedef typename Superclass::SizeType SizeType;
115 
118 
122 
126 
130 
133 
136  virtual void Initialize() ITK_OVERRIDE;
137 
139  virtual void Allocate(bool initialize = false) ITK_OVERRIDE;
140 
141  virtual void Graft(const DataObject *data) ITK_OVERRIDE;
142 
148  LabelObjectType * GetLabelObject(const LabelType & label);
149  const LabelObjectType * GetLabelObject(const LabelType & label) const;
151 
157  bool HasLabel(const LabelType label) const;
158 
166  const LabelObjectType * GetNthLabelObject(const SizeValueType & pos) const;
168 
176  const LabelType & GetPixel(const IndexType & idx) const;
177 
187  void SetPixel(const IndexType & idx, const LabelType & label);
188 
196  void AddPixel(const IndexType & idx, const LabelType & label);
197 
202  void RemovePixel(const IndexType & idx, const LabelType & label);
203 
211  void SetLine(const IndexType & idx, const LengthType & length, const LabelType & label);
212 
218  LabelObjectType * GetLabelObject(const IndexType & idx) const;
219 
224  void AddLabelObject(LabelObjectType *labelObject);
225 
230  void PushLabelObject(LabelObjectType *labelObject);
231 
235  void RemoveLabelObject(LabelObjectType *labelObject);
236 
240  void RemoveLabel(const LabelType & label);
241 
245  void ClearLabels();
246 
250  typename Self::SizeValueType GetNumberOfLabelObjects() const;
251 
255  LabelVectorType GetLabels() const;
256 
261 
265  itkGetConstMacro(BackgroundValue, LabelType);
266  itkSetMacro(BackgroundValue, LabelType);
268 
273  void PrintLabelObjects(std::ostream & os) const;
274 
275  void PrintLabelObjects() const
276  {
277  this->PrintLabelObjects(std::cerr);
278  }
279 
283  void Optimize();
284 
290  {
291  public:
292 
294 
295  ConstIterator(const Self *lm)
296  {
297  m_Begin = lm->m_LabelObjectContainer.begin();
298  m_End = lm->m_LabelObjectContainer.end();
300  }
301 
303  {
304  m_Iterator = iter.m_Iterator;
305  m_Begin = iter.m_Begin;
306  m_End = iter.m_End;
307  }
308 
310  {
311  m_Iterator = iter.m_Iterator;
312  m_Begin = iter.m_Begin;
313  m_End = iter.m_End;
314  return *this;
315  }
316 
318  {
319  return m_Iterator->second;
320  }
321 
322  const LabelType & GetLabel() const
323  {
324  return m_Iterator->first;
325  }
326 
328  {
329  ConstIterator tmp = *this;
330  ++(*this);
331  return tmp;
332  }
333 
335  {
336  ++m_Iterator;
337  return *this;
338  }
339 
340  bool operator==(const ConstIterator & iter) const
341  {
342  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
343  }
344 
345  bool operator!=(const ConstIterator & iter) const
346  {
347  return !( *this == iter );
348  }
349 
350  void GoToBegin()
351  {
353  }
354 
355  bool IsAtEnd() const
356  {
357  return m_Iterator == m_End;
358  }
359 
360  private:
361  typedef typename std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType;
365  };
366 
371  class Iterator
372  {
373  public:
374 
375  Iterator() {}
376 
378  {
379  m_Begin = lm->m_LabelObjectContainer.begin();
380  m_End = lm->m_LabelObjectContainer.end();
382  }
383 
384  Iterator(const Iterator & iter)
385  {
386  m_Iterator = iter.m_Iterator;
387  m_Begin = iter.m_Begin;
388  m_End = iter.m_End;
389  }
390 
391  Iterator & operator=(const Iterator & iter)
392  {
393  m_Iterator = iter.m_Iterator;
394  m_Begin = iter.m_Begin;
395  m_End = iter.m_End;
396  return *this;
397  }
398 
400  {
401  return m_Iterator->second;
402  }
403 
404  const LabelType & GetLabel() const
405  {
406  return m_Iterator->first;
407  }
408 
410  {
411  Iterator tmp = *this;
412  ++(*this);
413  return tmp;
414  }
415 
417  {
418  ++m_Iterator;
419  return *this;
420  }
421 
422  bool operator==(const Iterator & iter) const
423  {
424  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
425  }
426 
427  bool operator!=(const Iterator & iter) const
428  {
429  return !( *this == iter );
430  }
431 
432  void GoToBegin()
433  {
435  }
436 
437  bool IsAtEnd() const
438  {
439  return m_Iterator == m_End;
440  }
441 
442  private:
443  typedef typename std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType;
447 
448  friend class LabelMap;
449  };
450 
451 protected:
452  LabelMap();
453  virtual ~LabelMap() {}
454  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
455 
456 private:
457  LabelMap(const Self &); //purposely not implemented
458  void operator=(const Self &); //purposely not implemented
459 
461  typedef std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType;
462  typedef typename LabelObjectContainerType::iterator LabelObjectContainerIterator;
463  typedef typename LabelObjectContainerType::const_iterator
465 
468 
469  void AddPixel( const LabelObjectContainerIterator& it,
470  const IndexType& idx,
471  const LabelType& iLabel );
472 
474  const IndexType& idx,
475  bool iEmitModifiedEvent );
476 };
477 } // end namespace itk
478 
479 #ifndef ITK_MANUAL_INSTANTIATION
480 #include "itkLabelMap.hxx"
481 #endif
482 
483 #endif
Superclass::OffsetValueType OffsetValueType
Definition: itkLabelMap.h:132
void SetPixel(const IndexType &idx, const LabelType &label)
Set the pixel value at a given index in the image.
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:444
SizeValueType LengthType
Definition: itkLabelMap.h:91
Superclass::SizeType SizeType
Definition: itkLabelMap.h:114
Superclass::OffsetType OffsetType
Definition: itkLabelMap.h:111
const LabelType & GetPixel(const IndexType &idx) const
bool IsAtEnd() const
Definition: itkLabelMap.h:437
TLabelObject LabelObjectType
Definition: itkLabelMap.h:84
ConstIterator(const Self *lm)
Definition: itkLabelMap.h:295
void RemoveLabel(const LabelType &label)
LabelObjectVectorType GetLabelObjects() const
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:362
Iterator(const Iterator &iter)
Definition: itkLabelMap.h:384
virtual ~LabelMap()
Definition: itkLabelMap.h:453
Superclass::DirectionType DirectionType
Definition: itkLabelMap.h:117
LabelObjectType * GetLabelObject()
Definition: itkLabelMap.h:399
LabelMap Self
Definition: itkLabelMap.h:74
SmartPointer< Self > Pointer
Definition: itkLabelMap.h:76
virtual void Initialize() override
LabelObjectContainerType::iterator LabelObjectContainerIterator
Definition: itkLabelMap.h:462
WeakPointer< const Self > ConstWeakPointer
Definition: itkLabelMap.h:78
bool operator==(const Iterator &iter) const
Definition: itkLabelMap.h:422
Iterator & operator++()
Definition: itkLabelMap.h:416
LabelType PixelType
Definition: itkLabelMap.h:101
LabelVectorType GetLabels() const
Implements a weak reference to an object.
static const unsigned int ImageDimension
Definition: itkLabelMap.h:97
LabelObjectContainerType m_LabelObjectContainer
Definition: itkLabelMap.h:466
void AddPixel(const IndexType &idx, const LabelType &label)
bool operator!=(const ConstIterator &iter) const
Definition: itkLabelMap.h:345
const LabelType & GetLabel() const
Definition: itkLabelMap.h:404
Point< PointValueType, VImageDimension > PointType
Definition: itkImageBase.h:159
SmartPointer< const Self > ConstPointer
Definition: itkLabelMap.h:77
Matrix< SpacePrecisionType, VImageDimension, VImageDimension > DirectionType
Definition: itkImageBase.h:164
InternalIteratorType m_End
Definition: itkLabelMap.h:446
Self::SizeValueType GetNumberOfLabelObjects() const
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkLabelMap.h:309
bool HasLabel(const LabelType label) const
std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType
Definition: itkLabelMap.h:443
void PrintLabelObjects() const
Definition: itkLabelMap.h:275
LabelObjectType::Pointer LabelObjectPointerType
Definition: itkLabelMap.h:88
virtual void Allocate(bool initialize=false) override
Superclass::SizeValueType SizeValueType
Definition: itkLabelMap.h:90
LabelType m_BackgroundValue
Definition: itkLabelMap.h:467
void PushLabelObject(LabelObjectType *labelObject)
std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType
Definition: itkLabelMap.h:461
void operator=(const Self &)
Superclass::PointType PointType
Definition: itkLabelMap.h:129
bool operator!=(const Iterator &iter) const
Definition: itkLabelMap.h:427
virtual void Graft(const DataObject *data) override
void PrintSelf(std::ostream &os, Indent indent) const override
const LabelType & GetLabel() const
Definition: itkLabelMap.h:322
const LabelObjectType * GetLabelObject() const
Definition: itkLabelMap.h:317
Vector< SpacingValueType, VImageDimension > SpacingType
Definition: itkImageBase.h:154
std::vector< LabelType > LabelVectorType
Definition: itkLabelMap.h:104
std::vector< LabelObjectPointerType > LabelObjectVectorType
Definition: itkLabelMap.h:105
LabelObjectType * GetLabelObject(const LabelType &label)
Iterator & operator=(const Iterator &iter)
Definition: itkLabelMap.h:391
std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType
Definition: itkLabelMap.h:361
ConstIterator operator++(int)
Definition: itkLabelMap.h:327
A forward iterator over the LabelObjects of a LabelMap.
Definition: itkLabelMap.h:289
InternalIteratorType m_Begin
Definition: itkLabelMap.h:445
void AddLabelObject(LabelObjectType *labelObject)
void SetLine(const IndexType &idx, const LengthType &length, const LabelType &label)
Superclass::RegionType RegionType
Definition: itkLabelMap.h:121
Iterator operator++(int)
Definition: itkLabelMap.h:409
OffsetType::OffsetValueType OffsetValueType
Definition: itkImageBase.h:140
LabelObjectType * GetNthLabelObject(const SizeValueType &pos)
Superclass::SpacingType SpacingType
Definition: itkLabelMap.h:125
void RemoveLabelObject(LabelObjectType *labelObject)
Base class for templated image classes.
Definition: itkImageBase.h:112
LabelObjectContainerType::const_iterator LabelObjectContainerConstIterator
Definition: itkLabelMap.h:464
LabelObjectType::LabelType LabelType
Definition: itkLabelMap.h:100
Superclass::IndexType IndexType
Definition: itkLabelMap.h:108
Control indentation during Print() invocation.
Definition: itkIndent.h:49
InternalIteratorType m_Begin
Definition: itkLabelMap.h:363
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:70
ConstIterator & operator++()
Definition: itkLabelMap.h:334
ImageRegion< VImageDimension > RegionType
Definition: itkImageBase.h:147
InternalIteratorType m_End
Definition: itkLabelMap.h:364
void RemovePixel(const IndexType &idx, const LabelType &label)
A forward iterator over the LabelObjects of a LabelMap.
Definition: itkLabelMap.h:371
Base class for all data objects in ITK.
bool operator==(const ConstIterator &iter) const
Definition: itkLabelMap.h:340
ConstIterator(const ConstIterator &iter)
Definition: itkLabelMap.h:302
ImageBase< TLabelObject::ImageDimension > Superclass
Definition: itkLabelMap.h:75
void ClearLabels()