]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Mesh.cxx
f06aae0348568315170abdf49d668a8ba5a60980
[cpPlugins.git] / lib / cpPlugins / Mesh.cxx
1 #include <cpPlugins/Mesh.h>
2
3 #include <itkMesh.h>
4 #include <itkLineCell.h>
5 #include <itkTriangleCell.h>
6 #include <itkPolygonCell.h>
7
8 #define cpPlugins_Mesh_MAX_POLYS 65535
9
10 // -------------------------------------------------------------------------
11 void cpPlugins::Mesh::MeshActor::
12 SetMesh( vtkPolyData* mesh )
13 {
14   this->Normals = NULL;
15   this->Stripper = NULL;
16   this->Mapper = NULL;
17   this->LODActor = NULL;
18   this->Actor = NULL;
19   if( mesh != NULL )
20   {
21     unsigned long nPolys = mesh->GetNumberOfPolys( );
22     if( nPolys >= cpPlugins_Mesh_MAX_POLYS )
23     {
24       this->Normals = vtkSmartPointer< vtkPolyDataNormals >::New( );
25       this->Stripper = vtkSmartPointer< vtkStripper >::New( );
26       this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
27       this->LODActor = vtkSmartPointer< vtkQuadricLODActor >::New( );
28
29       double r[ 2 ];
30       mesh->GetScalarRange( r );
31
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( );
40     }
41     else
42     {
43       this->Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
44       this->Actor = vtkSmartPointer< vtkActor >::New( );
45       this->Mapper->SetInputData( mesh );
46       this->Actor->SetMapper( this->Mapper );
47
48     } // fi
49
50   } // fi
51 }
52
53 // -------------------------------------------------------------------------
54 void cpPlugins::Mesh::
55 SetITK( itk::LightObject* o )
56 {
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 );
62 }
63
64 // -------------------------------------------------------------------------
65 void cpPlugins::Mesh::
66 SetVTK( vtkObjectBase* o )
67 {
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;
74
75   vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
76   if( mesh == NULL )
77   {
78     this->m_ITKObject = NULL;
79     this->Modified( );
80     return;
81
82   } // fi
83
84   if( this->m_VTKObject.GetPointer( ) != mesh )
85   {
86     this->m_VTKObject = mesh;
87
88     // Copy points
89     _TMesh::Pointer imesh = _TMesh::New( );
90     double point[ 3 ];
91     for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
92     {
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 );
99
100     } // rof
101
102     // Copy cells
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( );
108
109     for( unsigned int c = 0; c < 4; c++ )
110     {
111       if( arrays[ c ] != NULL )
112       {
113         vtkSmartPointer< vtkIdList > ids =
114           vtkSmartPointer< vtkIdList >::New( );
115         arrays[ c ]->InitTraversal( );
116         while( arrays[ c ]->GetNextCell( ids ) == 1 )
117         {
118           long nPoints = ids->GetNumberOfIds( );
119           _TCellAutoPointer icell;
120           if( nPoints == 2 )
121           {
122             icell.TakeOwnership( new _TLine );
123             icell->SetPointId( 0, ids->GetId( 0 ) );
124             icell->SetPointId( 1, ids->GetId( 1 ) );
125           }
126           else if( nPoints == 3 )
127           {
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 ) );
132           }
133           else if( nPoints > 3 )
134           {
135             _TPolygon* polygon = new _TPolygon( );
136             for( long j = 0; j < nPoints; ++j )
137               polygon->AddPointId( ids->GetId( j ) );
138             icell.TakeOwnership( polygon );
139
140           } // fi
141           imesh->SetCell( imesh->GetNumberOfCells( ), icell );
142
143         } // elihw
144
145       } // fi
146
147     } // rof
148     this->m_ITKObject = imesh;
149     this->Modified( );
150
151   } // fi
152 }
153
154 // -------------------------------------------------------------------------
155 cpPlugins::Mesh::
156 Mesh( )
157   : Superclass( )
158 {
159 }
160
161 // -------------------------------------------------------------------------
162 cpPlugins::Mesh::
163 ~Mesh( )
164 {
165 }
166
167 // -------------------------------------------------------------------------
168 void cpPlugins::Mesh::
169 _CreateVTKActor( ) const
170 {
171   this->m_MeshActor.SetMesh(
172     const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) )
173     );
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( );
178   else
179     this->m_Actor = NULL;
180   
181   /* TODO
182   vtkPolyData* mesh =
183     const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) );
184   if( mesh != NULL )
185   {
186     unsigned long nPolys = mesh->GetNumberOfPolys( );
187     if( nPolys >= cpPlugins_Mesh_MAX_POLYS )
188     {
189       auto normals = vtkPolyDataNormals::New( );
190       auto stripper = vtkStripper::New( );
191       auto mapper = vtkPolyDataMapper::New( );
192       auto actor = vtkQuadricLODActor::New( );
193
194       double r[ 2 ];
195       mesh->GetScalarRange( r );
196
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;
206       mapper->Delete( );
207       stripper->Delete( );
208       normals->Delete( );
209     }
210     else
211     {
212       auto mapper = vtkPolyDataMapper::New( );
213       mapper->SetInputData( mesh );
214       auto actor = vtkActor::New( );
215       actor->SetMapper( mapper );
216       this->m_Actor = actor;
217       mapper->Delete( );
218
219     } // fi
220
221   } // fi
222   */
223 }
224
225 // eof - $RCSfile$