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;
47 _TMesh::Pointer imesh = _TMesh::New( );
49 for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
51 mesh->GetPoint( i, point );
52 _TMesh::PointType ipoint;
53 ipoint[ 0 ] = point[ 0 ];
54 ipoint[ 1 ] = point[ 1 ];
55 ipoint[ 2 ] = point[ 2 ];
56 imesh->SetPoint( i, ipoint );
61 for( long i = 0; i < mesh->GetNumberOfCells( ); ++i )
63 auto cell = mesh->GetCell( i );
64 long nPoints = cell->GetNumberOfPoints( );
65 _TCellAutoPointer icell;
68 icell.TakeOwnership( new _TLine );
69 icell->SetPointId( 0, cell->GetPointId( 0 ) );
70 icell->SetPointId( 1, cell->GetPointId( 1 ) );
72 else if( nPoints == 3 )
74 icell.TakeOwnership( new _TTriangle );
75 icell->SetPointId( 0, cell->GetPointId( 0 ) );
76 icell->SetPointId( 1, cell->GetPointId( 1 ) );
77 icell->SetPointId( 2, cell->GetPointId( 2 ) );
79 else if( nPoints > 3 )
81 _TPolygon* polygon = new _TPolygon( );
82 for( long j = 0; j < nPoints; ++j )
83 polygon->AddPointId( cell->GetPointId( j ) );
84 icell.TakeOwnership( polygon );
87 imesh->SetCell( imesh->GetNumberOfCells( ), icell );
91 this->m_ITKObject = imesh;
93 this->m_ITKObject = NULL; // TODO: replace this
98 // -------------------------------------------------------------------------
105 // -------------------------------------------------------------------------