]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Interaction/ImageSliceStyle.cxx
...
[cpPlugins.git] / lib / cpExtensions / Interaction / ImageSliceStyle.cxx
index a6731ed017f40b8a660c487144d0c7d6e910d1e1..7b9df5aea6ab5645a713b936475f079b3d301816 100644 (file)
@@ -1,7 +1,19 @@
 #include <cpExtensions/Interaction/ImageSliceStyle.h>
+#include <cpExtensions/Visualization/CursorActors.h>
+#include <cpExtensions/Visualization/LUTImageActor.h>
 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
-#include <cpExtensions/Visualization/ImageViewerActors.h>
+#include <vtkAssemblyPath.h>
+#include <vtkCommand.h>
+#include <vtkImageData.h>
+#include <vtkImageSlice.h>
+#include <vtkProperty.h>
+#include <vtkPropCollection.h>
 #include <vtkPropPicker.h>
+#include <vtkRenderer.h>
+#include <vtkRenderWindowInteractor.h>
+#include <vtkTextActor.h>
+#include <vtkTextProperty.h>
+#include <sstream>
 
 // -------------------------------------------------------------------------
 cpExtensions::Interaction::ImageSliceStyle::
@@ -12,6 +24,121 @@ New( )
 }
 
 // -------------------------------------------------------------------------
