]> Creatis software - cpMesh.git/blob - lib/cpm/VTK/PolyDataToMeshFilter.hxx
First commit
[cpMesh.git] / lib / cpm / VTK / PolyDataToMeshFilter.hxx
1 #ifndef __CPM__VTK__POLYDATATOMESHFILTER__HXX__
2 #define __CPM__VTK__POLYDATATOMESHFILTER__HXx__
3
4 #include <itkPolygonCell.h>
5 #include <vtkCell.h>
6
7 // -------------------------------------------------------------------------
8 template< class M >
9 const vtkPolyData* cpm::VTK::PolyDataToMeshFilter< M >::
10 GetInput( ) const
11 {
12   return( this->m_Input );
13 }
14
15 // -------------------------------------------------------------------------
16 template< class M >
17 void cpm::VTK::PolyDataToMeshFilter< M >::
18 SetInput( const vtkPolyData* input )
19 {
20   if( this->m_Input != input )
21   {
22     this->m_Input = input;
23     this->Modified( );
24
25   } // fi
26 }
27
28 // -------------------------------------------------------------------------
29 template< class M >
30 cpm::VTK::PolyDataToMeshFilter< M >::
31 PolyDataToMeshFilter( )
32   : Superclass( ),
33     m_Input( NULL )
34 {
35 }
36
37 // -------------------------------------------------------------------------
38 template< class M >
39 cpm::VTK::PolyDataToMeshFilter< M >::
40 ~PolyDataToMeshFilter( )
41 {
42 }
43
44 // -------------------------------------------------------------------------
45 template< class M >
46 void cpm::VTK::PolyDataToMeshFilter< M >::
47 GenerateData( )
48 {
49   typedef typename M::PixelType _TScalar;
50   typedef itk::PolygonCell< typename M::CellType > _TPolygonCell;
51
52   vtkPolyData* in = const_cast< vtkPolyData* >( this->m_Input );
53   typename M::Pointer out = this->GetOutput( );
54   out->Initialize( );
55   if( in == NULL )
56     return;
57
58   // Assign points
59   double vpnt[ 3 ];
60   typename M::PointType ipnt;
61   for( unsigned long pId = 0; pId < in->GetNumberOfPoints( ); ++pId )
62   {
63     in->GetPoint( pId, vpnt );
64     ipnt.Fill( _TScalar( 0 ) );
65     if( M::PointDimension > 0 ) ipnt[ 0 ] = _TScalar( vpnt[ 0 ] );
66     if( M::PointDimension > 1 ) ipnt[ 1 ] = _TScalar( vpnt[ 1 ] );
67     if( M::PointDimension > 2 ) ipnt[ 2 ] = _TScalar( vpnt[ 2 ] );
68     out->SetPoint( pId, ipnt );
69
70   } // rof
71
72   // Assign cells
73   for( unsigned long cId = 0; cId < in->GetNumberOfCells( ); ++cId )
74   {
75     vtkCell* vcell = in->GetCell( cId );
76     unsigned int nPoints = vcell->GetNumberOfPoints( );
77     if( 2 < nPoints )
78     {
79       // Add a face
80       typename M::CellAutoPointer cell;
81       _TPolygonCell* face = new _TPolygonCell( );
82       for( unsigned int k = 0; k < nPoints; ++k )
83         face->AddPointId( vcell->GetPointId( k ) );
84       cell.TakeOwnership( face );
85       out->SetCell( out->GetNumberOfCells( ), cell );
86
87     } // fi
88
89   } // rof
90 }
91
92 #endif // __CPM__VTK__POLYDATATOMESHFILTER__HXX__
93
94 // eof - $RCSfile$