Salome HOME
correct previous integration (Porting to Python 2.6)
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshEditPreview.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // SMESH SMESHGUI : GUI for SMESH component
23 // File   : SMESHGUI_MeshEditPreview.cxx
24 // Author : Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "SMESHGUI_MeshEditPreview.h"
28
29 #include "SMESHGUI_VTKUtils.h"
30
31 #include <SMESH_Actor.h>
32 #include <SMESH_ActorUtils.h>
33
34 // SALOME GUI includes
35 #include <VTKViewer_CellLocationsArray.h>
36 #include <SVTK_ViewWindow.h>
37
38 // VTK includes
39 #include <vtkPoints.h>
40 #include <vtkIdList.h>
41 #include <vtkCellArray.h>
42 #include <vtkUnsignedCharArray.h>
43 #include <vtkUnstructuredGrid.h>
44 #include <vtkUnstructuredGridWriter.h>
45 #include <vtkDataSetMapper.h>
46 #include <vtkProperty.h>
47
48 // Qt includes
49 #include <QColor>
50
51 // IDL includes
52 #include <SALOMEconfig.h>
53 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
54
55 //================================================================================
56 /*!
57  * \brief Constructor
58  */
59 //================================================================================
60
61 SMESHGUI_MeshEditPreview::SMESHGUI_MeshEditPreview(SVTK_ViewWindow* theViewWindow):
62   myViewWindow(theViewWindow)
63 {
64   myGrid = vtkUnstructuredGrid::New();
65
66   // Create and display actor
67   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
68   aMapper->SetInput( myGrid );
69
70   myPreviewActor = SALOME_Actor::New();
71   myPreviewActor->SetInfinitive(true);
72   myPreviewActor->VisibilityOn();
73   myPreviewActor->PickableOff();
74
75   vtkFloatingPointType anRGB[3];
76   SMESH::GetColor( "SMESH", "selection_element_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
77   SetColor( anRGB[0], anRGB[1], anRGB[2] );
78
79   myPreviewActor->SetMapper( aMapper );
80   aMapper->Delete();
81
82   myViewWindow->AddActor(myPreviewActor);
83
84 }
85
86 //================================================================================
87 /*!
88  * \brief Destroy
89  */
90 //================================================================================
91
92 SMESHGUI_MeshEditPreview::~SMESHGUI_MeshEditPreview()
93 {
94   myGrid->Delete();
95
96   myViewWindow->RemoveActor(myPreviewActor);
97   myPreviewActor->Delete();
98
99 }
100
101 //================================================================================
102 /*!
103  * \brief Returns vtk cell type
104  */
105 //================================================================================
106
107 vtkIdType getCellType( const SMDSAbs_ElementType theType,
108                        const bool thePoly,
109                        const int theNbNodes )
110 {
111   switch( theType ) 
112   {
113   case SMDSAbs_Node:              return VTK_VERTEX;
114   case SMDSAbs_Edge: 
115     if( theNbNodes == 2 )         return VTK_LINE;
116     else if ( theNbNodes == 3 )   return VTK_QUADRATIC_EDGE;
117     else return VTK_EMPTY_CELL;
118
119   case SMDSAbs_Face  :
120     if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
121     else if ( theNbNodes == 3 )   return VTK_TRIANGLE;
122     else if ( theNbNodes == 4 )   return VTK_QUAD;
123     else if ( theNbNodes == 6 )   return VTK_QUADRATIC_TRIANGLE;
124     else if ( theNbNodes == 8 )   return VTK_QUADRATIC_QUAD;
125     else return VTK_EMPTY_CELL;
126
127   case SMDSAbs_Volume:
128     if (thePoly && theNbNodes>3 ) return VTK_CONVEX_POINT_SET;
129     else if ( theNbNodes == 4 )   return VTK_TETRA;
130     else if ( theNbNodes == 5 )   return VTK_PYRAMID;
131     else if ( theNbNodes == 6 )   return VTK_WEDGE;
132     else if ( theNbNodes == 8 )   return VTK_HEXAHEDRON;
133     else if ( theNbNodes == 10 )  {
134       return VTK_QUADRATIC_TETRA;
135     }
136     else if ( theNbNodes == 20 )  {
137       return VTK_QUADRATIC_HEXAHEDRON;
138     }
139     else if ( theNbNodes==15  )  {
140       return VTK_QUADRATIC_WEDGE;
141     }
142     else if ( theNbNodes==13  )  {
143       return VTK_CONVEX_POINT_SET;
144     }
145     else return VTK_EMPTY_CELL;
146
147   default: return VTK_EMPTY_CELL;
148   }
149 }
150
151 //================================================================================
152 /*!
153  * \brief Set preview data
154  */
155 //================================================================================
156
157 void SMESHGUI_MeshEditPreview::SetData (const SMESH::MeshPreviewStruct* previewData)
158 {
159   // Create points
160   const SMESH::nodes_array& aNodesXYZ = previewData->nodesXYZ;
161   vtkPoints* aPoints = vtkPoints::New();
162   aPoints->SetNumberOfPoints(aNodesXYZ.length());
163
164   for ( int i = 0; i < aNodesXYZ.length(); i++ ) {
165     aPoints->SetPoint( i, aNodesXYZ[i].x, aNodesXYZ[i].y, aNodesXYZ[i].z );
166   }
167   myGrid->SetPoints(aPoints);
168
169   aPoints->Delete();
170
171   // Create cells
172   const SMESH::long_array&  anElemConnectivity = previewData->elementConnectivities;
173   const SMESH::types_array& anElemTypes = previewData->elementTypes;
174
175   vtkIdType aCellsSize = anElemConnectivity.length() + anElemTypes.length();
176   vtkIdType aNbCells = anElemTypes.length();
177
178   vtkCellArray* aConnectivity = vtkCellArray::New();
179   aConnectivity->Allocate( aCellsSize, 0 );
180
181   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
182   aCellTypesArray->SetNumberOfComponents( 1 );
183   aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
184
185   vtkIdList *anIdList = vtkIdList::New();
186   int aNodePos = 0;
187
188   for ( int i = 0; i < anElemTypes.length(); i++ ) {
189     const SMESH::ElementSubType& anElementSubType = anElemTypes[i];
190     SMDSAbs_ElementType aType = SMDSAbs_ElementType(anElementSubType.SMDS_ElementType);
191     vtkIdType aNbNodes = anElementSubType.nbNodesInElement;
192     anIdList->SetNumberOfIds( aNbNodes );
193
194     for ( vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++ ){
195       anIdList->SetId( aNodeId, anElemConnectivity[aNodePos] );
196       aNodePos++;
197     }
198
199     aConnectivity->InsertNextCell( anIdList );
200     aCellTypesArray->InsertNextValue( getCellType( aType,
201                                                    anElemTypes[i].isPoly, 
202                                                    aNbNodes ) );
203   }
204   anIdList->Delete();
205
206   // Insert cells in grid
207   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
208   aCellLocationsArray->SetNumberOfComponents( 1 );
209   aCellLocationsArray->SetNumberOfTuples( aNbCells );
210
211   aConnectivity->InitTraversal();
212   for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
213     aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
214
215   myGrid->SetCells( aCellTypesArray, aCellLocationsArray, aConnectivity );
216
217   myPreviewActor->GetMapper()->Update();
218
219   aCellTypesArray->Delete();
220   aCellLocationsArray->Delete();
221   aConnectivity->Delete();
222
223   SetVisibility(true);
224 }
225
226 //================================================================================
227 /*!
228  * \brief Set visibility
229  */
230 //================================================================================
231
232 void SMESHGUI_MeshEditPreview::SetVisibility (bool theVisibility)
233 {
234   myPreviewActor->SetVisibility(theVisibility);
235   SMESH::RepaintCurrentView();
236 }
237
238 //================================================================================
239 /*!
240  * \brief Set preview color
241  */
242 //================================================================================
243
244 void SMESHGUI_MeshEditPreview::SetColor(double R, double G, double B)
245 {
246   myPreviewActor->SetColor( R, G, B );
247 }
248
249 //================================================================================
250 /*!
251  * \brief Get preview actor
252  */
253 //================================================================================
254 SALOME_Actor* SMESHGUI_MeshEditPreview::GetActor() const
255
256   return myPreviewActor;
257 }