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