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 //----------------------------------------------------------------------------
323 ::SetShrinkable(bool theIsShrinkable)
325 Superclass::SetShrinkable(theIsShrinkable);
327 mySurfaceActor->SetShrinkable(theIsShrinkable);
332 ::SetShrinkFactor(vtkFloatingPointType theValue)
334 Superclass::SetShrinkFactor(theValue);
336 mySurfaceActor->SetShrinkFactor(theValue);
339 //----------------------------------------------------------------------------
344 if(myRepresentation == VTK_POINTS)
347 Superclass::SetShrink();
349 mySurfaceActor->SetShrink();
356 Superclass::UnShrink();
358 mySurfaceActor->UnShrink();
361 //----------------------------------------------------------------------------
364 ::SetFeatureEdgesAllowed(bool theIsFeatureEdgesAllowed)
366 Superclass::SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
368 mySurfaceActor->SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
373 ::SetFeatureEdgesAngle(vtkFloatingPointType theValue)
375 Superclass::SetFeatureEdgesAngle(theValue);
377 mySurfaceActor->SetFeatureEdgesAngle(theValue);
382 ::SetFeatureEdgesFlags(bool theIsFeatureEdges,
383 bool theIsBoundaryEdges,
384 bool theIsManifoldEdges,
385 bool theIsNonManifoldEdges)
387 Superclass::SetFeatureEdgesFlags(theIsFeatureEdges,
390 theIsNonManifoldEdges);
392 mySurfaceActor->SetFeatureEdgesFlags(theIsFeatureEdges,
395 theIsNonManifoldEdges);
400 ::SetFeatureEdgesColoring(bool theIsColoring)
402 Superclass::SetFeatureEdgesColoring(theIsColoring);
404 mySurfaceActor->SetFeatureEdgesColoring(theIsColoring);
407 //----------------------------------------------------------------------------
410 ::SetFeatureEdgesEnabled(bool theIsFeatureEdgesEnabled)
412 if(theIsFeatureEdgesEnabled && myRepresentation == VTK_POINTS)
415 Superclass::SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
417 mySurfaceActor->SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
420 //----------------------------------------------------------------------------
423 ::SetOpacity(vtkFloatingPointType theValue)
425 mySurfaceActor->GetProperty()->SetOpacity(theValue);
432 return mySurfaceActor->GetProperty()->GetOpacity();
435 //----------------------------------------------------------------------------
438 ::SetLineWidth(vtkFloatingPointType theLineWidth)
440 mySurfaceActor->GetProperty()->SetLineWidth(theLineWidth);
444 VISU_ScalarMapAct::GetLineWidth()
446 return mySurfaceActor->GetProperty()->GetLineWidth();
449 //----------------------------------------------------------------------------
452 ::DeepCopy(VISU_Actor *theActor)
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());
463 //----------------------------------------------------------------------------
466 ::AddToRender(vtkRenderer* theRenderer)
468 Superclass::AddToRender(theRenderer);
470 myPointsActor->SetInteractor( myInteractor );
473 theRenderer->AddActor2D(myScalarBar);
476 //----------------------------------------------------------------------------
479 ::RemoveFromRender(vtkRenderer* theRenderer)
482 theRenderer->RemoveActor(myScalarBar);
484 if ( vtkWindow* aWindow = theRenderer->GetRenderWindow() ) {
485 myPointsActor->ReleaseGraphicsResources( aWindow );
486 mySurfaceActor->ReleaseGraphicsResources( aWindow );
487 myEdgeActor->ReleaseGraphicsResources( aWindow );
490 Superclass::RemoveFromRender(theRenderer);
493 //----------------------------------------------------------------------------
496 ::SetVisibility(int theMode)
498 Superclass::SetVisibility( theMode );
500 myPointsActor->SetVisibility( theMode );
503 myScalarBar->SetVisibility(myBarVisibility && theMode);
506 //----------------------------------------------------------------------------
511 return myBarVisibility;
514 //----------------------------------------------------------------------------
522 //----------------------------------------------------------------------------
525 ::SetBarVisibility(bool theMode)
527 myBarVisibility = theMode;
529 myScalarBar->SetVisibility(myBarVisibility && GetVisibility());
533 //----------------------------------------------------------------------------
536 ::SetRepresentation(int theMode)
538 bool anIsShanding = IsShading();
540 Superclass::SetRepresentation(theMode);
542 if(theMode == SVTK::Representation::Surfaceframe)
543 mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
545 mySurfaceActor->SetRepresentation(theMode);
547 myPointsActor->SetProperty( mySurfaceActor->GetProperty() );
549 SetShading(anIsShanding);
553 //----------------------------------------------------------------------------
556 ::SetShading(bool theOn)
558 vtkProperty* aProperty = mySurfaceActor->GetProperty();
561 aProperty->SetAmbient(0.0);
562 aProperty->SetDiffuse(1.0);
564 aProperty->SetAmbient(1.0);
565 aProperty->SetDiffuse(0.0);
569 //----------------------------------------------------------------------------
574 vtkProperty* aProperty = mySurfaceActor->GetProperty();
576 return (fabs(aProperty->GetAmbient()) < EPS && fabs(aProperty->GetDiffuse() - 1.) < EPS);
581 ::RenderOpaqueGeometry(vtkViewport *ren)
583 GetMatrix(myEdgeActor->GetUserMatrix());
584 GetMatrix(mySurfaceActor->GetUserMatrix());
585 GetMatrix(myPointsActor->GetUserMatrix());
587 using namespace SVTK::Representation;
588 switch ( GetRepresentation() ) {
591 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
592 mySurfaceActor->RenderOpaqueGeometry(ren);
594 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
595 myEdgeActor->RenderOpaqueGeometry(ren);
599 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
600 myPointsActor->RenderOpaqueGeometry(ren);
604 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
605 mySurfaceActor->RenderOpaqueGeometry(ren);
613 ::RenderTranslucentGeometry(vtkViewport *ren)
615 GetMatrix(myEdgeActor->GetUserMatrix());
616 GetMatrix(mySurfaceActor->GetUserMatrix());
618 using namespace SVTK::Representation;
619 switch ( GetRepresentation() ) {
622 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
623 mySurfaceActor->RenderTranslucentGeometry(ren);
625 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
626 myEdgeActor->RenderTranslucentGeometry(ren);
630 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
631 myPointsActor->RenderTranslucentGeometry(ren);
635 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
636 mySurfaceActor->RenderTranslucentGeometry(ren);
642 //----------------------------------------------------------------------------
647 unsigned long int aSize = Superclass::GetMemorySize();
649 aSize += myPointsActor->GetMemorySize();
654 //----------------------------------------------------------------------------
656 VISU_Actor::EQuadratic2DRepresentation
658 ::GetQuadratic2DRepresentation() const
660 bool mode = (mySurfaceActor->GetQuadraticArcMode() && myEdgeActor->GetQuadraticArcMode());
662 return VISU_Actor::eArcs;
665 return VISU_Actor::eLines;
668 void VISU_ScalarMapAct::SetQuadratic2DRepresentation( VISU_Actor::EQuadratic2DRepresentation theMode )
670 Superclass::SetQuadratic2DRepresentation( theMode );
672 case VISU_Actor::eArcs:
673 mySurfaceActor->SetQuadraticArcMode(true);
674 myEdgeActor->SetQuadraticArcMode(true);
676 case VISU_Actor::eLines:
677 mySurfaceActor->SetQuadraticArcMode(false);
678 myEdgeActor->SetQuadraticArcMode(false);