1 #include <cpInstances/DataObjects/Mesh.h>
4 #include <itkLineCell.h>
5 #include <itkTriangleCell.h>
6 #include <itkPolygonCell.h>
7 #include <vtkPolyData.h>
9 // -------------------------------------------------------------------------
10 void cpInstances::DataObjects::Mesh::
11 SetITK( itk::LightObject* o )
13 this->Superclass::SetITK( o );
14 cpPlugins_Demangle_Mesh_AllMeshes_1( o, _ITK_2_VTK )
21 // -------------------------------------------------------------------------
22 void cpInstances::DataObjects::Mesh::
23 SetVTK( vtkObjectBase* o )
25 vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
34 if( this->m_VTK.GetPointer( ) != 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;
46 _TMesh::Pointer imesh = _TMesh::New( );
48 for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
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 );
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( );
66 for( unsigned int c = 0; c < 4; c++ )
68 if( arrays[ c ] != NULL )
70 vtkSmartPointer< vtkIdList > ids =
71 vtkSmartPointer< vtkIdList >::New( );
72 arrays[ c ]->InitTraversal( );
73 while( arrays[ c ]->GetNextCell( ids ) == 1 )
75 long nPoints = ids->GetNumberOfIds( );
76 _TCellAutoPointer icell;
79 icell.TakeOwnership( new _TLine );
80 icell->SetPointId( 0, ids->GetId( 0 ) );
81 icell->SetPointId( 1, ids->GetId( 1 ) );
83 else if( nPoints == 3 )
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 ) );
90 else if( nPoints > 3 )
92 _TPolygon* polygon = new _TPolygon( );
93 for( long j = 0; j < nPoints; ++j )
94 polygon->AddPointId( ids->GetId( j ) );
95 icell.TakeOwnership( polygon );
98 imesh->SetCell( imesh->GetNumberOfCells( ), icell );
106 #endif // cpPlugins_PROCESS_DIMS_3
112 // -------------------------------------------------------------------------
113 cpInstances::DataObjects::Mesh::
119 // -------------------------------------------------------------------------
120 cpInstances::DataObjects::Mesh::
125 // -------------------------------------------------------------------------
126 template< class _TMesh >
127 void cpInstances::DataObjects::Mesh::
128 _ITK_2_VTK( _TMesh* mesh )
130 long numPoints = mesh->GetNumberOfPoints( );
134 vtkSmartPointer< vtkPoints > vpoints =
135 vtkSmartPointer< vtkPoints >::New( );
136 vpoints->SetNumberOfPoints( numPoints );
137 auto points = mesh->GetPoints( );
141 std::map< vtkIdType, long > IndexMap;
142 for( auto i = points->Begin( ); i != points->End( ); ++i, VTKId++ )
144 IndexMap[ VTKId ] = i->Index( );
145 if( _TMesh::PointDimension == 2 )
148 i->Value( )[ 0 ], i->Value( )[ 1 ], 0
150 else if( _TMesh::PointDimension == 3 )
153 i->Value( )[ 0 ], i->Value( )[ 1 ], i->Value( )[ 2 ]
159 vtkSmartPointer< vtkCellArray > vcells =
160 vtkSmartPointer< vtkCellArray >::New( );
161 auto cells = mesh->GetCells( );
162 for( auto j = cells->Begin( ); j != cells->End( ); ++j )
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 ] );
171 // Final assignations
172 vtkSmartPointer< vtkPolyData > vmesh =
173 vtkSmartPointer< vtkPolyData >::New( );
174 vmesh->SetPoints( vpoints );
175 vmesh->SetPolys( vcells );