1 #include "MacheteFilter.h"
3 #include <cpPlugins/Interface/DataObject.h>
4 #include <cpPlugins/Interface/Image.h>
5 #include <cpPlugins/Interface/Mesh.h>
7 #include <cpExtensions/Interaction/ImageInteractorStyle.h>
8 #include <cpExtensions/DataStructures/InfinitePlaneSpatialObject.h>
9 #include <cpExtensions/Algorithms/SpatialObjectMaskImageFilter.h>
11 #include <itkPlaneSpatialObject.h>
13 #include <itkVector.h>
15 #include <vtkInteractorStyleSwitch.h>
16 #include <vtkPlaneWidget.h>
17 #include <vtkRenderWindowInteractor.h>
19 #ifdef cpPlugins_Interface_QT4
20 #include <QDialogButtonBox>
22 // -------------------------------------------------------------------------
23 cpPlugins::BasicFilters::MacheteFilter_Dialog::
25 QWidget* parent, MacheteFilter* filter, Qt::WindowFlags f
27 : QDialog( parent, f | Qt::WindowStaysOnTopHint ),
30 this->m_Title = new QLabel( this );
31 this->m_Title->setText( "Execute machete filter" );
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 );
39 QDialogButtonBox* bb = new QDialogButtonBox(
40 QDialogButtonBox::Cancel | QDialogButtonBox::Ok
42 QObject::connect( bb, SIGNAL( accepted( ) ), this, SLOT( accept( ) ) );
43 QObject::connect( bb, SIGNAL( rejected( ) ), this, SLOT( reject( ) ) );
44 this->m_ToolsLayout->addWidget( bb );
47 // -------------------------------------------------------------------------
48 cpPlugins::BasicFilters::MacheteFilter_Dialog::
49 ~MacheteFilter_Dialog( )
52 delete this->m_ToolsLayout;
53 delete this->m_MainLayout;
56 // -------------------------------------------------------------------------
57 void cpPlugins::BasicFilters::MacheteFilter_Dialog::
60 // Get interactive widget
61 if( this->m_Filter == NULL )
63 vtkPlaneWidget* wdg = this->m_Filter->m_PlaneWidget;
67 // Get/Set plane parameters
68 double center[ 3 ], normal[ 3 ];
69 wdg->GetCenter( center );
70 wdg->GetNormal( normal );
73 this->m_Filter->GetParameters( )->SetPoint( "PlaneCenter", 3, center );
74 this->m_Filter->GetParameters( )->SetVector( "PlaneNormal", 3, normal );
79 auto plugins = this->m_Filter->GetPlugins( );
82 auto app = plugins->GetApplication( );
84 app->UpdateActualFilter( );
90 // -------------------------------------------------------------------------
91 void cpPlugins::BasicFilters::MacheteFilter_Dialog::
95 auto plugins = this->m_Filter->GetPlugins( );
97 plugins->DeactivateFilter( );
99 this->Superclass::reject( );
102 #endif // cpPlugins_Interface_QT4
104 // -------------------------------------------------------------------------
105 cpPlugins::BasicFilters::MacheteFilter::
106 DialogResult cpPlugins::BasicFilters::MacheteFilter::
107 ExecConfigurationDialog( QWidget* parent )
109 #ifdef cpPlugins_Interface_QT4
111 typedef cpExtensions::Interaction::ImageInteractorStyle _TImageStyle;
113 // Choose a valid 3D interactor
114 vtkRenderWindowInteractor* iren = NULL;
115 auto iIt = this->m_Interactors.begin( );
116 for( ; iIt != this->m_Interactors.end( ) && iren == NULL; ++iIt )
118 _TImageStyle* istyle =
119 dynamic_cast< _TImageStyle* >(
120 ( *iIt )->GetInteractorStyle( )
131 cpPlugins::Interface::Image* image =
132 this->GetInput< cpPlugins::Interface::Image >( "Input" );
133 bool input_found = false;
136 image->GetVTK< vtkImageData >( )->GetBounds( bbox );
140 cpPlugins::Interface::Mesh* mesh =
141 this->GetInput< cpPlugins::Interface::Mesh >( "Input" );
144 mesh->GetVTK< vtkPolyData >( )->GetBounds( bbox );
151 // Create plane widget
152 if( this->m_PlaneWidget != NULL )
153 this->m_PlaneWidget->Delete( );
154 this->m_PlaneWidget = vtkPlaneWidget::New( );
155 this->m_PlaneWidget->NormalToXAxisOn( );
156 this->m_PlaneWidget->SetCenter(
157 ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
158 ( bbox[ 3 ] + bbox[ 2 ] ) * double( 0.5 ),
159 ( bbox[ 5 ] + bbox[ 4 ] ) * double( 0.5 )
161 this->m_PlaneWidget->SetOrigin(
162 ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
166 this->m_PlaneWidget->SetPoint1(
167 ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
171 this->m_PlaneWidget->SetPoint2(
172 ( bbox[ 1 ] + bbox[ 0 ] ) * double( 0.5 ),
176 this->m_PlaneWidget->SetResolution( 15 );
177 this->m_PlaneWidget->SetRepresentationToWireframe( );
178 this->m_PlaneWidget->SetInteractor( iren );
179 this->m_PlaneWidget->PlaceWidget( );
180 this->m_PlaneWidget->On( );
182 this->m_Dialog = new MacheteFilter_Dialog( NULL, this );
183 this->m_Dialog->show( );
186 #else // cpPlugins_Interface_QT4
188 #endif // cpPlugins_Interface_QT4
191 // -------------------------------------------------------------------------
192 cpPlugins::BasicFilters::MacheteFilter::
195 m_PlaneWidget( NULL )
197 this->_AddInput( "Input" );
198 this->_AddOutput< cpPlugins::Interface::DataObject >( "PositiveOutput" );
199 this->_AddOutput< cpPlugins::Interface::DataObject >( "NegativeOutput" );
202 this->m_Parameters->ConfigureAsPoint( "PlaneCenter" );
203 this->m_Parameters->ConfigureAsVector( "PlaneNormal" );
207 // -------------------------------------------------------------------------
208 cpPlugins::BasicFilters::MacheteFilter::
211 if( this->m_PlaneWidget != NULL )
212 this->m_PlaneWidget->Delete( );
215 // -------------------------------------------------------------------------
216 std::string cpPlugins::BasicFilters::MacheteFilter::
219 cpPlugins::Interface::Image* image =
220 this->GetInput< cpPlugins::Interface::Image >( "Input" );
222 return( this->_FromImage( image ) );
223 cpPlugins::Interface::Mesh* mesh =
224 this->GetInput< cpPlugins::Interface::Mesh >( "Input" );
226 return( this->_FromMesh( mesh ) );
227 return( "MacheteFilter: No valid input." );
230 // -------------------------------------------------------------------------
231 std::string cpPlugins::BasicFilters::MacheteFilter::
232 _FromImage( cpPlugins::Interface::Image* image )
234 itk::DataObject* itk_image = NULL;
236 cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _RealImage );
237 else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _RealImage );
238 else r = "MacheteFilter: Input image type not supported.";
242 // -------------------------------------------------------------------------
243 std::string cpPlugins::BasicFilters::MacheteFilter::
244 _FromMesh( cpPlugins::Interface::Mesh* mesh )
249 // -------------------------------------------------------------------------
251 std::string cpPlugins::BasicFilters::MacheteFilter::
252 _RealImage( itk::DataObject* dobj )
255 cpExtensions::DataStructures::
256 InfinitePlaneSpatialObject< I::ImageDimension > _TPlane;
258 cpExtensions::Algorithms::
259 SpatialObjectMaskImageFilter< I, I > _TFilter;
260 typedef cpPlugins::Interface::DataObject _TObj;
261 typedef cpPlugins::Interface::Image _TImage;
262 typedef typename _TPlane::PointType _TPoint;
263 typedef typename _TPlane::VectorType _TVector;
264 typedef typename I::PixelType _TPixel;
266 I* image = dynamic_cast< I* >( dobj );
269 _TPoint c = this->m_Parameters->GetPoint< _TPoint >(
270 "PlaneCenter", I::ImageDimension
272 _TVector n = this->m_Parameters->GetVector< _TVector >(
273 "PlaneNormal", I::ImageDimension
279 typename _TPlane::Pointer plane = _TPlane::New( );
280 plane->SetCenter( c );
281 plane->SetNormal( n );
284 _TFilter* filter = this->_CreateITK< _TFilter >( );
285 filter->SetInput( image );
286 filter->SetSpatialObject( plane );
287 filter->SetOutsideValue( _TPixel( 0 ) );
291 auto pos_name = this->GetOutput< _TObj >( "PositiveOutput" )->GetName( );
292 auto neg_name = this->GetOutput< _TObj >( "NegativeOutput" )->GetName( );
294 // Connect outputs (and correct their types and names)
295 _TImage* pos_out = this->GetOutput< _TImage >( "PositiveOutput" );
296 if( pos_out == NULL )
298 this->_AddOutput< _TImage >( "PositiveOutput" );
299 pos_out = this->GetOutput< _TImage >( "PositiveOutput" );
300 pos_out->SetName( pos_name );
303 _TImage* neg_out = this->GetOutput< _TImage >( "NegativeOutput" );
304 if( neg_out == NULL )
306 this->_AddOutput< _TImage >( "NegativeOutput" );
307 neg_out = this->GetOutput< _TImage >( "NegativeOutput" );
308 neg_out->SetName( neg_name );
313 pos_out->SetITK< I >( filter->GetPositiveOutput( ) );
314 neg_out->SetITK< I >( filter->GetNegativeOutput( ) );