Salome HOME
35673709de805e610be4ef5f79a751e59cf59274
[modules/visu.git] / src / OBJECT / VISU_ScalarMapAct.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  VISU OBJECT : interactive object for VISU entities implementation
24 //  File   : VISU_MeshAct.hxx
25 //  Author : Laurent CORNABE with the help of Nicolas REJNERI
26 //  Module : VISU
27 //  $Header$
28 //
29 #include "VISU_ScalarMapAct.h"
30 #include "VISU_LookupTable.hxx"
31 #include "VISU_ScalarBarActor.hxx"
32 #include "VISU_PipeLine.hxx"
33
34 #include "VISU_OpenGLPointSpriteMapper.hxx"
35 #include "VISU_GaussPtsDeviceActor.h"
36 #include "VISU_DeformedShapePL.hxx"
37 #include "VISU_PipeLineUtils.hxx"
38
39
40 #include <SUIT_ResourceMgr.h>
41 #include <SUIT_Session.h>
42
43 #include <SALOME_ExtractGeometry.h>
44 #include <SALOME_ExtractPolyDataGeometry.h>
45
46 #include <vtkObjectFactory.h>
47 #include <vtkRenderer.h>
48 #include <vtkProperty.h>
49 #include <vtkMatrix4x4.h>
50 #include <vtkMapper.h>
51 #include <vtkDataSetMapper.h>
52
53 #include <vtkRenderWindowInteractor.h>
54 #include <vtkCallbackCommand.h>
55 #include <vtkRenderWindow.h>
56 #include <vtkImageData.h>
57
58
59 //----------------------------------------------------------------------------
60 vtkStandardNewMacro(VISU_ScalarMapAct);
61 static vtkFloatingPointType EPS = 1.0 / VTK_LARGE_FLOAT;
62
63 //----------------------------------------------------------------------------
64 VISU_ScalarMapAct
65 ::VISU_ScalarMapAct()
66 {
67   myScalarBar = VISU_ScalarBarActor::New();
68
69   vtkProperty* aProperty = GetProperty();
70   aProperty->SetAmbient(1.0); 
71   aProperty->SetDiffuse(0.0);
72   aProperty->SetSpecular(0.0);
73   
74   myProperty->DeepCopy(aProperty);
75
76   vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
77
78   mySurfaceActor= SVTK_DeviceActor::New();
79   mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
80   mySurfaceActor->SetProperty(aProperty);
81   mySurfaceActor->SetUserMatrix(aMatrix);
82
83   myEdgeActor = SVTK_DeviceActor::New();
84   myEdgeActor->SetRepresentation(SVTK::Representation::Wireframe);
85   myEdgeActor->SetUserMatrix(aMatrix);
86   myEdgeActor->GetProperty()->SetColor(255.,255.,255.);
87
88   myPointsActor = SVTK_DeviceActor::New();
89   myPointsActor->SetRepresentation(SVTK::Representation::Points);
90   myPointsActor->SetProperty(aProperty);
91   myPointsActor->SetUserMatrix(aMatrix);
92
93   aMatrix->Delete();
94
95   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
96   //Quadratic 2D elements representation
97   if(aResourceMgr) {
98     //----------------------------------------------------------------------------
99     int aQuadraticAngle = aResourceMgr->integerValue( "VISU", "max_angle", 2);
100     mySurfaceActor->SetQuadraticArcAngle(aQuadraticAngle);
101     myEdgeActor->SetQuadraticArcAngle(aQuadraticAngle);
102
103     int anElem0DSize = aResourceMgr->integerValue( "VISU", "elem0d_size", 5);
104     mySurfaceActor->GetProperty()->SetPointSize(anElem0DSize);
105   }
106 }
107
108 //----------------------------------------------------------------------------
109 VISU_ScalarMapAct
110 ::~VISU_ScalarMapAct()
111 {
112   myScalarBar->Delete();
113
114   myPointsActor->Delete();
115
116   mySurfaceActor->Delete();
117   myEdgeActor->Delete();
118 }
119
120 void
121 VISU_ScalarMapAct
122 ::ShallowCopyPL(VISU_PipeLine* thePipeLine)
123 {
124   VISU_DataSetActor::ShallowCopyPL( thePipeLine );
125
126   myEdgeActor->GetMapper()->ScalarVisibilityOff();
127
128   VISU::CopyMapper( myPointsActor->GetMapper(), thePipeLine->GetMapper(), false );
129   VISU::CopyMapper( mySurfaceActor->GetMapper(), thePipeLine->GetMapper(), false );
130 }
131
132 //----------------------------------------------------------------------------
133 void
134 VISU_ScalarMapAct
135 ::SetMapperInput(vtkDataSet* theDataSet)
136 {
137   Superclass::SetMapperInput( theDataSet );
138
139 //    myPointsActor->SetInput( theDataSet );
140 //    mySurfaceActor->SetInput( theDataSet );
141 //    myEdgeActor->SetInput( theDataSet );
142
143   if (theDataSet->IsA("vtkPolyData")) {
144     myPointsActor->SetInput( myPolyDataExtractor->GetOutput() );
145     mySurfaceActor->SetInput( myPolyDataExtractor->GetOutput() );
146     myEdgeActor->SetInput( myPolyDataExtractor->GetOutput() );
147   } else {
148     myPointsActor->SetInput( myExtractor->GetOutput() );
149     mySurfaceActor->SetInput( myExtractor->GetOutput() );
150     myEdgeActor->SetInput( myExtractor->GetOutput() );
151   }
152 }
153
154 //----------------------------------------------------------------------------
155 void
156 VISU_ScalarMapAct
157 ::SetTransform(VTKViewer_Transform* theTransform)
158 {
159   Superclass::SetTransform(theTransform);
160
161   myPointsActor->SetTransform(theTransform);
162
163   mySurfaceActor->SetTransform(theTransform);
164   myEdgeActor->SetTransform(theTransform);
165 }
166
167 //----------------------------------------------------------------------------
168 vtkProperty* 
169 VISU_ScalarMapAct
170 ::GetEdgeProperty()
171 {
172   return myEdgeActor->GetProperty();
173 }
174
175 //----------------------------------------------------------------------------
176 void
177 VISU_ScalarMapAct
178 ::SetShrinkable(bool theIsShrinkable)
179 {
180   Superclass::SetShrinkable(theIsShrinkable);
181
182   mySurfaceActor->SetShrinkable(theIsShrinkable);
183 }
184
185 void
186 VISU_ScalarMapAct
187 ::SetShrinkFactor(vtkFloatingPointType theValue)
188 {
189   Superclass::SetShrinkFactor(theValue);
190
191   mySurfaceActor->SetShrinkFactor(theValue);
192 }
193
194 //----------------------------------------------------------------------------
195 void
196 VISU_ScalarMapAct
197 ::SetShrink()
198 {
199   if(myRepresentation == VTK_POINTS)
200     return;
201
202   Superclass::SetShrink();
203
204   mySurfaceActor->SetShrink();
205 }
206
207 void
208 VISU_ScalarMapAct
209 ::UnShrink()
210 {
211   Superclass::UnShrink();
212
213   mySurfaceActor->UnShrink();
214 }
215
216 //----------------------------------------------------------------------------
217 void
218 VISU_ScalarMapAct
219 ::SetFeatureEdgesAllowed(bool theIsFeatureEdgesAllowed)
220 {
221   Superclass::SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
222
223   mySurfaceActor->SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
224 }
225
226 void
227 VISU_ScalarMapAct
228 ::SetFeatureEdgesAngle(vtkFloatingPointType theValue)
229 {
230   Superclass::SetFeatureEdgesAngle(theValue);
231
232   mySurfaceActor->SetFeatureEdgesAngle(theValue);
233 }
234
235 void
236 VISU_ScalarMapAct
237 ::SetFeatureEdgesFlags(bool theIsFeatureEdges,
238                        bool theIsBoundaryEdges,
239                        bool theIsManifoldEdges,
240                        bool theIsNonManifoldEdges)
241 {
242   Superclass::SetFeatureEdgesFlags(theIsFeatureEdges,
243                                    theIsBoundaryEdges,
244                                    theIsManifoldEdges,
245                                    theIsNonManifoldEdges);
246
247   mySurfaceActor->SetFeatureEdgesFlags(theIsFeatureEdges,
248                                        theIsBoundaryEdges,
249                                        theIsManifoldEdges,
250                                        theIsNonManifoldEdges);
251 }
252
253 void
254 VISU_ScalarMapAct
255 ::SetFeatureEdgesColoring(bool theIsColoring)
256 {
257   Superclass::SetFeatureEdgesColoring(theIsColoring);
258
259   mySurfaceActor->SetFeatureEdgesColoring(theIsColoring);
260 }
261
262 //----------------------------------------------------------------------------
263 void
264 VISU_ScalarMapAct
265 ::SetFeatureEdgesEnabled(bool theIsFeatureEdgesEnabled)
266 {
267   if(theIsFeatureEdgesEnabled && myRepresentation == VTK_POINTS)
268     return;
269
270   Superclass::SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
271
272   mySurfaceActor->SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
273 }
274
275 //----------------------------------------------------------------------------
276 void
277 VISU_ScalarMapAct
278 ::SetOpacity(vtkFloatingPointType theValue)
279 {
280   mySurfaceActor->GetProperty()->SetOpacity(theValue);
281 }
282
283 vtkFloatingPointType
284 VISU_ScalarMapAct
285 ::GetOpacity()
286 {
287   return mySurfaceActor->GetProperty()->GetOpacity();
288 }
289
290 //----------------------------------------------------------------------------
291 void
292 VISU_ScalarMapAct
293 ::SetLineWidth(vtkFloatingPointType theLineWidth)
294 {
295   mySurfaceActor->GetProperty()->SetLineWidth(theLineWidth);
296 }
297
298 vtkFloatingPointType
299 VISU_ScalarMapAct::GetLineWidth()
300 {
301   return mySurfaceActor->GetProperty()->GetLineWidth();
302 }
303
304 //----------------------------------------------------------------------------
305 void
306 VISU_ScalarMapAct
307 ::DeepCopy(VISU_Actor *theActor)
308 {
309   if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
310     Superclass::DeepCopy(theActor);
311     SetBarVisibility(anActor->GetBarVisibility());
312     SetShading(anActor->IsShading());
313   }
314 }
315
316
317 //----------------------------------------------------------------------------
318 void
319 VISU_ScalarMapAct
320 ::AddToRender(vtkRenderer* theRenderer)
321 {
322   Superclass::AddToRender(theRenderer);
323
324   if(myScalarBar)
325     theRenderer->AddActor2D(myScalarBar);
326 }
327
328 //----------------------------------------------------------------------------
329 void
330 VISU_ScalarMapAct
331 ::RemoveFromRender(vtkRenderer* theRenderer)
332 {
333   if(myScalarBar)
334     theRenderer->RemoveActor(myScalarBar);
335
336   if ( vtkWindow* aWindow = theRenderer->GetRenderWindow() ) {
337     myPointsActor->ReleaseGraphicsResources( aWindow );
338     mySurfaceActor->ReleaseGraphicsResources( aWindow );
339     myEdgeActor->ReleaseGraphicsResources( aWindow );
340   }
341
342   Superclass::RemoveFromRender(theRenderer);
343 }
344
345 //----------------------------------------------------------------------------
346 void
347 VISU_ScalarMapAct
348 ::SetVisibility(int theMode)
349 {
350   Superclass::SetVisibility( theMode );
351
352   myPointsActor->SetVisibility( theMode );
353
354   if(myScalarBar) 
355     myScalarBar->SetVisibility(myBarVisibility && theMode);
356 }
357
358 //----------------------------------------------------------------------------
359 int 
360 VISU_ScalarMapAct
361 ::GetBarVisibility()
362 {
363   return myBarVisibility;
364 }
365
366 //----------------------------------------------------------------------------
367 VISU_ScalarBarActor*
368 VISU_ScalarMapAct
369 ::GetScalarBar()
370 {
371   return myScalarBar;
372 }
373
374 //----------------------------------------------------------------------------
375 void
376 VISU_ScalarMapAct
377 ::SetBarVisibility(bool theMode)
378 {
379   myBarVisibility = theMode;
380   if(myScalarBar) 
381     myScalarBar->SetVisibility(myBarVisibility && GetVisibility());
382 }
383
384
385 //----------------------------------------------------------------------------
386 void
387 VISU_ScalarMapAct
388 ::SetRepresentation(int theMode) 
389
390   bool anIsShanding = IsShading();
391
392   Superclass::SetRepresentation(theMode);
393
394   if(theMode == SVTK::Representation::Surfaceframe)
395     mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
396   else
397     mySurfaceActor->SetRepresentation(theMode);
398
399   myPointsActor->SetProperty( mySurfaceActor->GetProperty() );
400
401   SetShading(anIsShanding);
402 }
403
404
405 //----------------------------------------------------------------------------
406 void
407 VISU_ScalarMapAct
408 ::SetShading(bool theOn)
409 {
410   vtkProperty* aProperty = mySurfaceActor->GetProperty();
411
412   if (theOn) {
413     aProperty->SetAmbient(0.0); 
414     aProperty->SetDiffuse(1.0);
415   } else {
416     aProperty->SetAmbient(1.0); 
417     aProperty->SetDiffuse(0.0);
418   }
419 }
420
421 //----------------------------------------------------------------------------
422 bool
423 VISU_ScalarMapAct
424 ::IsShading()
425 {
426   vtkProperty* aProperty = mySurfaceActor->GetProperty();
427   
428   return (fabs(aProperty->GetAmbient()) < EPS && fabs(aProperty->GetDiffuse() - 1.) < EPS);
429 }
430
431 int
432 VISU_ScalarMapAct
433 ::RenderOpaqueGeometry(vtkViewport *ren)
434 {
435   GetMatrix(myEdgeActor->GetUserMatrix());
436   GetMatrix(mySurfaceActor->GetUserMatrix());
437   GetMatrix(myPointsActor->GetUserMatrix());
438
439   using namespace SVTK::Representation;
440   switch ( GetRepresentation() ) {
441
442   case Surfaceframe:
443     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
444     mySurfaceActor->RenderOpaqueGeometry(ren);
445
446     myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
447     myEdgeActor->RenderOpaqueGeometry(ren);
448     break;
449
450   case Points:
451     myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
452     myPointsActor->RenderOpaqueGeometry(ren);
453     break;
454
455   default:
456     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
457     mySurfaceActor->RenderOpaqueGeometry(ren);
458   }
459
460   return 1;
461 }
462
463 int
464 VISU_ScalarMapAct
465 ::RenderTranslucentGeometry(vtkViewport *ren)
466 {
467   GetMatrix(myEdgeActor->GetUserMatrix());
468   GetMatrix(mySurfaceActor->GetUserMatrix());
469
470   using namespace SVTK::Representation;
471   switch ( GetRepresentation() ) {
472
473   case Surfaceframe:
474     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
475 #if (VTK_XVERSION < 0x050100)
476     mySurfaceActor->RenderTranslucentGeometry(ren);
477 #else
478     mySurfaceActor->RenderTranslucentPolygonalGeometry(ren);
479 #endif
480
481     myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
482 #if (VTK_XVERSION < 0x050100)
483     myEdgeActor->RenderTranslucentGeometry(ren);
484 #else
485     myEdgeActor->RenderTranslucentPolygonalGeometry(ren);
486 #endif
487     break;
488
489   case Points:
490     myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
491 #if (VTK_XVERSION < 0x050100)
492     myPointsActor->RenderTranslucentGeometry(ren);
493 #else
494     myPointsActor->RenderTranslucentPolygonalGeometry(ren);
495 #endif
496     break;
497
498   default:
499     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
500 #if (VTK_XVERSION < 0x050100)
501     mySurfaceActor->RenderTranslucentGeometry(ren);
502 #else
503     mySurfaceActor->RenderTranslucentPolygonalGeometry(ren);
504 #endif
505   }
506
507   return 1;
508 }
509
510 //----------------------------------------------------------------------------
511
512 VISU_Actor::EQuadratic2DRepresentation 
513 VISU_ScalarMapAct
514 ::GetQuadratic2DRepresentation() const
515 {
516   bool mode = (mySurfaceActor->GetQuadraticArcMode() && myEdgeActor->GetQuadraticArcMode());
517   if(mode){
518     return VISU_Actor::eArcs;
519   }
520   else
521     return VISU_Actor::eLines;
522 }
523   
524 void VISU_ScalarMapAct::SetQuadratic2DRepresentation( VISU_Actor::EQuadratic2DRepresentation theMode )
525 {
526   Superclass::SetQuadratic2DRepresentation( theMode );
527   switch(theMode) {
528   case VISU_Actor::eArcs:
529     mySurfaceActor->SetQuadraticArcMode(true);
530     myEdgeActor->SetQuadraticArcMode(true);
531     break;
532   case VISU_Actor::eLines:
533     mySurfaceActor->SetQuadraticArcMode(false);
534     myEdgeActor->SetQuadraticArcMode(false);
535     break;
536   default:
537     break;
538   }
539 }
540
541 void VISU_ScalarMapAct::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
542 {
543   Superclass::SetMarkerStd( theMarkerType, theMarkerScale );
544   myPointsActor->SetMarkerStd( theMarkerType, theMarkerScale );
545 }
546
547 void VISU_ScalarMapAct::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
548 {
549   Superclass::SetMarkerTexture( theMarkerId, theMarkerTexture );
550   myPointsActor->SetMarkerTexture( theMarkerId, theMarkerTexture );
551 }
552
553 /**
554  * Set size of the 0D elements.
555  */
556 void VISU_ScalarMapAct::Set0DElemSize(vtkFloatingPointType theValue) {
557   mySurfaceActor->GetProperty()->SetPointSize(theValue);
558 }
559
560 /**
561  * Get size of the 0D elements.
562  */
563 vtkFloatingPointType VISU_ScalarMapAct::Get0DElemSize() {
564   return mySurfaceActor->GetProperty()->GetPointSize();
565 }
566