Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / SVTK / SVTK_Actor.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 //  This library is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU Lesser General Public
6 //  License as published by the Free Software Foundation; either
7 //  version 2.1 of the License.
8 //
9 //  This library is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 //  Lesser General Public License for more details.
13 //
14 //  You should have received a copy of the GNU Lesser General Public
15 //  License along with this library; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
19
20 #include "SVTK_Actor.h"
21 #include "SALOME_Actor.h"
22
23 // VTK Includes
24 #include <vtkObjectFactory.h>
25 #include <vtkUnstructuredGrid.h>
26 #include <vtkDataSetMapper.h>
27 #include <vtkRenderer.h>
28
29 #include <vtkCell.h>
30 #include <vtkPolyData.h>
31
32 using namespace std;
33
34
35 static 
36 void
37 CopyPoints(vtkUnstructuredGrid* theGrid, vtkDataSet *theSourceDataSet)
38 {
39   vtkPoints *aPoints = vtkPoints::New();
40   vtkIdType iEnd = theSourceDataSet->GetNumberOfPoints();
41   aPoints->SetNumberOfPoints(iEnd);
42   for(vtkIdType i = 0; i < iEnd; i++){
43     aPoints->SetPoint(i,theSourceDataSet->GetPoint(i));
44   }
45   theGrid->SetPoints(aPoints);
46   aPoints->Delete();
47 }
48
49 vtkStandardNewMacro(SVTK_Actor);
50
51 /*!
52   Constructor
53 */
54 SVTK_Actor
55 ::SVTK_Actor():
56   myUnstructuredGrid(vtkUnstructuredGrid::New())
57 {
58   myIsShaded = true;
59
60   Visibility = Pickable = false;
61
62   myUnstructuredGrid->Delete();
63   myUnstructuredGrid->Allocate();
64 }
65
66 void
67 SVTK_Actor
68 ::Initialize()
69 {
70   SetInput(GetSource());
71 }
72
73 void
74 SVTK_Actor
75 ::SetSource(vtkUnstructuredGrid* theUnstructuredGrid)
76 {
77   if(GetSource() == theUnstructuredGrid)
78     return;
79
80   myUnstructuredGrid = theUnstructuredGrid;
81
82   SetInput(theUnstructuredGrid);
83 }
84
85 vtkUnstructuredGrid*
86 SVTK_Actor
87 ::GetSource()
88 {
89   return myUnstructuredGrid.GetPointer();
90 }
91
92 /*!
93   Destructor
94 */
95 SVTK_Actor
96 ::~SVTK_Actor()
97 {
98 }
99
100 const TColStd_IndexedMapOfInteger&
101 SVTK_Actor
102 ::GetMapIndex() const
103 {
104   return myMapIndex;
105 }
106
107 void
108 SVTK_Actor
109 ::MapCells(SALOME_Actor* theMapActor,
110            const TColStd_IndexedMapOfInteger& theMapIndex)
111 {
112   myUnstructuredGrid->Initialize();
113   myUnstructuredGrid->Allocate();
114
115   vtkDataSet *aSourceDataSet = theMapActor->GetInput();
116   CopyPoints(GetSource(),aSourceDataSet);
117
118   int aNbOfParts = theMapIndex.Extent();
119   for(int ind = 1; ind <= aNbOfParts; ind++){
120     int aPartId = theMapIndex( ind );
121     if(vtkCell* aCell = theMapActor->GetElemCell(aPartId))
122       myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
123   }
124
125   UnShrink();
126   if(theMapActor->IsShrunk()){
127     SetShrinkFactor(theMapActor->GetShrinkFactor());
128     SetShrink();
129   }
130
131   myMapIndex = theMapIndex;
132 }
133
134 void 
135 SVTK_Actor
136 ::MapPoints(SALOME_Actor* theMapActor,
137             const TColStd_IndexedMapOfInteger& theMapIndex)
138 {
139   myUnstructuredGrid->Initialize();
140   myUnstructuredGrid->Allocate();
141
142   if(int aNbOfParts = theMapIndex.Extent()){
143     vtkPoints *aPoints = vtkPoints::New();
144     aPoints->SetNumberOfPoints(aNbOfParts);
145     for(int i = 0; i < aNbOfParts; i++){
146       int aPartId = theMapIndex( i+1 );
147       if(vtkFloatingPointType* aCoord = theMapActor->GetNodeCoord(aPartId)){
148         aPoints->SetPoint(i,aCoord);
149         myUnstructuredGrid->InsertNextCell(VTK_VERTEX,1,&i);
150       }
151     }
152     myUnstructuredGrid->SetPoints(aPoints);
153     aPoints->Delete();
154   }
155
156   UnShrink();
157
158   myMapIndex = theMapIndex;
159 }
160
161 void
162 SVTK_Actor
163 ::MapEdge(SALOME_Actor* theMapActor,
164           const TColStd_IndexedMapOfInteger& theMapIndex)
165 {
166   myUnstructuredGrid->Initialize();
167   myUnstructuredGrid->Allocate();
168
169   vtkDataSet *aSourceDataSet = theMapActor->GetInput();
170   CopyPoints(GetSource(),aSourceDataSet);
171
172
173   if(theMapIndex.Extent() == 2){
174     int anEdgeId = theMapIndex(1) < 0 ? theMapIndex(1) : theMapIndex(2);
175     int aCellId = theMapIndex(1) < 0 ? theMapIndex(2) : theMapIndex(1);
176
177     if(aCellId > 0){
178       if(vtkCell* aCell = theMapActor->GetElemCell(aCellId)){
179         if(anEdgeId < 0){
180           anEdgeId = -anEdgeId - 1;
181           int aNbOfEdges = aCell->GetNumberOfEdges();
182           if(0 <= anEdgeId || anEdgeId < aNbOfEdges){
183             if(vtkCell* anEdge = aCell->GetEdge(anEdgeId))
184               myUnstructuredGrid->InsertNextCell(VTK_LINE,anEdge->GetPointIds());
185           }
186         }
187       }
188     }
189   }
190
191   UnShrink();
192   if(theMapActor->IsShrunk()){
193     SetShrinkFactor(theMapActor->GetShrinkFactor());
194     SetShrink();
195   }
196
197   myMapIndex = theMapIndex;
198 }