ITK  5.4.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  itkOverrideGetNameOfClassMacro(LabelMap);
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
274  {
275  return static_cast<SizeValueType>(m_LabelObjectContainer.size());
276  }
277 
281  LabelVectorType
282  GetLabels() const;
283 
287  LabelObjectVectorType
288  GetLabelObjects() const;
289 
293  itkGetConstMacro(BackgroundValue, LabelType);
294  itkSetMacro(BackgroundValue, LabelType);
301  void
302  PrintLabelObjects(std::ostream & os) const;
303 
304  void
306  {
307  this->PrintLabelObjects(std::cerr);
308  }
309 
313  void
314  Optimize();
315 
322  {
323  public:
324  ConstIterator() = default;
325 
326  ConstIterator(const Self * lm)
327  {
328  m_Begin = lm->m_LabelObjectContainer.begin();
329  m_End = lm->m_LabelObjectContainer.end();
330  m_Iterator = m_Begin;
331  }
332 
333  const LabelObjectType *
335  {
336  return m_Iterator->second;
337  }
338 
339  const LabelType &
340  GetLabel() const
341  {
342  return m_Iterator->first;
343  }
344 
347  {
348  ConstIterator tmp = *this;
349  ++(*this);
350  return tmp;
351  }
352 
353  ConstIterator &
355  {
356  ++m_Iterator;
357  return *this;
358  }
359 
360  bool
361  operator==(const ConstIterator & iter) const
362  {
363  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
364  }
365 
366  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
367 
368  void
370  {
371  m_Iterator = m_Begin;
372  }
373 
374  bool
375  IsAtEnd() const
376  {
377  return m_Iterator == m_End;
378  }
379 
380  private:
381  using InternalIteratorType = typename std::map<LabelType, LabelObjectPointerType>::const_iterator;
385  };
386 
392  class Iterator
393  {
394  public:
395  Iterator() = default;
396 
398  {
399  m_Begin = lm->m_LabelObjectContainer.begin();
400  m_End = lm->m_LabelObjectContainer.end();
401  m_Iterator = m_Begin;
402  }
403 
406  {
407  return m_Iterator->second;
408  }
409 
410  const LabelType &
411  GetLabel() const
412  {
413  return m_Iterator->first;
414  }
415 
416  Iterator
418  {
419  Iterator tmp = *this;
420  ++(*this);
421  return tmp;
422  }
423 
424  Iterator &
426  {
427  ++m_Iterator;
428  return *this;
429  }
430 
431  bool
432  operator==(const Iterator & iter) const
433  {
434  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
435  }
436 
437  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
438 
439  void
441  {
442  m_Iterator = m_Begin;
443  }
444 
445  bool
446  IsAtEnd() const
447  {
448  return m_Iterator == m_End;
449  }
450 
451  private:
452  using InternalIteratorType = typename std::map<LabelType, LabelObjectPointerType>::iterator;
456 
457  friend class LabelMap;
458  };
459 
460 protected:
461  LabelMap() = default;
462  ~LabelMap() override = default;
463  void
464  PrintSelf(std::ostream & os, Indent indent) const override;
465  void
466  Graft(const DataObject * data) override;
467  using Superclass::Graft;
468 
469 private:
471  using LabelObjectContainerType = std::map<LabelType, LabelObjectPointerType>;
472  using LabelObjectContainerIterator = typename LabelObjectContainerType::iterator;
473  using LabelObjectContainerConstIterator = typename LabelObjectContainerType::const_iterator;
474 
475  LabelObjectContainerType m_LabelObjectContainer{};
476  LabelType m_BackgroundValue{};
477 
478  void
479  AddPixel(const LabelObjectContainerIterator & it, const IndexType & idx, const LabelType & label);
480 
481  void
482  RemovePixel(const LabelObjectContainerIterator & it, const IndexType & idx, bool iEmitModifiedEvent);
483 };
484 } // end namespace itk
485 
486 #ifndef ITK_MANUAL_INSTANTIATION
487 # include "itkLabelMap.hxx"
488 #endif
489 
490 #endif
itk::LabelMap::Iterator::IsAtEnd
bool IsAtEnd() const
Definition: itkLabelMap.h:446
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
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:321
itk::Index< VImageDimension >
itk::LabelMap::Iterator::Iterator
Iterator(Self *lm)
Definition: itkLabelMap.h:397
itk::GTest::TypedefsAndConstructors::Dimension2::DirectionType
ImageBaseType::DirectionType DirectionType
Definition: itkGTestTypedefsAndConstructors.h:52
itk::LabelMap::Iterator::operator++
Iterator operator++(int)
Definition: itkLabelMap.h:417
itk::LabelMap::ConstIterator::operator==
bool operator==(const ConstIterator &iter) const
Definition: itkLabelMap.h:361
itk::LabelMap::ConstIterator::m_Begin
InternalIteratorType m_Begin
Definition: itkLabelMap.h:383
itk::LabelMap::ConstIterator::GetLabel
const LabelType & GetLabel() const
Definition: itkLabelMap.h:340
itkWeakPointer.h
itk::LabelMap::LabelObjectVectorType
std::vector< LabelObjectPointerType > LabelObjectVectorType
Definition: itkLabelMap.h:108
itk::LabelMap::Iterator::GoToBegin
void GoToBegin()
Definition: itkLabelMap.h:440
itk::LabelMap::LabelType
typename LabelObjectType::LabelType LabelType
Definition: itkLabelMap.h:103
itk::ImageBase
Base class for templated image classes.
Definition: itkImageBase.h:114
itk::LabelMap::Iterator
A forward iterator over the LabelObjects of a LabelMap.
Definition: itkLabelMap.h:392
itk::ImageBase< TLabelObject::ImageDimension >::SizeValueType
typename SizeType::SizeValueType SizeValueType
Definition: itkImageBase.h:151
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:384
itk::LabelMap::Iterator::m_Begin
InternalIteratorType m_Begin
Definition: itkLabelMap.h:454
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:473
itk::LabelMap::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkLabelMap.h:346
itk::LabelMap
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:70
itk::LabelMap::LabelObjectContainerIterator
typename LabelObjectContainerType::iterator LabelObjectContainerIterator
Definition: itkLabelMap.h:472
itk::LabelMap::LengthType
SizeValueType LengthType
Definition: itkLabelMap.h:94
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::LabelMap::GetNumberOfLabelObjects
Self::SizeValueType GetNumberOfLabelObjects() const
Definition: itkLabelMap.h:273
itk::LabelMap::Iterator::m_End
InternalIteratorType m_End
Definition: itkLabelMap.h:455
itk::LabelMap::ConstIterator::IsAtEnd
bool IsAtEnd() const
Definition: itkLabelMap.h:375
itk::LabelMap::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkLabelMap.h:354
itk::LabelMap::LabelObjectContainerType
std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType
Definition: itkLabelMap.h:471
itk::LabelMap::ConstIterator::InternalIteratorType
typename std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType
Definition: itkLabelMap.h:381
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::LabelMap::PrintLabelObjects
void PrintLabelObjects() const
Definition: itkLabelMap.h:305
itk::LabelMap::Iterator::GetLabel
const LabelType & GetLabel() const
Definition: itkLabelMap.h:411
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:453
itk::LabelMap::Iterator::InternalIteratorType
typename std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType
Definition: itkLabelMap.h:452
itk::LabelMap::ConstIterator::GetLabelObject
const LabelObjectType * GetLabelObject() const
Definition: itkLabelMap.h:334
itk::WeakPointer
Implements a weak reference to an object.
Definition: itkWeakPointer.h:44
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:326
itk::LabelMap::ConstIterator::m_Iterator
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:382
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::LabelMap::Iterator::operator==
bool operator==(const Iterator &iter) const
Definition: itkLabelMap.h:432
itk::LabelMap::ConstIterator::GoToBegin
void GoToBegin()
Definition: itkLabelMap.h:369
itk::LabelMap::Iterator::operator++
Iterator & operator++()
Definition: itkLabelMap.h:425
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::LabelMap::Iterator::GetLabelObject
LabelObjectType * GetLabelObject()
Definition: itkLabelMap.h:405
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293
itkImageBase.h