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