DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkVectorImageRegionIterator.h
Go to the documentation of this file.
1 
11 #ifndef itkVectorImageRegionIterator_h
12 #define itkVectorImageRegionIterator_h
13 
14 #include "itkImageRegionConstIterator.h"
15 #include "itkImageRegionIterator.h"
16 #include "utlITKConceptChecking.h"
17 #include "itkVectorImage.h"
18 #include "itkVariableLengthVector.h"
19 
20 namespace itk
21 {
22 
33 template< typename TImage >
34 class VectorImageRegionIterator : public ImageRegionIterator< TImage >
35 {
36 public:
39 
44  itkStaticConstMacro(ImageIteratorDimension, unsigned int,
45  TImage::ImageDimension);
46 
48  typedef ImageRegionIterator< TImage > Superclass;
49 
50 
52  typedef typename Superclass::IndexType IndexType;
53  typedef typename Superclass::SizeType SizeType;
54  typedef typename Superclass::OffsetType OffsetType;
55  typedef typename Superclass::RegionType RegionType;
56  typedef typename Superclass::ImageType ImageType;
57  typedef typename Superclass::PixelContainer PixelContainer;
58  typedef typename Superclass::PixelContainerPointer PixelContainerPointer;
59  typedef typename Superclass::InternalPixelType InternalPixelType;
60  typedef typename Superclass::PixelType PixelType;
61  typedef typename Superclass::AccessorType AccessorType;
62 
63  typedef VariableLengthVector< InternalPixelType > PixelVectorType;
64 
65 #ifdef ITK_USE_CONCEPT_CHECKING
66  itkConceptMacro( SameTypeCheck,
67  ( itk::Concept::SameType< ImageType, VectorImage<InternalPixelType, ImageIteratorDimension> > ) );
68  itkConceptMacro( SameTypeCheck2,
69  ( itk::Concept::SameType< PixelType, PixelVectorType> ) );
70 #endif
71 
72 
75  VectorImageRegionIterator() : Superclass()
76  {
78  m_VectorSize=0;
80  }
81 
84 
87  VectorImageRegionIterator(const Self & it) : Superclass(it)
88  {
92  }
93 
96  VectorImageRegionIterator(TImage *ptr, const RegionType & region, int vectorAxis=-1) : Superclass(ptr, region)
97  {
98  // -1 means t axis
99  m_VectorAxis = (vectorAxis==-1) ? ImageIteratorDimension : vectorAxis;
100  itkAssertOrThrowMacro((m_VectorAxis<=(int)ImageIteratorDimension && m_VectorAxis>=0), "wrong vector axis, m_VectorAxis=" << m_VectorAxis << ", ImageIteratorDimension=" << ImageIteratorDimension);
101 
103  {
104  // Superclass(ptr, regionInput);
105  m_VectorSize = ptr->GetNumberOfComponentsPerPixel();
106  m_VectorStride = 1;
107  }
108  else
109  {
110  this->SetRegion(region);
111  typename RegionType::SizeType regionSize = this->m_Region.GetSize();
112  typename RegionType::IndexType regionIndex = this->m_Region.GetIndex();
113  if (ptr)
114  {
115  SizeType size = ptr->GetLargestPossibleRegion().GetSize();
116  const typename ImageType::OffsetValueType* offsetTable = ptr->GetOffsetTable();
117  m_VectorStride = ptr->GetNumberOfComponentsPerPixel()*offsetTable[m_VectorAxis];
118  m_VectorSize = size[m_VectorAxis];
119  }
120  }
121  }
122 
125  Self & operator=(const Self & it)
126  {
127  Superclass::operator=(it);
131  };
132 
134  void SetRegion(const RegionType & regionInput)
135  {
137  Superclass::SetRegion(regionInput);
138  else
139  {
140  typename RegionType::SizeType sizeInput = regionInput.GetSize();
141  typename RegionType::IndexType indexInput = regionInput.GetIndex();
142  sizeInput[m_VectorAxis] = 1;
143  indexInput[m_VectorAxis] = 0;
144  RegionType region;
145  this->m_Region.SetIndex(indexInput);
146  this->m_Region.SetSize(sizeInput);
147 
148  if ( region.GetNumberOfPixels() > 0 ) // If region is non-empty
149  {
150  const RegionType & bufferedRegion = this->m_Image->GetBufferedRegion();
151  itkAssertOrThrowMacro( ( bufferedRegion.IsInside(this->m_Region) ),
152  "Region " << this->m_Region << " is outside of buffered region " << bufferedRegion );
153  }
154 
155  // Compute the start offset
156  this->m_Offset = this->m_Image->ComputeOffset( this->m_Region.GetIndex() );
157  this->m_BeginOffset = this->m_Offset;
158 
159  // Compute the end offset. If any component of m_Region.GetSize()
160  // is zero, the region is not valid and we set the EndOffset
161  // to be same as BeginOffset so that iterator end condition is met
162  // immediately.
163  if ( this->m_Region.GetNumberOfPixels() == 0 )
164  {
165  // region is empty, probably has a size of 0 along one dimension
166  this->m_EndOffset = this->m_BeginOffset;
167  }
168  else
169  {
170  IndexType ind( this->m_Region.GetIndex() );
171  SizeType size( this->m_Region.GetSize() );
172  for ( unsigned int i = 0; i < ImageIteratorDimension; ++i )
173  {
174  if (i!=m_VectorAxis)
175  ind[i] += ( static_cast< IndexValueType >( size[i] ) - 1 );
176  else
177  ind[i] = 0;
178  }
179  this->m_EndOffset = this->m_Image->ComputeOffset(ind);
180  this->m_EndOffset++;
181  }
182  }
183  }
184 
186  void SetVector(const PixelVectorType & value, const int offIndex=0) const
187  {
189  {
190  this->m_PixelAccessorFunctor.Set(*( const_cast< InternalPixelType * >(
191  this->m_Buffer + this->m_Offset ) ), value);
192  }
193  else
194  {
195  int numberOfComponens = this->m_Image->GetNumberOfComponentsPerPixel();
196  InternalPixelType* p = const_cast<InternalPixelType*>(this->m_Buffer + numberOfComponens*this->m_Offset);
197  int off = offIndex;
198  for ( int i = 0; i < m_VectorSize; i += 1 )
199  {
200  *(p+off) = value[i];
201  off += m_VectorStride;
202  }
203  }
204  }
205 
207  void GetVector(PixelVectorType& vec, const int offIndex=0) const
208  {
210  {
211  vec = this->m_PixelAccessorFunctor.Get( *( this->m_Buffer + this->m_Offset ) );
212  }
213  else
214  {
215  vec.SetSize(m_VectorSize);
216  int numberOfComponens = this->m_Image->GetNumberOfComponentsPerPixel();
217  InternalPixelType* p = const_cast<InternalPixelType*>(this->m_Buffer + numberOfComponens*this->m_Offset);
218  int off = offIndex;
219  for ( int i = 0; i < m_VectorSize; i += 1 )
220  {
221  vec[i] = *(p+off);
222  off += m_VectorStride;
223  }
224  }
225  }
226 
227 
228 
229 protected:
230 
233  VectorImageRegionIterator(const ImageRegionIterator< TImage > & it);
234  Self & operator=(const ImageRegionIterator< TImage > & it);
235 
236 protected:
237 
238  OffsetValueType m_VectorStride;
241 };
242 
243 
244 template <typename TPixel, unsigned int VImageDimension>
245 class VectorImageRegionIterator<Image<TPixel, VImageDimension> > : public ImageRegionIterator<Image<TPixel, VImageDimension> >
246 {
247 public:
250 
251 
256  itkStaticConstMacro(ImageIteratorDimension, unsigned int,
257  VImageDimension);
258 
260  typedef ImageRegionIterator< Image<TPixel, VImageDimension> > Superclass;
261 
263  typedef typename Superclass::IndexType IndexType;
264  typedef typename Superclass::SizeType SizeType;
265  typedef typename Superclass::OffsetType OffsetType;
266  typedef typename Superclass::RegionType RegionType;
267  typedef typename Superclass::ImageType ImageType;
268  typedef typename Superclass::PixelContainer PixelContainer;
269  typedef typename Superclass::PixelContainerPointer PixelContainerPointer;
270  typedef typename Superclass::InternalPixelType InternalPixelType;
271  typedef typename Superclass::PixelType PixelType;
272  typedef typename Superclass::AccessorType AccessorType;
273 
274  typedef VariableLengthVector< InternalPixelType > PixelVectorType;
275 
276 #ifdef ITK_USE_CONCEPT_CHECKING
277  itkConceptMacro( SameTypeCheck,
278  ( itk::Concept::SameType< ImageType, Image<InternalPixelType, VImageDimension> > ) );
279 #endif
280 
283  VectorImageRegionIterator() : Superclass()
284  {
285  m_VolumeSize=0;
286  m_VectorStride=0;
287  m_VectorSize=0;
289  }
290 
293 
296  VectorImageRegionIterator(const Self & it) : Superclass(it)
297  {
298  m_VolumeSize = it.m_VolumeSize;
302  };
303 
306  VectorImageRegionIterator(ImageType *ptr, const RegionType & region, int vectorAxis=-1) : Superclass(ptr, region)
307  {
308  m_VectorAxis = (vectorAxis==-1) ? Superclass::ImageDimension-1 : vectorAxis;
309  itkAssertOrThrowMacro((m_VectorAxis<(int)ImageIteratorDimension && m_VectorAxis>=0), "wrong vector axis, m_VectorAxis=" << m_VectorAxis);
310 
311  this->SetRegion(region);
312  typename RegionType::SizeType regionSize = this->m_Region.GetSize();
313  typename RegionType::IndexType regionIndex = this->m_Region.GetIndex();
314  if (ptr)
315  {
316  SizeType size = ptr->GetLargestPossibleRegion().GetSize();
317  const typename ImageType::OffsetValueType* offsetTable = ptr->GetOffsetTable();
318  m_VectorStride = offsetTable[m_VectorAxis];
319  m_VectorSize = size[m_VectorAxis];
320  }
321  }
322 
325  Self & operator=(const Self & it)
326  {
327  Superclass::operator=(it);
328  m_VolumeSize = it.m_VolumeSize;
332  }
333 
335  void SetRegion(const RegionType & regionInput)
336  {
337  typename RegionType::SizeType sizeInput = regionInput.GetSize();
338  typename RegionType::IndexType indexInput = regionInput.GetIndex();
339  m_VolumeSize=1;
340  for ( int i = 0; i < VImageDimension-1; ++i )
341  m_VolumeSize *= sizeInput[i];
342 
343  sizeInput[m_VectorAxis] = 1;
344  indexInput[m_VectorAxis] = 0;
345  sizeInput[3] = 1;
346  indexInput[3] = 0;
347  this->m_Region.SetIndex(indexInput);
348  this->m_Region.SetSize(sizeInput);
349 
350  if ( this->m_Region.GetNumberOfPixels() > 0 ) // If region is non-empty
351  {
352  const RegionType & bufferedRegion = this->m_Image->GetBufferedRegion();
353  itkAssertOrThrowMacro( ( bufferedRegion.IsInside(this->m_Region) ),
354  "Region " << this->m_Region << " is outside of buffered region " << bufferedRegion );
355  }
356 
357  // Compute the start offset
358  this->m_Offset = this->m_Image->ComputeOffset( this->m_Region.GetIndex() );
359  this->m_BeginOffset = this->m_Offset;
360 
361  // Compute the end offset. If any component of m_Region.GetSize()
362  // is zero, the region is not valid and we set the EndOffset
363  // to be same as BeginOffset so that iterator end condition is met
364  // immediately.
365  if ( this->m_Region.GetNumberOfPixels() == 0 )
366  {
367  // region is empty, probably has a size of 0 along one dimension
368  this->m_EndOffset = this->m_BeginOffset;
369  }
370  else
371  {
372  IndexType ind( this->m_Region.GetIndex() );
373  SizeType size( this->m_Region.GetSize() );
374  for ( unsigned int i = 0; i < VImageDimension; ++i )
375  {
376  if (i!=m_VectorAxis)
377  ind[i] += ( static_cast< IndexValueType >( size[i] ) - 1 );
378  else
379  ind[i] = 0;
380  }
381  this->m_EndOffset = this->m_Image->ComputeOffset(ind);
382  this->m_EndOffset++;
383  }
384  }
385 
386 
388  void SetVector(const PixelVectorType & value, const int offIndex=0)
389  {
390  int off=offIndex*m_VolumeSize;
391  for ( int i = 0; i < m_VectorSize; ++i )
392  {
393  this->m_PixelAccessorFunctor.Set(
394  *( const_cast< InternalPixelType * >( this->m_Buffer ) + this->m_Offset + off), value[i]);
395  off += m_VectorStride;
396  }
397  }
398 
400  void GetVector(PixelVectorType& vec, const int offIndex=0) const
401  {
402  vec.SetSize(m_VectorSize);
403  int off=offIndex*m_VolumeSize;
404  for ( int i = 0; i < m_VectorSize; ++i )
405  {
406  vec[i] = this->m_PixelAccessorFunctor.Get( *( this->m_Buffer + this->m_Offset + off ) );
407  off += m_VectorStride;
408  }
409  }
410 
411 
412 protected:
413 
416  VectorImageRegionIterator(const ImageRegionIterator< ImageType > & it);
417  Self & operator=(const ImageRegionIterator< ImageType > & it);
418 
419 protected: //made protected so other iterators can access
420 
421  OffsetValueType m_VolumeSize;
422  OffsetValueType m_VectorStride;
425 
426 };
427 
428 
429 } // end namespace itk
430 
431 
432 #endif
433 
Superclass::InternalPixelType InternalPixelType
Superclass::PixelContainerPointer PixelContainerPointer
void SetVector(const PixelVectorType &value, const int offIndex=0) const
ImageRegionIterator< Image< TPixel, VImageDimension > > Superclass
A multi-dimensional iterator templated over image type.
VariableLengthVector< InternalPixelType > PixelVectorType
VectorImageRegionIterator(ImageType *ptr, const RegionType &region, int vectorAxis=-1)
VectorImageRegionIterator(TImage *ptr, const RegionType &region, int vectorAxis=-1)
void SetRegion(const RegionType &regionInput)
void SetVector(const PixelVectorType &value, const int offIndex=0)
void GetVector(PixelVectorType &vec, const int offIndex=0) const
void GetVector(PixelVectorType &vec, const int offIndex=0) const
Created "06-10-2016.
static const unsigned int ImageIteratorDimension
Superclass::PixelContainer PixelContainer
ImageRegionIterator< TImage > Superclass