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