]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Mesh.cxx
216f3b3ac9cf61b4300789d920d2cb3c21d6dfa0
[cpPlugins.git] / lib / cpPlugins / Mesh.cxx
1 #include <cpPlugins/Mesh.h>
2
3 #include <itkMesh.h>
4 #include <itkLineCell.h>
5 #include <itkTriangleCell.h>
6 #include <itkPolygonCell.h>
7
8 #include <vtkPolyData.h>
9
10 // -------------------------------------------------------------------------
11 void cpPlugins::Mesh::
12 SetITK( itk::LightObject* o )
13 {
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 );
19 }
20
21 // -------------------------------------------------------------------------
22 void cpPlugins::Mesh::
23 SetVTK( vtkObjectBase* o )
24 {
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;
31
32   vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
33   if( mesh == NULL )
34   {
35     this->m_ITKObject = NULL;
36     this->Modified( );
37     return;
38
39   } // fi
40
41   if( this->m_VTKObject.GetPointer( ) != mesh )
42   {
43     this->m_VTKObject = mesh;
44
45     // Copy points
46     _TMesh::Pointer imesh = _TMesh::New( );
47     double point[ 3 ];
48     for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
49     {
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 );
56
57     } // rof
58
59     // Copy cells
60     for( long i = 0; i < mesh->GetNumberOfCells( ); ++i )
61     {
62       auto cell = mesh->GetCell( i );
63       long nPoints = cell->GetNumberOfPoints( );
64       _TCellAutoPointer icell;
65       if( nPoints == 2 )
66       {
67         icell.TakeOwnership( new _TLine );
68         icell->SetPointId( 0, cell->GetPointId( 0 ) );
69         icell->SetPointId( 1, cell->GetPointId( 1 ) );
70       }
71       else if( nPoints == 3 )
72       {
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 ) );
77       }
78       else if( nPoints > 3 )
79       {
80         _TPolygon* polygon = new _TPolygon( );
81         for( long j = 0; j < nPoints; ++j )
82           polygon->AddPointId( cell->GetPointId( j ) );
83         icell.TakeOwnership( polygon );
84
85       } // fi
86       imesh->SetCell( imesh->GetNumberOfCells( ), icell );
87
88     } // rof
89     this->m_ITKObject = imesh;
90     this->Modified( );
91
92   } // fi
93 }
94 // -------------------------------------------------------------------------
95 cpPlugins::Mesh::
96 Mesh( )
97   : Superclass( )
98 {
99 }
100
101 // -------------------------------------------------------------------------
102 cpPlugins::Mesh::
103 ~Mesh( )
104 {
105 }
106
107 // eof - $RCSfile$