1 #include "MacheteFilter.h"
3 #include <cpPlugins/Interface/BaseApplication.h>
4 #include <cpPlugins/Interface/Plugins.h>
6 #include <cpPlugins/Interface/DataObject.h>
7 #include <cpPlugins/Interface/Image.h>
8 #include <cpPlugins/Interface/Mesh.h>
10 #include <cpExtensions/Interaction/ImageInteractorStyle.h>
11 #include <cpExtensions/DataStructures/InfinitePlaneSpatialObject.h>
12 #include <cpExtensions/Algorithms/SpatialObjectMaskImageFilter.h>
14 #include <itkPlaneSpatialObject.h>
16 #include <itkVector.h>
18 #include <vtkInteractorStyleSwitch.h>
19 #include <vtkPlaneWidget.h>
20 #include <vtkRenderWindowInteractor.h>
22 #ifdef cpPlugins_Interface_QT4
23 #include <QDialogButtonBox>
25 // -------------------------------------------------------------------------
26 cpPlugins::BasicFilters::MacheteFilter_Dialog::
28 QWidget* parent, MacheteFilter* filter, Qt::WindowFlags f
30 : QDialog( parent, f | Qt::WindowStaysOnTopHint ),
33 this->m_Title = new QLabel( this );
34 this->m_Title->setText( "Execute machete filter" );
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 );
42 QDialogButtonBox* bb = new QDialogButtonBox(
43 QDialogButtonBox::Cancel | QDialogButtonBox::Ok
45 QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
46 QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
47 this->m_ToolsLayout->addWidget( bb );
50 // -------------------------------------------------------------------------
51 cpPlugins::BasicFilters::MacheteFilter_Dialog::
52 ~MacheteFilter_Dialog( )
55 delete this->m_ToolsLayout;
56 delete this->m_MainLayout;
59 // -------------------------------------------------------------------------
60 void cpPlugins::BasicFilters::MacheteFilter_Dialog::
63 // Get interactive widget
64 if( this->m_Filter == NULL )
66 vtkPlaneWidget* wdg = this->m_Filter->m_PlaneWidget;
70 // Get/Set plane parameters
71 double center[ 3 ], normal[ 3 ];
72 wdg->GetCenter( center );
73 wdg->GetNormal( normal );
75 this->m_Filter->GetParameters( )->SetPoint( "PlaneCenter", 3, center );
76 this->m_Filter->GetParameters( )->SetVector( "PlaneNormal", 3, normal );
79 auto plugins = this->m_Filter->GetPlugins( );
82 auto app = plugins->GetApplication( );
84 app->UpdateActualFilter( );
89 // -------------------------------------------------------------------------
90 void cpPlugins::BasicFilters::MacheteFilter_Dialog::
93 auto plugins = this->m_Filter->GetPlugins( );
95 plugins->DeactivateFilter( );
96 this->Superclass::reject( );
99 #endif // cpPlugins_Interface_QT4
101 // -------------------------------------------------------------------------
102 cpPlugins::BasicFilters::MacheteFilter::
103 DialogResult cpPlugins::BasicFilters::MacheteFilter::
104 ExecConfigurationDialog( QWidget* parent )
106 #ifdef cpPlugins_Interface_QT4
108 typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
110 // Choose a valid 3D interactor
111 vtkRenderWindowInteractor* iren = NULL;
112 auto iIt = this->m_Interactors.begin( );
113 for( ; iIt != this->m_Interactors.end( ) && iren == NULL; ++iIt )
115 _TImageStyle* istyle =
116 dynamic_cast< _TImageStyle* >(
117 ( *iIt )->GetInteractorStyle( )
124 return( Self::DialogResult_Cancel );
128 cpPlugins::Interface::Image* image =
129 this->GetInput< cpPlugins::Interface::Image >( "Input" );
130 bool input_found = false;
133 image->GetVTK< vtkImageData >( )->GetBounds( bbox );
137 cpPlugins::Interface::Mesh* mesh =
138 this->GetInput< cpPlugins::Interface::Mesh >( "Input" );
141 mesh->GetVTK< vtkPolyData >( )->GetBounds( bbox );
146 return( Self::DialogResult_Cancel );
148 // Create plane widget
149 if( this->m_PlaneWidget != NULL )
150 this->m_PlaneWidget->Delete( );
151 this->m_PlaneWidget = vtkPlaneWidget::New( );
152 this->m_PlaneWidget->NormalToXAxisOn( );
153 this->m_PlaneWidget->SetCenter(
154 ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
155 ( bbox[ 3 ] + bbox[ 2 ] ) * double( 0.5 ),
156 ( bbox[ 5 ] + bbox[ 4 ] ) * double( 0.5 )
158 this->m_PlaneWidget->SetOrigin(
159 ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
163 this->m_PlaneWidget->SetPoint1(
164 ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
168 this->m_PlaneWidget->SetPoint2(
169 ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
173 this->m_PlaneWidget->SetResolution( 15 );
174 this->m_PlaneWidget->SetRepresentationToWireframe( );
175 this->m_PlaneWidget->SetInteractor( iren );
176 this->m_PlaneWidget->PlaceWidget( );
177 this->m_PlaneWidget->On( );
179 this->m_Dialog = new MacheteFilter_Dialog( NULL, this );
180 this->m_Dialog->show( );
182 return( Self::DialogResult_Modal );
183 #else // cpPlugins_Interface_QT4
184 return( Self::DialogResult_Cancel );
185 #endif // cpPlugins_Interface_QT4
188 // -------------------------------------------------------------------------
189 cpPlugins::BasicFilters::MacheteFilter::
192 m_PlaneWidget( NULL )
194 this->_AddInput( "Input" );
195 this->_MakeOutput< cpPlugins::Interface::DataObject >( "PositiveOutput" );
196 this->_MakeOutput< cpPlugins::Interface::DataObject >( "NegativeOutput" );
198 this->m_Parameters->ConfigureAsPoint( "PlaneCenter" );
199 this->m_Parameters->ConfigureAsVector( "PlaneNormal" );
202 // -------------------------------------------------------------------------
203 cpPlugins::BasicFilters::MacheteFilter::
206 if( this->m_PlaneWidget != NULL )
207 this->m_PlaneWidget->Delete( );
210 // -------------------------------------------------------------------------
211 std::string cpPlugins::BasicFilters::MacheteFilter::
214 cpPlugins::Interface::Image* image =
215 this->GetInput< cpPlugins::Interface::Image >( "Input" );
217 return( this->_FromImage( image ) );
218 cpPlugins::Interface::Mesh* mesh =
219 this->GetInput< cpPlugins::Interface::Mesh >( "Input" );
221 return( this->_FromMesh( mesh ) );
222 return( "MacheteFilter: No valid input." );
225 // -------------------------------------------------------------------------
226 std::string cpPlugins::BasicFilters::MacheteFilter::
227 _FromImage( cpPlugins::Interface::Image* image )
229 itk::DataObject* itk_image = NULL;
231 cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _RealImage );
232 else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _RealImage );
233 else r = "MacheteFilter: Input image type not supported.";
237 // -------------------------------------------------------------------------
238 std::string cpPlugins::BasicFilters::MacheteFilter::
239 _FromMesh( cpPlugins::Interface::Mesh* mesh )
244 // -------------------------------------------------------------------------
246 std::string cpPlugins::BasicFilters::MacheteFilter::
247 _RealImage( itk::DataObject* dobj )
250 cpExtensions::DataStructures::
251 InfinitePlaneSpatialObject< I::ImageDimension > _TPlane;
253 cpExtensions::Algorithms::
254 SpatialObjectMaskImageFilter< I, I > _TFilter;
255 typedef cpPlugins::Interface::DataObject _TObj;
256 typedef cpPlugins::Interface::Image _TImage;
257 typedef typename _TPlane::PointType _TPoint;
258 typedef typename _TPlane::VectorType _TVector;
259 typedef typename I::PixelType _TPixel;
261 I* image = dynamic_cast< I* >( dobj );
263 _TPoint c = this->m_Parameters->GetPoint< _TPoint >(
264 "PlaneCenter", I::ImageDimension
266 _TVector n = this->m_Parameters->GetVector< _TVector >(
267 "PlaneNormal", I::ImageDimension
270 typename _TPlane::Pointer plane = _TPlane::New( );
271 plane->SetCenter( c );
272 plane->SetNormal( n );
275 _TFilter* filter = this->_CreateITK< _TFilter >( );
276 filter->SetInput( image );
277 filter->SetSpatialObject( plane );
278 filter->SetOutsideValue( _TPixel( 0 ) );
282 auto pos_name = this->GetOutput< _TObj >( "PositiveOutput" )->GetName( );
283 auto neg_name = this->GetOutput< _TObj >( "NegativeOutput" )->GetName( );
285 // Connect outputs (and correct their types and names)
286 _TImage* pos_out = this->GetOutput< _TImage >( "PositiveOutput" );
287 if( pos_out == NULL )
289 this->_MakeOutput< _TImage >( "PositiveOutput" );
290 pos_out = this->GetOutput< _TImage >( "PositiveOutput" );
291 pos_out->SetName( pos_name );
294 _TImage* neg_out = this->GetOutput< _TImage >( "NegativeOutput" );
295 if( neg_out == NULL )
297 this->_MakeOutput< _TImage >( "NegativeOutput" );
298 neg_out = this->GetOutput< _TImage >( "NegativeOutput" );
299 neg_out->SetName( neg_name );
304 pos_out->SetITK< I >( filter->GetPositiveOutput( ) );
305 neg_out->SetITK< I >( filter->GetNegativeOutput( ) );