Salome HOME
d2f72e2b8e8f40b980fdf32092b66e3fd456e6c0
[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   static float MIN_DISTANCE = 1.0 / VTK_LARGE_FLOAT;
412
413   if(myIsInfinite || GetLength() < MIN_DISTANCE)
414     return true;
415   else
416     return false;
417 }
418
419
420 float* 
421 VTKViewer_Actor
422 ::GetBounds()
423 {
424   return Superclass::GetBounds();
425 }
426
427
428 void
429 VTKViewer_Actor
430 ::GetBounds(float theBounds[6])
431 {
432   Superclass::GetBounds(theBounds);
433 }
434
435
436 //----------------------------------------------------------------------------
437 bool
438 VTKViewer_Actor
439 ::IsSetCamera() const 
440
441   return false; 
442 }
443
444 bool
445 VTKViewer_Actor
446 ::IsResizable() const 
447
448   return false; 
449 }
450
451 void
452 VTKViewer_Actor
453 ::SetSize( const float ) 
454 {}
455
456
457 void 
458 VTKViewer_Actor
459 ::SetCamera( vtkCamera* ) 
460 {}
461
462 //----------------------------------------------------------------------------
463 void
464 VTKViewer_Actor
465 ::SetOpacity(float theOpacity)
466
467   myOpacity = theOpacity;
468   GetProperty()->SetOpacity(theOpacity);
469 }
470
471 float
472 VTKViewer_Actor
473 ::GetOpacity()
474 {
475   return myOpacity;
476 }
477
478
479 void
480 VTKViewer_Actor
481 ::SetColor(float r,float g,float b)
482 {
483   GetProperty()->SetColor(r,g,b);
484 }
485
486 void
487 VTKViewer_Actor
488 ::SetColor(const float theRGB[3])
489
490   SetColor(theRGB[0],theRGB[1],theRGB[2]);
491 }
492
493 void
494 VTKViewer_Actor
495 ::GetColor(float& r,float& g,float& b)
496 {
497   float aColor[3];
498   GetProperty()->GetColor(aColor);
499   r = aColor[0];
500   g = aColor[1];
501   b = aColor[2];
502 }
503
504
505 //----------------------------------------------------------------------------
506 int
507 VTKViewer_Actor
508 ::getDisplayMode()
509
510   return myDisplayMode; 
511 }
512
513 void
514 VTKViewer_Actor
515 ::setDisplayMode(int theMode)
516
517   SetRepresentation(theMode + 1); 
518   myDisplayMode = GetRepresentation() - 1;
519 }
520
521
522 //----------------------------------------------------------------------------
523 bool
524 VTKViewer_Actor
525 ::hasHighlight() 
526
527   return false; 
528
529
530 bool
531 VTKViewer_Actor
532 ::isHighlighted() 
533
534   return myIsHighlighted; 
535 }
536
537 void
538 VTKViewer_Actor
539 ::SetPreSelected(bool thePreselect) 
540
541   myIsPreselected = thePreselect;
542 }
543
544
545 //----------------------------------------------------------------
546 void
547 VTKViewer_Actor
548 ::highlight(bool theIsHighlight)
549 {
550   myIsHighlighted = theIsHighlight; 
551 }
552
553 vtkCxxSetObjectMacro(VTKViewer_Actor,PreviewProperty,vtkProperty);