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