Salome HOME
Merge remote branch 'origin/gdd/translations'
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshEditPreview.cxx
1 // Copyright (C) 2007-2015  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_Ball:              return VTK_VERTEX;
130   case SMDSAbs_Node:              return VTK_VERTEX;
131   case SMDSAbs_Edge:
132     if( theNbNodes == 2 )         return VTK_LINE;
133     else if ( theNbNodes == 3 )   return VTK_QUADRATIC_EDGE;
134     else return VTK_EMPTY_CELL;
135
136   case SMDSAbs_Face  :
137     if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
138     else if ( theNbNodes == 3 )   return VTK_TRIANGLE;
139     else if ( theNbNodes == 4 )   return VTK_QUAD;
140     else if ( theNbNodes == 6 )   return VTK_QUADRATIC_TRIANGLE;
141     else if ( theNbNodes == 7 )   return VTK_BIQUADRATIC_TRIANGLE;
142     else if ( theNbNodes == 8 )   return VTK_QUADRATIC_QUAD;
143     else if ( theNbNodes == 9 )   return VTK_BIQUADRATIC_QUAD;
144     else return VTK_EMPTY_CELL;
145
146   case SMDSAbs_Volume:
147     if (thePoly && theNbNodes>3 ) return VTK_CONVEX_POINT_SET;
148     else if ( theNbNodes == 4 )   return VTK_TETRA;
149     else if ( theNbNodes == 5 )   return VTK_PYRAMID;
150     else if ( theNbNodes == 6 )   return VTK_WEDGE;
151     else if ( theNbNodes == 8 )   return VTK_HEXAHEDRON;
152     else if ( theNbNodes == 10 )  return VTK_QUADRATIC_TETRA;
153     else if ( theNbNodes == 20 )  return VTK_QUADRATIC_HEXAHEDRON;
154     else if ( theNbNodes == 27 )  return VTK_TRIQUADRATIC_HEXAHEDRON;
155     else if ( theNbNodes == 15  ) return VTK_QUADRATIC_WEDGE;
156     else if ( theNbNodes == 13  ) return VTK_QUADRATIC_PYRAMID;//VTK_CONVEX_POINT_SET;
157     else return VTK_EMPTY_CELL;
158
159   default: return VTK_EMPTY_CELL;
160   }
161 }
162
163 //================================================================================
164 /*!
165  * \brief Set preview data
166  */
167 //================================================================================
168
169 void SMESHGUI_MeshEditPreview::SetData (const SMESH::MeshPreviewStruct* previewData)
170 {
171   // Create points
172   const SMESH::nodes_array& aNodesXYZ = previewData->nodesXYZ;
173   vtkPoints* aPoints = vtkPoints::New();
174   aPoints->SetNumberOfPoints(aNodesXYZ.length());
175
176   for ( int i = 0; i < aNodesXYZ.length(); i++ ) {
177     aPoints->SetPoint( i, aNodesXYZ[i].x, aNodesXYZ[i].y, aNodesXYZ[i].z );
178   }
179   myGrid->SetPoints(aPoints);
180
181   aPoints->Delete();
182
183   // Create cells
184   const SMESH::long_array&  anElemConnectivity = previewData->elementConnectivities;
185   const SMESH::types_array& anElemTypes = previewData->elementTypes;
186
187   vtkIdType aCellsSize = anElemConnectivity.length() + anElemTypes.length();
188   vtkIdType aNbCells = anElemTypes.length();
189
190   vtkCellArray* aConnectivity = vtkCellArray::New();
191   aConnectivity->Allocate( aCellsSize, 0 );
192
193   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
194   aCellTypesArray->SetNumberOfComponents( 1 );
195   aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
196
197   vtkIdList *anIdList = vtkIdList::New();
198   int aNodePos = 0;
199
200   for ( int i = 0; i < anElemTypes.length(); i++ ) {
201     const SMESH::ElementSubType& anElementSubType = anElemTypes[i];
202     SMDSAbs_ElementType aType = SMDSAbs_ElementType(anElementSubType.SMDS_ElementType);
203     vtkIdType aNbNodes = anElementSubType.nbNodesInElement;
204     anIdList->SetNumberOfIds( aNbNodes );
205
206     for ( vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++ ){
207       anIdList->SetId( aNodeId, anElemConnectivity[aNodePos] );
208       aNodePos++;
209     }
210
211     aConnectivity->InsertNextCell( anIdList );
212     aCellTypesArray->InsertNextValue( getCellType( aType,
213                                                    anElemTypes[i].isPoly, 
214                                                    aNbNodes ) );
215   }
216   anIdList->Delete();
217
218   // Insert cells in grid
219   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
220   aCellLocationsArray->SetNumberOfComponents( 1 );
221   aCellLocationsArray->SetNumberOfTuples( aNbCells );
222
223   aConnectivity->InitTraversal();
224   for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
225     aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
226
227   myGrid->SetCells( aCellTypesArray, aCellLocationsArray, aConnectivity );
228
229   myPreviewActor->GetMapper()->Update();
230
231   aCellTypesArray->Delete();
232   aCellLocationsArray->Delete();
233   aConnectivity->Delete();
234
235   SetVisibility(true);
236 }
237
238 //================================================================================
239 /*!
240  * \brief Set shape of an arrow of a unit length and nb of arrows
241  */
242 //================================================================================
243
244 void SMESHGUI_MeshEditPreview::SetArrowShapeAndNb( int         nbArrows,
245                                                    double      headLength,
246                                                    double      headRadius,
247                                                    double      start,
248                                                    const char* labels)
249 {
250   const int theNbPoints = 10; // in one arrow
251   myUnitArrowPnts.reserve( theNbPoints );
252   myUnitArrowPnts.clear();
253
254   // unit arrow || OZ
255
256   for ( int i = 0; i < theNbPoints - 2; ++i )
257   {
258     double angle = i * 2 * M_PI / ( theNbPoints - 2 );
259     myUnitArrowPnts.push_back( gp_Pnt( headRadius * Cos( angle ),
260                                        headRadius * Sin( angle ),
261                                        1. - headLength ));
262   }
263   myUnitArrowPnts.push_back( gp_Pnt( 0, 0, start ));
264   myUnitArrowPnts.push_back( gp_Pnt( 0, 0, 1 ));
265
266
267   // nodes of all arrows
268
269   vtkPoints* aPoints = vtkPoints::New();
270   aPoints->SetNumberOfPoints( theNbPoints * nbArrows );
271   for ( int iP = 0, iA = 0; iA < nbArrows; ++iA )
272     for ( int i = 0; i < theNbPoints; ++i, ++iP )
273       aPoints->SetPoint( iP,
274                          myUnitArrowPnts[i].X(),
275                          myUnitArrowPnts[i].Y(),
276                          myUnitArrowPnts[i].Z()  );
277   myGrid->SetPoints(aPoints);
278   aPoints->Delete();
279
280   // connectivity of all arrows
281
282   const int theNbCells = ( theNbPoints - 1 ); // in one arrow
283   myGrid->Allocate( theNbCells  * nbArrows );
284   for ( int nP = 0, iA = 0; iA < nbArrows; ++iA, nP += theNbPoints )
285   {
286     vtkIdType conn[3] = { theNbPoints - 1 + nP, // arrow end
287                           theNbPoints - 3 + nP, // point on a circle
288                           nP };                 // point on a circle
289     for ( int i = 0; i < theNbCells-1; ++i )
290     {
291       myGrid->InsertNextCell( VTK_TRIANGLE, 3, conn );
292       conn[1] = conn[2];
293       conn[2] = conn[2] + 1;
294     }
295     conn[1] = theNbPoints - 2 + nP;
296     myGrid->InsertNextCell( VTK_LINE, 2, conn );
297   }
298
299   myLabelActors.resize( nbArrows, ( vtkTextActor*) NULL );
300   char label[] = "X";
301   if ( labels )
302     for ( int iP = 0, iA = 0; iA < nbArrows; ++iA )
303     {
304       label[0] = labels[iA];
305       vtkTextMapper* text = vtkTextMapper::New();
306       text->SetInput( label );
307       vtkCoordinate* coord = vtkCoordinate::New();
308
309       myLabelActors[iA] = vtkTextActor::New();
310       //myLabelActors[iA]->SetMapper( text );
311       myLabelActors[iA]->SetInput( label );
312       myLabelActors[iA]->SetTextScaleModeToNone();
313       myLabelActors[iA]->PickableOff();
314       myLabelActors[iA]->GetPositionCoordinate()->SetReferenceCoordinate( coord );
315
316       text->Delete();
317       coord->Delete();
318
319       myPreviewActor->GetRenderer()->AddActor(myLabelActors[iA]);
320     }
321 }
322
323 //================================================================================
324 /*!
325  * \brief Set data to show moved/rotated/scaled arrows
326  *  \param [in] axes - location and direction of the arrows
327  *  \param [in] length - length of arrows
328  */
329 //================================================================================
330
331 void SMESHGUI_MeshEditPreview::SetArrows( const gp_Ax1* axes,
332                                           double        length )
333 {
334   vtkPoints* aPoints = myGrid->GetPoints();
335
336   for ( int iP = 0, iA = 0; iA < myLabelActors.size(); ++iA )
337   {
338     gp_Trsf trsf;
339     trsf.SetTransformation( gp_Ax3( axes[iA].Location(), axes[iA].Direction() ), gp::XOY() );
340
341     for ( size_t i = 0; i < myUnitArrowPnts.size(); ++i, ++iP )
342     {
343       gp_Pnt p = myUnitArrowPnts[i].Scaled( gp::Origin(), length );
344       p.Transform( trsf );
345       aPoints->SetPoint( iP, p.X(), p.Y(), p.Z() );
346     }
347     if ( myLabelActors[iA] )
348       if ( vtkCoordinate* aCoord =
349            myLabelActors[iA]->GetPositionCoordinate()->GetReferenceCoordinate() )
350       {
351         double p[3];
352         aPoints->GetPoint( iP-1, p );
353         aCoord->SetValue( p );
354       }
355   }
356
357   myGrid->Modified();
358 }
359
360 //================================================================================
361 /*!
362  * \brief Set visibility
363  */
364 //================================================================================
365
366 void SMESHGUI_MeshEditPreview::SetVisibility (bool theVisibility)
367 {
368   myPreviewActor->SetVisibility(theVisibility);
369   for ( size_t iA = 0; iA < myLabelActors.size(); ++iA )
370     if ( myLabelActors[iA] )
371       myLabelActors[iA]->SetVisibility(theVisibility);
372   SMESH::RepaintCurrentView();
373 }
374
375 //================================================================================
376 /*!
377  * \brief Set preview color
378  */
379 //================================================================================
380
381 void SMESHGUI_MeshEditPreview::SetColor(double R, double G, double B)
382 {
383   myPreviewActor->SetColor( R, G, B );
384 }
385
386 //================================================================================
387 /*!
388  * \brief Get preview actor
389  */
390 //================================================================================
391
392 SALOME_Actor* SMESHGUI_MeshEditPreview::GetActor() const
393
394   return myPreviewActor;
395 }
396
397 //================================================================================
398 /*!
399  * \brief Returns the priewed vtkUnstructuredGrid
400  */
401 //================================================================================
402
403 vtkUnstructuredGrid* SMESHGUI_MeshEditPreview::GetGrid() const
404 {
405   return myGrid;
406 }