]> Creatis software - cpMesh.git/blob - appli/InteractiveDeformableMeshSegmentation/MainWnd_ExecutePlugins.cxx
Simple flood fill plugin added.
[cpMesh.git] / appli / InteractiveDeformableMeshSegmentation / MainWnd_ExecutePlugins.cxx
1 #include "MainWnd.h"
2 #include "ui_MainWnd.h"
3
4 #include <cstdlib>
5 #include <sstream>
6
7 #include <QDoubleSpinBox>
8 #include <QLabel>
9 #include <QMessageBox>
10
11 #include <itkImageRegionIterator.h>
12 #include <itkImageRegionConstIterator.h>
13 #define ITK_MANUAL_INSTANTIATION
14 #include <itkImage.h>
15
16 // -------------------------------------------------------------------------
17 bool MainWnd::
18 _ParametersDialog( const TParameters& parameters )
19 {
20   if( this->m_ActiveParameters != NULL ) 
21     this->m_ActiveParameters->close( );
22   this->m_ActiveParameters = new QWidget( NULL );
23   this->m_ActiveParameters->setWindowFlags( Qt::FramelessWindowHint ); 
24   this->m_ActiveParameters->setWindowFlags( Qt::WindowTitleHint );
25
26   QGridLayout* gridLayout = new QGridLayout( this->m_ActiveParameters );
27   QVBoxLayout* verticalLayout = new QVBoxLayout( );
28
29   // Put values
30   QLabel* title = new QLabel( this->m_ActiveParameters );
31   title->setText( this->m_ActivePlugin->GetClassName( ).c_str( ) );
32   verticalLayout->addWidget( title );
33
34   TParameters::const_iterator pIt = parameters.begin( );
35   for( ; pIt != parameters.end( ); ++pIt )
36   {
37     std::string par_name = pIt->first;
38     std::string par_type = pIt->second.first;
39     std::string par_value = pIt->second.second;
40
41     if( par_type == "double" )
42     {
43       QHBoxLayout* horizontalLayout = new QHBoxLayout( );
44       QLabel* label = new QLabel( this->m_ActiveParameters );
45       label->setText( QString( par_name.c_str( ) ) );
46       horizontalLayout->addWidget( label );
47
48       QDoubleSpinBox* v_double =
49         new QDoubleSpinBox( this->m_ActiveParameters );
50       v_double->setDecimals( 3 );
51       v_double->setMinimum( -( std::numeric_limits< double >::max( ) ) );
52       v_double->setMaximum( std::numeric_limits< double >::max( ) );
53       v_double->setValue( std::atof( par_value.c_str( ) ) );
54       v_double->setObjectName( QString( par_name.c_str( ) ) );
55       horizontalLayout->addWidget( v_double );
56       verticalLayout->addLayout( horizontalLayout );
57
58     } // fi
59
60   } // rof
61   gridLayout->addLayout( verticalLayout, 0, 0, 1, 1 );
62
63   // Infere plugin type
64   TParameters::const_iterator seedIt = parameters.find( "Seed" );
65   TParameters::const_iterator radiusIt = parameters.find( "Radius" );
66   TParameters::const_iterator endIt = parameters.end( );
67   if( seedIt == endIt && radiusIt == endIt )
68     this->m_ActivePluginType = Self::GlobalPluginType;
69   else if( seedIt != endIt && radiusIt == endIt )
70     this->m_ActivePluginType = Self::DoubleClickPluginType;
71   else if( seedIt != endIt && radiusIt != endIt )
72     this->m_ActivePluginType = Self::SpherePluginType;
73   else
74     this->m_ActivePluginType = Self::NonePluginType;
75
76   QMetaObject::connectSlotsByName( this->m_ActiveParameters );
77   this->m_ActiveParameters->show( );
78
79   return( false );
80 }
81
82 // -------------------------------------------------------------------------
83 void MainWnd::
84 _ExecuteDoubleClickPlugin( const double* pnt )
85 {
86   if(
87     this->m_InputImage == NULL ||
88     this->m_SegmentedImage == NULL ||
89     this->m_ActivePlugin == NULL ||
90     this->m_ActiveParameters == NULL ||
91     this->m_ActivePluginType != Self::DoubleClickPluginType
92     )
93     return;
94
95   TParameters parameters = this->m_ActivePlugin->GetDefaultParameters( );
96
97   // Variable parameters
98   for(
99     TParameters::iterator pIt = parameters.begin( );
100     pIt != parameters.end( );
101     ++pIt
102     )
103   {
104     if( pIt->first != "Seed" )
105     {
106       if( pIt->second.first == "double" )
107       {
108         QDoubleSpinBox* v_double = this->m_ActiveParameters->
109           findChild< QDoubleSpinBox* >( pIt->first.c_str( ) );
110         if( v_double != NULL )
111           parameters[ pIt->first ] =
112             TParameter( "double", v_double->text( ).toStdString( ) );
113         
114       } // fi
115
116     } // fi
117
118   } // rof
119
120   // Seed
121   std::stringstream seed_str;
122   seed_str << pnt[ 0 ] << ":" << pnt[ 1 ] << ":" << pnt[ 2 ];
123   parameters[ "Seed" ] = TParameter( "point", seed_str.str( ) );
124
125   // Execute
126   this->m_ActivePlugin->SetParameters( parameters );
127   this->m_ActivePlugin->SetInput( 0, this->m_InputImage );
128   this->m_ActivePlugin->SetInput( 1, this->m_SegmentedImage );
129   std::string err = this->m_ActivePlugin->Update( );
130   if( err != "" )
131   {
132     QMessageBox::critical( this, tr( "Error caugth!" ), tr( err.c_str( ) ) );
133     return;
134
135   } // fi
136
137   // Join results
138   itk::DataObject* s = this->m_SegmentedImage->GetDataObject( );
139   if( dynamic_cast< itk::Image< char, 3 >* >( s ) != NULL )
140     this->_JoinSegmentations< char >( );
141   else if( dynamic_cast< itk::Image< short, 3 >* >( s ) != NULL )
142     this->_JoinSegmentations< short >( );
143   else if( dynamic_cast< itk::Image< int, 3 >* >( s ) != NULL )
144     this->_JoinSegmentations< int >( );
145   else if( dynamic_cast< itk::Image< long, 3 >* >( s ) != NULL )
146     this->_JoinSegmentations< long >( );
147   else if( dynamic_cast< itk::Image< unsigned char, 3 >* >( s ) != NULL )
148     this->_JoinSegmentations< unsigned char >( );
149   else if( dynamic_cast< itk::Image< unsigned short, 3 >* >( s ) != NULL )
150     this->_JoinSegmentations< unsigned short >( );
151   else if( dynamic_cast< itk::Image< unsigned int, 3 >* >( s ) != NULL )
152     this->_JoinSegmentations< unsigned int >( );
153   else if( dynamic_cast< itk::Image< unsigned long, 3 >* >( s ) != NULL )
154     this->_JoinSegmentations< unsigned long >( );
155
156   // Update visualization
157   this->m_SegmentedImage->UpdateVTKImageData( );
158   this->m_MPR->Render( 0 );
159   this->m_MPR->Render( 1 );
160   this->m_MPR->Render( 2 );
161 }
162
163 // -------------------------------------------------------------------------
164 void MainWnd::
165 _triggered_actionSegmentImage( )
166 {
167 }
168
169 // -------------------------------------------------------------------------
170 void MainWnd::
171 _triggered_actionFilterSegmentation( )
172 {
173   // Get filter name
174   if( this->m_SegmentedImage == NULL )
175     return;
176   QAction* action = dynamic_cast< QAction* >( this->sender( ) );
177   if( action == NULL )
178     return;
179   std::string filter_name = action->text( ).toStdString( );
180
181   // Create plugin
182   if( this->m_ActivePlugin != NULL ) delete this->m_ActivePlugin;
183   this->m_ActivePlugin =
184     dynamic_cast< TPlugin* >(
185       this->m_Plugins.CreateObject(
186         this->m_SegmentationFilterClasses[ filter_name ]
187         )
188       );
189   this->m_ActivePluginCategory = Self::SegmentationFilteringPluginCategory;
190
191   // Show parameters dialog
192   this->_ParametersDialog( this->m_ActivePlugin->GetDefaultParameters( ) );
193 }
194
195 // -------------------------------------------------------------------------
196 void MainWnd::
197 _triggered_actionProcessMesh( )
198 {
199 }
200
201 // -------------------------------------------------------------------------
202 template< class P >
203 void MainWnd::
204 _JoinSegmentations( )
205 {
206   typedef itk::Image< P, 3 > _TImage;
207
208   _TImage* old_segmentation =
209     dynamic_cast< _TImage* >( this->m_SegmentedImage->GetDataObject( ) );
210   _TImage* new_segmentation =
211     dynamic_cast< _TImage* >(
212       dynamic_cast< TPluginImage* >(
213         this->m_ActivePlugin->GetOutput( 0 )
214         )->GetDataObject( )
215       );
216
217   /* TODO: InPlaceOn does not execute correctly on input image
218      typedef itk::AndImageFilter< _TImage, _TImage, _TImage > _TFilter;
219      typename _TFilter::Pointer filter = _TFilter::New( );
220      filter->InPlaceOn( );
221      filter->SetInput( 0, old_segmentation );
222      filter->SetInput( 1, new_segmentation );
223      filter->Update( );
224      //old_segmentation->DisconnectPipeline( );
225
226      std::cout << old_segmentation->GetRequestedRegion( ) << std::endl;
227      std::cout << new_segmentation->GetRequestedRegion( ) << std::endl;
228      std::cout << old_segmentation->GetBufferedRegion( ) << std::endl;
229      std::cout << new_segmentation->GetBufferedRegion( ) << std::endl;
230   */
231   typedef itk::ImageRegionConstIterator< _TImage > _TConstIt;
232   typedef itk::ImageRegionIterator< _TImage > _TIt;
233   _TConstIt nIt( new_segmentation, new_segmentation->GetRequestedRegion( ) );
234   _TIt oIt( old_segmentation, old_segmentation->GetRequestedRegion( ) );
235   nIt.GoToBegin( );
236   oIt.GoToBegin( );
237   for( ; !nIt.IsAtEnd( ); ++nIt, ++oIt )
238     oIt.Set( nIt.Get( ) | oIt.Get( ) );
239 }
240
241 // -------------------------------------------------------------------------
242 DoubleClickCommand* DoubleClickCommand::
243 New( )
244 {
245   return( new DoubleClickCommand( ) );
246 }
247
248 // -------------------------------------------------------------------------
249 void DoubleClickCommand::
250 SetMainWnd( MainWnd* wnd )
251 {
252   this->m_MainWnd = wnd;
253 }
254
255 // -------------------------------------------------------------------------
256 void DoubleClickCommand::
257 Execute( vtkObject* caller, unsigned long eid, void* data )
258 {
259   if( this->m_MainWnd != NULL )
260     this->m_MainWnd->_ExecuteDoubleClickPlugin(
261       reinterpret_cast< const double* >( data )
262       );
263 }
264
265 // -------------------------------------------------------------------------
266 DoubleClickCommand::
267 DoubleClickCommand( )
268   : Superclass( ),
269     m_MainWnd( NULL )
270 {
271 }
272
273 // -------------------------------------------------------------------------
274 DoubleClickCommand::
275 ~DoubleClickCommand( )
276 {
277 }
278
279 // eof - $RCSfile$