]> SALOME platform Git repositories - modules/visu.git/blob - src/OBJECT/VISU_ScalarMapAct.cxx
Salome HOME
Issue 0020306: EDF 981 VISU : Color of edges of a surface frame representation
[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 vtkProperty* 
322 VISU_ScalarMapAct
323 ::GetEdgeProperty()
324 {
325   return myEdgeActor->GetProperty();
326 }
327
328 //----------------------------------------------------------------------------
329 void
330 VISU_ScalarMapAct
331 ::SetShrinkable(bool theIsShrinkable)
332 {
333   Superclass::SetShrinkable(theIsShrinkable);
334
335   mySurfaceActor->SetShrinkable(theIsShrinkable);
336 }
337
338 void
339 VISU_ScalarMapAct
340 ::SetShrinkFactor(vtkFloatingPointType theValue)
341 {
342   Superclass::SetShrinkFactor(theValue);
343
344   mySurfaceActor->SetShrinkFactor(theValue);
345 }
346
347 //----------------------------------------------------------------------------
348 void
349 VISU_ScalarMapAct
350 ::SetShrink()
351 {
352   if(myRepresentation == VTK_POINTS)
353     return;
354
355   Superclass::SetShrink();
356
357   mySurfaceActor->SetShrink();
358 }
359
360 void
361 VISU_ScalarMapAct
362 ::UnShrink()
363 {
364   Superclass::UnShrink();
365
366   mySurfaceActor->UnShrink();
367 }
368
369 //----------------------------------------------------------------------------
370 void
371 VISU_ScalarMapAct
372 ::SetFeatureEdgesAllowed(bool theIsFeatureEdgesAllowed)
373 {
374   Superclass::SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
375
376   mySurfaceActor->SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
377 }
378
379 void
380 VISU_ScalarMapAct
381 ::SetFeatureEdgesAngle(vtkFloatingPointType theValue)
382 {
383   Superclass::SetFeatureEdgesAngle(theValue);
384
385   mySurfaceActor->SetFeatureEdgesAngle(theValue);
386 }
387
388 void
389 VISU_ScalarMapAct
390 ::SetFeatureEdgesFlags(bool theIsFeatureEdges,
391                        bool theIsBoundaryEdges,
392                        bool theIsManifoldEdges,
393                        bool theIsNonManifoldEdges)
394 {
395   Superclass::SetFeatureEdgesFlags(theIsFeatureEdges,
396                                    theIsBoundaryEdges,
397                                    theIsManifoldEdges,
398                                    theIsNonManifoldEdges);
399
400   mySurfaceActor->SetFeatureEdgesFlags(theIsFeatureEdges,
401                                        theIsBoundaryEdges,
402                                        theIsManifoldEdges,
403                                        theIsNonManifoldEdges);
404 }
405
406 void
407 VISU_ScalarMapAct
408 ::SetFeatureEdgesColoring(bool theIsColoring)
409 {
410   Superclass::SetFeatureEdgesColoring(theIsColoring);
411
412   mySurfaceActor->SetFeatureEdgesColoring(theIsColoring);
413 }
414
415 //----------------------------------------------------------------------------
416 void
417 VISU_ScalarMapAct
418 ::SetFeatureEdgesEnabled(bool theIsFeatureEdgesEnabled)
419 {
420   if(theIsFeatureEdgesEnabled && myRepresentation == VTK_POINTS)
421     return;
422
423   Superclass::SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
424
425   mySurfaceActor->SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
426 }
427
428 //----------------------------------------------------------------------------
429 void
430 VISU_ScalarMapAct
431 ::SetOpacity(vtkFloatingPointType theValue)
432 {
433   mySurfaceActor->GetProperty()->SetOpacity(theValue);
434 }
435
436 vtkFloatingPointType
437 VISU_ScalarMapAct
438 ::GetOpacity()
439 {
440   return mySurfaceActor->GetProperty()->GetOpacity();
441 }
442
443 //----------------------------------------------------------------------------
444 void
445 VISU_ScalarMapAct
446 ::SetLineWidth(vtkFloatingPointType theLineWidth)
447 {
448   mySurfaceActor->GetProperty()->SetLineWidth(theLineWidth);
449 }
450
451 vtkFloatingPointType
452 VISU_ScalarMapAct::GetLineWidth()
453 {
454   return mySurfaceActor->GetProperty()->GetLineWidth();
455 }
456
457 //----------------------------------------------------------------------------
458 void
459 VISU_ScalarMapAct
460 ::DeepCopy(VISU_Actor *theActor)
461 {
462   if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
463     Superclass::DeepCopy(theActor);
464     SetBarVisibility(anActor->GetBarVisibility());
465     myPointsActor->DeepCopy( anActor->myPointsActor );
466     SetShading(anActor->IsShading());
467     GetEdgeProperty()->DeepCopy(anActor->GetEdgeProperty());
468   }
469 }
470
471
472 //----------------------------------------------------------------------------
473 void
474 VISU_ScalarMapAct
475 ::AddToRender(vtkRenderer* theRenderer)
476 {
477   Superclass::AddToRender(theRenderer);
478
479   myPointsActor->SetInteractor( myInteractor );
480
481   if(myScalarBar)
482     theRenderer->AddActor2D(myScalarBar);
483 }
484
485 //----------------------------------------------------------------------------
486 void
487 VISU_ScalarMapAct
488 ::RemoveFromRender(vtkRenderer* theRenderer)
489 {
490   if(myScalarBar)
491     theRenderer->RemoveActor(myScalarBar);
492
493   if ( vtkWindow* aWindow = theRenderer->GetRenderWindow() ) {
494     myPointsActor->ReleaseGraphicsResources( aWindow );
495     mySurfaceActor->ReleaseGraphicsResources( aWindow );
496     myEdgeActor->ReleaseGraphicsResources( aWindow );
497   }
498
499   Superclass::RemoveFromRender(theRenderer);
500 }
501
502 //----------------------------------------------------------------------------
503 void
504 VISU_ScalarMapAct
505 ::SetVisibility(int theMode)
506 {
507   Superclass::SetVisibility( theMode );
508
509   myPointsActor->SetVisibility( theMode );
510
511   if(myScalarBar) 
512     myScalarBar->SetVisibility(myBarVisibility && theMode);
513 }
514
515 //----------------------------------------------------------------------------
516 int 
517 VISU_ScalarMapAct
518 ::GetBarVisibility()
519 {
520   return myBarVisibility;
521 }
522
523 //----------------------------------------------------------------------------
524 VISU_ScalarBarActor*
525 VISU_ScalarMapAct
526 ::GetScalarBar()
527 {
528   return myScalarBar;
529 }
530
531 //----------------------------------------------------------------------------
532 void
533 VISU_ScalarMapAct
534 ::SetBarVisibility(bool theMode)
535 {
536   myBarVisibility = theMode;
537   if(myScalarBar) 
538     myScalarBar->SetVisibility(myBarVisibility && GetVisibility());
539 }
540
541
542 //----------------------------------------------------------------------------
543 void
544 VISU_ScalarMapAct
545 ::SetRepresentation(int theMode) 
546
547   bool anIsShanding = IsShading();
548
549   Superclass::SetRepresentation(theMode);
550
551   if(theMode == SVTK::Representation::Surfaceframe)
552     mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
553   else
554     mySurfaceActor->SetRepresentation(theMode);
555
556   myPointsActor->SetProperty( mySurfaceActor->GetProperty() );
557
558   SetShading(anIsShanding);
559 }
560
561
562 //----------------------------------------------------------------------------
563 void
564 VISU_ScalarMapAct
565 ::SetShading(bool theOn)
566 {
567   vtkProperty* aProperty = mySurfaceActor->GetProperty();
568
569   if (theOn) {
570     aProperty->SetAmbient(0.0); 
571     aProperty->SetDiffuse(1.0);
572   } else {
573     aProperty->SetAmbient(1.0); 
574     aProperty->SetDiffuse(0.0);
575   }
576 }
577
578 //----------------------------------------------------------------------------
579 bool
580 VISU_ScalarMapAct
581 ::IsShading()
582 {
583   vtkProperty* aProperty = mySurfaceActor->GetProperty();
584   
585   return (fabs(aProperty->GetAmbient()) < EPS && fabs(aProperty->GetDiffuse() - 1.) < EPS);
586 }
587
588 int
589 VISU_ScalarMapAct
590 ::RenderOpaqueGeometry(vtkViewport *ren)
591 {
592   GetMatrix(myEdgeActor->GetUserMatrix());
593   GetMatrix(mySurfaceActor->GetUserMatrix());
594   GetMatrix(myPointsActor->GetUserMatrix());
595
596   using namespace SVTK::Representation;
597   switch ( GetRepresentation() ) {
598
599   case Surfaceframe:
600     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
601     mySurfaceActor->RenderOpaqueGeometry(ren);
602
603     myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
604     myEdgeActor->RenderOpaqueGeometry(ren);
605     break;
606
607   case Points:
608     myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
609     myPointsActor->RenderOpaqueGeometry(ren);
610     break;
611
612   default:
613     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
614     mySurfaceActor->RenderOpaqueGeometry(ren);
615   }
616
617   return 1;
618 }
619
620 int
621 VISU_ScalarMapAct
622 ::RenderTranslucentGeometry(vtkViewport *ren)
623 {
624   GetMatrix(myEdgeActor->GetUserMatrix());
625   GetMatrix(mySurfaceActor->GetUserMatrix());
626
627   using namespace SVTK::Representation;
628   switch ( GetRepresentation() ) {
629
630   case Surfaceframe:
631     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
632     mySurfaceActor->RenderTranslucentGeometry(ren);
633
634     myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
635     myEdgeActor->RenderTranslucentGeometry(ren);
636     break;
637
638   case Points:
639     myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
640     myPointsActor->RenderTranslucentGeometry(ren);
641     break;
642
643   default:
644     mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
645     mySurfaceActor->RenderTranslucentGeometry(ren);
646   }
647
648   return 1;
649 }
650
651 //----------------------------------------------------------------------------
652 unsigned long int
653 VISU_ScalarMapAct
654 ::GetMemorySize()
655 {
656   unsigned long int aSize = Superclass::GetMemorySize();
657
658   aSize += myPointsActor->GetMemorySize();
659
660   return aSize;
661 }
662
663 //----------------------------------------------------------------------------
664
665 VISU_Actor::EQuadratic2DRepresentation 
666 VISU_ScalarMapAct
667 ::GetQuadratic2DRepresentation() const
668 {
669   bool mode = (mySurfaceActor->GetQuadraticArcMode() && myEdgeActor->GetQuadraticArcMode());
670   if(mode){
671     return VISU_Actor::eArcs;
672   }
673   else
674     return VISU_Actor::eLines;
675 }
676   
677 void VISU_ScalarMapAct::SetQuadratic2DRepresentation( VISU_Actor::EQuadratic2DRepresentation theMode )
678 {
679   Superclass::SetQuadratic2DRepresentation( theMode );
680   switch(theMode) {
681   case VISU_Actor::eArcs:
682     mySurfaceActor->SetQuadraticArcMode(true);
683     myEdgeActor->SetQuadraticArcMode(true);
684     break;
685   case VISU_Actor::eLines:
686     mySurfaceActor->SetQuadraticArcMode(false);
687     myEdgeActor->SetQuadraticArcMode(false);
688     break;
689   default:
690     break;
691   }
692 }