1 // Copyright (C) 2007-2011 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)
56 //================================================================================
60 //================================================================================
62 SMESHGUI_MeshEditPreview::SMESHGUI_MeshEditPreview(SVTK_ViewWindow* theViewWindow):
63 myViewWindow(theViewWindow)
65 myGrid = vtkUnstructuredGrid::New();
67 // Create and display actor
68 vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
69 aMapper->SetInput( myGrid );
71 myPreviewActor = SALOME_Actor::New();
72 myPreviewActor->SetInfinitive(true);
73 myPreviewActor->VisibilityOn();
74 myPreviewActor->PickableOff();
76 vtkFloatingPointType anRGB[3];
77 SMESH::GetColor( "SMESH", "selection_element_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
78 SetColor( anRGB[0], anRGB[1], anRGB[2] );
80 myPreviewActor->SetMapper( aMapper );
83 myViewWindow->AddActor(myPreviewActor);
87 //================================================================================
91 //================================================================================
93 SMESHGUI_MeshEditPreview::~SMESHGUI_MeshEditPreview()
97 myViewWindow->RemoveActor(myPreviewActor);
98 myPreviewActor->Delete();
102 //================================================================================
104 * \brief Returns vtk cell type
106 //================================================================================
108 vtkIdType getCellType( const SMDSAbs_ElementType theType,
110 const int theNbNodes )
114 case SMDSAbs_Node: return VTK_VERTEX;
116 if( theNbNodes == 2 ) return VTK_LINE;
117 else if ( theNbNodes == 3 ) return VTK_QUADRATIC_EDGE;
118 else return VTK_EMPTY_CELL;
121 if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
122 else if ( theNbNodes == 3 ) return VTK_TRIANGLE;
123 else if ( theNbNodes == 4 ) return VTK_QUAD;
124 else if ( theNbNodes == 6 ) return VTK_QUADRATIC_TRIANGLE;
125 else if ( theNbNodes == 8 ) return VTK_QUADRATIC_QUAD;
126 else if ( theNbNodes == 9 ) return VTK_BIQUADRATIC_QUAD;
127 else return VTK_EMPTY_CELL;
130 if (thePoly && theNbNodes>3 ) return VTK_CONVEX_POINT_SET;
131 else if ( theNbNodes == 4 ) return VTK_TETRA;
132 else if ( theNbNodes == 5 ) return VTK_PYRAMID;
133 else if ( theNbNodes == 6 ) return VTK_WEDGE;
134 else if ( theNbNodes == 8 ) return VTK_HEXAHEDRON;
135 else if ( theNbNodes == 10 ) return VTK_QUADRATIC_TETRA;
136 else if ( theNbNodes == 20 ) return VTK_QUADRATIC_HEXAHEDRON;
137 else if ( theNbNodes == 27 ) return VTK_TRIQUADRATIC_HEXAHEDRON;
138 else if ( theNbNodes == 15 ) return VTK_QUADRATIC_WEDGE;
139 else if ( theNbNodes == 13 ) return VTK_QUADRATIC_PYRAMID;//VTK_CONVEX_POINT_SET;
140 else return VTK_EMPTY_CELL;
142 default: return VTK_EMPTY_CELL;
146 //================================================================================
148 * \brief Set preview data
150 //================================================================================
152 void SMESHGUI_MeshEditPreview::SetData (const SMESH::MeshPreviewStruct* previewData)
155 const SMESH::nodes_array& aNodesXYZ = previewData->nodesXYZ;
156 vtkPoints* aPoints = vtkPoints::New();
157 aPoints->SetNumberOfPoints(aNodesXYZ.length());
159 for ( int i = 0; i < aNodesXYZ.length(); i++ ) {
160 aPoints->SetPoint( i, aNodesXYZ[i].x, aNodesXYZ[i].y, aNodesXYZ[i].z );
162 myGrid->SetPoints(aPoints);
167 const SMESH::long_array& anElemConnectivity = previewData->elementConnectivities;
168 const SMESH::types_array& anElemTypes = previewData->elementTypes;
170 vtkIdType aCellsSize = anElemConnectivity.length() + anElemTypes.length();
171 vtkIdType aNbCells = anElemTypes.length();
173 vtkCellArray* aConnectivity = vtkCellArray::New();
174 aConnectivity->Allocate( aCellsSize, 0 );
176 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
177 aCellTypesArray->SetNumberOfComponents( 1 );
178 aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
180 vtkIdList *anIdList = vtkIdList::New();
183 for ( int i = 0; i < anElemTypes.length(); i++ ) {
184 const SMESH::ElementSubType& anElementSubType = anElemTypes[i];
185 SMDSAbs_ElementType aType = SMDSAbs_ElementType(anElementSubType.SMDS_ElementType);
186 vtkIdType aNbNodes = anElementSubType.nbNodesInElement;
187 anIdList->SetNumberOfIds( aNbNodes );
189 for ( vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++ ){
190 anIdList->SetId( aNodeId, anElemConnectivity[aNodePos] );
194 aConnectivity->InsertNextCell( anIdList );
195 aCellTypesArray->InsertNextValue( getCellType( aType,
196 anElemTypes[i].isPoly,
201 // Insert cells in grid
202 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
203 aCellLocationsArray->SetNumberOfComponents( 1 );
204 aCellLocationsArray->SetNumberOfTuples( aNbCells );
206 aConnectivity->InitTraversal();
207 for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
208 aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
210 myGrid->SetCells( aCellTypesArray, aCellLocationsArray, aConnectivity );
212 myPreviewActor->GetMapper()->Update();
214 aCellTypesArray->Delete();
215 aCellLocationsArray->Delete();
216 aConnectivity->Delete();
221 //================================================================================
223 * \brief Set visibility
225 //================================================================================
227 void SMESHGUI_MeshEditPreview::SetVisibility (bool theVisibility)
229 myPreviewActor->SetVisibility(theVisibility);
230 SMESH::RepaintCurrentView();
233 //================================================================================
235 * \brief Set preview color
237 //================================================================================
239 void SMESHGUI_MeshEditPreview::SetColor(double R, double G, double B)
241 myPreviewActor->SetColor( R, G, B );
244 //================================================================================
246 * \brief Get preview actor
248 //================================================================================
249 SALOME_Actor* SMESHGUI_MeshEditPreview::GetActor() const
251 return myPreviewActor;