]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/BasicFilters/MacheteFilter.cxx
71b3af481495376306fb099683b047d7897e9858
[cpPlugins.git] / lib / cpPlugins / Plugins / BasicFilters / MacheteFilter.cxx
1 #include "MacheteFilter.h"
2
3 #include <cpPlugins/Interface/DataObject.h>
4 #include <cpPlugins/Interface/Image.h>
5 #include <cpPlugins/Interface/Mesh.h>
6
7 #include <cpExtensions/Interaction/ImageInteractorStyle.h>
8 #include <cpExtensions/DataStructures/InfinitePlaneSpatialObject.h>
9 #include <cpExtensions/Algorithms/SpatialObjectMaskImageFilter.h>
10
11 #include <itkPlaneSpatialObject.h>
12 #include <itkPoint.h>
13 #include <itkVector.h>
14
15 #include <vtkInteractorStyleSwitch.h>
16 #include <vtkPlaneWidget.h>
17 #include <vtkRenderWindowInteractor.h>
18
19 #ifdef cpPlugins_Interface_QT4
20 #include <QDialogButtonBox>
21
22 // -------------------------------------------------------------------------
23 cpPlugins::BasicFilters::MacheteFilter_Dialog::
24 MacheteFilter_Dialog(
25   QWidget* parent, MacheteFilter* filter, Qt::WindowFlags f
26   )
27   : QDialog( parent, f | Qt::WindowStaysOnTopHint ),
28     m_Filter( filter )
29 {
30   this->m_Title = new QLabel( this );
31   this->m_Title->setText( "Execute machete filter" );
32
33   this->m_MainLayout = new QGridLayout( this );
34   this->m_ToolsLayout = new QVBoxLayout( );
35   this->m_ToolsLayout->addWidget( this->m_Title );
36   this->m_MainLayout->addLayout( this->m_ToolsLayout, 0, 0, 1, 1 );
37
38   // Add buttons
39   QDialogButtonBox* bb = new QDialogButtonBox(
40     QDialogButtonBox::Cancel | QDialogButtonBox::Ok
41     );
42   QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
43   QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
44   this->m_ToolsLayout->addWidget( bb );
45 }
46
47 // -------------------------------------------------------------------------
48 cpPlugins::BasicFilters::MacheteFilter_Dialog::
49 ~MacheteFilter_Dialog( )
50 {
51   delete this->m_Title;
52   delete this->m_ToolsLayout;
53   delete this->m_MainLayout;
54 }
55
56 // -------------------------------------------------------------------------
57 void cpPlugins::BasicFilters::MacheteFilter_Dialog::
58 accept( )
59 {
60   // Get interactive widget
61   if( this->m_Filter == NULL )
62     return;
63   vtkPlaneWidget* wdg = this->m_Filter->m_PlaneWidget;
64   if( wdg == NULL )
65     return;
66
67   // Get/Set plane parameters
68   double center[ 3 ], normal[ 3 ];
69   wdg->GetCenter( center );
70   wdg->GetNormal( normal );
71
72   /* TODO
73      this->m_Filter->GetParameters( )->SetPoint( "PlaneCenter", 3, center );
74      this->m_Filter->GetParameters( )->SetVector( "PlaneNormal", 3, normal );
75   */
76
77   // Update filter
78   /* TODO
79      auto plugins = this->m_Filter->GetPlugins( );
80      if( plugins != NULL )
81      {
82      auto app = plugins->GetApplication( );
83      if( app != NULL )
84      app->UpdateActualFilter( );
85
86      } // fi
87   */
88 }
89
90 // -------------------------------------------------------------------------
91 void cpPlugins::BasicFilters::MacheteFilter_Dialog::
92 reject( )
93 {
94   /*
95   auto plugins = this->m_Filter->GetPlugins( );
96   if( plugins != NULL )
97     plugins->DeactivateFilter( );
98   */
99   this->Superclass::reject( );
100 }
101
102 #endif // cpPlugins_Interface_QT4
103
104 // -------------------------------------------------------------------------
105 cpPlugins::BasicFilters::MacheteFilter::
106 DialogResult cpPlugins::BasicFilters::MacheteFilter::
107 ExecConfigurationDialog( QWidget* parent )
108 {
109 #ifdef cpPlugins_Interface_QT4
110
111   typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
112
113   // Choose a valid 3D interactor
114   vtkRenderWindowInteractor* iren = NULL;
115   /* TODO
116   auto iIt = this->m_Interactors.begin( );
117   for( ; iIt != this->m_Interactors.end( ) && iren == NULL; ++iIt )
118   {
119     _TImageStyle* istyle =
120       dynamic_cast< _TImageStyle* >(
121         ( *iIt )->GetInteractorStyle( )
122         );
123     if( istyle == NULL )
124       iren = *iIt;
125     
126   } // rof
127   */
128   if( iren == NULL )
129     return( false );
130   
131   // Get bounding box
132   double bbox[ 6 ];
133   cpPlugins::Interface::Image* image =
134     this->GetInput< cpPlugins::Interface::Image >( "Input" );
135   bool input_found = false;
136   if( image != NULL )
137   {
138     image->GetVTK< vtkImageData >( )->GetBounds( bbox );
139     input_found = true;
140
141   } // fi
142   cpPlugins::Interface::Mesh* mesh =
143     this->GetInput< cpPlugins::Interface::Mesh >( "Input" );
144   if( mesh != NULL )
145   {
146     mesh->GetVTK< vtkPolyData >( )->GetBounds( bbox );
147     input_found = true;
148
149   } // fi
150   if( !input_found )
151     return( false );
152
153   // Create plane widget
154   if( this->m_PlaneWidget != NULL )
155     this->m_PlaneWidget->Delete( );
156   this->m_PlaneWidget = vtkPlaneWidget::New( );
157   this->m_PlaneWidget->NormalToXAxisOn( );
158   this->m_PlaneWidget->SetCenter(
159     ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
160     ( bbox[ 3 ] + bbox[ 2 ] ) * double( 0.5 ),
161     ( bbox[ 5 ] + bbox[ 4 ] ) * double( 0.5 )
162     );
163   this->m_PlaneWidget->SetOrigin(
164     ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
165     bbox[ 2 ],
166     bbox[ 4 ]
167     );
168   this->m_PlaneWidget->SetPoint1(
169     ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
170     bbox[ 3 ],
171     bbox[ 4 ]
172     );
173   this->m_PlaneWidget->SetPoint2(
174     ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
175     bbox[ 2 ],
176     bbox[ 5 ]
177     );
178   this->m_PlaneWidget->SetResolution( 15 );
179   this->m_PlaneWidget->SetRepresentationToWireframe( );
180   this->m_PlaneWidget->SetInteractor( iren );
181   this->m_PlaneWidget->PlaceWidget( );
182   this->m_PlaneWidget->On( );
183
184   this->m_Dialog = new MacheteFilter_Dialog( NULL, this );
185   this->m_Dialog->show( );
186
187   return( true );
188 #else // cpPlugins_Interface_QT4
189   return( false );
190 #endif // cpPlugins_Interface_QT4
191 }
192
193 // -------------------------------------------------------------------------
194 cpPlugins::BasicFilters::MacheteFilter::
195 MacheteFilter( )
196   : Superclass( ),
197     m_PlaneWidget( NULL )
198 {
199   this->_AddInput( "Input" );
200   this->_AddOutput< cpPlugins::Interface::DataObject >( "PositiveOutput" );
201   this->_AddOutput< cpPlugins::Interface::DataObject >( "NegativeOutput" );
202
203   /*
204   this->m_Parameters->ConfigureAsPoint( "PlaneCenter" );
205   this->m_Parameters->ConfigureAsVector( "PlaneNormal" );
206   */
207 }
208
209 // -------------------------------------------------------------------------
210 cpPlugins::BasicFilters::MacheteFilter::
211 ~MacheteFilter( )
212 {
213   if( this->m_PlaneWidget != NULL )
214     this->m_PlaneWidget->Delete( );
215 }
216
217 // -------------------------------------------------------------------------
218 std::string cpPlugins::BasicFilters::MacheteFilter::
219 _GenerateData( )
220 {
221   cpPlugins::Interface::Image* image =
222     this->GetInput< cpPlugins::Interface::Image >( "Input" );
223   if( image != NULL )
224     return( this->_FromImage( image ) );
225   cpPlugins::Interface::Mesh* mesh =
226     this->GetInput< cpPlugins::Interface::Mesh >( "Input" );
227   if( mesh == NULL )
228     return( this->_FromMesh( mesh ) );
229   return( "MacheteFilter: No valid input." );
230 }
231
232 // -------------------------------------------------------------------------
233 std::string cpPlugins::BasicFilters::MacheteFilter::
234 _FromImage( cpPlugins::Interface::Image* image )
235 {
236   itk::DataObject* itk_image = NULL;
237   std::string r = "";
238   cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _RealImage );
239   else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _RealImage );
240   else r = "MacheteFilter: Input image type not supported.";
241   return( r );
242 }
243
244 // -------------------------------------------------------------------------
245 std::string cpPlugins::BasicFilters::MacheteFilter::
246 _FromMesh( cpPlugins::Interface::Mesh* mesh )
247 {
248   return( "" );
249 }
250
251 // -------------------------------------------------------------------------
252 template< class I >
253 std::string cpPlugins::BasicFilters::MacheteFilter::
254 _RealImage( itk::DataObject* dobj )
255 {
256   typedef
257     cpExtensions::DataStructures::
258     InfinitePlaneSpatialObject< I::ImageDimension > _TPlane;
259   typedef
260     cpExtensions::Algorithms::
261     SpatialObjectMaskImageFilter< I, I > _TFilter;
262   typedef cpPlugins::Interface::DataObject _TObj;
263   typedef cpPlugins::Interface::Image      _TImage;
264   typedef typename _TPlane::PointType      _TPoint;
265   typedef typename _TPlane::VectorType     _TVector;
266   typedef typename I::PixelType            _TPixel;
267
268   I* image = dynamic_cast< I* >( dobj );
269
270   /*
271   _TPoint c = this->m_Parameters->GetPoint< _TPoint >(
272     "PlaneCenter", I::ImageDimension
273     );
274   _TVector n = this->m_Parameters->GetVector< _TVector >(
275     "PlaneNormal", I::ImageDimension
276     );
277   */
278   _TPoint c;
279   _TVector n;
280
281   typename _TPlane::Pointer plane = _TPlane::New( );
282   plane->SetCenter( c );
283   plane->SetNormal( n );
284
285   // Configure filter
286   _TFilter* filter = this->_CreateITK< _TFilter >( );
287   filter->SetInput( image );
288   filter->SetSpatialObject( plane );
289   filter->SetOutsideValue( _TPixel( 0 ) );
290   filter->Update( );
291
292   // Get output names
293   auto pos_name = this->GetOutput< _TObj >( "PositiveOutput" )->GetName( );
294   auto neg_name = this->GetOutput< _TObj >( "NegativeOutput" )->GetName( );
295
296   // Connect outputs (and correct their types and names)
297   _TImage* pos_out = this->GetOutput< _TImage >( "PositiveOutput" );
298   if( pos_out == NULL )
299   {
300     this->_AddOutput< _TImage >( "PositiveOutput" );
301     pos_out = this->GetOutput< _TImage >( "PositiveOutput" );
302     pos_out->SetName( pos_name );
303
304   } // fi
305   _TImage* neg_out = this->GetOutput< _TImage >( "NegativeOutput" );
306   if( neg_out == NULL )
307   {
308     this->_AddOutput< _TImage >( "NegativeOutput" );
309     neg_out = this->GetOutput< _TImage >( "NegativeOutput" );
310     neg_out->SetName( neg_name );
311
312   } // fi
313
314   // Assign outputs
315   pos_out->SetITK< I >( filter->GetPositiveOutput( ) );
316   neg_out->SetITK< I >( filter->GetNegativeOutput( ) );
317   return( "" );
318 }
319
320 // eof - $RCSfile$