ITK  4.13.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:
74  typedef LabelMap Self;
79 
81  itkNewMacro(Self);
82 
84  itkTypeMacro(LabelMap, ImageBase);
85 
86  typedef TLabelObject LabelObjectType;
87 
88  typedef typename LabelObjectType::Pointer LabelObjectPointerType;
89 
92 
97  itkStaticConstMacro(ImageDimension, unsigned int, LabelObjectType::ImageDimension);
98 
100  typedef typename LabelObjectType::LabelType LabelType;
102 
104  typedef std::vector< LabelType > LabelVectorType;
105  typedef std::vector< LabelObjectPointerType > LabelObjectVectorType;
106 
109 
111  typedef typename Superclass::OffsetType OffsetType;
112 
114  typedef typename Superclass::SizeType SizeType;
115 
118 
121  typedef typename Superclass::RegionType RegionType;
122 
125  typedef typename Superclass::SpacingType SpacingType;
126 
130 
133 
136  virtual void Initialize() ITK_OVERRIDE;
137 
139  virtual void Allocate(bool initialize = false) ITK_OVERRIDE;
140 
141  virtual void Graft(const Self *imgData);
142 
148  LabelObjectType * GetLabelObject(const LabelType & label);
149  const LabelObjectType * GetLabelObject(const LabelType & label) const;
151 
157  bool HasLabel(const LabelType label) const;
158 
165  LabelObjectType * GetNthLabelObject(const SizeValueType & pos);
166  const LabelObjectType * GetNthLabelObject(const SizeValueType & pos) const;
168 
176  const LabelType & GetPixel(const IndexType & idx) const;
177 
187  void SetPixel(const IndexType & idx, const LabelType & label);
188 
196  void AddPixel(const IndexType & idx, const LabelType & label);
197 
202  void RemovePixel(const IndexType & idx, const LabelType & label);
203 
211  void SetLine(const IndexType & idx, const LengthType & length, const LabelType & label);
212 
218  LabelObjectType * GetLabelObject(const IndexType & idx) const;
219 
224  void AddLabelObject(LabelObjectType *labelObject);
225 
230  void PushLabelObject(LabelObjectType *labelObject);
231 
235  void RemoveLabelObject(LabelObjectType *labelObject);
236 
240  void RemoveLabel(const LabelType & label);
241 
245  void ClearLabels();
246 
250  typename Self::SizeValueType GetNumberOfLabelObjects() const;
251 
255  LabelVectorType GetLabels() const;
256 
260  LabelObjectVectorType GetLabelObjects() const;
261 
265  itkGetConstMacro(BackgroundValue, LabelType);
266  itkSetMacro(BackgroundValue, LabelType);
268 
273  void PrintLabelObjects(std::ostream & os) const;
274 
275  void PrintLabelObjects() const
276  {
277  this->PrintLabelObjects(std::cerr);
278  }
279 
283  void Optimize();
284 
290  {
291  public:
292 
294 
295  ConstIterator(const Self *lm)
296  {
297  m_Begin = lm->m_LabelObjectContainer.begin();
298  m_End = lm->m_LabelObjectContainer.end();
299  m_Iterator = m_Begin;
300  }
301 
303  {
304  m_Iterator = iter.m_Iterator;
305  m_Begin = iter.m_Begin;
306  m_End = iter.m_End;
307  }
308 
310  {
311  m_Iterator = iter.m_Iterator;
312  m_Begin = iter.m_Begin;
313  m_End = iter.m_End;
314  return *this;
315  }
316 
318  {
319  return m_Iterator->second;
320  }
321 
322  const LabelType & GetLabel() const
323  {
324  return m_Iterator->first;
325  }
326 
328  {
329  ConstIterator tmp = *this;
330  ++(*this);
331  return tmp;
332  }
333 
335  {
336  ++m_Iterator;
337  return *this;
338  }
339 
340  bool operator==(const ConstIterator & iter) const
341  {
342  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
343  }
344 
345  bool operator!=(const ConstIterator & iter) const
346  {
347  return !( *this == iter );
348  }
349 
350  void GoToBegin()
351  {
352  m_Iterator = m_Begin;
353  }
354 
355  bool IsAtEnd() const
356  {
357  return m_Iterator == m_End;
358  }
359 
360  private:
361  typedef typename std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType;
365  };
366 
371  class Iterator
372  {
373  public:
374 
375  Iterator() {}
376 
378  {
379  m_Begin = lm->m_LabelObjectContainer.begin();
380  m_End = lm->m_LabelObjectContainer.end();
381  m_Iterator = m_Begin;
382  }
383 
384  Iterator(const Iterator & iter)
385  {
386  m_Iterator = iter.m_Iterator;
387  m_Begin = iter.m_Begin;
388  m_End = iter.m_End;
389  }
390 
391  Iterator & operator=(const Iterator & iter)
392  {
393  m_Iterator = iter.m_Iterator;
394  m_Begin = iter.m_Begin;
395  m_End = iter.m_End;
396  return *this;
397  }
398 
400  {
401  return m_Iterator->second;
402  }
403 
404  const LabelType & GetLabel() const
405  {
406  return m_Iterator->first;
407  }
408 
410  {
411  Iterator tmp = *this;
412  ++(*this);
413  return tmp;
414  }
415 
417  {
418  ++m_Iterator;
419  return *this;
420  }
421 
422  bool operator==(const Iterator & iter) const
423  {
424  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
425  }
426 
427  bool operator!=(const Iterator & iter) const
428  {
429  return !( *this == iter );
430  }
431 
432  void GoToBegin()
433  {
434  m_Iterator = m_Begin;
435  }
436 
437  bool IsAtEnd() const
438  {
439  return m_Iterator == m_End;
440  }
441 
442  private:
443  typedef typename std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType;
447 
448  friend class LabelMap;
449  };
450 
451 protected:
452  LabelMap();
453  virtual ~LabelMap() ITK_OVERRIDE {}
454  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
455  virtual void Graft(const DataObject *data) ITK_OVERRIDE;
456  using Superclass::Graft;
457 
458 private:
459  ITK_DISALLOW_COPY_AND_ASSIGN(LabelMap);
460 
462  typedef std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType;
463  typedef typename LabelObjectContainerType::iterator LabelObjectContainerIterator;
464  typedef typename LabelObjectContainerType::const_iterator
466 
469 
470  void AddPixel( const LabelObjectContainerIterator& it,
471  const IndexType& idx,
472  const LabelType& iLabel );
473 
474  void RemovePixel( const LabelObjectContainerIterator& it,
475  const IndexType& idx,
476  bool iEmitModifiedEvent );
477 };
478 } // end namespace itk
479 
480 #ifndef ITK_MANUAL_INSTANTIATION
481 #include "itkLabelMap.hxx"
482 #endif
483 
484 #endif
Superclass::OffsetValueType OffsetValueType
Definition: itkLabelMap.h:132
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:444
SizeValueType LengthType
Definition: itkLabelMap.h:91
Superclass::SizeType SizeType
Definition: itkLabelMap.h:114
Superclass::OffsetType OffsetType
Definition: itkLabelMap.h:111
bool IsAtEnd() const
Definition: itkLabelMap.h:437
TLabelObject LabelObjectType
Definition: itkLabelMap.h:84
ConstIterator(const Self *lm)
Definition: itkLabelMap.h:295
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:362
Iterator(const Iterator &iter)
Definition: itkLabelMap.h:384
signed long OffsetValueType
Definition: itkIntTypes.h:154
Superclass::DirectionType DirectionType
Definition: itkLabelMap.h:117
LabelObjectType * GetLabelObject()
Definition: itkLabelMap.h:399
LabelMap Self
Definition: itkLabelMap.h:74
SmartPointer< Self > Pointer
Definition: itkLabelMap.h:76
LabelObjectContainerType::iterator LabelObjectContainerIterator
Definition: itkLabelMap.h:463
WeakPointer< const Self > ConstWeakPointer
Definition: itkLabelMap.h:78
bool operator==(const Iterator &iter) const
Definition: itkLabelMap.h:422
Iterator & operator++()
Definition: itkLabelMap.h:416
LabelType PixelType
Definition: itkLabelMap.h:101
Implements a weak reference to an object.
unsigned long SizeValueType
Definition: itkIntTypes.h:143
LabelObjectContainerType m_LabelObjectContainer
Definition: itkLabelMap.h:467
bool operator!=(const ConstIterator &iter) const
Definition: itkLabelMap.h:345
const LabelType & GetLabel() const
Definition: itkLabelMap.h:404
SmartPointer< const Self > ConstPointer
Definition: itkLabelMap.h:77
InternalIteratorType m_End
Definition: itkLabelMap.h:446
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkLabelMap.h:309
std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType
Definition: itkLabelMap.h:443
LabelObjectType::Pointer LabelObjectPointerType
Definition: itkLabelMap.h:88
Superclass::SizeValueType SizeValueType
Definition: itkLabelMap.h:90
LabelType m_BackgroundValue
Definition: itkLabelMap.h:468
std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType
Definition: itkLabelMap.h:459
Superclass::PointType PointType
Definition: itkLabelMap.h:129
bool operator!=(const Iterator &iter) const
Definition: itkLabelMap.h:427
const LabelType & GetLabel() const
Definition: itkLabelMap.h:322
const LabelObjectType * GetLabelObject() const
Definition: itkLabelMap.h:317
virtual ~LabelMap() override
Definition: itkLabelMap.h:453
std::vector< LabelType > LabelVectorType
Definition: itkLabelMap.h:104
std::vector< LabelObjectPointerType > LabelObjectVectorType
Definition: itkLabelMap.h:105
Iterator & operator=(const Iterator &iter)
Definition: itkLabelMap.h:391
std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType
Definition: itkLabelMap.h:361
ConstIterator operator++(int)
Definition: itkLabelMap.h:327
A forward iterator over the LabelObjects of a LabelMap.
Definition: itkLabelMap.h:289
InternalIteratorType m_Begin
Definition: itkLabelMap.h:445
Superclass::RegionType RegionType
Definition: itkLabelMap.h:121
Iterator operator++(int)
Definition: itkLabelMap.h:409
Superclass::SpacingType SpacingType
Definition: itkLabelMap.h:125
Base class for templated image classes.
Definition: itkImageBase.h:114
LabelObjectContainerType::const_iterator LabelObjectContainerConstIterator
Definition: itkLabelMap.h:465
LabelObjectType::LabelType LabelType
Definition: itkLabelMap.h:100
Superclass::IndexType IndexType
Definition: itkLabelMap.h:108
Control indentation during Print() invocation.
Definition: itkIndent.h:49
InternalIteratorType m_Begin
Definition: itkLabelMap.h:363
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:70
ConstIterator & operator++()
Definition: itkLabelMap.h:334
InternalIteratorType m_End
Definition: itkLabelMap.h:364
A forward iterator over the LabelObjects of a LabelMap.
Definition: itkLabelMap.h:371
Base class for all data objects in ITK.
bool operator==(const ConstIterator &iter) const
Definition: itkLabelMap.h:340
ConstIterator(const ConstIterator &iter)
Definition: itkLabelMap.h:302
ImageBase< TLabelObject::ImageDimension > Superclass
Definition: itkLabelMap.h:75