]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Visualization/ImageSliceActors.cxx
half way work...
[cpPlugins.git] / lib / cpExtensions / Visualization / ImageSliceActors.cxx
index 5722c583d608b0159ebe9537e61ca5fcbcb84909..c9b8153e131c7480ac9f2144e90ce2447c070237 100644 (file)
@@ -3,6 +3,7 @@
 #include <sstream>
 
 #include <vtkAlgorithmOutput.h>
+#include <vtkCamera.h>
 #include <vtkCellArray.h>
 #include <vtkImageData.h>
 #include <vtkInformation.h>
@@ -10,6 +11,9 @@
 #include <vtkPoints.h>
 #include <vtkProperty.h>
 #include <vtkRenderer.h>
+#include <vtkRendererCollection.h>
+#include <vtkRenderWindow.h>
+#include <vtkRenderWindowInteractor.h>
 #include <vtkStreamingDemandDrivenPipeline.h>
 #include <vtkTextProperty.h>
 
@@ -56,6 +60,9 @@ Clear( )
   this->AssociatedActors.clear( );
 
   // Reconfigure unique objects
+  this->Cursor = vtkSmartPointer< vtkPolyData >::New( );
+  this->CursorMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
+  this->CursorActor = vtkSmartPointer< vtkActor >::New( );
   this->PlaneFunction = vtkSmartPointer< vtkPlane >::New( );
   this->PlaneSource   = vtkSmartPointer< vtkPolyData >::New( );
   this->PlaneMapper   = vtkSmartPointer< vtkPolyDataMapper >::New( );
@@ -64,6 +71,25 @@ Clear( )
   this->TextBuffer[ 0 ] = '\0';
 
   // Unique objects configuration
+  vtkSmartPointer< vtkPoints > cursor_points =
+    vtkSmartPointer< vtkPoints >::New( );
+  vtkSmartPointer< vtkCellArray > cursor_lines =
+    vtkSmartPointer< vtkCellArray >::New( );
+  cursor_points->InsertNextPoint( 0, 0, 0 );
+  cursor_points->InsertNextPoint( 0, 0, 0 );
+  cursor_points->InsertNextPoint( 0, 0, 0 );
+  cursor_points->InsertNextPoint( 0, 0, 0 );
+  cursor_lines->InsertNextCell( 2 );
+  cursor_lines->InsertCellPoint( 0 );
+  cursor_lines->InsertCellPoint( 1 );
+  cursor_lines->InsertNextCell( 2 );
+  cursor_lines->InsertCellPoint( 2 );
+  cursor_lines->InsertCellPoint( 3 );
+  this->Cursor->SetPoints( cursor_points );
+  this->Cursor->SetLines( cursor_lines );
+  this->CursorMapper->SetInputData( this->Cursor );
+  this->CursorActor->SetMapper( this->CursorMapper );
+
   vtkSmartPointer< vtkPoints > plane_points =
     vtkSmartPointer< vtkPoints >::New( );
   vtkSmartPointer< vtkCellArray > plane_lines =
@@ -116,26 +142,72 @@ GetStyle( ) const
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-PushActorsInto( vtkRenderer* renderer )
+PushActorsInto( vtkRenderWindow* window )
 {
-  unsigned int N = this->GetNumberOfImageActors( );
-  for( unsigned int n = 0; n < N; ++n )
-    renderer->AddActor( this->GetImageActor( n ) );
-  renderer->AddActor( this->GetTextActor( ) );
-  renderer->AddActor( this->GetPlaneActor( ) );
-  renderer->Modified( );
+  vtkRenderWindowInteractor* rwi = window->GetInteractor( );
+  vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
+
+  // Update style
+  if( rwi != NULL )
+  {
+    if( rwi->GetInteractorStyle( ) != this->Style.GetPointer( ) )
+      rwi->SetInteractorStyle( this->Style );
+
+  } // fi
+
+  if( renderer != NULL )
+  {
+    // Configure camera
+    vtkCamera* camera = renderer->GetActiveCamera( );
+    if( camera == NULL )
+      return;
+
+    // Parallel projections are better when displaying 2D images
+    int axis = this->GetAxis( );
+    camera->ParallelProjectionOn( );
+    camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
+    if( axis == 0 )
+    {
+      camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
+      camera->SetViewUp  ( double( 0 ), double( 1 ), double( 0 ) );
+    }
+    else if( axis == 1 )
+    {
+      camera->SetPosition( double( 0 ), double( 1 ), double(  0 ) );
+      camera->SetViewUp  ( double( 0 ), double( 0 ), double( -1 ) );
+    }
+    else // if( axis == 2 )
+    {
+      camera->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
+      camera->SetViewUp  ( double( 0 ), double( 1 ), double( 0 ) );
+
+    } // fi
+    renderer->ResetCamera( );
+
+    // Update actors
+    unsigned int N = this->GetNumberOfImageActors( );
+    for( unsigned int n = 0; n < N; ++n )
+      renderer->AddActor( this->GetImageActor( n ) );
+    renderer->AddActor( this->CursorActor );
+    renderer->AddActor( this->PlaneActor );
+    renderer->AddActor( this->TextActor );
+    renderer->Modified( );
+
+  } // fi
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-PopActorsFrom( vtkRenderer* renderer )
+PopActorsFrom( vtkRenderWindow* window )
 {
+  /*
   unsigned int N = this->GetNumberOfImageActors( );
   for( unsigned int n = 0; n < N; ++n )
     renderer->RemoveActor( this->GetImageActor( n ) );
   renderer->RemoveActor( this->GetTextActor( ) );
   renderer->RemoveActor( this->GetPlaneActor( ) );
   renderer->Modified( );
+  */
 }
 
 // -------------------------------------------------------------------------
@@ -273,6 +345,53 @@ GetDisplayBounds( double bounds[ 6 ] ) const
     this->ImageActors[ 0 ]->GetDisplayBounds( bounds );
 }
 
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::ImageSliceActors::
+ResetCursor( )
+{
+  vtkPoints* points = this->Cursor->GetPoints( );
+  points->SetPoint( 0, 0, 0, 0 );
+  points->SetPoint( 1, 0, 0, 0 );
+  points->SetPoint( 2, 0, 0, 0 );
+  points->SetPoint( 3, 0, 0, 0 );
+  this->Cursor->Modified( );
+  this->CursorMapper->Modified( );
+  this->CursorActor->Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::ImageSliceActors::
+SetCursor( double pos[ 3 ] )
+{
+  if( this->SliceMappers.size( ) == 0 )
+    return;
+
+  int a0 = this->GetAxis( );
+  int a1 = ( a0 + 1 ) % 3;
+  int a2 = ( a0 + 2 ) % 3;
+
+  double bounds[ 6 ];
+  this->SliceMappers[ 0 ]->GetBounds( bounds );
+
+  double p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ];
+  p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = pos[ a0 ];
+  p0[ a2 ] = p1[ a2 ] = pos[ a2 ];
+  p2[ a1 ] = p3[ a1 ] = pos[ a1 ];
+  p0[ a1 ] = bounds[ a1 << 1 ];
+  p1[ a1 ] = bounds[ ( a1 << 1 ) + 1 ];
+  p2[ a2 ] = bounds[ a2 << 1 ];
+  p3[ a2 ] = bounds[ ( a2 << 1 ) + 1 ];
+
+  vtkPoints* points = this->Cursor->GetPoints( );
+  points->SetPoint( 0, p0 );
+  points->SetPoint( 1, p1 );
+  points->SetPoint( 2, p2 );
+  points->SetPoint( 3, p3 );
+  this->Cursor->Modified( );
+  this->CursorMapper->Modified( );
+  this->CursorActor->Modified( );
+}
+
 // -------------------------------------------------------------------------
 int cpExtensions::Visualization::ImageSliceActors::
 GetAxis( ) const
