Salome HOME
da05fc98df4fa0c0a82b9c96066d6c83c94210c8
[modules/gui.git] / src / VTKViewer / VTKViewer_Actor.cxx
1 //  SALOME OBJECT : implementation of interactive object visualization for OCC and VTK viewers
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   : SALOME_Actor.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 /*!
30   \class SALOME_Actor SALOME_Actor.h
31   \brief Abstract class of SALOME Objects in VTK.
32 */
33
34
35 #include "VTKViewer_Actor.h"
36
37 #include "VTKViewer_Transform.h"
38 #include "VTKViewer_TransformFilter.h"
39 #include "VTKViewer_PassThroughFilter.h"
40 #include "VTKViewer_GeometryFilter.h"
41
42 // VTK Includes
43 #include <vtkCell.h>
44 #include <vtkPolyData.h>
45 #include <vtkObjectFactory.h>
46 #include <vtkDataSetMapper.h>
47 #include <vtkPolyDataMapper.h>
48 #include <vtkProperty.h>
49 #include <vtkRenderer.h>
50
51 using namespace std;
52
53 #if defined __GNUC__
54   #if __GNUC__ == 2
55     #define __GNUC_2__
56   #endif
57 #endif
58
59 int VTKViewer_POINT_SIZE = 5;
60 int VTKViewer_LINE_WIDTH = 3;
61
62
63 //----------------------------------------------------------------------------
64 vtkStandardNewMacro(VTKViewer_Actor);
65
66
67 //----------------------------------------------------------------------------
68 VTKViewer_Actor
69 ::VTKViewer_Actor():
70   myIsHighlighted(false),
71   myIsPreselected(false),
72   myRepresentation(VTK_SURFACE),
73   myDisplayMode(1),
74   myProperty(vtkProperty::New()),
75   PreviewProperty(NULL),
76   myIsInfinite(false),
77   myIsResolveCoincidentTopology(true),
78   myStoreMapping(false),
79   myGeomFilter(VTKViewer_GeometryFilter::New()),
80   myTransformFilter(VTKViewer_TransformFilter::New())
81 {
82   vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
83                                                                  myPolygonOffsetUnits);
84
85   for(int i = 0; i < 6; i++)
86     myPassFilter.push_back(VTKViewer_PassThroughFilter::New());
87 }
88
89
90 //----------------------------------------------------------------------------
91 VTKViewer_Actor
92 ::~VTKViewer_Actor()
93 {
94   SetPreviewProperty(NULL);
95
96   myGeomFilter->Delete();
97
98   myTransformFilter->Delete();
99
100   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++)
101     if(myPassFilter[i])
102       myPassFilter[i]->Delete();
103   
104   myProperty->Delete();
105 }
106
107
108 //----------------------------------------------------------------------------
109 const char* 
110 VTKViewer_Actor
111 ::getName() 
112
113   return myName.c_str(); 
114 }
115
116 void
117 VTKViewer_Actor
118 ::setName(const char* theName)
119 {
120   myName = theName;
121 }
122
123
124 //----------------------------------------------------------------------------
125 void
126 VTKViewer_Actor
127 ::AddToRender(vtkRenderer* theRenderer)
128 {
129   theRenderer->AddActor(this);
130 }
131
132 void 
133 VTKViewer_Actor
134 ::RemoveFromRender(vtkRenderer* theRenderer)
135 {
136   theRenderer->RemoveActor(this);
137 }
138
139 void
140 VTKViewer_Actor
141 ::GetChildActors(vtkActorCollection*) 
142 {}
143
144
145 //----------------------------------------------------------------------------
146 void
147 VTKViewer_Actor
148 ::SetTransform(VTKViewer_Transform* theTransform)
149 {
150   myTransformFilter->SetTransform(theTransform);
151 }
152
153
154 void
155 VTKViewer_Actor
156 ::SetMapper(vtkMapper* theMapper)
157 {
158   InitPipeLine(theMapper);
159 }
160
161 void
162 VTKViewer_Actor
163 ::InitPipeLine(vtkMapper* theMapper)
164 {
165   if(theMapper){
166     int anId = 0;
167     myPassFilter[ anId ]->SetInput( theMapper->GetInput() );
168     myPassFilter[ anId + 1]->SetInput( myPassFilter[ anId ]->GetOutput() );
169     
170     anId++; // 1
171     myGeomFilter->SetStoreMapping( myStoreMapping );
172     myGeomFilter->SetInput( myPassFilter[ anId ]->GetOutput() );
173
174     anId++; // 2
175     myPassFilter[ anId ]->SetInput( myGeomFilter->GetOutput() ); 
176     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
177
178     anId++; // 3
179     myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
180
181     anId++; // 4
182     myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
183     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
184
185     anId++; // 5
186     if(vtkDataSetMapper* aMapper = dynamic_cast<vtkDataSetMapper*>(theMapper)){
187       aMapper->SetInput(myPassFilter[anId]->GetOutput());
188     }else if(vtkPolyDataMapper* aMapper = dynamic_cast<vtkPolyDataMapper*>(theMapper)){
189       aMapper->SetInput(myPassFilter[anId]->GetPolyDataOutput());
190     }
191   }
192   Superclass::SetMapper(theMapper);
193 }
194
195
196 //----------------------------------------------------------------------------
197 void
198 VTKViewer_Actor
199 ::Render(vtkRenderer *ren, vtkMapper* m)
200 {
201   if(myIsResolveCoincidentTopology){
202     int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
203     float aFactor, aUnit; 
204     vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
205     
206     vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
207     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
208                                                                    myPolygonOffsetUnits);
209     Superclass::Render(ren,m);
210     
211     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
212     vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
213   }else{
214     Superclass::Render(ren,m);
215   }
216 }
217
218
219 void
220 VTKViewer_Actor
221 ::SetResolveCoincidentTopology(bool theIsResolve) 
222 {
223   myIsResolveCoincidentTopology = theIsResolve;
224 }
225
226 void
227 VTKViewer_Actor
228 ::SetPolygonOffsetParameters(float factor, float units)
229 {
230   myPolygonOffsetFactor = factor;
231   myPolygonOffsetUnits = units;
232 }
233
234 void
235 VTKViewer_Actor
236 ::GetPolygonOffsetParameters(float& factor, float& units)
237 {
238   factor = myPolygonOffsetFactor;
239   units = myPolygonOffsetUnits;
240 }
241
242
243 //----------------------------------------------------------------------------
244 float
245 VTKViewer_Actor
246 ::GetShrinkFactor() 
247
248   return 1.0;
249 }
250
251 bool
252 VTKViewer_Actor
253 ::IsShrunkable() 
254
255   return false;
256 }
257
258 bool
259 VTKViewer_Actor
260 ::IsShrunk() 
261
262   return false;
263 }
264
265 void
266 VTKViewer_Actor
267 ::SetShrink() 
268 {} 
269
270 void
271 VTKViewer_Actor
272 ::UnShrink() 
273 {}
274
275
276 //----------------------------------------------------------------------------
277 vtkDataSet* 
278 VTKViewer_Actor
279 ::GetInput()
280 {
281   return myPassFilter.front()->GetOutput();
282 }
283
284
285 unsigned long int
286 VTKViewer_Actor
287 ::GetMTime()
288 {
289   unsigned long mTime = this->Superclass::GetMTime();
290   unsigned long time = myTransformFilter->GetMTime();
291   mTime = ( time > mTime ? time : mTime );
292   if(vtkDataSet *aDataSet = myPassFilter[0]->GetInput()){
293     time = aDataSet->GetMTime();
294     mTime = ( time > mTime ? time : mTime );
295   }
296   return mTime;
297 }
298
299
300 //----------------------------------------------------------------------------
301 void
302 VTKViewer_Actor
303 ::SetRepresentation(int theMode) 
304
305   switch(myRepresentation){
306   case VTK_POINTS : 
307   case VTK_SURFACE : 
308     myProperty->DeepCopy(GetProperty());
309   }    
310   switch(theMode){
311   case VTK_POINTS : 
312   case VTK_SURFACE : 
313     GetProperty()->DeepCopy(myProperty);
314     break;
315   default:
316     GetProperty()->SetAmbient(1.0);
317     GetProperty()->SetDiffuse(0.0);
318     GetProperty()->SetSpecular(0.0);
319   }
320   switch(theMode){
321   case 3 : 
322     myGeomFilter->SetInside(true);
323     myGeomFilter->SetWireframeMode(true);
324     GetProperty()->SetRepresentation(VTK_WIREFRAME);
325     break;
326   case VTK_POINTS : 
327     GetProperty()->SetPointSize(VTKViewer_POINT_SIZE);  
328     GetProperty()->SetRepresentation(theMode);
329     myGeomFilter->SetWireframeMode(false);
330     myGeomFilter->SetInside(false);
331     break;
332   case VTK_WIREFRAME : 
333     GetProperty()->SetRepresentation(theMode);
334     myGeomFilter->SetWireframeMode(true);
335     myGeomFilter->SetInside(false);
336     break;
337   case VTK_SURFACE : 
338     GetProperty()->SetRepresentation(theMode);
339     myGeomFilter->SetWireframeMode(false);
340     myGeomFilter->SetInside(false);
341     break;
342   }
343   myRepresentation = theMode;
344 }
345
346 int
347 VTKViewer_Actor
348 ::GetRepresentation()
349
350   return myRepresentation;
351 }
352
353
354 //----------------------------------------------------------------------------
355 int 
356 VTKViewer_Actor
357 ::GetNodeObjId(int theVtkID)
358
359   return theVtkID;
360 }
361
362 float* 
363 VTKViewer_Actor
364 ::GetNodeCoord(int theObjID)
365 {
366   return GetInput()->GetPoint(theObjID);
367 }
368
369 vtkCell* 
370 VTKViewer_Actor
371 ::GetElemCell(int theObjID)
372 {
373   return GetInput()->GetCell(theObjID);
374 }
375
376 int
377 VTKViewer_Actor
378 ::GetElemObjId(int theVtkID) 
379
380   return theVtkID;
381 }
382
383
384 //=================================================================================
385 // function : GetObjDimension
386 // purpose  : Return object dimension.
387 //            Virtual method shoulb be redifined by derived classes
388 //=================================================================================
389 int
390 VTKViewer_Actor
391 ::GetObjDimension( const int theObjId )
392 {
393   if ( vtkCell* aCell = GetElemCell(theObjId) )
394     return aCell->GetCellDimension();
395   return 0;
396 }
397
398
399 void
400 VTKViewer_Actor
401 ::SetInfinitive(bool theIsInfinite)
402
403   myIsInfinite = theIsInfinite;
404 }
405
406
407 bool
408 VTKViewer_Actor
409 ::IsInfinitive()
410
411   return myIsInfinite; 
412 }
413
414
415 float* 
416 VTKViewer_Actor
417 ::GetBounds()
418 {
419   return Superclass::GetBounds();
420 }
421
422
423 void
424 VTKViewer_Actor
425 ::GetBounds(float theBounds[6])
426 {
427   Superclass::GetBounds(theBounds);
428 }
429
430
431 //----------------------------------------------------------------------------
432 bool
433 VTKViewer_Actor
434 ::IsSetCamera() const 
435
436   return false; 
437 }
438
439 bool
440 VTKViewer_Actor
441 ::IsResizable() const 
442
443   return false; 
444 }
445
446 void
447 VTKViewer_Actor
448 ::SetSize( const float ) 
449 {}
450
451
452 void 
453 VTKViewer_Actor
454 ::SetCamera( vtkCamera* ) 
455 {}
456
457 //----------------------------------------------------------------------------
458 void
459 VTKViewer_Actor
460 ::SetOpacity(float theOpacity)
461
462   myOpacity = theOpacity;
463   GetProperty()->SetOpacity(theOpacity);
464 }
465
466 float
467 VTKViewer_Actor
468 ::GetOpacity()
469 {
470   return myOpacity;
471 }
472
473
474 void
475 VTKViewer_Actor
476 ::SetColor(float r,float g,float b)
477 {
478   GetProperty()->SetColor(r,g,b);
479 }
480
481 void
482 VTKViewer_Actor
483 ::SetColor(const float theRGB[3])
484
485   SetColor(theRGB[0],theRGB[1],theRGB[2]);
486 }
487
488 void
489 VTKViewer_Actor
490 ::GetColor(float& r,float& g,float& b)
491 {
492   float aColor[3];
493   GetProperty()->GetColor(aColor);
494   r = aColor[0];
495   g = aColor[1];
496   b = aColor[2];
497 }
498
499
500 //----------------------------------------------------------------------------
501 int
502 VTKViewer_Actor
503 ::getDisplayMode()
504
505   return myDisplayMode; 
506 }
507
508 void
509 VTKViewer_Actor
510 ::setDisplayMode(int theMode)
511
512   SetRepresentation(theMode + 1); 
513   myDisplayMode = GetRepresentation() - 1;
514 }
515
516
517 //----------------------------------------------------------------------------
518 bool
519 VTKViewer_Actor
520 ::hasHighlight() 
521
522   return false; 
523
524
525 bool
526 VTKViewer_Actor
527 ::isHighlighted() 
528
529   return myIsHighlighted; 
530 }
531
532 void
533 VTKViewer_Actor
534 ::SetPreSelected(bool thePreselect) 
535
536   myIsPreselected = thePreselect;
537 }
538
539
540 //----------------------------------------------------------------
541 void
542 VTKViewer_Actor
543 ::highlight(bool theIsHighlight)
544 {
545   myIsHighlighted = theIsHighlight; 
546 }
547
548 vtkCxxSetObjectMacro(VTKViewer_Actor,PreviewProperty,vtkProperty);