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