@@ -484,12 +603,22 @@ UpdateText( double pos[ 3 ] )
     if     ( axId == 0 ) axis = 'X';
     else if( axId == 1 ) axis = 'Y';
     else if( axId == 2 ) axis = 'Z';
+    int slice = this->GetSliceNumber( );
 
     vtkImageData* image = this->SliceMappers[ 0 ]->GetInput( );
 
     int ijk[ 3 ];
     double pcoords[ 3 ];
     image->ComputeStructuredCoordinates( pos, ijk, pcoords );
+    ijk[ axId ] = slice;
+
+    int ext[ 6 ];
+    image->GetExtent( ext );
+    if(
+      ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
+      ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
+      ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
+      )
     {
       int nScl = image->GetNumberOfScalarComponents( );
       std::stringstream str;
@@ -498,9 +627,7 @@ UpdateText( double pos[ 3 ] )
         << "," << ijk[ 1 ]
         << "," << ijk[ 2 ] << "]=(";
       str <<
-        image->GetScalarComponentAsFloat(
-          ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0
-          );
+        image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
       for( int n = 1; n < nScl; ++n )
         str
           << " "
@@ -510,9 +637,7 @@ UpdateText( double pos[ 3 ] )
       str << ")";
       std::sprintf(
         this->TextBuffer, "Axis: %c (%d) | Pixel %s",
-        axis,
-        this->SliceMappers[ 0 ]->GetSliceNumber( ),
-        str.str( ).c_str( )
+        axis, slice, str.str( ).c_str( )
         );
 
     } // fi
@@ -596,8 +721,9 @@ _ConfigureNewInput( int axis )
 
   if( nImages == 0 )
   {
-    this->AddItem( this->TextActor );
+    this->AddItem( this->CursorActor );
     this->AddItem( this->PlaneActor );
+    this->AddItem( this->TextActor );
     this->Style->AssociateImageActor( actor );
 
   } // fi
@@ -653,6 +779,24 @@ _MouseDoubleClickCommand(
   bool alt, bool ctr, bool sft
   )
 {
+  ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
+  if( actors == NULL )
+    return;
+
+  if( btn == ImageInteractorStyle::ButtonID_Left )
+  {
+    if( !alt && !ctr && !sft )
+    {
+      actors->SetCursor( pos );
+
+    } // fi
+  }
+  else if( btn == ImageInteractorStyle::ButtonID_Middle )
+  {
+  }
+  else if( btn == ImageInteractorStyle::ButtonID_Right )
+  {
+  } // fi
 }
 
 // -------------------------------------------------------------------------
@@ -662,6 +806,19 @@ _MouseWheelCommand(
   const int& dir, bool alt, bool ctr, bool sft
   )
 {
+  ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
+  if( actors == NULL )
+    return;
+
+  if( !alt && !ctr && !sft )
+  {
+    actors->SetSliceNumber( actors->GetSliceNumber( ) + dir );
+  }
+  else if( !alt && !ctr && sft )
+  {
+    actors->SetSliceNumber( actors->GetSliceNumber( ) + ( dir * 10 ) );
+
+  } // fi
 }
 
 // -------------------------------------------------------------------------