]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/DataObjects/Mesh.cxx
94c4087922b6a84e3b10f7091399cb6ec4d60c11
[cpPlugins.git] / lib / cpPlugins / DataObjects / Mesh.cxx
1 #include <cpPlugins/DataObjects/Mesh.h>
2 #include <vtkPolyData.h>
3
4 // -------------------------------------------------------------------------
5 void cpPlugins::DataObjects::Mesh::
6 SetITK( itk::LightObject* o )
7 {
8   this->Superclass::SetITK( o );
9   bool s = false;
10   cpPlugins_Demangle_Meshes_Dims( o, _ITK_2_VTK );
11 }
12
13 // -------------------------------------------------------------------------
14 void cpPlugins::DataObjects::Mesh::
15 SetVTK( vtkObjectBase* o )
16 {
17   typedef itk::Mesh< double, 3 >      _TMesh;
18   typedef _TMesh::CellType            _TCell;
19   typedef _TCell::CellAutoPointer     _TCellAutoPointer;
20   typedef itk::LineCell< _TCell >     _TLine;
21   typedef itk::TriangleCell< _TCell > _TTriangle;
22   typedef itk::PolygonCell< _TCell >  _TPolygon;
23
24   vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
25   if( mesh == NULL )
26   {
27     this->m_ITK = NULL;
28     this->Modified( );
29     return;
30
31   } // fi
32
33   if( this->m_VTK.GetPointer( ) != mesh )
34   {
35     this->m_VTK = mesh;
36
37     // Copy points
38     _TMesh::Pointer imesh = _TMesh::New( );
39     double point[ 3 ];
40     for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
41     {
42       mesh->GetPoint( i, point );
43       _TMesh::PointType ipoint;
44       ipoint[ 0 ] = point[ 0 ];
45       ipoint[ 1 ] = point[ 1 ];
46       ipoint[ 2 ] = point[ 2 ];
47       imesh->SetPoint( i, ipoint );
48
49     } // rof
50
51     // Copy cells
52     vtkCellArray* arrays[ 4 ];
53     arrays[ 0 ] = mesh->GetLines( );
54     arrays[ 1 ] = mesh->GetPolys( );
55     arrays[ 2 ] = NULL; // TODO: mesh->GetStrips( );
56     arrays[ 3 ] = mesh->GetVerts( );
57
58     for( unsigned int c = 0; c < 4; c++ )
59     {
60       if( arrays[ c ] != NULL )
61       {
62         vtkSmartPointer< vtkIdList > ids =
63           vtkSmartPointer< vtkIdList >::New( );
64         arrays[ c ]->InitTraversal( );
65         while( arrays[ c ]->GetNextCell( ids ) == 1 )
66         {
67           long nPoints = ids->GetNumberOfIds( );
68           _TCellAutoPointer icell;
69           if( nPoints == 2 )
70           {
71             icell.TakeOwnership( new _TLine );
72             icell->SetPointId( 0, ids->GetId( 0 ) );
73             icell->SetPointId( 1, ids->GetId( 1 ) );
74           }
75           else if( nPoints == 3 )
76           {
77             icell.TakeOwnership( new _TTriangle );
78             icell->SetPointId( 0, ids->GetId( 0 ) );
79             icell->SetPointId( 1, ids->GetId( 1 ) );
80             icell->SetPointId( 2, ids->GetId( 2 ) );
81           }
82           else if( nPoints > 3 )
83           {
84             _TPolygon* polygon = new _TPolygon( );
85             for( long j = 0; j < nPoints; ++j )
86               polygon->AddPointId( ids->GetId( j ) );
87             icell.TakeOwnership( polygon );
88
89           } // fi
90           imesh->SetCell( imesh->GetNumberOfCells( ), icell );
91
92         } // elihw
93
94       } // fi
95
96     } // rof
97     this->m_ITK = imesh;
98     this->Modified( );
99
100   } // fi
101 }
102
103 // -------------------------------------------------------------------------
104 cpPlugins::DataObjects::Mesh::
105 Mesh( )
106   : Superclass( )
107 {
108 }
109
110 // -------------------------------------------------------------------------
111 cpPlugins::DataObjects::Mesh::
112 ~Mesh( )
113 {
114 }
115
116 // eof - $RCSfile$