Salome HOME
Remove dependency from KERNEL
[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
22 #include "VTKViewer_PassThroughFilter.h"
23
24 // VTK Includes
25 #include <vtkObjectFactory.h>
26 #include <vtkUnstructuredGrid.h>
27 #include <vtkDataSetMapper.h>
28 #include <vtkRenderer.h>
29
30 #include <vtkCell.h>
31 #include <vtkPolyData.h>
32 #include <vtkShrinkFilter.h>
33
34 using namespace std;
35
36 #ifdef _DEBUG_
37 static int MYDEBUG = 0;
38 #else
39 static int MYDEBUG = 0;
40 #endif
41
42
43 static void CopyPoints(vtkUnstructuredGrid* theGrid, vtkDataSet *theSourceDataSet){
44   vtkPoints *aPoints = vtkPoints::New();
45   vtkIdType iEnd = theSourceDataSet->GetNumberOfPoints();
46   aPoints->SetNumberOfPoints(iEnd);
47   for(vtkIdType i = 0; i < iEnd; i++){
48     aPoints->SetPoint(i,theSourceDataSet->GetPoint(i));
49   }
50   theGrid->SetPoints(aPoints);
51   aPoints->Delete();
52 }
53
54 //=======================================================================
55
56 vtkStandardNewMacro(SVTK_Actor);
57
58 SVTK_Actor::SVTK_Actor()
59 {
60   myRenderer = NULL;
61   myIsInfinite = true;
62
63   Visibility = Pickable = false;
64
65   myUnstructuredGrid = vtkUnstructuredGrid::New();
66   myUnstructuredGrid->Allocate();
67
68   myIsShrunk = false;
69   myIsShrinkable = true;
70   myShrinkFilter = vtkShrinkFilter::New();
71
72   myMapper = vtkDataSetMapper::New();
73
74   myMapper->SetInput(myUnstructuredGrid);
75   Superclass::InitPipeLine(myMapper);
76
77   SetResolveCoincidentTopology(false);
78 }
79
80 void SVTK_Actor::SetShrinkFactor(float theValue){
81   myShrinkFilter->SetShrinkFactor(theValue);
82   Modified();
83 }
84
85 void SVTK_Actor::SetShrink()
86 {
87   if ( !myIsShrinkable ) return;
88   if ( vtkDataSet* aDataSet = myPassFilter[0]->GetOutput() )
89   {
90     myShrinkFilter->SetInput( aDataSet );
91     myPassFilter[1]->SetInput( myShrinkFilter->GetOutput() );
92     myIsShrunk = true;
93   }
94 }
95
96 void SVTK_Actor::UnShrink()
97 {
98   if ( !myIsShrunk ) return;
99   if ( vtkDataSet* aDataSet = myPassFilter[0]->GetOutput() )
100   {
101     myPassFilter[1]->SetInput( aDataSet );
102     myPassFilter[1]->Modified();
103     myIsShrunk = false;
104     Modified();
105   }
106 }
107
108
109 //----------------------------------------------------------------------------
110 SVTK_Actor::~SVTK_Actor()
111 {
112   //if(MYDEBUG) INFOS("VTKViewer_Actor::~VTKViewer_Actor()");
113
114   myMapper->RemoveAllInputs();
115   myMapper->Delete();
116
117   myShrinkFilter->UnRegisterAllOutputs();
118   myShrinkFilter->Delete();
119
120   myUnstructuredGrid->Delete();
121 }
122
123
124 //----------------------------------------------------------------------------
125 void SVTK_Actor::MapCells(SALOME_Actor* theMapActor,
126                                const TColStd_IndexedMapOfInteger& theMapIndex)
127 {
128   myUnstructuredGrid->Reset();
129
130   vtkDataSet *aSourceDataSet = theMapActor->GetInput();
131   CopyPoints(myUnstructuredGrid,aSourceDataSet);
132
133   int aNbOfParts = theMapIndex.Extent();
134   for(int ind = 1; ind <= aNbOfParts; ind++){
135     int aPartId = theMapIndex( ind );
136     vtkCell* aCell = theMapActor->GetElemCell(aPartId);
137     myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
138     //for (int i = 0, iEnd = aCell->GetNumberOfEdges(); i < iEnd; i++){
139     //  vtkCell* anEdgeCell = aCell->GetEdge(i);
140     //  myUnstructuredGrid->InsertNextCell(VTK_LINE,anEdgeCell->GetPointIds());
141     //}
142   }
143
144   UnShrink();
145   if(theMapActor->IsShrunk()){
146     SetShrinkFactor(theMapActor->GetShrinkFactor());
147     SetShrink();
148   }
149 }
150
151
152 //----------------------------------------------------------------------------
153 void SVTK_Actor::MapPoints(SALOME_Actor* theMapActor,
154                                 const TColStd_IndexedMapOfInteger& theMapIndex)
155 {
156   myUnstructuredGrid->Reset();
157   if(int aNbOfParts = theMapIndex.Extent()){
158     vtkPoints *aPoints = vtkPoints::New();
159     aPoints->SetNumberOfPoints(aNbOfParts);
160     for(int i = 0; i < aNbOfParts; i++){
161       int aPartId = theMapIndex( i+1 );
162       float* aCoord = theMapActor->GetNodeCoord(aPartId);
163       aPoints->SetPoint(i,aCoord);
164       myUnstructuredGrid->InsertNextCell(VTK_VERTEX,1,&i);
165     }
166     myUnstructuredGrid->SetPoints(aPoints);
167     aPoints->Delete();
168   }
169
170   UnShrink();
171 }
172
173
174 //----------------------------------------------------------------------------
175 void SVTK_Actor::MapEdge(SALOME_Actor* theMapActor,
176                               const TColStd_IndexedMapOfInteger& theMapIndex)
177 {
178   myUnstructuredGrid->Reset();
179
180   vtkDataSet *aSourceDataSet = theMapActor->GetInput();
181   CopyPoints(myUnstructuredGrid,aSourceDataSet);
182
183   int iEnd = theMapIndex.Extent();
184   int aCellId = -1, aCellCounter = 0;
185   for(int i = 1; i <= iEnd; i++){
186     int anId = theMapIndex( i );
187     if(anId > 0) {
188       aCellCounter++;
189       aCellId = anId;
190     }
191   }
192
193   if(aCellCounter == 1){
194     vtkCell* aCell = theMapActor->GetElemCell(aCellId);
195     if(aCell->GetCellType() <= VTK_LINE){
196       myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
197     }else{
198       int aNbOfParts = aCell->GetNumberOfEdges();
199       for(int i = 1; i <= iEnd; i++){
200         int aPartId = theMapIndex(i);
201         if( aPartId < 0){
202           aPartId = -aPartId-1;
203           if(0 > aPartId || aPartId >= aNbOfParts) break;
204           vtkCell* anEdgeCell = aCell->GetEdge(aPartId);
205           myUnstructuredGrid->InsertNextCell(VTK_LINE,anEdgeCell->GetPointIds());
206         }
207       }
208     }
209   }else{
210     int aNbOfParts = aSourceDataSet->GetNumberOfCells();
211     for(int i = 1; i <= iEnd; i++){
212       int aPartId = theMapIndex( i );
213       if(aPartId > 0){
214         if(aPartId >= aNbOfParts) break;
215         vtkCell* aCell = aSourceDataSet->GetCell(aPartId);
216         myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
217       }
218     }
219   }
220
221   UnShrink();
222   if(theMapActor->IsShrunk()){
223     SetShrinkFactor(theMapActor->GetShrinkFactor());
224     SetShrink();
225   }
226 }
227
228 //----------------------------------------------------------------------------