#ifndef __FPA__VTK__IMAGE2DOBSERVER__HXX__ #define __FPA__VTK__IMAGE2DOBSERVER__HXX__ #include #include #include #include #include #include #include // ------------------------------------------------------------------------- template< class F, class R > void fpa::VTK::Image2DObserver< F, R >:: SetRenderWindow( R* rw ) { this->m_RenderWindow = rw; } // ------------------------------------------------------------------------- template< class F, class R > void fpa::VTK::Image2DObserver< F, R >:: SetPixel( typename TImage::IndexType idx, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha ) { this->m_Stencil->SetScalarComponentFromDouble ( idx[ 0 ], idx[ 1 ], 0, 0, red ); this->m_Stencil->SetScalarComponentFromDouble ( idx[ 0 ], idx[ 1 ], 0, 1, green ); this->m_Stencil->SetScalarComponentFromDouble ( idx[ 0 ], idx[ 1 ], 0, 2, blue ); this->m_Stencil->SetScalarComponentFromDouble ( idx[ 0 ], idx[ 1 ], 0, 3, alpha ); this->m_Stencil->Modified( ); this->m_StencilActor->Modified( ); } // ------------------------------------------------------------------------- template< class F, class R > void fpa::VTK::Image2DObserver< F, R >:: Render( ) { if( this->m_RenderWindow != NULL ) this->m_RenderWindow->Render( ); } // ------------------------------------------------------------------------- template< class F, class R > void fpa::VTK::Image2DObserver< 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; 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 ); if( startEvt != 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 ); this->m_StencilActor->SetInputData( this->m_Stencil ); this->m_StencilActor->InterpolateOff( ); this->m_Count = 0; this->m_RenderCount = reg.GetNumberOfPixels( ); vtkRenderer* ren = this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( ); ren->AddActor( this->m_StencilActor ); 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_StencilActor ); this->Render( ); return; } // fi } // ------------------------------------------------------------------------- template< class F, class R > fpa::VTK::Image2DObserver< F, R >:: Image2DObserver( ) : Superclass( ), m_RenderWindow( NULL ), m_RenderPercentage( double( 0.01 ) ) { this->m_Stencil = vtkSmartPointer< vtkImageData >::New( ); this->m_StencilActor = vtkSmartPointer< vtkImageActor >::New( ); } // ------------------------------------------------------------------------- template< class F, class R > fpa::VTK::Image2DObserver< F, R >:: ~Image2DObserver( ) { } #endif // __FPA__VTK__IMAGE2DOBSERVER__HXX__ // eof - $RCSfile$