From 94151bbd3c88422c42b82776e2ea07eebea73907 Mon Sep 17 00:00:00 2001 From: Leonardo Florez-Valencia Date: Thu, 8 Oct 2015 10:51:28 -0500 Subject: [PATCH] half way work... --- appli/examples/example_View2DImage.cxx | 10 +- .../Visualization/ImageInteractorStyle.cxx | 13 ++ .../Visualization/ImageSliceActors.cxx | 187 ++++++++++++++++-- .../Visualization/ImageSliceActors.h | 12 +- 4 files changed, 196 insertions(+), 26 deletions(-) diff --git a/appli/examples/example_View2DImage.cxx b/appli/examples/example_View2DImage.cxx index 66eaf29..46775e2 100644 --- a/appli/examples/example_View2DImage.cxx +++ b/appli/examples/example_View2DImage.cxx @@ -83,12 +83,6 @@ int main( int argc, char* argv[] ) window->AddRenderer( renderer ); window->SetSize( 600, 600 ); - // Set up interaction style - /* - vtkSmartPointer< example_BaseInteractorStyle > style = - vtkSmartPointer< example_BaseInteractorStyle >::New( ); - */ - // Set up the interaction vtkSmartPointer< vtkRenderWindowInteractor > interactor = vtkSmartPointer< vtkRenderWindowInteractor >::New( ); @@ -99,10 +93,10 @@ int main( int argc, char* argv[] ) vtkSmartPointer< TSliceActors > image_actors = vtkSmartPointer< TSliceActors >::New( ); image_actors->AddInputData( image->GetVTK< vtkImageData >( ) ); - image_actors->PushActorsInto( renderer ); - interactor->SetInteractorStyle( image_actors->GetStyle( ) ); + image_actors->PushActorsInto( window ); // Begin interaction + renderer->ResetCamera( ); window->Render( ); interactor->Start( ); diff --git a/lib/cpExtensions/Visualization/ImageInteractorStyle.cxx b/lib/cpExtensions/Visualization/ImageInteractorStyle.cxx index aa688bf..890eabb 100644 --- a/lib/cpExtensions/Visualization/ImageInteractorStyle.cxx +++ b/lib/cpExtensions/Visualization/ImageInteractorStyle.cxx @@ -64,6 +64,7 @@ OnMouseWheelForward( ) // Invoke possible events this->MouseWheelCommand( this->Data, 1, alt, ctr, sft ); + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -81,6 +82,7 @@ OnMouseWheelBackward( ) // Invoke possible events this->MouseWheelCommand( this->Data, -1, alt, ctr, sft ); + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -102,6 +104,7 @@ OnLeftClick( ) // Invoke possible events this->MouseClickCommand( this->Data, Self::ButtonID_Left, pos, alt, ctr, sft ); + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -123,6 +126,7 @@ OnLeftDoubleClick( ) // Invoke possible events this->MouseDoubleClickCommand( this->Data, Self::ButtonID_Left, pos, alt, ctr, sft ); + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -144,6 +148,7 @@ OnMiddleClick( ) // Invoke possible events this->MouseClickCommand( this->Data, Self::ButtonID_Middle, pos, alt, ctr, sft ); + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -165,6 +170,7 @@ OnMiddleDoubleClick( ) // Invoke possible events this->MouseDoubleClickCommand( this->Data, Self::ButtonID_Middle, pos, alt, ctr, sft ); + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -186,6 +192,7 @@ OnRightClick( ) // Invoke possible events this->MouseClickCommand( this->Data, Self::ButtonID_Right, pos, alt, ctr, sft ); + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -207,6 +214,7 @@ OnRightDoubleClick( ) // Invoke possible events this->MouseDoubleClickCommand( this->Data, Self::ButtonID_Right, pos, alt, ctr, sft ); + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -217,6 +225,7 @@ OnChar( ) if( rwi == NULL || this->KeyCommand == NULL ) return; this->KeyCommand( this->Data, rwi->GetKeyCode( ) ); + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -226,6 +235,7 @@ OnExpose( ) vtkRenderWindowInteractor* rwi = this->GetInteractor( ); if( rwi == NULL ) return; + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -235,6 +245,7 @@ OnConfigure( ) vtkRenderWindowInteractor* rwi = this->GetInteractor( ); if( rwi == NULL ) return; + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -244,6 +255,7 @@ OnEnter( ) vtkRenderWindowInteractor* rwi = this->GetInteractor( ); if( rwi == NULL ) return; + rwi->Render( ); } // ------------------------------------------------------------------------- @@ -253,6 +265,7 @@ OnLeave( ) vtkRenderWindowInteractor* rwi = this->GetInteractor( ); if( rwi == NULL ) return; + rwi->Render( ); } // ------------------------------------------------------------------------- diff --git a/lib/cpExtensions/Visualization/ImageSliceActors.cxx b/lib/cpExtensions/Visualization/ImageSliceActors.cxx index 5722c58..c9b8153 100644 --- a/lib/cpExtensions/Visualization/ImageSliceActors.cxx +++ b/lib/cpExtensions/Visualization/ImageSliceActors.cxx @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -10,6 +11,9 @@ #include #include #include +#include +#include +#include #include #include @@ -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 } // ------------------------------------------------------------------------- diff --git a/lib/cpExtensions/Visualization/ImageSliceActors.h b/lib/cpExtensions/Visualization/ImageSliceActors.h index f940a90..6322d18 100644 --- a/lib/cpExtensions/Visualization/ImageSliceActors.h +++ b/lib/cpExtensions/Visualization/ImageSliceActors.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -13,7 +14,6 @@ #include #include #include -#include #include #include @@ -49,8 +49,8 @@ namespace cpExtensions vtkInteractorStyle* GetStyle( ); const vtkInteractorStyle* GetStyle( ) const; - void PushActorsInto( vtkRenderer* renderer ); - void PopActorsFrom( vtkRenderer* renderer ); + void PushActorsInto( vtkRenderWindow* window ); + void PopActorsFrom( vtkRenderWindow* window ); unsigned int GetNumberOfImageActors( ) const; vtkImageActor* GetImageActor( unsigned int id ); const vtkImageActor* GetImageActor( unsigned int id ) const; @@ -71,6 +71,9 @@ namespace cpExtensions double* GetDisplayBounds( ) const; void GetDisplayBounds( double bounds[ 6 ] ) const; + void ResetCursor( ); + void SetCursor( double pos[ 3 ] ); + int GetAxis( ) const; int GetSliceNumber( ) const; int GetSliceNumberMinValue( ) const; @@ -130,6 +133,9 @@ namespace cpExtensions TAssociatedActors AssociatedActors; // Unique objects + vtkSmartPointer< vtkPolyData > Cursor; + vtkSmartPointer< vtkPolyDataMapper > CursorMapper; + vtkSmartPointer< vtkActor > CursorActor; vtkSmartPointer< vtkPlane > PlaneFunction; vtkSmartPointer< vtkPolyData > PlaneSource; vtkSmartPointer< vtkPolyDataMapper > PlaneMapper; -- 2.45.1