Salome HOME
Updated copyright comment
[modules/gui.git] / src / SVTK / SVTK_Actor.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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 SVTK_TIndexedMapOfVtkId&
100 SVTK_Actor
101 ::GetMapIndex() const
102 {
103   return myMapIndex;
104 }
105
106
107 const SVTK_IndexedMapOfVtkIds&
108 SVTK_Actor
109 ::GetMapCompositeIndex() const 
110 {
111   return myMapCompositeIndex;
112 }
113
114 void
115 SVTK_Actor
116 ::MapCells(SALOME_Actor* theMapActor,
117            const SVTK_TIndexedMapOfVtkId& theMapIndex)
118 {
119   myUnstructuredGrid->Initialize();
120   myUnstructuredGrid->Allocate();
121
122   vtkUnstructuredGrid * aSourceGrid = (vtkUnstructuredGrid *)theMapActor->GetInput();
123   GetSource()->SetPoints( aSourceGrid->GetPoints() );
124
125   int aNbOfParts = theMapIndex.Extent();
126   for(int ind = 1; ind <= aNbOfParts; ind++){
127     int aPartId = theMapIndex( ind );
128     if(vtkCell* aCell = theMapActor->GetElemCell(aPartId))
129       {
130 #if VTK_XVERSION > 50700
131       if (aCell->GetCellType() != VTK_POLYHEDRON)
132 #endif
133         myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
134 #if VTK_XVERSION > 50700
135       else
136         {
137           vtkPolyhedron *polyhedron = dynamic_cast<vtkPolyhedron*>(aCell);
138           if (!polyhedron)
139             throw SALOME_Exception(LOCALIZED ("not a polyhedron"));
140           vtkIdType *pts = polyhedron->GetFaces();
141           myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),pts[0], pts+1);
142         }
143 #endif
144       }
145   }
146
147   UnShrink();
148   if(theMapActor->IsShrunk()){
149     SetShrinkFactor(theMapActor->GetShrinkFactor());
150     SetShrink();
151   }
152
153   myMapIndex = theMapIndex;
154 }
155
156 void 
157 SVTK_Actor
158 ::MapPoints(SALOME_Actor* theMapActor,
159             const SVTK_TIndexedMapOfVtkId& theMapIndex)
160 {
161   myUnstructuredGrid->Initialize();
162   myUnstructuredGrid->Allocate();
163
164   if(int aNbOfParts = theMapIndex.Extent()){
165     vtkPoints *aPoints = vtkPoints::New();
166     aPoints->SetNumberOfPoints(aNbOfParts);
167     for(vtkIdType i = 0; i < aNbOfParts; i++){
168           vtkIdType aPartId = theMapIndex( i+1 );
169       if(double* aCoord = theMapActor->GetNodeCoord(aPartId)){
170         aPoints->SetPoint(i,aCoord);
171         // Change the type from int to vtkIdType in order to avoid compilation errors while using VTK
172         // from ParaView-3.4.0 compiled on 64-bit Debian platform with VTK_USE_64BIT_IDS = ON
173         myUnstructuredGrid->InsertNextCell(VTK_VERTEX,(vtkIdType) 1,&i);
174       }
175     }
176     myUnstructuredGrid->SetPoints(aPoints);
177     aPoints->Delete();
178   }
179
180   UnShrink();
181
182   myMapIndex = theMapIndex;
183 }
184
185 void
186 SVTK_Actor
187 ::MapEdge(SALOME_Actor* theMapActor,
188           const SVTK_TIndexedMapOfVtkId& theMapIndex)
189 {
190   myUnstructuredGrid->Initialize();
191   myUnstructuredGrid->Allocate();
192
193   vtkUnstructuredGrid * aSourceGrid = (vtkUnstructuredGrid *)theMapActor->GetInput();
194   GetSource()->SetPoints( aSourceGrid->GetPoints() );
195
196
197   if(theMapIndex.Extent() == 2){
198     int anEdgeId = theMapIndex(1) < 0 ? theMapIndex(1) : theMapIndex(2);
199     int aCellId = theMapIndex(1) < 0 ? theMapIndex(2) : theMapIndex(1);
200
201     if(aCellId > 0){
202       if(vtkCell* aCell = theMapActor->GetElemCell(aCellId)){
203         if(anEdgeId < 0){
204           anEdgeId = -anEdgeId - 1;
205           int aNbOfEdges = aCell->GetNumberOfEdges();
206           if(0 <= anEdgeId || anEdgeId < aNbOfEdges){
207             if(vtkCell* anEdge = aCell->GetEdge(anEdgeId))
208               myUnstructuredGrid->InsertNextCell(VTK_LINE,anEdge->GetPointIds());
209           }
210         }
211       }
212     }
213   }
214
215   UnShrink();
216   if(theMapActor->IsShrunk()){
217     SetShrinkFactor(theMapActor->GetShrinkFactor());
218     SetShrink();
219   }
220
221   myMapIndex = theMapIndex;
222 }
223
224 void
225 SVTK_Actor
226 ::MapEdge( SALOME_Actor* theMapActor, 
227            const SVTK_IndexedMapOfVtkIds& theMapCompositeIndex) {
228   myUnstructuredGrid->Initialize();
229   myUnstructuredGrid->Allocate();
230
231   vtkUnstructuredGrid * aSourceGrid = ( vtkUnstructuredGrid * )theMapActor->GetInput();
232   GetSource()->SetPoints( aSourceGrid->GetPoints() );
233
234   vtkIdType aNbOfParts = theMapCompositeIndex.Extent();
235   for(vtkIdType ind = 1; ind <= aNbOfParts; ind++){
236       std::vector<vtkIdType> aNodesIds = theMapCompositeIndex( ind );
237       vtkSmartPointer<vtkIdList> ids = vtkSmartPointer<vtkIdList>::New();
238       ids->InsertNextId(theMapActor->GetNodeVtkId( aNodesIds[0] ) );
239       ids->InsertNextId(theMapActor->GetNodeVtkId( aNodesIds[1] ) );
240       myUnstructuredGrid->InsertNextCell(VTK_LINE,ids);
241   }
242
243   UnShrink();
244   if(theMapActor->IsShrunk()){
245     SetShrinkFactor(theMapActor->GetShrinkFactor());
246     SetShrink();
247   }
248   
249   myMapCompositeIndex = theMapCompositeIndex;
250 }
251
252 /*!
253   To publish the actor an all its internal devices
254 */
255 void
256 SVTK_Actor
257 ::AddToRender(vtkRenderer* theRenderer)
258 {
259   theRenderer->AddActor(this);
260 }
261
262 void
263 SVTK_Actor
264 ::RemoveFromRender(vtkRenderer* theRenderer) 
265 {
266   theRenderer->RemoveActor(this);
267 }