From 818377732bc3fd1b02ca56bc78c225345a5fac27 Mon Sep 17 00:00:00 2001 From: Leonardo Florez-Valencia Date: Tue, 3 Mar 2015 17:13:16 -0500 Subject: [PATCH] Visual debugging improved --- .../example_ImageAlgorithmDijkstra_03.cxx | 109 ++++---- lib/fpa/VTK/Image3DObserver.h | 44 +-- lib/fpa/VTK/Image3DObserver.hxx | 258 ++++++++++-------- 3 files changed, 205 insertions(+), 206 deletions(-) diff --git a/appli/examples/example_ImageAlgorithmDijkstra_03.cxx b/appli/examples/example_ImageAlgorithmDijkstra_03.cxx index f2d140b..3b354ba 100644 --- a/appli/examples/example_ImageAlgorithmDijkstra_03.cxx +++ b/appli/examples/example_ImageAlgorithmDijkstra_03.cxx @@ -133,11 +133,11 @@ protected: marks->Allocate( ); marks->FillBuffer( _TMark( 0 ) ); - // Iterate over the candidates, starting fromt the candidates that + // Iterate over the candidates, starting from the candidates that // are near thin branches _TCandidates::const_reverse_iterator cIt = this->m_Candidates.rbegin( ); - for( int leo = 0; leo < 100 && cIt != this->m_Candidates.rend( ); ++cIt ) + for( int leo = 0; leo < 1 && cIt != this->m_Candidates.rend( ); ++cIt ) { // If pixel hasn't been visited, continue TVertex v = cIt->second; @@ -154,83 +154,78 @@ protected: unsigned int nN = 1; for( unsigned int d = 0; d < TImage::ImageDimension; ++d ) nN *= iIt.GetSize( )[ d ]; - TImage::PixelType min_value = iIt.GetPixel( 0 ); - TVertex min_vertex = iIt.GetIndex( 0 ); - if( !( min_value > TImage::PixelType( 0 ) ) ) - min_value = std::numeric_limits< TImage::PixelType >::max( ); + TVertex max_vertex = iIt.GetIndex( 0 ); + TImage::PixelType max_value = iIt.GetPixel( 0 ); for( unsigned int i = 1; i < nN; ++i ) { TImage::PixelType value = iIt.GetPixel( i ); - if( !( value > TImage::PixelType( 0 ) ) ) - value = std::numeric_limits< TImage::PixelType >::max( ); - if( value < min_value ) + if( value > max_value ) { - min_value = value; - min_vertex = iIt.GetIndex( i ); + max_value = value; + max_vertex = iIt.GetIndex( i ); } // fi } // rof - if( min_value < std::numeric_limits< TImage::PixelType >::max( ) ) + if( marks->GetPixel( max_vertex ) != _TMark( 0 ) ) + continue; + leo++; + std::cout << "Leaf: " << leo << " " << max_value << " " << max_vertex << std::endl; + + bool start = true; + do { - if( marks->GetPixel( min_vertex ) != _TMark( 0 ) ) - continue; - leo++; - std::cout << "Leaf: " << leo << " " << min_value << " " << min_vertex << std::endl; + double dist = double( 1.5 ) * std::sqrt( double( input->GetPixel( max_vertex ) ) ); + for( unsigned int d = 0; d < TImage::ImageDimension; ++d ) + radius[ d ] = + ( unsigned long )( dist / spacing[ d ] ) + 1; + itk::NeighborhoodIterator< _TMarkImage > mIt( + radius, marks, marks->GetRequestedRegion( ) + ); + mIt.SetLocation( max_vertex ); + nN = 1; + for( unsigned int d = 0; d < TImage::ImageDimension; ++d ) + nN *= mIt.GetSize( )[ d ]; + for( unsigned int i = 0; i < nN; ++i ) + if( marks->GetRequestedRegion( ).IsInside( mIt.GetIndex( i ) ) ) + { + mIt.SetPixel( i, ( start )? 255: 100 ); + start = false; + } - bool start = true; - do - { - double dist = double( 1.5 ) * std::sqrt( double( input->GetPixel( min_vertex ) ) ); - for( unsigned int d = 0; d < TImage::ImageDimension; ++d ) - radius[ d ] = - ( unsigned long )( dist / spacing[ d ] ) + 1; - itk::NeighborhoodIterator< _TMarkImage > mIt( - radius, marks, marks->GetRequestedRegion( ) - ); - mIt.SetLocation( min_vertex ); - nN = 1; + /* + TImage::SizeType radius; + mIt.GoToBegin( ); + mIt.SetLocation( vIt ); + + TImage::SizeType size = mIt.GetSize( ); + unsigned int nN = 1; for( unsigned int d = 0; d < TImage::ImageDimension; ++d ) - nN *= mIt.GetSize( )[ d ]; + nN *= size[ d ]; for( unsigned int i = 0; i < nN; ++i ) - if( marks->GetRequestedRegion( ).IsInside( mIt.GetIndex( i ) ) ) - { - mIt.SetPixel( i, ( start )? 255: 100 ); - start = false; - } - - /* - TImage::SizeType radius; - mIt.GoToBegin( ); - mIt.SetLocation( vIt ); - - TImage::SizeType size = mIt.GetSize( ); - unsigned int nN = 1; - for( unsigned int d = 0; d < TImage::ImageDimension; ++d ) - nN *= size[ d ]; - for( unsigned int i = 0; i < nN; ++i ) - if( marks->GetRequestedRegion( ).IsInside( mIt.GetIndex( i ) ) ) - mIt.SetPixel( i, ( start )? 255: 100 ); + if( marks->GetRequestedRegion( ).IsInside( mIt.GetIndex( i ) ) ) + mIt.SetPixel( i, ( start )? 255: 100 ); - start = false; - */ - // Next vertex in current path - min_vertex = this->_Parent( min_vertex ); + start = false; + */ + // Next vertex in current path + max_vertex = this->_Parent( max_vertex ); - } while( min_vertex != this->_Parent( min_vertex ) ); - } - else - marks->SetPixel( v, _TMark( 1 ) ); + } while( max_vertex != this->_Parent( max_vertex ) ); } // rof + /* + else + marks->SetPixel( v, _TMark( 1 ) ); + } // rof + */ itk::ImageFileWriter< _TMarkImage >::Pointer w = itk::ImageFileWriter< _TMarkImage >::New( ); w->SetInput( marks ); w->SetFileName( "marks.mhd" ); w->Update( ); - std::exit( 1 ); /* @@ -421,7 +416,7 @@ int main( int argc, char* argv[] ) { // Configure observer TDijkstraObs::Pointer obs = TDijkstraObs::New( ); - obs->SetImage( input_image, view.GetWindow( ) ); + obs->SetRenderWindow( view.GetWindow( ) ); paths->AddObserver( itk::AnyEvent( ), obs ); paths->ThrowEventsOn( ); } diff --git a/lib/fpa/VTK/Image3DObserver.h b/lib/fpa/VTK/Image3DObserver.h index 860946d..4e7a430 100644 --- a/lib/fpa/VTK/Image3DObserver.h +++ b/lib/fpa/VTK/Image3DObserver.h @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -26,35 +27,17 @@ 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::pair< unsigned long, unsigned long > TVertexIds; - - struct TVertexCmp - { - bool operator()( const TVertex& a, const TVertex& b ) const - { - unsigned int d = 0; - while( d < TVertex::Dimension && a[ d ] == b[ d ] ) - d++; - if( d < TVertex::Dimension ) - return( a[ d ] < b[ d ] ); - else - return( false ); - } - }; - typedef std::map< TVertex, TVertexIds, TVertexCmp > TVertices; + typedef F TFilter; + typedef R TRenderWindow; + + typedef itk::Image< long, 3 > TMarks; public: itkNewMacro( Self ); itkTypeMacro( Image3DObserver, itkCommand ); public: - void SetImage( const TImage* img, R* rw ); - void Render( ); + void SetRenderWindow( R* rw ); 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 ); @@ -69,17 +52,14 @@ namespace fpa void operator=( const Self& ); // Not impl. protected: - typename TImage::ConstPointer m_Image; - R* m_RenderWindow; - unsigned long m_Number; - unsigned long m_Percentage; - - vtkSmartPointer< vtkPolyData > m_PolyData; - vtkSmartPointer< vtkPolyDataMapper > m_PolyDataMapper; - vtkSmartPointer< vtkActor > m_PolyDataActor; + unsigned long m_Count; + unsigned long m_RenderCount; - TVertices m_Vertices; + 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 e79bbbe..ff4e645 100644 --- a/lib/fpa/VTK/Image3DObserver.hxx +++ b/lib/fpa/VTK/Image3DObserver.hxx @@ -1,85 +1,40 @@ #ifndef __FPA__VTK__IMAGE3DOBSERVER__HXX__ #define __FPA__VTK__IMAGE3DOBSERVER__HXX__ +#include + #include +#include +#include #include -#include #include #include + /* #include - #include - #include - #include + #include */ // ------------------------------------------------------------------------- template< class F, class R > void fpa::VTK::Image3DObserver< F, R >:: -SetImage( const TImage* img, R* rw ) +SetRenderWindow( R* rw ) { - this->m_Image = img; this->m_RenderWindow = rw; - this->m_Vertices.clear( ); - /* - unsigned int minD = TImage::ImageDimension; - minD = ( minD < 3 )? minD: 3; - - int e[ 6 ] = { 0 }; - typename TImage::RegionType reg = this->m_Image->GetRequestedRegion( ); - for( unsigned int i = 0; i < minD; i++ ) + if( this->m_RenderWindow != NULL ) { - e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ]; - e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1; - - } // rof - - typename TImage::SpacingType spac = this->m_Image->GetSpacing( ); - double s[ 3 ] = { 1, 1, 1 }; - for( unsigned int i = 0; i < minD; i++ ) - s[ i ] = double( spac[ i ] ); - - typename TImage::PointType orig = this->m_Image->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 ); - - this->m_StencilActor->SetInputData( this->m_Stencil ); - this->m_StencilActor->InterpolateOff( ); - */ - - double nPix = - double( this->m_Image->GetRequestedRegion( ).GetNumberOfPixels( ) ); - this->m_Percentage = ( unsigned long )( nPix * 0.001 ); - this->m_Number = 0; - - if( this->m_RenderWindow != NULL ) - { vtkRenderer* ren = - this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( ); + this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( ); + if( ren != NULL ) + { ren->AddActor( this->m_PolyDataActor ); - this->Render( ); + this->m_RenderWindow->Render( ); - } // fi -} + } // fi -// ------------------------------------------------------------------------- -template< class F, class R > -void fpa::VTK::Image3DObserver< F, R >:: -Render( ) -{ - if( this->m_RenderWindow != NULL ) - this->m_RenderWindow->Render( ); + } // fi + */ } // ------------------------------------------------------------------------- @@ -87,42 +42,109 @@ template< class F, class R > void fpa::VTK::Image3DObserver< F, R >:: Execute( const itk::Object* c, const itk::EventObject& e ) { - typedef typename TImage::IndexType TIndex; - typedef typename TImage::PointType TPoint; - typedef typename TFilter::TEvent TEvent; - typedef typename TFilter::TFrontEvent TFrontEvent; - typedef typename TFilter::TMarkEvent TMarkEvent; - typedef typename TFilter::TCollisionEvent TCollisionEvent; - typedef typename TFilter::TEndEvent TEndEvent; - + typedef itk::ImageBase< 3 > _TImage; + typedef typename F::TEvent _TEvent; + typedef typename F::TFrontEvent _TFrontEvent; + typedef typename F::TMarkEvent _TMarkEvent; + typedef typename F::TCollisionEvent _TCollisionEvent; + typedef typename F::TEndEvent _TEndEvent; + + // Check inputs if( this->m_RenderWindow == NULL ) return; + vtkRenderer* ren = + this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( ); + if( ren == NULL ) + return; + const F* filter = dynamic_cast< const F* >( c ); + if( filter == NULL ) + return; + const _TImage* image = filter->GetInput( ); + if( image == NULL ) + return; - const TEvent* evt = dynamic_cast< const TEvent* >( &e ); - TFrontEvent fevt; - TMarkEvent mevt; - TCollisionEvent cevt; - TEndEvent eevt; - if( evt != NULL ) + // Create actor + _TImage::RegionType reg = image->GetLargestPossibleRegion( ); + _TImage::SizeType siz = reg.GetSize( ); + if( this->m_Data.GetPointer( ) == NULL ) { - typename TImage::PointType pnt; - this->m_Image->TransformIndexToPhysicalPoint( evt->Node.Vertex, pnt ); + this->m_Data = vtkSmartPointer< vtkPolyData >::New( ); + this->m_Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( ); + this->m_Actor = vtkSmartPointer< vtkActor >::New( ); + + vtkSmartPointer< vtkPoints > points = + vtkSmartPointer< vtkPoints >::New( ); + vtkSmartPointer< vtkCellArray > vertices = + vtkSmartPointer< vtkCellArray >::New( ); + vtkSmartPointer< vtkFloatArray > scalars = + vtkSmartPointer< vtkFloatArray >::New( ); + this->m_Data->SetPoints( points ); + this->m_Data->SetVerts( vertices ); + this->m_Data->GetPointData( )->SetScalars( scalars ); + + this->m_Mapper->SetInputData( this->m_Data ); + this->m_Actor->SetMapper( this->m_Mapper ); + ren->AddActor( this->m_Actor ); + + this->m_Marks = TMarks::New( ); + this->m_Marks->SetLargestPossibleRegion( + image->GetLargestPossibleRegion( ) + ); + this->m_Marks->SetRequestedRegion( image->GetRequestedRegion( ) ); + this->m_Marks->SetBufferedRegion( image->GetBufferedRegion( ) ); + this->m_Marks->SetOrigin( image->GetOrigin( ) ); + this->m_Marks->SetSpacing( image->GetSpacing( ) ); + this->m_Marks->SetDirection( image->GetDirection( ) ); + this->m_Marks->Allocate( ); + this->m_Marks->FillBuffer( -1 ); + + } // fi + + // Get possible events + const _TEvent* evt = dynamic_cast< const _TEvent* >( &e ); + _TFrontEvent fevt; + _TMarkEvent mevt; + _TCollisionEvent cevt; + _TEndEvent eevt; + if( evt != NULL ) + { if( fevt.CheckEvent( evt ) ) { - if( - this->m_Vertices.find( evt->Node.Vertex ) == this->m_Vertices.end( ) - ) + if( this->m_Marks->GetPixel( evt->Node.Vertex ) == -1 ) { - unsigned long pId = this->m_PolyData->GetPoints( )->InsertNextPoint( - double( pnt[ 0 ] ), double( pnt[ 1 ] ), double( pnt[ 2 ] ) - ); - unsigned long cId = this->m_PolyData->GetVerts( )->InsertNextCell( 1 ); - this->m_PolyData->GetVerts( )->InsertCellPoint( pId ); - this->m_PolyData->Modified( ); - this->m_Vertices[ evt->Node.Vertex ] = TVertexIds( pId, cId ); - - } // rof + typename _TImage::PointType pnt; + image->TransformIndexToPhysicalPoint( evt->Node.Vertex, pnt ); + + long pId = this->m_Data->GetNumberOfPoints( ); + this->m_Data->GetVerts( )->InsertNextCell( 1 ); + this->m_Data->GetVerts( )->InsertCellPoint( pId ); + this->m_Data->GetPoints( )-> + InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] ); + this->m_Data->GetPointData( )-> + GetScalars( )->InsertNextTuple1( 0.5 ); + this->m_Data->Modified( ); + + this->m_Marks->SetPixel( evt->Node.Vertex, pId ); + + } // fi + this->m_RenderWindow->Render( ); + + /* TODO + if( + this->m_Vertices.find( evt->Node.Vertex ) == this->m_Vertices.end( ) + ) + { + unsigned long pId = this->m_PolyData->GetPoints( )->InsertNextPoint( + double( pnt[ 0 ] ), double( pnt[ 1 ] ), double( pnt[ 2 ] ) + ); + unsigned long cId = this->m_PolyData->GetVerts( )->InsertNextCell( 1 ); + this->m_PolyData->GetVerts( )->InsertCellPoint( pId ); + this->m_PolyData->Modified( ); + this->m_Vertices[ evt->Node.Vertex ] = TVertexIds( pId, cId ); + + } // rof + */ } else if( mevt.CheckEvent( evt ) ) { @@ -138,7 +160,8 @@ Execute( const itk::Object* c, const itk::EventObject& e ) if( cevt.CheckEvent( evt ) ) { - // std::cout << "Collision"; + // TODO: std::cout << "collision" << std::endl; + } // fi /* this->SetPixel( @@ -156,23 +179,24 @@ Execute( const itk::Object* c, const itk::EventObject& e ) */ // Update visualization - if( this->m_Number == 0 || this->m_Percentage < 0 ) - this->Render( ); - this->m_Number++; - this->m_Number %= this->m_Percentage; - - if( eevt.CheckEvent( evt ) ) - { - if( this->m_RenderWindow != NULL ) - { - vtkRenderer* ren = - this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( ); - ren->RemoveActor( this->m_PolyDataActor ); - this->Render( ); - - } // fi - - } // fi + /* TODO + if( this->m_Number == 0 || this->m_Percentage < 0 ) + this->Render( ); + this->m_Number++; + this->m_Number %= this->m_Percentage; + if( eevt.CheckEvent( evt ) ) + { + if( this->m_RenderWindow != NULL ) + { + vtkRenderer* ren = + this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( ); + ren->RemoveActor( this->m_PolyDataActor ); + this->Render( ); + + } // fi + + } // fi + */ } // fi } @@ -182,20 +206,20 @@ template< class F, class R > fpa::VTK::Image3DObserver< F, R >:: Image3DObserver( ) : Superclass( ), - m_RenderWindow( NULL ), - m_Number( 0 ), - m_Percentage( 0 ) + m_RenderWindow( NULL ) { - this->m_PolyData = vtkSmartPointer< vtkPolyData >::New( ); - this->m_PolyDataMapper = vtkSmartPointer< vtkPolyDataMapper >::New( ); - this->m_PolyDataActor = vtkSmartPointer< vtkActor >::New( ); + /* + this->m_PolyData = vtkSmartPointer< vtkPolyData >::New( ); + this->m_PolyDataMapper = vtkSmartPointer< vtkPolyDataMapper >::New( ); + this->m_PolyDataActor = vtkSmartPointer< vtkActor >::New( ); - this->m_PolyData->SetPoints( vtkPoints::New( ) ); - this->m_PolyData->SetVerts( vtkCellArray::New( ) ); + this->m_PolyData->SetPoints( vtkPoints::New( ) ); + this->m_PolyData->SetVerts( vtkCellArray::New( ) ); - this->m_PolyDataMapper->SetInputData( this->m_PolyData ); - this->m_PolyDataActor->SetMapper( this->m_PolyDataMapper ); - this->m_PolyDataActor->GetProperty( )->SetColor( 0, 0, 1 ); + this->m_PolyDataMapper->SetInputData( this->m_PolyData ); + this->m_PolyDataActor->SetMapper( this->m_PolyDataMapper ); + this->m_PolyDataActor->GetProperty( )->SetColor( 0, 0, 1 ); + */ } #endif // __FPA__VTK__IMAGE3DOBSERVER__HXX__ -- 2.47.1