DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkImageToVTKImageDataFilter.hxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Image to VTK Image Data Converter
4 
5  Copyright (c) Pew-Thian Yap. All rights reserved.
6  See http://www.unc.edu/~ptyap/ for details.
7 
8  This software is distributed WITHOUT ANY WARRANTY; without even
9  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10  PURPOSE. See the above copyright notices for more information.
11 
12  =========================================================================*/
13 
14 #ifndef __itkImageToVTKImageDataFilter_hxx
15 #define __itkImageToVTKImageDataFilter_hxx
16 
18 
19 #include "vtkImageData.h"
20 #include "vtkDoubleArray.h"
21 #include "vtkPointData.h"
22 
23 namespace itk
24 {
25 
29 template <class TInputImage>
32 {
33  m_Image = NULL;
34 }
35 
39 template <class TInputImage>
40 vtkImageData *
42 ::GetOutput() const
43 {
44  return m_Image;
45 }
46 
50 template <class TInputImage>
51 void
53 ::SetInput( const InputImageType *image )
54 {
55  // Process object is not const-correct so the const_cast is required here
56  this->ProcessObject::SetNthInput( 0,
57  const_cast< InputImageType * >( image ) );
58 }
59 
63 template <class TInputImage>
66 ::GetInput() const
67 {
68  return itkDynamicCastInDebugMode< const TInputImage * >(this->GetPrimaryInput());
69 }
70 
74 template <class TInputImage>
75 void
78 {
79  // Get input image
80  InputImageConstPointer inputImage = this->GetInput();
81 
82  // Image information
83  InputSizeType inputSize = inputImage->GetLargestPossibleRegion().GetSize();
84  InputSpacingType inputSpacing = inputImage->GetSpacing();
85  InputPointType inputOrigin = inputImage->GetOrigin();
86  unsigned int numberOfComponentsPerPixel
87  = inputImage->GetNumberOfComponentsPerPixel();
88 
89  // Image data
90  m_Image = OutputImageType::New();
91 
92  int outputSize[Dimension];
93  double outputSpacing[Dimension];
94  double outputOrigin[Dimension];
95  vtkIdType numberOfVoxels = 1;
96 
97  for (unsigned int k=0; k<Dimension; k++)
98  {
99  outputSize[k] = inputSize[k];
100  outputSpacing[k] = inputSpacing[k];
101  outputOrigin[k] = inputOrigin[k];
102  numberOfVoxels *= outputSize[k];
103  }
104 
105  m_Image->SetDimensions(outputSize);
106  m_Image->SetSpacing(outputSpacing);
107  m_Image->SetOrigin(outputOrigin);
108 
109  // Data array
110  vtkSmartPointer<vtkDoubleArray> data =
111  vtkSmartPointer<vtkDoubleArray>::New();
112 
113  data->SetNumberOfComponents(numberOfComponentsPerPixel);
114  data->SetNumberOfTuples(numberOfVoxels);
115  data->SetName("scalars");
116 
117  // Iterator
119  inputIt(inputImage, inputImage->GetLargestPossibleRegion());
120 
121  inputIt.GoToBegin();
122 
123  // Populate data
124  InputPixelType inputPixel;
125  vtkIdType i = 0;
126  double dataEntry[numberOfComponentsPerPixel];
127 
128  while(!inputIt.IsAtEnd())
129  {
130  inputPixel = inputIt.Get();
131  for (unsigned int k = 0; k < numberOfComponentsPerPixel; k++)
132  {
133  dataEntry[k] = static_cast<double>( inputPixel[k] );
134  }
135  data->SetTuple(i, dataEntry);
136  ++inputIt;
137  i++;
138  }
139 
140  m_Image->GetPointData()->AddArray(data);
141  m_Image->GetPointData()->SetActiveScalars(data->GetName());
142 }
143 
144 } // end namespace itk
145 
146 #endif
virtual void SetInput(const InputImageType *image)
InputImageType::SpacingType InputSpacingType
const InputImageType * GetInput() const
InputImageType::ConstPointer InputImageConstPointer
ImageRegionConstIterator< InputImageType > InputImageIteratorType