]> Creatis software - cpPlugins.git/blob - lib/cpInstances/Image.cxx
dc52b37a9e573f5315c70b5e19a6360d3e427377
[cpPlugins.git] / lib / cpInstances / Image.cxx
1 #include <cpInstances/Image.h>
2
3 #include <itkImageToVTKImageFilter.h>
4 #include <itkVTKImageToImageFilter.h>
5 #include <vtkImageData.h>
6 #include <itkDiffusionTensor3D.h>
7 #include <itkRGBPixel.h>
8 #include <itkRGBAPixel.h>
9
10 // -------------------------------------------------------------------------
11 void cpInstances::Image::
12 SetITK( itk::LightObject* o )
13 {
14   this->Superclass::SetITK( o );
15   this->m_VTK = NULL;
16   this->m_ITKvVTK = NULL;
17   this->Modified( );
18 }
19
20 // -------------------------------------------------------------------------
21 void cpInstances::Image::
22 SetVTK( vtkObjectBase* o )
23 {
24   this->Superclass::SetVTK( o );
25   this->m_ITK = NULL;
26   this->m_ITKvVTK = NULL;
27   this->Modified( );
28
29   /* TODO
30      vtkImageData* img = dynamic_cast< vtkImageData* >( o );
31      this->Superclass::SetVTK( img );
32      if( img == NULL )
33      {
34      this->m_ITK = NULL;
35      this->m_ITKvVTK = NULL;
36      this->Modified( );
37      return;
38
39      } // fi
40
41      bool success = false;
42      auto stype = img->GetScalarType( );
43      #ifdef cpPlugins_CONFIG_INTEGER_TYPES_char
44      if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< char >( img );
45      if( stype == VTK_UNSIGNED_CHAR ) success = this->_VTK_2_ITK_0< unsigned char >( img );
46      #endif // cpPlugins_CONFIG_INTEGER_TYPES_char
47      #ifdef cpPlugins_CONFIG_INTEGER_TYPES_short
48      if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< short >( img );
49      if( stype == VTK_UNSIGNED_CHAR ) success = this->_VTK_2_ITK_0< unsigned short >( img );
50      #endif // cpPlugins_CONFIG_INTEGER_TYPES_short
51      #ifdef cpPlugins_CONFIG_INTEGER_TYPES_int
52      if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< int >( img );
53      if( stype == VTK_UNSIGNED_CHAR ) success = this->_VTK_2_ITK_0< unsigned int >( img );
54      #endif // cpPlugins_CONFIG_INTEGER_TYPES_int
55      #ifdef cpPlugins_CONFIG_INTEGER_TYPES_long
56      if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< long >( img );
57      if( stype == VTK_UNSIGNED_CHAR ) success = this->_VTK_2_ITK_0< unsigned long >( img );
58      #endif // cpPlugins_CONFIG_INTEGER_TYPES_long
59      #ifdef cpPlugins_CONFIG_REAL_TYPES_float
60      if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< float >( img );
61      #endif // cpPlugins_CONFIG_REAL_TYPES_float
62      #ifdef cpPlugins_CONFIG_REAL_TYPES_double
63      if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< double >( img );
64      #endif // cpPlugins_CONFIG_REAL_TYPES_double
65   */
66   /* TODO
67      #define cpPlugins_CONFIG_COLOR_PIXELS_RGBPixel
68      #define cpPlugins_CONFIG_COLOR_PIXELS_RGBAPixel
69      #define cpPlugins_CONFIG_VECTORS_CovariantVector
70      #define cpPlugins_CONFIG_VECTORS_Point
71      #define cpPlugins_CONFIG_VECTORS_SymmetricSecondRankTensor
72      #define cpPlugins_CONFIG_VECTORS_Vector
73      #define cpPlugins_CONFIG_DIFFUSIONTENSORS_DiffusionTensor3D
74      #define cpPlugins_CONFIG_MATRICES_Matrix
75   */
76   /* TODO
77      if( !success )
78      {
79      this->m_ITK = NULL;
80      this->m_ITKvVTK = NULL;
81
82      } // fi
83      this->Modified( );
84   */
85 }
86
87 // -------------------------------------------------------------------------
88 cpInstances::Image::
89 Image( )
90   : Superclass( )
91 {
92 }
93
94 // -------------------------------------------------------------------------
95 cpInstances::Image::
96 ~Image( )
97 {
98 }
99
100 // -------------------------------------------------------------------------
101 void cpInstances::Image::
102 _UpdateITK( ) const
103 {
104   // TODO: std::cout << "Create ITK representation." << std::endl;
105 }
106
107 // -------------------------------------------------------------------------
108 void cpInstances::Image::
109 _UpdateVTK( ) const
110 {
111   auto i = const_cast< itk::LightObject* >( this->m_ITK.GetPointer( ) );
112   auto v = const_cast< vtkObjectBase* >( this->m_VTK.GetPointer( ) );
113   if( i != NULL && v == NULL )
114   {
115     cpPlugins_Demangle_Image_VisualDims_1( i, _ITK_2_VTK_0 )
116       cpPlugins_Demangle_Image_DiffTensors3D_1( i, _ITK_2_VTK_1 );
117
118   } // fi
119 }
120
121 // -------------------------------------------------------------------------
122 template< class _TImage >
123 void cpInstances::Image::
124 _ITK_2_VTK_0( _TImage* image ) const
125 {
126   static const unsigned int d = _TImage::ImageDimension;
127   cpPlugins_Demangle_Image_ScalarPixels_1( image, _ITK_2_VTK_1, d )
128     cpPlugins_Demangle_Image_ColorPixels_1( image, _ITK_2_VTK_1, d )
129     cpPlugins_Demangle_Image_VectorPixels_1( image, _ITK_2_VTK_1, d );
130 }
131
132 // -------------------------------------------------------------------------
133 template< class _TImage >
134 void cpInstances::Image::
135 _ITK_2_VTK_1( _TImage* image ) const
136 {
137   typedef itk::ImageToVTKImageFilter< _TImage > _TFilter;
138
139   Self* self = const_cast< Self* >( this );
140   _TFilter* f = dynamic_cast< _TFilter* >( self->m_ITKvVTK.GetPointer( ) );
141   if( f == NULL )
142   {
143     typename _TFilter::Pointer nf = _TFilter::New( );
144     self->m_ITKvVTK = nf;
145     f = nf.GetPointer( );
146
147   } // fi
148   f->SetInput( image );
149   f->Update( );
150
151   // Keep object track
152   self->m_ITK = image;
153   self->m_VTK = f->GetOutput( );
154 }
155
156 // -------------------------------------------------------------------------
157 template< class _TPixel >
158 bool cpInstances::Image::
159 _VTK_2_ITK_0( vtkImageData* image )
160 {
161   bool success = false;
162   unsigned int dim = image->GetDataDimension( );
163 #ifdef cpPlugins_CONFIG_VISUAL_DIMENSIONS_2
164   if( dim == 2 ) success = this->_VTK_2_ITK_1< _TPixel, 2 >( image );
165 #endif // cpPlugins_CONFIG_VISUAL_DIMENSIONS_2
166 #ifdef cpPlugins_CONFIG_VISUAL_DIMENSIONS_3
167   if( dim == 3 ) success = this->_VTK_2_ITK_1< _TPixel, 3 >( image );
168 #endif // cpPlugins_CONFIG_VISUAL_DIMENSIONS_2
169   return( success );
170 }
171
172 // -------------------------------------------------------------------------
173 template< class _TPixel, unsigned int _VDim >
174 bool cpInstances::Image::
175 _VTK_2_ITK_1( vtkImageData* image )
176 {
177   typedef itk::Image< _TPixel, _VDim > _TImage;
178   typedef itk::VTKImageToImageFilter< _TImage > _TFilter;
179   _TFilter* f = dynamic_cast< _TFilter* >( this->m_ITKvVTK.GetPointer( ) );
180   if( f == NULL )
181   {
182     typename _TFilter::Pointer nf = _TFilter::New( );
183     this->m_ITKvVTK = nf;
184     f = nf.GetPointer( );
185
186   } // fi
187   f->SetInput( image );
188   f->Update( );
189
190   // Keep object track
191   this->m_VTK = image;
192   this->m_ITK = f->GetOutput( );
193   return( true );
194 }
195
196 // eof - $RCSfile$