ITK  5.2.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  * 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 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);
70 
72  using Self = LabelObject;
78 
80  itkNewMacro(Self);
81 
83  itkTypeMacro(LabelObject, LightObject);
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  bool
268  operator!=(const ConstLineIterator & iter) const
269  {
270  return !(*this == iter);
271  }
272 
273  void
275  {
276  m_Iterator = m_Begin;
277  }
278 
279  bool
280  IsAtEnd() const
281  {
282  return m_Iterator == m_End;
283  }
284 
285  private:
286  using LineContainerType = typename std::deque<LineType>;
287  using InternalIteratorType = typename LineContainerType::const_iterator;
291  };
292 
299  {
300  public:
302  : m_Iterator()
303  , m_Begin()
304  , m_End()
305  {
306  m_Index.Fill(0);
307  }
308 
310  {
311  m_Begin = lo->m_LineContainer.begin();
312  m_End = lo->m_LineContainer.end();
313  GoToBegin();
314  }
315 
317  {
318  m_Iterator = iter.m_Iterator;
319  m_Index = iter.m_Index;
320  m_Begin = iter.m_Begin;
321  m_End = iter.m_End;
322  }
323 
326  {
327  m_Iterator = iter.m_Iterator;
328  m_Index = iter.m_Index;
329  m_Begin = iter.m_Begin;
330  m_End = iter.m_End;
331  return *this;
332  }
333 
334  const IndexType &
335  GetIndex() const
336  {
337  return m_Index;
338  }
339 
342  {
343  m_Index[0]++;
344  if (m_Index[0] >= m_Iterator->GetIndex()[0] + (OffsetValueType)m_Iterator->GetLength())
345  {
346  // we've reached the end of the line - go to the next one
347  ++m_Iterator;
348  NextValidLine();
349  }
350  return *this;
351  }
352 
355  {
356  ConstIndexIterator tmp = *this;
357  ++(*this);
358  return tmp;
359  }
360 
361  bool
362  operator==(const ConstIndexIterator & iter) const
363  {
364  return m_Index == iter.m_Index && m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
365  }
366 
367  bool
368  operator!=(const ConstIndexIterator & iter) const
369  {
370  return !(*this == iter);
371  }
372 
373  void
375  {
376  m_Iterator = m_Begin;
377  m_Index.Fill(0);
378  NextValidLine();
379  }
380 
381  bool
382  IsAtEnd() const
383  {
384  return m_Iterator == m_End;
385  }
386 
387  private:
388  using LineContainerType = typename std::deque<LineType>;
389  using InternalIteratorType = typename LineContainerType::const_iterator;
390  void
392  {
393  // search for the next valid position
394  while (m_Iterator != m_End && m_Iterator->GetLength() == 0)
395  {
396  ++m_Iterator;
397  }
398  if (m_Iterator != m_End)
399  {
400  m_Index = m_Iterator->GetIndex();
401  }
402  }
403 
408  };
409 
410 protected:
411  LabelObject();
412  void
413  PrintSelf(std::ostream & os, Indent indent) const override;
414 
415 private:
416  using LineContainerType = typename std::deque<LineType>;
417 
420 };
421 } // end namespace itk
422 
423 #ifndef ITK_MANUAL_INSTANTIATION
424 # include "itkLabelObject.hxx"
425 #endif
426 
427 #endif
itk::LabelObject::AttributeType
unsigned int AttributeType
Definition: itkLabelObject.h:92
itk::LabelObject::ConstLineIterator::GoToBegin
void GoToBegin()
Definition: itkLabelObject.h:274
itk::LabelObject::ConstIndexIterator::GoToBegin
void GoToBegin()
Definition: itkLabelObject.h:374
itk::LabelObject::ConstLineIterator::m_Iterator
InternalIteratorType m_Iterator
Definition: itkLabelObject.h:288
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:286
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:406
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:341
itk::LabelObject::ConstIndexIterator::m_Begin
InternalIteratorType m_Begin
Definition: itkLabelObject.h:405
itk::LabelObject::ConstLineIterator::operator=
ConstLineIterator & operator=(const ConstLineIterator &iter)
Definition: itkLabelObject.h:232
itk::LabelObject::ConstLineIterator::InternalIteratorType
typename LineContainerType::const_iterator InternalIteratorType
Definition: itkLabelObject.h:287
itk::LabelObject::ConstLineIterator::operator!=
bool operator!=(const ConstLineIterator &iter) const
Definition: itkLabelObject.h:268
itk::LabelObject::ConstIndexIterator::operator!=
bool operator!=(const ConstIndexIterator &iter) const
Definition: itkLabelObject.h:368
itk::LabelObject::ConstIndexIterator::operator==
bool operator==(const ConstIndexIterator &iter) const
Definition: itkLabelObject.h:362
itk::LabelObject::ConstLineIterator::ConstLineIterator
ConstLineIterator(const ConstLineIterator &iter)
Definition: itkLabelObject.h:224
itk::LabelObject::ConstIndexIterator::GetIndex
const IndexType & GetIndex() const
Definition: itkLabelObject.h:335
itk::LabelObjectLine::LengthType
SizeValueType LengthType
Definition: itkLabelObjectLine.h:48
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:416
itk::LabelObject::ConstIndexIterator::m_Index
IndexType m_Index
Definition: itkLabelObject.h:407
itk::LabelObject::SizeValueType
itk::SizeValueType SizeValueType
Definition: itkLabelObject.h:93
itk::LabelObject::ConstIndexIterator::LineContainerType
typename std::deque< LineType > LineContainerType
Definition: itkLabelObject.h:388
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::LabelObject::ConstIndexIterator::operator=
ConstIndexIterator & operator=(const ConstIndexIterator &iter)
Definition: itkLabelObject.h:325
itk::LabelObject
The base class for the representation of an labeled binary object in an image.
Definition: itkLabelObject.h:65
itk::LabelObject::ConstIndexIterator
Definition: itkLabelObject.h:298
itk::LabelObject::m_LineContainer
LineContainerType m_LineContainer
Definition: itkLabelObject.h:418
itk::LabelObject::ConstIndexIterator::ConstIndexIterator
ConstIndexIterator()
Definition: itkLabelObject.h:301
itk::LabelObject::ConstIndexIterator::InternalIteratorType
typename LineContainerType::const_iterator InternalIteratorType
Definition: itkLabelObject.h:389
itk::LabelObject::ConstLineIterator::operator==
bool operator==(const ConstLineIterator &iter) const
Definition: itkLabelObject.h:262
itk::LabelObject::ConstIndexIterator::operator++
ConstIndexIterator operator++(int)
Definition: itkLabelObject.h:354
itk::LabelObject::ConstIndexIterator::m_Iterator
InternalIteratorType m_Iterator
Definition: itkLabelObject.h:404
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:290
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:67
itk::LabelObjectLine
Definition: itkLabelObjectLine.h:42
itk::WeakPointer
Implements a weak reference to an object.
Definition: itkWeakPointer.h:44
itk::LabelObject::ConstLineIterator::IsAtEnd
bool IsAtEnd() const
Definition: itkLabelObject.h:280
itk::LabelObject::m_Label
LabelType m_Label
Definition: itkLabelObject.h:419
itk::LabelObject::ConstLineIterator::m_Begin
InternalIteratorType m_Begin
Definition: itkLabelObject.h:289
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::LabelObject::ConstLineIterator::operator++
ConstLineIterator operator++(int)
Definition: itkLabelObject.h:247
itk::LabelObject::ConstIndexIterator::ConstIndexIterator
ConstIndexIterator(const Self *lo)
Definition: itkLabelObject.h:309
itk::OffsetValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
itk::LabelObject::ConstIndexIterator::NextValidLine
void NextValidLine()
Definition: itkLabelObject.h:391
itk::LabelObject::ConstIndexIterator::ConstIndexIterator
ConstIndexIterator(const ConstIndexIterator &iter)
Definition: itkLabelObject.h:316
itkLightObject.h
itk::LabelObject::ConstIndexIterator::IsAtEnd
bool IsAtEnd() const
Definition: itkLabelObject.h:382
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itkLabelObjectLine.h