Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshEditPreview.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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 //
23 //
24 //  File   : SMESHGUI_MeshEditPreview.cxx
25 //  Module : SMESH
26 //  $Header:
27
28 #include "SMESHGUI_MeshEditPreview.h"
29
30 #include "VTKViewer_CellLocationsArray.h"
31 #include "SVTK_ViewWindow.h"
32
33 #include "SMESH_Actor.h"
34 #include "SMESH_ActorUtils.h"
35 #include "SMESHGUI_VTKUtils.h"
36
37 // VTK Includes
38 #include <vtkPoints.h>
39 #include <vtkIdList.h>
40 #include <vtkCellArray.h>
41 #include <vtkUnsignedCharArray.h>
42 #include <vtkUnstructuredGrid.h>
43 #include <vtkUnstructuredGridWriter.h>
44 #include <vtkDataSetMapper.h>
45 #include <vtkProperty.h>
46
47 // QT Includes
48 #include <qcolor.h>
49
50 // IDL Headers
51 #include "SALOMEconfig.h"
52 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
53
54 using namespace SMESH;
55
56 //================================================================================
57 /*!
58  * \brief Constructor
59  */
60 //================================================================================
61
62 SMESHGUI_MeshEditPreview::SMESHGUI_MeshEditPreview(SVTK_ViewWindow* theViewWindow):
63   myViewWindow(theViewWindow)
64 {
65   myGrid = vtkUnstructuredGrid::New();
66
67   // Create and display actor
68   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
69   aMapper->SetInput( myGrid );
70
71   myPreviewActor = SALOME_Actor::New();
72   myPreviewActor->SetInfinitive(true);
73   myPreviewActor->VisibilityOn();
74   myPreviewActor->PickableOff();
75
76   vtkFloatingPointType anRGB[3];
77   GetColor( "SMESH", "selection_element_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
78   SetColor( anRGB[0], anRGB[1], anRGB[2] );
79
80   myPreviewActor->SetMapper( aMapper );
81   aMapper->Delete();
82
83   myViewWindow->AddActor(myPreviewActor);
84
85 }
86
87 //================================================================================
88 /*!
89  * \brief Destroy
90  */
91 //================================================================================
92
93 SMESHGUI_MeshEditPreview::~SMESHGUI_MeshEditPreview()
94 {
95   myGrid->Delete();
96
97   myViewWindow->RemoveActor(myPreviewActor);
98   myPreviewActor->Delete();
99
100 }
101
102 //================================================================================
103 /*!
104  * \brief Returns vtk cell type
105  */
106 //================================================================================
107
108 vtkIdType getCellType( const SMDSAbs_ElementType theType,
109                        const bool thePoly,
110                        const int theNbNodes )
111 {
112   switch( theType ) 
113   {
114   case SMDSAbs_Node:              return VTK_VERTEX;
115   case SMDSAbs_Edge: 
116     if( theNbNodes == 2 )         return VTK_LINE;
117     else if ( theNbNodes == 3 )   return VTK_QUADRATIC_EDGE;
118     else return VTK_EMPTY_CELL;
119
120   case SMDSAbs_Face  :
121     if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
122     else if ( theNbNodes == 3 )   return VTK_TRIANGLE;
123     else if ( theNbNodes == 4 )   return VTK_QUAD;
124     else if ( theNbNodes == 6 )   return VTK_QUADRATIC_TRIANGLE;
125     else if ( theNbNodes == 8 )   return VTK_QUADRATIC_QUAD;
126     else return VTK_EMPTY_CELL;
127
128   case SMDSAbs_Volume:
129     if (thePoly && theNbNodes>3 ) return VTK_CONVEX_POINT_SET;
130     else if ( theNbNodes == 4 )   return VTK_TETRA;
131     else if ( theNbNodes == 5 )   return VTK_PYRAMID;
132     else if ( theNbNodes == 6 )   return VTK_WEDGE;
133     else if ( theNbNodes == 8 )   return VTK_HEXAHEDRON;
134     else if ( theNbNodes == 10 )  {
135       return VTK_QUADRATIC_TETRA;
136     }
137     else if ( theNbNodes == 20 )  {
138       return VTK_QUADRATIC_HEXAHEDRON;
139     }
140     else if ( theNbNodes==13 || theNbNodes==15 )  {
141       return VTK_CONVEX_POINT_SET;
142     }
143     else return VTK_EMPTY_CELL;
144
145   default: return VTK_EMPTY_CELL;
146   }
147 }
148
149 //================================================================================
150 /*!
151  * \brief Set preview data
152  */
153 //================================================================================
154
155 void SMESHGUI_MeshEditPreview::SetData (const SMESH::MeshPreviewStruct* previewData)
156 {
157   // Create points
158   const SMESH::nodes_array& aNodesXYZ = previewData->nodesXYZ;
159   vtkPoints* aPoints = vtkPoints::New();
160   aPoints->SetNumberOfPoints(aNodesXYZ.length());
161
162   for ( int i = 0; i < aNodesXYZ.length(); i++ ) {
163     aPoints->SetPoint( i, aNodesXYZ[i].x, aNodesXYZ[i].y, aNodesXYZ[i].z );
164   }
165   myGrid->SetPoints(aPoints);
166
167   aPoints->Delete();
168
169   // Create cells
170   const SMESH::long_array&  anElemConnectivity = previewData->elementConnectivities;
171   const SMESH::types_array& anElemTypes = previewData->elementTypes;
172
173   vtkIdType aCellsSize = anElemConnectivity.length() + anElemTypes.length();
174   vtkIdType aNbCells = anElemTypes.length();
175
176   vtkCellArray* aConnectivity = vtkCellArray::New();
177   aConnectivity->Allocate( aCellsSize, 0 );
178
179   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
180   aCellTypesArray->SetNumberOfComponents( 1 );
181   aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
182
183   vtkIdList *anIdList = vtkIdList::New();
184   int aNodePos = 0;
185
186   for ( int i = 0; i < anElemTypes.length(); i++ ) {
187     const ElementSubType& anElementSubType = anElemTypes[i];
188     SMDSAbs_ElementType aType = SMDSAbs_ElementType(anElementSubType.SMDS_ElementType);
189     vtkIdType aNbNodes = anElementSubType.nbNodesInElement;
190     anIdList->SetNumberOfIds( aNbNodes );
191
192     for ( vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++ ){
193       anIdList->SetId( aNodeId, anElemConnectivity[aNodePos] );
194       aNodePos++;
195     }
196
197     aConnectivity->InsertNextCell( anIdList );
198     aCellTypesArray->InsertNextValue( getCellType( aType,
199                                                    anElemTypes[i].isPoly, 
200                                                    aNbNodes ) );
201   }
202   anIdList->Delete();
203
204   // Insert cells in grid
205   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
206   aCellLocationsArray->SetNumberOfComponents( 1 );
207   aCellLocationsArray->SetNumberOfTuples( aNbCells );
208
209   aConnectivity->InitTraversal();
210   for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
211     aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
212
213   myGrid->SetCells( aCellTypesArray, aCellLocationsArray, aConnectivity );
214
215   myPreviewActor->GetMapper()->Update();
216
217   aCellTypesArray->Delete();
218   aCellLocationsArray->Delete();
219   aConnectivity->Delete();
220
221   SetVisibility(true);
222 }
223
224 //================================================================================
225 /*!
226  * \brief Set visibility
227  */
228 //================================================================================
229
230 void SMESHGUI_MeshEditPreview::SetVisibility (bool theVisibility)
231 {
232   myPreviewActor->SetVisibility(theVisibility);
233   RepaintCurrentView();
234 }
235
236 //================================================================================
237 /*!
238  * \brief Set preview color
239  */
240 //================================================================================
241
242 void SMESHGUI_MeshEditPreview::SetColor(double R, double G, double B)
243 {
244   myPreviewActor->SetColor( R, G, B );
245 }