]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Image.hxx
640e01c3cf05a8a5a22b69a21c9c077bebc54f56
[cpPlugins.git] / lib / cpPlugins / Interface / Image.hxx
1 #ifndef __CPPLUGINS__INTERFACE__IMAGE__HXX__
2 #define __CPPLUGINS__INTERFACE__IMAGE__HXX__
3
4 #include <cpPlugins/Interface/Macros.h>
5
6 #include <itkImage.h>
7 #include <itkImageToVTKImageFilter.h>
8
9 #include <itkCovariantVector.h>
10 #include <itkDiffusionTensor3D.h>
11 #include <itkPoint.h>
12 #include <itkRGBPixel.h>
13 #include <itkRGBAPixel.h>
14 #include <itkSymmetricSecondRankTensor.h>
15 #include <itkVector.h>
16
17 // -------------------------------------------------------------------------
18 #define cpPlugins_Image_Demangle( TI, T, D, o )                 \
19   if( typeid( typename TI::PixelType ) == typeid( T ) )         \
20     this->_ITK_2_VTK< T, D >( o )
21
22 // -------------------------------------------------------------------------
23 #define cpPlugins_ImageArray_Demangle( TI, T, P, DP, DI, o )           \
24   if( typeid( typename TI::PixelType ) == typeid( T< P, DP > ) )       \
25     this->_ITK_2_VTK< T< P, DP >, DI >( o )
26
27 // -------------------------------------------------------------------------
28 #define cpPlugins_Image_Demangle_Dimension( TI, D, o )                  \
29   cpPlugins_Image_Demangle( TI, char, D, o );                           \
30   else cpPlugins_Image_Demangle( TI, short, D, o );                     \
31   else cpPlugins_Image_Demangle( TI, int, D, o );                       \
32   else cpPlugins_Image_Demangle( TI, long, D, o );                      \
33   else cpPlugins_Image_Demangle( TI, float, D, o );                     \
34   else cpPlugins_Image_Demangle( TI, double, D, o );                    \
35   else cpPlugins_Image_Demangle( TI, unsigned char, D, o );             \
36   else cpPlugins_Image_Demangle( TI, unsigned short, D, o );            \
37   else cpPlugins_Image_Demangle( TI, unsigned int, D, o );              \
38   else cpPlugins_Image_Demangle( TI, unsigned long, D, o );             \
39   else cpPlugins_Image_Demangle( TI, itk::RGBPixel< char >, D, o );     \
40   else cpPlugins_Image_Demangle( TI, itk::RGBPixel< short >, D, o );    \
41   else cpPlugins_Image_Demangle(                                        \
42     TI, itk::RGBPixel< unsigned char >, D, o                            \
43     );                                                                  \
44   else cpPlugins_Image_Demangle(                                        \
45     TI, itk::RGBPixel< unsigned short >, D, o                           \
46     );                                                                  \
47   else cpPlugins_Image_Demangle( TI, itk::RGBAPixel< char >, D, o );    \
48   else cpPlugins_Image_Demangle( TI, itk::RGBAPixel< short >, D, o );   \
49   else cpPlugins_Image_Demangle(                                        \
50     TI, itk::RGBAPixel< unsigned char >, D, o                           \
51     );                                                                  \
52   else cpPlugins_Image_Demangle(                                        \
53     TI, itk::RGBAPixel< unsigned short >, D, o                          \
54     );                                                                  \
55   else cpPlugins_ImageArray_Demangle(                                   \
56     TI, itk::Vector, float, D, D, o                                     \
57     );                                                                  \
58   else cpPlugins_ImageArray_Demangle(                                   \
59     TI, itk::Vector, double, D, D, o                                    \
60     );                                                                  \
61   else cpPlugins_ImageArray_Demangle(                                   \
62     TI, itk::CovariantVector, float, D, D, o                            \
63     );                                                                  \
64   else cpPlugins_ImageArray_Demangle(                                   \
65     TI, itk::CovariantVector, double, D, D, o                           \
66     );                                                                  \
67   else cpPlugins_ImageArray_Demangle( TI, itk::Point, float, D, D, o ); \
68   else cpPlugins_ImageArray_Demangle(                                   \
69     TI, itk::Point, double, D, D, o                                     \
70     );                                                                  \
71   else cpPlugins_ImageArray_Demangle(                                   \
72     TI, itk::SymmetricSecondRankTensor, float, D, D, o                  \
73     );                                                                  \
74   else cpPlugins_ImageArray_Demangle(                                   \
75     TI, itk::SymmetricSecondRankTensor, double, D, D, o                 \
76     )
77
78 // -------------------------------------------------------------------------
79 #define cpPlugins_Image_Import( T, D )                                  \
80   cpPlugins_TEMPLATE_IMPORT(                                            \
81     2(class cpPlugins_Interface_EXPORT itk::Image< T, D >)              \
82     )
83
84 // -------------------------------------------------------------------------
85 #define cpPlugins_VTKImage_Import( T, D )                               \
86   cpPlugins_TEMPLATE_IMPORT(                                            \
87     2(                                                                  \
88       class cpPlugins_Interface_EXPORT                                  \
89       itk::ImageToVTKImageFilter< itk::Image< T, D > >                  \
90       )                                                                 \
91     )
92
93 // -------------------------------------------------------------------------
94 #define cpPlugins_ImageArray_Import( A, T, DA, DI )                     \
95   cpPlugins_TEMPLATE_IMPORT(                                            \
96     3(                                                                  \
97       class cpPlugins_Interface_EXPORT                                  \
98       itk::Image< itk::A< T, DA >, DI >                                 \
99       )                                                                 \
100     )
101
102 // -------------------------------------------------------------------------
103 #define cpPlugins_VTKImageArray_Import( A, T, DA, DI )                  \
104   cpPlugins_TEMPLATE_IMPORT(                                            \
105     3(                                                                  \
106       class cpPlugins_Interface_EXPORT                                  \
107       itk::ImageToVTKImageFilter< itk::Image< itk::A< T, DA >, DI > >   \
108       )                                                                 \
109     )
110
111 // -------------------------------------------------------------------------
112 #define cpPlugins_Image_Import_AllDimensions( T )       \
113   cpPlugins_Image_Import( T, 2 );                       \
114   cpPlugins_Image_Import( T, 3 );                       \
115   cpPlugins_Image_Import( T, 4 )
116
117 // -------------------------------------------------------------------------
118 #define cpPlugins_VTKImage_Import_AllDimensions( T )       \
119   cpPlugins_VTKImage_Import( T, 2 );                       \
120   cpPlugins_VTKImage_Import( T, 3 )
121
122 // -------------------------------------------------------------------------
123 #define cpPlugins_ImageArray_Import_AllDimensions( A, T )             \
124   cpPlugins_ImageArray_Import( A, T, 2, 2 );                          \
125   cpPlugins_ImageArray_Import( A, T, 3, 3 );                          \
126   cpPlugins_ImageArray_Import( A, T, 4, 4 )
127
128 // -------------------------------------------------------------------------
129 #define cpPlugins_VTKImageArray_Import_AllDimensions( A, T )            \
130   cpPlugins_VTKImageArray_Import( A, T, 2, 2 );                         \
131   cpPlugins_VTKImageArray_Import( A, T, 3, 3 )
132
133 // -------------------------------------------------------------------------
134 // ITK base clases
135 cpPlugins_Image_Import_AllDimensions( char );
136 cpPlugins_Image_Import_AllDimensions( short );
137 cpPlugins_Image_Import_AllDimensions( int );
138 cpPlugins_Image_Import_AllDimensions( long );
139 cpPlugins_Image_Import_AllDimensions( unsigned char );
140 cpPlugins_Image_Import_AllDimensions( unsigned short );
141 cpPlugins_Image_Import_AllDimensions( unsigned int );
142 cpPlugins_Image_Import_AllDimensions( unsigned long );
143 cpPlugins_Image_Import_AllDimensions( float );
144 cpPlugins_Image_Import_AllDimensions( double );
145
146 cpPlugins_Image_Import_AllDimensions( std::complex< float > );
147 cpPlugins_Image_Import_AllDimensions( std::complex< double > );
148
149 cpPlugins_Image_Import_AllDimensions( itk::RGBPixel< char > );
150 cpPlugins_Image_Import_AllDimensions( itk::RGBPixel< short > );
151 cpPlugins_Image_Import_AllDimensions( itk::RGBPixel< int > );
152 cpPlugins_Image_Import_AllDimensions( itk::RGBPixel< long > );
153 cpPlugins_Image_Import_AllDimensions( itk::RGBPixel< unsigned char > );
154 cpPlugins_Image_Import_AllDimensions( itk::RGBPixel< unsigned short > );
155 cpPlugins_Image_Import_AllDimensions( itk::RGBPixel< unsigned int > );
156 cpPlugins_Image_Import_AllDimensions( itk::RGBPixel< unsigned long > );
157
158 cpPlugins_Image_Import_AllDimensions( itk::RGBAPixel< char > );
159 cpPlugins_Image_Import_AllDimensions( itk::RGBAPixel< short > );
160 cpPlugins_Image_Import_AllDimensions( itk::RGBAPixel< int > );
161 cpPlugins_Image_Import_AllDimensions( itk::RGBAPixel< long > );
162 cpPlugins_Image_Import_AllDimensions( itk::RGBAPixel< unsigned char > );
163 cpPlugins_Image_Import_AllDimensions( itk::RGBAPixel< unsigned short > );
164 cpPlugins_Image_Import_AllDimensions( itk::RGBAPixel< unsigned int > );
165 cpPlugins_Image_Import_AllDimensions( itk::RGBAPixel< unsigned long > );
166
167 cpPlugins_ImageArray_Import_AllDimensions( Vector, float );
168 cpPlugins_ImageArray_Import_AllDimensions( Vector, double );
169
170 cpPlugins_ImageArray_Import_AllDimensions( CovariantVector, float );
171 cpPlugins_ImageArray_Import_AllDimensions( CovariantVector, double );
172
173 cpPlugins_ImageArray_Import_AllDimensions( Point, float );
174 cpPlugins_ImageArray_Import_AllDimensions( Point, double );
175
176 cpPlugins_ImageArray_Import_AllDimensions(
177   SymmetricSecondRankTensor, float
178   );
179 cpPlugins_ImageArray_Import_AllDimensions(
180   SymmetricSecondRankTensor, double
181   );
182
183 cpPlugins_Image_Import( itk::DiffusionTensor3D< float >, 3 );
184 cpPlugins_Image_Import( itk::DiffusionTensor3D< double >, 3 );
185 cpPlugins_Image_Import( itk::Offset< 2 >, 2 );
186 cpPlugins_Image_Import( itk::Offset< 3 >, 3 );
187 cpPlugins_Image_Import( itk::Offset< 4 >, 4 );
188
189 // -------------------------------------------------------------------------
190 // ITK<->VTK base clases
191 cpPlugins_VTKImage_Import_AllDimensions( char );
192 cpPlugins_VTKImage_Import_AllDimensions( short );
193 cpPlugins_VTKImage_Import_AllDimensions( int );
194 cpPlugins_VTKImage_Import_AllDimensions( long );
195 cpPlugins_VTKImage_Import_AllDimensions( unsigned char );
196 cpPlugins_VTKImage_Import_AllDimensions( unsigned short );
197 cpPlugins_VTKImage_Import_AllDimensions( unsigned int );
198 cpPlugins_VTKImage_Import_AllDimensions( unsigned long );
199 cpPlugins_VTKImage_Import_AllDimensions( float );
200 cpPlugins_VTKImage_Import_AllDimensions( double );
201
202 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBPixel< char > );
203 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBPixel< short > );
204 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBPixel< int > );
205 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBPixel< long > );
206 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBPixel< unsigned char > );
207 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBPixel< unsigned short > );
208 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBPixel< unsigned int > );
209 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBPixel< unsigned long > );
210
211 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBAPixel< char > );
212 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBAPixel< short > );
213 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBAPixel< int > );
214 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBAPixel< long > );
215 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBAPixel< unsigned char > );
216 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBAPixel< unsigned short > );
217 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBAPixel< unsigned int > );
218 cpPlugins_VTKImage_Import_AllDimensions( itk::RGBAPixel< unsigned long > );
219
220 cpPlugins_VTKImageArray_Import_AllDimensions( Vector, float );
221 cpPlugins_VTKImageArray_Import_AllDimensions( Vector, double );
222
223 cpPlugins_VTKImageArray_Import_AllDimensions( CovariantVector, float );
224 cpPlugins_VTKImageArray_Import_AllDimensions( CovariantVector, double );
225
226 cpPlugins_VTKImageArray_Import_AllDimensions( Point, float );
227 cpPlugins_VTKImageArray_Import_AllDimensions( Point, double );
228
229 cpPlugins_VTKImageArray_Import_AllDimensions(
230   SymmetricSecondRankTensor, float
231   );
232 cpPlugins_VTKImageArray_Import_AllDimensions(
233   SymmetricSecondRankTensor, double
234   );
235
236 cpPlugins_VTKImage_Import( itk::DiffusionTensor3D< float >, 3 );
237 cpPlugins_VTKImage_Import( itk::DiffusionTensor3D< double >, 3 );
238
239 // -------------------------------------------------------------------------
240 template< class I >
241 void cpPlugins::Interface::Image::
242 SetITKImage( itk::DataObject* object )
243 {
244   // Check if input object has the desired type
245   I* image = dynamic_cast< I* >( object );
246   if( image == NULL )
247   {
248     this->m_ITKObject = NULL;
249     this->m_VTKObject = NULL;
250     this->m_ITKvVTKConnection = NULL;
251     this->Modified( );
252
253   } // fi
254
255   // Connect it to VTK
256   if( I::ImageDimension == 2 )
257   {
258     cpPlugins_Image_Demangle_Dimension( I, 2, object );
259     else
260     {
261       this->m_VTKObject = NULL;
262       this->m_ITKvVTKConnection = NULL;
263
264     } // fi
265   }
266   else if( I::ImageDimension == 3 )
267   {
268     cpPlugins_Image_Demangle_Dimension( I, 3, object );
269     else cpPlugins_Image_Demangle(
270       I, itk::DiffusionTensor3D< float >, 3, object
271       );
272     else cpPlugins_Image_Demangle(
273       I, itk::DiffusionTensor3D< double >, 3, object
274       );
275     else
276     {
277       this->m_VTKObject = NULL;
278       this->m_ITKvVTKConnection = NULL;
279
280     } // fi
281   }
282   else
283   {
284     this->m_VTKObject = NULL;
285     this->m_ITKvVTKConnection = NULL;
286
287   } // fi
288
289   // Keep objects
290   this->m_ITKObject = object;
291   this->Modified( );
292 }
293
294 // -------------------------------------------------------------------------
295 template< class I >
296 I* cpPlugins::Interface::Image::
297 GetITKImage( )
298 {
299   return( dynamic_cast< I* >( this->m_ITKObject.GetPointer( ) ) );
300 }
301
302 // -------------------------------------------------------------------------
303 template< class I >
304 const I* cpPlugins::Interface::Image::
305 GetITKImage( ) const
306 {
307   return( dynamic_cast< const I* >( this->m_ITKObject.GetPointer( ) ) );
308 }
309
310 // -------------------------------------------------------------------------
311 template< class P, unsigned int D >
312 void cpPlugins::Interface::Image::
313 _ITK_2_VTK( itk::DataObject* object )
314 {
315   typedef itk::Image< P, D > _I;
316
317   // Check if input object has the desired type
318   _I* image = dynamic_cast< _I* >( object );
319   if( image == NULL )
320     return;
321
322   // Connect it to VTK
323   typename itk::ImageToVTKImageFilter< _I >::Pointer f =
324     itk::ImageToVTKImageFilter< _I >::New( );
325   f->SetInput( image );
326   f->Update( );
327
328   // Keep objects
329   this->m_VTKObject = f->GetOutput( );
330   this->m_ITKvVTKConnection = f;
331 }
332
333 #endif // __CPPLUGINS__INTERFACE__IMAGE__HXX__
334
335 // eof - $RCSfile$