Back to index

salome-gui  6.5.0
SVTK_Actor.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 #include "SVTK_Actor.h"
00024 #include "SALOME_Actor.h"
00025 
00026 #include "SALOME_InteractiveObject.hxx"
00027 
00028 // VTK Includes
00029 #include <vtkObjectFactory.h>
00030 #include <vtkUnstructuredGrid.h>
00031 #include <vtkDataSetMapper.h>
00032 #include <vtkRenderer.h>
00033 
00034 #include <vtkCell.h>
00035 #define VTK_XVERSION (VTK_MAJOR_VERSION*10000+VTK_MINOR_VERSION*100+VTK_BUILD_VERSION)
00036 #if VTK_XVERSION > 50700
00037 #include <vtkPolyhedron.h>
00038 #endif
00039 #include <vtkPolyData.h>
00040 
00041 #include "Utils_SALOME_Exception.hxx"
00042 #include "utilities.h"
00043 
00044 static 
00045 void
00046 CopyPoints(vtkUnstructuredGrid* theGrid, vtkDataSet *theSourceDataSet)
00047 {
00048   vtkPoints *aPoints = vtkPoints::New();
00049   vtkIdType iEnd = theSourceDataSet->GetNumberOfPoints();
00050   aPoints->SetNumberOfPoints(iEnd);
00051   for(vtkIdType i = 0; i < iEnd; i++){
00052     aPoints->SetPoint(i,theSourceDataSet->GetPoint(i));
00053   }
00054   theGrid->SetPoints(aPoints);
00055   aPoints->Delete();
00056 }
00057 
00058 vtkStandardNewMacro(SVTK_Actor);
00059 
00063 SVTK_Actor
00064 ::SVTK_Actor():
00065   myUnstructuredGrid(vtkUnstructuredGrid::New())
00066 {
00067   myIsShaded = true;
00068   myIsResolveCoincidentTopology = false;
00069 
00070   Visibility = Pickable = false;
00071 
00072   myUnstructuredGrid->Delete();
00073   myUnstructuredGrid->Allocate();
00074 }
00075 
00076 void
00077 SVTK_Actor
00078 ::Initialize()
00079 {
00080   SetInput(GetSource());
00081 }
00082 
00083 void
00084 SVTK_Actor
00085 ::SetSource(vtkUnstructuredGrid* theUnstructuredGrid)
00086 {
00087   if(GetSource() == theUnstructuredGrid)
00088     return;
00089 
00090   myUnstructuredGrid = theUnstructuredGrid;
00091 
00092   SetInput(theUnstructuredGrid);
00093 }
00094 
00095 vtkUnstructuredGrid*
00096 SVTK_Actor
00097 ::GetSource()
00098 {
00099   return myUnstructuredGrid.GetPointer();
00100 }
00101 
00105 SVTK_Actor
00106 ::~SVTK_Actor()
00107 {
00108 }
00109 
00110 const TColStd_IndexedMapOfInteger&
00111 SVTK_Actor
00112 ::GetMapIndex() const
00113 {
00114   return myMapIndex;
00115 }
00116 
00117 void
00118 SVTK_Actor
00119 ::MapCells(SALOME_Actor* theMapActor,
00120            const TColStd_IndexedMapOfInteger& theMapIndex)
00121 {
00122   myUnstructuredGrid->Initialize();
00123   myUnstructuredGrid->Allocate();
00124 
00125   vtkDataSet *aSourceDataSet = theMapActor->GetInput();
00126   CopyPoints(GetSource(),aSourceDataSet);
00127 
00128   int aNbOfParts = theMapIndex.Extent();
00129   for(int ind = 1; ind <= aNbOfParts; ind++){
00130     int aPartId = theMapIndex( ind );
00131     if(vtkCell* aCell = theMapActor->GetElemCell(aPartId))
00132       {
00133 #if VTK_XVERSION > 50700
00134       if (aCell->GetCellType() != VTK_POLYHEDRON)
00135 #endif
00136         myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
00137 #if VTK_XVERSION > 50700
00138       else
00139         {
00140           vtkPolyhedron *polyhedron = dynamic_cast<vtkPolyhedron*>(aCell);
00141           if (!polyhedron)
00142             throw SALOME_Exception(LOCALIZED ("not a polyhedron"));
00143           vtkIdType *pts = polyhedron->GetFaces();
00144           myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),pts[0], pts+1);
00145         }
00146 #endif
00147       }
00148   }
00149 
00150   UnShrink();
00151   if(theMapActor->IsShrunk()){
00152     SetShrinkFactor(theMapActor->GetShrinkFactor());
00153     SetShrink();
00154   }
00155 
00156   myMapIndex = theMapIndex;
00157 }
00158 
00159 void 
00160 SVTK_Actor
00161 ::MapPoints(SALOME_Actor* theMapActor,
00162             const TColStd_IndexedMapOfInteger& theMapIndex)
00163 {
00164   myUnstructuredGrid->Initialize();
00165   myUnstructuredGrid->Allocate();
00166 
00167   if(int aNbOfParts = theMapIndex.Extent()){
00168     vtkPoints *aPoints = vtkPoints::New();
00169     aPoints->SetNumberOfPoints(aNbOfParts);
00170     for(vtkIdType i = 0; i < aNbOfParts; i++){
00171       int aPartId = theMapIndex( i+1 );
00172       if(vtkFloatingPointType* aCoord = theMapActor->GetNodeCoord(aPartId)){
00173         aPoints->SetPoint(i,aCoord);
00174         // Change the type from int to vtkIdType in order to avoid compilation errors while using VTK
00175         // from ParaView-3.4.0 compiled on 64-bit Debian platform with VTK_USE_64BIT_IDS = ON
00176         myUnstructuredGrid->InsertNextCell(VTK_VERTEX,(vtkIdType) 1,&i);
00177       }
00178     }
00179     myUnstructuredGrid->SetPoints(aPoints);
00180     aPoints->Delete();
00181   }
00182 
00183   UnShrink();
00184 
00185   myMapIndex = theMapIndex;
00186 }
00187 
00188 void
00189 SVTK_Actor
00190 ::MapEdge(SALOME_Actor* theMapActor,
00191           const TColStd_IndexedMapOfInteger& theMapIndex)
00192 {
00193   myUnstructuredGrid->Initialize();
00194   myUnstructuredGrid->Allocate();
00195 
00196   vtkDataSet *aSourceDataSet = theMapActor->GetInput();
00197   CopyPoints(GetSource(),aSourceDataSet);
00198 
00199 
00200   if(theMapIndex.Extent() == 2){
00201     int anEdgeId = theMapIndex(1) < 0 ? theMapIndex(1) : theMapIndex(2);
00202     int aCellId = theMapIndex(1) < 0 ? theMapIndex(2) : theMapIndex(1);
00203 
00204     if(aCellId > 0){
00205       if(vtkCell* aCell = theMapActor->GetElemCell(aCellId)){
00206         if(anEdgeId < 0){
00207           anEdgeId = -anEdgeId - 1;
00208           int aNbOfEdges = aCell->GetNumberOfEdges();
00209           if(0 <= anEdgeId || anEdgeId < aNbOfEdges){
00210             if(vtkCell* anEdge = aCell->GetEdge(anEdgeId))
00211               myUnstructuredGrid->InsertNextCell(VTK_LINE,anEdge->GetPointIds());
00212           }
00213         }
00214       }
00215     }
00216   }
00217 
00218   UnShrink();
00219   if(theMapActor->IsShrunk()){
00220     SetShrinkFactor(theMapActor->GetShrinkFactor());
00221     SetShrink();
00222   }
00223 
00224   myMapIndex = theMapIndex;
00225 }