]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Mesh.cxx
2854840d33f8eeb5158f2cec010214e79302bd82
[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 <vtkPolyData.h>
9
10 // -------------------------------------------------------------------------
11 void cpPlugins::Mesh::
12 SetITK( itk::LightObject* o )
13 {
14   this->Superclass::SetITK( o );
15   bool     r = this->_ITK_2_VTK< itk::Mesh< float, 2 > >( o );
16   if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 2 > >( o );
17   if( !r ) r = this->_ITK_2_VTK< itk::Mesh< float, 3 > >( o );
18   if( !r ) r = this->_ITK_2_VTK< itk::Mesh< double, 3 > >( o );
19 }
20
21 // -------------------------------------------------------------------------
22 void cpPlugins::Mesh::
23 SetVTK( vtkObjectBase* o )
24 {
25   typedef itk::Mesh< double, 3 >      _TMesh;
26   typedef _TMesh::CellType            _TCell;
27   typedef _TCell::CellAutoPointer     _TCellAutoPointer;
28   typedef itk::LineCell< _TCell >     _TLine;
29   typedef itk::TriangleCell< _TCell > _TTriangle;
30   typedef itk::PolygonCell< _TCell >  _TPolygon;
31
32   vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
33   if( mesh == NULL )
34   {
35     this->m_ITKObject = NULL;
36     this->Modified( );
37     return;
38
39   } // fi
40
41   if( this->m_VTKObject.GetPointer( ) != mesh )
42   {
43     this->m_VTKObject = mesh;
44
45     // Copy points
46     _TMesh::Pointer imesh = _TMesh::New( );
47     double point[ 3 ];
48     for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
49     {
50       mesh->GetPoint( i, point );
51       _TMesh::PointType ipoint;
52       ipoint[ 0 ] = point[ 0 ];
53       ipoint[ 1 ] = point[ 1 ];
54       ipoint[ 2 ] = point[ 2 ];
55       imesh->SetPoint( i, ipoint );
56
57     } // rof
58
59     // Copy cells
60     vtkCellArray* arrays[ 4 ];
61     arrays[ 0 ] = mesh->GetLines( );
62     arrays[ 1 ] = mesh->GetPolys( );
63     arrays[ 2 ] = NULL; // TODO: mesh->GetStrips( );
64     arrays[ 3 ] = mesh->GetVerts( );
65
66     for( unsigned int c = 0; c < 4; c++ )
67     {
68       if( arrays[ c ] != NULL )
69       {
70         vtkSmartPointer< vtkIdList > ids =
71           vtkSmartPointer< vtkIdList >::New( );
72         arrays[ c ]->InitTraversal( );
73         while( arrays[ c ]->GetNextCell( ids ) == 1 )
74         {
75           long nPoints = ids->GetNumberOfIds( );
76           _TCellAutoPointer icell;
77           if( nPoints == 2 )
78           {
79             icell.TakeOwnership( new _TLine );
80             icell->SetPointId( 0, ids->GetId( 0 ) );
81             icell->SetPointId( 1, ids->GetId( 1 ) );
82           }
83           else if( nPoints == 3 )
84           {
85             icell.TakeOwnership( new _TTriangle );
86             icell->SetPointId( 0, ids->GetId( 0 ) );
87             icell->SetPointId( 1, ids->GetId( 1 ) );
88             icell->SetPointId( 2, ids->GetId( 2 ) );
89           }
90           else if( nPoints > 3 )
91           {
92             _TPolygon* polygon = new _TPolygon( );
93             for( long j = 0; j < nPoints; ++j )
94               polygon->AddPointId( ids->GetId( j ) );
95             icell.TakeOwnership( polygon );
96
97           } // fi
98           imesh->SetCell( imesh->GetNumberOfCells( ), icell );
99
100         } // elihw
101
102       } // fi
103
104     } // rof
105     this->m_ITKObject = imesh;
106     this->Modified( );
107
108   } // fi
109 }
110
111 // -------------------------------------------------------------------------
112 cpPlugins::Mesh::
113 Mesh( )
114   : Superclass( )
115 {
116 }
117
118 // -------------------------------------------------------------------------
119 cpPlugins::Mesh::
120 ~Mesh( )
121 {
122 }
123
124 // -------------------------------------------------------------------------
125 void cpPlugins::Mesh::
126 _CreateVTKActor( ) const
127 {
128   vtkPolyData* mesh =
129     const_cast< vtkPolyData* >( this->GetVTK< vtkPolyData >( ) );
130   if( mesh != NULL )
131   {
132     std::cout << "Mesh: " << mesh << std::endl;
133     /* TODO
134        vtkImageSliceMapper* mapper = vtkImageSliceMapper::New( );
135        vtkImageActor* actor = vtkImageActor::New( );
136        mapper->SetInputData( image );
137        actor->SetMapper( mapper );
138        this->m_Actor = actor;
139     */
140
141   } // fi
142 }
143
144 // eof - $RCSfile$