]> Creatis software - clitk.git/blob - common/vvImage.txx
ab518cb9095f60df17204d5d74e9614096fe8890
[clitk.git] / common / vvImage.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
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>
20
21 //--------------------------------------------------------------------
22 template<class TItkImageType>
23 void vvImage::AddItkImage(TItkImageType *input)
24 {
25   // Update input before conversion to enable exceptions thrown by the ITK pipeline.
26   // Otherwise, vtkImageImport catches the exception for us.
27   input->Update();
28   
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);
35   converter->Update();
36   mVtkImages.push_back( converter->GetOutput() );
37
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();
41   matrix->Identity();
42   for(unsigned int i=0; i<input->GetImageDimension(); i++) {
43     for(unsigned int j=0; j<input->GetImageDimension(); j++) {
44       (*matrix)[i][j] = input->GetDirection()[i][j];
45       // Direction is used around the image origin in ITK
46       (*matrix)[i][3] -= (*matrix)[i][j] * input->GetOrigin()[j];
47     }
48     (*matrix)[i][3] += input->GetOrigin()[i];
49   }
50
51   // GetDirection provides the forward transform, vtkImageReslice wants the inverse
52   matrix->Invert();
53
54   mTransform.push_back(vtkSmartPointer<vtkTransform>::New());
55   mTransform.back()->SetMatrix(matrix);
56   //META DATA
57   mDictionary.push_back(&(input->GetMetaDataDictionary()));
58 }
59 //--------------------------------------------------------------------
60  
61 /** Dispatch the computation of scalar range between vector and scalar image */
62 template<class TPixelType, unsigned int VImageDimension>
63 void vvImage::ComputeScalarRangeBase(itk::Image<TPixelType,VImageDimension> *input)
64
65  itkStaticConstMacro(Dimension1, unsigned int, itk::PixelTraits< TPixelType >::Dimension);
66  ComputeScalarRange(DimensionDispatch< Dimension1 >(), input);
67 }
68
69 //--------------------------------------------------------------------
70 /** Compute the scalar range for a vector pixel type */
71 /** TO DO*/
72 template<class TPixelType, unsigned int VImageDimension>
73 void vvImage::ComputeScalarRange(DimensionDispatchBase, itk::Image<TPixelType,VImageDimension> *input)
74 {
75      cout << "try" << endl;
76 }
77
78 //--------------------------------------------------------------------
79 /** Compute the scalar range for a scalar pixel type */
80 template<class TPixelType, unsigned int VImageDimension>
81 void vvImage::ComputeScalarRange(DimensionDispatch< 1 >, itk::Image<TPixelType,VImageDimension> *input)
82 {  
83   typedef typename itk::Image<TPixelType,VImageDimension> TItkImageType;
84   typedef itk::MinimumMaximumImageCalculator <TItkImageType> ImageCalculatorFilterType;
85   
86   typename ImageCalculatorFilterType::Pointer imageCalculatorFilter = ImageCalculatorFilterType::New ();
87   TPixelType tempMin, tempMax;
88   double tempRange[2];
89   imageCalculatorFilter->SetImage(input);
90   imageCalculatorFilter->Compute();
91   tempMin= imageCalculatorFilter->GetMinimum();
92   tempMax= imageCalculatorFilter->GetMaximum();
93
94   tempRange[0] = (double) tempMin;
95   tempRange[1] = (double) tempMax;
96
97   if (tempRange[0] < mrange[0]) mrange[0]=tempRange[0];
98   if (tempRange[1] > mrange[1]) mrange[1]=tempRange[1];
99 }
100 //--------------------------------------------------------------------
101