1 #ifndef __FPA__VTK__IMAGE2DOBSERVER__HXX__
2 #define __FPA__VTK__IMAGE2DOBSERVER__HXX__
4 #include <vtkCellArray.h>
5 #include <vtkPolyDataMapper.h>
7 #include <vtkPointData.h>
8 #include <vtkProperty.h>
9 #include <vtkRenderer.h>
10 #include <vtkRendererCollection.h>
12 // -------------------------------------------------------------------------
13 template< class F, class R >
14 void fpa::VTK::Image2DObserver< F, R >::
15 SetImage( const TImage* img, R* rw )
18 this->m_RenderWindow = rw;
20 unsigned int minD = TImage::ImageDimension;
21 minD = ( minD < 3 )? minD: 3;
24 typename TImage::RegionType reg = this->m_Image->GetRequestedRegion( );
25 for( unsigned int i = 0; i < minD; i++ )
27 e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ];
28 e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1;
32 typename TImage::SpacingType spac = this->m_Image->GetSpacing( );
33 double s[ 3 ] = { 1, 1, 1 };
34 for( unsigned int i = 0; i < minD; i++ )
35 s[ i ] = double( spac[ i ] );
37 typename TImage::PointType orig = this->m_Image->GetOrigin( );
38 double o[ 3 ] = { 0 };
39 for( unsigned int i = 0; i < minD; i++ )
40 o[ i ] = double( orig[ i ] );
42 this->m_Stencil->SetExtent( e );
43 this->m_Stencil->SetSpacing( s );
44 this->m_Stencil->SetOrigin( o );
45 this->m_Stencil->AllocateScalars( VTK_UNSIGNED_CHAR, 4 );
46 for( unsigned int i = 0; i < 3; i++ )
47 this->m_Stencil->GetPointData( )->
48 GetScalars( )->FillComponent( i, 255 );
49 this->m_Stencil->GetPointData( )->GetScalars( )->FillComponent( 3, 0 );
51 this->m_StencilActor->SetInputData( this->m_Stencil );
52 this->m_StencilActor->InterpolateOff( );
55 double( this->m_Image->GetRequestedRegion( ).GetNumberOfPixels( ) );
56 this->m_Percentage = ( unsigned long )( nPix * 0.01 );
59 if( this->m_RenderWindow != NULL )
62 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
63 ren->AddActor( this->m_StencilActor );
69 // -------------------------------------------------------------------------
70 template< class F, class R >
71 void fpa::VTK::Image2DObserver< F, R >::
73 typename TImage::IndexType idx,
80 this->m_Stencil->SetScalarComponentFromDouble
81 ( idx[ 0 ], idx[ 1 ], 0, 0, red );
82 this->m_Stencil->SetScalarComponentFromDouble
83 ( idx[ 0 ], idx[ 1 ], 0, 1, green );
84 this->m_Stencil->SetScalarComponentFromDouble
85 ( idx[ 0 ], idx[ 1 ], 0, 2, blue );
86 this->m_Stencil->SetScalarComponentFromDouble
87 ( idx[ 0 ], idx[ 1 ], 0, 3, alpha );
88 this->m_Stencil->Modified( );
89 this->m_StencilActor->Modified( );
92 // -------------------------------------------------------------------------
93 template< class F, class R >
94 void fpa::VTK::Image2DObserver< F, R >::
97 if( this->m_RenderWindow != NULL )
98 this->m_RenderWindow->Render( );
101 // -------------------------------------------------------------------------
102 template< class F, class R >
103 void fpa::VTK::Image2DObserver< F, R >::
104 Execute( const itk::Object* c, const itk::EventObject& e )
106 typedef typename TImage::IndexType TIndex;
107 typedef typename TImage::PointType TPoint;
108 typedef typename TFilter::TEvent TEvent;
109 typedef typename TFilter::TFrontEvent TFrontEvent;
110 typedef typename TFilter::TMarkEvent TMarkEvent;
111 typedef typename TFilter::TCollisionEvent TCollisionEvent;
112 typedef typename TFilter::TEndEvent TEndEvent;
114 static unsigned char Colors[][4] =
117 { 0, 127, 127, 127 },
118 { 127, 0, 127, 127 },
119 { 127, 127, 0, 127 },
124 { 63, 63, 127, 127 },
125 { 63, 127, 127, 127 },
126 { 127, 63, 127, 127 },
127 { 127, 127, 63, 127 },
128 { 127, 127, 63, 127 },
129 { 127, 63, 63, 127 },
130 { 63, 127, 63, 127 },
134 if( this->m_RenderWindow == NULL )
137 const TEvent* evt = dynamic_cast< const TEvent* >( &e );
140 TCollisionEvent cevt;
144 if( mevt.CheckEvent( evt ) )
147 Colors[ evt->Node.FrontId ][ 0 ],
148 Colors[ evt->Node.FrontId ][ 1 ],
149 Colors[ evt->Node.FrontId ][ 2 ],
150 Colors[ evt->Node.FrontId ][ 3 ]
152 else if( fevt.CheckEvent( evt ) )
153 this->SetPixel( evt->Node.Vertex, 255, 0, 0, 255 );
154 if( cevt.CheckEvent( evt ) )
155 this->SetPixel( evt->Node.Vertex, 100, 50, 25, 255 );
157 // Update visualization
158 if( this->m_Number == 0 || this->m_Percentage < 0 )
161 this->m_Number %= this->m_Percentage;
163 if( eevt.CheckEvent( evt ) )
165 if( this->m_RenderWindow != NULL )
169 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
170 ren->RemoveActor( this->m_StencilActor );
186 #endif // __FPA__VTK__IMAGE2DOBSERVER__HXX__