]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Visualization/ImageSliceActors.cxx
...
[cpPlugins.git] / lib / cpExtensions / Visualization / ImageSliceActors.cxx
index 5722c583d608b0159ebe9537e61ca5fcbcb84909..d6542efbd25152f0870fa8b2a2645827d67407c4 100644 (file)
@@ -1,93 +1,44 @@
 #include <cpExtensions/Visualization/ImageSliceActors.h>
 
 #include <sstream>
-
-#include <vtkAlgorithmOutput.h>
-#include <vtkCellArray.h>
+#include <vtkCamera.h>
 #include <vtkImageData.h>
-#include <vtkInformation.h>
+#include <vtkImageProperty.h>
 #include <vtkPlane.h>
-#include <vtkPoints.h>
 #include <vtkProperty.h>
 #include <vtkRenderer.h>
-#include <vtkStreamingDemandDrivenPipeline.h>
+#include <vtkRenderWindow.h>
 #include <vtkTextProperty.h>
 
 // -------------------------------------------------------------------------
-cpExtensions::Visualization::ImageSliceActors*
 cpExtensions::Visualization::ImageSliceActors::
+Self* cpExtensions::Visualization::ImageSliceActors::
 New( )
 {
   return( new Self( ) );
 }
 
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::ImageSliceActors::
-AddInputConnection( vtkAlgorithmOutput* aout, int axis )
-{
-  vtkSmartPointer< vtkImageSliceMapper > mapper =
-    vtkSmartPointer< vtkImageSliceMapper >::New( );
-  this->SliceMappers.push_back( mapper );
-  mapper->SetInputConnection( aout );
-  this->_ConfigureNewInput( axis );
-}
-
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::ImageSliceActors::
-AddInputData( vtkImageData* data, int axis )
-{
-  vtkSmartPointer< vtkImageSliceMapper > mapper =
-    vtkSmartPointer< vtkImageSliceMapper >::New( );
-  this->SliceMappers.push_back( mapper );
-  mapper->SetInputData( data );
-  this->_ConfigureNewInput( axis );
-}
-
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
 Clear( )
 {
-  // Unbind from container
   this->RemoveAllItems( );
 
-  // Delete all images
-  this->SliceMappers.clear( );
-  this->ImageActors.clear( );
-  this->AssociatedActors.clear( );
-
-  // Reconfigure unique objects
-  this->PlaneFunction = vtkSmartPointer< vtkPlane >::New( );
-  this->PlaneSource   = vtkSmartPointer< vtkPolyData >::New( );
-  this->PlaneMapper   = vtkSmartPointer< vtkPolyDataMapper >::New( );
-  this->TextActor     = vtkSmartPointer< vtkTextActor >::New( );
-  this->PlaneActor    = vtkSmartPointer< vtkActor >::New( );
-  this->TextBuffer[ 0 ] = '\0';
-
-  // Unique objects configuration
-  vtkSmartPointer< vtkPoints > plane_points =
-    vtkSmartPointer< vtkPoints >::New( );
-  vtkSmartPointer< vtkCellArray > plane_lines =
-    vtkSmartPointer< vtkCellArray >::New( );
-
-  plane_points->InsertNextPoint( 0, 0, 0 );
-  plane_points->InsertNextPoint( 0, 1, 0 );
-  plane_points->InsertNextPoint( 1, 1, 0 );
-  plane_points->InsertNextPoint( 1, 0, 0 );
-  plane_lines->InsertNextCell( 5 );
-  plane_lines->InsertCellPoint( 0 );
-  plane_lines->InsertCellPoint( 1 );
-  plane_lines->InsertCellPoint( 2 );
-  plane_lines->InsertCellPoint( 3 );
-  plane_lines->InsertCellPoint( 0 );
-  this->PlaneSource->SetPoints( plane_points );
-  this->PlaneSource->SetLines( plane_lines );
-
-  this->PlaneMapper->SetInputData( this->PlaneSource );
-  this->PlaneActor->SetMapper( this->PlaneMapper );
-
-  this->TextActor->SetTextScaleModeToNone( );
-  vtkTextProperty* textprop = this->TextActor->GetTextProperty( );
-  textprop->SetColor( 1, 1, 1 );
+  // Main image
+  this->m_Mapper = vtkSmartPointer< vtkImageSliceMapper >::New( );
+  this->m_Actor = vtkSmartPointer< vtkImageActor >::New( );
+  this->m_Actor->SetMapper( this->m_Mapper );
+
+  // Cursor
+  this->m_Cursor.Create( );
+  this->m_Cursor.Actor->GetProperty( )->SetColor( 1, 1, 0 );
+
+  // Text
+  this->m_TextBuffer[ 0 ] = '\0';
+  this->m_TextActor = vtkSmartPointer< vtkTextActor >::New( );
+  this->m_TextActor->SetTextScaleModeToNone( );
+  vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
+  textprop->SetColor( 1, 1, 0 );
   textprop->SetFontFamilyToCourier( );
   textprop->SetFontSize( 18 );
   textprop->BoldOff( );
@@ -95,401 +46,191 @@ Clear( )
   textprop->ShadowOff( );
   textprop->SetJustificationToLeft( );
   textprop->SetVerticalJustificationToBottom( );
-  vtkCoordinate* coord = this->TextActor->GetPositionCoordinate( );
+  vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
   coord->SetCoordinateSystemToNormalizedViewport( );
-  coord->SetValue( 0.01, 0.01 );
-}
+  coord->SetValue( 0.01, 0.05 );
 
