]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Mesh.cxx
f0e29f50b31054eb8a7fd34558b92640a6e14d62
[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 #include <vtkActor.h>
9 #include <vtkPolyDataNormals.h>
10 #include <vtkPolyData.h>
11 #include <vtkPolyDataMapper.h>
12 #include <vtkQuadricLODActor.h>
13 #include <vtkStripper.h>
14
15 #define cpPlugins_Mesh_MAX_POLYS 65535
16
17 // -------------------------------------------------------------------------
18 void cpPlugins::Mesh::
19 SetITK( itk::LightObject* o )
20 {
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 );
26 }
27
28 // -------------------------------------------------------------------------
29 void cpPlugins::Mesh::
30 SetVTK( vtkObjectBase* o )
31 {
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;
38
39   vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
40   if( mesh == NULL )
41   {
42     this->m_ITKObject = NULL;
43     this->Modified( );
44     return;
45
46   } // fi
47
48   if( this->m_VTKObject.GetPointer( ) != mesh )
49   {
50     this->m_VTKObject = mesh;
51
52     // Copy points
53     _TMesh::Pointer imesh = _TMesh::New( );
54     double point[ 3 ];
55     for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
56     {
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 );
63
64     } // rof
65
66     // Copy cells
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( );
72
73     for( unsigned int c = 0; c < 4; c++ )
74     {
75       if( arrays[ c ] != NULL )
76       {
77         vtkSmartPointer< vtkIdList > ids =
78           vtkSmartPointer< vtkIdList >::New( );
79         arrays[ c ]->InitTraversal( );
80         while( arrays[ c ]->GetNextCell( ids ) == 1 )
81         {
82           long nPoints = ids->GetNumberOfIds( );
83           _TCellAutoPointer icell;
84           if( nPoints == 2 )
85           {
86             icell.TakeOwnership( new _TLine );
87             icell->SetPointId( 0, ids->GetId( 0 ) );
88             icell->SetPointId( 1, ids->GetId( 1 ) );
89           }
90           else if( nPoints == 3 )
91           {
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 ) );
96           }
97           else if( nPoints > 3 )
98           {
99             _TPolygon* polygon = new _TPolygon( );
100             for( long j = 0; j < nPoints; ++j )
101               polygon->AddPointId( ids->GetId( j ) );
102             icell.TakeOwnership( polygon );
103
104           } // fi
105           imesh->SetCell( imesh->GetNumberOfCells( ), icell );
106
107         } // elihw
108
109       } // fi
110
111     } // rof
112     this->m_ITKObject = imesh;
113     this->Modified( );
114
115   } // fi
116 }
117
118 // -------------------------------------------------------------------------
119 cpPlugins::Mesh::
120 Mesh( )
121   : Superclass( )
122 {
123 }
124
125 // -------------------------------------------------------------------------
126 cpPlugins::Mesh::
127 ~Mesh( )
128 {
129 }
130
131 // -------------------------------------------------------------------------
132 void cpPlugins::Mesh::
133 _CreateVTKActor( ) const
134 {
135   vtkPolyData* mesh =
136     const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) );
137   if( mesh != NULL )
138   {
139     unsigned long nPolys = mesh->GetNumberOfPolys( );
140     if( nPolys >= cpPlugins_Mesh_MAX_POLYS )
141     {
142       auto normals = vtkPolyDataNormals::New( );
143       auto stripper = vtkStripper::New( );
144       auto mapper = vtkPolyDataMapper::New( );
145       auto actor = vtkQuadricLODActor::New( );
146
147       double r[ 2 ];
148       mesh->GetScalarRange( r );
149
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;
159       mapper->Delete( );
160       stripper->Delete( );
161       normals->Delete( );
162     }
163     else
164     {
165       auto mapper = vtkPolyDataMapper::New( );
166       mapper->SetInputData( mesh );
167       auto actor = vtkActor::New( );
168       actor->SetMapper( mapper );
169       this->m_Actor = actor;
170       mapper->Delete( );
171
172     } // fi
173
174   } // fi
175 }
176
177 // eof - $RCSfile$