]> Creatis software - cpMesh.git/blobdiff - appli/InteractiveDeformableMeshSegmentation/MainWnd_ExecutePlugins.cxx
Simple flood fill plugin added.
[cpMesh.git] / appli / InteractiveDeformableMeshSegmentation / MainWnd_ExecutePlugins.cxx
index 8429a818d38ceb7c41dfa1f792359d1e30801dc4..cbbfac64d213a9884fbe31df39da5466529e14b0 100644 (file)
@@ -2,9 +2,16 @@
 #include "ui_MainWnd.h"
 
 #include <cstdlib>
+#include <sstream>
 
 #include <QDoubleSpinBox>
 #include <QLabel>
+#include <QMessageBox>
+
+#include <itkImageRegionIterator.h>
+#include <itkImageRegionConstIterator.h>
+#define ITK_MANUAL_INSTANTIATION
+#include <itkImage.h>
 
 // -------------------------------------------------------------------------
 bool MainWnd::
@@ -52,100 +59,105 @@ _ParametersDialog( const TParameters& parameters )
 
   } // rof
   gridLayout->addLayout( verticalLayout, 0, 0, 1, 1 );
+
+  // Infere plugin type
+  TParameters::const_iterator seedIt = parameters.find( "Seed" );
+  TParameters::const_iterator radiusIt = parameters.find( "Radius" );
+  TParameters::const_iterator endIt = parameters.end( );
+  if( seedIt == endIt && radiusIt == endIt )
+    this->m_ActivePluginType = Self::GlobalPluginType;
+  else if( seedIt != endIt && radiusIt == endIt )
+    this->m_ActivePluginType = Self::DoubleClickPluginType;
+  else if( seedIt != endIt && radiusIt != endIt )
+    this->m_ActivePluginType = Self::SpherePluginType;
+  else
+    this->m_ActivePluginType = Self::NonePluginType;
+
   QMetaObject::connectSlotsByName( this->m_ActiveParameters );
   this->m_ActiveParameters->show( );
 
