]> SALOME platform Git repositories - modules/visu.git/blob - src/OBJECT/VISU_ScalarMapAct.cxx
Salome HOME
Merge from V5_1_2_BR branch (14 July 2009)
[modules/visu.git] / src / OBJECT / VISU_ScalarMapAct.cxx
1 //  Copyright (C) 2007-2008  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 //  VISU OBJECT : interactive object for VISU entities implementation
23 //  File   : VISU_MeshAct.hxx
24 //  Author : Laurent CORNABE with the help of Nicolas REJNERI
25 //  Module : VISU
26 //  $Header$
27 //
28 #include "VISU_ScalarMapAct.h"
29 #include "VISU_LookupTable.hxx"
30 #include "VISU_ScalarBarActor.hxx"
31 #include "VISU_PipeLine.hxx"
32
33 #include "VISU_OpenGLPointSpriteMapper.hxx"
34 #include "VISU_GaussPtsDeviceActor.h"
35 #include "VISU_DeformedShapePL.hxx"
36 #include "VISU_PipeLineUtils.hxx"
37
38
39 #include <SUIT_ResourceMgr.h>
40 #include <SUIT_Session.h>
41
42 #include <SALOME_ExtractGeometry.h>
43 #include <SALOME_ExtractPolyDataGeometry.h>
44
45 #include <vtkObjectFactory.h>
46 #include <vtkRenderer.h>
47 #include <vtkProperty.h>
48 #include <vtkMatrix4x4.h>
49 #include <vtkMapper.h>
50 #include <vtkDataSetMapper.h>
51
52 #include <vtkRenderWindowInteractor.h>
53 #include <vtkCallbackCommand.h>
54 #include <vtkRenderWindow.h>
55 #include <vtkImageData.h>
56
57
58 //============================================================================
59 class VISU_PointsDeviceActor: public VISU_GaussDeviceActorBase
60 {
61  public:
62   vtkTypeMacro(VISU_PointsDeviceActor, VISU_GaussDeviceActorBase);
63
64   static 
65   VISU_PointsDeviceActor* 
66   New();
67  
68
69   //----------------------------------------------------------------------------
70   virtual
71   void
72   SetInput(vtkDataSet* theDataSet)
73   {
74     myGeomFilter->SetInput( theDataSet );
75   }
76
77
78   //----------------------------------------------------------------------------
79   void
80   SetInteractor(vtkRenderWindowInteractor* theInteractor)
81   {
82     if(theInteractor == myInteractor)
83       return;
84
85     if(myInteractor)
86       myInteractor->RemoveObserver(myEventCallbackCommand);
87     
88     if(theInteractor)
89       theInteractor->AddObserver(vtkCommand::CharEvent, 
90                                  myEventCallbackCommand, 
91                                  0.0);
92
93     myInteractor = theInteractor;
94   }
95
96
97   //----------------------------------------------------------------------------
98   void
99   DoMapperShallowCopy( vtkMapper* theMapper,
100                        bool theIsCopyInput )
101   {
102     Superclass::DoMapperShallowCopy( theMapper, theIsCopyInput );
103
104     vtkDataSet* aDataSet = theMapper->GetInput();
105     vtkFloatingPointType aScaleFactor = VISU_DeformedShapePL::GetScaleFactor( aDataSet );
106
107     GetPointSpriteMapper()->SetAverageCellSize( aScaleFactor );
108   }
109
110
111   //----------------------------------------------------------------------------
112   void
113   DeepCopy( VISU_PointsDeviceActor *theActor )
114   {
115     VISU::CopyPointSpriteDataMapper( GetPointSpriteMapper(), theActor->GetPointSpriteMapper(), false );
116   }
117
118  protected:
119   //----------------------------------------------------------------------------
120   VISU_PointsDeviceActor():
121     myGeomFilter( VTKViewer_GeometryFilter::New() ),
122     myEventCallbackCommand( vtkCallbackCommand::New() ),
123     myInteractor( NULL )
124   {
125     myGeomFilter->SetInside(true);
126
127     VISU_OpenGLPointSpriteMapper* aMapper = VISU_OpenGLPointSpriteMapper::New();
128     aMapper->SetInput( myGeomFilter->GetOutput() );
129
130     std::string aRootDir( getenv( "VISU_ROOT_DIR") );
131     std::string aMainTexture = aRootDir + "/share/salome/resources/visu/sprite_texture.bmp";
132     std::string anAlphaTexture = aRootDir + "/share/salome/resources/visu/sprite_alpha.bmp";
133     VISU::TTextureValue aTextureValue = VISU::GetTexture( aMainTexture, anAlphaTexture );
134     aMapper->SetImageData( aTextureValue.GetPointer() );
135
136     aMapper->SetUseLookupTableScalarRange(true);
137     aMapper->SetColorModeToMapScalars();
138     aMapper->SetScalarVisibility(true);
139
140     SetPointSpriteMapper( aMapper );
141
142     aMapper->Delete();
143
144     myEventCallbackCommand->SetClientData( this );
145     myEventCallbackCommand->SetCallback( VISU_PointsDeviceActor::ProcessEvents );
146   }
147
148
149   //----------------------------------------------------------------------------
150   ~VISU_PointsDeviceActor()
151   {
152     SetInteractor( NULL );
153     myGeomFilter->Delete();
154     myEventCallbackCommand->Delete();
155   }
156
157
158   //----------------------------------------------------------------------------
159   static
160   void
161   ProcessEvents(vtkObject* theObject, 
162                 unsigned long theEvent,
163                 void* theClientData, 
164                 void* theCallData)
165   {
166     if ( VISU_PointsDeviceActor* self = reinterpret_cast<VISU_PointsDeviceActor*>( theClientData ) )
167       self->OnInteractorEvent( theEvent );
168   }
169
170
171   //----------------------------------------------------------------------------
172   void
173   OnInteractorEvent(unsigned long theEvent)
174   {
175     switch ( theEvent ) {
176     case vtkCommand::CharEvent: {
177       switch( myInteractor->GetKeyCode() ) {
178       case 'M' :
179       case 'm' : {
180         if ( !GetVisibility() )
181           return;
182
183         static vtkFloatingPointType anIncrement = 2;
184         vtkFloatingPointType aMagnification = GetPointSpriteMapper()->GetPointSpriteMagnification();
185         vtkFloatingPointType coefficient = myInteractor->GetShiftKey() ? anIncrement : 1 / anIncrement;
186
187         GetPointSpriteMapper()->SetPointSpriteMagnification( aMagnification * coefficient );
188
189         myInteractor->CreateTimer(VTKI_TIMER_UPDATE);
190         break;
191       }
192       default:
193         return;
194       }
195       break;
196     }
197     default:
198       return;
199     }
200   }
201
202
203   //----------------------------------------------------------------------------
204   vtkCallbackCommand* myEventCallbackCommand;
205   vtkRenderWindowInteractor* myInteractor;
206   VTKViewer_GeometryFilter* myGeomFilter;
207
208  private:
209   VISU_PointsDeviceActor(const VISU_PointsDeviceActor&); // Not implemented
210   void operator=(const VISU_PointsDeviceActor&); // Not implemented
211 };
212
213 vtkStandardNewMacro(VISU_PointsDeviceActor);
214
215 //----------------------------------------------------------------------------
216 vtkStandardNewMacro(VISU_ScalarMapAct);
217 static vtkFloatingPointType EPS = 1.0 / VTK_LARGE_FLOAT;
218
219 //----------------------------------------------------------------------------
220 VISU_ScalarMapAct
221 ::VISU_ScalarMapAct()
222 {
223   myScalarBar = VISU_ScalarBarActor::New();
224
225   vtkProperty* aProperty = GetProperty();
226   aProperty->SetAmbient(1.0); 
227   aProperty->SetDiffuse(0.0);
228   aProperty->SetSpecular(0.0);
229   
230   myProperty->DeepCopy(aProperty);
231
232   vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
233
234   mySurfaceActor= SVTK_DeviceActor::New();
235   mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
236   mySurfaceActor->SetProperty(aProperty);
237   mySurfaceActor->SetUserMatrix(aMatrix);
238
239   myEdgeActor = SVTK_DeviceActor::New();
240   myEdgeActor->SetRepresentation(SVTK::Representation::Wireframe);
241   myEdgeActor->SetUserMatrix(aMatrix);
242   myEdgeActor->GetProperty()->SetColor(255.,255.,255.);
243
244   myPointsActor = VISU_PointsDeviceActor::New();
245   myPointsActor->SetProperty(aProperty);
246   myPointsActor->SetUserMatrix(aMatrix);
247
248   aMatrix->Delete();
249
250   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
251   //Quadratic 2D elements representation
252   if(aResourceMgr) {
253     //----------------------------------------------------------------------------
254     int aQuadraticAngle = aResourceMgr->integerValue( "VISU", "max_angle", 2);
255     mySurfaceActor->SetQuadraticArcAngle(aQuadraticAngle);
256     myEdgeActor->SetQuadraticArcAngle(aQuadraticAngle);
257   }
258 }
259
260 //----------------------------------------------------------------------------
261 VISU_ScalarMapAct
262 ::~VISU_ScalarMapAct()
263 {
264   myScalarBar->Delete();
265
266   myPointsActor->Delete();
267
268   mySurfaceActor->Delete();
269   myEdgeActor->Delete();
270 }
271
272 void
273 VISU_ScalarMapAct
274 ::ShallowCopyPL(VISU_PipeLine* thePipeLine)
275 {
276   VISU_DataSetActor::ShallowCopyPL( thePipeLine );
277
278   myEdgeActor->GetMapper()->ScalarVisibilityOff();
279
280   myPointsActor->DoMapperShallowCopy( thePipeLine->GetMapper(), false );
281
282   VISU::CopyMapper( mySurfaceActor->GetMapper(), thePipeLine->GetMapper(), false );
283 }
284
285 //----------------------------------------------------------------------------
286 void
287 VISU_ScalarMapAct
288 ::SetMapperInput(vtkDataSet* theDataSet)
289 {
290   Superclass::SetMapperInput( theDataSet );
291
292 //    myPointsActor->SetInput( theDataSet );
293 //    mySurfaceActor->SetInput( theDataSet );
294 //    myEdgeActor->SetInput( theDataSet );
295
296   if (theDataSet->IsA("vtkPolyData")) {
297     myPointsActor->SetInput( myPolyDataExtractor->GetOutput() );
298     mySurfaceActor->SetInput( myPolyDataExtractor->GetOutput() );
299     myEdgeActor->SetInput( myPolyDataExtractor->GetOutput() );
300   } else {
301     myPointsActor->SetInput( myExtractor->GetOutput() );
302     mySurfaceActor->SetInput( myExtractor->GetOutput() );
303     myEdgeActor->SetInput( myExtractor->GetOutput() );
304   }
305 }
306
307 //----------------------------------------------------------------------------
308 void
309 VISU_ScalarMapAct
310 ::SetTransform(VTKViewer_Transform* theTransform)
311 {
312   Superclass::SetTransform(theTransform);
313
314   myPointsActor->SetTransform(theTransform);
315
316   mySurfaceActor->SetTransform(theTransform);
317   myEdgeActor->SetTransform(theTransform);
318 }
319
320 //----------------------------------------------------------------------------
321 void
322 VISU_ScalarMapAct
323 ::SetShrinkable(bool theIsShrinkable)
324 {
325   Superclass::SetShrinkable(theIsShrinkable);
326
327   mySurfaceActor->SetShrinkable(theIsShrinkable);
328 }
329
330 void
331 VISU_ScalarMapAct
332 ::SetShrinkFactor(vtkFloatingPointType theValue)
333 {
334   Superclass::SetShrinkFactor(theValue);
335
336   mySurfaceActor->SetShrinkFactor(theValue);
337 }
338
339 //----------------------------------------------------------------------------
340 void
341 VISU_ScalarMapAct
342 ::SetShrink()
343 {
344   if(myRepresentation == VTK_POINTS)
345     return;
346
347   Superclass::SetShrink();
348
349   mySurfaceActor->SetShrink();
350 }
351
352 void
353 VISU_ScalarMapAct
354 ::UnShrink()
355 {
356   Superclass::UnShrink();
357
358   mySurfaceActor->UnShrink();
359 }
360
361 //----------------------------------------------------------------------------
362 void
363 VISU_ScalarMapAct
364 ::SetFeatureEdgesAllowed(bool theIsFeatureEdgesAllowed)
365 {
366   Superclass::SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
367
368   mySurfaceActor->SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
369 }
370
371 void
372 VISU_ScalarMapAct
373 ::SetFeatureEdgesAngle(vtkFloatingPointType theValue)
374 {
375   Superclass::SetFeatureEdgesAngle(theValue);
376
377   mySurfaceActor->SetFeatureEdgesAngle(theValue);
378 }
379
380 void
381 VISU_ScalarMapAct
382 ::SetFeatureEdgesFlags(bool theIsFeatureEdges,
383                        bool theIsBoundaryEdges,
384                        bool theIsManifoldEdges,
385                        bool theIsNonManifoldEdges)
386 {
387   Superclass::SetFeatureEdgesFlags(theIsFeatureEdges,
388                                    theIsBoundaryEdges,
389                                    theIsManifoldEdges,
390                                    theIsNonManifoldEdges);
391
392   mySurfaceActor->SetFeatureEdgesFlags(theIsFeatureEdges,
393                                        theIsBoundaryEdges,
394                                        theIsManifoldEdges,
395                                        theIsNonManifoldEdges);
396 }
397
398 void
399 VISU_ScalarMapAct
400 ::SetFeatureEdgesColoring(bool theIsColoring)
401 {
402   Superclass::SetFeatureEdgesColoring(theIsColoring);
403
404   mySurfaceActor->SetFeatureEdgesColoring(theIsColoring);
405 }
406
407 //----------------------------------------------------------------------------
408 void
409 VISU_ScalarMapAct
410 ::SetFeatureEdgesEnabled(bool theIsFeatureEdgesEnabled)
411 {
412   if(theIsFeatureEdgesEnabled && myRepresentation == VTK_POINTS)
413     return;
414
415   Superclass::SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
416
417   mySurfaceActor->SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
418 }
419
420 //----------------------------------------------------------------------------
421 void
422 VISU_ScalarMapAct
423 ::SetOpacity(vtkFloatingPointType theValue)
424 {
425   mySurfaceActor->GetProperty()->SetOpacity(theValue);
426 }
427
428 vtkFloatingPointType
429 VISU_ScalarMapAct
430 ::GetOpacity()
431 {
432   return mySurfaceActor->GetProperty()->GetOpacity();
433 }
434
435 //----------------------------------------------------------------------------
436 void
437 VISU_ScalarMapAct
438 ::SetLineWidth(vtkFloatingPointType theLineWidth)
439 {
440   mySurfaceActor->GetProperty()->SetLineWidth(theLineWidth);
441 }
442
443 vtkFloatingPointType
444 VISU_ScalarMapAct::GetLineWidth()
445 {
446   return mySurfaceActor->GetProperty()->GetLineWidth();
447 }
448
449 //----------------------------------------------------------------------------
450 void
451 VISU_ScalarMapAct
452 ::DeepCopy(VISU_Actor *theActor)
453 {
454   if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
455     Superclass::DeepCopy(theActor);
456     SetBarVisibility(anActor->GetBarVisibility());
457     myPointsActor->DeepCopy( anActor->myPointsActor );
458     SetShading(anActor->IsShading());
459   }
460 }
461
462
463 //----------------------------------------------------------------------------
464 void
465 VISU_ScalarMapAct
466 ::AddToRender(vtkRenderer* theRenderer)
467 {
468   Superclass::AddToRender(theRenderer);
469
470   myPointsActor->SetInteractor( myInteractor );
471
472   if(myScalarBar)
473     theRenderer->AddActor2D(myScalarBar);
474 }
475
476 //----------------------------------------------------------------------------
477 void
478 VISU_ScalarMapAct
479 ::RemoveFromRender(vtkRenderer* theRenderer)
480 {
481   if(myScalarBar)
482     theRenderer->RemoveActor(myScalarBar);
483
484   if ( vtkWindow* aWindow = theRenderer->GetRenderWindow() ) {
485     myPointsActor->ReleaseGraphicsResources( aWindow );
486     mySurfaceActor->ReleaseGraphicsResources( aWindow );
487     myEdgeActor->ReleaseGraphicsResources( aWindow );
488   }
489
490   Superclass::RemoveFromRender(theRenderer);
491 }
492
493 //----------------------------------------------------------------------------
494 void
495 VISU_ScalarMapAct
496 ::SetVisibility(int theMode)
497 {
498   Superclass::SetVisibility( theMode );
499
500   myPointsActor->SetVisibility( theMode );
501
502   if(myScalarBar) 
503     myScalarBar->SetVisibility(myBarVisibility && theMode);
504 }
505
506 //----------------------------------------------------------------------------
507 int 
508 VISU_ScalarMapAct
509 ::GetBarVisibility()
510 {
511   return myBarVisibility;
512 }
513
514 //----------------------------------------------------------------------------
515 VISU_ScalarBarActor*
516 VISU_ScalarMapAct
517 ::GetScalarBar()
518 {
519   return myScalarBar;
520 }
521
522 //----------------------------------------------------------------------------
523 void
524 VISU_ScalarMapAct
525 ::SetBarVisibility(bool theMode)
526 {
527   myBarVisibility = theMode;
528   if(myScalarBar) 
529     myScalarBar->SetVisibility(myBarVisibility && GetVisibility());
530 }
531
532
533 //----------------------------------------------------------------------------
534 void
535 VISU_ScalarMapAct
536 ::SetRepresentation(int theMode) 
537
538   bool anIsShanding = IsShading();
539
540   Superclass::SetRepresentation(theMode);
541
542   if(theMode == SVTK::Representation::Surfaceframe)
543     mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
544   else
545     mySurfaceActor->SetRepresentation(theMode);
546
547   myPointsActor->SetProperty( mySurfaceActor->GetProperty() );
548
549   SetShading(anIsShanding);
550 }
551
552
553 //----------------------------------------------------------------------------
554 void
555 VISU_ScalarMapAct
556 ::SetShading(bool theOn)
557 {
558   vtkProperty* aProperty = mySurfaceActor->GetProperty();
559
560   if (theOn) {
561     aProperty->SetAmbient(0.0); 
562     aProperty->SetDiffuse(1.0);
563   } else {
564     aProperty->SetAmbient(1.0); 
565     aProperty->SetDiffuse(0.0);
566   }
567 }
568
569 //----------------------------------------------------------------------------
570 bool
571 VISU_ScalarMapAct
572 ::IsShading()
573 {
574   vtkProperty* aProperty = mySurfaceActor->GetProperty();
575   
576   return (fabs(aProperty->GetAmbient()) < EPS && fabs(aProperty->GetDiffuse() - 1.) < EPS);
577 }
578
579 int
580 VISU_ScalarMapAct
581 ::RenderOpaqueGeometry(vtkViewport *ren)
582 {
583   GetMatrix(myEdgeActor->GetUserMatrix());
584   GetMatrix(mySurfaceActor->GetUserMatrix());
585   GetMatrix(myPointsActor->GetUserMatrix());
586
587   using namespace SVTK::Representation;
588   switch ( GetRepresentation() ) {
589
590   case Surfaceframe:
591     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
592     mySurfaceActor->RenderOpaqueGeometry(ren);
593
594     myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
595     myEdgeActor->RenderOpaqueGeometry(ren);
596     break;
597
598   case Points:
599     myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
600     myPointsActor->RenderOpaqueGeometry(ren);
601     break;
602
603   default:
604     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
605     mySurfaceActor->RenderOpaqueGeometry(ren);
606   }
607
608   return 1;
609 }
610
611 int
612 VISU_ScalarMapAct
613 ::RenderTranslucentGeometry(vtkViewport *ren)
614 {
615   GetMatrix(myEdgeActor->GetUserMatrix());
616   GetMatrix(mySurfaceActor->GetUserMatrix());
617
618   using namespace SVTK::Representation;
619   switch ( GetRepresentation() ) {
620
621   case Surfaceframe:
622     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
623     mySurfaceActor->RenderTranslucentGeometry(ren);
624
625     myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
626     myEdgeActor->RenderTranslucentGeometry(ren);
627     break;
628
629   case Points:
630     myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
631     myPointsActor->RenderTranslucentGeometry(ren);
632     break;
633
634   default:
635     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
636     mySurfaceActor->RenderTranslucentGeometry(ren);
637   }
638
639   return 1;
640 }
641
642 //----------------------------------------------------------------------------
643 unsigned long int
644 VISU_ScalarMapAct
645 ::GetMemorySize()
646 {
647   unsigned long int aSize = Superclass::GetMemorySize();
648
649   aSize += myPointsActor->GetMemorySize();
650
651   return aSize;
652 }
653
654 //----------------------------------------------------------------------------
655
656 VISU_Actor::EQuadratic2DRepresentation 
657 VISU_ScalarMapAct
658 ::GetQuadratic2DRepresentation() const
659 {
660   bool mode = (mySurfaceActor->GetQuadraticArcMode() && myEdgeActor->GetQuadraticArcMode());
661   if(mode){
662     return VISU_Actor::eArcs;
663   }
664   else
665     return VISU_Actor::eLines;
666 }
667   
668 void VISU_ScalarMapAct::SetQuadratic2DRepresentation( VISU_Actor::EQuadratic2DRepresentation theMode )
669 {
670   Superclass::SetQuadratic2DRepresentation( theMode );
671   switch(theMode) {
672   case VISU_Actor::eArcs:
673     mySurfaceActor->SetQuadraticArcMode(true);
674     myEdgeActor->SetQuadraticArcMode(true);
675     break;
676   case VISU_Actor::eLines:
677     mySurfaceActor->SetQuadraticArcMode(false);
678     myEdgeActor->SetQuadraticArcMode(false);
679     break;
680   default:
681     break;
682   }
683 }