ITK  6.0.0
Insight Toolkit
itkLabelObject.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 itkLabelObject_h
19 #define itkLabelObject_h
20 
21 #include <deque>
22 #include "itkLightObject.h"
23 #include "itkLabelObjectLine.h"
24 #include "itkWeakPointer.h"
25 #include "itkObjectFactory.h"
26 
27 namespace itk
28 {
64 template <typename TLabel, unsigned int VImageDimension>
65 class ITK_TEMPLATE_EXPORT LabelObject : public LightObject
66 {
67 public:
68  ITK_DISALLOW_COPY_AND_MOVE(LabelObject);
72  using Self = LabelObject;
78 
80  itkNewMacro(Self);
81 
83  itkOverrideGetNameOfClassMacro(LabelObject);
84 
85  static constexpr unsigned int ImageDimension = VImageDimension;
86 
89  using LabelType = TLabel;
91  using LengthType = typename LineType::LengthType;
92  using AttributeType = unsigned int;
94 
95  static constexpr AttributeType LABEL = 0;
96 
97  static AttributeType
98  GetAttributeFromName(const std::string & s);
99 
100  static std::string
101  GetNameFromAttribute(const AttributeType & a);
102 
106  const LabelType &
107  GetLabel() const;
108 
109  void
110  SetLabel(const LabelType & label);
111 
116  bool
117  HasIndex(const IndexType & idx) const;
118 
123  void
124  AddIndex(const IndexType & idx);
125 
131  bool
132  RemoveIndex(const IndexType & idx);
133 
137  void
138  AddLine(const IndexType & idx, const LengthType & length);
139 
143  void
144  AddLine(const LineType & line);
145 
147  GetNumberOfLines() const;
148 
149  const LineType &
150  GetLine(SizeValueType i) const;
151 
152  LineType &
153  GetLine(SizeValueType i);
154 
163  Size() const;
164 
169  bool
170  Empty() const;
171 
172  void
173  Clear();
174 
179  IndexType
180  GetIndex(SizeValueType offset) const;
181 
183  template <typename TSourceLabelObject>
184  void
185  CopyLinesFrom(const TSourceLabelObject * src);
186 
188  template <typename TSourceLabelObject>
189  void
190  CopyAttributesFrom(const TSourceLabelObject * src);
191 
193  template <typename TSourceLabelObject>
194  void
195  CopyAllFrom(const TSourceLabelObject * src);
196 
200  void
201  Optimize();
202 
204  void
205  Shift(OffsetType offset);
206 
213  {
214  public:
215  ConstLineIterator() = default;
216 
218  {
219  m_Begin = lo->m_LineContainer.begin();
220  m_End = lo->m_LineContainer.end();
221  m_Iterator = m_Begin;
222  }
223 
225  {
226  m_Iterator = iter.m_Iterator;
227  m_Begin = iter.m_Begin;
228  m_End = iter.m_End;
229  }
230 
233  {
234  m_Iterator = iter.m_Iterator;
235  m_Begin = iter.m_Begin;
236  m_End = iter.m_End;
237  return *this;
238  }
239 
240  const LineType &
241  GetLine() const
242  {
243  return *m_Iterator;
244  }
245 
248  {
249  ConstLineIterator tmp = *this;
250  ++(*this);
251  return tmp;
252  }
253 
256  {
257  ++m_Iterator;
258  return *this;
259  }
260 
261  bool
262  operator==(const ConstLineIterator & iter) const
263  {
264  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
265  }
266 
267  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstLineIterator);
268 
269  void
271  {
272  m_Iterator = m_Begin;
273  }
274 
275  bool
276  IsAtEnd() const
277  {
278  return m_Iterator == m_End;
279  }
280 
281  private:
282  using LineContainerType = typename std::deque<LineType>;
283  using InternalIteratorType = typename LineContainerType::const_iterator;
287  };
288 
295  {
296  public:
298  : m_Iterator()
299  , m_Begin()
300  , m_End()
301  {
302  m_Index.Fill(0);
303  }
304 
306  {
307  m_Begin = lo->m_LineContainer.begin();
308  m_End = lo->m_LineContainer.end();
309  GoToBegin();
310  }
311 
313  {
314  m_Iterator = iter.m_Iterator;
315  m_Index = iter.m_Index;
316  m_Begin = iter.m_Begin;
317  m_End = iter.m_End;
318  }
319 
322  {
323  m_Iterator = iter.m_Iterator;
324  m_Index = iter.m_Index;
325  m_Begin = iter.m_Begin;
326  m_End = iter.m_End;
327  return *this;
328  }
329 
330  const IndexType &
331  GetIndex() const
332  {
333  return m_Index;
334  }
335 
338  {
339  m_Index[0]++;
340  if (m_Index[0] >= m_Iterator->GetIndex()[0] + (OffsetValueType)m_Iterator->GetLength())
341  {
342  // we've reached the end of the line - go to the next one
343  ++m_Iterator;
344  NextValidLine();
345  }
346  return *this;
347  }
348 
351  {
352  ConstIndexIterator tmp = *this;
353  ++(*this);
354  return tmp;
355  }
356 
357  bool
358  operator==(const ConstIndexIterator & iter) const
359  {
360  return m_Index == iter.m_Index && m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
361  }
362 
363  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIndexIterator);
364 
365  void
367  {
368  m_Iterator = m_Begin;
369  m_Index.Fill(0);
370  NextValidLine();
371  }
372 
373  bool
374  IsAtEnd() const
375  {
376  return m_Iterator == m_End;
377  }
378 
379  private:
380  using LineContainerType = typename std::deque<LineType>;
381  using InternalIteratorType = typename LineContainerType::const_iterator;
382  void
384  {
385  // search for the next valid position
386  while (m_Iterator != m_End && m_Iterator->GetLength() == 0)
387  {
388  ++m_Iterator;
389  }
390  if (m_Iterator != m_End)
391  {
392  m_Index = m_Iterator->GetIndex();
393  }
394  }
395 
400  };
401 
402 protected:
403  LabelObject();
404  void
405  PrintSelf(std::ostream & os, Indent indent) const override;
406 
407 private:
408  using LineContainerType = typename std::deque<LineType>;
409 
410  LineContainerType m_LineContainer{};
411  LabelType m_Label{};
412 };
413 } // end namespace itk
414 
415 #ifndef ITK_MANUAL_INSTANTIATION
416 # include "itkLabelObject.hxx"
417 #endif
418 
419 #endif
itk::LabelObject::AttributeType
unsigned int AttributeType
Definition: itkLabelObject.h:92
itk::LabelObject::ConstLineIterator::GoToBegin
void GoToBegin()
Definition: itkLabelObject.h:270
itk::LabelObject::ConstIndexIterator::GoToBegin
void GoToBegin()
Definition: itkLabelObject.h:366
itk::LabelObject::ConstLineIterator::m_Iterator
InternalIteratorType m_Iterator
Definition: itkLabelObject.h:284
itk::LabelObject::LengthType
typename LineType::LengthType LengthType
Definition: itkLabelObject.h:91
itkObjectFactory.h
itk::LabelObject::ConstLineIterator::LineContainerType
typename std::deque< LineType > LineContainerType
Definition: itkLabelObject.h:282
itk::Index< VImageDimension >
itk::LabelObject::ConstLineIterator::ConstLineIterator
ConstLineIterator(const Self *lo)
Definition: itkLabelObject.h:217
itk::LabelObject::ConstIndexIterator::m_End
InternalIteratorType m_End
Definition: itkLabelObject.h:398
itkWeakPointer.h
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::LabelObject::ConstIndexIterator::operator++
ConstIndexIterator & operator++()
Definition: itkLabelObject.h:337
itk::LabelObject::ConstIndexIterator::m_Begin
InternalIteratorType m_Begin
Definition: itkLabelObject.h:397
itk::LabelObject::ConstLineIterator::operator=
ConstLineIterator & operator=(const ConstLineIterator &iter)
Definition: itkLabelObject.h:232
Self
AddImageFilter Self
Definition: itkAddImageFilter.h:89
itk::LabelObject::ConstLineIterator::InternalIteratorType
typename LineContainerType::const_iterator InternalIteratorType
Definition: itkLabelObject.h:283
itk::LabelObject::ConstIndexIterator::operator==
bool operator==(const ConstIndexIterator &iter) const
Definition: itkLabelObject.h:358
itk::LabelObject::ConstLineIterator::ConstLineIterator
ConstLineIterator(const ConstLineIterator &iter)
Definition: itkLabelObject.h:224
itk::LabelObject::ConstIndexIterator::GetIndex
const IndexType & GetIndex() const
Definition: itkLabelObject.h:331
itk::LabelObjectLine::LengthType
SizeValueType LengthType
Definition: itkLabelObjectLine.h:51
itk::SmartPointer< Self >
itk::LabelObject::ConstLineIterator::GetLine
const LineType & GetLine() const
Definition: itkLabelObject.h:241
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::LabelObject::LineContainerType
typename std::deque< LineType > LineContainerType
Definition: itkLabelObject.h:408
itk::LabelObject::ConstIndexIterator::m_Index
IndexType m_Index
Definition: itkLabelObject.h:399
itk::LabelObject::SizeValueType
itk::SizeValueType SizeValueType
Definition: itkLabelObject.h:93
itk::LabelObject::ConstIndexIterator::LineContainerType
typename std::deque< LineType > LineContainerType
Definition: itkLabelObject.h:380
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::LabelObject::ConstIndexIterator::operator=
ConstIndexIterator & operator=(const ConstIndexIterator &iter)
Definition: itkLabelObject.h:321
itk::LabelObject
The base class for the representation of a labeled binary object in an image.
Definition: itkLabelObject.h:65
itk::LabelObject::ConstIndexIterator
Definition: itkLabelObject.h:294
itk::LabelObject::ConstIndexIterator::ConstIndexIterator
ConstIndexIterator()
Definition: itkLabelObject.h:297
itk::LabelObject::ConstIndexIterator::InternalIteratorType
typename LineContainerType::const_iterator InternalIteratorType
Definition: itkLabelObject.h:381
itk::LabelObject::ConstLineIterator::operator==
bool operator==(const ConstLineIterator &iter) const
Definition: itkLabelObject.h:262
itk::LabelObject::ConstIndexIterator::operator++
ConstIndexIterator operator++(int)
Definition: itkLabelObject.h:350
itk::LabelObject::ConstIndexIterator::m_Iterator
InternalIteratorType m_Iterator
Definition: itkLabelObject.h:396
itk::LabelObject::ConstLineIterator
A forward iterator over the lines of a LabelObject.
Definition: itkLabelObject.h:212
itk::LabelObject::ConstLineIterator::m_End
InternalIteratorType m_End
Definition: itkLabelObject.h:286
itk::OffsetValueType
long OffsetValueType
Definition: itkIntTypes.h:97
itk::LabelObject::ConstLineIterator::operator++
ConstLineIterator & operator++()
Definition: itkLabelObject.h:255
itk::LabelObject::LabelType
TLabel LabelType
Definition: itkLabelObject.h:89
itk::Offset
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition: itkOffset.h:66
itk::LabelObjectLine
Definition: itkLabelObjectLine.h:43
itk::WeakPointer
Implements a weak reference to an object.
Definition: itkWeakPointer.h:44
itk::LabelObject::ConstLineIterator::IsAtEnd
bool IsAtEnd() const
Definition: itkLabelObject.h:276
itk::LabelObject::ConstLineIterator::m_Begin
InternalIteratorType m_Begin
Definition: itkLabelObject.h:285
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::LabelObject::ConstLineIterator::operator++
ConstLineIterator operator++(int)
Definition: itkLabelObject.h:247
itk::LabelObject::ConstIndexIterator::ConstIndexIterator
ConstIndexIterator(const Self *lo)
Definition: itkLabelObject.h:305
itk::LabelObject::ConstIndexIterator::NextValidLine
void NextValidLine()
Definition: itkLabelObject.h:383
itk::LabelObject::ConstIndexIterator::ConstIndexIterator
ConstIndexIterator(const ConstIndexIterator &iter)
Definition: itkLabelObject.h:312
itkLightObject.h
itk::LabelObject::ConstIndexIterator::IsAtEnd
bool IsAtEnd() const
Definition: itkLabelObject.h:374
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86
itkLabelObjectLine.h