DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkVectorImageRegionIteratorWithIndex.h
Go to the documentation of this file.
1 
12 #ifndef itkVectorImageRegionIteratorWithIndex_h
13 #define itkVectorImageRegionIteratorWithIndex_h
14 
15 #include "itkImageRegionConstIterator.h"
16 #include "itkImageRegionIteratorWithIndex.h"
17 #include "utlITKConceptChecking.h"
18 #include "itkVectorImage.h"
19 #include "itkVariableLengthVector.h"
20 #include "utlCoreMacro.h"
21 #include "utlITK.h"
22 
23 namespace itk
24 {
25 
62 template< typename TImage >
63 class VectorImageRegionIteratorWithIndex : public ImageRegionIteratorWithIndex< TImage >
64 {
65 public:
68 
73  itkStaticConstMacro(ImageIteratorDimension, unsigned int,
74  TImage::ImageDimension);
75 
77  typedef ImageRegionIteratorWithIndex< TImage > Superclass;
78 
79 
81  typedef typename Superclass::IndexType IndexType;
82  typedef typename Superclass::SizeType SizeType;
83  typedef typename Superclass::OffsetType OffsetType;
84  typedef typename Superclass::RegionType RegionType;
85  typedef typename Superclass::ImageType ImageType;
86  typedef typename Superclass::PixelContainer PixelContainer;
87  typedef typename Superclass::PixelContainerPointer PixelContainerPointer;
88  typedef typename Superclass::InternalPixelType InternalPixelType;
89  typedef typename Superclass::PixelType PixelType;
90  typedef typename Superclass::AccessorType AccessorType;
91 
92  typedef ImageRegion<TImage::ImageDimension+1> NDImageRegionType;
93  typedef ImageRegion<TImage::ImageDimension> VectorImageRegionType;
94 
95  typedef VariableLengthVector< InternalPixelType > PixelVectorType;
96 
97 #ifdef ITK_USE_CONCEPT_CHECKING
98  itkConceptMacro( SameTypeCheck,
99  ( itk::Concept::SameType< ImageType, VectorImage<InternalPixelType, ImageIteratorDimension> > ) );
100  itkConceptMacro( SameTypeCheck2,
101  ( itk::Concept::SameType< PixelType, PixelVectorType> ) );
102 #endif
103 
104 
108  {
109  m_VectorStride=0;
110  m_VectorSize=0;
113  }
114 
117 
120  VectorImageRegionIteratorWithIndex(const Self & it) : Superclass(it)
121  {
126  };
127 
130  VectorImageRegionIteratorWithIndex(TImage *ptr, const RegionType & regionInput, int vectorAxis=-1) //: Superclass(ptr, regionInput)
131  {
132  Initialize(ptr, regionInput, vectorAxis);
133  }
134 
135  // VectorImageRegionIteratorWithIndex(TImage *ptr, const NDImageRegionType & regionInput, int vectorAxis=-1) : Superclass(ptr, regionInput)
136  // {
137  // RegionType region;
138  // itk::CopyImageRegion(regionInput, region);
139  // Initialize(ptr, region, vectorAxis);
140  // }
141 
144  Self & operator=(const Self & it)
145  {
146  Superclass::operator=(it);
151  return *this;
152  };
153 
155  void SetVector(const PixelVectorType & value, const int offIndex=0) const
156  {
158  {
159  this->m_PixelAccessorFunctor.Set(*( const_cast< InternalPixelType * >( this->m_Position ) ), value);
160  }
161  else
162  {
163  int numberOfComponens = this->m_Image->GetNumberOfComponentsPerPixel();
164  InternalPixelType* p = const_cast<InternalPixelType*>(this->m_BeginBuffer + numberOfComponens*(this->m_Position-this->m_Begin));
165  int off = offIndex;
166  for ( int i = 0; i < m_VectorSize; i += 1 )
167  {
168  *(p+off) = value[i];
169  off += m_VectorStride;
170  }
171  }
172  }
173 
175  void GetVector(PixelVectorType& vec, const int offIndex=0) const
176  {
178  {
179  vec = this->m_PixelAccessorFunctor.Get( *this->m_Position );
180  }
181  else
182  {
183  vec.SetSize(m_VectorSize);
184  int numberOfComponens = this->m_Image->GetNumberOfComponentsPerPixel();
185  InternalPixelType* p = const_cast<InternalPixelType*>(this->m_BeginBuffer + numberOfComponens*(this->m_Position-this->m_Begin));
186  int off = offIndex;
187  for ( int i = 0; i < m_VectorSize; i += 1 )
188  {
189  vec[i] = *(p+off);
190  off += m_VectorStride;
191  }
192  }
193  }
194 
195 
196 
197 protected:
198 
199  void Initialize(TImage *ptr, const RegionType & regionInput, int vectorAxis=-1)
200  {
201  // -1 means t axis
202  m_VectorAxis = (vectorAxis==-1) ? ImageIteratorDimension : vectorAxis;
203  itkAssertOrThrowMacro((m_VectorAxis<=(int)ImageIteratorDimension && m_VectorAxis>=0), "wrong vector axis, m_VectorAxis=" << m_VectorAxis << ", ImageIteratorDimension=" << ImageIteratorDimension);
204 
205  this->m_Image = ptr;
206  const InternalPixelType *buffer = this->m_Image->GetBufferPointer();
207 
209  {
210  // Superclass(ptr, regionInput);
211 
212  this->m_BeginIndex = regionInput.GetIndex();
213  this->m_PositionIndex = this->m_BeginIndex;
214  this->m_Region = regionInput;
215 
216  if ( regionInput.GetNumberOfPixels() > 0 ) // If region is non-empty
217  {
218  const RegionType & bufferedRegion = this->m_Image->GetBufferedRegion();
219  itkAssertOrThrowMacro( ( bufferedRegion.IsInside(this->m_Region) ),
220  "Region " << this->m_Region << " is outside of buffered region " << bufferedRegion );
221  }
222 
223  std::copy(this->m_Image->GetOffsetTable(),
224  this->m_Image->GetOffsetTable()+Superclass::ImageDimension + 1 ,
225  this->m_OffsetTable);
226 
227  // Compute the start position
228  OffsetValueType offs = this->m_Image->ComputeOffset(this->m_BeginIndex);
229  this->m_Begin = buffer + offs;
230  this->m_Position = this->m_Begin;
231 
232  // Compute the end offset
233  this->m_Remaining = false;
234  IndexType pastEnd;
235  for ( unsigned int i = 0; i < Superclass::ImageDimension; ++i )
236  {
237  SizeValueType size = regionInput.GetSize()[i];
238  if ( size > 0 )
239  {
240  this->m_Remaining = true;
241  }
242  this->m_EndIndex[i] = this->m_BeginIndex[i] + static_cast< OffsetValueType >( size );
243  pastEnd[i] = this->m_BeginIndex[i] + static_cast< OffsetValueType >( size ) - 1;
244  }
245  this->m_End = buffer + this->m_Image->ComputeOffset(pastEnd);
246 
247  this->m_PixelAccessor = this->m_Image->GetPixelAccessor();
248  this->m_PixelAccessorFunctor.SetPixelAccessor(this->m_PixelAccessor);
249  this->m_PixelAccessorFunctor.SetBegin(buffer);
250 
251  this->GoToBegin();
252 
253 
254  m_VectorSize = ptr->GetNumberOfComponentsPerPixel();
255  m_VectorStride = 1;
256  m_BeginBuffer = buffer + offs;
257  }
258  else
259  {
260 
261  typename RegionType::SizeType sizeInput = regionInput.GetSize();
262  typename RegionType::IndexType indexInput = regionInput.GetIndex();
263  sizeInput[m_VectorAxis] = 1;
264  indexInput[m_VectorAxis] = 0;
265  RegionType region;
266  region.SetIndex(indexInput);
267  region.SetSize(sizeInput);
268 
269 
270  this->m_BeginIndex = region.GetIndex();
271  this->m_PositionIndex = this->m_BeginIndex;
272  this->m_Region = region;
273 
274  if ( region.GetNumberOfPixels() > 0 ) // If region is non-empty
275  {
276  const RegionType & bufferedRegion = this->m_Image->GetBufferedRegion();
277  itkAssertOrThrowMacro( ( bufferedRegion.IsInside(this->m_Region) ),
278  "Region " << this->m_Region << " is outside of buffered region " << bufferedRegion );
279  }
280 
281  std::copy(this->m_Image->GetOffsetTable(),
282  this->m_Image->GetOffsetTable()+Superclass::ImageDimension + 1 ,
283  this->m_OffsetTable);
284 
285  // Compute the start position
286  OffsetValueType offs = this->m_Image->ComputeOffset(this->m_BeginIndex);
287  this->m_Begin = buffer + offs;
288  this->m_Position = this->m_Begin;
289 
290  // Compute the end offset
291  this->m_Remaining = false;
292  IndexType pastEnd;
293  for ( unsigned int i = 0; i < Superclass::ImageDimension; ++i )
294  {
295  if (i!=m_VectorAxis)
296  {
297  SizeValueType size = region.GetSize()[i];
298  if ( size > 0 )
299  {
300  this->m_Remaining = true;
301  }
302  this->m_EndIndex[i] = this->m_BeginIndex[i] + static_cast< OffsetValueType >( size );
303  pastEnd[i] = this->m_BeginIndex[i] + static_cast< OffsetValueType >( size ) - 1;
304  }
305  else
306  {
307  this->m_EndIndex[m_VectorAxis] = 0;
308  pastEnd[i] = 0;
309  this->m_Remaining = true;
310  }
311  }
312  this->m_End = buffer + this->m_Image->ComputeOffset(pastEnd);
313 
314  this->m_PixelAccessor = this->m_Image->GetPixelAccessor();
315  this->m_PixelAccessorFunctor.SetPixelAccessor(this->m_PixelAccessor);
316  this->m_PixelAccessorFunctor.SetBegin(buffer);
317 
318  this->GoToBegin();
319 
320  if (ptr)
321  {
322  SizeType size = ptr->GetLargestPossibleRegion().GetSize();
323  const typename ImageType::OffsetValueType* offsetTable = ptr->GetOffsetTable();
324  m_VectorStride = ptr->GetNumberOfComponentsPerPixel()*offsetTable[m_VectorAxis];
325  m_VectorSize = size[m_VectorAxis];
326  m_BeginBuffer = buffer + offs*m_VectorStride;
327  }
328  }
329  }
330 
333  VectorImageRegionIteratorWithIndex(const ImageRegionIteratorWithIndex< TImage > & it);
334  Self & operator=(const ImageRegionIteratorWithIndex< TImage > & it);
335 
336 protected:
337 
338  const InternalPixelType *m_BeginBuffer;
339 
341  OffsetValueType m_VectorStride;
346 };
347 
348 
350 template <typename TPixel, unsigned int VImageDimension>
351 class VectorImageRegionIteratorWithIndex<Image<TPixel, VImageDimension> > : public ImageRegionIteratorWithIndex<Image<TPixel, VImageDimension> >
352 {
353 public:
356 
357 
362  itkStaticConstMacro(ImageIteratorDimension, unsigned int,
363  VImageDimension);
364 
366  typedef ImageRegionIteratorWithIndex< Image<TPixel, VImageDimension> > Superclass;
367 
369  typedef typename Superclass::IndexType IndexType;
370  typedef typename Superclass::SizeType SizeType;
371  typedef typename Superclass::OffsetType OffsetType;
372  typedef typename Superclass::RegionType RegionType;
373  typedef typename Superclass::ImageType ImageType;
374  typedef typename Superclass::PixelContainer PixelContainer;
375  typedef typename Superclass::PixelContainerPointer PixelContainerPointer;
376  typedef typename Superclass::InternalPixelType InternalPixelType;
377  typedef typename Superclass::PixelType PixelType;
378  typedef typename Superclass::AccessorType AccessorType;
379 
380  typedef ImageRegion<VImageDimension> NDImageRegionType;
381  typedef ImageRegion<VImageDimension-1> VectorImageRegionType;
382 
383  typedef VariableLengthVector< InternalPixelType > PixelVectorType;
384 
385 #ifdef ITK_USE_CONCEPT_CHECKING
386  itkConceptMacro( SameTypeCheck,
387  ( itk::Concept::SameType< ImageType, Image<InternalPixelType, VImageDimension> > ) );
388 #endif
389 
393  {
394  m_VolumeSize=0;
395  m_VectorStride=0;
396  m_VectorSize=0;
398  }
399 
402 
405  VectorImageRegionIteratorWithIndex(const Self & it) : Superclass(it)
406  {
407  m_VolumeSize = it.m_VolumeSize;
411  };
412 
415  VectorImageRegionIteratorWithIndex(ImageType *ptr, const RegionType & regionInput, int vectorAxis=-1)
416  {
417  Initialize(ptr, regionInput, vectorAxis);
418  }
419 
420  // VectorImageRegionIteratorWithIndex(ImageType *ptr, const VectorImageRegionType & regionInput, int vectorAxis=-1)
421  // {
422  // RegionType region;
423  // itk::CopyImageRegion(regionInput, region);
424  // Initialize(ptr, region, vectorAxis);
425  // }
426 
429  Self & operator=(const Self & it)
430  {
431  Superclass::operator=(it);
432  m_VolumeSize = it.m_VolumeSize;
436  return *this;
437  };
438 
439 
441  void SetVector(const PixelVectorType & value, const int offIndex=0)
442  {
443  int off=offIndex*m_VolumeSize;
444  for ( int i = 0; i < m_VectorSize; ++i )
445  {
446  this->m_PixelAccessorFunctor.Set(
447  *( const_cast< InternalPixelType * >( this->m_Position + off) ), value[i]);
448  off += m_VectorStride;
449  }
450  }
451 
453  void GetVector(PixelVectorType& vec, const int offIndex=0) const
454  {
455  vec.SetSize(m_VectorSize);
456  int off=offIndex*m_VolumeSize;
457  for ( int i = 0; i < m_VectorSize; ++i )
458  {
459  vec[i] = this->m_PixelAccessorFunctor.Get( *( this->m_Position + off ) );
460  off += m_VectorStride;
461  }
462  }
463 
464 
465 protected:
466 
467  void Initialize(ImageType *ptr, const RegionType & regionInput, int vectorAxis=-1)
468  {
469 
470  this->m_Image = ptr;
471 
472  const InternalPixelType *buffer = this->m_Image->GetBufferPointer();
473 
474  m_VectorAxis = (vectorAxis==-1) ? Superclass::ImageDimension-1 : vectorAxis;
475  itkAssertOrThrowMacro((m_VectorAxis<=(int)ImageIteratorDimension && m_VectorAxis>=0), "wrong vector axis, m_VectorAxis=" << m_VectorAxis);
476 
477  typename RegionType::SizeType size4d = this->m_Image->GetLargestPossibleRegion().GetSize();
478  m_VolumeSize=1;
479  for ( int i = 0; i < VImageDimension-1; ++i )
480  m_VolumeSize *= size4d[i];
481 
482  typename RegionType::SizeType sizeInput = regionInput.GetSize();
483  typename RegionType::IndexType indexInput = regionInput.GetIndex();
484 
486  {
487  // set size as 1 in m_VectorAxis and the last t-axis
488  sizeInput[m_VectorAxis] = 1;
489  indexInput[m_VectorAxis] = 0;
490  sizeInput[ImageIteratorDimension-1] = 1;
491  indexInput[ImageIteratorDimension-1] = 0;
492  }
493  RegionType region;
494  region.SetIndex(indexInput);
495  region.SetSize(sizeInput);
496 
497 
498  this->m_BeginIndex = region.GetIndex();
499  this->m_PositionIndex = this->m_BeginIndex;
500  this->m_Region = region;
501 
502  if ( region.GetNumberOfPixels() > 0 ) // If region is non-empty
503  {
504  const RegionType & bufferedRegion = this->m_Image->GetBufferedRegion();
505  itkAssertOrThrowMacro( ( bufferedRegion.IsInside(this->m_Region) ),
506  "Region " << this->m_Region << " is outside of buffered region " << bufferedRegion );
507  }
508 
509  std::copy(this->m_Image->GetOffsetTable(),
510  this->m_Image->GetOffsetTable()+Superclass::ImageDimension + 1 ,
511  this->m_OffsetTable);
512 
513  // Compute the start position
514  OffsetValueType offs = this->m_Image->ComputeOffset(this->m_BeginIndex);
515  this->m_Begin = buffer + offs;
516  this->m_Position = this->m_Begin;
517 
518  // Compute the end offset
519  this->m_Remaining = false;
520  IndexType pastEnd;
521  for ( unsigned int i = 0; i < Superclass::ImageDimension; ++i )
522  {
523  if (i!=m_VectorAxis)
524  {
525  SizeValueType size = region.GetSize()[i];
526  if ( size > 0 )
527  {
528  this->m_Remaining = true;
529  }
530  this->m_EndIndex[i] = this->m_BeginIndex[i] + static_cast< OffsetValueType >( size );
531  pastEnd[i] = this->m_BeginIndex[i] + static_cast< OffsetValueType >( size ) - 1;
532  }
533  else
534  {
535  this->m_EndIndex[m_VectorAxis] = 0;
536  pastEnd[i] = 0;
537  this->m_Remaining = true;
538  }
539  }
540  this->m_End = buffer + this->m_Image->ComputeOffset(pastEnd);
541 
542  this->m_PixelAccessor = this->m_Image->GetPixelAccessor();
543  this->m_PixelAccessorFunctor.SetPixelAccessor(this->m_PixelAccessor);
544  this->m_PixelAccessorFunctor.SetBegin(buffer);
545 
546  this->GoToBegin();
547 
548  if (ptr)
549  {
550  SizeType size = ptr->GetLargestPossibleRegion().GetSize();
551  const typename ImageType::OffsetValueType* offsetTable = ptr->GetOffsetTable();
553  {
554  m_VectorStride = offsetTable[m_VectorAxis];
555  m_VectorSize = size[m_VectorAxis];
556  }
557  else
558  {
559  m_VectorStride = 0;
560  m_VectorSize = 1;
561  }
562  }
563 
564  }
565 
568  VectorImageRegionIteratorWithIndex(const ImageRegionIteratorWithIndex< ImageType > & it);
569  Self & operator=(const ImageRegionIteratorWithIndex< ImageType > & it);
570 
571 protected:
572 
573  OffsetValueType m_VolumeSize;
574  OffsetValueType m_VectorStride;
577 
578 };
579 
581 template <typename TPixel>
582 class VectorImageRegionIteratorWithIndex<Image<VariableLengthVector<TPixel>, 3 > > : public ImageRegionIteratorWithIndex<Image<VariableLengthVector<TPixel>,3> >
583 {
584 public:
586  typedef ImageRegionIteratorWithIndex< Image<VariableLengthVector<TPixel>, 3> > Superclass;
587 
589  typedef typename Superclass::IndexType IndexType;
590  typedef typename Superclass::SizeType SizeType;
591  typedef typename Superclass::OffsetType OffsetType;
592  typedef typename Superclass::RegionType RegionType;
593  typedef typename Superclass::ImageType ImageType;
594  typedef typename Superclass::PixelContainer PixelContainer;
595  typedef typename Superclass::PixelContainerPointer PixelContainerPointer;
596  typedef typename Superclass::InternalPixelType InternalPixelType;
597  typedef typename Superclass::PixelType PixelType;
598  typedef typename Superclass::AccessorType AccessorType;
599 
601  {}
602 
603  VectorImageRegionIteratorWithIndex(ImageType *ptr, const RegionType & region) : Superclass(ptr, region)
604  {
605  }
606 
607  VectorImageRegionIteratorWithIndex(const ImageIteratorWithIndex< ImageType > & it) : Superclass(it)
608  {
609  }
610 
611  void GetVector(PixelType& vec) const
612  {
613  vec = Superclass::Get();
614  }
615 
616  void SetVector(const PixelType & value)
617  {
618  Superclass::Set(value);
619  }
620 };
621 
622 
623 } // end namespace itk
624 
625 
626 #endif
VariableLengthVector< InternalPixelType > PixelVectorType
ImageRegion< TImage::ImageDimension > VectorImageRegionType
ImageRegion< TImage::ImageDimension+1 > NDImageRegionType
VectorImageRegionIteratorWithIndex(ImageType *ptr, const RegionType &regionInput, int vectorAxis=-1)
void Initialize(TImage *ptr, const RegionType &regionInput, int vectorAxis=-1)
void Initialize(ImageType *ptr, const RegionType &regionInput, int vectorAxis=-1)
ImageRegionIteratorWithIndex< Image< TPixel, VImageDimension > > Superclass
ImageRegionIteratorWithIndex< Image< VariableLengthVector< TPixel >, 3 > > Superclass
void GetVector(PixelVectorType &vec, const int offIndex=0) const
Created "06-10-2016.
macros for utlCore
A multi-dimensional iterator templated over image type. It provides the same interfaces for both itk:...
#define ITK_NULLPTR
Definition: utlITKMacro.h:55
void SetVector(const PixelVectorType &value, const int offIndex=0) const
VectorImageRegionIteratorWithIndex(TImage *ptr, const RegionType &regionInput, int vectorAxis=-1)