1 #include <cpPlugins/Mesh.h>
4 #include <itkLineCell.h>
5 #include <itkTriangleCell.h>
6 #include <itkPolygonCell.h>
8 #include <vtkPolyData.h>
10 // -------------------------------------------------------------------------
11 void cpPlugins::Mesh::
12 SetITK( itk::LightObject* o )
14 this->Superclass::SetITK( o );
15 bool r = this->_ITK_2_VTK< itk::Mesh< float, 2 > >( o );
16 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 2 > >( o );
17 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< float, 3 > >( o );
18 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 3 > >( o );
21 // -------------------------------------------------------------------------
22 void cpPlugins::Mesh::
23 SetVTK( vtkObjectBase* o )
25 typedef itk::Mesh< double, 3 > _TMesh;
26 typedef _TMesh::CellType _TCell;
27 typedef _TCell::CellAutoPointer _TCellAutoPointer;
28 typedef itk::LineCell< _TCell > _TLine;
29 typedef itk::TriangleCell< _TCell > _TTriangle;
30 typedef itk::PolygonCell< _TCell > _TPolygon;
32 vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
35 this->m_ITKObject = NULL;
41 if( this->m_VTKObject.GetPointer( ) != mesh )
43 this->m_VTKObject = mesh;
46 _TMesh::Pointer imesh = _TMesh::New( );
48 for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
50 mesh->GetPoint( i, point );
51 _TMesh::PointType ipoint;
52 ipoint[ 0 ] = point[ 0 ];
53 ipoint[ 1 ] = point[ 1 ];
54 ipoint[ 2 ] = point[ 2 ];
55 imesh->SetPoint( i, ipoint );
60 for( long i = 0; i < mesh->GetNumberOfCells( ); ++i )
62 auto cell = mesh->GetCell( i );
63 long nPoints = cell->GetNumberOfPoints( );
64 _TCellAutoPointer icell;
67 icell.TakeOwnership( new _TLine );
68 icell->SetPointId( 0, cell->GetPointId( 0 ) );
69 icell->SetPointId( 1, cell->GetPointId( 1 ) );
71 else if( nPoints == 3 )
73 icell.TakeOwnership( new _TTriangle );
74 icell->SetPointId( 0, cell->GetPointId( 0 ) );
75 icell->SetPointId( 1, cell->GetPointId( 1 ) );
76 icell->SetPointId( 2, cell->GetPointId( 2 ) );
78 else if( nPoints > 3 )
80 _TPolygon* polygon = new _TPolygon( );
81 for( long j = 0; j < nPoints; ++j )
82 polygon->AddPointId( cell->GetPointId( j ) );
83 icell.TakeOwnership( polygon );
86 imesh->SetCell( imesh->GetNumberOfCells( ), icell );
89 this->m_ITKObject = imesh;
94 // -------------------------------------------------------------------------
101 // -------------------------------------------------------------------------