1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================*/
18 #include <itkImageToVTKImageFilter.h>
19 #include <itkPixelTraits.h>
21 //--------------------------------------------------------------------
22 template<class TItkImageType>
23 void vvImage::AddItkImage(TItkImageType *input)
25 // Update input before conversion to enable exceptions thrown by the ITK pipeline.
26 // Otherwise, vtkImageImport catches the exception for us.
29 // Convert from ITK object to VTK object
30 mImageDimension = TItkImageType::ImageDimension;
31 typedef itk::ImageToVTKImageFilter <TItkImageType> ConverterType;
32 typename ConverterType::Pointer converter = ConverterType::New();
33 mItkToVtkConverters.push_back(dynamic_cast< itk::ProcessObject *>(converter.GetPointer()));
34 converter->SetInput(input);
36 mVtkImages.push_back( converter->GetOutput() );
38 // Account for direction in transform. The offset is already accounted for
39 // in the VTK image coordinates, no need to put it in the transform.
40 vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
42 for(unsigned int i=0; i<input->GetImageDimension(); i++) {
43 for(unsigned int j=0; j<input->GetImageDimension(); j++) {
44 #if VTK_MAJOR_VERSION <= 6
45 (*matrix)[i][j] = input->GetDirection()[i][j];
46 // Direction is used around the image origin in ITK
47 (*matrix)[i][3] -= (*matrix)[i][j] * input->GetOrigin()[j];
49 (*matrix).SetElement(i, j, input->GetDirection()[i][j]);
50 // Direction is used around the image origin in ITK
51 (*matrix).SetElement(i, 3, (*matrix).GetElement(i,3) - (*matrix).GetElement(i,j) * input->GetOrigin()[j]);
54 #if VTK_MAJOR_VERSION <= 6
55 (*matrix)[i][3] += input->GetOrigin()[i];
57 (*matrix).SetElement(i, 3, (*matrix).GetElement(i,3) + input->GetOrigin()[i]);
61 // GetDirection provides the forward transform, vtkImageReslice wants the inverse
64 mTransform.push_back(vtkSmartPointer<vtkTransform>::New());
65 mTransform.back()->SetMatrix(matrix);
67 mDictionary.push_back(&(input->GetMetaDataDictionary()));
69 //--------------------------------------------------------------------
71 /** Dispatch the computation of scalar range between vector and scalar image */
72 template<class TPixelType, unsigned int VImageDimension>
73 void vvImage::ComputeScalarRangeBase(itk::Image<TPixelType,VImageDimension> *input)
75 itkStaticConstMacro(Dimension1, unsigned int, itk::PixelTraits< TPixelType >::Dimension);
76 ComputeScalarRange(DimensionDispatch< Dimension1 >(), input);
79 //--------------------------------------------------------------------
80 /** Compute the scalar range for a vector pixel type */
82 template<class TPixelType, unsigned int VImageDimension>
83 void vvImage::ComputeScalarRange(DimensionDispatchBase, itk::Image<TPixelType,VImageDimension> *input)
87 //--------------------------------------------------------------------
88 /** Compute the scalar range for a scalar pixel type */
89 template<class TPixelType, unsigned int VImageDimension>
90 void vvImage::ComputeScalarRange(DimensionDispatch< 1 >, itk::Image<TPixelType,VImageDimension> *input)
92 typedef typename itk::Image<TPixelType,VImageDimension> TItkImageType;
93 typedef itk::MinimumMaximumImageCalculator <TItkImageType> ImageCalculatorFilterType;
95 typename ImageCalculatorFilterType::Pointer imageCalculatorFilter = ImageCalculatorFilterType::New ();
96 TPixelType tempMin, tempMax;
98 imageCalculatorFilter->SetImage(input);
99 imageCalculatorFilter->Compute();
100 tempMin= imageCalculatorFilter->GetMinimum();
101 tempMax= imageCalculatorFilter->GetMaximum();
103 tempRange[0] = (double) tempMin;
104 tempRange[1] = (double) tempMax;
106 if (tempRange[0] < mrange[0]) mrange[0]=tempRange[0];
107 if (tempRange[1] > mrange[1]) mrange[1]=tempRange[1];
109 //--------------------------------------------------------------------