]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Extensions/Visualization/Image3DMPR.cxx
3D MPR updated
[cpPlugins.git] / lib / cpPlugins / Extensions / Visualization / Image3DMPR.cxx
diff --git a/lib/cpPlugins/Extensions/Visualization/Image3DMPR.cxx b/lib/cpPlugins/Extensions/Visualization/Image3DMPR.cxx
new file mode 100644 (file)
index 0000000..9d31298
--- /dev/null
@@ -0,0 +1,364 @@
+#include <cpPlugins/Extensions/Visualization/Image3DMPR.h>
+
+#include <vtkCommand.h>
+#include <vtkInteractorStyleSwitch.h>
+
+#include <cpPlugins/Extensions/Visualization/SeedWidgetCorrector.h>
+
+/**
+ */
+class cpPlugins_Extensions_Visualization_InteractionModeCallback
+  : public vtkCommand
+{
+public:
+  static cpPlugins_Extensions_Visualization_InteractionModeCallback* New( )
+    {
+      return( new cpPlugins_Extensions_Visualization_InteractionModeCallback );
+    }
+  virtual void Execute(
+    vtkObject* caller, unsigned long eventId, void* arguments )
+    {
+      if( eventId == vtkCommand::KeyPressEvent )
+      {
+        vtkRenderWindowInteractor* iren = 
+          static_cast< vtkRenderWindowInteractor* >( caller );
+        if( caller == NULL )
+          return;
+
+        switch( iren->GetKeyCode( ) )
+        {
+        case 'x':
+        case 'X':
+        {
+          bool eSeed = ( this->SeedWidget->GetProcessEvents( ) == 1 );
+          if( eSeed )
+          {
+            this->SeedWidget->ProcessEventsOff( );
+            this->WidgetX->InteractionOn( );
+            this->WidgetY->InteractionOn( );
+            this->WidgetZ->InteractionOn( );
+          }
+          else
+          {
+            this->SeedWidget->ProcessEventsOn( );
+            this->WidgetX->InteractionOff( );
+            this->WidgetY->InteractionOff( );
+            this->WidgetZ->InteractionOff( );
+
+          } // fi
+        }
+        break;
+        case 'z':
+        case 'Z':
+        {
+          this->SeedWidget->ProcessEventsOff( );
+          this->WidgetX->InteractionOff( );
+          this->WidgetY->InteractionOff( );
+          this->WidgetZ->InteractionOff( );
+        }
+        break;
+        default:
+          break;
+
+        } // hctiws
+
+      } // fi
+    }
+
+public:
+  vtkImagePlaneWidget* WidgetX;
+  vtkImagePlaneWidget* WidgetY;
+  vtkImagePlaneWidget* WidgetZ;
+  vtkSeedWidget*       SeedWidget;
+};
+
+// -------------------------------------------------------------------------
+cpPlugins::Extensions::Visualization::Image3DMPR::
+Image3DMPR( )
+{
+  this->m_Outline = vtkSmartPointer< vtkOutlineSource >::New( );
+  this->m_OutlineMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
+  this->m_OutlineActor = vtkSmartPointer< vtkActor >::New( );
+  this->m_Picker = vtkSmartPointer< vtkCellPicker >::New( );
+  this->m_WidgetX = vtkSmartPointer< vtkImagePlaneWidget >::New( );
+  this->m_WidgetY = vtkSmartPointer< vtkImagePlaneWidget >::New( );
+  this->m_WidgetZ = vtkSmartPointer< vtkImagePlaneWidget >::New( );
+  this->m_Renderer = vtkSmartPointer< vtkRenderer >::New( );
+  this->m_Window = vtkSmartPointer< vtkRenderWindow >::New( );
+  this->m_Interactor = vtkSmartPointer< vtkRenderWindowInteractor >::New( );
+  this->m_SeedHandleRepresentation =
+    vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
+  this->m_SeedRepresentation =
+    vtkSmartPointer< vtkSeedRepresentation >::New( );
+  this->m_SeedWidget = vtkSmartPointer<vtkSeedWidget>::New();
+
+  this->m_SeedHandleRepresentation->GetProperty()->SetColor( 1, 0, 1 );
+  this->m_SeedRepresentation->
+    SetHandleRepresentation( this->m_SeedHandleRepresentation );
+  this->m_SeedWidget->SetRepresentation( this->m_SeedRepresentation );
+
+  this->m_WidgetX->KeyPressActivationOff( );
+  this->m_WidgetY->KeyPressActivationOff( );
+  this->m_WidgetZ->KeyPressActivationOff( );
+  this->m_SeedWidget->KeyPressActivationOff( );
+
+  typedef
+    cpPlugins_Extensions_Visualization_InteractionModeCallback
+    _TInteractionCB;
+  vtkSmartPointer< _TInteractionCB > int_cb =
+    vtkSmartPointer< _TInteractionCB >::New( );
+  int_cb->WidgetX = this->m_WidgetX;
+  int_cb->WidgetY = this->m_WidgetY;
+  int_cb->WidgetZ = this->m_WidgetZ;
+  int_cb->SeedWidget = this->m_SeedWidget;
+  this->m_Interactor->AddObserver( vtkCommand::KeyPressEvent, int_cb );
+
+  typedef
+    cpPlugins::Extensions::Visualization::SeedWidgetCorrector
+    _TCorrector;
+  vtkSmartPointer< _TCorrector > corr_cb =
+    vtkSmartPointer< _TCorrector >::New( );
+  corr_cb->Configure( this->m_Picker );
+  this->m_SeedWidget->AddObserver( vtkCommand::PlacePointEvent, corr_cb );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Extensions::Visualization::Image3DMPR::
+~Image3DMPR( )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Extensions::Visualization::Image3DMPR::
+SetImage( vtkImageData* image )
+{
+  this->m_Image = image;
+
+  // Outline
+  this->m_Outline->SetBounds( this->m_Image->GetBounds( ) );
+  this->m_OutlineMapper->
+    SetInputConnection( this->m_Outline->GetOutputPort( ) );
+  this->m_OutlineActor->SetMapper( this->m_OutlineMapper );
+  this->m_OutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
+
+  // Local picker
+  this->m_Picker->SetTolerance( 0.005 );
+
+  // Image planes
+  this->m_WidgetX->DisplayTextOn( );
+  this->m_WidgetX->SetInputData( this->m_Image );
+  this->m_WidgetX->SetPlaneOrientationToXAxes( );
+  this->m_WidgetX->SetSliceIndex( this->m_Image->GetExtent( )[ 0 ] );
+  this->m_WidgetX->SetPicker( this->m_Picker );
+  this->m_WidgetX->SetKeyPressActivationValue( 'x' );
+  this->m_WidgetX->GetPlaneProperty( )->SetLineWidth( 3 );
+  this->m_WidgetX->GetPlaneProperty( )->SetColor( 0, 1, 1 );
+
+  this->m_WidgetY->DisplayTextOn( );
+  this->m_WidgetY->SetInputData( this->m_Image );
+  this->m_WidgetY->SetPlaneOrientationToYAxes( );
+  this->m_WidgetY->SetSliceIndex( this->m_Image->GetExtent( )[ 2 ] );
+  this->m_WidgetY->SetPicker( this->m_Picker );
+  this->m_WidgetY->SetKeyPressActivationValue( 'y' );
+  this->m_WidgetY->GetPlaneProperty( )->SetColor( 1, 0, 1 );
+  this->m_WidgetY->GetPlaneProperty( )->SetLineWidth( 3 );
+  this->m_WidgetY->SetLookupTable( this->m_WidgetX->GetLookupTable( ) );
+
+  this->m_WidgetZ->DisplayTextOn( );
+  this->m_WidgetZ->SetInputData( this->m_Image );
+  this->m_WidgetZ->SetPlaneOrientationToZAxes( );
+  this->m_WidgetZ->SetSliceIndex( this->m_Image->GetExtent( )[ 4 ] );
+  this->m_WidgetZ->SetPicker( this->m_Picker );
+  this->m_WidgetZ->SetKeyPressActivationValue( 'z' );
+  this->m_WidgetZ->GetPlaneProperty( )->SetColor( 1, 1, 0 );
+  this->m_WidgetZ->GetPlaneProperty( )->SetLineWidth( 3 );
+  this->m_WidgetZ->SetLookupTable( this->m_WidgetX->GetLookupTable( ) );
+
+  // Rendering stuff
+  this->m_Window->AddRenderer( this->m_Renderer );
+  this->m_Interactor->SetRenderWindow( this->m_Window );
+
+  // Command
+  double spac[ 3 ];
+  this->m_Image->GetSpacing( spac );
+  double min_spacing = spac[ 0 ];
+  for( unsigned int d = 1; d < 3; d++ )
+    min_spacing = ( spac[ d ] < min_spacing )? spac[ d ]: min_spacing;
+
+  vtkInteractorStyleSwitch* iswitch =
+    dynamic_cast< vtkInteractorStyleSwitch* >(
+      this->m_Interactor->GetInteractorStyle( )
+      );
+  if( iswitch != NULL )
+    iswitch->SetCurrentStyleToTrackballCamera( );
+
+  // Add actors
+  this->m_Renderer->AddActor( this->m_OutlineActor );
+
+  // Prepare widgets
+  this->m_WidgetX->SetInteractor( this->m_Interactor );
+  this->m_WidgetY->SetInteractor( this->m_Interactor );
+  this->m_WidgetZ->SetInteractor( this->m_Interactor );
+  this->m_SeedWidget->SetInteractor( this->m_Interactor );
+
+  this->m_WidgetX->On( );
+  this->m_WidgetY->On( );
+  this->m_WidgetZ->On( );
+  this->m_SeedWidget->On( );
+  this->m_SeedWidget->ProcessEventsOff( );
+  this->m_Interactor->SetPicker( this->m_Picker );
+  /*
+    this->m_Interactor->GetPickingManager( )->AddPicker( this->m_Picker );
+    this->m_Interactor->GetPickingManager( )->EnabledOn( );
+  */
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Extensions::Visualization::Image3DMPR::
+SetBackground( double r, double g, double b )
+{
+  this->m_Renderer->SetBackground( r, g, b );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Extensions::Visualization::Image3DMPR::
+SetSize( unsigned int w, unsigned int h )
+{
+  this->m_Window->SetSize( w, h );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Extensions::Visualization::Image3DMPR::
+SetWindowLevel( double w, double l )
+{
+  this->m_WidgetX->SetWindowLevel( w, l );
+  this->m_WidgetY->SetWindowLevel( w, l );
+  this->m_WidgetZ->SetWindowLevel( w, l );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Extensions::Visualization::Image3DMPR::
+AddPolyData( vtkPolyData* pd, double opacity )
+{
+  unsigned int i = this->m_PolyDatas.size( );
+
+  this->m_PolyDatas.push_back( pd );
+  this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
+  this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
+
+  this->m_Mappers[ i ]->SetInputData( pd );
+  this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
+  this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
+  this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
+  this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 );
+  this->m_Renderer->AddActor( this->m_Actors[ i ] );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Extensions::Visualization::Image3DMPR::
+AddPolyData( vtkPolyData* pd, double r, double g, double b, double opacity )
+{
+  unsigned int i = this->m_PolyDatas.size( );
+
+  this->m_PolyDatas.push_back( pd );
+  this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
+  this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
+
+  this->m_Mappers[ i ]->SetInputData( pd );
+  this->m_Mappers[ i ]->ScalarVisibilityOff( );
+  this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
+  this->m_Actors[ i ]->GetProperty( )->SetColor( r, g, b );
+  this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
+  this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
+  this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 );
+  this->m_Renderer->AddActor( this->m_Actors[ i ] );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Extensions::Visualization::Image3DMPR::
+AddPolyData( vtkPolyData* pd, vtkLookupTable* lut, double opacity )
+{
+  unsigned int i = this->m_PolyDatas.size( );
+
+  this->m_PolyDatas.push_back( pd );
+  this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
+  this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
+
+  this->m_Mappers[ i ]->SetInputData( pd );
+  this->m_Mappers[ i ]->SetLookupTable( lut );
+  this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
+  this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
+  this->m_Renderer->AddActor( this->m_Actors[ i ] );
+}
+
+// -------------------------------------------------------------------------
+unsigned int cpPlugins::Extensions::Visualization::Image3DMPR::
+GetNumberOfSeeds( ) const
+{
+  return( this->m_SeedRepresentation->GetNumberOfSeeds( ) );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Extensions::Visualization::Image3DMPR::
+GetSeed( int n, double* s ) const
+{
+  vtkHandleWidget* hWdg = this->m_SeedWidget->GetSeed( n );
+  if( hWdg == NULL )
+    return;
+  vtkHandleRepresentation* hRep =
+    dynamic_cast< vtkHandleRepresentation* >( hWdg->GetRepresentation( ) );
+  if( hRep == NULL )
+    return;
+  hRep->GetWorldPosition( s );
+}
+
+// -------------------------------------------------------------------------
+unsigned int cpPlugins::Extensions::Visualization::Image3DMPR::
+AddSeed( const double& x, const double& y, const double& z ) const
+{
+  double pos[ 3 ] = { x, y, z };
+
+  int hnd_id = this->m_SeedRepresentation->CreateHandle( pos );
+  vtkHandleWidget* hnd = this->m_SeedWidget->CreateNewHandle( );
+  this->m_SeedRepresentation->GetHandleRepresentation( hnd_id )->
+    SetWorldPosition( pos );
+  hnd->SetEnabled( 1 );
+
+  return( this->GetNumberOfSeeds( ) - 1 );
+}
+
+// -------------------------------------------------------------------------
+vtkRenderWindow* cpPlugins::Extensions::Visualization::Image3DMPR::
+GetWindow( ) const
+{
+  return( this->m_Window );
+}
+
+// -------------------------------------------------------------------------
+vtkRenderer* cpPlugins::Extensions::Visualization::Image3DMPR::
+GetRenderer( ) const
+{
+  return( this->m_Renderer );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Extensions::Visualization::Image3DMPR::
+Start( )
+{
+  this->m_WidgetX->On( );
+  this->m_WidgetY->On( );
+  this->m_WidgetZ->On( );
+
+  this->m_Renderer->ResetCamera( );
+  this->m_Interactor->Initialize( );
+  this->m_Interactor->Start( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Extensions::Visualization::Image3DMPR::
+Render( )
+{
+  this->m_Window->Render( );
+}
+
+// eof - $RCSfile$