]> Creatis software - cpPlugins.git/blobdiff - appli/examples/example_MPR.cxx
contour widget example update
[cpPlugins.git] / appli / examples / example_MPR.cxx
index 1691e089a0ab571acf47238508146bc5a6327176..9f6e113151d5d0ed38b07a13ac998d3bf0a58e3a 100644 (file)
 #include <cpPlugins/Interface/Interface.h>
 #include <cpPlugins/Interface/ProcessObject.h>
 #include <cpPlugins/Interface/Image.h>
-#include <cpExtensions/Visualization/MPRObjects.h>
 
+#include <vtkSmartPointer.h>
+#include <vtkCommand.h>
 #include <vtkRenderer.h>
 #include <vtkRenderWindow.h>
 #include <vtkRenderWindowInteractor.h>
+#include <vtkSliderRepresentation2D.h>
+#include <vtkSliderWidget.h>
 
+#include <cpExtensions/Visualization/MPRActors.h>
+
+// -------------------------------------------------------------------------
+typedef cpPlugins::Interface::Interface     TInterface;
+typedef cpPlugins::Interface::ProcessObject TFilter;
+typedef cpPlugins::Interface::Parameters    TParameters;
+typedef cpPlugins::Interface::Image         TImage;
+
+typedef cpExtensions::Visualization::MPRActors TMPRActors;
+
+// -------------------------------------------------------------------------
+class SliderCallback
+  : public vtkCommand
+{
+public:
+  static SliderCallback* New( )
+    {
+      return new SliderCallback;
+    }
+  virtual void Execute( vtkObject* caller, unsigned long eId , void* data )
+    {
+      vtkSliderWidget* wdg =
+        reinterpret_cast< vtkSliderWidget* >( caller );
+      if( wdg == NULL )
+        return;
+      vtkSliderRepresentation* rep =
+        static_cast< vtkSliderRepresentation* >(
+          wdg->GetRepresentation( )
+          );
+      if( rep == NULL )
+        return;
+      std::string title = rep->GetTitleText( );
+      if( title == "X" )
+        this->Actors->SetSlice( 0, int( rep->GetValue( ) ) );
+      else if( title == "Y" )
+        this->Actors->SetSlice( 1, int( rep->GetValue( ) ) );
+      else if( title == "Z" )
+        this->Actors->SetSlice( 2, int( rep->GetValue( ) ) );
+      else if( title == "Window" )
+        this->Actors->SetWindow( 0, rep->GetValue( ) );
+      else if( title == "Level" )
+        this->Actors->SetLevel( 0, rep->GetValue( ) );
+    }
+  SliderCallback( )
+    : vtkCommand( )
+    {
+    }
+
+public:
+  TMPRActors* Actors;
+};
+
+// -------------------------------------------------------------------------
+struct Slider
+{
+  vtkSmartPointer< vtkSliderRepresentation2D > Representation;
+  vtkSmartPointer< vtkSliderWidget >           Widget;
+
+  Slider(
+    double min_value,
+    double max_value,
+    double value,
+    const std::string& title,
+    vtkRenderWindowInteractor* iren,
+    double p1x, double p1y, double p2x, double p2y,
+    SliderCallback* callback = NULL
+    )
+    {
+      this->Representation =
+        vtkSmartPointer< vtkSliderRepresentation2D >::New( );
+      this->Representation->SetMinimumValue( min_value );
+      this->Representation->SetMaximumValue( max_value );
+      this->Representation->SetValue( value );
+      this->Representation->SetTitleText( title.c_str( ) );
+      this->Representation->GetPoint1Coordinate( )->
+        SetCoordinateSystemToNormalizedDisplay();
+      this->Representation->GetPoint1Coordinate( )->
+        SetValue( p1x, p1y );
+      this->Representation->GetPoint2Coordinate( )->
+        SetCoordinateSystemToNormalizedDisplay();
+      this->Representation->GetPoint2Coordinate( )->SetValue( p2x, p2y );
+
+      this->Widget = vtkSmartPointer< vtkSliderWidget >::New( );
+      this->Widget->SetInteractor( iren );
+      this->Widget->SetRepresentation( this->Representation );
+      this->Widget->SetAnimationModeToAnimate( );
+      this->Widget->EnabledOn( );
+      if( callback != NULL )
+        this->Widget->AddObserver( vtkCommand::InteractionEvent, callback );
+    }
+};
+
+// -------------------------------------------------------------------------
 int main( int argc, char* argv[] )
 {
   if( argc < 3 )
@@ -25,20 +121,15 @@ int main( int argc, char* argv[] )
   std::string plugins_file = argv[ 1 ];
 
   // Create interface
-  typedef cpPlugins::Interface::Interface TInterface;
-  typedef TInterface::TClasses            TClasses;
-
   TInterface plugins;
   plugins.Load( plugins_file );
 
-  // Create objects
-  typedef cpPlugins::Interface::ProcessObject TProcessObject;
-  typedef cpPlugins::Interface::Parameters    TParameters;
-  cpPlugins::Interface::ProcessObject::Pointer reader;
-  reader = plugins.CreateProcessObject( "cpPlugins::ImageReader" );
+  // Create reader
+  TFilter::Pointer reader =
+    plugins.CreateProcessObject( "cpPlugins::ImageReader" );
   if( reader.IsNull( ) )
   {
-    std::cerr << "No suitable reader found in plugins." << std::endl;
+    std::cerr << "No suitable image reader found in plugins." << std::endl;
     return( 1 );
 
   } // fi
@@ -57,10 +148,10 @@ int main( int argc, char* argv[] )
     return( 1 );
 
   } // fi
