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