ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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{
68template <typename TLabelObject>
69class ITK_TEMPLATE_EXPORT LabelMap : public ImageBase<TLabelObject::ImageDimension>
70{
71public:
72 ITK_DISALLOW_COPY_AND_MOVE(LabelMap);
73
75 using Self = LabelMap;
80
82 itkNewMacro(Self);
83
85 itkOverrideGetNameOfClassMacro(LabelMap);
86
87 using LabelObjectType = TLabelObject;
88
90
91 using typename Superclass::SizeValueType;
93
98 static constexpr unsigned int ImageDimension = LabelObjectType::ImageDimension;
99
103
105 using LabelVectorType = std::vector<LabelType>;
106 using LabelObjectVectorType = std::vector<LabelObjectPointerType>;
107
109 using typename Superclass::IndexType;
110
112 using typename Superclass::OffsetType;
113
115 using typename Superclass::SizeType;
116
118 using typename Superclass::DirectionType;
119
122 using typename Superclass::RegionType;
123
126 using typename Superclass::SpacingType;
127
130 using typename Superclass::PointType;
131
133 using typename Superclass::OffsetValueType;
134
137 void
138 Initialize() override;
139
141 void
142 Allocate(bool initialize = false) override;
143
144 virtual void
145 Graft(const Self * imgData);
146
154 GetLabelObject(const LabelType & label);
155 const LabelObjectType *
156 GetLabelObject(const LabelType & label) const;
163 bool
164 HasLabel(const LabelType label) const;
165
175 const LabelObjectType *
185 const LabelType &
186 GetPixel(const IndexType & idx) const;
187
197 void
198 SetPixel(const IndexType & idx, const LabelType & iLabel);
199
207 void
208 AddPixel(const IndexType & idx, const LabelType & label);
209
214 void
215 RemovePixel(const IndexType & idx, const LabelType & label);
216
224 void
225 SetLine(const IndexType & idx, const LengthType & length, const LabelType & label);
226
233 GetLabelObject(const IndexType & idx) const;
234
239 void
241
246 void
248
252 void
254
258 void
259 RemoveLabel(const LabelType & label);
260
264 void
266
270 typename Self::SizeValueType
272 {
273 return static_cast<SizeValueType>(m_LabelObjectContainer.size());
274 }
275
279 LabelVectorType
280 GetLabels() const;
281
287
292 itkGetConstMacro(BackgroundValue, LabelType);
293 itkSetMacro(BackgroundValue, LabelType);
299 void
300 PrintLabelObjects(std::ostream & os) const;
301
302 void
304 {
305 this->PrintLabelObjects(std::cerr);
306 }
307
311 void
313
320 {
321 public:
322 ConstIterator() = default;
323
324 ConstIterator(const Self * lm)
325 : m_Begin(lm->m_LabelObjectContainer.begin())
326 , m_End(lm->m_LabelObjectContainer.end())
327 {
329 }
330
331 [[nodiscard]] const LabelObjectType *
333 {
334 return m_Iterator->second;
335 }
336
337 [[nodiscard]] const LabelType &
338 GetLabel() const
339 {
340 return m_Iterator->first;
341 }
342
345 {
346 ConstIterator tmp = *this;
347 ++(*this);
348 return tmp;
349 }
350
353 {
354 ++m_Iterator;
355 return *this;
356 }
357
358 bool
359 operator==(const ConstIterator & iter) const
360 {
361 return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
362 }
363
365
366 void
368 {
370 }
371
372 [[nodiscard]] bool
373 IsAtEnd() const
374 {
375 return m_Iterator == m_End;
376 }
377
378 private:
379 using InternalIteratorType = typename std::map<LabelType, LabelObjectPointerType>::const_iterator;
383 };
384
391 {
392 public:
393 Iterator() = default;
394
396 : m_Begin(lm->m_LabelObjectContainer.begin())
397 , m_End(lm->m_LabelObjectContainer.end())
398 {
400 }
401
404 {
405 return m_Iterator->second;
406 }
407
408 [[nodiscard]] const LabelType &
409 GetLabel() const
410 {
411 return m_Iterator->first;
412 }
413
416 {
417 Iterator tmp = *this;
418 ++(*this);
419 return tmp;
420 }
421
422 Iterator &
424 {
425 ++m_Iterator;
426 return *this;
427 }
428
429 bool
430 operator==(const Iterator & iter) const
431 {
432 return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
433 }
434
436
437 void
439 {
441 }
442
443 [[nodiscard]] bool
444 IsAtEnd() const
445 {
446 return m_Iterator == m_End;
447 }
448
449 private:
450 using InternalIteratorType = typename std::map<LabelType, LabelObjectPointerType>::iterator;
454
455 friend class LabelMap;
456 };
457
458protected:
459 LabelMap() = default;
460 ~LabelMap() override = default;
461 void
462 PrintSelf(std::ostream & os, Indent indent) const override;
463 void
464 Graft(const DataObject * data) override;
465 using Superclass::Graft;
466
467private:
469 using LabelObjectContainerType = std::map<LabelType, LabelObjectPointerType>;
470 using LabelObjectContainerIterator = typename LabelObjectContainerType::iterator;
471 using LabelObjectContainerConstIterator = typename LabelObjectContainerType::const_iterator;
472
475
476 void
477 AddPixel(const LabelObjectContainerIterator & it, const IndexType & idx, const LabelType & label);
478
479 void
480 RemovePixel(const LabelObjectContainerIterator & it, const IndexType & idx, bool iEmitModifiedEvent);
481};
482} // end namespace itk
483
484#ifndef ITK_MANUAL_INSTANTIATION
485# include "itkLabelMap.hxx"
486#endif
487
488#endif
Base class for all data objects in ITK.
Vector< SpacingValueType, VImageDimension > SpacingType
ImageRegion< VImageDimension > RegionType
Index< VImageDimension > IndexType
Offset< VImageDimension > OffsetType
typename OffsetType::OffsetValueType OffsetValueType
virtual void Graft(const Self *image)
Matrix< SpacePrecisionType, VImageDimension, VImageDimension > DirectionType
Point< PointValueType, VImageDimension > PointType
typename SizeType::SizeValueType SizeValueType
Control indentation during Print() invocation.
Definition itkIndent.h:50
A forward iterator over the LabelObjects of a LabelMap.
ConstIterator(const Self *lm)
typename std::map< LabelType, LabelObjectPointerType >::const_iterator InternalIteratorType
const LabelType & GetLabel() const
const LabelObjectType * GetLabelObject() const
ConstIterator & operator++()
InternalIteratorType m_Iterator
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator)
InternalIteratorType m_Begin
InternalIteratorType m_End
bool operator==(const ConstIterator &iter) const
ConstIterator operator++(int)
A forward iterator over the LabelObjects of a LabelMap.
const LabelType & GetLabel() const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator)
bool operator==(const Iterator &iter) const
InternalIteratorType m_End
Iterator operator++(int)
InternalIteratorType m_Begin
typename std::map< LabelType, LabelObjectPointerType >::iterator InternalIteratorType
InternalIteratorType m_Iterator
LabelObjectType * GetLabelObject()
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)
void PrintLabelObjects(std::ostream &os) const
void ClearLabels()
typename LabelObjectType::Pointer LabelObjectPointerType
Definition itkLabelMap.h:89
typename LabelObjectType::LabelType LabelType
LabelObjectType * GetNthLabelObject(const SizeValueType &pos)
LabelVectorType GetLabels() const
void RemovePixel(const IndexType &idx, const LabelType &label)
LabelMap()=default
void Initialize() override
static constexpr unsigned int ImageDimension
Definition itkLabelMap.h:98
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
std::vector< LabelObjectPointerType > LabelObjectVectorType
Self::SizeValueType GetNumberOfLabelObjects() const
virtual void Graft(const Self *imgData)
typename LabelObjectContainerType::iterator LabelObjectContainerIterator
void AddLabelObject(LabelObjectType *labelObject)
void AddPixel(const IndexType &idx, const LabelType &label)
SizeValueType LengthType
Definition itkLabelMap.h:92
void Allocate(bool initialize=false) override
typename LabelObjectContainerType::const_iterator LabelObjectContainerConstIterator
void PushLabelObject(LabelObjectType *labelObject)
const LabelObjectType * GetNthLabelObject(const SizeValueType &pos) const
bool HasLabel(const LabelType label) const
void PrintLabelObjects() const
SmartPointer< const Self > ConstPointer
Definition itkLabelMap.h:78
void PrintSelf(std::ostream &os, Indent indent) const override
std::vector< LabelType > LabelVectorType
LabelType m_BackgroundValue
LabelObjectContainerType m_LabelObjectContainer
~LabelMap() override=default
WeakPointer< const Self > ConstWeakPointer
Definition itkLabelMap.h:79
ImageBase< Self::ImageDimension > Superclass
Definition itkLabelMap.h:76
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)
SmartPointer< Self > Pointer
Definition itkLabelMap.h:77
Implements transparent reference counting.
Implements a weak reference to an object.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....