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{
69template <typename TLabelObject>
70class ITK_TEMPLATE_EXPORT LabelMap : public ImageBase<TLabelObject::ImageDimension>
71{
72public:
73 ITK_DISALLOW_COPY_AND_MOVE(LabelMap);
75
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
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;
159
165 bool
166 HasLabel(const LabelType label) const;
167
176 const LabelObjectType *
179
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);
296
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();
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 {
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();
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 {
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
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.
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:91
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
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:94
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:80
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:81
ImageBase< Self::ImageDimension > Superclass
Definition itkLabelMap.h:78
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:79
Implements transparent reference counting.
Implements a weak reference to an object.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....