]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Interaction/ImageSliceStyle.cxx
yet another refactoring
[cpPlugins.git] / lib / cpExtensions / Interaction / ImageSliceStyle.cxx
diff --git a/lib/cpExtensions/Interaction/ImageSliceStyle.cxx b/lib/cpExtensions/Interaction/ImageSliceStyle.cxx
new file mode 100644 (file)
index 0000000..7b9df5a
--- /dev/null
@@ -0,0 +1,414 @@
+#include <cpExtensions/Interaction/ImageSliceStyle.h>
+#include <cpExtensions/Visualization/CursorActors.h>
+#include <cpExtensions/Visualization/LUTImageActor.h>
+#include <cpExtensions/Visualization/WindowLevelImageActor.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::
+Self* cpExtensions::Interaction::ImageSliceStyle::
+New( )
+{
+  return( new Self( ) );
+}
+
+// -------------------------------------------------------------------------
+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( )
+{
+  return( this->m_Actors );
+}
+
+// -------------------------------------------------------------------------
+const cpExtensions::Visualization::ImageViewerActors*
+cpExtensions::Interaction::ImageSliceStyle::
+GetActors( ) const
+{
+  return( this->m_Actors );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Interaction::ImageSliceStyle::
+SetActors( cpExtensions::Visualization::ImageViewerActors* actors )
+{
+  this->m_Actors = actors;
+  if( actors != NULL )
+  {
+    this->m_PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
+    this->m_PropPicker->PickFromListOn( );
+    this->m_PropPicker->GetPickList( )->RemoveAllItems( );
+    this->m_PropPicker->
+      AddPickList( this->m_Actors->GetWindowLevelImageActor( ) );
+  }
+  else
+    this->m_PropPicker = NULL;
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Interaction::ImageSliceStyle::
+OnMouseMove( )
+{
+  this->Superclass::OnMouseMove( );
+  if( this->m_Actors.GetPointer( ) == NULL )
+    return;
+
+  // Slice synch
+  static int idx[ 2 ];
+  static double pos[ 3 ];
+  if( this->_PickPosition( idx, pos ) )
+  {
+    if(
+      this->Interactor->GetControlKey( ) == 1 &&
+      this->GetButtonID( ) == Self::ButtonID_Left
+      )
+    {
+      this->m_Actors->SetSuperCursor( pos, this->m_Actors->GetOrientation( ) == 2 );
+      this->InvokeEvent( vtkCommand::UserEvent + 1, pos );
+    }
+    else
+      this->m_Actors->SetCursor( pos, this->m_Actors->GetOrientation( ) == 2 );
+  }
+  else
+    this->m_Actors->HideViewerActors( );
+  this->Interactor->Render( );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Interaction::ImageSliceStyle::
+OnMouseWheelForward( )
+{
+  static int s = 0;
+  if( this->m_Actors.GetPointer( ) == NULL )
+    return;
+  s = this->m_Actors->GetSliceNumber( );
+  s += ( this->Interactor->GetShiftKey( ) == 1 )? 10: 1;
+  this->m_Actors->SetSliceNumber( s );
+  s = this->m_Actors->GetSliceNumber( );
+  this->InvokeEvent( vtkCommand::UserEvent + 2, &s );
+  this->Interactor->Render( );
+  this->OnMouseMove( );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Interaction::ImageSliceStyle::
+OnMouseWheelBackward( )
+{
+  static int s = 0;
+  if( this->m_Actors.GetPointer( ) == NULL )
+    return;
+  s = this->m_Actors->GetSliceNumber( );
+  s -= ( this->Interactor->GetShiftKey( ) == 1 )? 10: 1;
+  this->m_Actors->SetSliceNumber( s );
+  s = this->m_Actors->GetSliceNumber( );
+  this->InvokeEvent( vtkCommand::UserEvent + 2, &s );
+  this->Interactor->Render( );
+  this->OnMouseMove( );
+}
+
+// -------------------------------------------------------------------------
+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( );
+}
+
+// -------------------------------------------------------------------------
+cpExtensions::Interaction::ImageSliceStyle::
+~ImageSliceStyle( )
+{
+}
+
+// -------------------------------------------------------------------------
+bool cpExtensions::Interaction::ImageSliceStyle::
+_PickPointOnImageActor( int idx[ 2 ], double pnt[ 3 ] )
+{
+  // Check if data is valid
+  if(
+    this->Interactor == NULL ||
+    this->CurrentRenderer == NULL ||
+    this->m_PropPicker.GetPointer( ) == NULL
+    )
+    return( false );
+
+  // Find the renderer where the event has been raised
+  idx[ 0 ] = this->Interactor->GetEventPosition( )[ 0 ];
+  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 ),
+    this->CurrentRenderer
+    );
+  if( r == 0 )
+    return( false );
+  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$