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