Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 vtkStandardNewMacro(SVTK_DeviceActor);
50
51 /*!
52   Constructor
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   Destructor
82 */
83 SVTK_DeviceActor
84 ::~SVTK_DeviceActor()
85 {
86   myMapper->Delete();
87
88   myProperty->Delete();
89
90   myGeomFilter->Delete();
91
92   myTransformFilter->Delete();
93
94   myShrinkFilter->Delete();
95
96   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++)
97     myPassFilter[i]->Delete();
98 }
99
100 /*!
101   To insert some additional filters and then sets the given vtkMapper
102 */
103 void
104 SVTK_DeviceActor
105 ::SetMapper(vtkMapper* theMapper)
106 {
107   InitPipeLine(theMapper);
108 }
109
110 /*!
111   To initialize internal pipeline
112 */
113 void
114 SVTK_DeviceActor
115 ::InitPipeLine(vtkMapper* theMapper)
116 {
117   if(theMapper){
118     int anId = 0;
119     myPassFilter[ anId ]->SetInput( theMapper->GetInput() );
120     myPassFilter[ anId + 1]->SetInput( myPassFilter[ anId ]->GetOutput() );
121     
122     anId++; // 1
123     myGeomFilter->SetInput( myPassFilter[ anId ]->GetOutput() );
124
125     anId++; // 2
126     myPassFilter[ anId ]->SetInput( myGeomFilter->GetOutput() ); 
127     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
128
129     anId++; // 3
130     myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
131
132     anId++; // 4
133     myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
134     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
135
136     anId++; // 5
137     if(vtkDataSetMapper* aMapper = dynamic_cast<vtkDataSetMapper*>(theMapper)){
138       aMapper->SetInput(myPassFilter[anId]->GetOutput());
139     }else if(vtkPolyDataMapper* aMapper = dynamic_cast<vtkPolyDataMapper*>(theMapper)){
140       aMapper->SetInput(myPassFilter[anId]->GetPolyDataOutput());
141     }
142   }else
143     myPassFilter[ 0 ]->SetInput( NULL );
144   Superclass::SetMapper(theMapper);
145 }
146
147 /*!
148   Allows to get initial vtkDataSet
149 */
150 vtkDataSet* 
151 SVTK_DeviceActor
152 ::GetInput()
153 {
154   return myPassFilter.front()->GetOutput();
155 }
156
157 /*!
158   Allows to set initial vtkDataSet
159 */
160 void
161 SVTK_DeviceActor
162 ::SetInput(vtkDataSet* theDataSet)
163 {
164   myMapper->SetInput(theDataSet);
165   InitPipeLine(myMapper);
166 }
167
168 /*!
169   To provide VTK to Object and backward mapping
170 */
171 void
172 SVTK_DeviceActor::
173 SetStoreMapping(bool theStoreMapping)
174 {
175   myGeomFilter->SetStoreMapping(theStoreMapping);
176 }
177
178
179 /*!
180   \return time of modification
181 */
182 unsigned long int 
183 SVTK_DeviceActor
184 ::GetMTime()
185 {
186   unsigned long mTime = this->Superclass::GetMTime();
187
188   mTime = max(mTime,myGeomFilter->GetMTime());
189
190   mTime = max(mTime,myTransformFilter->GetMTime());
191
192   if(myIsShrunk)
193     mTime = max(mTime,myShrinkFilter->GetMTime());
194
195   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++)
196     max(mTime,myPassFilter[i]->GetMTime());
197
198   return mTime;
199 }
200
201 /*!
202   Apply a view transformation
203   \param theTransform - transformation
204 */
205 void 
206 SVTK_DeviceActor
207 ::SetTransform(VTKViewer_Transform* theTransform)
208 {
209   myTransformFilter->SetTransform(theTransform);
210 }
211
212 /*!
213   \return true if actor is shrinkable
214 */
215 bool
216 SVTK_DeviceActor
217 ::IsShrunkable() 
218
219   return myIsShrinkable;
220 }
221
222 /*!
223   Changes shrinkable state of actor
224   theIsShrinkable - new shrinkable state
225 */  
226 void
227 SVTK_DeviceActor
228 ::SetShrinkable(bool theIsShrinkable) 
229
230   myIsShrinkable = theIsShrinkable;
231 }
232   
233 /*!
234   \return true if actor is shrunkable
235 */
236 bool
237 SVTK_DeviceActor
238 ::IsShrunk() 
239
240   return myIsShrunk;
241 }
242
243 /*!
244   Insert shrink filter into pipeline
245 */
246 void
247 SVTK_DeviceActor
248 ::SetShrink() 
249 {
250   if ( !myIsShrinkable ) 
251     return;
252   if ( vtkDataSet* aDataSet = myPassFilter[ 0 ]->GetOutput() )
253   {
254     myShrinkFilter->SetInput( aDataSet );
255     myPassFilter[ 1 ]->SetInput( myShrinkFilter->GetOutput() );
256     myIsShrunk = true;
257   }
258 }
259
260 /*!
261   Remove shrink filter from pipeline
262 */
263 void 
264 SVTK_DeviceActor
265 ::UnShrink() 
266 {
267   if ( !myIsShrunk ) return;
268   if ( vtkDataSet* aDataSet = myPassFilter[ 0 ]->GetOutput() )
269   {    
270     myPassFilter[ 1 ]->SetInput( aDataSet );
271     myIsShrunk = false;
272   }
273 }
274
275 /*!
276   \return shrink factor
277 */
278 vtkFloatingPointType
279 SVTK_DeviceActor
280 ::GetShrinkFactor()
281 {
282   return myShrinkFilter->GetShrinkFactor();
283 }
284
285 /*!
286   Changes shrink factor
287   \param theValue - new shrink factor
288 */
289 void 
290 SVTK_DeviceActor
291 ::SetShrinkFactor(vtkFloatingPointType theValue)
292 {
293   myShrinkFilter->SetShrinkFactor(theValue);
294 }
295
296
297 /*!
298   Set representation (VTK_SURFACE, VTK_POINTS, VTK_WIREFRAME and so on)
299   param theMode - new mode
300 */
301 void
302 SVTK_DeviceActor
303 ::SetRepresentation(SVTK::Representation::Type theMode)
304
305   using namespace SVTK::Representation;
306   if(IsShaded()){
307     switch(myRepresentation){
308     case Points : 
309     case Surface : 
310       myProperty->DeepCopy(GetProperty());
311     }
312     
313     switch(theMode){
314     case Points : 
315     case Surface : 
316       GetProperty()->DeepCopy(myProperty);
317       break;
318     default:
319       GetProperty()->SetAmbient(1.0);
320       GetProperty()->SetDiffuse(0.0);
321       GetProperty()->SetSpecular(0.0);
322     }
323   }
324
325   switch(theMode){
326   case Insideframe : 
327     myGeomFilter->SetInside(true);
328     myGeomFilter->SetWireframeMode(true);
329     GetProperty()->SetRepresentation(VTK_WIREFRAME);
330     break;
331   case Points : 
332     GetProperty()->SetPointSize(GetDefaultPointSize());  
333     GetProperty()->SetRepresentation(VTK_POINTS);
334     myGeomFilter->SetWireframeMode(false);
335     myGeomFilter->SetInside(false);
336     break;
337   case Wireframe : 
338     GetProperty()->SetRepresentation(VTK_WIREFRAME);
339     myGeomFilter->SetWireframeMode(true);
340     myGeomFilter->SetInside(false);
341     break;
342   case Surface : 
343     GetProperty()->SetRepresentation(VTK_SURFACE);
344     myGeomFilter->SetWireframeMode(false);
345     myGeomFilter->SetInside(false);
346     break;
347   }
348
349   myRepresentation = theMode;
350 }
351
352 /*!
353   \return current representation mode
354 */
355 SVTK::Representation::Type 
356 SVTK_DeviceActor
357 ::GetRepresentation()
358 {
359   return myRepresentation;
360 }
361
362 /*!
363   \return default point size
364 */
365 vtkFloatingPointType
366 SVTK_DeviceActor
367 ::GetDefaultPointSize()
368 {
369   return 5;
370 }
371
372 /*!
373   \return default line width
374 */
375 vtkFloatingPointType
376 SVTK_DeviceActor
377 ::GetDefaultLineWidth()
378 {
379   return 3;
380 }
381
382 /*!
383   \return true if actor is shaded
384 */
385 bool
386 SVTK_DeviceActor
387 ::IsShaded()
388 {
389   return myIsShaded;
390 }
391
392 /*!
393   Sets shaded state of actor
394   \param theShaded - new shaded state
395 */
396 void
397 SVTK_DeviceActor
398 ::SetShaded(bool theShaded)
399 {
400   myIsShaded = theShaded;
401 }
402
403 /*!
404   Maps VTK index of a node to corresponding object index
405 */
406 int
407 SVTK_DeviceActor
408 ::GetNodeObjId(int theVtkID)
409 {
410   return theVtkID;
411 }
412
413 /*!
414   Get coordinates of a node for given object index
415 */
416 vtkFloatingPointType* 
417 SVTK_DeviceActor
418 ::GetNodeCoord(int theObjID)
419 {
420   return GetInput()->GetPoint(theObjID);
421 }
422
423
424 /*!
425   Get corresponding #vtkCell for given object index
426 */
427 vtkCell* 
428 SVTK_DeviceActor
429 ::GetElemCell(int theObjID)
430 {
431   return GetInput()->GetCell(theObjID);
432 }
433
434 /*!
435   Maps VTK index of a cell to corresponding object index
436 */
437 int
438 SVTK_DeviceActor
439 ::GetElemObjId(int theVtkID) 
440
441   return theVtkID;
442 }
443
444 /*!
445   Renders actor
446 */
447 void
448 SVTK_DeviceActor
449 ::Render(vtkRenderer *ren, vtkMapper* m)
450 {
451   if(myIsResolveCoincidentTopology){
452     int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
453     vtkFloatingPointType aFactor, aUnit; 
454     vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
455     
456     vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
457     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
458                                                                    myPolygonOffsetUnits);
459     Superclass::Render(ren,m);
460     
461     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
462     vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
463   }else{
464     Superclass::Render(ren,m);
465   }
466 }
467
468 /*!
469   Set polygon offset parameters
470   \param factor, units  - Opengl polygon offset parameters
471 */
472 void
473 SVTK_DeviceActor
474 ::SetPolygonOffsetParameters(vtkFloatingPointType factor, 
475                              vtkFloatingPointType units)
476 {
477   myPolygonOffsetFactor = factor;
478   myPolygonOffsetUnits = units;
479 }
480
481 /*!
482   Get polygon offset parameters
483   \param factor, units  - Opengl polygon offset parameters
484 */
485 void
486 SVTK_DeviceActor
487 ::GetPolygonOffsetParameters(vtkFloatingPointType& factor, 
488                              vtkFloatingPointType& units)
489 {
490   factor = myPolygonOffsetFactor;
491   units = myPolygonOffsetUnits;
492 }