Salome HOME
22316: EDF 2719 SMESH: Split hexas into prisms
[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 #include <gp_Ax3.hxx>
57
58 //================================================================================
59 /*!
60  * \brief Constructor
61  */
62 //================================================================================
63
64 SMESHGUI_MeshEditPreview::SMESHGUI_MeshEditPreview(SVTK_ViewWindow* theViewWindow):
65   myViewWindow(theViewWindow)
66 {
67   myGrid = vtkUnstructuredGrid::New();
68
69   // Create and display actor
70   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
71   aMapper->SetInputData( myGrid );
72
73   myPreviewActor = SALOME_Actor::New();
74   myPreviewActor->SetInfinitive(true);
75   myPreviewActor->VisibilityOn();
76   myPreviewActor->PickableOff();
77
78   double aFactor,aUnits;
79   myPreviewActor->SetResolveCoincidentTopology(true);
80   myPreviewActor->GetPolygonOffsetParameters(aFactor,aUnits);
81   myPreviewActor->SetPolygonOffsetParameters(aFactor,0.2*aUnits);
82
83   double anRGB[3];
84   SMESH::GetColor( "SMESH", "selection_element_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
85   SetColor( anRGB[0], anRGB[1], anRGB[2] );
86
87   myPreviewActor->SetMapper( aMapper );
88   aMapper->Delete();
89
90   myViewWindow->AddActor(myPreviewActor);
91
92 }
93
94 //================================================================================
95 /*!
96  * \brief Destroy
97  */
98 //================================================================================
99
100 SMESHGUI_MeshEditPreview::~SMESHGUI_MeshEditPreview()
101 {
102   myGrid->Delete();
103
104   myViewWindow->RemoveActor(myPreviewActor);
105   myPreviewActor->Delete();
106
107 }
108
109 //================================================================================
110 /*!
111  * \brief Returns vtk cell type
112  */
113 //================================================================================
114
115 vtkIdType getCellType( const SMDSAbs_ElementType theType,
116                        const bool                thePoly,
117                        const int                 theNbNodes )
118 {
119   switch( theType ) 
120   {
121   case SMDSAbs_Node:              return VTK_VERTEX;
122   case SMDSAbs_Edge: 
123     if( theNbNodes == 2 )         return VTK_LINE;
124     else if ( theNbNodes == 3 )   return VTK_QUADRATIC_EDGE;
125     else return VTK_EMPTY_CELL;
126
127   case SMDSAbs_Face  :
128     if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
129     else if ( theNbNodes == 3 )   return VTK_TRIANGLE;
130     else if ( theNbNodes == 4 )   return VTK_QUAD;
131     else if ( theNbNodes == 6 )   return VTK_QUADRATIC_TRIANGLE;
132     else if ( theNbNodes == 7 )   return VTK_BIQUADRATIC_TRIANGLE;
133     else if ( theNbNodes == 8 )   return VTK_QUADRATIC_QUAD;
134     else if ( theNbNodes == 9 )   return VTK_BIQUADRATIC_QUAD;
135     else return VTK_EMPTY_CELL;
136
137   case SMDSAbs_Volume:
138     if (thePoly && theNbNodes>3 ) return VTK_CONVEX_POINT_SET;
139     else if ( theNbNodes == 4 )   return VTK_TETRA;
140     else if ( theNbNodes == 5 )   return VTK_PYRAMID;
141     else if ( theNbNodes == 6 )   return VTK_WEDGE;
142     else if ( theNbNodes == 8 )   return VTK_HEXAHEDRON;
143     else if ( theNbNodes == 10 )  return VTK_QUADRATIC_TETRA;
144     else if ( theNbNodes == 20 )  return VTK_QUADRATIC_HEXAHEDRON;
145     else if ( theNbNodes == 27 )  return VTK_TRIQUADRATIC_HEXAHEDRON;
146     else if ( theNbNodes == 15  ) return VTK_QUADRATIC_WEDGE;
147     else if ( theNbNodes == 13  ) return VTK_QUADRATIC_PYRAMID;//VTK_CONVEX_POINT_SET;
148     else return VTK_EMPTY_CELL;
149
150   default: return VTK_EMPTY_CELL;
151   }
152 }
153
154 //================================================================================
155 /*!
156  * \brief Set preview data
157  */
158 //================================================================================
159
160 void SMESHGUI_MeshEditPreview::SetData (const SMESH::MeshPreviewStruct* previewData)
161 {
162   // Create points
163   const SMESH::nodes_array& aNodesXYZ = previewData->nodesXYZ;
164   vtkPoints* aPoints = vtkPoints::New();
165   aPoints->SetNumberOfPoints(aNodesXYZ.length());
166
167   for ( int i = 0; i < aNodesXYZ.length(); i++ ) {
168     aPoints->SetPoint( i, aNodesXYZ[i].x, aNodesXYZ[i].y, aNodesXYZ[i].z );
169   }
170   myGrid->SetPoints(aPoints);
171
172   aPoints->Delete();
173
174   // Create cells
175   const SMESH::long_array&  anElemConnectivity = previewData->elementConnectivities;
176   const SMESH::types_array& anElemTypes = previewData->elementTypes;
177
178   vtkIdType aCellsSize = anElemConnectivity.length() + anElemTypes.length();
179   vtkIdType aNbCells = anElemTypes.length();
180
181   vtkCellArray* aConnectivity = vtkCellArray::New();
182   aConnectivity->Allocate( aCellsSize, 0 );
183
184   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
185   aCellTypesArray->SetNumberOfComponents( 1 );
186   aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
187
188   vtkIdList *anIdList = vtkIdList::New();
189   int aNodePos = 0;
190
191   for ( int i = 0; i < anElemTypes.length(); i++ ) {
192     const SMESH::ElementSubType& anElementSubType = anElemTypes[i];
193     SMDSAbs_ElementType aType = SMDSAbs_ElementType(anElementSubType.SMDS_ElementType);
194     vtkIdType aNbNodes = anElementSubType.nbNodesInElement;
195     anIdList->SetNumberOfIds( aNbNodes );
196
197     for ( vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++ ){
198       anIdList->SetId( aNodeId, anElemConnectivity[aNodePos] );
199       aNodePos++;
200     }
201
202     aConnectivity->InsertNextCell( anIdList );
203     aCellTypesArray->InsertNextValue( getCellType( aType,
204                                                    anElemTypes[i].isPoly, 
205                                                    aNbNodes ) );
206   }
207   anIdList->Delete();
208
209   // Insert cells in grid
210   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
211   aCellLocationsArray->SetNumberOfComponents( 1 );
212   aCellLocationsArray->SetNumberOfTuples( aNbCells );
213
214   aConnectivity->InitTraversal();
215   for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
216     aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
217
218   myGrid->SetCells( aCellTypesArray, aCellLocationsArray, aConnectivity );
219
220   myPreviewActor->GetMapper()->Update();
221
222   aCellTypesArray->Delete();
223   aCellLocationsArray->Delete();
224   aConnectivity->Delete();
225
226   SetVisibility(true);
227 }
228
229 //================================================================================
230 /*!
231  * \brief Set shape of an arrow of a unit length and nb of arrows
232  */
233 //================================================================================
234
235 void SMESHGUI_MeshEditPreview::SetArrowShapeAndNb( int    nbArrows,
236                                                    double headLength,
237                                                    double headRadius,
238                                                    double start)
239 {
240   const int theNbPoints = 10; // in one arrow
241   myUnitArrowPnts.reserve( theNbPoints );
242   myUnitArrowPnts.clear();
243
244   // unit arrow || OZ
245
246   for ( int i = 0; i < theNbPoints - 2; ++i )
247   {
248     double angle = i * 2 * M_PI / ( theNbPoints - 2 );
249     myUnitArrowPnts.push_back( gp_Pnt( headRadius * Cos( angle ),
250                                        headRadius * Sin( angle ),
251                                        1. - headLength ));
252   }
253   myUnitArrowPnts.push_back( gp_Pnt( 0, 0, start ));
254   myUnitArrowPnts.push_back( gp_Pnt( 0, 0, 1 ));
255
256
257   // nodes of all arrows
258
259   vtkPoints* aPoints = vtkPoints::New();
260   aPoints->SetNumberOfPoints( theNbPoints * nbArrows );
261   for ( int iP = 0, iA = 0; iA < nbArrows; ++iA )
262     for ( int i = 0; i < theNbPoints; ++i, ++iP )
263       aPoints->SetPoint( iP,
264                          myUnitArrowPnts[i].X(),
265                          myUnitArrowPnts[i].Y(),
266                          myUnitArrowPnts[i].Z()  );
267   myGrid->SetPoints(aPoints);
268   aPoints->Delete();
269
270   // connectivity of all arrows
271
272   const int theNbCells = ( theNbPoints - 1 ); // in one arrow
273   myGrid->Allocate( theNbCells  * nbArrows );
274   for ( int nP = 0, iA = 0; iA < nbArrows; ++iA, nP += theNbPoints )
275   {
276     vtkIdType conn[3] = { theNbPoints - 1 + nP, // arrow end
277                           theNbPoints - 3 + nP, // point on a circle
278                           nP };                 // point on a circle
279     for ( int i = 0; i < theNbCells-1; ++i )
280     {
281       myGrid->InsertNextCell( VTK_TRIANGLE, 3, conn );
282       conn[1] = conn[2];
283       conn[2] = conn[2] + 1;
284     }
285     conn[1] = theNbPoints - 2 + nP;
286     myGrid->InsertNextCell( VTK_LINE, 2, conn );
287   }
288
289   myNbArrows = nbArrows;
290 }
291
292 //================================================================================
293 /*!
294  * \brief Set data to show moved/rotated/scaled arrows
295  *  \param [in] axes - location and direction of the arrows
296  *  \param [in] length - length of arrows
297  */
298 //================================================================================
299
300 void SMESHGUI_MeshEditPreview::SetArrows( const gp_Ax1* axes,
301                                           double        length )
302 {
303   vtkPoints* aPoints = myGrid->GetPoints();
304
305   for ( int iP = 0, iA = 0; iA < myNbArrows; ++iA )
306   {
307     gp_Trsf trsf;
308     trsf.SetTransformation( gp_Ax3( axes[iA].Location(), axes[iA].Direction() ), gp::XOY() );
309
310     for ( size_t i = 0; i < myUnitArrowPnts.size(); ++i, ++iP )
311     {
312       gp_Pnt p = myUnitArrowPnts[i].Scaled( gp::Origin(), length );
313       p.Transform( trsf );
314       aPoints->SetPoint( iP, p.X(), p.Y(), p.Z() );
315     }
316   }
317
318   myGrid->Modified();
319 }
320
321 //================================================================================
322 /*!
323  * \brief Set visibility
324  */
325 //================================================================================
326
327 void SMESHGUI_MeshEditPreview::SetVisibility (bool theVisibility)
328 {
329   myPreviewActor->SetVisibility(theVisibility);
330   SMESH::RepaintCurrentView();
331 }
332
333 //================================================================================
334 /*!
335  * \brief Set preview color
336  */
337 //================================================================================
338
339 void SMESHGUI_MeshEditPreview::SetColor(double R, double G, double B)
340 {
341   myPreviewActor->SetColor( R, G, B );
342 }
343
344 //================================================================================
345 /*!
346  * \brief Get preview actor
347  */
348 //================================================================================
349
350 SALOME_Actor* SMESHGUI_MeshEditPreview::GetActor() const
351
352   return myPreviewActor;
353 }
354
355 //================================================================================
356 /*!
357  * \brief Returns the priewed vtkUnstructuredGrid
358  */
359 //================================================================================
360
361 vtkUnstructuredGrid* SMESHGUI_MeshEditPreview::GetGrid() const
362 {
363   return myGrid;
364 }