From e3fe1f9a8ffcaf9fd59453aadb61547e1c76c584 Mon Sep 17 00:00:00 2001 From: Leonardo Florez-Valencia Date: Tue, 2 Feb 2016 17:41:42 -0500 Subject: [PATCH] ... --- lib/fpa/VTK/Image2DObserver.hxx | 4 +- lib/fpa/VTK/Image3DObserver.h | 67 +++++++- lib/fpa/VTK/Image3DObserver.hxx | 279 +++++++++++++++++++++++++++++-- lib/fpaPlugins/BaseImageFilter.h | 18 +- 4 files changed, 346 insertions(+), 22 deletions(-) diff --git a/lib/fpa/VTK/Image2DObserver.hxx b/lib/fpa/VTK/Image2DObserver.hxx index 49d5577..61ea5a9 100644 --- a/lib/fpa/VTK/Image2DObserver.hxx +++ b/lib/fpa/VTK/Image2DObserver.hxx @@ -63,8 +63,8 @@ Execute( const itk::Object* c, const itk::EventObject& e ) typedef typename F::TFreezeEvent _TFreezeEvent; typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent; - typedef typename F::TEndBacktrackingEvent _TEndBacktrackingEvent; - typedef typename F::TBacktrackingEvent _TBacktrackingEvent; + typedef typename F::TEndBacktrackingEvent _TEndBacktrackingEvent; + typedef typename F::TBacktrackingEvent _TBacktrackingEvent; static unsigned char Colors[][4] = { diff --git a/lib/fpa/VTK/Image3DObserver.h b/lib/fpa/VTK/Image3DObserver.h index 344ede4..6b6af43 100644 --- a/lib/fpa/VTK/Image3DObserver.h +++ b/lib/fpa/VTK/Image3DObserver.h @@ -4,9 +4,9 @@ #include #include -#include #include +#include #include #include #include @@ -27,11 +27,70 @@ namespace fpa typedef itk::SmartPointer< Self > Pointer; typedef itk::SmartPointer< const Self > ConstPointer; + typedef F TFilter; + typedef R TRenderWindow; + typedef typename TFilter::TInputImage TImage; + typedef typename TFilter::TVertex TVertex; + + typedef std::set< TVertex > TVertices; + + public: + itkNewMacro( Self ); + itkTypeMacro( Image3DObserver, itkCommand ); + + itkGetConstMacro( RenderPercentage, double ); + itkSetMacro( RenderPercentage, double ); + + public: + void SetRenderWindow( R* rw ); + void SetPixel( + typename TImage::IndexType idx, + unsigned char red, + unsigned char green, + unsigned char blue, + unsigned char alpha + ); + void Render( ); + void Execute( itk::Object* c, const itk::EventObject& e ) + { this->Execute( ( const itk::Object* )( c ), e ); } + void Execute( const itk::Object* c, const itk::EventObject& e ); + + protected: + Image3DObserver( ); + virtual ~Image3DObserver( ); + + private: + Image3DObserver( const Self& ); // Not impl. + void operator=( const Self& ); // Not impl. + + protected: + vtkSmartPointer< vtkImageData > m_Stencil; + vtkSmartPointer< vtkPolyData > m_PolyData; + vtkSmartPointer< vtkPolyDataMapper > m_PolyDataMapper; + vtkSmartPointer< vtkActor > m_PolyDataActor; + + R* m_RenderWindow; + unsigned long m_Count; + unsigned long m_RenderCount; + double m_RenderPercentage; + }; + + /** + */ + /* + template< class F, class R > + class Image3DObserver + : public itk::Command + { + public: + typedef Image3DObserver Self; + typedef itk::Command Superclass; + typedef itk::SmartPointer< Self > Pointer; + typedef itk::SmartPointer< const Self > ConstPointer; + typedef F TFilter; typedef R TRenderWindow; - typedef itk::Image< long, 3 > TMarks; - public: itkNewMacro( Self ); itkTypeMacro( Image3DObserver, itkCommand ); @@ -60,11 +119,11 @@ namespace fpa unsigned long m_RenderCount; double m_RenderPercentage; - TMarks::Pointer m_Marks; vtkSmartPointer< vtkPolyData > m_Data; vtkSmartPointer< vtkPolyDataMapper > m_Mapper; vtkSmartPointer< vtkActor > m_Actor; }; + */ } // ecapseman diff --git a/lib/fpa/VTK/Image3DObserver.hxx b/lib/fpa/VTK/Image3DObserver.hxx index 3baac71..c89bb4b 100644 --- a/lib/fpa/VTK/Image3DObserver.hxx +++ b/lib/fpa/VTK/Image3DObserver.hxx @@ -1,8 +1,268 @@ #ifndef __FPA__VTK__IMAGE3DOBSERVER__HXX__ #define __FPA__VTK__IMAGE3DOBSERVER__HXX__ -#include +#include +#include +#include +#include +#include +#include +#include + +// ------------------------------------------------------------------------- +template< class F, class R > +void fpa::VTK::Image3DObserver< F, R >:: +SetRenderWindow( R* rw ) +{ + this->m_RenderWindow = rw; +} + +// ------------------------------------------------------------------------- +template< class F, class R > +void fpa::VTK::Image3DObserver< F, R >:: +SetPixel( + typename TImage::IndexType idx, + unsigned char red, + unsigned char green, + unsigned char blue, + unsigned char alpha + ) +{ + std::cout << idx << std::endl; + this->m_Stencil->SetScalarComponentFromDouble + ( idx[ 0 ], idx[ 1 ], idx[ 2 ], 0, red ); + this->m_Stencil->SetScalarComponentFromDouble + ( idx[ 0 ], idx[ 1 ], idx[ 2 ], 1, green ); + this->m_Stencil->SetScalarComponentFromDouble + ( idx[ 0 ], idx[ 1 ], idx[ 2 ], 2, blue ); + this->m_Stencil->SetScalarComponentFromDouble + ( idx[ 0 ], idx[ 1 ], idx[ 2 ], 3, alpha ); + + int ijk[ 3 ]; + ijk[ 0 ] = idx[ 0 ]; + ijk[ 1 ] = idx[ 1 ]; + ijk[ 2 ] = idx[ 2 ]; + long id = this->m_Stencil->ComputePointId( ijk ); + vtkUnsignedCharArray* pd_data = + dynamic_cast< vtkUnsignedCharArray* >( + this->m_PolyData->GetPointData( )->GetScalars( ) + ); + pd_data->SetTuple4( id, red, green, blue, alpha ); + this->m_Stencil->Modified( ); + this->m_PolyData->Modified( ); + this->m_PolyDataMapper->Modified( ); + this->m_PolyDataActor->Modified( ); +} + +// ------------------------------------------------------------------------- +template< class F, class R > +void fpa::VTK::Image3DObserver< F, R >:: +Render( ) +{ + if( this->m_RenderWindow != NULL ) + this->m_RenderWindow->Render( ); +} + +// ------------------------------------------------------------------------- +template< class F, class R > +void fpa::VTK::Image3DObserver< F, R >:: +Execute( const itk::Object* c, const itk::EventObject& e ) +{ + typedef typename F::TStartEvent _TStartEvent; + typedef typename F::TStartLoopEvent _TStartLoopEvent; + typedef typename F::TEndEvent _TEndEvent; + typedef typename F::TEndLoopEvent _TEndLoopEvent; + typedef typename F::TAliveEvent _TAliveEvent; + typedef typename F::TFrontEvent _TFrontEvent; + typedef typename F::TFreezeEvent _TFreezeEvent; + + typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent; + typedef typename F::TEndBacktrackingEvent _TEndBacktrackingEvent; + typedef typename F::TBacktrackingEvent _TBacktrackingEvent; + + static unsigned char Colors[][4] = + { + { 0, 0, 127, 127 }, + { 0, 127, 127, 127 }, + { 127, 0, 127, 127 }, + { 127, 127, 0, 127 }, + { 0, 0, 63, 127 }, + { 0, 63, 63, 127 }, + { 63, 0, 63, 127 }, + { 63, 63, 0, 127 }, + { 63, 63, 127, 127 }, + { 63, 127, 127, 127 }, + { 127, 63, 127, 127 }, + { 127, 127, 63, 127 }, + { 127, 127, 63, 127 }, + { 127, 63, 63, 127 }, + { 63, 127, 63, 127 }, + { 63, 63, 127, 127 } + }; + + const F* filter = dynamic_cast< const F* >( c ); + if( this->m_RenderWindow == NULL || filter == NULL ) + return; + + const _TStartEvent* startEvt = dynamic_cast< const _TStartEvent* >( &e ); + const _TStartBacktrackingEvent* startBackEvt = + dynamic_cast< const _TStartBacktrackingEvent* >( &e ); + if( startEvt != NULL || startBackEvt != NULL ) + { + const typename F::TInputImage* img = filter->GetInput( ); + unsigned int minD = F::TInputImage::ImageDimension; + minD = ( minD < 3 )? minD: 3; + + int e[ 6 ] = { 0 }; + typename F::TInputImage::RegionType reg = img->GetRequestedRegion( ); + for( unsigned int i = 0; i < minD; i++ ) + { + e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ]; + e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1; + + } // rof + + typename F::TInputImage::SpacingType spac = img->GetSpacing( ); + double s[ 3 ] = { 1, 1, 1 }; + for( unsigned int i = 0; i < minD; i++ ) + s[ i ] = double( spac[ i ] ); + + typename F::TInputImage::PointType orig = img->GetOrigin( ); + double o[ 3 ] = { 0 }; + for( unsigned int i = 0; i < minD; i++ ) + o[ i ] = double( orig[ i ] ); + + this->m_Stencil->SetExtent( e ); + this->m_Stencil->SetSpacing( s ); + this->m_Stencil->SetOrigin( o ); + this->m_Stencil->AllocateScalars( VTK_UNSIGNED_CHAR, 4 ); + for( unsigned int i = 0; i < 3; i++ ) + this->m_Stencil->GetPointData( )-> + GetScalars( )->FillComponent( i, 255 ); + this->m_Stencil->GetPointData( )->GetScalars( )->FillComponent( 3, 0 ); + + vtkSmartPointer< vtkPoints > pd_points = + vtkSmartPointer< vtkPoints >::New( ); + vtkSmartPointer< vtkCellArray > pd_verts = + vtkSmartPointer< vtkCellArray >::New( ); + vtkSmartPointer< vtkUnsignedCharArray > pd_data = + vtkSmartPointer< vtkUnsignedCharArray >::New( ); + long nPoints = this->m_Stencil->GetNumberOfPoints( ); + + pd_data->SetNumberOfComponents( 4 ); + pd_data->SetNumberOfTuples( nPoints ); + pd_points->SetNumberOfPoints( nPoints ); + double point[ 3 ]; + for( long pId = 0; pId < nPoints; ++pId ) + { + this->m_Stencil->GetPoint( pId, point ); + pd_points->InsertNextPoint( point ); + pd_verts->InsertNextCell( 1 ); + pd_verts->InsertCellPoint( pId ); + pd_data->SetTuple4( pId, 0, 0, 0, 0 ); + + } // rof + this->m_PolyData->SetPoints( pd_points ); + this->m_PolyData->SetVerts( pd_verts ); + this->m_PolyData->GetPointData( )->SetScalars( pd_data ); + + this->m_PolyDataMapper->SetInputData( this->m_PolyData ); + this->m_PolyDataActor->SetMapper( this->m_PolyDataMapper ); + this->m_Count = 0; + this->m_RenderCount = reg.GetNumberOfPixels( ); + + vtkRenderer* ren = + this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( ); + ren->AddActor( this->m_PolyDataActor ); + return; + + } // fi + + const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e ); + const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e ); + if( aliveEvt != NULL || frontEvt != NULL ) + { + if( aliveEvt != NULL ) + this->SetPixel( + aliveEvt->Vertex, + Colors[ aliveEvt->FrontId ][ 0 ], + Colors[ aliveEvt->FrontId ][ 1 ], + Colors[ aliveEvt->FrontId ][ 2 ], + Colors[ aliveEvt->FrontId ][ 3 ] + ); + else if( frontEvt != NULL ) + this->SetPixel( frontEvt->Vertex, 255, 0, 0, 255 ); + this->m_Count++; + + // Render visual debug + double per = double( this->m_RenderCount ) * this->m_RenderPercentage; + if( double( this->m_Count ) >= per ) + this->Render( ); + if( double( this->m_Count ) >= per ) + this->m_Count = 0; + + return; + + } // fi + + const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e ); + if( endEvt != NULL ) + { + vtkRenderer* ren = + this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( ); + ren->RemoveActor( this->m_PolyDataActor ); + this->Render( ); + return; + + } // fi + + const _TBacktrackingEvent* backEvt = + dynamic_cast< const _TBacktrackingEvent* >( &e ); + const _TEndBacktrackingEvent* endBackEvt = + dynamic_cast< const _TEndBacktrackingEvent* >( &e ); + if( backEvt != NULL ) + { + this->SetPixel( backEvt->Vertex, 0, 0, 255, 255 ); + return; + + } // fi + + if( endBackEvt != NULL ) + { + this->m_RenderWindow->Render( ); + /* TODO: DEBUG + std::cout << "Press enter: " << std::ends; + int aux; + std::cin >> aux; + */ + return; + + } // fi +} + +// ------------------------------------------------------------------------- +template< class F, class R > +fpa::VTK::Image3DObserver< F, R >:: +Image3DObserver( ) + : Superclass( ), + m_RenderWindow( NULL ), + m_RenderPercentage( double( 0.001 ) ) +{ + this->m_Stencil = vtkSmartPointer< vtkImageData >::New( ); + this->m_PolyData = vtkSmartPointer< vtkPolyData >::New( ); + this->m_PolyDataMapper = vtkSmartPointer< vtkPolyDataMapper >::New( ); + this->m_PolyDataActor =vtkSmartPointer< vtkActor >::New( ); +} + +// ------------------------------------------------------------------------- +template< class F, class R > +fpa::VTK::Image3DObserver< F, R >:: +~Image3DObserver( ) +{ +} + +/* #include #include #include @@ -10,11 +270,6 @@ #include #include -/* - #include - #include -*/ - // ------------------------------------------------------------------------- template< class F, class R > void fpa::VTK::Image3DObserver< F, R >:: @@ -28,7 +283,6 @@ template< class F, class R > void fpa::VTK::Image3DObserver< F, R >:: Execute( const itk::Object* c, const itk::EventObject& e ) { - typedef itk::ImageBase< 3 > _TImage; typedef typename F::TStartEvent _TStartEvent; typedef typename F::TStartLoopEvent _TStartLoopEvent; typedef typename F::TEndEvent _TEndEvent; @@ -38,8 +292,8 @@ Execute( const itk::Object* c, const itk::EventObject& e ) typedef typename F::TFreezeEvent _TFreezeEvent; typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent; - typedef typename F::TEndBacktrackingEvent _TEndBacktrackingEvent; - typedef typename F::TBacktrackingEvent _TBacktrackingEvent; + typedef typename F::TEndBacktrackingEvent _TEndBacktrackingEvent; + typedef typename F::TBacktrackingEvent _TBacktrackingEvent; // Check inputs if( this->m_RenderWindow == NULL ) @@ -51,8 +305,8 @@ Execute( const itk::Object* c, const itk::EventObject& e ) const F* filter = dynamic_cast< const F* >( c ); if( filter == NULL ) return; - const _TImage* image = filter->GetInput( ); - if( image == NULL ) + const _TImage* image = filter->GetInput( ); + if( image == NULL ) return; const _TStartEvent* startEvt = dynamic_cast< const _TStartEvent* >( &e ); @@ -182,11 +436,9 @@ Execute( const itk::Object* c, const itk::EventObject& e ) if( endBackEvt != NULL ) { this->m_RenderWindow->Render( ); - /* TODO: DEBUG std::cout << "Press enter: " << std::ends; int aux; std::cin >> aux; - */ return; } // fi @@ -201,6 +453,7 @@ Image3DObserver( ) m_RenderPercentage( double( 0.0001 ) ) { } +*/ #endif // __FPA__VTK__IMAGE3DOBSERVER__HXX__ diff --git a/lib/fpaPlugins/BaseImageFilter.h b/lib/fpaPlugins/BaseImageFilter.h index e9b9f70..05fdee6 100644 --- a/lib/fpaPlugins/BaseImageFilter.h +++ b/lib/fpaPlugins/BaseImageFilter.h @@ -48,7 +48,7 @@ namespace fpaPlugins // ------------------------------------------------------------------------- #include -//#include +#include #include #include @@ -59,7 +59,7 @@ _ConfigureDebugger( F* filter ) { typedef typename F::TInputImage _I; typedef fpa::VTK::Image2DObserver< F, vtkRenderWindow > _2D; - // typedef fpa::VTK::Image3DObserver< F, vtkRenderWindow > _3D; + typedef fpa::VTK::Image3DObserver< F, vtkRenderWindow > _3D; this->m_Observers.clear( ); if( this->m_Parameters->GetBool( "VisualDebug" ) ) @@ -83,7 +83,19 @@ _ConfigureDebugger( F* filter ) } else if( _I::ImageDimension == 3 ) { - // this->m_MPRViewer->GetInteractor( 3 ); + auto iren = this->m_MPRViewer->GetInteractor( 2 ); + if( iren != NULL ) + { + std::cout << "OK 3D" << std::endl; + typename _3D::Pointer debugger = _3D::New( ); + debugger->SetRenderWindow( iren->GetRenderWindow( ) ); + debugger->SetRenderPercentage( 0.001 ); + this->m_Observers.insert( + filter->AddObserver( itk::AnyEvent( ), debugger ) + ); + filter->ThrowEventsOn( ); + + } // fi } // fi -- 2.47.1