ITK  4.2.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< class TLabelObject >
70 class ITK_EXPORT LabelMap:public ImageBase< ::itk::GetImageDimension< 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 
108  typedef typename Superclass::IndexType IndexType;
109 
111  typedef typename Superclass::OffsetType OffsetType;
112 
114  typedef typename Superclass::SizeType SizeType;
115 
117  typedef typename Superclass::DirectionType DirectionType;
118 
121  typedef typename Superclass::RegionType RegionType;
122 
125  typedef typename Superclass::SpacingType SpacingType;
126 
129  typedef typename Superclass::PointType PointType;
130 
133 
136  virtual void Initialize();
137 
139  virtual void Allocate();
140 
141  virtual void Graft(const DataObject *data);
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 
309  ConstIterator & operator=(const ConstIterator & iter)
310  {
311  m_Iterator = iter.m_Iterator;
312  m_Begin = iter.m_Begin;
313  m_End = iter.m_End;
314  return *this;
315  }
316 
317  const LabelObjectType * GetLabelObject() const
318  {
319  return m_Iterator->second;
320  }
321 
322  const LabelType & GetLabel() const
323  {
324  return m_Iterator->first;
325  }
326 
327  ConstIterator operator++(int)
328  {
329  ConstIterator tmp = *this;
330  ++(*this);
331  return tmp;
332  }
333 
334  ConstIterator & operator++()
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 
399  LabelObjectType * GetLabelObject()
400  {
401  return m_Iterator->second;
402  }
403 
404  const LabelType & GetLabel() const
405  {
406  return m_Iterator->first;
407  }
408 
409  Iterator operator++(int)
410  {
411  Iterator tmp = *this;
412  ++(*this);
413  return tmp;
414  }
415 
416  Iterator & operator++()
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() {}
454  void PrintSelf(std::ostream & os, Indent indent) const;
455 
456 private:
457  LabelMap(const Self &); //purposely not implemented
458  void operator=(const Self &); //purposely not implemented
459 
461  typedef std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType;
462  typedef typename LabelObjectContainerType::iterator LabelObjectContainerIterator;
463  typedef 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
484