]> Creatis software - cpPlugins.git/blob - lib/cpInstances/Image.cxx
debug
[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 #include <cpPlugins/BaseObjects/ProcessObject.h>
134
135 template< class _TImage >
136 void cpInstances::Image::
137 _ITK_2_VTK_1( _TImage* image ) const
138 {
139   typedef itk::ImageToVTKImageFilter< _TImage > _TFilter;
140
141   Self* self = const_cast< Self* >( this );
142   _TFilter* f = dynamic_cast< _TFilter* >( self->m_ITKvVTK.GetPointer( ) );
143   if( f == NULL )
144   {
145     typename _TFilter::Pointer nf = _TFilter::New( );
146     self->m_ITKvVTK = nf;
147     f = nf.GetPointer( );
148
149   } // fi
150
151   std::string name = "<noname>";
152   if( this->m_Source != NULL )
153     name = this->m_Source->GetName( );
154
155   std::cout << "HOLALALAL: " << name << " " << image << " " << f->GetInput( ) << std::endl;
156
157   f->SetInput( image );
158   f->Update( );
159
160   // Keep object track
161   self->m_ITK = image;
162   self->m_VTK = f->GetOutput( );
163 }
164
165 // -------------------------------------------------------------------------
166 template< class _TPixel >
167 bool cpInstances::Image::
168 _VTK_2_ITK_0( vtkImageData* image )
169 {
170   bool success = false;
171   unsigned int dim = image->GetDataDimension( );
172 #ifdef cpPlugins_CONFIG_VISUAL_DIMENSIONS_2
173   if( dim == 2 ) success = this->_VTK_2_ITK_1< _TPixel, 2 >( image );
174 #endif // cpPlugins_CONFIG_VISUAL_DIMENSIONS_2
175 #ifdef cpPlugins_CONFIG_VISUAL_DIMENSIONS_3
176   if( dim == 3 ) success = this->_VTK_2_ITK_1< _TPixel, 3 >( image );
177 #endif // cpPlugins_CONFIG_VISUAL_DIMENSIONS_2
178   return( success );
179 }
180
181 // -------------------------------------------------------------------------
182 template< class _TPixel, unsigned int _VDim >
183 bool cpInstances::Image::
184 _VTK_2_ITK_1( vtkImageData* image )
185 {
186   typedef itk::Image< _TPixel, _VDim > _TImage;
187   typedef itk::VTKImageToImageFilter< _TImage > _TFilter;
188   _TFilter* f = dynamic_cast< _TFilter* >( this->m_ITKvVTK.GetPointer( ) );
189   if( f == NULL )
190   {
191     typename _TFilter::Pointer nf = _TFilter::New( );
192     this->m_ITKvVTK = nf;
193     f = nf.GetPointer( );
194
195   } // fi
196   f->SetInput( image );
197   f->Update( );
198
199   // Keep object track
200   this->m_VTK = image;
201   this->m_ITK = f->GetOutput( );
202   return( true );
203 }
204
205 // eof - $RCSfile$