ITK  6.0.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
25namespace itk
26{
69template <typename TLabelObject>
70class ITK_TEMPLATE_EXPORT LabelMap : public ImageBase<TLabelObject::ImageDimension>
71{
72public:
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
176 const LabelObjectType *
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
243
248 void
250
254 void
256
260 void
261 RemoveLabel(const LabelType & label);
262
266 void
268
272 typename Self::SizeValueType
274 {
275 return static_cast<SizeValueType>(m_LabelObjectContainer.size());
276 }
277
281 LabelVectorType
282 GetLabels() const;
283
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
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
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
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
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
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
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
460protected:
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
469private:
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
Base class for all data objects in ITK.
Base class for templated image classes.
Definition: itkImageBase.h:115
typename SizeType::SizeValueType SizeValueType
Definition: itkImageBase.h:151
Control indentation during Print() invocation.
Definition: itkIndent.h:50
A forward iterator over the LabelObjects of a LabelMap.
Definition: itkLabelMap.h:322
ConstIterator(const Self *lm)
Definition: itkLabelMap.h:326
typename std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType
Definition: itkLabelMap.h:381
const LabelType & GetLabel() const
Definition: itkLabelMap.h:340
const LabelObjectType * GetLabelObject() const
Definition: itkLabelMap.h:334
ConstIterator & operator++()
Definition: itkLabelMap.h:354
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:382
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator)
InternalIteratorType m_Begin
Definition: itkLabelMap.h:383
InternalIteratorType m_End
Definition: itkLabelMap.h:384
bool operator==(const ConstIterator &iter) const
Definition: itkLabelMap.h:361
ConstIterator operator++(int)
Definition: itkLabelMap.h:346
A forward iterator over the LabelObjects of a LabelMap.
Definition: itkLabelMap.h:393
const LabelType & GetLabel() const
Definition: itkLabelMap.h:411
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator)
bool operator==(const Iterator &iter) const
Definition: itkLabelMap.h:432
InternalIteratorType m_End
Definition: itkLabelMap.h:455
Iterator & operator++()
Definition: itkLabelMap.h:425
Iterator operator++(int)
Definition: itkLabelMap.h:417
InternalIteratorType m_Begin
Definition: itkLabelMap.h:454
typename std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType
Definition: itkLabelMap.h:452
InternalIteratorType m_Iterator
Definition: itkLabelMap.h:453
LabelObjectType * GetLabelObject()
Definition: itkLabelMap.h:405
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:71
void SetPixel(const IndexType &idx, const LabelType &iLabel)
Set the pixel value at a given index in the image.
LabelObjectType * GetLabelObject(const LabelType &label)
void Graft(const DataObject *data) override
void RemoveLabel(const LabelType &label)
void RemoveLabelObject(LabelObjectType *labelObject)
TLabelObject LabelObjectType
Definition: itkLabelMap.h:89
void PrintLabelObjects(std::ostream &os) const
void ClearLabels()
typename LabelObjectType::Pointer LabelObjectPointerType
Definition: itkLabelMap.h:91
typename LabelObjectType::LabelType LabelType
Definition: itkLabelMap.h:103
LabelObjectType * GetNthLabelObject(const SizeValueType &pos)
LabelVectorType GetLabels() const
void RemovePixel(const IndexType &idx, const LabelType &label)
LabelMap()=default
void Initialize() override
LabelObjectVectorType GetLabelObjects() const
const LabelType & GetPixel(const IndexType &idx) const
void AddPixel(const LabelObjectContainerIterator &it, const IndexType &idx, const LabelType &label)
std::map< LabelType, LabelObjectPointerType > LabelObjectContainerType
Definition: itkLabelMap.h:471
std::vector< LabelObjectPointerType > LabelObjectVectorType
Definition: itkLabelMap.h:108
Self::SizeValueType GetNumberOfLabelObjects() const
Definition: itkLabelMap.h:273
virtual void Graft(const Self *imgData)
typename LabelObjectContainerType::iterator LabelObjectContainerIterator
Definition: itkLabelMap.h:472
void AddLabelObject(LabelObjectType *labelObject)
void AddPixel(const IndexType &idx, const LabelType &label)
SizeValueType LengthType
Definition: itkLabelMap.h:94
void Allocate(bool initialize=false) override
LabelType PixelType
Definition: itkLabelMap.h:104
typename LabelObjectContainerType::const_iterator LabelObjectContainerConstIterator
Definition: itkLabelMap.h:473
void PushLabelObject(LabelObjectType *labelObject)
const LabelObjectType * GetNthLabelObject(const SizeValueType &pos) const
bool HasLabel(const LabelType label) const
void PrintLabelObjects() const
Definition: itkLabelMap.h:305
void PrintSelf(std::ostream &os, Indent indent) const override
std::vector< LabelType > LabelVectorType
Definition: itkLabelMap.h:107
~LabelMap() override=default
LabelObjectType * GetLabelObject(const IndexType &idx) const
const LabelObjectType * GetLabelObject(const LabelType &label) const
void SetLine(const IndexType &idx, const LengthType &length, const LabelType &label)
void RemovePixel(const LabelObjectContainerIterator &it, const IndexType &idx, bool iEmitModifiedEvent)
Base class for most ITK classes.
Definition: itkObject.h:62
Implements a weak reference to an object.
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition: itkIntTypes.h:86
long OffsetValueType
Definition: itkIntTypes.h:97