]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/VTK/Image2DObserver.hxx
First commit
[FrontAlgorithms.git] / lib / fpa / VTK / Image2DObserver.hxx
1 #ifndef __FPA__VTK__IMAGE2DOBSERVER__HXX__
2 #define __FPA__VTK__IMAGE2DOBSERVER__HXX__
3
4 #include <vtkCellArray.h>
5 #include <vtkPolyDataMapper.h>
6 #include <vtkPoints.h>
7 #include <vtkPointData.h>
8 #include <vtkProperty.h>
9 #include <vtkRenderer.h>
10 #include <vtkRendererCollection.h>
11
12 // -------------------------------------------------------------------------
13 template< class F, class R >
14 void fpa::VTK::Image2DObserver< F, R >::
15 SetImage( const TImage* img, R* rw )
16 {
17   this->m_Image = img;
18   this->m_RenderWindow = rw;
19
20   unsigned int minD = TImage::ImageDimension;
21   minD = ( minD < 3 )? minD: 3;
22
23   int e[ 6 ] = { 0 };
24   typename TImage::RegionType reg = this->m_Image->GetRequestedRegion( );
25   for( unsigned int i = 0; i < minD; i++ )
26   {
27     e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ];
28     e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1;
29
30   } // rof
31
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 ] );
36
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 ] );
41
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 );
50
51   this->m_StencilActor->SetInputData( this->m_Stencil );
52   this->m_StencilActor->InterpolateOff( );
53
54   double nPix =
55     double( this->m_Image->GetRequestedRegion( ).GetNumberOfPixels( ) );
56   this->m_Percentage = ( unsigned long )( nPix * 0.01 );
57   this->m_Number = 0;
58
59   if( this->m_RenderWindow != NULL )
60   {
61     vtkRenderer* ren =
62       this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
63     ren->AddActor( this->m_StencilActor );
64     this->Render( );
65
66   } // fi
67 }
68
69 // -------------------------------------------------------------------------
70 template< class F, class R >
71 void fpa::VTK::Image2DObserver< F, R >::
72 SetPixel(
73   typename TImage::IndexType idx,
74   unsigned char red,
75   unsigned char green,
76   unsigned char blue,
77   unsigned char alpha
78   )
79 {
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( );
90 }
91
92 // -------------------------------------------------------------------------
93 template< class F, class R >
94 void fpa::VTK::Image2DObserver< F, R >::
95 Render( )
96 {
97   if( this->m_RenderWindow != NULL )
98     this->m_RenderWindow->Render( );
99 }
100
101 // -------------------------------------------------------------------------
102 template< class F, class R >
103 void fpa::VTK::Image2DObserver< F, R >::
104 Execute( const itk::Object* c, const itk::EventObject& e )
105 {
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;
113
114   static unsigned char Colors[][4] =
115     {
116       {   0,   0, 127, 127 },
117       {   0, 127, 127, 127 },
118       { 127,   0, 127, 127 },
119       { 127, 127,   0, 127 },
120       {   0,   0,  63, 127 },
121       {   0,  63,  63, 127 },
122       {  63,   0,  63, 127 },
123       {  63,  63,   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 },
131       {  63,  63, 127, 127 }
132     };
133
134   if( this->m_RenderWindow == NULL )
135     return;
136
137   const TEvent* evt = dynamic_cast< const TEvent* >( &e );
138   TFrontEvent fevt;
139   TMarkEvent mevt;
140   TCollisionEvent cevt;
141   TEndEvent eevt;
142   if( evt != NULL )
143   {
144     if( mevt.CheckEvent( evt ) )
145       this->SetPixel(
146         evt->Node.Vertex,
147         Colors[ evt->Node.FrontId ][ 0 ],
148         Colors[ evt->Node.FrontId ][ 1 ],
149         Colors[ evt->Node.FrontId ][ 2 ],
150         Colors[ evt->Node.FrontId ][ 3 ]
151         );
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 );
156
157     // Update visualization
158     if( this->m_Number == 0 || this->m_Percentage < 0 )
159       this->Render( );
160     this->m_Number++;
161     this->m_Number %= this->m_Percentage;
162
163     if( eevt.CheckEvent( evt ) )
164     {
165       if( this->m_RenderWindow != NULL )
166       {
167         /* TODO
168            vtkRenderer* ren =
169            this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
170            ren->RemoveActor( this->m_StencilActor );
171         */
172         this->Render( );
173
174       } // fi
175
176     } // fi
177   }
178   else
179   {
180     // TODO: Remove all!
181     this->Render( );
182
183   } // fi
184 }
185
186 #endif // __FPA__VTK__IMAGE2DOBSERVER__HXX__
187
188 // eof - $RCSfile$