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