1 #include <cpPlugins/Mesh.h>
4 #include <itkLineCell.h>
5 #include <itkTriangleCell.h>
6 #include <itkPolygonCell.h>
8 #include <vtkPolyData.h>
10 // -------------------------------------------------------------------------
11 void cpPlugins::Mesh::
12 SetITK( itk::LightObject* o )
14 this->Superclass::SetITK( o );
15 bool r = this->_ITK_2_VTK< itk::Mesh< float, 2 > >( o );
16 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 2 > >( o );
17 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< float, 3 > >( o );
18 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 3 > >( o );
21 // -------------------------------------------------------------------------
22 void cpPlugins::Mesh::
23 SetVTK( vtkObjectBase* o )
25 typedef itk::Mesh< double, 3 > _TMesh;
26 typedef _TMesh::CellType _TCell;
27 typedef _TCell::CellAutoPointer _TCellAutoPointer;
28 typedef itk::LineCell< _TCell > _TLine;
29 typedef itk::TriangleCell< _TCell > _TTriangle;
30 typedef itk::PolygonCell< _TCell > _TPolygon;
32 vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
35 this->m_ITKObject = NULL;
41 if( this->m_VTKObject.GetPointer( ) != mesh )
43 this->m_VTKObject = mesh;
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 );
105 this->m_ITKObject = imesh;
111 // -------------------------------------------------------------------------
118 // -------------------------------------------------------------------------
124 // -------------------------------------------------------------------------
125 void cpPlugins::Mesh::
126 _CreateVTKActor( ) const
129 const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) );
132 std::cout << "Mesh: " << mesh << std::endl;
134 vtkImageSliceMapper* mapper = vtkImageSliceMapper::New( );
135 vtkImageActor* actor = vtkImageActor::New( );
136 mapper->SetInputData( image );
137 actor->SetMapper( mapper );
138 this->m_Actor = actor;