1 #include <cpPlugins/Mesh.h>
4 #include <itkLineCell.h>
5 #include <itkTriangleCell.h>
6 #include <itkPolygonCell.h>
8 #define cpPlugins_Mesh_MAX_POLYS 65535
10 // -------------------------------------------------------------------------
11 void cpPlugins::Mesh::MeshActor::
12 SetMesh( vtkPolyData* mesh )
15 this->Stripper = NULL;
17 this->LODActor = NULL;
21 unsigned long nPolys = mesh->GetNumberOfPolys( );
22 if( nPolys >= cpPlugins_Mesh_MAX_POLYS )
24 this->Normals = vtkSmartPointer< vtkPolyDataNormals >::New( );
25 this->Stripper = vtkSmartPointer< vtkStripper >::New( );
26 this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
27 this->LODActor = vtkSmartPointer< vtkQuadricLODActor >::New( );
30 mesh->GetScalarRange( r );
32 this->Normals->SetInputData( mesh );
33 this->Normals->SetFeatureAngle( 60.0 );
34 this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
35 this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
36 this->Mapper->UseLookupTableScalarRangeOff( );
37 this->Mapper->SetScalarRange( r[ 0 ], r[ 1 ] );
38 this->LODActor->SetMapper( this->Mapper );
39 this->LODActor->DeferLODConstructionOff( );
43 this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
44 this->Actor = vtkSmartPointer< vtkActor >::New( );
45 this->Mapper->SetInputData( mesh );
46 this->Actor->SetMapper( this->Mapper );
53 // -------------------------------------------------------------------------
54 void cpPlugins::Mesh::
55 SetITK( itk::LightObject* o )
57 this->Superclass::SetITK( o );
58 bool r = this->_ITK_2_VTK< itk::Mesh< float, 2 > >( o );
59 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 2 > >( o );
60 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< float, 3 > >( o );
61 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 3 > >( o );
64 // -------------------------------------------------------------------------
65 void cpPlugins::Mesh::
66 SetVTK( vtkObjectBase* o )
68 typedef itk::Mesh< double, 3 > _TMesh;
69 typedef _TMesh::CellType _TCell;
70 typedef _TCell::CellAutoPointer _TCellAutoPointer;
71 typedef itk::LineCell< _TCell > _TLine;
72 typedef itk::TriangleCell< _TCell > _TTriangle;
73 typedef itk::PolygonCell< _TCell > _TPolygon;
75 vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
78 this->m_ITKObject = NULL;
84 if( this->m_VTKObject.GetPointer( ) != mesh )
86 this->m_VTKObject = mesh;
89 _TMesh::Pointer imesh = _TMesh::New( );
91 for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
93 mesh->GetPoint( i, point );
94 _TMesh::PointType ipoint;
95 ipoint[ 0 ] = point[ 0 ];
96 ipoint[ 1 ] = point[ 1 ];
97 ipoint[ 2 ] = point[ 2 ];
98 imesh->SetPoint( i, ipoint );
103 vtkCellArray* arrays[ 4 ];
104 arrays[ 0 ] = mesh->GetLines( );
105 arrays[ 1 ] = mesh->GetPolys( );
106 arrays[ 2 ] = NULL; // TODO: mesh->GetStrips( );
107 arrays[ 3 ] = mesh->GetVerts( );
109 for( unsigned int c = 0; c < 4; c++ )
111 if( arrays[ c ] != NULL )
113 vtkSmartPointer< vtkIdList > ids =
114 vtkSmartPointer< vtkIdList >::New( );
115 arrays[ c ]->InitTraversal( );
116 while( arrays[ c ]->GetNextCell( ids ) == 1 )
118 long nPoints = ids->GetNumberOfIds( );
119 _TCellAutoPointer icell;
122 icell.TakeOwnership( new _TLine );
123 icell->SetPointId( 0, ids->GetId( 0 ) );
124 icell->SetPointId( 1, ids->GetId( 1 ) );
126 else if( nPoints == 3 )
128 icell.TakeOwnership( new _TTriangle );
129 icell->SetPointId( 0, ids->GetId( 0 ) );
130 icell->SetPointId( 1, ids->GetId( 1 ) );
131 icell->SetPointId( 2, ids->GetId( 2 ) );
133 else if( nPoints > 3 )
135 _TPolygon* polygon = new _TPolygon( );
136 for( long j = 0; j < nPoints; ++j )
137 polygon->AddPointId( ids->GetId( j ) );
138 icell.TakeOwnership( polygon );
141 imesh->SetCell( imesh->GetNumberOfCells( ), icell );
148 this->m_ITKObject = imesh;
154 // -------------------------------------------------------------------------
161 // -------------------------------------------------------------------------
167 // -------------------------------------------------------------------------
168 void cpPlugins::Mesh::
169 _CreateVTKActor( ) const
171 this->m_MeshActor.SetMesh(
172 const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) )
174 if( this->m_MeshActor.LODActor.GetPointer( ) != NULL )
175 this->m_Actor = this->m_MeshActor.LODActor.GetPointer( );
176 else if( this->m_MeshActor.Actor.GetPointer( ) != NULL )
177 this->m_Actor = this->m_MeshActor.Actor.GetPointer( );
179 this->m_Actor = NULL;
183 const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) );
186 unsigned long nPolys = mesh->GetNumberOfPolys( );
187 if( nPolys >= cpPlugins_Mesh_MAX_POLYS )
189 auto normals = vtkPolyDataNormals::New( );
190 auto stripper = vtkStripper::New( );
191 auto mapper = vtkPolyDataMapper::New( );
192 auto actor = vtkQuadricLODActor::New( );
195 mesh->GetScalarRange( r );
197 normals->SetInputData( mesh );
198 normals->SetFeatureAngle( 60.0 );
199 stripper->SetInputConnection( normals->GetOutputPort( ) );
200 mapper->SetInputConnection( stripper->GetOutputPort( ) );
201 mapper->UseLookupTableScalarRangeOff( );
202 mapper->SetScalarRange( r[ 0 ], r[ 1 ] );
203 actor->SetMapper( mapper );
204 actor->DeferLODConstructionOff( );
205 this->m_Actor = actor;
212 auto mapper = vtkPolyDataMapper::New( );
213 mapper->SetInputData( mesh );
214 auto actor = vtkActor::New( );
215 actor->SetMapper( mapper );
216 this->m_Actor = actor;