1 // Copyright (C) 2007-2012 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
23 // VISU OBJECT : interactive object for VISU entities implementation
24 // File : VISU_MeshAct.hxx
25 // Author : Laurent CORNABE with the help of Nicolas REJNERI
29 #include "VISU_ScalarMapAct.h"
30 #include "VISU_LookupTable.hxx"
31 #include "VISU_ScalarBarActor.hxx"
32 #include "VISU_PipeLine.hxx"
34 #include "VISU_OpenGLPointSpriteMapper.hxx"
35 #include "VISU_GaussPtsDeviceActor.h"
36 #include "VISU_DeformedShapePL.hxx"
37 #include "VISU_PipeLineUtils.hxx"
40 #include <SUIT_ResourceMgr.h>
41 #include <SUIT_Session.h>
43 #include <SALOME_ExtractGeometry.h>
44 #include <SALOME_ExtractPolyDataGeometry.h>
46 #include <vtkObjectFactory.h>
47 #include <vtkRenderer.h>
48 #include <vtkProperty.h>
49 #include <vtkMatrix4x4.h>
50 #include <vtkMapper.h>
51 #include <vtkDataSetMapper.h>
53 #include <vtkRenderWindowInteractor.h>
54 #include <vtkCallbackCommand.h>
55 #include <vtkRenderWindow.h>
56 #include <vtkImageData.h>
59 //============================================================================
60 class VISU_PointsDeviceActor: public VISU_GaussDeviceActorBase
63 vtkTypeMacro(VISU_PointsDeviceActor, VISU_GaussDeviceActorBase);
66 VISU_PointsDeviceActor*
70 //----------------------------------------------------------------------------
73 SetInputData(vtkDataSet* theDataSet)
75 myGeomFilter->SetInputData( theDataSet );
79 //----------------------------------------------------------------------------
82 SetInputConnection(vtkAlgorithmOutput* theAlgorithmOutput)
84 myGeomFilter->SetInputConnection( theAlgorithmOutput );
88 //----------------------------------------------------------------------------
90 SetInteractor(vtkRenderWindowInteractor* theInteractor)
92 if(theInteractor == myInteractor)
96 myInteractor->RemoveObserver(myEventCallbackCommand);
99 theInteractor->AddObserver(vtkCommand::CharEvent,
100 myEventCallbackCommand,
103 myInteractor = theInteractor;
107 //----------------------------------------------------------------------------
109 DoMapperShallowCopy( vtkMapper* theMapper,
110 bool theIsCopyInput )
112 Superclass::DoMapperShallowCopy( theMapper, theIsCopyInput );
114 vtkDataSet* aDataSet = theMapper->GetInput();
115 double aScaleFactor = VISU_DeformedShapePL::GetScaleFactor( aDataSet );
117 GetPointSpriteMapper()->SetAverageCellSize( aScaleFactor );
121 //----------------------------------------------------------------------------
123 DeepCopy( VISU_PointsDeviceActor *theActor )
125 VISU::CopyPointSpriteDataMapper( GetPointSpriteMapper(), theActor->GetPointSpriteMapper(), false );
129 //----------------------------------------------------------------------------
130 VISU_PointsDeviceActor():
131 myGeomFilter( VTKViewer_GeometryFilter::New() ),
132 myEventCallbackCommand( vtkCallbackCommand::New() ),
135 myGeomFilter->SetInside(true);
137 VISU_OpenGLPointSpriteMapper* aMapper = VISU_OpenGLPointSpriteMapper::New();
138 aMapper->SetInputConnection( myGeomFilter->GetOutputPort() );
140 std::string aRootDir( getenv( "VISU_ROOT_DIR") );
141 std::string aMainTexture = aRootDir + "/share/salome/resources/visu/sprite_texture.bmp";
142 std::string anAlphaTexture = aRootDir + "/share/salome/resources/visu/sprite_alpha.bmp";
143 VISU::TTextureValue aTextureValue = VISU::GetTexture( aMainTexture, anAlphaTexture );
144 aMapper->SetImageData( aTextureValue.GetPointer() );
146 aMapper->SetUseLookupTableScalarRange(true);
147 aMapper->SetColorModeToMapScalars();
148 aMapper->SetScalarVisibility(true);
150 SetPointSpriteMapper( aMapper );
152 myGeomFilter->SetAppendCoincident3D(true);
156 myEventCallbackCommand->SetClientData( this );
157 myEventCallbackCommand->SetCallback( VISU_PointsDeviceActor::ProcessEvents );
161 //----------------------------------------------------------------------------
162 ~VISU_PointsDeviceActor()
164 SetInteractor( NULL );
165 myGeomFilter->Delete();
166 myEventCallbackCommand->Delete();
170 //----------------------------------------------------------------------------
173 ProcessEvents(vtkObject* theObject,
174 unsigned long theEvent,
178 if ( VISU_PointsDeviceActor* self = reinterpret_cast<VISU_PointsDeviceActor*>( theClientData ) )
179 self->OnInteractorEvent( theEvent );
183 //----------------------------------------------------------------------------
185 OnInteractorEvent(unsigned long theEvent)
187 switch ( theEvent ) {
188 case vtkCommand::CharEvent: {
189 switch( myInteractor->GetKeyCode() ) {
192 if ( !GetVisibility() )
195 static double anIncrement = 2;
196 double aMagnification = GetPointSpriteMapper()->GetPointSpriteMagnification();
197 double coefficient = myInteractor->GetShiftKey() ? anIncrement : 1 / anIncrement;
199 GetPointSpriteMapper()->SetPointSpriteMagnification( aMagnification * coefficient );
201 myInteractor->CreateTimer(VTKI_TIMER_UPDATE);
215 //----------------------------------------------------------------------------
216 vtkCallbackCommand* myEventCallbackCommand;
217 vtkRenderWindowInteractor* myInteractor;
218 VTKViewer_GeometryFilter* myGeomFilter;
221 VISU_PointsDeviceActor(const VISU_PointsDeviceActor&); // Not implemented
222 void operator=(const VISU_PointsDeviceActor&); // Not implemented
225 vtkStandardNewMacro(VISU_PointsDeviceActor);
227 //----------------------------------------------------------------------------
228 vtkStandardNewMacro(VISU_ScalarMapAct);
229 static double EPS = 1.0 / VTK_LARGE_FLOAT;
231 //----------------------------------------------------------------------------
233 ::VISU_ScalarMapAct()
235 myScalarBar = VISU_ScalarBarActor::New();
237 vtkProperty* aProperty = GetProperty();
238 aProperty->SetAmbient(1.0);
239 aProperty->SetDiffuse(0.0);
240 aProperty->SetSpecular(0.0);
242 myProperty->DeepCopy(aProperty);
244 vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
246 mySurfaceActor= SVTK_DeviceActor::New();
247 mySurfaceActor->SetRepresentation(VTKViewer::Representation::Surface);
248 mySurfaceActor->SetProperty(aProperty);
249 mySurfaceActor->SetUserMatrix(aMatrix);
250 mySurfaceActor->SetCoincident3DAllowed(true);
252 myEdgeActor = SVTK_DeviceActor::New();
253 myEdgeActor->SetRepresentation(VTKViewer::Representation::Wireframe);
254 myEdgeActor->SetUserMatrix(aMatrix);
255 myEdgeActor->GetProperty()->SetColor(255.,255.,255.);
257 myEdgeActor->SetCoincident3DAllowed(true);
259 myPointsActor = SVTK_DeviceActor::New();
260 myPointsActor->SetRepresentation(VTKViewer::Representation::Points);
261 myPointsActor->SetProperty(aProperty);
262 myPointsActor->SetUserMatrix(aMatrix);
264 myPointSpriteActor = VISU_PointsDeviceActor::New();
265 myPointSpriteActor->SetProperty(aProperty);
266 myPointSpriteActor->SetUserMatrix(aMatrix);
270 myIsPointSpriteMode = false;
272 SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
273 //Quadratic 2D elements representation
275 //----------------------------------------------------------------------------
276 int aQuadraticAngle = aResourceMgr->integerValue( "VISU", "max_angle", 2);
277 mySurfaceActor->SetQuadraticArcAngle(aQuadraticAngle);
278 myEdgeActor->SetQuadraticArcAngle(aQuadraticAngle);
280 int anElem0DSize = aResourceMgr->integerValue( "VISU", "elem0d_size", 5);
281 mySurfaceActor->GetProperty()->SetPointSize(anElem0DSize);
285 //----------------------------------------------------------------------------
287 ::~VISU_ScalarMapAct()
289 myScalarBar->Delete();
291 myPointsActor->Delete();
292 myPointSpriteActor->Delete();
294 mySurfaceActor->Delete();
295 myEdgeActor->Delete();
300 ::ShallowCopyPL(VISU_PipeLine* thePipeLine)
302 VISU_DataSetActor::ShallowCopyPL( thePipeLine );
304 myEdgeActor->GetMapper()->ScalarVisibilityOff();
306 myPointSpriteActor->DoMapperShallowCopy( thePipeLine->GetMapper(), false );
308 VISU::CopyMapper( myPointsActor->GetMapper(), thePipeLine->GetMapper(), false );
309 VISU::CopyMapper( mySurfaceActor->GetMapper(), thePipeLine->GetMapper(), false );
312 //----------------------------------------------------------------------------
315 ::SetMapperInput(vtkDataSet* theDataSet)
317 Superclass::SetMapperInput( theDataSet );
319 // myPointsActor->SetInput( theDataSet );
320 // mySurfaceActor->SetInput( theDataSet );
321 // myEdgeActor->SetInput( theDataSet );
323 if (theDataSet->IsA("vtkPolyData")) {
324 myPointSpriteActor->SetInputConnection( myPolyDataExtractor->GetOutputPort() );
325 myPointsActor->SetInputConnection( myPolyDataExtractor->GetOutputPort() );
326 mySurfaceActor->SetInputConnection( myPolyDataExtractor->GetOutputPort() );
327 myEdgeActor->SetInputConnection( myPolyDataExtractor->GetOutputPort() );
329 myPointSpriteActor->SetInputConnection( myExtractor->GetOutputPort() );
330 myPointsActor->SetInputConnection( myExtractor->GetOutputPort() );
331 mySurfaceActor->SetInputConnection( myExtractor->GetOutputPort() );
332 myEdgeActor->SetInputConnection( myExtractor->GetOutputPort() );
336 //----------------------------------------------------------------------------
339 ::SetTransform(VTKViewer_Transform* theTransform)
341 Superclass::SetTransform(theTransform);
343 myPointsActor->SetTransform(theTransform);
344 myPointSpriteActor->SetTransform(theTransform);
346 mySurfaceActor->SetTransform(theTransform);
347 myEdgeActor->SetTransform(theTransform);
350 //----------------------------------------------------------------------------
355 return myEdgeActor->GetProperty();
358 //----------------------------------------------------------------------------
361 ::SetShrinkable(bool theIsShrinkable)
363 Superclass::SetShrinkable(theIsShrinkable);
365 mySurfaceActor->SetShrinkable(theIsShrinkable);
366 myEdgeActor->SetShrinkable(theIsShrinkable);
371 ::SetShrinkFactor(double theValue)
373 Superclass::SetShrinkFactor(theValue);
375 mySurfaceActor->SetShrinkFactor(theValue);
376 myEdgeActor->SetShrinkFactor(theValue);
379 //----------------------------------------------------------------------------
384 if(myRepresentation == VTK_POINTS)
387 Superclass::SetShrink();
389 mySurfaceActor->SetShrink();
390 myEdgeActor->SetShrink();
397 Superclass::UnShrink();
399 mySurfaceActor->UnShrink();
400 myEdgeActor->UnShrink();
403 //----------------------------------------------------------------------------
406 ::SetFeatureEdgesAllowed(bool theIsFeatureEdgesAllowed)
408 Superclass::SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
410 mySurfaceActor->SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
415 ::SetFeatureEdgesAngle(double theValue)
417 Superclass::SetFeatureEdgesAngle(theValue);
419 mySurfaceActor->SetFeatureEdgesAngle(theValue);
424 ::SetFeatureEdgesFlags(bool theIsFeatureEdges,
425 bool theIsBoundaryEdges,
426 bool theIsManifoldEdges,
427 bool theIsNonManifoldEdges)
429 Superclass::SetFeatureEdgesFlags(theIsFeatureEdges,
432 theIsNonManifoldEdges);
434 mySurfaceActor->SetFeatureEdgesFlags(theIsFeatureEdges,
437 theIsNonManifoldEdges);
442 ::SetFeatureEdgesColoring(bool theIsColoring)
444 Superclass::SetFeatureEdgesColoring(theIsColoring);
446 mySurfaceActor->SetFeatureEdgesColoring(theIsColoring);
449 //----------------------------------------------------------------------------
452 ::SetFeatureEdgesEnabled(bool theIsFeatureEdgesEnabled)
454 if(theIsFeatureEdgesEnabled && myRepresentation == VTK_POINTS)
457 Superclass::SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
459 mySurfaceActor->SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
462 //----------------------------------------------------------------------------
465 ::SetOpacity(double theValue)
467 mySurfaceActor->GetProperty()->SetOpacity(theValue);
469 double aPointSpriteOpacity = theValue > 0.0 ? 1.0 : 0.0;
470 myPointSpriteActor->GetPointSpriteMapper()->SetPointSpriteOpacity(aPointSpriteOpacity);
477 return mySurfaceActor->GetProperty()->GetOpacity();
480 //----------------------------------------------------------------------------
483 ::SetLineWidth(double theLineWidth)
485 mySurfaceActor->GetProperty()->SetLineWidth(theLineWidth);
489 VISU_ScalarMapAct::GetLineWidth()
491 return mySurfaceActor->GetProperty()->GetLineWidth();
494 //----------------------------------------------------------------------------
497 ::DeepCopy(VISU_Actor *theActor)
499 if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
500 Superclass::DeepCopy(theActor);
501 SetBarVisibility(anActor->GetBarVisibility());
502 myPointSpriteActor->DeepCopy( anActor->myPointSpriteActor );
503 SetShading(anActor->IsShading());
508 //----------------------------------------------------------------------------
511 ::AddToRender(vtkRenderer* theRenderer)
513 Superclass::AddToRender(theRenderer);
515 myPointSpriteActor->SetInteractor( myInteractor );
518 theRenderer->AddActor2D(myScalarBar);
521 //----------------------------------------------------------------------------
524 ::RemoveFromRender(vtkRenderer* theRenderer)
527 theRenderer->RemoveActor(myScalarBar);
529 if ( vtkWindow* aWindow = theRenderer->GetRenderWindow() ) {
530 myPointSpriteActor->ReleaseGraphicsResources( aWindow );
531 myPointsActor->ReleaseGraphicsResources( aWindow );
532 mySurfaceActor->ReleaseGraphicsResources( aWindow );
533 myEdgeActor->ReleaseGraphicsResources( aWindow );
536 Superclass::RemoveFromRender(theRenderer);
539 //----------------------------------------------------------------------------
542 ::SetVisibility(int theMode)
544 Superclass::SetVisibility( theMode );
546 myPointSpriteActor->SetVisibility( theMode );
547 myPointsActor->SetVisibility( theMode );
550 myScalarBar->SetVisibility(myBarVisibility && theMode);
553 //----------------------------------------------------------------------------
558 return myBarVisibility;
561 //----------------------------------------------------------------------------
569 //----------------------------------------------------------------------------
572 ::SetBarVisibility(bool theMode)
574 myBarVisibility = theMode;
576 myScalarBar->SetVisibility(myBarVisibility && GetVisibility());
580 //----------------------------------------------------------------------------
583 ::SetRepresentation(int theMode)
585 bool anIsShanding = IsShading();
587 Superclass::SetRepresentation(theMode);
589 if(theMode == VTKViewer::Representation::Surfaceframe)
590 mySurfaceActor->SetRepresentation(VTKViewer::Representation::Surface);
592 mySurfaceActor->SetRepresentation(theMode);
594 myPointSpriteActor->SetProperty( mySurfaceActor->GetProperty() );
595 myPointsActor->SetProperty( mySurfaceActor->GetProperty() );
597 SetShading(anIsShanding);
601 //----------------------------------------------------------------------------
604 ::SetShading(bool theOn)
606 vtkProperty* aProperty = mySurfaceActor->GetProperty();
609 aProperty->SetAmbient(0.0);
610 aProperty->SetDiffuse(1.0);
612 aProperty->SetAmbient(1.0);
613 aProperty->SetDiffuse(0.0);
617 //----------------------------------------------------------------------------
622 vtkProperty* aProperty = mySurfaceActor->GetProperty();
624 return (fabs(aProperty->GetAmbient()) < EPS && fabs(aProperty->GetDiffuse() - 1.) < EPS);
629 ::RenderOpaqueGeometry(vtkViewport *ren)
631 GetMatrix(myEdgeActor->GetUserMatrix());
632 GetMatrix(mySurfaceActor->GetUserMatrix());
633 GetMatrix(myPointsActor->GetUserMatrix());
634 GetMatrix(myPointSpriteActor->GetUserMatrix());
636 using namespace VTKViewer::Representation;
637 switch ( GetRepresentation() ) {
640 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
641 mySurfaceActor->RenderOpaqueGeometry(ren);
643 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
644 myEdgeActor->RenderOpaqueGeometry(ren);
648 if( myIsPointSpriteMode ) {
649 myPointSpriteActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
650 myPointSpriteActor->RenderOpaqueGeometry(ren);
652 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
653 myPointsActor->RenderOpaqueGeometry(ren);
658 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
659 mySurfaceActor->RenderOpaqueGeometry(ren);
667 #if (VTK_XVERSION < 0x050100)
668 ::RenderTranslucentGeometry(vtkViewport *ren)
670 ::RenderTranslucentPolygonalGeometry(vtkViewport *ren)
673 GetMatrix(myEdgeActor->GetUserMatrix());
674 GetMatrix(mySurfaceActor->GetUserMatrix());
676 using namespace VTKViewer::Representation;
677 switch ( GetRepresentation() ) {
680 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
681 #if (VTK_XVERSION < 0x050100)
682 mySurfaceActor->RenderTranslucentGeometry(ren);
684 mySurfaceActor->RenderTranslucentPolygonalGeometry(ren);
687 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
688 #if (VTK_XVERSION < 0x050100)
689 myEdgeActor->RenderTranslucentGeometry(ren);
691 myEdgeActor->RenderTranslucentPolygonalGeometry(ren);
696 if( myIsPointSpriteMode ) {
697 myPointSpriteActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
698 #if (VTK_XVERSION < 0x050100)
699 myPointSpriteActor->RenderTranslucentGeometry(ren);
701 myPointSpriteActor->RenderTranslucentPolygonalGeometry(ren);
704 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
705 #if (VTK_XVERSION < 0x050100)
706 myPointsActor->RenderTranslucentGeometry(ren);
708 myPointsActor->RenderTranslucentPolygonalGeometry(ren);
714 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
715 #if (VTK_XVERSION < 0x050100)
716 mySurfaceActor->RenderTranslucentGeometry(ren);
718 mySurfaceActor->RenderTranslucentPolygonalGeometry(ren);
725 //----------------------------------------------------------------------------
726 #if (VTK_XVERSION >= 0x050100)
729 ::HasTranslucentPolygonalGeometry()
733 using namespace VTKViewer::Representation;
734 switch ( GetRepresentation() ) {
736 result |= mySurfaceActor->HasTranslucentPolygonalGeometry();
737 result |= myEdgeActor->HasTranslucentPolygonalGeometry();
740 if( myIsPointSpriteMode )
741 result |= myPointSpriteActor->HasTranslucentPolygonalGeometry();
743 result |= myPointsActor->HasTranslucentPolygonalGeometry();
746 result |= mySurfaceActor->HasTranslucentPolygonalGeometry();
753 //----------------------------------------------------------------------------
758 unsigned long int aSize = Superclass::GetMemorySize();
760 aSize += myPointSpriteActor->GetMemorySize();
765 //----------------------------------------------------------------------------
767 VISU_Actor::EQuadratic2DRepresentation
769 ::GetQuadratic2DRepresentation() const
771 bool mode = (mySurfaceActor->GetQuadraticArcMode() && myEdgeActor->GetQuadraticArcMode());
773 return VISU_Actor::eArcs;
776 return VISU_Actor::eLines;
779 void VISU_ScalarMapAct::SetQuadratic2DRepresentation( VISU_Actor::EQuadratic2DRepresentation theMode )
781 Superclass::SetQuadratic2DRepresentation( theMode );
783 case VISU_Actor::eArcs:
784 mySurfaceActor->SetQuadraticArcMode(true);
785 myEdgeActor->SetQuadraticArcMode(true);
787 case VISU_Actor::eLines:
788 mySurfaceActor->SetQuadraticArcMode(false);
789 myEdgeActor->SetQuadraticArcMode(false);
796 void VISU_ScalarMapAct::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
798 Superclass::SetMarkerStd( theMarkerType, theMarkerScale );
799 myPointsActor->SetMarkerStd( theMarkerType, theMarkerScale );
801 myIsPointSpriteMode = theMarkerType == VTK::MT_POINT_SPRITE;
804 void VISU_ScalarMapAct::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
806 Superclass::SetMarkerTexture( theMarkerId, theMarkerTexture );
807 myPointsActor->SetMarkerTexture( theMarkerId, theMarkerTexture );
809 myIsPointSpriteMode = false;
813 * Set size of the 0D elements.
815 void VISU_ScalarMapAct::Set0DElemSize(double theValue) {
816 mySurfaceActor->GetProperty()->SetPointSize(theValue);
820 * Get size of the 0D elements.
822 double VISU_ScalarMapAct::Get0DElemSize() {
823 return mySurfaceActor->GetProperty()->GetPointSize();