1 #include <cpPlugins/Mesh.h>
3 #define cpPlugins_Mesh_MAX_POLYS 65535
5 // -------------------------------------------------------------------------
6 void cpPlugins::Mesh::MeshActor::
7 SetMesh( vtkPolyData* mesh )
10 this->Stripper = NULL;
12 this->LODActor = NULL;
16 unsigned long nPolys = mesh->GetNumberOfPolys( );
17 if( nPolys >= cpPlugins_Mesh_MAX_POLYS )
19 this->Normals = vtkSmartPointer< vtkPolyDataNormals >::New( );
20 this->Stripper = vtkSmartPointer< vtkStripper >::New( );
21 this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
22 this->LODActor = vtkSmartPointer< vtkQuadricLODActor >::New( );
25 mesh->GetScalarRange( r );
27 this->Normals->SetInputData( mesh );
28 this->Normals->SetFeatureAngle( 60.0 );
29 this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
30 this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
31 this->Mapper->UseLookupTableScalarRangeOff( );
32 this->Mapper->SetScalarRange( r[ 0 ], r[ 1 ] );
33 this->LODActor->SetMapper( this->Mapper );
34 this->LODActor->DeferLODConstructionOff( );
38 this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
39 this->Actor = vtkSmartPointer< vtkActor >::New( );
40 this->Mapper->SetInputData( mesh );
41 this->Actor->SetMapper( this->Mapper );
48 // -------------------------------------------------------------------------
49 void cpPlugins::Mesh::
50 SetITK( itk::LightObject* o )
52 this->Superclass::SetITK( o );
53 bool r = this->_ITK_2_VTK< itk::Mesh< float, 2 > >( o );
54 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 2 > >( o );
55 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< float, 3 > >( o );
56 if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 3 > >( o );
59 // -------------------------------------------------------------------------
60 void cpPlugins::Mesh::
61 SetVTK( vtkObjectBase* o )
63 typedef itk::Mesh< double, 3 > _TMesh;
64 typedef _TMesh::CellType _TCell;
65 typedef _TCell::CellAutoPointer _TCellAutoPointer;
66 typedef itk::LineCell< _TCell > _TLine;
67 typedef itk::TriangleCell< _TCell > _TTriangle;
68 typedef itk::PolygonCell< _TCell > _TPolygon;
70 vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
73 this->m_ITKObject = NULL;
79 if( this->m_VTKObject.GetPointer( ) != mesh )
81 this->m_VTKObject = mesh;
84 _TMesh::Pointer imesh = _TMesh::New( );
86 for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
88 mesh->GetPoint( i, point );
89 _TMesh::PointType ipoint;
90 ipoint[ 0 ] = point[ 0 ];
91 ipoint[ 1 ] = point[ 1 ];
92 ipoint[ 2 ] = point[ 2 ];
93 imesh->SetPoint( i, ipoint );
98 vtkCellArray* arrays[ 4 ];
99 arrays[ 0 ] = mesh->GetLines( );
100 arrays[ 1 ] = mesh->GetPolys( );
101 arrays[ 2 ] = NULL; // TODO: mesh->GetStrips( );
102 arrays[ 3 ] = mesh->GetVerts( );
104 for( unsigned int c = 0; c < 4; c++ )
106 if( arrays[ c ] != NULL )
108 vtkSmartPointer< vtkIdList > ids =
109 vtkSmartPointer< vtkIdList >::New( );
110 arrays[ c ]->InitTraversal( );
111 while( arrays[ c ]->GetNextCell( ids ) == 1 )
113 long nPoints = ids->GetNumberOfIds( );
114 _TCellAutoPointer icell;
117 icell.TakeOwnership( new _TLine );
118 icell->SetPointId( 0, ids->GetId( 0 ) );
119 icell->SetPointId( 1, ids->GetId( 1 ) );
121 else if( nPoints == 3 )
123 icell.TakeOwnership( new _TTriangle );
124 icell->SetPointId( 0, ids->GetId( 0 ) );
125 icell->SetPointId( 1, ids->GetId( 1 ) );
126 icell->SetPointId( 2, ids->GetId( 2 ) );
128 else if( nPoints > 3 )
130 _TPolygon* polygon = new _TPolygon( );
131 for( long j = 0; j < nPoints; ++j )
132 polygon->AddPointId( ids->GetId( j ) );
133 icell.TakeOwnership( polygon );
136 imesh->SetCell( imesh->GetNumberOfCells( ), icell );
143 this->m_ITKObject = imesh;
149 // -------------------------------------------------------------------------
156 // -------------------------------------------------------------------------
162 // -------------------------------------------------------------------------
163 void cpPlugins::Mesh::
164 _CreateVTKActor( ) const
166 this->m_MeshActor.SetMesh(
167 const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) )
169 if( this->m_MeshActor.LODActor.GetPointer( ) != NULL )
170 this->m_Actor = this->m_MeshActor.LODActor.GetPointer( );
171 else if( this->m_MeshActor.Actor.GetPointer( ) != NULL )
172 this->m_Actor = this->m_MeshActor.Actor.GetPointer( );
174 this->m_Actor = NULL;
178 const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) );
181 unsigned long nPolys = mesh->GetNumberOfPolys( );
182 if( nPolys >= cpPlugins_Mesh_MAX_POLYS )
184 auto normals = vtkPolyDataNormals::New( );
185 auto stripper = vtkStripper::New( );
186 auto mapper = vtkPolyDataMapper::New( );
187 auto actor = vtkQuadricLODActor::New( );
190 mesh->GetScalarRange( r );
192 normals->SetInputData( mesh );
193 normals->SetFeatureAngle( 60.0 );
194 stripper->SetInputConnection( normals->GetOutputPort( ) );
195 mapper->SetInputConnection( stripper->GetOutputPort( ) );
196 mapper->UseLookupTableScalarRangeOff( );
197 mapper->SetScalarRange( r[ 0 ], r[ 1 ] );
198 actor->SetMapper( mapper );
199 actor->DeferLODConstructionOff( );
200 this->m_Actor = actor;
207 auto mapper = vtkPolyDataMapper::New( );
208 mapper->SetInputData( mesh );
209 auto actor = vtkActor::New( );
210 actor->SetMapper( mapper );
211 this->m_Actor = actor;