ITK  5.2.0
Insight Toolkit
itkLabelMap.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 {
70 template <typename TLabelObject>
71 class ITK_TEMPLATE_EXPORT LabelMap : public ImageBase<TLabelObject::ImageDimension>
72 {
73 public:
74  ITK_DISALLOW_COPY_AND_ASSIGN(LabelMap);
76 
78  using Self = LabelMap;
83 
85  itkNewMacro(Self);
86 
88  itkTypeMacro(LabelMap, ImageBase);
89 
90  using LabelObjectType = TLabelObject;
91 
92  using LabelObjectPointerType = typename LabelObjectType::Pointer;
93 
96 
101  static constexpr unsigned int ImageDimension = LabelObjectType::ImageDimension;
102 
104  using LabelType = typename LabelObjectType::LabelType;
106 
108  using LabelVectorType = std::vector<LabelType>;
109  using LabelObjectVectorType = std::vector<LabelObjectPointerType>;
110 
113 
115  using OffsetType = typename Superclass::OffsetType;
116 
118  using SizeType = typename Superclass::SizeType;
119 
122 
126 
129  using SpacingType = typename Superclass::SpacingType;
130 
134 
137 
140  void
141  Initialize() override;
142 
144  void
145  Allocate(bool initialize = false) override;
146 
147  virtual void
148  Graft(const Self * imgData);
149 
156  GetLabelObject(const LabelType & label);
157  const LabelObjectType *
158  GetLabelObject(const LabelType & label) const;
160 
166  bool
167  HasLabel(const LabelType label) const;
168 
176  GetNthLabelObject(const SizeValueType & pos);
177  const LabelObjectType *
178  GetNthLabelObject(const SizeValueType & pos) const;
180 
188  const LabelType &
189  GetPixel(const IndexType & idx) const;
190 
200  void
201  SetPixel(const IndexType & idx, const LabelType & label);
202 
210  void
211  AddPixel(const IndexType & idx, const LabelType & label);
212 
217  void
218  RemovePixel(const IndexType & idx, const LabelType & label);
219 
227  void
228  SetLine(const IndexType & idx, const LengthType & length, const LabelType & label);
229 
236  GetLabelObject(const IndexType & idx) const;
237 
242  void
243  AddLabelObject(LabelObjectType * labelObject);
244 
249  void
250  PushLabelObject(LabelObjectType * labelObject);
251 
255  void
256  RemoveLabelObject(LabelObjectType * labelObject);
257 
261  void
262  RemoveLabel(const LabelType & label);
263 
267  void
268  ClearLabels();
269 
273  typename Self::SizeValueType
274  GetNumberOfLabelObjects() const;
275 
280  GetLabels() const;
281 
286  GetLabelObjects() const;
287 
291  itkGetConstMacro(BackgroundValue, LabelType);
292  itkSetMacro(BackgroundValue, LabelType);
294 
299  void
300  PrintLabelObjects(std::ostream & os) const;
301 
302  void
304  {
305  this->PrintLabelObjects(std::cerr);
306  }
307 
311  void
312  Optimize();
313 
320  {
321  public:
322  ConstIterator() = default;
323 
324  ConstIterator(const Self * lm)
325  {
326  m_Begin = lm->m_LabelObjectContainer.begin();
327  m_End = lm->m_LabelObjectContainer.end();
328  m_Iterator = m_Begin;
329  }
330 
332  {
333  m_Iterator = iter.m_Iterator;
334  m_Begin = iter.m_Begin;
335  m_End = iter.m_End;
336  }
337 
338  ConstIterator &
339  operator=(const ConstIterator & iter)
340  {
341  m_Iterator = iter.m_Iterator;
342  m_Begin = iter.m_Begin;
343  m_End = iter.m_End;
344  return *this;
345  }
346 
347  const LabelObjectType *
349  {
350  return m_Iterator->second;
351  }
352 
353  const LabelType &
354  GetLabel() const
355  {
356  return m_Iterator->first;
357  }
358 
361  {
362  ConstIterator tmp = *this;
363  ++(*this);
364  return tmp;
365  }
366 
367  ConstIterator &
369  {
370  ++m_Iterator;
371  return *this;
372  }
373 
374  bool
375  operator==(const ConstIterator & iter) const
376  {
377  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
378  }
379 
380  bool
381  operator!=(const ConstIterator & iter) const
382  {
383  return !(*this == iter);
384  }
385 
386  void
388  {
389  m_Iterator = m_Begin;
390  }
391 
392  bool
393  IsAtEnd() const
394  {
395  return m_Iterator == m_End;
396  }
397 
398  private:
399  using InternalIteratorType = typename std::map<LabelType, LabelObjectPointerType>::const_iterator;
403  };
404 
410  class Iterator
411  {
412  public:
413  Iterator() = default;
414 
416  {
417  m_Begin = lm->m_LabelObjectContainer.begin();
418  m_End = lm->m_LabelObjectContainer.end();
419  m_Iterator = m_Begin;
420  }
421 
422  Iterator(const Iterator & iter)
423  {
424  m_Iterator = iter.m_Iterator;
425  m_Begin = iter.m_Begin;
426  m_End = iter.m_End;
427  }
428 
429  Iterator &
430  operator=(const Iterator & iter)
431  {
432  m_Iterator = iter.m_Iterator;
433  m_Begin = iter.m_Begin;
434  m_End = iter.m_End;
435  return *this;
436  }
437 
440  {
441  return m_Iterator->second;
442  }
443 
444  const LabelType &
445  GetLabel() const
446  {
447  return m_Iterator->first;
448  }
449 
450  Iterator
452  {
453  Iterator tmp = *this;
454  ++(*this);
455  return tmp;
456  }
457 
458  Iterator &
460  {
461  ++m_Iterator;
462  return *this;
463  }
464 
465  bool
466  operator==(const Iterator & iter) const
467  {
468  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
469  }
470 
471  bool
472  operator!=(const Iterator & iter) const
473  {
474  return !(*this == iter);
475  }
476 
477  void
479  {
480  m_Iterator = m_Begin;
481  }
482 
483  bool
484  IsAtEnd() const
485  {
486  return m_Iterator == m_End;
487  }
488 
489  private:
490  using InternalIteratorType = typename std::map<LabelType, LabelObjectPointerType>::iterator;
494 
495  friend class LabelMap;
496  };
497 
498 protected:
499  LabelMap();
500  ~LabelMap() override = default;
501  void
502  PrintSelf(std::ostream & os, Indent indent) const override;
503  void
504  Graft(const DataObject * data) override;
505  using Superclass::Graft;
506 
507 private:
509  using LabelObjectContainerType = std::map<LabelType, LabelObjectPointerType>;
510  using LabelObjectContainerIterator = typename LabelObjectContainerType::iterator;
511  using LabelObjectContainerConstIterator = typename LabelObjectContainerType::const_iterator;
512 
515 
516  void
517  AddPixel(const LabelObjectContainerIterator & it, const IndexType & idx, const LabelType & iLabel);
518 
519  void
520  RemovePixel(const LabelObjectContainerIterator & it, const IndexType & idx, bool iEmitModifiedEvent);
521 };
522 } // end namespace itk
523 
524 #ifndef ITK_MANUAL_INSTANTIATION
525 # include "itkLabelMap.hxx"
526 #endif
527 
528 #endif
itk::LabelMap::Iterator::IsAtEnd
bool IsAtEnd() const
Definition: itkLabelMap.h:484
itk::LabelMap::LabelObjectPointerType
typename LabelObjectType::Pointer LabelObjectPointerType
Definition: itkLabelMap.h:92
itk::LabelMap::ConstIterator
A forward iterator over the LabelObjects of a LabelMap.
Definition: itkLabelMap.h:319
itk::LabelMap::SizeValueType
typename Superclass::SizeValueType SizeValueType
Definition: itkLabelMap.h:94
itk::LabelMap::Iterator::Iterator
Iterator(Self *lm)
Definition: itkLabelMap.h:415
itk::GTest::TypedefsAndConstructors::Dimension2::DirectionType
ImageBaseType::DirectionType DirectionType
Definition: itkGTestTypedefsAndConstructors.h:52
itk::LabelMap::Iterator::operator++
Iterator operator++(int)
Definition: itkLabelMap.h:451
itk::LabelMap::ConstIterator::operator==
bool operator==(const ConstIterator &iter) const
Definition: itkLabelMap.h:375
itk::LabelMap::ConstIterator::operator!=
bool operator!=(const ConstIterator &iter) const
Definition: itkLabelMap.h:381
itk::LabelMap::ConstIterator::m_Begin
InternalIteratorType m_Begin
Definition: itkLabelMap.h:401
itk::LabelMap::ConstIterator::GetLabel
const LabelType & GetLabel() const
Definition: itkLabelMap.h:354
itkWeakPointer.h
itk::LabelMap::Iterator::operator!=
bool operator!=(const Iterator &iter) const
Definition: itkLabelMap.h:472
itk::LabelMap::LabelObjectVectorType
std::vector< LabelObjectPointerType > LabelObjectVectorType
Definition: itkLabelMap.h:109
itk::LabelMap::Iterator::GoToBegin
void GoToBegin()
Definition: itkLabelMap.h:478
itk::LabelMap::LabelType
typename LabelObjectType::LabelType LabelType
Definition: itkLabelMap.h:104
itk::ImageBase
Base class for templated image classes.
Definition: itkImageBase.h:105
itk::LabelMap::Iterator
A forward iterator over the LabelObjects of a LabelMap.
Definition: itkLabelMap.h:410
itk::GTest::TypedefsAndConstructors::Dimension2::PointType
ImageBaseType::PointType PointType
Definition: itkGTestTypedefsAndConstructors.h:51
itk::LabelMap::OffsetType
typename Superclass::OffsetType OffsetType
Definition: itkLabelMap.h:115
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::LabelMap::ConstIterator::m_End
InternalIteratorType m_End
Definition: itkLabelMap.h:402
itk::LabelMap::Iterator::m_Begin
InternalIteratorType m_Begin
Definition: itkLabelMap.h:492
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::LabelMap::SpacingType
typename Superclass::SpacingType SpacingType
Definition: itkLabelMap.h:129
itk::LabelMap::LabelObjectContainerConstIterator
typename LabelObjectContainerType::const_iterator LabelObjectContainerConstIterator
Definition: itkLabelMap.h:511
itk::LabelMap::m_BackgroundValue
LabelType m_BackgroundValue
Definition: itkLabelMap.h:514
itk::LabelMap::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkLabelMap.h:360
itk::LabelMap
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:71
itk::LabelMap::RegionType
typename Superclass::RegionType RegionType
Definition: itkLabelMap.h:125
itk::LabelMap::LabelObjectContainerIterator
typename LabelObjectContainerType::iterator LabelObjectContainerIterator
Definition: itkLabelMap.h:510
itk::LabelMap::LengthType
SizeValueType LengthType
Definition: itkLabelMap.h:95
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::LabelMap::DirectionType
typename Superclass::DirectionType DirectionType
Definition: itkLabelMap.h:121
itk::LabelMap::Iterator::m_End
InternalIteratorType m_End
Definition: itkLabelMap.h:493
itk::LabelMap::ConstIterator::ConstIterator
ConstIterator(const ConstIterator &iter)
Definition: itkLabelMap.h:331
itk::LabelMap::ConstIterator::IsAtEnd
bool IsAtEnd() const
Definition: itkLabelMap.h:393
itk::LabelMap::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkLabelMap.h:368
itk::LabelMap::LabelObjectContainerType
std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType
Definition: itkLabelMap.h:509
itk::LabelMap::ConstIterator::InternalIteratorType
typename std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType
Definition: itkLabelMap.h:399
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::LabelMap::PrintLabelObjects
void PrintLabelObjects() const
Definition: itkLabelMap.h:303
itk::LabelMap::Iterator::GetLabel
const LabelType & GetLabel() const
Definition: itkLabelMap.h:445
itk::LabelMap::Iterator::operator=
Iterator & operator=(const Iterator &iter)
Definition: itkLabelMap.h:430
itk::LabelMap::LabelVectorType
std::vector< LabelType > LabelVectorType
Definition: itkLabelMap.h:108
itk::LabelMap::LabelObjectType
TLabelObject LabelObjectType
Definition: itkLabelMap.h:90
itk::LabelMap::PixelType
LabelType PixelType
Definition: itkLabelMap.h:105
itk::LabelMap::PointType
typename Superclass::PointType PointType
Definition: itkLabelMap.h:133
itk::LabelMap::SizeType
typename Superclass::SizeType SizeType
Definition: itkLabelMap.h:118
itk::LabelMap::Iterator::m_Iterator
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:491
itk::LabelMap::Iterator::InternalIteratorType
typename std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType
Definition: itkLabelMap.h:490
itk::LabelMap::ConstIterator::GetLabelObject
const LabelObjectType * GetLabelObject() const
Definition: itkLabelMap.h:348
itk::WeakPointer
Implements a weak reference to an object.
Definition: itkWeakPointer.h:44
itk::LabelMap::Iterator::Iterator
Iterator(const Iterator &iter)
Definition: itkLabelMap.h:422
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::LabelMap::ConstIterator::ConstIterator
ConstIterator(const Self *lm)
Definition: itkLabelMap.h:324
itk::OffsetValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
itk::LabelMap::m_LabelObjectContainer
LabelObjectContainerType m_LabelObjectContainer
Definition: itkLabelMap.h:513
itk::LabelMap::ConstIterator::m_Iterator
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:400
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::LabelMap::ConstIterator::operator=
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkLabelMap.h:339
itk::LabelMap::Iterator::operator==
bool operator==(const Iterator &iter) const
Definition: itkLabelMap.h:466
itk::LabelMap::ConstIterator::GoToBegin
void GoToBegin()
Definition: itkLabelMap.h:387
itk::LabelMap::Iterator::operator++
Iterator & operator++()
Definition: itkLabelMap.h:459
itk::LabelMap::OffsetValueType
typename Superclass::OffsetValueType OffsetValueType
Definition: itkLabelMap.h:136
itk::LabelMap::IndexType
typename Superclass::IndexType IndexType
Definition: itkLabelMap.h:112
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::LabelMap::Iterator::GetLabelObject
LabelObjectType * GetLabelObject()
Definition: itkLabelMap.h:439
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293
itkImageBase.h