-  /*
-    typedef std::map< std::string, QWidget* > _TWidgets;
-    _TWidgets widgets;
-    TParameters::const_iterator pIt = parameters.begin( );
-    for( ; pIt != parameters.end( ); ++pIt )
-    {
-    unsigned long pos = pIt->first.find_last_of( ":" );
-    std::string v_name = pIt->first.substr( 0, pos );
-    std::string v_type = pIt->first.substr( pos + 1 );
-    QHBoxLayout* horizontalLayout = new QHBoxLayout( );
-    QLabel* label = new QLabel( this->m_ActiveParameters );
-    label->setText( QString( v_name.c_str( ) ) );
-    horizontalLayout->addWidget( label );
-    if( v_type == "real" )
-    {
-    QDoubleSpinBox* v_double = new QDoubleSpinBox( this->m_ActiveParameters );
-    v_double->setDecimals( 3 );
-    v_double->setMinimum( -( std::numeric_limits< double >::max( ) ) );
-    v_double->setMaximum( std::numeric_limits< double >::max( ) );
-    v_double->setValue( std::atof( pIt->second.c_str( ) ) );
-    horizontalLayout->addWidget( v_double );
-    widgets[ pIt->first ] = v_double;
-    }
-    else if( v_type == "atomic_real" )
-    {
-    if( v_name == "MeshType" )
-    {
-    QLabel* info = new QLabel( this->m_ActiveParameters );
-    if( typeid( TScalar ) == typeid( float ) )
-    info->setText( QString( "float" ) );
-    else if( typeid( TScalar ) == typeid( double ) )
-    info->setText( QString( "double" ) );
-    horizontalLayout->addWidget( info );
-    widgets[ pIt->first ] = info;
-    } // fi
-    } // fi
-    verticalLayout.addLayout( horizontalLayout );
-    } // rof
-    gridLayout.addLayout( &verticalLayout, 0, 0, 1, 1 );
-    // Buttons box
-    QDialogButtonBox buttonBox( this->m_ActiveParameters );
-    buttonBox.setOrientation( Qt::Horizontal );
-    buttonBox.setStandardButtons(
-    QDialogButtonBox::Cancel | QDialogButtonBox::Ok
-    );
-    gridLayout.addWidget( &buttonBox, 1, 0, 1, 1 );
-    QObject::connect(
-    &buttonBox, SIGNAL( accepted( ) ),
-    this->m_ActiveParameters, SLOT( accept( ) )
-    );
-    QObject::connect(
-    &buttonBox, SIGNAL( rejected( ) ),
-    this->m_ActiveParameters, SLOT( reject( ) )
-    );
-    QMetaObject::connectSlotsByName( this->m_ActiveParameters );
-
-    // Execute dialog
-    if( dlg.exec( ) == QDialog::Accepted )
-    {
-    _TWidgets::const_iterator wIt = widgets.begin( );
-    for( ; wIt != widgets.end( ); ++wIt )
-    {
-    unsigned long pos = wIt->first.find_last_of( ":" );
-    std::string v_name = wIt->first.substr( 0, pos );
-    std::string v_type = wIt->first.substr( pos + 1 );
-    std::stringstream sstr;
-    if( v_type == "real" )
-    {
-    QDoubleSpinBox* v_double =
-    dynamic_cast< QDoubleSpinBox* >( wIt->second );
-    if( v_double != NULL )
-    sstr << v_double->value( );
-    }
-    else if( v_type == "atomic_real" )
-    {
-    if( v_name == "MeshType" )
+  return( false );
+}
+
+// -------------------------------------------------------------------------
+void MainWnd::
+_ExecuteDoubleClickPlugin( const double* pnt )
+{
+  if(
+    this->m_InputImage == NULL ||
+    this->m_SegmentedImage == NULL ||
+    this->m_ActivePlugin == NULL ||
+    this->m_ActiveParameters == NULL ||
+    this->m_ActivePluginType != Self::DoubleClickPluginType
+    )
+    return;
+
+  TParameters parameters = this->m_ActivePlugin->GetDefaultParameters( );
+
+  // Variable parameters
+  for(
+    TParameters::iterator pIt = parameters.begin( );
+    pIt != parameters.end( );
+    ++pIt
+    )
+  {
+    if( pIt->first != "Seed" )
     {
-    QLabel* info = dynamic_cast< QLabel* >( wIt->second );
-    if( info != NULL )
-    sstr << info->text( ).toStdString( );
+      if( pIt->second.first == "double" )
+      {
+        QDoubleSpinBox* v_double = this->m_ActiveParameters->
+          findChild< QDoubleSpinBox* >( pIt->first.c_str( ) );
+        if( v_double != NULL )
+          parameters[ pIt->first ] =
+            TParameter( "double", v_double->text( ).toStdString( ) );
+        
+      } // fi
 
     } // fi
 
-    } // fi
-    parameters[ wIt->first ] = sstr.str( );
+  } // rof
 
-    } // rof
-    return( true );
-    }
-  */
-  return( false );
+  // Seed
+  std::stringstream seed_str;
+  seed_str << pnt[ 0 ] << ":" << pnt[ 1 ] << ":" << pnt[ 2 ];
+  parameters[ "Seed" ] = TParameter( "point", seed_str.str( ) );
+
+  // Execute
+  this->m_ActivePlugin->SetParameters( parameters );
+  this->m_ActivePlugin->SetInput( 0, this->m_InputImage );
+  this->m_ActivePlugin->SetInput( 1, this->m_SegmentedImage );
+  std::string err = this->m_ActivePlugin->Update( );
+  if( err != "" )
+  {
+    QMessageBox::critical( this, tr( "Error caugth!" ), tr( err.c_str( ) ) );
+    return;
+
+  } // fi
+
+  // Join results
+  itk::DataObject* s = this->m_SegmentedImage->GetDataObject( );
+  if( dynamic_cast< itk::Image< char, 3 >* >( s ) != NULL )
+    this->_JoinSegmentations< char >( );
+  else if( dynamic_cast< itk::Image< short, 3 >* >( s ) != NULL )
+    this->_JoinSegmentations< short >( );
+  else if( dynamic_cast< itk::Image< int, 3 >* >( s ) != NULL )
+    this->_JoinSegmentations< int >( );
+  else if( dynamic_cast< itk::Image< long, 3 >* >( s ) != NULL )
+    this->_JoinSegmentations< long >( );
+  else if( dynamic_cast< itk::Image< unsigned char, 3 >* >( s ) != NULL )
+    this->_JoinSegmentations< unsigned char >( );
+  else if( dynamic_cast< itk::Image< unsigned short, 3 >* >( s ) != NULL )
+    this->_JoinSegmentations< unsigned short >( );
+  else if( dynamic_cast< itk::Image< unsigned int, 3 >* >( s ) != NULL )
+    this->_JoinSegmentations< unsigned int >( );
+  else if( dynamic_cast< itk::Image< unsigned long, 3 >* >( s ) != NULL )
+    this->_JoinSegmentations< unsigned long >( );
+
+  // Update visualization
+  this->m_SegmentedImage->UpdateVTKImageData( );
+  this->m_MPR->Render( 0 );
+  this->m_MPR->Render( 1 );
+  this->m_MPR->Render( 2 );
 }
 
 // -------------------------------------------------------------------------
