1 // Copyright (C) 2007-2013 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.
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"
32 #include <SMESH_Actor.h>
33 #include <SMESH_ActorUtils.h>
35 // SALOME GUI includes
36 #include <VTKViewer_CellLocationsArray.h>
37 #include <SVTK_ViewWindow.h>
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>
53 #include <SALOMEconfig.h>
54 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
58 //================================================================================
62 //================================================================================
64 SMESHGUI_MeshEditPreview::SMESHGUI_MeshEditPreview(SVTK_ViewWindow* theViewWindow):
65 myViewWindow(theViewWindow)
67 myGrid = vtkUnstructuredGrid::New();
69 // Create and display actor
70 vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
71 aMapper->SetInputData( myGrid );
73 myPreviewActor = SALOME_Actor::New();
74 myPreviewActor->SetInfinitive(true);
75 myPreviewActor->VisibilityOn();
76 myPreviewActor->PickableOff();
78 double aFactor,aUnits;
79 myPreviewActor->SetResolveCoincidentTopology(true);
80 myPreviewActor->GetPolygonOffsetParameters(aFactor,aUnits);
81 myPreviewActor->SetPolygonOffsetParameters(aFactor,0.2*aUnits);
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] );
87 myPreviewActor->SetMapper( aMapper );
90 myViewWindow->AddActor(myPreviewActor);
94 //================================================================================
98 //================================================================================
100 SMESHGUI_MeshEditPreview::~SMESHGUI_MeshEditPreview()
104 myViewWindow->RemoveActor(myPreviewActor);
105 myPreviewActor->Delete();
109 //================================================================================
111 * \brief Returns vtk cell type
113 //================================================================================
115 vtkIdType getCellType( const SMDSAbs_ElementType theType,
117 const int theNbNodes )
121 case SMDSAbs_Node: return VTK_VERTEX;
123 if( theNbNodes == 2 ) return VTK_LINE;
124 else if ( theNbNodes == 3 ) return VTK_QUADRATIC_EDGE;
125 else return VTK_EMPTY_CELL;
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;
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;
150 default: return VTK_EMPTY_CELL;
154 //================================================================================
156 * \brief Set preview data
158 //================================================================================
160 void SMESHGUI_MeshEditPreview::SetData (const SMESH::MeshPreviewStruct* previewData)
163 const SMESH::nodes_array& aNodesXYZ = previewData->nodesXYZ;
164 vtkPoints* aPoints = vtkPoints::New();
165 aPoints->SetNumberOfPoints(aNodesXYZ.length());
167 for ( int i = 0; i < aNodesXYZ.length(); i++ ) {
168 aPoints->SetPoint( i, aNodesXYZ[i].x, aNodesXYZ[i].y, aNodesXYZ[i].z );
170 myGrid->SetPoints(aPoints);
175 const SMESH::long_array& anElemConnectivity = previewData->elementConnectivities;
176 const SMESH::types_array& anElemTypes = previewData->elementTypes;
178 vtkIdType aCellsSize = anElemConnectivity.length() + anElemTypes.length();
179 vtkIdType aNbCells = anElemTypes.length();
181 vtkCellArray* aConnectivity = vtkCellArray::New();
182 aConnectivity->Allocate( aCellsSize, 0 );
184 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
185 aCellTypesArray->SetNumberOfComponents( 1 );
186 aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
188 vtkIdList *anIdList = vtkIdList::New();
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 );
197 for ( vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++ ){
198 anIdList->SetId( aNodeId, anElemConnectivity[aNodePos] );
202 aConnectivity->InsertNextCell( anIdList );
203 aCellTypesArray->InsertNextValue( getCellType( aType,
204 anElemTypes[i].isPoly,
209 // Insert cells in grid
210 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
211 aCellLocationsArray->SetNumberOfComponents( 1 );
212 aCellLocationsArray->SetNumberOfTuples( aNbCells );
214 aConnectivity->InitTraversal();
215 for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
216 aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
218 myGrid->SetCells( aCellTypesArray, aCellLocationsArray, aConnectivity );
220 myPreviewActor->GetMapper()->Update();
222 aCellTypesArray->Delete();
223 aCellLocationsArray->Delete();
224 aConnectivity->Delete();
229 //================================================================================
231 * \brief Set shape of an arrow of a unit length and nb of arrows
233 //================================================================================
235 void SMESHGUI_MeshEditPreview::SetArrowShapeAndNb( int nbArrows,
240 const int theNbPoints = 10; // in one arrow
241 myUnitArrowPnts.reserve( theNbPoints );
242 myUnitArrowPnts.clear();
246 for ( int i = 0; i < theNbPoints - 2; ++i )
248 double angle = i * 2 * M_PI / ( theNbPoints - 2 );
249 myUnitArrowPnts.push_back( gp_Pnt( headRadius * Cos( angle ),
250 headRadius * Sin( angle ),
253 myUnitArrowPnts.push_back( gp_Pnt( 0, 0, start ));
254 myUnitArrowPnts.push_back( gp_Pnt( 0, 0, 1 ));
257 // nodes of all arrows
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);
270 // connectivity of all arrows
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 )
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 )
281 myGrid->InsertNextCell( VTK_TRIANGLE, 3, conn );
283 conn[2] = conn[2] + 1;
285 conn[1] = theNbPoints - 2 + nP;
286 myGrid->InsertNextCell( VTK_LINE, 2, conn );
289 myNbArrows = nbArrows;
292 //================================================================================
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
298 //================================================================================
300 void SMESHGUI_MeshEditPreview::SetArrows( const gp_Ax1* axes,
303 vtkPoints* aPoints = myGrid->GetPoints();
305 for ( int iP = 0, iA = 0; iA < myNbArrows; ++iA )
308 trsf.SetTransformation( gp_Ax3( axes[iA].Location(), axes[iA].Direction() ), gp::XOY() );
310 for ( size_t i = 0; i < myUnitArrowPnts.size(); ++i, ++iP )
312 gp_Pnt p = myUnitArrowPnts[i].Scaled( gp::Origin(), length );
314 aPoints->SetPoint( iP, p.X(), p.Y(), p.Z() );
321 //================================================================================
323 * \brief Set visibility
325 //================================================================================
327 void SMESHGUI_MeshEditPreview::SetVisibility (bool theVisibility)
329 myPreviewActor->SetVisibility(theVisibility);
330 SMESH::RepaintCurrentView();
333 //================================================================================
335 * \brief Set preview color
337 //================================================================================
339 void SMESHGUI_MeshEditPreview::SetColor(double R, double G, double B)
341 myPreviewActor->SetColor( R, G, B );
344 //================================================================================
346 * \brief Get preview actor
348 //================================================================================
350 SALOME_Actor* SMESHGUI_MeshEditPreview::GetActor() const
352 return myPreviewActor;
355 //================================================================================
357 * \brief Returns the priewed vtkUnstructuredGrid
359 //================================================================================
361 vtkUnstructuredGrid* SMESHGUI_MeshEditPreview::GetGrid() const