]> Creatis software - cpPlugins.git/blob - lib/cpInstances/DataObjects/Mesh.cxx
yet another refactoring
[cpPlugins.git] / lib / cpInstances / DataObjects / Mesh.cxx
1 #include <cpInstances/DataObjects/Mesh.h>
2
3 #include <itkMesh.h>
4 #include <itkLineCell.h>
5 #include <itkTriangleCell.h>
6 #include <itkPolygonCell.h>
7 #include <vtkPolyData.h>
8
9 // -------------------------------------------------------------------------
10 void cpInstances::DataObjects::Mesh::
11 SetITK( itk::LightObject* o )
12 {
13   this->Superclass::SetITK( o );
14   cpPlugins_Demangle_Mesh_AllMeshes_1( o, _ITK_2_VTK )
15   {
16     this->m_VTK = NULL;
17   }
18   this->Modified( );
19 }
20
21 // -------------------------------------------------------------------------
22 void cpInstances::DataObjects::Mesh::
23 SetVTK( vtkObjectBase* o )
24 {
25   vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
26   if( mesh == NULL )
27   {
28     this->m_ITK = NULL;
29     this->Modified( );
30     return;
31
32   } // fi
33
34   if( this->m_VTK.GetPointer( ) != mesh )
35   {
36     this->m_VTK = mesh;
37 #ifdef cpPlugins_PROCESS_DIMS_3
38     typedef itk::Mesh< double, 3 >      _TMesh;
39     typedef _TMesh::CellType            _TCell;
40     typedef _TCell::CellAutoPointer     _TCellAutoPointer;
41     typedef itk::LineCell< _TCell >     _TLine;
42     typedef itk::TriangleCell< _TCell > _TTriangle;
43     typedef itk::PolygonCell< _TCell >  _TPolygon;
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     vtkCellArray* arrays[ 4 ];
61     arrays[ 0 ] = mesh->GetLines( );
62     arrays[ 1 ] = mesh->GetPolys( );
63     arrays[ 2 ] = NULL; // TODO: mesh->GetStrips( );
64     arrays[ 3 ] = mesh->GetVerts( );
65
66     for( unsigned int c = 0; c < 4; c++ )
67     {
68       if( arrays[ c ] != NULL )
69       {
70         vtkSmartPointer< vtkIdList > ids =
71           vtkSmartPointer< vtkIdList >::New( );
72         arrays[ c ]->InitTraversal( );
73         while( arrays[ c ]->GetNextCell( ids ) == 1 )
74         {
75           long nPoints = ids->GetNumberOfIds( );
76           _TCellAutoPointer icell;
77           if( nPoints == 2 )
78           {
79             icell.TakeOwnership( new _TLine );
80             icell->SetPointId( 0, ids->GetId( 0 ) );
81             icell->SetPointId( 1, ids->GetId( 1 ) );
82           }
83           else if( nPoints == 3 )
84           {
85             icell.TakeOwnership( new _TTriangle );
86             icell->SetPointId( 0, ids->GetId( 0 ) );
87             icell->SetPointId( 1, ids->GetId( 1 ) );
88             icell->SetPointId( 2, ids->GetId( 2 ) );
89           }
90           else if( nPoints > 3 )
91           {
92             _TPolygon* polygon = new _TPolygon( );
93             for( long j = 0; j < nPoints; ++j )
94               polygon->AddPointId( ids->GetId( j ) );
95             icell.TakeOwnership( polygon );
96
97           } // fi
98           imesh->SetCell( imesh->GetNumberOfCells( ), icell );
99
100         } // elihw
101
102       } // fi
103
104     } // rof
105     this->m_ITK = imesh;
106 #endif // cpPlugins_PROCESS_DIMS_3
107     this->Modified( );
108
109   } // fi
110 }
111
112 // -------------------------------------------------------------------------
113 cpInstances::DataObjects::Mesh::
114 Mesh( )
115   : Superclass( )
116 {
117 }
118
119 // -------------------------------------------------------------------------
120 cpInstances::DataObjects::Mesh::
121 ~Mesh( )
122 {
123 }
124
125 // -------------------------------------------------------------------------
126 template< class _TMesh >
127 void cpInstances::DataObjects::Mesh::
128 _ITK_2_VTK( _TMesh* mesh )
129 {
130   long numPoints = mesh->GetNumberOfPoints( );
131   if( numPoints == 0 )
132     return;
133
134   vtkSmartPointer< vtkPoints > vpoints =
135     vtkSmartPointer< vtkPoints >::New( );
136   vpoints->SetNumberOfPoints( numPoints );
137   auto points = mesh->GetPoints( );
138
139   // Copy points
140   vtkIdType VTKId = 0;
141   std::map< vtkIdType, long > IndexMap;
142   for( auto i = points->Begin( ); i != points->End( ); ++i, VTKId++ )
143   {
144     IndexMap[ VTKId ] = i->Index( );
145     if( _TMesh::PointDimension == 2 )
146       vpoints->SetPoint(
147         VTKId,
148         i->Value( )[ 0 ], i->Value( )[ 1 ], 0
149         );
150     else if( _TMesh::PointDimension == 3 )
151       vpoints->SetPoint(
152         VTKId,
153         i->Value( )[ 0 ], i->Value( )[ 1 ], i->Value( )[ 2 ]
154         );
155
156   } // rof
157
158   // Copy cells
159   vtkSmartPointer< vtkCellArray > vcells =
160     vtkSmartPointer< vtkCellArray >::New( );
161   auto cells = mesh->GetCells( );
162   for( auto j = cells->Begin( ); j != cells->End( ); ++j )
163   {
164     auto cell = j->Value( );
165     vcells->InsertNextCell( cell->GetNumberOfPoints( ) );
166     for( auto k = cell->PointIdsBegin( ); k != cell->PointIdsEnd( ); ++k )
167       vcells->InsertCellPoint( IndexMap[ *k ] );
168
169   } // rof
170
171   // Final assignations
172   vtkSmartPointer< vtkPolyData > vmesh =
173     vtkSmartPointer< vtkPolyData >::New( );
174   vmesh->SetPoints( vpoints );
175   vmesh->SetPolys( vcells );
176   this->m_VTK = vmesh;
177   this->Modified( );
178 }
179
180 // eof - $RCSfile$