1 #include <cpPlugins/DataObjects/Mesh.h>
2 #include <cpPlugins/DataObjects/Mesh_Demanglers.h>
5 #include <itkLineCell.h>
6 #include <itkTriangleCell.h>
7 #include <itkPolygonCell.h>
8 #include <vtkPolyData.h>
10 // -------------------------------------------------------------------------
11 void cpPlugins::DataObjects::Mesh::
12 SetITK( itk::LightObject* o )
14 this->Superclass::SetITK( o );
15 cpPlugins_Demangle_Mesh_Meshes( o, _ITK_2_VTK, 2, )
16 cpPlugins_Demangle_Mesh_Meshes( o, _ITK_2_VTK, 3, )
24 // -------------------------------------------------------------------------
25 void cpPlugins::DataObjects::Mesh::
26 SetVTK( vtkObjectBase* o )
28 typedef itk::Mesh< double, 3 > _TMesh;
29 typedef _TMesh::CellType _TCell;
30 typedef _TCell::CellAutoPointer _TCellAutoPointer;
31 typedef itk::LineCell< _TCell > _TLine;
32 typedef itk::TriangleCell< _TCell > _TTriangle;
33 typedef itk::PolygonCell< _TCell > _TPolygon;
35 vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
44 if( this->m_VTK.GetPointer( ) != mesh )
49 _TMesh::Pointer imesh = _TMesh::New( );
51 for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
53 mesh->GetPoint( i, point );
54 _TMesh::PointType ipoint;
55 ipoint[ 0 ] = point[ 0 ];
56 ipoint[ 1 ] = point[ 1 ];
57 ipoint[ 2 ] = point[ 2 ];
58 imesh->SetPoint( i, ipoint );
63 vtkCellArray* arrays[ 4 ];
64 arrays[ 0 ] = mesh->GetLines( );
65 arrays[ 1 ] = mesh->GetPolys( );
66 arrays[ 2 ] = NULL; // TODO: mesh->GetStrips( );
67 arrays[ 3 ] = mesh->GetVerts( );
69 for( unsigned int c = 0; c < 4; c++ )
71 if( arrays[ c ] != NULL )
73 vtkSmartPointer< vtkIdList > ids =
74 vtkSmartPointer< vtkIdList >::New( );
75 arrays[ c ]->InitTraversal( );
76 while( arrays[ c ]->GetNextCell( ids ) == 1 )
78 long nPoints = ids->GetNumberOfIds( );
79 _TCellAutoPointer icell;
82 icell.TakeOwnership( new _TLine );
83 icell->SetPointId( 0, ids->GetId( 0 ) );
84 icell->SetPointId( 1, ids->GetId( 1 ) );
86 else if( nPoints == 3 )
88 icell.TakeOwnership( new _TTriangle );
89 icell->SetPointId( 0, ids->GetId( 0 ) );
90 icell->SetPointId( 1, ids->GetId( 1 ) );
91 icell->SetPointId( 2, ids->GetId( 2 ) );
93 else if( nPoints > 3 )
95 _TPolygon* polygon = new _TPolygon( );
96 for( long j = 0; j < nPoints; ++j )
97 polygon->AddPointId( ids->GetId( j ) );
98 icell.TakeOwnership( polygon );
101 imesh->SetCell( imesh->GetNumberOfCells( ), icell );
114 // -------------------------------------------------------------------------
115 cpPlugins::DataObjects::Mesh::
121 // -------------------------------------------------------------------------
122 cpPlugins::DataObjects::Mesh::
127 // -------------------------------------------------------------------------
128 template< class _TMesh >
129 void cpPlugins::DataObjects::Mesh::
130 _ITK_2_VTK( _TMesh* mesh )
132 long numPoints = mesh->GetNumberOfPoints( );
136 vtkSmartPointer< vtkPoints > vpoints =
137 vtkSmartPointer< vtkPoints >::New( );
138 vpoints->SetNumberOfPoints( numPoints );
139 auto points = mesh->GetPoints( );
143 std::map< vtkIdType, long > IndexMap;
144 for( auto i = points->Begin( ); i != points->End( ); ++i, VTKId++ )
146 IndexMap[ VTKId ] = i->Index( );
147 if( _TMesh::PointDimension == 2 )
150 i->Value( )[ 0 ], i->Value( )[ 1 ], 0
152 else if( _TMesh::PointDimension == 3 )
155 i->Value( )[ 0 ], i->Value( )[ 1 ], i->Value( )[ 2 ]
161 vtkSmartPointer< vtkCellArray > vcells =
162 vtkSmartPointer< vtkCellArray >::New( );
163 auto cells = mesh->GetCells( );
164 for( auto j = cells->Begin( ); j != cells->End( ); ++j )
166 auto cell = j->Value( );
167 vcells->InsertNextCell( cell->GetNumberOfPoints( ) );
168 for( auto k = cell->PointIdsBegin( ); k != cell->PointIdsEnd( ); ++k )
169 vcells->InsertCellPoint( IndexMap[ *k ] );
173 // Final assignations
174 vtkSmartPointer< vtkPolyData > vmesh =
175 vtkSmartPointer< vtkPolyData >::New( );
176 vmesh->SetPoints( vpoints );
177 vmesh->SetPolys( vcells );