-// -------------------------------------------------------------------------
-vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
-GetStyle( )
-{
-  return( this->Style.GetPointer( ) );
-}
-
-// -------------------------------------------------------------------------
-const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
-GetStyle( ) const
-{
-  return( this->Style.GetPointer( ) );
+  // Configure style
+  vtkSmartPointer< TStyle > st = vtkSmartPointer< TStyle >::New( );
+  st->SetAssociatedObject( this );
+  st->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
+  st->AddMouseClickCommand( Self::_MouseClickCommand, this );
+  st->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
+  st->AddKeyCommand( Self::_KeyCommand, this );
+  st->AddEnterCommand( Self::_EnterCommand, this );
+  st->AddLeaveCommand( Self::_LeaveCommand, this );
+  this->m_Style = st;
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-PushActorsInto( vtkRenderer* renderer )
+SetInputConnection( vtkAlgorithmOutput* aout, int orientation )
 {
-  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( );
+  if( aout == NULL )
+    return;
+  this->m_Mapper->SetInputConnection( aout );
+  this->_ConfigureInput( orientation );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-PopActorsFrom( vtkRenderer* renderer )
-{
-  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( );
-}
-
-// -------------------------------------------------------------------------
-unsigned int cpExtensions::Visualization::ImageSliceActors::
-GetNumberOfImageActors( ) const
+SetInputData( vtkImageData* data, int orientation )
 {
-  return( this->ImageActors.size( ) );
-}
-
-// -------------------------------------------------------------------------
-vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
-GetImageActor( unsigned int id )
-{
-  if( id < this->ImageActors.size( ) )
-    return( this->ImageActors[ id ] );
-  else
-    return( NULL );
+  if( data == NULL )
+    return;
+  this->m_Mapper->SetInputData( data );
+  this->_ConfigureInput( orientation );
 }
 
 // -------------------------------------------------------------------------
-const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
-GetImageActor( unsigned int id ) const
+vtkImageData* cpExtensions::Visualization::ImageSliceActors::
+GetInputData( )
 {
-  if( id < this->ImageActors.size( ) )
-    return( this->ImageActors[ id ] );
+  if( this->m_Mapper.GetPointer( ) != NULL )
+    return( this->m_Mapper->GetInput( ) );
   else
     return( NULL );
 }
 
 // -------------------------------------------------------------------------
-vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
-GetTextActor( )
-{
-  return( this->TextActor );
-}
-
-// -------------------------------------------------------------------------
-const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
-GetTextActor( ) const
-{
-  return( this->TextActor );
-}
-
-// -------------------------------------------------------------------------
-vtkActor* cpExtensions::Visualization::ImageSliceActors::
-GetPlaneActor( )
-{
-  return( this->PlaneActor );
-}
-
-// -------------------------------------------------------------------------
-const vtkActor* cpExtensions::Visualization::ImageSliceActors::
-GetPlaneActor( ) const
-{
-  return( this->PlaneActor );
-}
-
-// -------------------------------------------------------------------------
-vtkPlane* cpExtensions::Visualization::ImageSliceActors::
-GetPlaneFunction( )
+void cpExtensions::Visualization::ImageSliceActors::
+AddMesh( vtkPolyData* mesh )
 {
-  return( this->PlaneFunction );
-}
+  SourceActor< vtkCutter > a;
+  a.Create( );
+  a.Source->SetInputData( mesh );
+  a.Source->SetCutFunction( this->m_Mapper->GetSlicePlane( ) );
+  a.Source->SetValue( 0, 0 );
+  a.Source->GenerateTrianglesOff( );
+  a.Source->Update( );
+  a.Modified( );
+  this->m_Meshes[ mesh ] = a;
+  this->AddItem( a.Actor );
 
-// -------------------------------------------------------------------------
-const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
-GetPlaneFunction( ) const
-{
-  return( this->PlaneFunction );
+  auto ren = this->m_Style->GetCurrentRenderer( );
+  if( ren != NULL )
+    if( ren->HasViewProp( this->m_Actor ) )
+      ren->AddViewProp( a.Actor );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-AddActor( vtkAlgorithm* algorithm, vtkActor* actor )
+AssociateSlice( Self* slice )
 {
-  this->AssociatedActors.push_back( TAssociatedActor( algorithm, actor ) );
-  this->AddItem( actor );
+  this->m_AssociatedSlices.push_back( slice );
+  this->Modified( );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-AddActor( vtkActor* actor )
+CleanAssociatedSlices( )
 {
-  this->AddActor( NULL, actor );
+  this->m_AssociatedSlices.clear( );
+  this->Modified( );
 }
 
 // -------------------------------------------------------------------------
-void cpExtensions::Visualization::ImageSliceActors::
-SetInterpolate( bool v )
+cpExtensions::Interaction::ImageInteractorStyle*
+cpExtensions::Visualization::ImageSliceActors::
+GetStyle( )
 {
-  if( this->Interpolate != v )
-  {
-    for( unsigned int i = 0; i < this->ImageActors.size( ); ++i )
-      this->ImageActors[ i ]->SetInterpolate( v );
-    this->Interpolate = v;
-    this->Modified( );
-
-  } // fi
+  return( dynamic_cast< TStyle* >( this->m_Style.GetPointer( ) ) );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-InterpolateOn( )
+SetStyle( vtkInteractorStyle* st )
 {
-  this->SetInterpolate( true );
+  this->m_Style = st;
+  this->Modified( );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-InterpolateOff( )
-{
-  this->SetInterpolate( false );
-}
-
-// -------------------------------------------------------------------------
-double* cpExtensions::Visualization::ImageSliceActors::
-GetDisplayBounds( ) const
+PushInto( vtkRenderer* ren )
 {
-  if( this->ImageActors.size( ) > 0 )
-    return( this->ImageActors[ 0 ]->GetDisplayBounds( ) );
-  else
-    return( NULL );
+  this->InitTraversal( );
+  vtkProp* prop;
+  while( ( prop = this->GetNextProp( ) ) != NULL )
+    ren->AddViewProp( prop );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-GetDisplayBounds( double bounds[ 6 ] ) const
-{
-  if( this->ImageActors.size( ) == 0 )
-  {
-    bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
-    bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
-  }
-  else
-    this->ImageActors[ 0 ]->GetDisplayBounds( bounds );
-}
-
-// -------------------------------------------------------------------------
-int cpExtensions::Visualization::ImageSliceActors::
-GetAxis( ) const
+PopFrom( vtkRenderer* ren )
 {
-  if( this->SliceMappers.size( ) > 0 )
-    return( this->SliceMappers[ 0 ]->GetOrientation( ) );
-  else
-    return( -1 );
+  this->InitTraversal( );
+  vtkProp* prop;
+  while( ( prop = this->GetNextProp( ) ) != NULL )
+    ren->RemoveViewProp( prop );
 }
 
 // -------------------------------------------------------------------------
-int cpExtensions::Visualization::ImageSliceActors::
+long cpExtensions::Visualization::ImageSliceActors::
 GetSliceNumber( ) const
 {
-  if( this->SliceMappers.size( ) > 0 )
-    return( this->SliceMappers[ 0 ]->GetSliceNumber( ) );
-  else
-    return( -1 );
-}
-
-// -------------------------------------------------------------------------
-int cpExtensions::Visualization::ImageSliceActors::
-GetSliceNumberMinValue( ) const
-{
-  if( this->SliceMappers.size( ) > 0 )
-    return( this->SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
-  else
-    return( -1 );
-}
-
-// -------------------------------------------------------------------------
-int cpExtensions::Visualization::ImageSliceActors::
-GetSliceNumberMaxValue( ) const
-{
-  if( this->SliceMappers.size( ) > 0 )
-    return( this->SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
+  if( this->m_Mapper.GetPointer( ) != NULL )
+    return( this->m_Mapper->GetSliceNumber( ) );
   else
     return( -1 );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-SetSliceNumber( const int& slice )
+SetSliceNumber( long slice )
 {
-  unsigned int nImages = this->SliceMappers.size( );
-  if( nImages == 0 )
+  if( this->m_Mapper.GetPointer( ) == NULL )
     return;
 
-  // Change visualization extent
-  for( unsigned int i = 0; i < nImages; ++i )
-  {
-    this->SliceMappers[ i ]->SetSliceNumber( slice );
-    this->SliceMappers[ i ]->Modified( );
-    this->ImageActors[ i ]->Modified( );
-    this->SliceMappers[ i ]->Update( );
-
-  } // rof
+  // Get orientation
+  int a = this->m_Mapper->GetOrientation( );
 
-  // Compute plane
-  vtkAlgorithm* algo = this->SliceMappers[ 0 ]->GetInputAlgorithm( );
-  vtkInformation* info = algo->GetOutputInformation( 0 );
+  // Check extent
   int ext[ 6 ];
-  double ori[ 3 ], spac[ 3 ], pos[ 3 ];
-  info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
-  info->Get( vtkDataObject::ORIGIN( ), ori );
-  info->Get( vtkDataObject::SPACING( ), spac );
-  this->SliceMappers[ 0 ]->GetSlicePlane( )->GetOrigin( pos );
-
-  // Prevent obscuring voxels by offsetting the plane geometry
-  double xbnds[ ] =
-    {
-      ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
-      ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
-    };
-  double ybnds[ ] =
-    {
-      ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
-      ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
-    };
-  double zbnds[ ] =
-    {
-      ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
-      ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
-    };
+  this->m_Mapper->GetInput( )->GetExtent( ext );
+  long rs = slice;
+  rs = ( rs > ext[ a << 1 ] )? rs: ext[ a << 1 ];
+  rs = ( rs < ext[ ( a << 1 ) + 1 ] )? rs: ext[ ( a << 1 ) + 1 ];
 
-  if( spac[ 0 ] < double( 0 ) )
-  {
-    double t = xbnds[ 0 ];
-    xbnds[ 0 ] = xbnds[ 1 ];
-    xbnds[ 1 ] = t;
-
-  } // fi
-  if( spac[ 1 ] < double( 0 ) )
-  {
-    double t = ybnds[ 0 ];
-    ybnds[ 0 ] = ybnds[ 1 ];
-    ybnds[ 1 ] = t;
-
-  } // fi
-  if( spac[ 2 ] < double( 0 ) )
-  {
-    double t = zbnds[ 0 ];
-    zbnds[ 0 ] = zbnds[ 1 ];
-    zbnds[ 1 ] = t;
-
-  } // fi
-
-  // Plane function origin
-  this->PlaneFunction->SetOrigin( pos );
+  // Update pipeline
+  this->m_Mapper->SetSliceNumber( rs );
 
-  // Configure visualization and implicit plane orientation
-  int axis = this->SliceMappers[ 0 ]->GetOrientation( );
-  this->PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
-  this->PlaneActor->GetProperty( )->SetLineWidth( 2 );
-  vtkPoints* plane_points = this->PlaneSource->GetPoints( );
-  if( axis == 0 ) // YZ, x-normal
-  {
-    this->PlaneFunction->SetNormal( 1, 0, 0 );
-    plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
-    plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
-    plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
-    plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
-    this->PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
-  }
-  else if( axis == 1 ) // ZX, y-normal
-  {
-    this->PlaneFunction->SetNormal( 0, 1, 0 );
-    plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
-    plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
-    plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
-    plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
-    this->PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
-  }
-  else // XY, z-normal
-  {
-    this->PlaneFunction->SetNormal( 0, 0, 1 );
-    plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
-    plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
-    plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
-    plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
-    this->PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
+  // Update display extent (this isn't done automatically)
+  ext[ a << 1 ] = ext[ ( a << 1 ) + 1 ] = rs;
+  this->m_Actor->SetDisplayExtent( ext );
 
-  } // fi
-  this->PlaneFunction->Modified( );
-  this->PlaneSource->Modified( );
-  this->PlaneMapper->Modified( );
-  this->PlaneActor->Modified( );
-
-  // Prepare other actors to update
-  /* TODO
-     for( unsigned int i = 0; i < this->OtherActors.size( ); ++i )
-     {
-     if( this->OtherActors[ i ].first.GetPointer( ) != NULL )
-     {
-     this->OtherActors[ i ].first->Modified( );
-     this->OtherActors[ i ].first->Update( );
-
-     } // fi
-
-     if( this->OtherActors[ i ].second.GetPointer( ) != NULL )
-     {
-     this->OtherActors[ i ].second->GetMapper( )->Modified( );
-     this->OtherActors[ i ].second->Modified( );
-
-     } // fi
-
-     } // rof
-
-     if( this->m_UpdateCommand != NULL )
-     this->m_UpdateCommand( this->m_UpdateData );
-  */
-
-  // Update text
-  this->UpdateText( );
-}
-
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::ImageSliceActors::
-UpdateText( )
-{
-  if( this->SliceMappers.size( ) > 0 )
-  {
-    char axis;
-    int axId = this->SliceMappers[ 0 ]->GetOrientation( );
-    if     ( axId == 0 ) axis = 'X';
-    else if( axId == 1 ) axis = 'Y';
-    else if( axId == 2 ) axis = 'Z';
-
-    std::sprintf(
-      this->TextBuffer, "Axis: %c (%d)",
-      axis, this->SliceMappers[ 0 ]->GetSliceNumber( )
-      );
-  }
-  else
-    this->TextBuffer[ 0 ] = '\0';
-  this->TextActor->SetInput( this->TextBuffer );
-  this->TextActor->Modified( );
+  this->m_Mapper->Modified( );
+  this->m_Actor->Modified( );
+  this->_ConfigureCursor( );
   this->Modified( );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-UpdateText( double pos[ 3 ] )
+ShowPixelText( double* pos )
 {
-  if( this->SliceMappers.size( ) > 0 )
+  if( this->m_Mapper.GetPointer( ) != NULL )
   {
     char axis;
-    int axId = this->SliceMappers[ 0 ]->GetOrientation( );
+    int axId = this->m_Mapper->GetOrientation( );
     if     ( axId == 0 ) axis = 'X';
     else if( axId == 1 ) axis = 'Y';
     else if( axId == 2 ) axis = 'Z';
+    long slice = this->GetSliceNumber( );
 
-    vtkImageData* image = this->SliceMappers[ 0 ]->GetInput( );
-
+    vtkImageData* image = this->m_Actor->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 +239,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
           << " "
@@ -508,61 +247,123 @@ UpdateText( double pos[ 3 ] )
             ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
             );
       str << ")";
+
+#if defined(WIN32)
+      sprintf_s(
+        this->m_TextBuffer, MAX_TEXT_BUFFER, "Axis: %c (%d)\nPixel %s",
+        axis, slice, str.str( ).c_str( )
+        );
+#else // defined(WIN32)
       std::sprintf(
-        this->TextBuffer, "Axis: %c (%d) | Pixel %s",
-        axis,
-        this->SliceMappers[ 0 ]->GetSliceNumber( ),
-        str.str( ).c_str( )
+        this->m_TextBuffer, "Axis: %c (%ld)\nPixel %s",
+        axis, slice, str.str( ).c_str( )
         );
-
+#endif // defined(WIN32)
     } // fi
   }
   else
-    this->TextBuffer[ 0 ] = '\0';
-  this->TextActor->SetInput( this->TextBuffer );
-  this->TextActor->Modified( );
+    this->m_TextBuffer[ 0 ] = '\0';
+  this->m_TextActor->SetInput( this->m_TextBuffer );
+  this->m_TextActor->Modified( );
   this->Modified( );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-UpdateText( const double& w, const double& l )
+GetScalarRange( double r[ 2 ] ) const
+{
+  r[ 0 ] = this->m_ScalarRange[ 0 ];
+  r[ 1 ] = this->m_ScalarRange[ 1 ];
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::ImageSliceActors::
+SetScalarRange( const double& a, const double& b )
 {
-  if( this->SliceMappers.size( ) > 0 )
+  this->m_ManualScalarRange = true;
+  this->m_ScalarRange[ 0 ] = a;
+  this->m_ScalarRange[ 1 ] = b;
+  this->SetWindowLevel( 1, 0.5 );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::ImageSliceActors::
+UnsetScalarRange( )
+{
+  auto image = this->m_Actor->GetInput( );
+  if( image != NULL )
   {
-    char axis;
-    int axId = this->SliceMappers[ 0 ]->GetOrientation( );
-    if     ( axId == 0 ) axis = 'X';
-    else if( axId == 1 ) axis = 'Y';
-    else if( axId == 2 ) axis = 'Z';
+    double r[ 2 ];
+    image->GetScalarRange( r );
+    this->SetScalarRange( r[ 0 ], r[ 1 ] );
 
-    std::sprintf(
-      this->TextBuffer, "Axis: %c (%d) | W/L (%.2f/%.2f)",
-      axis, this->SliceMappers[ 0 ]->GetSliceNumber( ), w, l
-      );
-  }
+  } // fi
+  this->m_ManualScalarRange = false;
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::ImageSliceActors::
+SetWindowLevel( const double& w, const double& l )
+{
+  static const double _0 = double( 0 );
+  static const double _1 = double( 1 );
+
+  double rw = ( w < _0 )? _0: ( ( w > _1 )? _1: w );
+  double rl = ( l < _0 )? _0: ( ( l > _1 )? _1: l );
+
+  double d = this->m_ScalarRange[ 1 ] - this->m_ScalarRange[ 0 ];
+  rw *= d;
+  rl *= d;
+  rl += this->m_ScalarRange[ 0 ];
+
+  if( this->m_Actor.GetPointer( ) != NULL )
+  {
+    this->m_Actor->GetProperty( )->SetColorWindow( rw );
+    this->m_Actor->GetProperty( )->SetColorLevel( rl );
+    this->Modified( );
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+double cpExtensions::Visualization::ImageSliceActors::
+GetWindow( ) const
+{
+  if( this->m_Actor.GetPointer( ) != NULL )
+    return( this->m_Actor->GetProperty( )->GetColorWindow( ) );
   else
-    this->TextBuffer[ 0 ] = '\0';
-  this->TextActor->SetInput( this->TextBuffer );
-  this->TextActor->Modified( );
-  this->Modified( );
+    return( double( 0 ) );
+}
+
+// -------------------------------------------------------------------------
+double cpExtensions::Visualization::ImageSliceActors::
+GetLevel( ) const
+{
+  if( this->m_Actor.GetPointer( ) != NULL )
+    return( this->m_Actor->GetProperty( )->GetColorLevel( ) );
+  else
+    return( double( 0 ) );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::ImageSliceActors::
+Render( )
+{
+  auto ren = this->m_Style->GetCurrentRenderer( );
+  if( ren == NULL )
+    return;
+  auto win = ren->GetRenderWindow( );
+  if( win == NULL )
+    return;
+  win->Render( );
 }
 
 // -------------------------------------------------------------------------
 cpExtensions::Visualization::ImageSliceActors::
 ImageSliceActors( )
   : Superclass( ),
-    Interpolate( false )
+    m_ManualScalarRange( false )
 {
-  // Connect this view with a controller
-  this->Style = vtkSmartPointer< ImageInteractorStyle >::New( );
-  this->Style->AssociateView( this );
-  this->Style->SetMouseMoveCommand( Self::_MouseMoveCommand );
-  this->Style->SetMouseClickCommand( Self::_MouseClickCommand );
-  this->Style->SetMouseDoubleClickCommand( Self::_MouseDoubleClickCommand );
-  this->Style->SetMouseWheelCommand( Self::_MouseWheelCommand );
-  this->Style->SetKeyCommand( Self::_KeyCommand );
-
   this->Clear( );
 }
 
@@ -574,64 +375,142 @@ cpExtensions::Visualization::ImageSliceActors::
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-_ConfigureNewInput( int axis )
-{
-  unsigned int nImages = this->ImageActors.size( );
+_ConfigureInput( int orientation )
+{
+  // Prepare main image
+  this->m_Mapper->SetOrientation( orientation );
+  this->m_Mapper->Update( );
+  this->m_Actor->GetProperty( )->SetOpacity( 0.8 );
+  this->m_Actor->Modified( );
+  auto st = this->GetStyle( );
+  if( st != NULL )
+    st->AssociateImageActor( this->m_Actor );
+  this->AddItem( this->m_Actor );
+
+  // Prepare main cursor
+  this->_ConfigureCursor( );
+  this->AddItem( this->m_Cursor.Actor );
+
+  // Prepare window/level
+  if( !( this->m_ManualScalarRange ) )
+    this->UnsetScalarRange( );
+
+  // Add all other actors
+  this->AddItem( this->m_TextActor );
+
+  // Put it in the first slice
+  auto image = this->m_Mapper->GetInput( );
+  int ext[ 6 ];
+  image->GetExtent( ext );
+  this->SetSliceNumber( ext[ orientation << 1 ] );
 
-  // Configure mapper
-  vtkImageSliceMapper* mapper = this->SliceMappers[ nImages ];
-  if( nImages == 0 )
-    mapper->SetOrientation( axis );
-  else
-    mapper->SetOrientation( this->SliceMappers[ 0 ]->GetOrientation( ) );
-  mapper->Update( );
-
-  // Create actor
-  vtkSmartPointer< vtkImageActor > actor =
-    vtkSmartPointer< vtkImageActor >::New( );
-  this->ImageActors.push_back( actor );
-  actor->SetMapper( mapper );
-  actor->SetInterpolate( this->Interpolate );
-  actor->Modified( );
-
-  if( nImages == 0 )
-  {
-    this->AddItem( this->TextActor );
-    this->AddItem( this->PlaneActor );
-    this->Style->AssociateImageActor( actor );
+  /*
+    if( this->m_Style.GetPointer( ) != NULL )
+    this->AddItem( this->m_Actor );
 
-  } // fi
-  this->AddItem( actor );
+    this->SetSliceNumber( this->GetSliceNumberMinValue( ) );
+    this->ResetCursor( );
+    this->Modified( );
 
-  if( nImages > 1 )
-    this->SetSliceNumber( this->GetSliceNumber( ) );
-  this->Modified( );
+    // Reset cursors
+    this->ResetCursor( );
+    this->ResetAxesCursor( );
+
+    // Update window/level ranges
+    vtkImageData* data = this->GetInputImage( );
+    if( data != NULL )
+    {
+    double r[ 2 ];
+    data->GetScalarRange( r );
+    this->m_WLRange[ 0 ] = double( 0 );
+    this->m_WLRange[ 1 ] = r[ 1 ] - r[ 0 ];
+    this->m_WLRange[ 2 ] = r[ 0 ];
+    this->m_WLRange[ 3 ] = r[ 1 ];
+    this->ResetWindowLevel( );
+
+    // Configure blender
+    this->m_BlenderBase = vtkSmartPointer< vtkImageData >::New( );
+    this->m_BlenderBase->ShallowCopy( data );
+    this->m_BlenderBase->AllocateScalars( VTK_UNSIGNED_CHAR, 1 );
+    std::memset(
+    this->m_BlenderBase->GetScalarPointer( ), 0,
+    this->m_BlenderBase->GetActualMemorySize( )
+    );
+    this->m_Blender->AddInputData( this->m_BlenderBase );
+
+    } // fi
+  */
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::ImageSliceActors::
+_ConfigureCursor( )
+{
+  int a = this->m_Mapper->GetOrientation( );
+  double bounds[ 6 ];
+  this->m_Actor->GetBounds( bounds );
+  bounds[   a << 1       ] += 1e-5;
+  bounds[ ( a << 1 ) + 1 ] += 1e-5;
+
+  this->m_Cursor.Source->SetModelBounds( bounds );
+  this->m_Cursor.Source->AllOn( );
+  this->m_Cursor.Actor->GetProperty( )->SetOpacity( 1 );
+  this->m_Cursor.Actor->GetProperty( )->SetLineWidth( 3 );
+  this->m_Cursor.Modified( );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
 _MouseMoveCommand(
   void* data,
-  const ImageInteractorStyle::ButtonID& btn, double* pos,
+  const TStyle::ButtonID& btn,
+  int* idx, double* pos,
   bool alt, bool ctr, bool sft
   )
 {
-  ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
-  if( actors == NULL )
+  ImageSliceActors* self = reinterpret_cast< ImageSliceActors* >( data );
+  if( self == NULL )
     return;
 
-  if( btn == ImageInteractorStyle::ButtonID_None )
-  {
-    actors->UpdateText( pos );
-  }
-  else if( btn == ImageInteractorStyle::ButtonID_Left )
+  if( btn == TStyle::ButtonID_None )
   {
+    self->ShowPixelText( pos );
+    self->m_Cursor.Source->SetFocalPoint( pos );
+    self->m_Cursor.Source->Modified( );
+    self->m_Cursor.Modified( );
+    self->Render( );
   }
-  else if( btn == ImageInteractorStyle::ButtonID_Middle )
-  {
-  }
-  else if( btn == ImageInteractorStyle::ButtonID_Right )
+  else if( btn == TStyle::ButtonID_Right )
   {
+    if( !alt && !ctr && sft )
+    {
+      int a0 = self->m_Mapper->GetOrientation( );
+      int a1 = ( a0 + 1 ) % 3;
+      int a2 = ( a0 + 2 ) % 3;
+      double dx = pos[ a1 ] - self->m_StartMouseEvent[ a1 ];
+      double dy = pos[ a2 ] - self->m_StartMouseEvent[ a2 ];
+      double bounds[ 6 ];
+      self->m_Actor->GetBounds( bounds );
+      dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
+      dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
+      double w = self->m_StartWindow + dx;
+      double l = self->m_StartLevel + dy;
+
+      self->SetWindowLevel( w, l );
+      self->Render( );
+      for(
+        auto aIt = self->m_AssociatedSlices.begin( );
+        aIt != self->m_AssociatedSlices.end( );
+        ++aIt
+        )
+      {
+        ( *aIt )->SetWindowLevel( w, l );
+        ( *aIt )->Render( );
+
+      } // rof
+
+    } // fi
+
   } // fi
 }
 
@@ -639,20 +518,32 @@ _MouseMoveCommand(
 void cpExtensions::Visualization::ImageSliceActors::
 _MouseClickCommand(
   void* data,
-  const ImageInteractorStyle::ButtonID& btn, double* pos,
+  const TStyle::ButtonID& btn,
+  int* idx, double* pos,
   bool alt, bool ctr, bool sft
   )
 {
-}
+  ImageSliceActors* self = reinterpret_cast< ImageSliceActors* >( data );
+  if( self == NULL )
+    return;
 
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::ImageSliceActors::
-_MouseDoubleClickCommand(
-  void* data,
-  const ImageInteractorStyle::ButtonID& btn, double* pos,
-  bool alt, bool ctr, bool sft
-  )
-{
+  self->m_StartMouseEvent[ 0 ] = pos[ 0 ];
+  self->m_StartMouseEvent[ 1 ] = pos[ 1 ];
+  self->m_StartMouseEvent[ 2 ] = pos[ 2 ];
+
+  if( btn == TStyle::ButtonID_Right )
+  {
+    if( !alt && !ctr && sft )
+    {
+      double r[ 2 ];
+      self->GetScalarRange( r );
+      double d = r[ 1 ] - r[ 0 ];
+      self->m_StartWindow = self->GetWindow( ) / d;
+      self->m_StartLevel = ( self->GetLevel( ) - r[ 0 ] ) / d;
+
+    } // fi
+
+  } // fi
 }
 
 // -------------------------------------------------------------------------
@@ -662,6 +553,55 @@ _MouseWheelCommand(
   const int& dir, bool alt, bool ctr, bool sft
   )
 {
+  ImageSliceActors* self = reinterpret_cast< ImageSliceActors* >( data );
+  if( self == NULL )
+    return;
+  auto ren = self->m_Style->GetCurrentRenderer( );
+  if( ren == NULL )
+    return;
+  auto cam = ren->GetActiveCamera( );
+  if( cam == NULL )
+    return;
+  auto win = ren->GetRenderWindow( );
+  if( win == NULL )
+    return;
+
+  // Get previous values
+  int a = self->m_Mapper->GetOrientation( );
+  double bounds[ 6 ];
+  self->m_Actor->GetBounds( bounds );
+  double prev_pos = bounds[ a << 1 ];
+
+  // Move slice
+  long slice = self->GetSliceNumber( ) + dir * ( ( sft )? 10: 1 );
+  self->SetSliceNumber( slice );
+  self->m_Actor->GetBounds( bounds );
+
+  // Reset camera
+  double cam_pos[ 3 ];
+  cam->GetPosition( cam_pos );
+  cam_pos[ a ] += bounds[ a << 1 ] - prev_pos;
+  cam->SetPosition( cam_pos );
+
+  // Update text
+  self->ShowPixelText( self->m_Cursor.Source->GetFocalPoint( ) );
+
+  // Update all
+  win->Render( );
+  for(
+    auto aIt = self->m_AssociatedSlices.begin( );
+    aIt != self->m_AssociatedSlices.end( );
+    ++aIt
+    )
+  {
+    if( ( *aIt )->m_Mapper->GetOrientation( ) == a )
+    {
+      ( *aIt )->SetSliceNumber( slice );
+      ( *aIt )->Render( );
+
+    } // fi
+
+  } // fi
 }
 
 // -------------------------------------------------------------------------
@@ -673,4 +613,16 @@ _KeyCommand(
 {
 }
 
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::ImageSliceActors::
+_EnterCommand( void* data )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::ImageSliceActors::
+_LeaveCommand( void* data )
+{
+}
+
 // eof - $RCSfile$