Back to index

salome-smesh  6.5.0
SMESHGUI_MeshEditPreview.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // SMESH SMESHGUI : GUI for SMESH component
00024 // File   : SMESHGUI_MeshEditPreview.cxx
00025 // Author : Open CASCADE S.A.S.
00026 // SMESH includes
00027 //
00028 #include "SMESHGUI_MeshEditPreview.h"
00029 
00030 #include "SMESHGUI_VTKUtils.h"
00031 
00032 #include <SMESH_Actor.h>
00033 #include <SMESH_ActorUtils.h>
00034 
00035 // SALOME GUI includes
00036 #include <VTKViewer_CellLocationsArray.h>
00037 #include <SVTK_ViewWindow.h>
00038 
00039 // VTK includes
00040 #include <vtkPoints.h>
00041 #include <vtkIdList.h>
00042 #include <vtkCellArray.h>
00043 #include <vtkUnsignedCharArray.h>
00044 #include <vtkUnstructuredGrid.h>
00045 #include <vtkUnstructuredGridWriter.h>
00046 #include <vtkDataSetMapper.h>
00047 #include <vtkProperty.h>
00048 
00049 // Qt includes
00050 #include <QColor>
00051 
00052 // IDL includes
00053 #include <SALOMEconfig.h>
00054 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
00055 
00056 //================================================================================
00060 //================================================================================
00061 
00062 SMESHGUI_MeshEditPreview::SMESHGUI_MeshEditPreview(SVTK_ViewWindow* theViewWindow):
00063   myViewWindow(theViewWindow)
00064 {
00065   myGrid = vtkUnstructuredGrid::New();
00066 
00067   // Create and display actor
00068   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
00069   aMapper->SetInput( myGrid );
00070 
00071   myPreviewActor = SALOME_Actor::New();
00072   myPreviewActor->SetInfinitive(true);
00073   myPreviewActor->VisibilityOn();
00074   myPreviewActor->PickableOff();
00075 
00076   vtkFloatingPointType aFactor,aUnits;
00077   myPreviewActor->SetResolveCoincidentTopology(true);
00078   myPreviewActor->GetPolygonOffsetParameters(aFactor,aUnits);
00079   myPreviewActor->SetPolygonOffsetParameters(aFactor,0.2*aUnits);
00080 
00081   vtkFloatingPointType anRGB[3];
00082   SMESH::GetColor( "SMESH", "selection_element_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
00083   SetColor( anRGB[0], anRGB[1], anRGB[2] );
00084 
00085   myPreviewActor->SetMapper( aMapper );
00086   aMapper->Delete();
00087 
00088   myViewWindow->AddActor(myPreviewActor);
00089 
00090 }
00091 
00092 //================================================================================
00096 //================================================================================
00097 
00098 SMESHGUI_MeshEditPreview::~SMESHGUI_MeshEditPreview()
00099 {
00100   myGrid->Delete();
00101 
00102   myViewWindow->RemoveActor(myPreviewActor);
00103   myPreviewActor->Delete();
00104 
00105 }
00106 
00107 //================================================================================
00111 //================================================================================
00112 
00113 vtkIdType getCellType( const SMDSAbs_ElementType theType,
00114                        const bool thePoly,
00115                        const int theNbNodes )
00116 {
00117   switch( theType ) 
00118   {
00119   case SMDSAbs_Node:              return VTK_VERTEX;
00120   case SMDSAbs_Edge: 
00121     if( theNbNodes == 2 )         return VTK_LINE;
00122     else if ( theNbNodes == 3 )   return VTK_QUADRATIC_EDGE;
00123     else return VTK_EMPTY_CELL;
00124 
00125   case SMDSAbs_Face  :
00126     if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
00127     else if ( theNbNodes == 3 )   return VTK_TRIANGLE;
00128     else if ( theNbNodes == 4 )   return VTK_QUAD;
00129     else if ( theNbNodes == 6 )   return VTK_QUADRATIC_TRIANGLE;
00130     else if ( theNbNodes == 8 )   return VTK_QUADRATIC_QUAD;
00131     else if ( theNbNodes == 9 )   return VTK_BIQUADRATIC_QUAD;
00132     else return VTK_EMPTY_CELL;
00133 
00134   case SMDSAbs_Volume:
00135     if (thePoly && theNbNodes>3 ) return VTK_CONVEX_POINT_SET;
00136     else if ( theNbNodes == 4 )   return VTK_TETRA;
00137     else if ( theNbNodes == 5 )   return VTK_PYRAMID;
00138     else if ( theNbNodes == 6 )   return VTK_WEDGE;
00139     else if ( theNbNodes == 8 )   return VTK_HEXAHEDRON;
00140     else if ( theNbNodes == 10 )  return VTK_QUADRATIC_TETRA;
00141     else if ( theNbNodes == 20 )  return VTK_QUADRATIC_HEXAHEDRON;
00142     else if ( theNbNodes == 27 )  return VTK_TRIQUADRATIC_HEXAHEDRON;
00143     else if ( theNbNodes == 15  ) return VTK_QUADRATIC_WEDGE;
00144     else if ( theNbNodes == 13  ) return VTK_QUADRATIC_PYRAMID;//VTK_CONVEX_POINT_SET;
00145     else return VTK_EMPTY_CELL;
00146 
00147   default: return VTK_EMPTY_CELL;
00148   }
00149 }
00150 
00151 //================================================================================
00155 //================================================================================
00156 
00157 void SMESHGUI_MeshEditPreview::SetData (const SMESH::MeshPreviewStruct* previewData)
00158 {
00159   // Create points
00160   const SMESH::nodes_array& aNodesXYZ = previewData->nodesXYZ;
00161   vtkPoints* aPoints = vtkPoints::New();
00162   aPoints->SetNumberOfPoints(aNodesXYZ.length());
00163 
00164   for ( int i = 0; i < aNodesXYZ.length(); i++ ) {
00165     aPoints->SetPoint( i, aNodesXYZ[i].x, aNodesXYZ[i].y, aNodesXYZ[i].z );
00166   }
00167   myGrid->SetPoints(aPoints);
00168 
00169   aPoints->Delete();
00170 
00171   // Create cells
00172   const SMESH::long_array&  anElemConnectivity = previewData->elementConnectivities;
00173   const SMESH::types_array& anElemTypes = previewData->elementTypes;
00174 
00175   vtkIdType aCellsSize = anElemConnectivity.length() + anElemTypes.length();
00176   vtkIdType aNbCells = anElemTypes.length();
00177 
00178   vtkCellArray* aConnectivity = vtkCellArray::New();
00179   aConnectivity->Allocate( aCellsSize, 0 );
00180 
00181   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
00182   aCellTypesArray->SetNumberOfComponents( 1 );
00183   aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
00184 
00185   vtkIdList *anIdList = vtkIdList::New();
00186   int aNodePos = 0;
00187 
00188   for ( int i = 0; i < anElemTypes.length(); i++ ) {
00189     const SMESH::ElementSubType& anElementSubType = anElemTypes[i];
00190     SMDSAbs_ElementType aType = SMDSAbs_ElementType(anElementSubType.SMDS_ElementType);
00191     vtkIdType aNbNodes = anElementSubType.nbNodesInElement;
00192     anIdList->SetNumberOfIds( aNbNodes );
00193 
00194     for ( vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++ ){
00195       anIdList->SetId( aNodeId, anElemConnectivity[aNodePos] );
00196       aNodePos++;
00197     }
00198 
00199     aConnectivity->InsertNextCell( anIdList );
00200     aCellTypesArray->InsertNextValue( getCellType( aType,
00201                                                    anElemTypes[i].isPoly, 
00202                                                    aNbNodes ) );
00203   }
00204   anIdList->Delete();
00205 
00206   // Insert cells in grid
00207   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
00208   aCellLocationsArray->SetNumberOfComponents( 1 );
00209   aCellLocationsArray->SetNumberOfTuples( aNbCells );
00210 
00211   aConnectivity->InitTraversal();
00212   for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
00213     aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
00214 
00215   myGrid->SetCells( aCellTypesArray, aCellLocationsArray, aConnectivity );
00216 
00217   myPreviewActor->GetMapper()->Update();
00218 
00219   aCellTypesArray->Delete();
00220   aCellLocationsArray->Delete();
00221   aConnectivity->Delete();
00222 
00223   SetVisibility(true);
00224 }
00225 
00226 //================================================================================
00230 //================================================================================
00231 
00232 void SMESHGUI_MeshEditPreview::SetVisibility (bool theVisibility)
00233 {
00234   myPreviewActor->SetVisibility(theVisibility);
00235   SMESH::RepaintCurrentView();
00236 }
00237 
00238 //================================================================================
00242 //================================================================================
00243 
00244 void SMESHGUI_MeshEditPreview::SetColor(double R, double G, double B)
00245 {
00246   myPreviewActor->SetColor( R, G, B );
00247 }
00248 
00249 //================================================================================
00253 //================================================================================
00254 SALOME_Actor* SMESHGUI_MeshEditPreview::GetActor() const
00255 { 
00256   return myPreviewActor;
00257 }