#ifndef __FPA__VTK__IMAGE3DOBSERVER__HXX__ #define __FPA__VTK__IMAGE3DOBSERVER__HXX__ #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 >:: 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; 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 F::TInputImage::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 F, class R > fpa::VTK::Image3DObserver< F, R >:: Image3DObserver( ) : Superclass( ), m_RenderWindow( NULL ), m_RenderPercentage( double( 0.001 ) ) { 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 F, class R > fpa::VTK::Image3DObserver< F, R >:: ~Image3DObserver( ) { } #endif // __FPA__VTK__IMAGE3DOBSERVER__HXX__ // eof - $RCSfile$