1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // VISU OBJECT : interactive object for VISU entities implementation
23 // File : VISU_MeshAct.hxx
24 // Author : Laurent CORNABE with the help of Nicolas REJNERI
28 #include "VISU_ScalarMapAct.h"
29 #include "VISU_LookupTable.hxx"
30 #include "VISU_ScalarBarActor.hxx"
31 #include "VISU_PipeLine.hxx"
33 #include "VISU_OpenGLPointSpriteMapper.hxx"
34 #include "VISU_GaussPtsDeviceActor.h"
35 #include "VISU_DeformedShapePL.hxx"
36 #include "VISU_PipeLineUtils.hxx"
39 #include <SUIT_ResourceMgr.h>
40 #include <SUIT_Session.h>
42 #include <SALOME_ExtractGeometry.h>
43 #include <SALOME_ExtractPolyDataGeometry.h>
45 #include <vtkObjectFactory.h>
46 #include <vtkRenderer.h>
47 #include <vtkProperty.h>
48 #include <vtkMatrix4x4.h>
49 #include <vtkMapper.h>
50 #include <vtkDataSetMapper.h>
52 #include <vtkRenderWindowInteractor.h>
53 #include <vtkCallbackCommand.h>
54 #include <vtkRenderWindow.h>
55 #include <vtkImageData.h>
58 //============================================================================
59 class VISU_PointsDeviceActor: public VISU_GaussDeviceActorBase
62 vtkTypeMacro(VISU_PointsDeviceActor, VISU_GaussDeviceActorBase);
65 VISU_PointsDeviceActor*
69 //----------------------------------------------------------------------------
72 SetInput(vtkDataSet* theDataSet)
74 myGeomFilter->SetInput( theDataSet );
78 //----------------------------------------------------------------------------
80 SetInteractor(vtkRenderWindowInteractor* theInteractor)
82 if(theInteractor == myInteractor)
86 myInteractor->RemoveObserver(myEventCallbackCommand);
89 theInteractor->AddObserver(vtkCommand::CharEvent,
90 myEventCallbackCommand,
93 myInteractor = theInteractor;
97 //----------------------------------------------------------------------------
99 DoMapperShallowCopy( vtkMapper* theMapper,
100 bool theIsCopyInput )
102 Superclass::DoMapperShallowCopy( theMapper, theIsCopyInput );
104 vtkDataSet* aDataSet = theMapper->GetInput();
105 vtkFloatingPointType aScaleFactor = VISU_DeformedShapePL::GetScaleFactor( aDataSet );
107 GetPointSpriteMapper()->SetAverageCellSize( aScaleFactor );
111 //----------------------------------------------------------------------------
113 DeepCopy( VISU_PointsDeviceActor *theActor )
115 VISU::CopyPointSpriteDataMapper( GetPointSpriteMapper(), theActor->GetPointSpriteMapper(), false );
119 //----------------------------------------------------------------------------
120 VISU_PointsDeviceActor():
121 myGeomFilter( VTKViewer_GeometryFilter::New() ),
122 myEventCallbackCommand( vtkCallbackCommand::New() ),
125 myGeomFilter->SetInside(true);
127 VISU_OpenGLPointSpriteMapper* aMapper = VISU_OpenGLPointSpriteMapper::New();
128 aMapper->SetInput( myGeomFilter->GetOutput() );
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() );
136 aMapper->SetUseLookupTableScalarRange(true);
137 aMapper->SetColorModeToMapScalars();
138 aMapper->SetScalarVisibility(true);
140 SetPointSpriteMapper( aMapper );
144 myEventCallbackCommand->SetClientData( this );
145 myEventCallbackCommand->SetCallback( VISU_PointsDeviceActor::ProcessEvents );
149 //----------------------------------------------------------------------------
150 ~VISU_PointsDeviceActor()
152 SetInteractor( NULL );
153 myGeomFilter->Delete();
154 myEventCallbackCommand->Delete();
158 //----------------------------------------------------------------------------
161 ProcessEvents(vtkObject* theObject,
162 unsigned long theEvent,
166 if ( VISU_PointsDeviceActor* self = reinterpret_cast<VISU_PointsDeviceActor*>( theClientData ) )
167 self->OnInteractorEvent( theEvent );
171 //----------------------------------------------------------------------------
173 OnInteractorEvent(unsigned long theEvent)
175 switch ( theEvent ) {
176 case vtkCommand::CharEvent: {
177 switch( myInteractor->GetKeyCode() ) {
180 if ( !GetVisibility() )
183 static vtkFloatingPointType anIncrement = 2;
184 vtkFloatingPointType aMagnification = GetPointSpriteMapper()->GetPointSpriteMagnification();
185 vtkFloatingPointType coefficient = myInteractor->GetShiftKey() ? anIncrement : 1 / anIncrement;
187 GetPointSpriteMapper()->SetPointSpriteMagnification( aMagnification * coefficient );
189 myInteractor->CreateTimer(VTKI_TIMER_UPDATE);
203 //----------------------------------------------------------------------------
204 vtkCallbackCommand* myEventCallbackCommand;
205 vtkRenderWindowInteractor* myInteractor;
206 VTKViewer_GeometryFilter* myGeomFilter;
209 VISU_PointsDeviceActor(const VISU_PointsDeviceActor&); // Not implemented
210 void operator=(const VISU_PointsDeviceActor&); // Not implemented
213 vtkStandardNewMacro(VISU_PointsDeviceActor);
215 //----------------------------------------------------------------------------
216 vtkStandardNewMacro(VISU_ScalarMapAct);
217 static vtkFloatingPointType EPS = 1.0 / VTK_LARGE_FLOAT;
219 //----------------------------------------------------------------------------
221 ::VISU_ScalarMapAct()
223 myScalarBar = VISU_ScalarBarActor::New();
225 vtkProperty* aProperty = GetProperty();
226 aProperty->SetAmbient(1.0);
227 aProperty->SetDiffuse(0.0);
228 aProperty->SetSpecular(0.0);
230 myProperty->DeepCopy(aProperty);
232 vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
234 mySurfaceActor= SVTK_DeviceActor::New();
235 mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
236 mySurfaceActor->SetProperty(aProperty);
237 mySurfaceActor->SetUserMatrix(aMatrix);
239 myEdgeActor = SVTK_DeviceActor::New();
240 myEdgeActor->SetRepresentation(SVTK::Representation::Wireframe);
241 myEdgeActor->SetUserMatrix(aMatrix);
242 myEdgeActor->GetProperty()->SetColor(255.,255.,255.);
244 myPointsActor = VISU_PointsDeviceActor::New();
245 myPointsActor->SetProperty(aProperty);
246 myPointsActor->SetUserMatrix(aMatrix);
250 SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
251 //Quadratic 2D elements representation
253 //----------------------------------------------------------------------------
254 int aQuadraticAngle = aResourceMgr->integerValue( "VISU", "max_angle", 2);
255 mySurfaceActor->SetQuadraticArcAngle(aQuadraticAngle);
256 myEdgeActor->SetQuadraticArcAngle(aQuadraticAngle);
260 //----------------------------------------------------------------------------
262 ::~VISU_ScalarMapAct()
264 myScalarBar->Delete();
266 myPointsActor->Delete();
268 mySurfaceActor->Delete();
269 myEdgeActor->Delete();
274 ::ShallowCopyPL(VISU_PipeLine* thePipeLine)
276 VISU_DataSetActor::ShallowCopyPL( thePipeLine );
278 myEdgeActor->GetMapper()->ScalarVisibilityOff();
280 myPointsActor->DoMapperShallowCopy( thePipeLine->GetMapper(), false );
282 VISU::CopyMapper( mySurfaceActor->GetMapper(), thePipeLine->GetMapper(), false );
285 //----------------------------------------------------------------------------
288 ::SetMapperInput(vtkDataSet* theDataSet)
290 Superclass::SetMapperInput( theDataSet );
292 // myPointsActor->SetInput( theDataSet );
293 // mySurfaceActor->SetInput( theDataSet );
294 // myEdgeActor->SetInput( theDataSet );
296 if (theDataSet->IsA("vtkPolyData")) {
297 myPointsActor->SetInput( myPolyDataExtractor->GetOutput() );
298 mySurfaceActor->SetInput( myPolyDataExtractor->GetOutput() );
299 myEdgeActor->SetInput( myPolyDataExtractor->GetOutput() );
301 myPointsActor->SetInput( myExtractor->GetOutput() );
302 mySurfaceActor->SetInput( myExtractor->GetOutput() );
303 myEdgeActor->SetInput( myExtractor->GetOutput() );
307 //----------------------------------------------------------------------------
310 ::SetTransform(VTKViewer_Transform* theTransform)
312 Superclass::SetTransform(theTransform);
314 myPointsActor->SetTransform(theTransform);
316 mySurfaceActor->SetTransform(theTransform);
317 myEdgeActor->SetTransform(theTransform);
320 //----------------------------------------------------------------------------
325 return myEdgeActor->GetProperty();
328 //----------------------------------------------------------------------------
331 ::SetShrinkable(bool theIsShrinkable)
333 Superclass::SetShrinkable(theIsShrinkable);
335 mySurfaceActor->SetShrinkable(theIsShrinkable);
340 ::SetShrinkFactor(vtkFloatingPointType theValue)
342 Superclass::SetShrinkFactor(theValue);
344 mySurfaceActor->SetShrinkFactor(theValue);
347 //----------------------------------------------------------------------------
352 if(myRepresentation == VTK_POINTS)
355 Superclass::SetShrink();
357 mySurfaceActor->SetShrink();
364 Superclass::UnShrink();
366 mySurfaceActor->UnShrink();
369 //----------------------------------------------------------------------------
372 ::SetFeatureEdgesAllowed(bool theIsFeatureEdgesAllowed)
374 Superclass::SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
376 mySurfaceActor->SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
381 ::SetFeatureEdgesAngle(vtkFloatingPointType theValue)
383 Superclass::SetFeatureEdgesAngle(theValue);
385 mySurfaceActor->SetFeatureEdgesAngle(theValue);
390 ::SetFeatureEdgesFlags(bool theIsFeatureEdges,
391 bool theIsBoundaryEdges,
392 bool theIsManifoldEdges,
393 bool theIsNonManifoldEdges)
395 Superclass::SetFeatureEdgesFlags(theIsFeatureEdges,
398 theIsNonManifoldEdges);
400 mySurfaceActor->SetFeatureEdgesFlags(theIsFeatureEdges,
403 theIsNonManifoldEdges);
408 ::SetFeatureEdgesColoring(bool theIsColoring)
410 Superclass::SetFeatureEdgesColoring(theIsColoring);
412 mySurfaceActor->SetFeatureEdgesColoring(theIsColoring);
415 //----------------------------------------------------------------------------
418 ::SetFeatureEdgesEnabled(bool theIsFeatureEdgesEnabled)
420 if(theIsFeatureEdgesEnabled && myRepresentation == VTK_POINTS)
423 Superclass::SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
425 mySurfaceActor->SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
428 //----------------------------------------------------------------------------
431 ::SetOpacity(vtkFloatingPointType theValue)
433 mySurfaceActor->GetProperty()->SetOpacity(theValue);
440 return mySurfaceActor->GetProperty()->GetOpacity();
443 //----------------------------------------------------------------------------
446 ::SetLineWidth(vtkFloatingPointType theLineWidth)
448 mySurfaceActor->GetProperty()->SetLineWidth(theLineWidth);
452 VISU_ScalarMapAct::GetLineWidth()
454 return mySurfaceActor->GetProperty()->GetLineWidth();
457 //----------------------------------------------------------------------------
460 ::DeepCopy(VISU_Actor *theActor)
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());
472 //----------------------------------------------------------------------------
475 ::AddToRender(vtkRenderer* theRenderer)
477 Superclass::AddToRender(theRenderer);
479 myPointsActor->SetInteractor( myInteractor );
482 theRenderer->AddActor2D(myScalarBar);
485 //----------------------------------------------------------------------------
488 ::RemoveFromRender(vtkRenderer* theRenderer)
491 theRenderer->RemoveActor(myScalarBar);
493 if ( vtkWindow* aWindow = theRenderer->GetRenderWindow() ) {
494 myPointsActor->ReleaseGraphicsResources( aWindow );
495 mySurfaceActor->ReleaseGraphicsResources( aWindow );
496 myEdgeActor->ReleaseGraphicsResources( aWindow );
499 Superclass::RemoveFromRender(theRenderer);
502 //----------------------------------------------------------------------------
505 ::SetVisibility(int theMode)
507 Superclass::SetVisibility( theMode );
509 myPointsActor->SetVisibility( theMode );
512 myScalarBar->SetVisibility(myBarVisibility && theMode);
515 //----------------------------------------------------------------------------
520 return myBarVisibility;
523 //----------------------------------------------------------------------------
531 //----------------------------------------------------------------------------
534 ::SetBarVisibility(bool theMode)
536 myBarVisibility = theMode;
538 myScalarBar->SetVisibility(myBarVisibility && GetVisibility());
542 //----------------------------------------------------------------------------
545 ::SetRepresentation(int theMode)
547 bool anIsShanding = IsShading();
549 Superclass::SetRepresentation(theMode);
551 if(theMode == SVTK::Representation::Surfaceframe)
552 mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
554 mySurfaceActor->SetRepresentation(theMode);
556 myPointsActor->SetProperty( mySurfaceActor->GetProperty() );
558 SetShading(anIsShanding);
562 //----------------------------------------------------------------------------
565 ::SetShading(bool theOn)
567 vtkProperty* aProperty = mySurfaceActor->GetProperty();
570 aProperty->SetAmbient(0.0);
571 aProperty->SetDiffuse(1.0);
573 aProperty->SetAmbient(1.0);
574 aProperty->SetDiffuse(0.0);
578 //----------------------------------------------------------------------------
583 vtkProperty* aProperty = mySurfaceActor->GetProperty();
585 return (fabs(aProperty->GetAmbient()) < EPS && fabs(aProperty->GetDiffuse() - 1.) < EPS);
590 ::RenderOpaqueGeometry(vtkViewport *ren)
592 GetMatrix(myEdgeActor->GetUserMatrix());
593 GetMatrix(mySurfaceActor->GetUserMatrix());
594 GetMatrix(myPointsActor->GetUserMatrix());
596 using namespace SVTK::Representation;
597 switch ( GetRepresentation() ) {
600 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
601 mySurfaceActor->RenderOpaqueGeometry(ren);
603 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
604 myEdgeActor->RenderOpaqueGeometry(ren);
608 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
609 myPointsActor->RenderOpaqueGeometry(ren);
613 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
614 mySurfaceActor->RenderOpaqueGeometry(ren);
622 ::RenderTranslucentGeometry(vtkViewport *ren)
624 GetMatrix(myEdgeActor->GetUserMatrix());
625 GetMatrix(mySurfaceActor->GetUserMatrix());
627 using namespace SVTK::Representation;
628 switch ( GetRepresentation() ) {
631 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
632 mySurfaceActor->RenderTranslucentGeometry(ren);
634 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
635 myEdgeActor->RenderTranslucentGeometry(ren);
639 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
640 myPointsActor->RenderTranslucentGeometry(ren);
644 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
645 mySurfaceActor->RenderTranslucentGeometry(ren);
651 //----------------------------------------------------------------------------
656 unsigned long int aSize = Superclass::GetMemorySize();
658 aSize += myPointsActor->GetMemorySize();
663 //----------------------------------------------------------------------------
665 VISU_Actor::EQuadratic2DRepresentation
667 ::GetQuadratic2DRepresentation() const
669 bool mode = (mySurfaceActor->GetQuadraticArcMode() && myEdgeActor->GetQuadraticArcMode());
671 return VISU_Actor::eArcs;
674 return VISU_Actor::eLines;
677 void VISU_ScalarMapAct::SetQuadratic2DRepresentation( VISU_Actor::EQuadratic2DRepresentation theMode )
679 Superclass::SetQuadratic2DRepresentation( theMode );
681 case VISU_Actor::eArcs:
682 mySurfaceActor->SetQuadraticArcMode(true);
683 myEdgeActor->SetQuadraticArcMode(true);
685 case VISU_Actor::eLines:
686 mySurfaceActor->SetQuadraticArcMode(false);
687 myEdgeActor->SetQuadraticArcMode(false);