]> SALOME platform Git repositories - modules/gui.git/blob - src/SVTK/SVTK_DeviceActor.cxx
Salome HOME
a3bb037730cf4a8d353b54196fd75b41c5ec9c0c
[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_GeometryFilter.h"
35
36 // VTK Includes
37 #include <vtkObjectFactory.h>
38 #include <vtkShrinkFilter.h>
39
40 #include <vtkPolyData.h>
41 #include <vtkUnstructuredGrid.h>
42
43 #include <vtkPolyDataMapper.h>
44 #include <vtkDataSetMapper.h>
45
46 #include <vtkPassThroughFilter.h>
47
48 using namespace std;
49
50 vtkStandardNewMacro(SVTK_DeviceActor);
51
52 /*!
53   Constructor
54 */
55 SVTK_DeviceActor
56 ::SVTK_DeviceActor()
57 {
58   myIsShrunk = false;
59   myIsShrinkable = true;
60
61   myIsShaded = true;
62   myProperty = vtkProperty::New();
63   myRepresentation = SVTK::Representation::Surface;
64
65   myIsResolveCoincidentTopology = true;
66   vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
67                                                                  myPolygonOffsetUnits);
68
69   myMapper = vtkDataSetMapper::New();
70
71   myShrinkFilter = vtkShrinkFilter::New();
72
73   myGeomFilter = VTKViewer_GeometryFilter::New();
74
75   myTransformFilter = VTKViewer_TransformFilter::New();
76
77   for(int i = 0; i < 6; i++)
78     myPassFilter.push_back(vtkPassThroughFilter::New());
79 }
80
81 /*!
82   Destructor
83 */
84 SVTK_DeviceActor
85 ::~SVTK_DeviceActor()
86 {
87   myMapper->Delete();
88
89   myProperty->Delete();
90
91   myGeomFilter->Delete();
92
93   myTransformFilter->Delete();
94
95   myShrinkFilter->Delete();
96
97   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++)
98     myPassFilter[i]->Delete();
99 }
100
101 /*!
102   To insert some additional filters and then sets the given vtkMapper
103 */
104 void
105 SVTK_DeviceActor
106 ::SetMapper(vtkMapper* theMapper)
107 {
108   InitPipeLine(theMapper);
109 }
110
111 /*!
112   To initialize internal pipeline
113 */
114 void
115 SVTK_DeviceActor
116 ::InitPipeLine(vtkMapper* theMapper)
117 {
118   if(theMapper){
119     int anId = 0;
120     myPassFilter[ anId ]->SetInput( theMapper->GetInput() );
121     myPassFilter[ anId + 1]->SetInput( myPassFilter[ anId ]->GetOutput() );
122     
123     anId++; // 1
124     myGeomFilter->SetInput( myPassFilter[ anId ]->GetOutput() );
125
126     anId++; // 2
127     myPassFilter[ anId ]->SetInput( myGeomFilter->GetOutput() ); 
128     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
129
130     anId++; // 3
131     myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
132
133     anId++; // 4
134     myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
135     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
136
137     anId++; // 5
138     if(vtkDataSetMapper* aMapper = dynamic_cast<vtkDataSetMapper*>(theMapper)){
139       aMapper->SetInput(myPassFilter[anId]->GetOutput());
140     }else if(vtkPolyDataMapper* aMapper = dynamic_cast<vtkPolyDataMapper*>(theMapper)){
141       aMapper->SetInput(myPassFilter[anId]->GetPolyDataOutput());
142     }
143   }
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   
253   if ( vtkDataSet* aDataSet = myPassFilter[ 0 ]->GetOutput() )
254   {     
255     aDataSet->Update();
256     int numCells=aDataSet->GetNumberOfCells();
257     int numPts = aDataSet->GetNumberOfPoints();
258     //It's impossible to use to apply "shrink" for "empty" dataset
259     if (numCells < 1 || numPts < 1)
260             return;
261     myShrinkFilter->SetInput( aDataSet );
262     myPassFilter[ 1 ]->SetInput( myShrinkFilter->GetOutput() );
263     myIsShrunk = true;
264   }
265 }
266
267 /*!
268   Remove shrink filter from pipeline
269 */
270 void 
271 SVTK_DeviceActor
272 ::UnShrink() 
273 {
274   if ( !myIsShrunk ) return;
275   if ( vtkDataSet* aDataSet = myPassFilter[ 0 ]->GetOutput() )
276   {    
277     myPassFilter[ 1 ]->SetInput( aDataSet );
278     myIsShrunk = false;
279   }
280 }
281
282 /*!
283   \return shrink factor
284 */
285 vtkFloatingPointType
286 SVTK_DeviceActor
287 ::GetShrinkFactor()
288 {
289   return myShrinkFilter->GetShrinkFactor();
290 }
291
292 /*!
293   Changes shrink factor
294   \param theValue - new shrink factor
295 */
296 void 
297 SVTK_DeviceActor
298 ::SetShrinkFactor(vtkFloatingPointType theValue)
299 {
300   myShrinkFilter->SetShrinkFactor(theValue);
301 }
302
303 /*!
304   Set representation (VTK_SURFACE, VTK_POINTS, VTK_WIREFRAME and so on)
305   param theMode - new mode
306 */
307 void
308 SVTK_DeviceActor
309 ::SetRepresentation(SVTK::Representation::Type theMode)
310
311   using namespace SVTK::Representation;
312   if(IsShaded()){
313     switch(myRepresentation){
314     case Points : 
315     case Surface : 
316       myProperty->SetAmbient(GetProperty()->GetAmbient());
317       myProperty->SetDiffuse(GetProperty()->GetDiffuse());
318       myProperty->SetSpecular(GetProperty()->GetSpecular());
319     }
320     
321     switch(theMode){
322     case Points : 
323     case Surface : 
324       GetProperty()->SetAmbient(myProperty->GetAmbient());
325       GetProperty()->SetDiffuse(myProperty->GetDiffuse());
326       GetProperty()->SetSpecular(myProperty->GetSpecular());
327       break;
328     default:
329       GetProperty()->SetAmbient(1.0);
330       GetProperty()->SetDiffuse(0.0);
331       GetProperty()->SetSpecular(0.0);
332     }
333   }
334
335   switch(theMode){
336   case Insideframe : 
337     myGeomFilter->SetInside(true);
338     myGeomFilter->SetWireframeMode(true);
339     GetProperty()->SetRepresentation(VTK_WIREFRAME);
340     break;
341   case Points : 
342     GetProperty()->SetPointSize(GetDefaultPointSize());  
343     GetProperty()->SetRepresentation(VTK_POINTS);
344     myGeomFilter->SetWireframeMode(false);
345     myGeomFilter->SetInside(false);
346     break;
347   case Wireframe : 
348     GetProperty()->SetRepresentation(VTK_WIREFRAME);
349     myGeomFilter->SetWireframeMode(true);
350     myGeomFilter->SetInside(false);
351     break;
352   case Surface : 
353     GetProperty()->SetRepresentation(VTK_SURFACE);
354     myGeomFilter->SetWireframeMode(false);
355     myGeomFilter->SetInside(false);
356     break;
357   }
358
359   myRepresentation = theMode;
360 }
361
362 /*!
363   \return current representation mode
364 */
365 SVTK::Representation::Type 
366 SVTK_DeviceActor
367 ::GetRepresentation()
368 {
369   return myRepresentation;
370 }
371
372 /*!
373   \return default point size
374 */
375 vtkFloatingPointType
376 SVTK_DeviceActor
377 ::GetDefaultPointSize()
378 {
379   return 5;
380 }
381
382 /*!
383   \return default line width
384 */
385 vtkFloatingPointType
386 SVTK_DeviceActor
387 ::GetDefaultLineWidth()
388 {
389   return 3;
390 }
391
392 /*!
393   \return true if actor is shaded
394 */
395 bool
396 SVTK_DeviceActor
397 ::IsShaded()
398 {
399   return myIsShaded;
400 }
401
402 /*!
403   Sets shaded state of actor
404   \param theShaded - new shaded state
405 */
406 void
407 SVTK_DeviceActor
408 ::SetShaded(bool theShaded)
409 {
410   myIsShaded = theShaded;
411 }
412
413 /*!
414   Maps VTK index of a node to corresponding object index
415 */
416 int
417 SVTK_DeviceActor
418 ::GetNodeObjId(int theVtkID)
419 {
420   return theVtkID;
421 }
422
423 /*!
424   Get coordinates of a node for given object index
425 */
426 vtkFloatingPointType* 
427 SVTK_DeviceActor
428 ::GetNodeCoord(int theObjID)
429 {
430   return GetInput()->GetPoint(theObjID);
431 }
432
433
434 /*!
435   Get corresponding #vtkCell for given object index
436 */
437 vtkCell* 
438 SVTK_DeviceActor
439 ::GetElemCell(int theObjID)
440 {
441   return GetInput()->GetCell(theObjID);
442 }
443
444 /*!
445   Maps VTK index of a cell to corresponding object index
446 */
447 int
448 SVTK_DeviceActor
449 ::GetElemObjId(int theVtkID) 
450
451   return theVtkID;
452 }
453
454 /*!
455   Renders actor
456 */
457 void
458 SVTK_DeviceActor
459 ::Render(vtkRenderer *ren, vtkMapper* m)
460 {
461   if(myIsResolveCoincidentTopology){
462     int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
463     vtkFloatingPointType aFactor, aUnit; 
464     vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
465     
466     vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
467     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
468                                                                    myPolygonOffsetUnits);
469     Superclass::Render(ren,m);
470     
471     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
472     vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
473   }else{
474     Superclass::Render(ren,m);
475   }
476 }
477
478 /*!
479   Set polygon offset parameters
480   \param factor, units  - Opengl polygon offset parameters
481 */
482 void
483 SVTK_DeviceActor
484 ::SetPolygonOffsetParameters(vtkFloatingPointType factor, 
485                              vtkFloatingPointType units)
486 {
487   myPolygonOffsetFactor = factor;
488   myPolygonOffsetUnits = units;
489 }
490
491 /*!
492   Get polygon offset parameters
493   \param factor, units  - Opengl polygon offset parameters
494 */
495 void
496 SVTK_DeviceActor
497 ::GetPolygonOffsetParameters(vtkFloatingPointType& factor, 
498                              vtkFloatingPointType& units)
499 {
500   factor = myPolygonOffsetFactor;
501   units = myPolygonOffsetUnits;
502 }
503
504 vtkDataSetMapper* SVTK_DeviceActor::GetDataSetMapper()
505 {
506   return myMapper;
507 }