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