Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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   myIsResolveCoincidentTopology = false;
60
61   Visibility = Pickable = false;
62
63   myUnstructuredGrid->Delete();
64   myUnstructuredGrid->Allocate();
65 }
66
67 void
68 SVTK_Actor
69 ::Initialize()
70 {
71   SetInput(GetSource());
72 }
73
74 void
75 SVTK_Actor
76 ::SetSource(vtkUnstructuredGrid* theUnstructuredGrid)
77 {
78   if(GetSource() == theUnstructuredGrid)
79     return;
80
81   myUnstructuredGrid = theUnstructuredGrid;
82
83   SetInput(theUnstructuredGrid);
84 }
85
86 vtkUnstructuredGrid*
87 SVTK_Actor
88 ::GetSource()
89 {
90   return myUnstructuredGrid.GetPointer();
91 }
92
93 /*!
94   Destructor
95 */
96 SVTK_Actor
97 ::~SVTK_Actor()
98 {
99 }
100
101 const TColStd_IndexedMapOfInteger&
102 SVTK_Actor
103 ::GetMapIndex() const
104 {
105   return myMapIndex;
106 }
107
108 void
109 SVTK_Actor
110 ::MapCells(SALOME_Actor* theMapActor,
111            const TColStd_IndexedMapOfInteger& theMapIndex)
112 {
113   myUnstructuredGrid->Initialize();
114   myUnstructuredGrid->Allocate();
115
116   vtkDataSet *aSourceDataSet = theMapActor->GetInput();
117   CopyPoints(GetSource(),aSourceDataSet);
118
119   int aNbOfParts = theMapIndex.Extent();
120   for(int ind = 1; ind <= aNbOfParts; ind++){
121     int aPartId = theMapIndex( ind );
122     if(vtkCell* aCell = theMapActor->GetElemCell(aPartId))
123       myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
124   }
125
126   UnShrink();
127   if(theMapActor->IsShrunk()){
128     SetShrinkFactor(theMapActor->GetShrinkFactor());
129     SetShrink();
130   }
131
132   myMapIndex = theMapIndex;
133 }
134
135 void 
136 SVTK_Actor
137 ::MapPoints(SALOME_Actor* theMapActor,
138             const TColStd_IndexedMapOfInteger& theMapIndex)
139 {
140   myUnstructuredGrid->Initialize();
141   myUnstructuredGrid->Allocate();
142
143   if(int aNbOfParts = theMapIndex.Extent()){
144     vtkPoints *aPoints = vtkPoints::New();
145     aPoints->SetNumberOfPoints(aNbOfParts);
146     for(int i = 0; i < aNbOfParts; i++){
147       int aPartId = theMapIndex( i+1 );
148       if(vtkFloatingPointType* aCoord = theMapActor->GetNodeCoord(aPartId)){
149         aPoints->SetPoint(i,aCoord);
150         myUnstructuredGrid->InsertNextCell(VTK_VERTEX,1,&i);
151       }
152     }
153     myUnstructuredGrid->SetPoints(aPoints);
154     aPoints->Delete();
155   }
156
157   UnShrink();
158
159   myMapIndex = theMapIndex;
160 }
161
162 void
163 SVTK_Actor
164 ::MapEdge(SALOME_Actor* theMapActor,
165           const TColStd_IndexedMapOfInteger& theMapIndex)
166 {
167   myUnstructuredGrid->Initialize();
168   myUnstructuredGrid->Allocate();
169
170   vtkDataSet *aSourceDataSet = theMapActor->GetInput();
171   CopyPoints(GetSource(),aSourceDataSet);
172
173
174   if(theMapIndex.Extent() == 2){
175     int anEdgeId = theMapIndex(1) < 0 ? theMapIndex(1) : theMapIndex(2);
176     int aCellId = theMapIndex(1) < 0 ? theMapIndex(2) : theMapIndex(1);
177
178     if(aCellId > 0){
179       if(vtkCell* aCell = theMapActor->GetElemCell(aCellId)){
180         if(anEdgeId < 0){
181           anEdgeId = -anEdgeId - 1;
182           int aNbOfEdges = aCell->GetNumberOfEdges();
183           if(0 <= anEdgeId || anEdgeId < aNbOfEdges){
184             if(vtkCell* anEdge = aCell->GetEdge(anEdgeId))
185               myUnstructuredGrid->InsertNextCell(VTK_LINE,anEdge->GetPointIds());
186           }
187         }
188       }
189     }
190   }
191
192   UnShrink();
193   if(theMapActor->IsShrunk()){
194     SetShrinkFactor(theMapActor->GetShrinkFactor());
195     SetShrink();
196   }
197
198   myMapIndex = theMapIndex;
199 }