Salome HOME
be959115cd70535e0f41e59b466884d5f7a6fb79
[modules/gui.git] / src / SVTK / SVTK_DeviceActor.cxx
1 //  SVTK OBJECT : interactive object for SVTK visualization
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SVTK_DeviceActor.cxx
25 //  Author : 
26 //  Module : 
27 //  $Header$
28
29
30 #include "SVTK_DeviceActor.h"
31
32 #include "VTKViewer_Transform.h"
33 #include "VTKViewer_TransformFilter.h"
34 #include "VTKViewer_PassThroughFilter.h"
35 #include "VTKViewer_GeometryFilter.h"
36
37 // VTK Includes
38 #include <vtkObjectFactory.h>
39 #include <vtkShrinkFilter.h>
40
41 #include <vtkPolyData.h>
42 #include <vtkUnstructuredGrid.h>
43
44 #include <vtkPolyDataMapper.h>
45 #include <vtkDataSetMapper.h>
46
47 using namespace std;
48
49 //----------------------------------------------------------------------------
50 vtkStandardNewMacro(SVTK_DeviceActor);
51
52
53 //----------------------------------------------------------------------------
54 SVTK_DeviceActor
55 ::SVTK_DeviceActor()
56 {
57   myIsShrunk = false;
58   myIsShrinkable = true;
59
60   myIsShaded = true;
61   myProperty = vtkProperty::New();
62   myRepresentation = SVTK::Representation::Surface;
63
64   myIsResolveCoincidentTopology = false;
65   vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
66                                                                  myPolygonOffsetUnits);
67
68   myMapper = vtkDataSetMapper::New();
69
70   myShrinkFilter = vtkShrinkFilter::New();
71
72   myGeomFilter = VTKViewer_GeometryFilter::New();
73
74   myTransformFilter = VTKViewer_TransformFilter::New();
75
76   for(int i = 0; i < 6; i++)
77     myPassFilter.push_back(VTKViewer_PassThroughFilter::New());
78 }
79
80
81 //----------------------------------------------------------------------------
82 SVTK_DeviceActor
83 ::~SVTK_DeviceActor()
84 {
85   myMapper->Delete();
86
87   myProperty->Delete();
88
89   myGeomFilter->Delete();
90
91   myTransformFilter->Delete();
92
93   myShrinkFilter->Delete();
94
95   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++)
96     myPassFilter[i]->Delete();
97 }
98
99
100 //----------------------------------------------------------------------------
101 void
102 SVTK_DeviceActor
103 ::SetMapper(vtkMapper* theMapper)
104 {
105   InitPipeLine(theMapper);
106 }
107
108 void
109 SVTK_DeviceActor
110 ::InitPipeLine(vtkMapper* theMapper)
111 {
112   if(theMapper){
113     int anId = 0;
114     myPassFilter[ anId ]->SetInput( theMapper->GetInput() );
115     myPassFilter[ anId + 1]->SetInput( myPassFilter[ anId ]->GetOutput() );
116     
117     anId++; // 1
118     myGeomFilter->SetInput( myPassFilter[ anId ]->GetOutput() );
119
120     anId++; // 2
121     myPassFilter[ anId ]->SetInput( myGeomFilter->GetOutput() ); 
122     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
123
124     anId++; // 3
125     myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
126
127     anId++; // 4
128     myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
129     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
130
131     anId++; // 5
132     if(vtkDataSetMapper* aMapper = dynamic_cast<vtkDataSetMapper*>(theMapper)){
133       aMapper->SetInput(myPassFilter[anId]->GetOutput());
134     }else if(vtkPolyDataMapper* aMapper = dynamic_cast<vtkPolyDataMapper*>(theMapper)){
135       aMapper->SetInput(myPassFilter[anId]->GetPolyDataOutput());
136     }
137   }else
138     myPassFilter[ 0 ]->SetInput( NULL );
139   Superclass::SetMapper(theMapper);
140 }
141
142 //----------------------------------------------------------------------------
143 vtkDataSet* 
144 SVTK_DeviceActor
145 ::GetInput()
146 {
147   return myPassFilter.front()->GetOutput();
148 }
149
150 void
151 SVTK_DeviceActor
152 ::SetInput(vtkDataSet* theDataSet)
153 {
154   myMapper->SetInput(theDataSet);
155   InitPipeLine(myMapper);
156 }
157
158 //----------------------------------------------------------------------------
159 void
160 SVTK_DeviceActor::
161 SetStoreMapping(bool theStoreMapping)
162 {
163   myGeomFilter->SetStoreMapping(theStoreMapping);
164 }
165
166
167
168 //----------------------------------------------------------------------------
169 unsigned long int 
170 SVTK_DeviceActor
171 ::GetMTime()
172 {
173   unsigned long mTime = this->Superclass::GetMTime();
174
175   mTime = max(mTime,myGeomFilter->GetMTime());
176
177   mTime = max(mTime,myTransformFilter->GetMTime());
178
179   if(myIsShrunk)
180     mTime = max(mTime,myShrinkFilter->GetMTime());
181
182   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++)
183     max(mTime,myPassFilter[i]->GetMTime());
184
185   return mTime;
186 }
187
188
189 //----------------------------------------------------------------------------
190 void 
191 SVTK_DeviceActor
192 ::SetTransform(VTKViewer_Transform* theTransform)
193 {
194   myTransformFilter->SetTransform(theTransform);
195 }
196
197
198 //----------------------------------------------------------------------------
199 bool
200 SVTK_DeviceActor
201 ::IsShrunkable() 
202
203   return myIsShrinkable;
204 }
205   
206 void
207 SVTK_DeviceActor
208 ::SetShrinkable(bool theIsShrinkable) 
209
210   myIsShrinkable = theIsShrinkable;
211 }
212   
213 bool
214 SVTK_DeviceActor
215 ::IsShrunk() 
216
217   return myIsShrunk;
218 }
219
220 void
221 SVTK_DeviceActor
222 ::SetShrink() 
223 {
224   if ( !myIsShrinkable ) 
225     return;
226   if ( vtkDataSet* aDataSet = myPassFilter[ 0 ]->GetOutput() )
227   {
228     myShrinkFilter->SetInput( aDataSet );
229     myPassFilter[ 1 ]->SetInput( myShrinkFilter->GetOutput() );
230     myIsShrunk = true;
231   }
232 }
233
234 void 
235 SVTK_DeviceActor
236 ::UnShrink() 
237 {
238   if ( !myIsShrunk ) return;
239   if ( vtkDataSet* aDataSet = myPassFilter[ 0 ]->GetOutput() )
240   {    
241     myPassFilter[ 1 ]->SetInput( aDataSet );
242     myIsShrunk = false;
243   }
244 }
245
246 float
247 SVTK_DeviceActor
248 ::GetShrinkFactor()
249 {
250   return myShrinkFilter->GetShrinkFactor();
251 }
252
253 void 
254 SVTK_DeviceActor
255 ::SetShrinkFactor(float theValue)
256 {
257   myShrinkFilter->SetShrinkFactor(theValue);
258 }
259
260
261
262 //----------------------------------------------------------------------------
263 void
264 SVTK_DeviceActor
265 ::SetRepresentation(SVTK::Representation::Type theMode)
266
267   using namespace SVTK::Representation;
268   if(IsShaded()){
269     switch(myRepresentation){
270     case Points : 
271     case Surface : 
272       myProperty->DeepCopy(GetProperty());
273     }
274     
275     switch(theMode){
276     case Points : 
277     case Surface : 
278       GetProperty()->DeepCopy(myProperty);
279       break;
280     default:
281       GetProperty()->SetAmbient(1.0);
282       GetProperty()->SetDiffuse(0.0);
283       GetProperty()->SetSpecular(0.0);
284     }
285   }
286
287   switch(theMode){
288   case Insideframe : 
289     myGeomFilter->SetInside(true);
290     myGeomFilter->SetWireframeMode(true);
291     GetProperty()->SetRepresentation(VTK_WIREFRAME);
292     break;
293   case Points : 
294     GetProperty()->SetPointSize(GetDefaultPointSize());  
295     GetProperty()->SetRepresentation(VTK_POINTS);
296     myGeomFilter->SetWireframeMode(false);
297     myGeomFilter->SetInside(false);
298     break;
299   case Wireframe : 
300     GetProperty()->SetRepresentation(VTK_WIREFRAME);
301     myGeomFilter->SetWireframeMode(true);
302     myGeomFilter->SetInside(false);
303     break;
304   case Surface : 
305     GetProperty()->SetRepresentation(VTK_SURFACE);
306     myGeomFilter->SetWireframeMode(false);
307     myGeomFilter->SetInside(false);
308     break;
309   }
310
311   myRepresentation = theMode;
312 }
313
314 SVTK::Representation::Type 
315 SVTK_DeviceActor
316 ::GetRepresentation()
317 {
318   return myRepresentation;
319 }
320
321 float
322 SVTK_DeviceActor
323 ::GetDefaultPointSize()
324 {
325   return 5;
326 }
327
328 float
329 SVTK_DeviceActor
330 ::GetDefaultLineWidth()
331 {
332   return 3;
333 }
334
335
336 bool
337 SVTK_DeviceActor
338 ::IsShaded()
339 {
340   return myIsShaded;
341 }
342
343 void
344 SVTK_DeviceActor
345 ::SetShaded(bool theShaded)
346 {
347   myIsShaded = theShaded;
348 }
349
350
351 //----------------------------------------------------------------------------
352 int
353 SVTK_DeviceActor
354 ::GetNodeObjId(int theVtkID)
355 {
356   return theVtkID;
357 }
358
359 float* 
360 SVTK_DeviceActor
361 ::GetNodeCoord(int theObjID)
362 {
363   return GetInput()->GetPoint(theObjID);
364 }
365
366
367 vtkCell* 
368 SVTK_DeviceActor
369 ::GetElemCell(int theObjID)
370 {
371   return GetInput()->GetCell(theObjID);
372 }
373
374 int
375 SVTK_DeviceActor
376 ::GetElemObjId(int theVtkID) 
377
378   return theVtkID;
379 }
380
381
382 //----------------------------------------------------------------------------
383 void
384 SVTK_DeviceActor
385 ::Render(vtkRenderer *ren, vtkMapper* m)
386 {
387   if(myIsResolveCoincidentTopology){
388     int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
389     float aFactor, aUnit; 
390     vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
391     
392     vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
393     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
394                                                                    myPolygonOffsetUnits);
395     Superclass::Render(ren,m);
396     
397     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
398     vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
399   }else{
400     Superclass::Render(ren,m);
401   }
402 }
403
404
405 void
406 SVTK_DeviceActor
407 ::SetPolygonOffsetParameters(float factor, float units)
408 {
409   myPolygonOffsetFactor = factor;
410   myPolygonOffsetUnits = units;
411 }
412
413 void
414 SVTK_DeviceActor
415 ::GetPolygonOffsetParameters(float& factor, float& units)
416 {
417   factor = myPolygonOffsetFactor;
418   units = myPolygonOffsetUnits;
419 }