ITK  5.0.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 ITK_TEMPLATE_EXPORT LabelMap:public ImageBase< TLabelObject::ImageDimension >
71 {
72 public:
73  ITK_DISALLOW_COPY_AND_ASSIGN(LabelMap);
74 
76  using Self = LabelMap;
81 
83  itkNewMacro(Self);
84 
86  itkTypeMacro(LabelMap, ImageBase);
87 
88  using LabelObjectType = TLabelObject;
89 
90  using LabelObjectPointerType = typename LabelObjectType::Pointer;
91 
94 
99  static constexpr unsigned int ImageDimension = LabelObjectType::ImageDimension;
100 
102  using LabelType = typename LabelObjectType::LabelType;
104 
106  using LabelVectorType = std::vector< LabelType >;
107  using LabelObjectVectorType = std::vector< LabelObjectPointerType >;
108 
111 
113  using OffsetType = typename Superclass::OffsetType;
114 
116  using SizeType = typename Superclass::SizeType;
117 
120 
124 
127  using SpacingType = typename Superclass::SpacingType;
128 
132 
135 
138  void Initialize() override;
139 
141  void Allocate(bool initialize = false) override;
142 
143  virtual void Graft(const Self *imgData);
144 
150  LabelObjectType * GetLabelObject(const LabelType & label);
151  const LabelObjectType * GetLabelObject(const LabelType & label) const;
153 
159  bool HasLabel(const LabelType label) const;
160 
167  LabelObjectType * GetNthLabelObject(const SizeValueType & pos);
168  const LabelObjectType * GetNthLabelObject(const SizeValueType & pos) const;
170 
178  const LabelType & GetPixel(const IndexType & idx) const;
179 
189  void SetPixel(const IndexType & idx, const LabelType & label);
190 
198  void AddPixel(const IndexType & idx, const LabelType & label);
199 
204  void RemovePixel(const IndexType & idx, const LabelType & label);
205 
213  void SetLine(const IndexType & idx, const LengthType & length, const LabelType & label);
214 
220  LabelObjectType * GetLabelObject(const IndexType & idx) const;
221 
226  void AddLabelObject(LabelObjectType *labelObject);
227 
232  void PushLabelObject(LabelObjectType *labelObject);
233 
237  void RemoveLabelObject(LabelObjectType *labelObject);
238 
242  void RemoveLabel(const LabelType & label);
243 
247  void ClearLabels();
248 
252  typename Self::SizeValueType GetNumberOfLabelObjects() const;
253 
257  LabelVectorType GetLabels() const;
258 
262  LabelObjectVectorType GetLabelObjects() const;
263 
267  itkGetConstMacro(BackgroundValue, LabelType);
268  itkSetMacro(BackgroundValue, LabelType);
270 
275  void PrintLabelObjects(std::ostream & os) const;
276 
277  void PrintLabelObjects() const
278  {
279  this->PrintLabelObjects(std::cerr);
280  }
281 
285  void Optimize();
286 
292  {
293  public:
294 
295  ConstIterator() = default;
296 
297  ConstIterator(const Self *lm)
298  {
299  m_Begin = lm->m_LabelObjectContainer.begin();
300  m_End = lm->m_LabelObjectContainer.end();
301  m_Iterator = m_Begin;
302  }
303 
305  {
306  m_Iterator = iter.m_Iterator;
307  m_Begin = iter.m_Begin;
308  m_End = iter.m_End;
309  }
310 
312  {
313  m_Iterator = iter.m_Iterator;
314  m_Begin = iter.m_Begin;
315  m_End = iter.m_End;
316  return *this;
317  }
318 
320  {
321  return m_Iterator->second;
322  }
323 
324  const LabelType & GetLabel() const
325  {
326  return m_Iterator->first;
327  }
328 
330  {
331  ConstIterator tmp = *this;
332  ++(*this);
333  return tmp;
334  }
335 
337  {
338  ++m_Iterator;
339  return *this;
340  }
341 
342  bool operator==(const ConstIterator & iter) const
343  {
344  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
345  }
346 
347  bool operator!=(const ConstIterator & iter) const
348  {
349  return !( *this == iter );
350  }
351 
352  void GoToBegin()
353  {
354  m_Iterator = m_Begin;
355  }
356 
357  bool IsAtEnd() const
358  {
359  return m_Iterator == m_End;
360  }
361 
362  private:
363  using InternalIteratorType = typename std::map< LabelType, LabelObjectPointerType >::const_iterator;
367  };
368 
373  class Iterator
374  {
375  public:
376 
377  Iterator() = default;
378 
380  {
381  m_Begin = lm->m_LabelObjectContainer.begin();
382  m_End = lm->m_LabelObjectContainer.end();
383  m_Iterator = m_Begin;
384  }
385 
386  Iterator(const Iterator & iter)
387  {
388  m_Iterator = iter.m_Iterator;
389  m_Begin = iter.m_Begin;
390  m_End = iter.m_End;
391  }
392 
393  Iterator & operator=(const Iterator & iter)
394  {
395  m_Iterator = iter.m_Iterator;
396  m_Begin = iter.m_Begin;
397  m_End = iter.m_End;
398  return *this;
399  }
400 
402  {
403  return m_Iterator->second;
404  }
405 
406  const LabelType & GetLabel() const
407  {
408  return m_Iterator->first;
409  }
410 
412  {
413  Iterator tmp = *this;
414  ++(*this);
415  return tmp;
416  }
417 
419  {
420  ++m_Iterator;
421  return *this;
422  }
423 
424  bool operator==(const Iterator & iter) const
425  {
426  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
427  }
428 
429  bool operator!=(const Iterator & iter) const
430  {
431  return !( *this == iter );
432  }
433 
434  void GoToBegin()
435  {
436  m_Iterator = m_Begin;
437  }
438 
439  bool IsAtEnd() const
440  {
441  return m_Iterator == m_End;
442  }
443 
444  private:
445  using InternalIteratorType = typename std::map< LabelType, LabelObjectPointerType >::iterator;
449 
450  friend class LabelMap;
451  };
452 
453 protected:
454  LabelMap();
455  ~LabelMap() override = default;
456  void PrintSelf(std::ostream & os, Indent indent) const override;
457  void Graft(const DataObject *data) override;
458  using Superclass::Graft;
459 
460 private:
462  using LabelObjectContainerType = std::map< LabelType, LabelObjectPointerType >;
463  using LabelObjectContainerIterator = typename LabelObjectContainerType::iterator;
464  using LabelObjectContainerConstIterator = typename LabelObjectContainerType::const_iterator;
465 
468 
469  void AddPixel( const LabelObjectContainerIterator& it,
470  const IndexType& idx,
471  const LabelType& iLabel );
472 
473  void RemovePixel( const LabelObjectContainerIterator& it,
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
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:446
bool IsAtEnd() const
Definition: itkLabelMap.h:439
std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType
Definition: itkLabelMap.h:462
ConstIterator(const Self *lm)
Definition: itkLabelMap.h:297
typename std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType
Definition: itkLabelMap.h:363
std::vector< LabelObjectPointerType > LabelObjectVectorType
Definition: itkLabelMap.h:107
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:364
Iterator(const Iterator &iter)
Definition: itkLabelMap.h:386
typename Superclass::DirectionType DirectionType
Definition: itkLabelMap.h:119
unsigned long SizeValueType
Definition: itkIntTypes.h:83
typename Superclass::SizeValueType SizeValueType
Definition: itkLabelMap.h:92
LabelObjectType * GetLabelObject()
Definition: itkLabelMap.h:401
typename Superclass::SpacingType SpacingType
Definition: itkLabelMap.h:127
typename Superclass::OffsetValueType OffsetValueType
Definition: itkLabelMap.h:134
bool operator==(const Iterator &iter) const
Definition: itkLabelMap.h:424
typename std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType
Definition: itkLabelMap.h:445
typename Superclass::IndexType IndexType
Definition: itkLabelMap.h:110
Iterator & operator++()
Definition: itkLabelMap.h:418
typename LabelObjectType::Pointer LabelObjectPointerType
Definition: itkLabelMap.h:90
Implements a weak reference to an object.
LabelObjectContainerType m_LabelObjectContainer
Definition: itkLabelMap.h:466
bool operator!=(const ConstIterator &iter) const
Definition: itkLabelMap.h:347
const LabelType & GetLabel() const
Definition: itkLabelMap.h:406
InternalIteratorType m_End
Definition: itkLabelMap.h:448
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkLabelMap.h:311
std::vector< LabelType > LabelVectorType
Definition: itkLabelMap.h:106
void PrintLabelObjects() const
Definition: itkLabelMap.h:277
LabelType m_BackgroundValue
Definition: itkLabelMap.h:467
bool operator!=(const Iterator &iter) const
Definition: itkLabelMap.h:429
typename Superclass::PointType PointType
Definition: itkLabelMap.h:131
const LabelType & GetLabel() const
Definition: itkLabelMap.h:324
const LabelObjectType * GetLabelObject() const
Definition: itkLabelMap.h:319
Iterator & operator=(const Iterator &iter)
Definition: itkLabelMap.h:393
ConstIterator operator++(int)
Definition: itkLabelMap.h:329
typename LabelObjectContainerType::const_iterator LabelObjectContainerConstIterator
Definition: itkLabelMap.h:464
A forward iterator over the LabelObjects of a LabelMap.
Definition: itkLabelMap.h:291
TLabelObject LabelObjectType
Definition: itkLabelMap.h:88
InternalIteratorType m_Begin
Definition: itkLabelMap.h:447
typename LabelObjectContainerType::iterator LabelObjectContainerIterator
Definition: itkLabelMap.h:463
Iterator operator++(int)
Definition: itkLabelMap.h:411
typename Superclass::RegionType RegionType
Definition: itkLabelMap.h:123
typename Superclass::SizeType SizeType
Definition: itkLabelMap.h:116
Base class for templated image classes.
Definition: itkImageBase.h:105
Control indentation during Print() invocation.
Definition: itkIndent.h:49
InternalIteratorType m_Begin
Definition: itkLabelMap.h:365
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:70
ConstIterator & operator++()
Definition: itkLabelMap.h:336
SizeValueType LengthType
Definition: itkLabelMap.h:93
typename LabelObjectType::LabelType LabelType
Definition: itkLabelMap.h:102
LabelType PixelType
Definition: itkLabelMap.h:103
InternalIteratorType m_End
Definition: itkLabelMap.h:366
typename Superclass::OffsetType OffsetType
Definition: itkLabelMap.h:113
signed long OffsetValueType
Definition: itkIntTypes.h:94
A forward iterator over the LabelObjects of a LabelMap.
Definition: itkLabelMap.h:373
Base class for all data objects in ITK.
bool operator==(const ConstIterator &iter) const
Definition: itkLabelMap.h:342
ConstIterator(const ConstIterator &iter)
Definition: itkLabelMap.h:304