Salome HOME
0155b629a94f532e04538253dc08979ba957582e
[modules/gui.git] / src / SVTK / SVTK_Actor.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SVTK_Actor.h"
24 #include "SALOME_Actor.h"
25 #include "SVTK_Utils.h"
26
27 #include "SALOME_InteractiveObject.hxx"
28
29 // VTK Includes
30 #include <vtkObjectFactory.h>
31 #include <vtkUnstructuredGrid.h>
32 #include <vtkDataSetMapper.h>
33 #include <vtkRenderer.h>
34
35 #include <vtkCell.h>
36 #define VTK_XVERSION (VTK_MAJOR_VERSION*10000+VTK_MINOR_VERSION*100+VTK_BUILD_VERSION)
37 #if VTK_XVERSION > 50700
38 #include <vtkPolyhedron.h>
39 #endif
40 #include <vtkPolyData.h>
41
42 #include "Utils_SALOME_Exception.hxx"
43 #include "utilities.h"
44
45
46 vtkStandardNewMacro(SVTK_Actor);
47
48 /*!
49   Constructor
50 */
51 SVTK_Actor
52 ::SVTK_Actor():
53   myUnstructuredGrid(vtkUnstructuredGrid::New())
54 {
55   myIsShaded = true;
56   myIsResolveCoincidentTopology = false;
57
58   Visibility = Pickable = false;
59
60   myUnstructuredGrid->Delete();
61   myUnstructuredGrid->Allocate();
62 }
63
64 void
65 SVTK_Actor
66 ::Initialize()
67 {
68   SetInput(GetSource());
69 }
70
71 void
72 SVTK_Actor
73 ::SetSource(vtkUnstructuredGrid* theUnstructuredGrid)
74 {
75   if(GetSource() == theUnstructuredGrid)
76     return;
77
78   myUnstructuredGrid = theUnstructuredGrid;
79
80   SetInput(theUnstructuredGrid);
81 }
82
83 vtkUnstructuredGrid*
84 SVTK_Actor
85 ::GetSource()
86 {
87   return myUnstructuredGrid.GetPointer();
88 }
89
90 /*!
91   Destructor
92 */
93 SVTK_Actor
94 ::~SVTK_Actor()
95 {
96 }
97
98 const TColStd_IndexedMapOfInteger&
99 SVTK_Actor
100 ::GetMapIndex() const
101 {
102   return myMapIndex;
103 }
104
105 void
106 SVTK_Actor
107 ::MapCells(SALOME_Actor* theMapActor,
108            const TColStd_IndexedMapOfInteger& theMapIndex)
109 {
110   myUnstructuredGrid->Initialize();
111   myUnstructuredGrid->Allocate();
112
113   vtkDataSet *aSourceDataSet = theMapActor->GetInput();
114   SVTK::CopyPoints(GetSource(),aSourceDataSet);
115
116   int aNbOfParts = theMapIndex.Extent();
117   for(int ind = 1; ind <= aNbOfParts; ind++){
118     int aPartId = theMapIndex( ind );
119     if(vtkCell* aCell = theMapActor->GetElemCell(aPartId))
120       {
121 #if VTK_XVERSION > 50700
122       if (aCell->GetCellType() != VTK_POLYHEDRON)
123 #endif
124         myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
125 #if VTK_XVERSION > 50700
126       else
127         {
128           vtkPolyhedron *polyhedron = dynamic_cast<vtkPolyhedron*>(aCell);
129           if (!polyhedron)
130             throw SALOME_Exception(LOCALIZED ("not a polyhedron"));
131           vtkIdType *pts = polyhedron->GetFaces();
132           myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),pts[0], pts+1);
133         }
134 #endif
135       }
136   }
137
138   UnShrink();
139   if(theMapActor->IsShrunk()){
140     SetShrinkFactor(theMapActor->GetShrinkFactor());
141     SetShrink();
142   }
143
144   myMapIndex = theMapIndex;
145 }
146
147 void 
148 SVTK_Actor
149 ::MapPoints(SALOME_Actor* theMapActor,
150             const TColStd_IndexedMapOfInteger& theMapIndex)
151 {
152   myUnstructuredGrid->Initialize();
153   myUnstructuredGrid->Allocate();
154
155   if(int aNbOfParts = theMapIndex.Extent()){
156     vtkPoints *aPoints = vtkPoints::New();
157     aPoints->SetNumberOfPoints(aNbOfParts);
158     for(vtkIdType i = 0; i < aNbOfParts; i++){
159       int aPartId = theMapIndex( i+1 );
160       if(vtkFloatingPointType* aCoord = theMapActor->GetNodeCoord(aPartId)){
161         aPoints->SetPoint(i,aCoord);
162         // Change the type from int to vtkIdType in order to avoid compilation errors while using VTK
163         // from ParaView-3.4.0 compiled on 64-bit Debian platform with VTK_USE_64BIT_IDS = ON
164         myUnstructuredGrid->InsertNextCell(VTK_VERTEX,(vtkIdType) 1,&i);
165       }
166     }
167     myUnstructuredGrid->SetPoints(aPoints);
168     aPoints->Delete();
169   }
170
171   UnShrink();
172
173   myMapIndex = theMapIndex;
174 }
175
176 void
177 SVTK_Actor
178 ::MapEdge(SALOME_Actor* theMapActor,
179           const TColStd_IndexedMapOfInteger& theMapIndex)
180 {
181   myUnstructuredGrid->Initialize();
182   myUnstructuredGrid->Allocate();
183
184   vtkDataSet *aSourceDataSet = theMapActor->GetInput();
185   SVTK::CopyPoints(GetSource(),aSourceDataSet);
186
187
188   if(theMapIndex.Extent() == 2){
189     int anEdgeId = theMapIndex(1) < 0 ? theMapIndex(1) : theMapIndex(2);
190     int aCellId = theMapIndex(1) < 0 ? theMapIndex(2) : theMapIndex(1);
191
192     if(aCellId > 0){
193       if(vtkCell* aCell = theMapActor->GetElemCell(aCellId)){
194         if(anEdgeId < 0){
195           anEdgeId = -anEdgeId - 1;
196           int aNbOfEdges = aCell->GetNumberOfEdges();
197           if(0 <= anEdgeId || anEdgeId < aNbOfEdges){
198             if(vtkCell* anEdge = aCell->GetEdge(anEdgeId))
199               myUnstructuredGrid->InsertNextCell(VTK_LINE,anEdge->GetPointIds());
200           }
201         }
202       }
203     }
204   }
205
206   UnShrink();
207   if(theMapActor->IsShrunk()){
208     SetShrinkFactor(theMapActor->GetShrinkFactor());
209     SetShrink();
210   }
211
212   myMapIndex = theMapIndex;
213 }
214
215 /*!
216   To publish the actor an all its internal devices
217 */
218 void
219 SVTK_Actor
220 ::AddToRender(vtkRenderer* theRenderer)
221 {
222   theRenderer->AddActor(this);
223 }
224
225 void
226 SVTK_Actor
227 ::RemoveFromRender(vtkRenderer* theRenderer) 
228 {
229   theRenderer->RemoveActor(this);
230 }