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