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  * https://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);
77  using Self = LabelMap;
82 
84  itkNewMacro(Self);
85 
87  itkTypeMacro(LabelMap, ImageBase);
88 
89  using LabelObjectType = TLabelObject;
90 
92 
93  using typename Superclass::SizeValueType;
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 
111  using typename Superclass::IndexType;
112 
114  using typename Superclass::OffsetType;
115 
117  using typename Superclass::SizeType;
118 
120  using typename Superclass::DirectionType;
121 
124  using typename Superclass::RegionType;
125 
128  using typename Superclass::SpacingType;
129 
132  using typename Superclass::PointType;
133 
135  using typename Superclass::OffsetValueType;
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;
165  bool
166  HasLabel(const LabelType label) const;
167 
175  GetNthLabelObject(const SizeValueType & pos);
176  const LabelObjectType *
177  GetNthLabelObject(const SizeValueType & pos) const;
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);
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  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
380 
381  void
383  {
384  m_Iterator = m_Begin;
385  }
386 
387  bool
388  IsAtEnd() const
389  {
390  return m_Iterator == m_End;
391  }
392 
393  private:
394  using InternalIteratorType = typename std::map<LabelType, LabelObjectPointerType>::const_iterator;
398  };
399 
405  class Iterator
406  {
407  public:
408  Iterator() = default;
409 
411  {
412  m_Begin = lm->m_LabelObjectContainer.begin();
413  m_End = lm->m_LabelObjectContainer.end();
414  m_Iterator = m_Begin;
415  }
416 
417  Iterator(const Iterator & iter)
418  {
419  m_Iterator = iter.m_Iterator;
420  m_Begin = iter.m_Begin;
421  m_End = iter.m_End;
422  }
423 
424  Iterator &
425  operator=(const Iterator & iter)
426  {
427  m_Iterator = iter.m_Iterator;
428  m_Begin = iter.m_Begin;
429  m_End = iter.m_End;
430  return *this;
431  }
432 
435  {
436  return m_Iterator->second;
437  }
438 
439  const LabelType &
440  GetLabel() const
441  {
442  return m_Iterator->first;
443  }
444 
445  Iterator
447  {
448  Iterator tmp = *this;
449  ++(*this);
450  return tmp;
451  }
452 
453  Iterator &
455  {
456  ++m_Iterator;
457  return *this;
458  }
459 
460  bool
461  operator==(const Iterator & iter) const
462  {
463  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
464  }
465 
466  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
467 
468  void
470  {
471  m_Iterator = m_Begin;
472  }
473 
474  bool
475  IsAtEnd() const
476  {
477  return m_Iterator == m_End;
478  }
479 
480  private:
481  using InternalIteratorType = typename std::map<LabelType, LabelObjectPointerType>::iterator;
485 
486  friend class LabelMap;
487  };
488 
489 protected:
490  LabelMap();
491  ~LabelMap() override = default;
492  void
493  PrintSelf(std::ostream & os, Indent indent) const override;
494  void
495  Graft(const DataObject * data) override;
496  using Superclass::Graft;
497 
498 private:
500  using LabelObjectContainerType = std::map<LabelType, LabelObjectPointerType>;
501  using LabelObjectContainerIterator = typename LabelObjectContainerType::iterator;
502  using LabelObjectContainerConstIterator = typename LabelObjectContainerType::const_iterator;
503 
506 
507  void
508  AddPixel(const LabelObjectContainerIterator & it, const IndexType & idx, const LabelType & label);
509 
510  void
511  RemovePixel(const LabelObjectContainerIterator & it, const IndexType & idx, bool iEmitModifiedEvent);
512 };
513 } // end namespace itk
514 
515 #ifndef ITK_MANUAL_INSTANTIATION
516 # include "itkLabelMap.hxx"
517 #endif
518 
519 #endif
itk::LabelMap::Iterator::IsAtEnd
bool IsAtEnd() const
Definition: itkLabelMap.h:475
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:92
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::Index< VImageDimension >
itk::LabelMap::Iterator::Iterator
Iterator(Self *lm)
Definition: itkLabelMap.h:410
itk::GTest::TypedefsAndConstructors::Dimension2::DirectionType
ImageBaseType::DirectionType DirectionType
Definition: itkGTestTypedefsAndConstructors.h:52
itk::LabelMap::Iterator::operator++
Iterator operator++(int)
Definition: itkLabelMap.h:446
itk::LabelMap::ConstIterator::operator==
bool operator==(const ConstIterator &iter) const
Definition: itkLabelMap.h:374
itk::LabelMap::ConstIterator::m_Begin
InternalIteratorType m_Begin
Definition: itkLabelMap.h:396
itk::LabelMap::ConstIterator::GetLabel
const LabelType & GetLabel() const
Definition: itkLabelMap.h:353
itkWeakPointer.h
itk::LabelMap::LabelObjectVectorType
std::vector< LabelObjectPointerType > LabelObjectVectorType
Definition: itkLabelMap.h:108
itk::LabelMap::Iterator::GoToBegin
void GoToBegin()
Definition: itkLabelMap.h:469
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:405
itk::ImageBase< TLabelObject::ImageDimension >::SizeValueType
typename SizeType::SizeValueType SizeValueType
Definition: itkImageBase.h:142
itk::GTest::TypedefsAndConstructors::Dimension2::PointType
ImageBaseType::PointType PointType
Definition: itkGTestTypedefsAndConstructors.h:51
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::LabelMap::ConstIterator::m_End
InternalIteratorType m_End
Definition: itkLabelMap.h:397
itk::LabelMap::Iterator::m_Begin
InternalIteratorType m_Begin
Definition: itkLabelMap.h:483
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::LabelMap::LabelObjectContainerConstIterator
typename LabelObjectContainerType::const_iterator LabelObjectContainerConstIterator
Definition: itkLabelMap.h:502
itk::LabelMap::m_BackgroundValue
LabelType m_BackgroundValue
Definition: itkLabelMap.h:505
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::LabelObjectContainerIterator
typename LabelObjectContainerType::iterator LabelObjectContainerIterator
Definition: itkLabelMap.h:501
itk::LabelMap::LengthType
SizeValueType LengthType
Definition: itkLabelMap.h:94
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::LabelMap::Iterator::m_End
InternalIteratorType m_End
Definition: itkLabelMap.h:484
itk::LabelMap::ConstIterator::ConstIterator
ConstIterator(const ConstIterator &iter)
Definition: itkLabelMap.h:330
itk::LabelMap::ConstIterator::IsAtEnd
bool IsAtEnd() const
Definition: itkLabelMap.h:388
itk::LabelMap::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkLabelMap.h:367
itk::LabelMap::LabelObjectContainerType
std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType
Definition: itkLabelMap.h:500
itk::LabelMap::ConstIterator::InternalIteratorType
typename std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType
Definition: itkLabelMap.h:394
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:440
itk::LabelMap::Iterator::operator=
Iterator & operator=(const Iterator &iter)
Definition: itkLabelMap.h:425
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::OffsetValueType
long OffsetValueType
Definition: itkIntTypes.h:94
itk::LabelMap::Iterator::m_Iterator
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:482
itk::LabelMap::Iterator::InternalIteratorType
typename std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType
Definition: itkLabelMap.h:481
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:417
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::LabelMap::m_LabelObjectContainer
LabelObjectContainerType m_LabelObjectContainer
Definition: itkLabelMap.h:504
itk::LabelMap::ConstIterator::m_Iterator
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:395
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
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:461
itk::LabelMap::ConstIterator::GoToBegin
void GoToBegin()
Definition: itkLabelMap.h:382
itk::LabelMap::Iterator::operator++
Iterator & operator++()
Definition: itkLabelMap.h:454
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::LabelMap::Iterator::GetLabelObject
LabelObjectType * GetLabelObject()
Definition: itkLabelMap.h:434
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293
itkImageBase.h