]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/BasicFilters/MacheteFilter.cxx
...
[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   this->m_Filter->GetParameters( )->SetPoint( "PlaneCenter", 3, center );
75   this->m_Filter->GetParameters( )->SetVector( "PlaneNormal", 3, normal );
76
77   // Update filter
78   auto plugins = this->m_Filter->GetPlugins( );
79   if( plugins != NULL )
80   {
81     auto app = plugins->GetApplication( );
82     if( app != NULL )
83       app->UpdateActualFilter( );
84
85   } // fi
86 }
87
88 // -------------------------------------------------------------------------
89 void cpPlugins::BasicFilters::MacheteFilter_Dialog::
90 reject( )
91 {
92   std::cout << "reject" << std::endl;
93 }
94 #endif // cpPlugins_Interface_QT4
95
96 // -------------------------------------------------------------------------
97 cpPlugins::BasicFilters::MacheteFilter::
98 DialogResult cpPlugins::BasicFilters::MacheteFilter::
99 ExecConfigurationDialog( QWidget* parent )
100 {
101 #ifdef cpPlugins_Interface_QT4
102
103   typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
104
105   // Choose a valid 3D interactor
106   vtkRenderWindowInteractor* iren = NULL;
107   auto iIt = this->m_Interactors.begin( );
108   for( ; iIt != this->m_Interactors.end( ) && iren == NULL; ++iIt )
109   {
110     _TImageStyle* istyle =
111       dynamic_cast< _TImageStyle* >(
112         ( *iIt )->GetInteractorStyle( )
113         );
114     if( istyle == NULL )
115       iren = *iIt;
116     
117   } // rof
118   if( iren == NULL )
119     return( Self::DialogResult_Cancel );
120
121   // Get bounding box
122   double bbox[ 6 ];
123   cpPlugins::Interface::Image* image =
124     this->GetInput< cpPlugins::Interface::Image >( "Input" );
125   bool input_found = false;
126   if( image != NULL )
127   {
128     image->GetVTK< vtkImageData >( )->GetBounds( bbox );
129     input_found = true;
130
131   } // fi
132   cpPlugins::Interface::Mesh* mesh =
133     this->GetInput< cpPlugins::Interface::Mesh >( "Input" );
134   if( mesh != NULL )
135   {
136     mesh->GetVTK< vtkPolyData >( )->GetBounds( bbox );
137     input_found = true;
138
139   } // fi
140   if( !input_found )
141     return( Self::DialogResult_Cancel );
142
143   // Create plane widget
144   if( this->m_PlaneWidget != NULL )
145     this->m_PlaneWidget->Delete( );
146   this->m_PlaneWidget = vtkPlaneWidget::New( );
147   this->m_PlaneWidget->NormalToXAxisOn( );
148   this->m_PlaneWidget->SetCenter(
149     ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
150     ( bbox[ 3 ] + bbox[ 2 ] ) * double( 0.5 ),
151     ( bbox[ 5 ] + bbox[ 4 ] ) * double( 0.5 )
152     );
153   this->m_PlaneWidget->SetOrigin(
154     ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
155     bbox[ 2 ],
156     bbox[ 4 ]
157     );
158   this->m_PlaneWidget->SetPoint1(
159     ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
160     bbox[ 3 ],
161     bbox[ 4 ]
162     );
163   this->m_PlaneWidget->SetPoint2(
164     ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
165     bbox[ 2 ],
166     bbox[ 5 ]
167     );
168   this->m_PlaneWidget->SetResolution( 15 );
169   this->m_PlaneWidget->SetRepresentationToWireframe( );
170   this->m_PlaneWidget->SetInteractor( iren );
171   this->m_PlaneWidget->PlaceWidget( );
172   this->m_PlaneWidget->On( );
173
174   this->m_Dialog = new MacheteFilter_Dialog( NULL, this );
175   this->m_Dialog->show( );
176
177   return( Self::DialogResult_Modal );
178 #else // cpPlugins_Interface_QT4
179   return( Self::DialogResult_Cancel );
180 #endif // cpPlugins_Interface_QT4
181 }
182
183 // -------------------------------------------------------------------------
184 cpPlugins::BasicFilters::MacheteFilter::
185 MacheteFilter( )
186   : Superclass( ),
187     m_PlaneWidget( NULL )
188 {
189   this->_AddInput( "Input" );
190   this->_MakeOutput< cpPlugins::Interface::DataObject >( "Output" );
191
192   itk::Point< double, 3 > center;
193   itk::Vector< double, 3 > normal;
194
195   center.Fill( double( 0 ) );
196   normal.Fill( double( 0 ) );
197   normal[ 0 ] = double( 1 );
198
199   this->m_Parameters->ConfigureAsPoint( "PlaneCenter", 3, center );
200   this->m_Parameters->ConfigureAsVector( "PlaneNormal", 3, normal );
201 }
202
203 // -------------------------------------------------------------------------
204 cpPlugins::BasicFilters::MacheteFilter::
205 ~MacheteFilter( )
206 {
207   if( this->m_PlaneWidget != NULL )
208     this->m_PlaneWidget->Delete( );
209 }
210
211 // -------------------------------------------------------------------------
212 std::string cpPlugins::BasicFilters::MacheteFilter::
213 _GenerateData( )
214 {
215   cpPlugins::Interface::Image* image =
216     this->GetInput< cpPlugins::Interface::Image >( "Input" );
217   if( image != NULL )
218     return( this->_FromImage( image ) );
219   cpPlugins::Interface::Mesh* mesh =
220     this->GetInput< cpPlugins::Interface::Mesh >( "Input" );
221   if( mesh == NULL )
222     return( this->_FromMesh( mesh ) );
223   return( "MacheteFilter: No valid input." );
224 }
225
226 // -------------------------------------------------------------------------
227 std::string cpPlugins::BasicFilters::MacheteFilter::
228 _FromImage( cpPlugins::Interface::Image* image )
229 {
230   itk::DataObject* itk_image = NULL;
231   std::string r = "";
232   cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _RealImage );
233   else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _RealImage );
234   else r = "MacheteFilter: Input image type not supported.";
235   return( r );
236 }
237
238 // -------------------------------------------------------------------------
239 std::string cpPlugins::BasicFilters::MacheteFilter::
240 _FromMesh( cpPlugins::Interface::Mesh* mesh )
241 {
242   return( "" );
243 }
244
245 // -------------------------------------------------------------------------
246 template< class I >
247 std::string cpPlugins::BasicFilters::MacheteFilter::
248 _RealImage( itk::DataObject* dobj )
249 {
250   typedef
251     cpExtensions::DataStructures::
252     InfinitePlaneSpatialObject< I::ImageDimension > _TPlane;
253   typedef
254     cpExtensions::Algorithms::
255     SpatialObjectMaskImageFilter< I > _TFilter;
256   typedef cpPlugins::Interface::DataObject _TDataObject;
257   typedef cpPlugins::Interface::Image      _TImage;
258   typedef typename _TPlane::PointType  _TPoint;
259   typedef typename _TPlane::VectorType _TVector;
260   typedef typename I::PixelType        _TPixel;
261
262   I* image = dynamic_cast< I* >( dobj );
263
264   _TPoint c = this->m_Parameters->GetPoint< _TPoint >(
265     "PlaneCenter", I::ImageDimension
266     );
267   _TVector n = this->m_Parameters->GetVector< _TVector >(
268     "PlaneNormal", I::ImageDimension
269     );
270
271   typename _TPlane::Pointer plane = _TPlane::New( );
272   plane->SetCenter( c );
273   plane->SetNormal( n );
274
275   // Configure filter
276   _TFilter* filter = this->_CreateITK< _TFilter >( );
277   filter->SetInput( image );
278   filter->SetSpatialObject( plane );
279   filter->SetOutsideValue( _TPixel( 0 ) );
280   filter->Update( );
281
282   // Connect output (and correct its type)
283   auto name = this->GetOutput< _TDataObject >( "Output" )->GetName( );
284   this->_MakeOutput< _TImage >( "Output" );
285   _TImage* out = this->GetOutput< _TImage >( "Output" );
286   if( out != NULL )
287   {
288     out->SetITK< I >( filter->GetOutput( ) );
289     out->SetName( name );
290     return( "" );
291   }
292   else
293     return( "MacheteFilter: output image not correctly created." );
294 }
295
296 // eof - $RCSfile$