#ifndef __FPA__VTK__IMAGE__OBSERVER3D__HXX__ #define __FPA__VTK__IMAGE__OBSERVER3D__HXX__ #include #include #include #include #include #include #include // ------------------------------------------------------------------------- template< class _TFilter, class _TRenderWindow > void fpa::VTK::Image::Observer3D< _TFilter, _TRenderWindow >:: SetRenderWindow( _TRenderWindow* rw ) { this->m_RenderWindow = rw; } // ------------------------------------------------------------------------- template< class _TFilter, class _TRenderWindow > void fpa::VTK::Image::Observer3D< _TFilter, _TRenderWindow >:: Render( ) { if( this->m_RenderWindow != NULL ) this->m_RenderWindow->Render( ); } // ------------------------------------------------------------------------- template< class _TFilter, class _TRenderWindow > void fpa::VTK::Image::Observer3D< _TFilter, _TRenderWindow >:: Execute( const itk::Object* c, const itk::EventObject& e ) { typedef typename _TFilter::TStartEvent _TStartEvent; typedef typename _TFilter::TStartLoopEvent _TStartLoopEvent; typedef typename _TFilter::TEndEvent _TEndEvent; typedef typename _TFilter::TEndLoopEvent _TEndLoopEvent; typedef typename _TFilter::TAliveEvent _TAliveEvent; typedef typename _TFilter::TFrontEvent _TFrontEvent; typedef typename _TFilter::TFreezeEvent _TFreezeEvent; typedef typename _TFilter::TStartBacktrackingEvent _TStartBacktrackingEvent; typedef typename _TFilter::TEndBacktrackingEvent _TEndBacktrackingEvent; typedef typename _TFilter::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 } }; auto filter = dynamic_cast< const _TFilter* >( c ); if( this->m_RenderWindow == NULL || filter == NULL ) return; auto image = filter->GetInput( ); if( image == NULL ) return; const _TStartEvent* startEvt = dynamic_cast< const _TStartEvent* >( &e ); const _TStartBacktrackingEvent* startBackEvt = dynamic_cast< const _TStartBacktrackingEvent* >( &e ); if( startEvt != NULL || startBackEvt != NULL ) { this->m_Count = 0; this->m_RenderCount = image->GetLargestPossibleRegion( ).GetNumberOfPixels( ); this->m_PointsToReplace.clear( ); this->m_PointsInFront.clear( ); vtkRenderer* ren = this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( ); ren->AddActor( this->m_PolyDataActor ); return; } // fi const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e ); if( frontEvt != NULL ) { typename TImage::PointType pnt; image->TransformIndexToPhysicalPoint( frontEvt->Vertex, pnt ); if( this->m_PointsToReplace.empty( ) ) { unsigned long nPoints = this->m_PolyData->GetNumberOfPoints( ); this->m_PolyData->GetPoints( )-> InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] ); this->m_PolyData->GetVerts( )->InsertNextCell( 1 ); this->m_PolyData->GetVerts( )->InsertCellPoint( nPoints ); this->m_PointsInFront[ frontEvt->Vertex ] = nPoints; } else { auto pIt = this->m_PointsToReplace.begin( ); this->m_PolyData->GetPoints( )-> SetPoint( pIt->second, pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] ); this->m_PointsToReplace.erase( pIt ); } // fi this->m_PolyData->Modified( ); this->m_PolyDataMapper->Modified( ); this->m_PolyDataActor->Modified( ); // Render visual debug this->m_Count++; 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 _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e ); if( aliveEvt != NULL ) { auto pIt = this->m_PointsInFront.find( aliveEvt->Vertex ); if( pIt != this->m_PointsInFront.end( ) ) { this->m_PointsToReplace[ pIt->first ] = pIt->second; this->m_PointsInFront.erase( pIt ); } // fi 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 ) { // TODO: 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 _TFilter, class _TRenderWindow > fpa::VTK::Image::Observer3D< _TFilter, _TRenderWindow >:: Observer3D( ) : Superclass( ), m_RenderWindow( NULL ), m_RenderPercentage( double( 0.000001 ) ) { this->m_PolyData = vtkSmartPointer< vtkPolyData >::New( ); this->m_PolyDataMapper = vtkSmartPointer< vtkPolyDataMapper >::New( ); this->m_PolyDataActor =vtkSmartPointer< vtkActor >::New( ); vtkSmartPointer< vtkPoints > points = vtkSmartPointer< vtkPoints >::New( ); vtkSmartPointer< vtkCellArray > verts = vtkSmartPointer< vtkCellArray >::New( ); this->m_PolyData->SetPoints( points ); this->m_PolyData->SetVerts( verts ); this->m_PolyDataMapper->SetInputData( this->m_PolyData ); this->m_PolyDataActor->SetMapper( this->m_PolyDataMapper ); this->m_PolyDataActor->GetProperty( )->SetColor( 0, 1, 0 ); } // ------------------------------------------------------------------------- template< class _TFilter, class _TRenderWindow > fpa::VTK::Image::Observer3D< _TFilter, _TRenderWindow >:: ~Observer3D( ) { } #endif // __FPA__VTK__IMAGE__OBSERVER3D__HXX__ // eof - $RCSfile$