1 #include <cpPlugins/Mesh.h>
4 #include <itkQuadEdgeMesh.h>
5 #include <itkLineCell.h>
6 #include <itkTriangleCell.h>
7 #include <itkPolygonCell.h>
9 #include <vtkPolyData.h>
11 // -------------------------------------------------------------------------
12 void cpPlugins::Mesh::
13 SetITK( itk::LightObject* o )
15 this->Superclass::SetITK( o );
16 bool r = this->_ITK_2_VTK< itk::Mesh< float, 2 > >( o );
17 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 2 > >( o );
18 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< float, 3 > >( o );
19 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 3 > >( o );
20 if( !r ) r = this->_ITK_2_VTK< itk::QuadEdgeMesh< float, 2 > >( o );
21 if( !r ) r = this->_ITK_2_VTK< itk::QuadEdgeMesh< double, 2 > >( o );
22 if( !r ) r = this->_ITK_2_VTK< itk::QuadEdgeMesh< float, 3 > >( o );
23 if( !r ) r = this->_ITK_2_VTK< itk::QuadEdgeMesh< double, 3 > >( o );
26 // -------------------------------------------------------------------------
27 void cpPlugins::Mesh::
28 SetVTK( vtkObjectBase* o )
30 typedef itk::Mesh< double, 3 > _TMesh;
31 typedef _TMesh::CellType _TCell;
32 typedef _TCell::CellAutoPointer _TCellAutoPointer;
33 typedef itk::LineCell< _TCell > _TLine;
34 typedef itk::TriangleCell< _TCell > _TTriangle;
35 typedef itk::PolygonCell< _TCell > _TPolygon;
37 vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
40 this->m_ITKObject = NULL;
46 if( this->m_VTKObject.GetPointer( ) != mesh )
48 this->m_VTKObject = mesh;
52 _TMesh::Pointer imesh = _TMesh::New( );
54 for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
56 mesh->GetPoint( i, point );
57 _TMesh::PointType ipoint;
58 ipoint[ 0 ] = point[ 0 ];
59 ipoint[ 1 ] = point[ 1 ];
60 ipoint[ 2 ] = point[ 2 ];
61 imesh->SetPoint( i, ipoint );
66 for( long i = 0; i < mesh->GetNumberOfCells( ); ++i )
68 auto cell = mesh->GetCell( i );
69 long nPoints = cell->GetNumberOfPoints( );
70 _TCellAutoPointer icell;
73 icell.TakeOwnership( new _TLine );
74 icell->SetPointId( 0, cell->GetPointId( 0 ) );
75 icell->SetPointId( 1, cell->GetPointId( 1 ) );
77 else if( nPoints == 3 )
79 icell.TakeOwnership( new _TTriangle );
80 icell->SetPointId( 0, cell->GetPointId( 0 ) );
81 icell->SetPointId( 1, cell->GetPointId( 1 ) );
82 icell->SetPointId( 2, cell->GetPointId( 2 ) );
84 else if( nPoints > 3 )
86 _TPolygon* polygon = new _TPolygon( );
87 for( long j = 0; j < nPoints; ++j )
88 polygon->AddPointId( cell->GetPointId( j ) );
89 icell.TakeOwnership( polygon );
92 imesh->SetCell( imesh->GetNumberOfCells( ), icell );
96 this->m_ITKObject = imesh;
98 this->m_ITKObject = NULL; // TODO: replace this
103 // -------------------------------------------------------------------------
110 // -------------------------------------------------------------------------