ITK  4.2.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< class TLabel, unsigned int VImageDimension >
64 class ITK_EXPORT LabelObject:public LightObject
65 {
66 public:
68  typedef LabelObject Self;
74 
76  itkNewMacro(Self);
77 
79  itkTypeMacro(LabelObject, LightObject);
80 
81  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
82 
85  typedef TLabel LabelType;
87  typedef typename LineType::LengthType LengthType;
88  typedef unsigned int AttributeType;
90 
91  itkStaticConstMacro(LABEL, AttributeType, 0);
92 
93  static AttributeType GetAttributeFromName(const std::string & s);
94 
95  static std::string GetNameFromAttribute(const AttributeType & a);
96 
100  const LabelType & GetLabel() const;
101 
102  void SetLabel(const LabelType & label);
103 
108  bool HasIndex(const IndexType & idx) const;
109 
114  void AddIndex(const IndexType & idx);
115 
121  bool RemoveIndex(const IndexType & idx );
122 
126  void AddLine(const IndexType & idx, const LengthType & length);
127 
131  void AddLine(const LineType & line);
132 
133  SizeValueType GetNumberOfLines() const;
134 
135  const LineType & GetLine(SizeValueType i) const;
136 
137  LineType & GetLine(SizeValueType i);
138 
146  SizeValueType Size() const;
147 
152  bool Empty() const;
153 
154  void Clear();
155 
160  IndexType GetIndex(SizeValueType i) const;
161 
163  virtual void CopyAttributesFrom(const Self *src);
164 
166  void CopyAllFrom(const Self *src);
167 
171  void Optimize();
172 
174  void Shift( OffsetType offset );
175 
181  {
182  public:
183 
185 
187  {
188  m_Begin = lo->m_LineContainer.begin();
189  m_End = lo->m_LineContainer.end();
190  m_Iterator = m_Begin;
191  }
192 
194  {
195  m_Iterator = iter.m_Iterator;
196  m_Begin = iter.m_Begin;
197  m_End = iter.m_End;
198  }
199 
200  ConstLineIterator & operator=(const ConstLineIterator & iter)
201  {
202  m_Iterator = iter.m_Iterator;
203  m_Begin = iter.m_Begin;
204  m_End = iter.m_End;
205  return *this;
206  }
207 
208  const LineType & GetLine() const
209  {
210  return *m_Iterator;
211  }
212 
213  ConstLineIterator operator++(int)
214  {
215  ConstLineIterator tmp = *this;
216  ++(*this);
217  return tmp;
218  }
219 
220  ConstLineIterator & operator++()
221  {
222  ++m_Iterator;
223  return *this;
224  }
225 
226  bool operator==(const ConstLineIterator & iter) const
227  {
228  return m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
229  }
230 
231  bool operator!=(const ConstLineIterator & iter) const
232  {
233  return !( *this == iter );
234  }
235 
236  void GoToBegin()
237  {
238  m_Iterator = m_Begin;
239  }
240 
241  bool IsAtEnd() const
242  {
243  return m_Iterator == m_End;
244  }
245 
246  private:
247  typedef typename std::deque< LineType > LineContainerType;
248  typedef typename LineContainerType::const_iterator InternalIteratorType;
252  };
253 
259  {
260  public:
261 
263  m_Iterator(),
264  m_Begin(),
265  m_End()
266  {
267  m_Index.Fill(0);
268  }
269 
271  {
272  m_Begin = lo->m_LineContainer.begin();
273  m_End = lo->m_LineContainer.end();
274  GoToBegin();
275  }
276 
278  {
279  m_Iterator = iter.m_Iterator;
280  m_Index = iter.m_Index;
281  m_Begin = iter.m_Begin;
282  m_End = iter.m_End;
283  }
284 
285  ConstIndexIterator & operator=(const ConstIndexIterator & iter)
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  return *this;
292  }
293 
294  const IndexType & GetIndex() const
295  {
296  return m_Index;
297  }
298 
299  ConstIndexIterator & operator++()
300  {
301  m_Index[0]++;
302  if( m_Index[0] >= m_Iterator->GetIndex()[0] + (OffsetValueType)m_Iterator->GetLength() )
303  {
304  // we've reached the end of the line - go to the next one
305  ++m_Iterator;
306  NextValidLine();
307  }
308  return *this;
309  }
310 
311  ConstIndexIterator operator++(int)
312  {
313  ConstIndexIterator tmp = *this;
314  ++(*this);
315  return tmp;
316  }
317 
318  bool operator==(const ConstIndexIterator & iter) const
319  {
320  return m_Index == iter.m_Index && m_Iterator == iter.m_Iterator && m_Begin == iter.m_Begin && m_End == iter.m_End;
321  }
322 
323  bool operator!=(const ConstIndexIterator & iter) const
324  {
325  return !( *this == iter );
326  }
327 
328  void GoToBegin()
329  {
330  m_Iterator = m_Begin;
331  m_Index.Fill(0);
332  NextValidLine();
333  }
334 
335  bool IsAtEnd() const
336  {
337  return m_Iterator == m_End;
338  }
339 
340  private:
341 
342  typedef typename std::deque< LineType > LineContainerType;
343  typedef typename LineContainerType::const_iterator InternalIteratorType;
344  void NextValidLine()
345  {
346  // search for the next valid position
347  while( m_Iterator != m_End && m_Iterator->GetLength() == 0 )
348  {
349  ++m_Iterator;
350  }
351  if( m_Iterator != m_End )
352  {
353  m_Index = m_Iterator->GetIndex();
354  }
355  }
356 
361  };
362 
363 protected:
364  LabelObject();
365  void PrintSelf(std::ostream & os, Indent indent) const;
366 
367 private:
368  LabelObject(const Self &); //purposely not implemented
369  void operator=(const Self &); //purposely not implemented
370 
371  typedef typename std::deque< LineType > LineContainerType;
372 
375 };
376 } // end namespace itk
377 
378 #ifndef ITK_MANUAL_INSTANTIATION
379 #include "itkLabelObject.hxx"
380 #endif
381 
382 #endif
383