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