ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkLabelObject.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 {
63 template< typename TLabel, unsigned int VImageDimension >
64 class ITK_TEMPLATE_EXPORT LabelObject:public LightObject
65 {
66 public:
67  ITK_DISALLOW_COPY_AND_ASSIGN(LabelObject);
68 
70  using Self = LabelObject;
76 
78  itkNewMacro(Self);
79 
81  itkTypeMacro(LabelObject, LightObject);
82 
83  static constexpr unsigned int ImageDimension = VImageDimension;
84 
87  using LabelType = TLabel;
89  using LengthType = typename LineType::LengthType;
90  using AttributeType = unsigned int;
92 
93  static constexpr AttributeType LABEL = 0;
94 
95  static AttributeType GetAttributeFromName(const std::string & s);
96 
97  static std::string GetNameFromAttribute(const AttributeType & a);
98 
102  const LabelType & GetLabel() const;
103 
104  void SetLabel(const LabelType & label);
105 
110  bool HasIndex(const IndexType & idx) const;
111 
116  void AddIndex(const IndexType & idx);
117 
123  bool RemoveIndex(const IndexType & idx );
124 
128  void AddLine(const IndexType & idx, const LengthType & length);
129 
133  void AddLine(const LineType & line);
134 
135  SizeValueType GetNumberOfLines() const;
136 
137  const LineType & GetLine(SizeValueType i) const;
138 
139  LineType & GetLine(SizeValueType i);
140 
148  SizeValueType Size() const;
149 
154  bool Empty() const;
155 
156  void Clear();
157 
162  IndexType GetIndex(SizeValueType i) const;
163 
165  template< typename TSourceLabelObject >
166  void CopyLinesFrom(const TSourceLabelObject *src);
167 
169  template< typename TSourceLabelObject >
170  void CopyAttributesFrom(const TSourceLabelObject *src);
171 
173  template< typename TSourceLabelObject >
174  void CopyAllFrom(const TSourceLabelObject *src);
175 
179  void Optimize();
180 
182  void Shift( OffsetType offset );
183 
189  {
190  public:
191 
192  ConstLineIterator() = default;
193 
195  {
196  m_Begin = lo->m_LineContainer.begin();
197  m_End = lo->m_LineContainer.end();
198  m_Iterator = m_Begin;
199  }
200 
202  {
203  m_Iterator = iter.m_Iterator;
204  m_Begin = iter.m_Begin;
205  m_End = iter.m_End;
206  }
207 
209  {
210  m_Iterator = iter.m_Iterator;
211  m_Begin = iter.m_Begin;
212  m_End = iter.m_End;
213  return *this;
214  }
215 
216  const LineType & GetLine() const
217  {
218  return *m_Iterator;
219  }
220 
222  {
223  ConstLineIterator tmp = *this;
224  ++(*this);
225  return tmp;
226  }
227 
229  {
230  ++m_Iterator;
231  return *this;
232  }
233 
234  bool operator==(const ConstLineIterator & iter) const
235  {
236  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
237  }
238 
239  bool operator!=(const ConstLineIterator & iter) const
240  {
241  return !( *this == iter );
242  }
243 
244  void GoToBegin()
245  {
246  m_Iterator = m_Begin;
247  }
248 
249  bool IsAtEnd() const
250  {
251  return m_Iterator == m_End;
252  }
253 
254  private:
255  using LineContainerType = typename std::deque< LineType >;
256  using InternalIteratorType = typename LineContainerType::const_iterator;
260  };
261 
267  {
268  public:
269 
271  m_Iterator(),
272  m_Begin(),
273  m_End()
274  {
275  m_Index.Fill(0);
276  }
277 
279  {
280  m_Begin = lo->m_LineContainer.begin();
281  m_End = lo->m_LineContainer.end();
282  GoToBegin();
283  }
284 
286  {
287  m_Iterator = iter.m_Iterator;
288  m_Index = iter.m_Index;
289  m_Begin = iter.m_Begin;
290  m_End = iter.m_End;
291  }
292 
294  {
295  m_Iterator = iter.m_Iterator;
296  m_Index = iter.m_Index;
297  m_Begin = iter.m_Begin;
298  m_End = iter.m_End;
299  return *this;
300  }
301 
302  const IndexType & GetIndex() const
303  {
304  return m_Index;
305  }
306 
308  {
309  m_Index[0]++;
310  if( m_Index[0] >= m_Iterator->GetIndex()[0] + (OffsetValueType)m_Iterator->GetLength() )
311  {
312  // we've reached the end of the line - go to the next one
313  ++m_Iterator;
314  NextValidLine();
315  }
316  return *this;
317  }
318 
320  {
321  ConstIndexIterator tmp = *this;
322  ++(*this);
323  return tmp;
324  }
325 
326  bool operator==(const ConstIndexIterator & iter) const
327  {
328  return m_Index == iter.m_Index && m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
329  }
330 
331  bool operator!=(const ConstIndexIterator & iter) const
332  {
333  return !( *this == iter );
334  }
335 
336  void GoToBegin()
337  {
338  m_Iterator = m_Begin;
339  m_Index.Fill(0);
340  NextValidLine();
341  }
342 
343  bool IsAtEnd() const
344  {
345  return m_Iterator == m_End;
346  }
347 
348  private:
349 
350  using LineContainerType = typename std::deque< LineType >;
351  using InternalIteratorType = typename LineContainerType::const_iterator;
353  {
354  // search for the next valid position
355  while( m_Iterator != m_End && m_Iterator->GetLength() == 0 )
356  {
357  ++m_Iterator;
358  }
359  if( m_Iterator != m_End )
360  {
361  m_Index = m_Iterator->GetIndex();
362  }
363  }
364 
369  };
370 
371 protected:
372  LabelObject();
373  void PrintSelf(std::ostream & os, Indent indent) const override;
374 
375 private:
376  using LineContainerType = typename std::deque< LineType >;
377 
380 };
381 } // end namespace itk
382 
383 #ifndef ITK_MANUAL_INSTANTIATION
384 #include "itkLabelObject.hxx"
385 #endif
386 
387 #endif
Light weight base class for most itk classes.
bool operator==(const ConstLineIterator &iter) const
unsigned long SizeValueType
Definition: itkIntTypes.h:83
unsigned int AttributeType
bool operator==(const ConstIndexIterator &iter) const
bool operator!=(const ConstIndexIterator &iter) const
Implements a weak reference to an object.
const IndexType & GetIndex() const
LineContainerType m_LineContainer
typename std::deque< LineType > LineContainerType
itk::SizeValueType SizeValueType
const LineType & GetLine() const
typename LineContainerType::const_iterator InternalIteratorType
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image...
Definition: itkOffset.h:67
ConstIndexIterator & operator=(const ConstIndexIterator &iter)
typename std::deque< LineType > LineContainerType
typename LineType::LengthType LengthType
ConstLineIterator operator++(int)
ConstIndexIterator(const ConstIndexIterator &iter)
ConstIndexIterator operator++(int)
bool operator!=(const ConstLineIterator &iter) const
The base class for the representation of an labeled binary object in an image.
ConstLineIterator(const ConstLineIterator &iter)
Control indentation during Print() invocation.
Definition: itkIndent.h:49
typename LineContainerType::const_iterator InternalIteratorType
signed long OffsetValueType
Definition: itkIntTypes.h:94
typename std::deque< LineType > LineContainerType
A forward iterator over the lines of a LabelObject.
ConstLineIterator & operator=(const ConstLineIterator &iter)