1 #ifndef __FPA__VTK__IMAGE3DOBSERVER__HXX__
2 #define __FPA__VTK__IMAGE3DOBSERVER__HXX__
4 #include <vtkCellArray.h>
6 #include <vtkProperty.h>
7 #include <vtkRenderer.h>
8 #include <vtkRendererCollection.h>
10 #include <vtkCellArray.h>
11 #include <vtkPolyDataMapper.h>
12 #include <vtkPoints.h>
13 #include <vtkPointData.h>
16 // -------------------------------------------------------------------------
17 template< class F, class R >
18 void fpa::VTK::Image3DObserver< F, R >::
19 SetImage( const TImage* img, R* rw )
22 this->m_RenderWindow = rw;
23 this->m_Vertices.clear( );
26 unsigned int minD = TImage::ImageDimension;
27 minD = ( minD < 3 )? minD: 3;
30 typename TImage::RegionType reg = this->m_Image->GetRequestedRegion( );
31 for( unsigned int i = 0; i < minD; i++ )
33 e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ];
34 e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1;
38 typename TImage::SpacingType spac = this->m_Image->GetSpacing( );
39 double s[ 3 ] = { 1, 1, 1 };
40 for( unsigned int i = 0; i < minD; i++ )
41 s[ i ] = double( spac[ i ] );
43 typename TImage::PointType orig = this->m_Image->GetOrigin( );
44 double o[ 3 ] = { 0 };
45 for( unsigned int i = 0; i < minD; i++ )
46 o[ i ] = double( orig[ i ] );
48 this->m_Stencil->SetExtent( e );
49 this->m_Stencil->SetSpacing( s );
50 this->m_Stencil->SetOrigin( o );
51 this->m_Stencil->AllocateScalars( VTK_UNSIGNED_CHAR, 4 );
52 for( unsigned int i = 0; i < 3; i++ )
53 this->m_Stencil->GetPointData( )->
54 GetScalars( )->FillComponent( i, 255 );
55 this->m_Stencil->GetPointData( )->GetScalars( )->FillComponent( 3, 0 );
57 this->m_StencilActor->SetInputData( this->m_Stencil );
58 this->m_StencilActor->InterpolateOff( );
62 double( this->m_Image->GetRequestedRegion( ).GetNumberOfPixels( ) );
63 this->m_Percentage = ( unsigned long )( nPix * 0.001 );
66 if( this->m_RenderWindow != NULL )
69 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
70 ren->AddActor( this->m_PolyDataActor );
76 // -------------------------------------------------------------------------
77 template< class F, class R >
78 void fpa::VTK::Image3DObserver< F, R >::
81 if( this->m_RenderWindow != NULL )
82 this->m_RenderWindow->Render( );
85 // -------------------------------------------------------------------------
86 template< class F, class R >
87 void fpa::VTK::Image3DObserver< F, R >::
88 Execute( const itk::Object* c, const itk::EventObject& e )
90 typedef typename TImage::IndexType TIndex;
91 typedef typename TImage::PointType TPoint;
92 typedef typename TFilter::TEvent TEvent;
93 typedef typename TFilter::TFrontEvent TFrontEvent;
94 typedef typename TFilter::TMarkEvent TMarkEvent;
95 typedef typename TFilter::TCollisionEvent TCollisionEvent;
96 typedef typename TFilter::TEndEvent TEndEvent;
98 if( this->m_RenderWindow == NULL )
101 const TEvent* evt = dynamic_cast< const TEvent* >( &e );
104 TCollisionEvent cevt;
108 typename TImage::PointType pnt;
109 this->m_Image->TransformIndexToPhysicalPoint( evt->Node.Vertex, pnt );
111 if( fevt.CheckEvent( evt ) )
114 this->m_Vertices.find( evt->Node.Vertex ) == this->m_Vertices.end( )
117 unsigned long pId = this->m_PolyData->GetPoints( )->InsertNextPoint(
118 double( pnt[ 0 ] ), double( pnt[ 1 ] ), double( pnt[ 2 ] )
120 unsigned long cId = this->m_PolyData->GetVerts( )->InsertNextCell( 1 );
121 this->m_PolyData->GetVerts( )->InsertCellPoint( pId );
122 this->m_PolyData->Modified( );
123 this->m_Vertices[ evt->Node.Vertex ] = TVertexIds( pId, cId );
127 else if( mevt.CheckEvent( evt ) )
130 typename TVertices::iterator vIt =
131 this->m_Vertices.find( evt->Node.Vertex );
132 if( vIt != this->m_Vertices.end( ) )
134 std::cout << "Erase " << evt->Node.Vertex << " " << vIt->second.second << std::endl;
139 if( cevt.CheckEvent( evt ) )
141 // std::cout << "Collision";
146 Colors[ evt->Node.FrontId ][ 0 ],
147 Colors[ evt->Node.FrontId ][ 1 ],
148 Colors[ evt->Node.FrontId ][ 2 ],
149 Colors[ evt->Node.FrontId ][ 3 ]
151 else if( mevt.CheckEvent( evt ) )
152 this->SetPixel( evt->Node.Vertex, 255, 0, 0, 255 );
154 if( cevt.CheckEvent( evt ) )
155 this->SetPixel( evt->Node.Vertex, 100, 50, 25, 255 );
158 // Update visualization
159 if( this->m_Number == 0 || this->m_Percentage < 0 )
162 this->m_Number %= this->m_Percentage;
164 if( eevt.CheckEvent( evt ) )
166 if( this->m_RenderWindow != NULL )
169 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
170 // TODO: ren->RemoveActor( this->m_PolyDataActor );
180 // -------------------------------------------------------------------------
181 template< class F, class R >
182 fpa::VTK::Image3DObserver< F, R >::
185 m_RenderWindow( NULL ),
189 this->m_PolyData = vtkSmartPointer< vtkPolyData >::New( );
190 this->m_PolyDataMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
191 this->m_PolyDataActor = vtkSmartPointer< vtkActor >::New( );
193 this->m_PolyData->SetPoints( vtkPoints::New( ) );
194 this->m_PolyData->SetVerts( vtkCellArray::New( ) );
196 this->m_PolyDataMapper->SetInputData( this->m_PolyData );
197 this->m_PolyDataActor->SetMapper( this->m_PolyDataMapper );
198 this->m_PolyDataActor->GetProperty( )->SetColor( 0, 0, 1 );
201 #endif // __FPA__VTK__IMAGE3DOBSERVER__HXX__