@@ -174,6 +186,7 @@ _triggered_actionFilterSegmentation( )
         this->m_SegmentationFilterClasses[ filter_name ]
         )
       );
+  this->m_ActivePluginCategory = Self::SegmentationFilteringPluginCategory;
 
   // Show parameters dialog
   this->_ParametersDialog( this->m_ActivePlugin->GetDefaultParameters( ) );
@@ -185,4 +198,82 @@ _triggered_actionProcessMesh( )
 {
 }
 
+// -------------------------------------------------------------------------
+template< class P >
+void MainWnd::
+_JoinSegmentations( )
+{
+  typedef itk::Image< P, 3 > _TImage;
+
+  _TImage* old_segmentation =
+    dynamic_cast< _TImage* >( this->m_SegmentedImage->GetDataObject( ) );
+  _TImage* new_segmentation =
+    dynamic_cast< _TImage* >(
+      dynamic_cast< TPluginImage* >(
+        this->m_ActivePlugin->GetOutput( 0 )
+        )->GetDataObject( )
+      );
+
+  /* TODO: InPlaceOn does not execute correctly on input image
+     typedef itk::AndImageFilter< _TImage, _TImage, _TImage > _TFilter;
+     typename _TFilter::Pointer filter = _TFilter::New( );
+     filter->InPlaceOn( );
+     filter->SetInput( 0, old_segmentation );
+     filter->SetInput( 1, new_segmentation );
+     filter->Update( );
+     //old_segmentation->DisconnectPipeline( );
+
+     std::cout << old_segmentation->GetRequestedRegion( ) << std::endl;
+     std::cout << new_segmentation->GetRequestedRegion( ) << std::endl;
+     std::cout << old_segmentation->GetBufferedRegion( ) << std::endl;
+     std::cout << new_segmentation->GetBufferedRegion( ) << std::endl;
+  */
+  typedef itk::ImageRegionConstIterator< _TImage > _TConstIt;
+  typedef itk::ImageRegionIterator< _TImage > _TIt;
+  _TConstIt nIt( new_segmentation, new_segmentation->GetRequestedRegion( ) );
+  _TIt oIt( old_segmentation, old_segmentation->GetRequestedRegion( ) );
+  nIt.GoToBegin( );
+  oIt.GoToBegin( );
+  for( ; !nIt.IsAtEnd( ); ++nIt, ++oIt )
+    oIt.Set( nIt.Get( ) | oIt.Get( ) );
+}
+
+// -------------------------------------------------------------------------
+DoubleClickCommand* DoubleClickCommand::
+New( )
+{
+  return( new DoubleClickCommand( ) );
+}
+
+// -------------------------------------------------------------------------
+void DoubleClickCommand::
+SetMainWnd( MainWnd* wnd )
+{
+  this->m_MainWnd = wnd;
+}
+
+// -------------------------------------------------------------------------
+void DoubleClickCommand::
+Execute( vtkObject* caller, unsigned long eid, void* data )
+{
+  if( this->m_MainWnd != NULL )
+    this->m_MainWnd->_ExecuteDoubleClickPlugin(
+      reinterpret_cast< const double* >( data )
+      );
+}
+
+// -------------------------------------------------------------------------
+DoubleClickCommand::
+DoubleClickCommand( )
+  : Superclass( ),
+    m_MainWnd( NULL )
+{
+}
+
+// -------------------------------------------------------------------------
+DoubleClickCommand::
+~DoubleClickCommand( )
+{
+}
+
 // eof - $RCSfile$