#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 >:: SetImage( const TImage* img, R* rw ) { this->m_Image = img; this->m_RenderWindow = rw; 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++ ) { 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.01 ); this->m_Number = 0; if( this->m_RenderWindow != NULL ) { vtkRenderer* ren = this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( ); ren->AddActor( this->m_StencilActor ); this->Render( ); } // fi } // ------------------------------------------------------------------------- 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 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; 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 } }; if( this->m_RenderWindow == NULL ) return; const TEvent* evt = dynamic_cast< const TEvent* >( &e ); TFrontEvent fevt; TMarkEvent mevt; TCollisionEvent cevt; TEndEvent eevt; if( evt != NULL ) { if( mevt.CheckEvent( evt ) ) this->SetPixel( evt->Node.Vertex, Colors[ evt->Node.FrontId ][ 0 ], Colors[ evt->Node.FrontId ][ 1 ], Colors[ evt->Node.FrontId ][ 2 ], Colors[ evt->Node.FrontId ][ 3 ] ); else if( fevt.CheckEvent( evt ) ) this->SetPixel( evt->Node.Vertex, 255, 0, 0, 255 ); if( cevt.CheckEvent( evt ) ) this->SetPixel( evt->Node.Vertex, 100, 50, 25, 255 ); // 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 ) { /* TODO vtkRenderer* ren = this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( ); ren->RemoveActor( this->m_StencilActor ); */ this->Render( ); } // fi } // fi } else { // TODO: Remove all! this->Render( ); } // fi } #endif // __FPA__VTK__IMAGE2DOBSERVER__HXX__ // eof - $RCSfile$