]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Mesh.cxx
Code cleaning
[cpPlugins.git] / lib / cpPlugins / Mesh.cxx
1 #include <cpPlugins/Mesh.h>
2
3 #define cpPlugins_Mesh_MAX_POLYS 65535
4
5 // -------------------------------------------------------------------------
6 void cpPlugins::Mesh::MeshActor::
7 SetMesh( vtkPolyData* mesh )
8 {
9   this->Normals = NULL;
10   this->Stripper = NULL;
11   this->Mapper = NULL;
12   this->LODActor = NULL;
13   this->Actor = NULL;
14   if( mesh != NULL )
15   {
16     unsigned long nPolys = mesh->GetNumberOfPolys( );
17     if( nPolys >= cpPlugins_Mesh_MAX_POLYS )
18     {
19       this->Normals = vtkSmartPointer< vtkPolyDataNormals >::New( );
20       this->Stripper = vtkSmartPointer< vtkStripper >::New( );
21       this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
22       this->LODActor = vtkSmartPointer< vtkQuadricLODActor >::New( );
23
24       double r[ 2 ];
25       mesh->GetScalarRange( r );
26
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( );
35     }
36     else
37     {
38       this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
39       this->Actor = vtkSmartPointer< vtkActor >::New( );
40       this->Mapper->SetInputData( mesh );
41       this->Actor->SetMapper( this->Mapper );
42
43     } // fi
44
45   } // fi
46 }
47
48 // -------------------------------------------------------------------------
49 void cpPlugins::Mesh::
50 SetITK( itk::LightObject* o )
51 {
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 );
57 }
58
59 // -------------------------------------------------------------------------
60 void cpPlugins::Mesh::
61 SetVTK( vtkObjectBase* o )
62 {
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;
69
70   vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
71   if( mesh == NULL )
72   {
73     this->m_ITKObject = NULL;
74     this->Modified( );
75     return;
76
77   } // fi
78
79   if( this->m_VTKObject.GetPointer( ) != mesh )
80   {
81     this->m_VTKObject = mesh;
82
83     // Copy points
84     _TMesh::Pointer imesh = _TMesh::New( );
85     double point[ 3 ];
86     for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
87     {
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 );
94
95     } // rof
96
97     // Copy cells
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( );
103
104     for( unsigned int c = 0; c < 4; c++ )
105     {
106       if( arrays[ c ] != NULL )
107       {
108         vtkSmartPointer< vtkIdList > ids =
109           vtkSmartPointer< vtkIdList >::New( );
110         arrays[ c ]->InitTraversal( );
111         while( arrays[ c ]->GetNextCell( ids ) == 1 )
112         {
113           long nPoints = ids->GetNumberOfIds( );
114           _TCellAutoPointer icell;
115           if( nPoints == 2 )
116           {
117             icell.TakeOwnership( new _TLine );
118             icell->SetPointId( 0, ids->GetId( 0 ) );
119             icell->SetPointId( 1, ids->GetId( 1 ) );
120           }
121           else if( nPoints == 3 )
122           {
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 ) );
127           }
128           else if( nPoints > 3 )
129           {
130             _TPolygon* polygon = new _TPolygon( );
131             for( long j = 0; j < nPoints; ++j )
132               polygon->AddPointId( ids->GetId( j ) );
133             icell.TakeOwnership( polygon );
134
135           } // fi
136           imesh->SetCell( imesh->GetNumberOfCells( ), icell );
137
138         } // elihw
139
140       } // fi
141
142     } // rof
143     this->m_ITKObject = imesh;
144     this->Modified( );
145
146   } // fi
147 }
148
149 // -------------------------------------------------------------------------
150 cpPlugins::Mesh::
151 Mesh( )
152   : Superclass( )
153 {
154 }
155
156 // -------------------------------------------------------------------------
157 cpPlugins::Mesh::
158 ~Mesh( )
159 {
160 }
161
162 // -------------------------------------------------------------------------
163 void cpPlugins::Mesh::
164 _CreateVTKActor( ) const
165 {
166   this->m_MeshActor.SetMesh(
167     const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) )
168     );
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( );
173   else
174     this->m_Actor = NULL;
175   
176   /* TODO
177   vtkPolyData* mesh =
178     const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) );
179   if( mesh != NULL )
180   {
181     unsigned long nPolys = mesh->GetNumberOfPolys( );
182     if( nPolys >= cpPlugins_Mesh_MAX_POLYS )
183     {
184       auto normals = vtkPolyDataNormals::New( );
185       auto stripper = vtkStripper::New( );
186       auto mapper = vtkPolyDataMapper::New( );
187       auto actor = vtkQuadricLODActor::New( );
188
189       double r[ 2 ];
190       mesh->GetScalarRange( r );
191
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;
201       mapper->Delete( );
202       stripper->Delete( );
203       normals->Delete( );
204     }
205     else
206     {
207       auto mapper = vtkPolyDataMapper::New( );
208       mapper->SetInputData( mesh );
209       auto actor = vtkActor::New( );
210       actor->SetMapper( mapper );
211       this->m_Actor = actor;
212       mapper->Delete( );
213
214     } // fi
215
216   } // fi
217   */
218 }
219
220 // eof - $RCSfile$