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