]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Mesh.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / Mesh.cxx
1 #include <cpPlugins/Interface/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::Interface::Mesh::
12 SetITK( itk::LightObject* o )
13 {
14   this->Superclass::SetITK( o );
15   this->_ITK_2_VTK< itk::Mesh< float, 2 > >( o );
16   this->_ITK_2_VTK< itk::Mesh< double, 2 > >( o );
17   this->_ITK_2_VTK< itk::Mesh< float, 3 > >( o );
18   this->_ITK_2_VTK< itk::Mesh< double, 3 > >( o );
19   this->_ITK_2_VTK< itk::QuadEdgeMesh< float, 2 > >( o );
20   this->_ITK_2_VTK< itk::QuadEdgeMesh< double, 2 > >( o );
21   this->_ITK_2_VTK< itk::QuadEdgeMesh< float, 3 > >( o );
22   this->_ITK_2_VTK< itk::QuadEdgeMesh< double, 3 > >( o );
23 }
24
25 // -------------------------------------------------------------------------
26 void cpPlugins::Interface::Mesh::
27 SetVTK( vtkObjectBase* o )
28 {
29   typedef itk::Mesh< double, 3 >      _TMesh;
30   typedef _TMesh::CellType            _TCell;
31   typedef _TCell::CellAutoPointer     _TCellAutoPointer;
32   typedef itk::LineCell< _TCell >     _TLine;
33   typedef itk::TriangleCell< _TCell > _TTriangle;
34   typedef itk::PolygonCell< _TCell >  _TPolygon;
35
36   vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
37   if( mesh == NULL )
38   {
39     this->m_ITKObject = NULL;
40     this->Modified( );
41     return;
42
43   } // fi
44
45   if( this->m_VTKObject.GetPointer( ) != mesh )
46   {
47     this->m_VTKObject = mesh;
48
49     // Copy points
50     _TMesh::Pointer imesh = _TMesh::New( );
51     double point[ 3 ];
52     for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
53     {
54       mesh->GetPoint( i, point );
55       _TMesh::PointType ipoint;
56       ipoint[ 0 ] = point[ 0 ];
57       ipoint[ 1 ] = point[ 1 ];
58       ipoint[ 2 ] = point[ 2 ];
59       imesh->SetPoint( i, ipoint );
60
61     } // rof
62
63     // Copy cells
64     for( long i = 0; i < mesh->GetNumberOfCells( ); ++i )
65     {
66       auto cell = mesh->GetCell( i );
67       long nPoints = cell->GetNumberOfPoints( );
68       _TCellAutoPointer icell;
69       if( nPoints == 2 )
70       {
71         icell.TakeOwnership( new _TLine );
72         icell->SetPointId( 0, cell->GetPointId( 0 ) );
73         icell->SetPointId( 1, cell->GetPointId( 1 ) );
74       }
75       else if( nPoints == 3 )
76       {
77         icell.TakeOwnership( new _TTriangle );
78         icell->SetPointId( 0, cell->GetPointId( 0 ) );
79         icell->SetPointId( 1, cell->GetPointId( 1 ) );
80         icell->SetPointId( 2, cell->GetPointId( 2 ) );
81       }
82       else if( nPoints > 3 )
83       {
84         _TPolygon* polygon = new _TPolygon( );
85         for( long j = 0; j < nPoints; ++j )
86           polygon->AddPointId( cell->GetPointId( j ) );
87         icell.TakeOwnership( polygon );
88
89       } // fi
90       imesh->SetCell( imesh->GetNumberOfCells( ), icell );
91
92     } // rof
93
94     this->m_ITKObject = imesh;
95     this->Modified( );
96
97   } // fi
98 }
99 // -------------------------------------------------------------------------
100 cpPlugins::Interface::Mesh::
101 Mesh( )
102   : Superclass( )
103 {
104 }
105
106 // -------------------------------------------------------------------------
107 cpPlugins::Interface::Mesh::
108 ~Mesh( )
109 {
110 }
111
112 // eof - $RCSfile$