1 #include <cpPlugins/Mesh.h>
4 #include <itkLineCell.h>
5 #include <itkTriangleCell.h>
6 #include <itkPolygonCell.h>
9 #include <vtkPolyDataNormals.h>
10 #include <vtkPolyData.h>
11 #include <vtkPolyDataMapper.h>
12 #include <vtkQuadricLODActor.h>
13 #include <vtkStripper.h>
15 #define cpPlugins_Mesh_MAX_POLYS 65535
17 // -------------------------------------------------------------------------
18 void cpPlugins::Mesh::
19 SetITK( itk::LightObject* o )
21 this->Superclass::SetITK( o );
22 bool r = this->_ITK_2_VTK< itk::Mesh< float, 2 > >( o );
23 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 2 > >( o );
24 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< float, 3 > >( o );
25 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 3 > >( o );
28 // -------------------------------------------------------------------------
29 void cpPlugins::Mesh::
30 SetVTK( vtkObjectBase* o )
32 typedef itk::Mesh< double, 3 > _TMesh;
33 typedef _TMesh::CellType _TCell;
34 typedef _TCell::CellAutoPointer _TCellAutoPointer;
35 typedef itk::LineCell< _TCell > _TLine;
36 typedef itk::TriangleCell< _TCell > _TTriangle;
37 typedef itk::PolygonCell< _TCell > _TPolygon;
39 vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
42 this->m_ITKObject = NULL;
48 if( this->m_VTKObject.GetPointer( ) != mesh )
50 this->m_VTKObject = mesh;
53 _TMesh::Pointer imesh = _TMesh::New( );
55 for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
57 mesh->GetPoint( i, point );
58 _TMesh::PointType ipoint;
59 ipoint[ 0 ] = point[ 0 ];
60 ipoint[ 1 ] = point[ 1 ];
61 ipoint[ 2 ] = point[ 2 ];
62 imesh->SetPoint( i, ipoint );
67 vtkCellArray* arrays[ 4 ];
68 arrays[ 0 ] = mesh->GetLines( );
69 arrays[ 1 ] = mesh->GetPolys( );
70 arrays[ 2 ] = NULL; // TODO: mesh->GetStrips( );
71 arrays[ 3 ] = mesh->GetVerts( );
73 for( unsigned int c = 0; c < 4; c++ )
75 if( arrays[ c ] != NULL )
77 vtkSmartPointer< vtkIdList > ids =
78 vtkSmartPointer< vtkIdList >::New( );
79 arrays[ c ]->InitTraversal( );
80 while( arrays[ c ]->GetNextCell( ids ) == 1 )
82 long nPoints = ids->GetNumberOfIds( );
83 _TCellAutoPointer icell;
86 icell.TakeOwnership( new _TLine );
87 icell->SetPointId( 0, ids->GetId( 0 ) );
88 icell->SetPointId( 1, ids->GetId( 1 ) );
90 else if( nPoints == 3 )
92 icell.TakeOwnership( new _TTriangle );
93 icell->SetPointId( 0, ids->GetId( 0 ) );
94 icell->SetPointId( 1, ids->GetId( 1 ) );
95 icell->SetPointId( 2, ids->GetId( 2 ) );
97 else if( nPoints > 3 )
99 _TPolygon* polygon = new _TPolygon( );
100 for( long j = 0; j < nPoints; ++j )
101 polygon->AddPointId( ids->GetId( j ) );
102 icell.TakeOwnership( polygon );
105 imesh->SetCell( imesh->GetNumberOfCells( ), icell );
112 this->m_ITKObject = imesh;
118 // -------------------------------------------------------------------------
125 // -------------------------------------------------------------------------
131 // -------------------------------------------------------------------------
132 void cpPlugins::Mesh::
133 _CreateVTKActor( ) const
136 const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) );
139 unsigned long nPolys = mesh->GetNumberOfPolys( );
140 if( nPolys >= cpPlugins_Mesh_MAX_POLYS )
142 auto normals = vtkPolyDataNormals::New( );
143 auto stripper = vtkStripper::New( );
144 auto mapper = vtkPolyDataMapper::New( );
145 auto actor = vtkQuadricLODActor::New( );
148 mesh->GetScalarRange( r );
150 normals->SetInputData( mesh );
151 normals->SetFeatureAngle( 60.0 );
152 stripper->SetInputConnection( normals->GetOutputPort( ) );
153 mapper->SetInputConnection( stripper->GetOutputPort( ) );
154 mapper->UseLookupTableScalarRangeOff( );
155 mapper->SetScalarRange( r[ 0 ], r[ 1 ] );
156 actor->SetMapper( mapper );
157 actor->DeferLODConstructionOff( );
158 this->m_Actor = actor;
165 auto mapper = vtkPolyDataMapper::New( );
166 mapper->SetInputData( mesh );
167 auto actor = vtkActor::New( );
168 actor->SetMapper( mapper );
169 this->m_Actor = actor;