+int cpExtensions::Interaction::ImageSliceStyle::
+GetSliceNumber( ) const
+{
+  if( this->m_WLActor.GetPointer( ) != NULL )
+    return( this->m_WLActor->GetSliceNumber( ) );
+  else
+    return( -1 );
+}
+
+// -------------------------------------------------------------------------
+int cpExtensions::Interaction::ImageSliceStyle::
+GetOrientation( ) const
+{
+  if( this->m_WLActor.GetPointer( ) != NULL )
+    return( this->m_WLActor->GetOrientation( ) );
+  else
+    return( -1 );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Interaction::ImageSliceStyle::
+OnMouseMove( )
+{
+  static bool cursor_visible = false;
+
+  int idx[ 2 ];
+  double pnt[ 3 ];
+  if( this->_PickPointOnImageActor( idx, pnt ) )
+  {
+    this->m_Cursor->SetCursor( pnt, false );
+    this->_ShowText( pnt );
+    if( !cursor_visible )
+    {
+      this->m_Cursor->VisibilityOn( );
+      this->m_Text->VisibilityOn( );
+      cursor_visible = true;
+
+    } // fi
+    this->Interactor->Render( );
+  }
+  else
+  {
+    if( cursor_visible )
+    {
+      this->m_Cursor->VisibilityOff( );
+      this->m_Text->VisibilityOff( );
+      cursor_visible = false;
+      this->Interactor->Render( );
+
+    } // fi
+
+  } // fi
+
+  this->Overclass::OnMouseMove( );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Interaction::ImageSliceStyle::
+OnMouseWheelForward( )
+{
+  static int s = 0;
+  if( this->m_WLActor.GetPointer( ) == NULL )
+    return;
+  s = this->m_WLActor->GetSliceNumber( );
+  s += ( this->Interactor->GetShiftKey( ) == 1 )? 10: 1;
+  this->m_WLActor->SetSliceNumber( s );
+  s = this->m_WLActor->GetSliceNumber( );
+  if( this->m_LUTActor.GetPointer( ) != NULL )
+    this->m_LUTActor->SetSliceNumber( s );
+  this->InvokeEvent( vtkCommand::UserEvent + 2, &s );
+  this->Interactor->Render( );
+  this->OnMouseMove( );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Interaction::ImageSliceStyle::
+OnMouseWheelBackward( )
+{
+  static int s = 0;
+  if( this->m_WLActor.GetPointer( ) == NULL )
+    return;
+  s = this->m_WLActor->GetSliceNumber( );
+  s -= ( this->Interactor->GetShiftKey( ) == 1 )? 10: 1;
+  this->m_WLActor->SetSliceNumber( s );
+  s = this->m_WLActor->GetSliceNumber( );
+  if( this->m_LUTActor.GetPointer( ) != NULL )
+    this->m_LUTActor->SetSliceNumber( s );
+  this->InvokeEvent( vtkCommand::UserEvent + 2, &s );
+  this->Interactor->Render( );
+  this->OnMouseMove( );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Interaction::ImageSliceStyle::
+OnChar( )
+{
+  switch( this->Interactor->GetKeyCode( ) )
+  {
+  case 'r': case 'R':
+  {
+    this->FindPokedRenderer(
+      this->Interactor->GetEventPosition( )[ 0 ],
+      this->Interactor->GetEventPosition( )[ 1 ]
+      );
+    this->CurrentRenderer->ResetCamera( );
+    this->Interactor->Render( );
+  }
+  break;
+  default:
+    break;
+
+  } // hctiws
+}
+
+/* TODO
 cpExtensions::Visualization::ImageViewerActors*
 cpExtensions::Interaction::ImageSliceStyle::
 GetActors( )
@@ -110,12 +237,39 @@ void cpExtensions::Interaction::ImageSliceStyle::
 OnChar( )
 {
 }
+*/
 
 // -------------------------------------------------------------------------
 cpExtensions::Interaction::ImageSliceStyle::
 ImageSliceStyle( )
   : Superclass( )
 {
+  this->SetInteractionModeToImage2D( );
+  this->m_PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
+  this->m_PropPicker->PickFromListOn( );
+
+  this->m_Cursor = vtkSmartPointer< TCursor >::New( );
+  this->m_Cursor->VisibilityOff( );
+  this->m_Cursor->GetProperty( 0 )->SetColor( 1, 1, 0 );
+  this->m_Cursor->GetProperty( 1 )->SetColor( 1, 1, 0 );
+
+  // Prepare text
+  this->m_TextBuffer[ 0 ] = '\0';
+  this->m_Text = vtkSmartPointer< vtkTextActor >::New( );
+  this->m_Text->SetTextScaleModeToNone( );
+  auto textprop = this->m_Text->GetTextProperty( );
+  textprop->SetColor( 1, 1, 0 );
+  textprop->SetFontFamilyToCourier( );
+  textprop->SetFontSize( 12 );
+  textprop->BoldOff( );
+  textprop->ItalicOff( );
+  textprop->ShadowOff( );
+  textprop->SetJustificationToLeft( );
+  textprop->SetVerticalJustificationToBottom( );
+  auto coord = this->m_Text->GetPositionCoordinate( );
+  coord->SetCoordinateSystemToNormalizedViewport( );
+  coord->SetValue( 0.01, 0.02 );
+  this->m_Text->VisibilityOff( );
 }
 
 // -------------------------------------------------------------------------
@@ -126,8 +280,9 @@ cpExtensions::Interaction::ImageSliceStyle::
 
 // -------------------------------------------------------------------------
 bool cpExtensions::Interaction::ImageSliceStyle::
-_PickPosition( int idx[ 2 ], double pos[ 3 ] )
+_PickPointOnImageActor( int idx[ 2 ], double pnt[ 3 ] )
 {
+  // Check if data is valid
   if(
     this->Interactor == NULL ||
     this->CurrentRenderer == NULL ||
@@ -140,6 +295,42 @@ _PickPosition( int idx[ 2 ], double pos[ 3 ] )
   idx[ 1 ] = this->Interactor->GetEventPosition( )[ 1 ];
   this->FindPokedRenderer( double( idx[ 0 ] ), double( idx[ 1 ] ) );
 
+  // Check if the image has changed
+  this->Interactor->GetPicker( )->
+    Pick( idx[ 0 ], idx[ 1 ], 0.0, this->CurrentRenderer );
+  auto picker =
+    vtkAbstractPropPicker::SafeDownCast( this->Interactor->GetPicker( ) );
+  if( picker == NULL )
+    return( false );
+  TWLActor* curr_actor = dynamic_cast< TWLActor* >( picker->GetProp3D( ) );
+  if( curr_actor != this->m_WLActor.GetPointer( ) && curr_actor != NULL )
+  {
+    this->m_WLActor = curr_actor;
+    this->m_PropPicker->GetPickList( )->RemoveAllItems( );
+    this->m_PropPicker->AddPickList( curr_actor );
+
+    auto props = this->CurrentRenderer->GetViewProps( );
+    this->m_LUTActor = NULL;
+    props->InitTraversal( );
+    vtkProp* prop;
+    while( ( ( prop = props->GetNextProp( ) ) != NULL ) && ( this->m_LUTActor.GetPointer( ) == NULL ) )
+      this->m_LUTActor = dynamic_cast< TLUTActor* >( prop );
+
+    this->m_Cursor->SetImageBounds( curr_actor->GetImage( )->GetBounds( ) );
+    this->m_Cursor->SetImageOrientation( curr_actor->GetOrientation( ) );
+    this->m_Cursor->InitTraversal( );
+    while( vtkProp* prop = this->m_Cursor->GetNextProp( ) )
+      this->CurrentRenderer->AddViewProp( prop );
+    this->CurrentRenderer->AddViewProp( this->m_Text );
+
+  } // fi
+  if( this->m_WLActor.GetPointer( ) == NULL )
+  {
+    this->m_PropPicker->GetPickList( )->RemoveAllItems( );
+    return( false );
+
+  } // fi
+
   // Pick a 3D position
   int r = this->m_PropPicker->Pick(
     double( idx[ 0 ] ), double( idx[ 1 ] ), double( 0 ),
@@ -147,8 +338,77 @@ _PickPosition( int idx[ 2 ], double pos[ 3 ] )
     );
   if( r == 0 )
     return( false );
-  this->m_PropPicker->GetPickPosition( pos );
+  this->m_PropPicker->GetPickPosition( pnt );
   return( true );
 }
 
+// -------------------------------------------------------------------------
+void cpExtensions::Interaction::ImageSliceStyle::
+_CorrectPosition( double pos[ 3 ], int ijk[ 3 ] )
+{
+  if( this->m_WLActor.GetPointer( ) == NULL )
+    return;
+  auto image = this->m_WLActor->GetImage( );
+  if( image == NULL )
+    return;
+
+  // Approximate image index
+  double pcoords[ 3 ];
+  image->ComputeStructuredCoordinates( pos, ijk, pcoords );
+
+  // Manually correct index
+  int ext[ 6 ];
+  image->GetExtent( ext );
+  for( int i = 0; i < 3; ++i )
+  {
+    if( ijk[ i ] < ext[ i << 1 ] )
+      ijk[ i ] = ext[ i << 1 ];
+    if( ext[ ( i << 1 ) + 1 ] < ijk[ i ] )
+      ijk[ i ] = ext[ ( i << 1 ) + 1 ];
+
+  } // rof
+
+  // Get real coordinates
+  int o = this->m_WLActor->GetOrientation( );
+  ijk[ o ] = this->m_WLActor->GetSliceNumber( );
+  image->GetPoint( image->ComputePointId( ijk ), pos );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Interaction::ImageSliceStyle::
+_ShowText( double pos[ 3 ] )
+{
+  if( this->m_WLActor.GetPointer( ) == NULL )
+    return;
+  auto image = this->m_WLActor->GetImage( );
+  if( image == NULL )
+    return;
+
+  int ijk[ 3 ];
+  this->_CorrectPosition( pos, ijk );
+  int o = this->m_WLActor->GetOrientation( );
+
+  std::stringstream buffer;
+  buffer << "Axis: " << char( 'X' + char( o ) ) << std::endl;
+  int nScl = image->GetNumberOfScalarComponents( );
+  buffer
+    << "Pixel: [" << ijk[ 0 ]
+    << "," << ijk[ 1 ]
+    << "," << ijk[ 2 ] << "] = < "
+    << image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
+  for( int n = 1; n < nScl; ++n )
+    buffer
+      << " : "
+      << image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n );
+  buffer << " >" << std::endl;
+  buffer
+    << "Point: ("
+    << pos[ 0 ] << ", " << pos[ 1 ] << ", " << pos[ 2 ]
+    << ")";
+  cpExtensions_SPRINTF( this->m_TextBuffer, 1024, buffer.str( ).c_str( ) );
+  this->m_Text->SetInput( this->m_TextBuffer );
+  this->m_Text->VisibilityOn( );
+  this->m_Text->Modified( );
+}
+
 // eof - $RCSfile$