1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File : SMESHGUI_MeshEditPreview.cxx
25 // Author : Open CASCADE S.A.S.
28 #include "SMESHGUI_MeshEditPreview.h"
30 #include "SMESHGUI_VTKUtils.h"
31 #include "SMESH_Actor.h"
32 #include "SMESH_ActorUtils.h"
34 // SALOME GUI includes
35 #include <SVTK_Renderer.h>
36 #include <SVTK_ViewWindow.h>
37 #include <VTKViewer_CellLocationsArray.h>
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>
56 #include <SALOMEconfig.h>
57 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
61 //================================================================================
65 //================================================================================
67 SMESHGUI_MeshEditPreview::SMESHGUI_MeshEditPreview(SVTK_ViewWindow* theViewWindow):
68 myViewWindow(theViewWindow)
70 myGrid = vtkUnstructuredGrid::New();
72 // Create and display actor
73 vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
74 aMapper->SetInputData( myGrid );
76 myPreviewActor = SALOME_Actor::New();
77 myPreviewActor->SetInfinitive(true);
78 myPreviewActor->VisibilityOn();
79 myPreviewActor->PickableOff();
81 double aFactor,aUnits;
82 myPreviewActor->SetResolveCoincidentTopology(true);
83 myPreviewActor->GetPolygonOffsetParameters(aFactor,aUnits);
84 myPreviewActor->SetPolygonOffsetParameters(aFactor,0.2*aUnits);
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] );
90 myPreviewActor->SetMapper( aMapper );
93 myViewWindow->AddActor(myPreviewActor);
96 //================================================================================
100 //================================================================================
102 SMESHGUI_MeshEditPreview::~SMESHGUI_MeshEditPreview()
106 for ( size_t iA = 0; iA < myLabelActors.size(); ++iA )
107 if ( myLabelActors[iA] )
109 myPreviewActor->GetRenderer()->RemoveActor( myLabelActors[iA] );
110 myLabelActors[iA]->Delete();
113 myViewWindow->RemoveActor(myPreviewActor);
114 myPreviewActor->Delete();
117 //================================================================================
119 * \brief Returns vtk cell type
121 //================================================================================
123 vtkIdType getCellType( const SMDSAbs_ElementType theType,
125 const int theNbNodes )
129 case SMDSAbs_Ball: return VTK_VERTEX;
130 case SMDSAbs_Node: return VTK_VERTEX;
132 if( theNbNodes == 2 ) return VTK_LINE;
133 else if ( theNbNodes == 3 ) return VTK_QUADRATIC_EDGE;
134 else return VTK_EMPTY_CELL;
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;
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;
159 default: return VTK_EMPTY_CELL;
163 //================================================================================
165 * \brief Set preview data
167 //================================================================================
169 void SMESHGUI_MeshEditPreview::SetData (const SMESH::MeshPreviewStruct* previewData)
172 const SMESH::nodes_array& aNodesXYZ = previewData->nodesXYZ;
173 vtkPoints* aPoints = vtkPoints::New();
174 aPoints->SetNumberOfPoints(aNodesXYZ.length());
176 for ( int i = 0; i < aNodesXYZ.length(); i++ ) {
177 aPoints->SetPoint( i, aNodesXYZ[i].x, aNodesXYZ[i].y, aNodesXYZ[i].z );
179 myGrid->SetPoints(aPoints);
184 const SMESH::long_array& anElemConnectivity = previewData->elementConnectivities;
185 const SMESH::types_array& anElemTypes = previewData->elementTypes;
187 vtkIdType aCellsSize = anElemConnectivity.length() + anElemTypes.length();
188 vtkIdType aNbCells = anElemTypes.length();
190 vtkCellArray* aConnectivity = vtkCellArray::New();
191 aConnectivity->Allocate( aCellsSize, 0 );
193 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
194 aCellTypesArray->SetNumberOfComponents( 1 );
195 aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
197 vtkIdList *anIdList = vtkIdList::New();
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 );
206 for ( vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++ ){
207 anIdList->SetId( aNodeId, anElemConnectivity[aNodePos] );
211 aConnectivity->InsertNextCell( anIdList );
212 aCellTypesArray->InsertNextValue( getCellType( aType,
213 anElemTypes[i].isPoly,
218 // Insert cells in grid
219 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
220 aCellLocationsArray->SetNumberOfComponents( 1 );
221 aCellLocationsArray->SetNumberOfTuples( aNbCells );
223 aConnectivity->InitTraversal();
224 for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
225 aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
227 myGrid->SetCells( aCellTypesArray, aCellLocationsArray, aConnectivity );
229 myPreviewActor->GetMapper()->Update();
231 aCellTypesArray->Delete();
232 aCellLocationsArray->Delete();
233 aConnectivity->Delete();
238 //================================================================================
240 * \brief Set shape of an arrow of a unit length and nb of arrows
242 //================================================================================
244 void SMESHGUI_MeshEditPreview::SetArrowShapeAndNb( int nbArrows,
250 const int theNbPoints = 10; // in one arrow
251 myUnitArrowPnts.reserve( theNbPoints );
252 myUnitArrowPnts.clear();
256 for ( int i = 0; i < theNbPoints - 2; ++i )
258 double angle = i * 2 * M_PI / ( theNbPoints - 2 );
259 myUnitArrowPnts.push_back( gp_Pnt( headRadius * Cos( angle ),
260 headRadius * Sin( angle ),
263 myUnitArrowPnts.push_back( gp_Pnt( 0, 0, start ));
264 myUnitArrowPnts.push_back( gp_Pnt( 0, 0, 1 ));
267 // nodes of all arrows
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);
280 // connectivity of all arrows
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 )
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 )
291 myGrid->InsertNextCell( VTK_TRIANGLE, 3, conn );
293 conn[2] = conn[2] + 1;
295 conn[1] = theNbPoints - 2 + nP;
296 myGrid->InsertNextCell( VTK_LINE, 2, conn );
299 myLabelActors.resize( nbArrows, ( vtkTextActor*) NULL );
302 for ( int iP = 0, iA = 0; iA < nbArrows; ++iA )
304 label[0] = labels[iA];
305 vtkTextMapper* text = vtkTextMapper::New();
306 text->SetInput( label );
307 vtkCoordinate* coord = vtkCoordinate::New();
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 );
319 myPreviewActor->GetRenderer()->AddActor(myLabelActors[iA]);
323 //================================================================================
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
329 //================================================================================
331 void SMESHGUI_MeshEditPreview::SetArrows( const gp_Ax1* axes,
334 vtkPoints* aPoints = myGrid->GetPoints();
336 for ( int iP = 0, iA = 0; iA < myLabelActors.size(); ++iA )
339 trsf.SetTransformation( gp_Ax3( axes[iA].Location(), axes[iA].Direction() ), gp::XOY() );
341 for ( size_t i = 0; i < myUnitArrowPnts.size(); ++i, ++iP )
343 gp_Pnt p = myUnitArrowPnts[i].Scaled( gp::Origin(), length );
345 aPoints->SetPoint( iP, p.X(), p.Y(), p.Z() );
347 if ( myLabelActors[iA] )
348 if ( vtkCoordinate* aCoord =
349 myLabelActors[iA]->GetPositionCoordinate()->GetReferenceCoordinate() )
352 aPoints->GetPoint( iP-1, p );
353 aCoord->SetValue( p );
360 //================================================================================
362 * \brief Set visibility
364 //================================================================================
366 void SMESHGUI_MeshEditPreview::SetVisibility (bool theVisibility)
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();
375 //================================================================================
377 * \brief Set preview color
379 //================================================================================
381 void SMESHGUI_MeshEditPreview::SetColor(double R, double G, double B)
383 myPreviewActor->SetColor( R, G, B );
386 //================================================================================
388 * \brief Get preview actor
390 //================================================================================
392 SALOME_Actor* SMESHGUI_MeshEditPreview::GetActor() const
394 return myPreviewActor;
397 //================================================================================
399 * \brief Returns the priewed vtkUnstructuredGrid
401 //================================================================================
403 vtkUnstructuredGrid* SMESHGUI_MeshEditPreview::GetGrid() const