-  cpPlugins::Interface::Image* image =
-    dynamic_cast< cpPlugins::Interface::Image* >( reader->GetOutput( 0 ) );
-  vtkImageData* vtk_image = image->GetVTKImageData( );
-  if( vtk_image == NULL )
+
+  // Get input image's vtk representation
+  vtkImageData* image = reader->GetOutput< TImage >( 0 )->GetVTKImageData( );
+  if( image == NULL )
   {
     std::cerr
       << "ERROR: read image does not have a valid VTK conversion."
@@ -85,13 +176,51 @@ int main( int argc, char* argv[] )
   interactor->SetRenderWindow( window );
 
   // Actors
-  cpExtensions::Visualization::MPRObjects mpr;
-  mpr.SetImage( vtk_image );
-  /*
-    mpr.AssociatePlaneInteractor( 0, interactor );
-    mpr.AssociatePlaneInteractor( 1, interactor );
-    mpr.AssociatePlaneInteractor( 2, interactor );
-  */
+  vtkSmartPointer< TMPRActors > mpr_actors =
+    vtkSmartPointer< TMPRActors >::New( );
+  mpr_actors->AddInputData( image );
+  mpr_actors->PushDataInto( NULL, NULL, NULL, renderer );
+
+  // Callbacks
+  vtkSmartPointer< SliderCallback > cb =
+    vtkSmartPointer< SliderCallback >::New( );
+  cb->Actors = mpr_actors;
+
+  Slider x_slider(
+    mpr_actors->GetSliceNumberMinValue( 0 ),
+    mpr_actors->GetSliceNumberMaxValue( 0 ),
+    mpr_actors->GetSliceNumberMinValue( 0 ),
+    "X", interactor,
+    0.100, 0.15, 0.290, 0.15, cb
+    );
+  Slider y_slider(
+    mpr_actors->GetSliceNumberMinValue( 1 ),
+    mpr_actors->GetSliceNumberMaxValue( 1 ),
+    mpr_actors->GetSliceNumberMinValue( 1 ),
+    "Y", interactor,
+    0.300, 0.15, 0.490, 0.15, cb
+    );
+  Slider z_slider(
+    mpr_actors->GetSliceNumberMinValue( 2 ),
+    mpr_actors->GetSliceNumberMaxValue( 2 ),
+    mpr_actors->GetSliceNumberMinValue( 2 ),
+    "Z", interactor,
+    0.500, 0.15, 0.690, 0.15, cb
+    );
+  Slider w_slider(
+    mpr_actors->GetMinWindow( 0 ),
+    mpr_actors->GetMaxWindow( 0 ),
+    mpr_actors->GetWindow( 0 ),
+    "Window", interactor,
+    0.100, 0.05, 0.290, 0.05, cb
+    );
+  Slider l_slider(
+    mpr_actors->GetMinWindow( 0 ),
+    mpr_actors->GetMaxWindow( 0 ),
+    mpr_actors->GetWindow( 0 ),
+    "Level", interactor,
+    0.300, 0.05, 0.490, 0.05, cb
+    );
 
   // Begin interaction
   renderer->ResetCamera( );