1 // Copyright (C) 2007-2011 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 SetInput(vtkDataSet* theDataSet)
75 myGeomFilter->SetInput( theDataSet );
79 //----------------------------------------------------------------------------
81 SetInteractor(vtkRenderWindowInteractor* theInteractor)
83 if(theInteractor == myInteractor)
87 myInteractor->RemoveObserver(myEventCallbackCommand);
90 theInteractor->AddObserver(vtkCommand::CharEvent,
91 myEventCallbackCommand,
94 myInteractor = theInteractor;
98 //----------------------------------------------------------------------------
100 DoMapperShallowCopy( vtkMapper* theMapper,
101 bool theIsCopyInput )
103 Superclass::DoMapperShallowCopy( theMapper, theIsCopyInput );
105 vtkDataSet* aDataSet = theMapper->GetInput();
106 vtkFloatingPointType aScaleFactor = VISU_DeformedShapePL::GetScaleFactor( aDataSet );
108 GetPointSpriteMapper()->SetAverageCellSize( aScaleFactor );
112 //----------------------------------------------------------------------------
114 DeepCopy( VISU_PointsDeviceActor *theActor )
116 VISU::CopyPointSpriteDataMapper( GetPointSpriteMapper(), theActor->GetPointSpriteMapper(), false );
120 //----------------------------------------------------------------------------
121 VISU_PointsDeviceActor():
122 myGeomFilter( VTKViewer_GeometryFilter::New() ),
123 myEventCallbackCommand( vtkCallbackCommand::New() ),
126 myGeomFilter->SetInside(true);
128 VISU_OpenGLPointSpriteMapper* aMapper = VISU_OpenGLPointSpriteMapper::New();
129 aMapper->SetInput( myGeomFilter->GetOutput() );
131 std::string aRootDir( getenv( "VISU_ROOT_DIR") );
132 std::string aMainTexture = aRootDir + "/share/salome/resources/visu/sprite_texture.bmp";
133 std::string anAlphaTexture = aRootDir + "/share/salome/resources/visu/sprite_alpha.bmp";
134 VISU::TTextureValue aTextureValue = VISU::GetTexture( aMainTexture, anAlphaTexture );
135 aMapper->SetImageData( aTextureValue.GetPointer() );
137 aMapper->SetUseLookupTableScalarRange(true);
138 aMapper->SetColorModeToMapScalars();
139 aMapper->SetScalarVisibility(true);
141 SetPointSpriteMapper( aMapper );
145 myEventCallbackCommand->SetClientData( this );
146 myEventCallbackCommand->SetCallback( VISU_PointsDeviceActor::ProcessEvents );
150 //----------------------------------------------------------------------------
151 ~VISU_PointsDeviceActor()
153 SetInteractor( NULL );
154 myGeomFilter->Delete();
155 myEventCallbackCommand->Delete();
159 //----------------------------------------------------------------------------
162 ProcessEvents(vtkObject* theObject,
163 unsigned long theEvent,
167 if ( VISU_PointsDeviceActor* self = reinterpret_cast<VISU_PointsDeviceActor*>( theClientData ) )
168 self->OnInteractorEvent( theEvent );
172 //----------------------------------------------------------------------------
174 OnInteractorEvent(unsigned long theEvent)
176 switch ( theEvent ) {
177 case vtkCommand::CharEvent: {
178 switch( myInteractor->GetKeyCode() ) {
181 if ( !GetVisibility() )
184 static vtkFloatingPointType anIncrement = 2;
185 vtkFloatingPointType aMagnification = GetPointSpriteMapper()->GetPointSpriteMagnification();
186 vtkFloatingPointType coefficient = myInteractor->GetShiftKey() ? anIncrement : 1 / anIncrement;
188 GetPointSpriteMapper()->SetPointSpriteMagnification( aMagnification * coefficient );
190 myInteractor->CreateTimer(VTKI_TIMER_UPDATE);
204 //----------------------------------------------------------------------------
205 vtkCallbackCommand* myEventCallbackCommand;
206 vtkRenderWindowInteractor* myInteractor;
207 VTKViewer_GeometryFilter* myGeomFilter;
210 VISU_PointsDeviceActor(const VISU_PointsDeviceActor&); // Not implemented
211 void operator=(const VISU_PointsDeviceActor&); // Not implemented
214 vtkStandardNewMacro(VISU_PointsDeviceActor);
216 //----------------------------------------------------------------------------
217 vtkStandardNewMacro(VISU_ScalarMapAct);
218 static vtkFloatingPointType EPS = 1.0 / VTK_LARGE_FLOAT;
220 //----------------------------------------------------------------------------
222 ::VISU_ScalarMapAct()
224 myScalarBar = VISU_ScalarBarActor::New();
226 vtkProperty* aProperty = GetProperty();
227 aProperty->SetAmbient(1.0);
228 aProperty->SetDiffuse(0.0);
229 aProperty->SetSpecular(0.0);
231 myProperty->DeepCopy(aProperty);
233 vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
235 mySurfaceActor= SVTK_DeviceActor::New();
236 mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
237 mySurfaceActor->SetProperty(aProperty);
238 mySurfaceActor->SetUserMatrix(aMatrix);
240 myEdgeActor = SVTK_DeviceActor::New();
241 myEdgeActor->SetRepresentation(SVTK::Representation::Wireframe);
242 myEdgeActor->SetUserMatrix(aMatrix);
243 myEdgeActor->GetProperty()->SetColor(255.,255.,255.);
245 myPointsActor = SVTK_DeviceActor::New();
246 myPointsActor->SetRepresentation(SVTK::Representation::Points);
247 myPointsActor->SetProperty(aProperty);
248 myPointsActor->SetUserMatrix(aMatrix);
250 myPointSpriteActor = VISU_PointsDeviceActor::New();
251 myPointSpriteActor->SetProperty(aProperty);
252 myPointSpriteActor->SetUserMatrix(aMatrix);
256 myIsPointSpriteMode = false;
258 SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
259 //Quadratic 2D elements representation
261 //----------------------------------------------------------------------------
262 int aQuadraticAngle = aResourceMgr->integerValue( "VISU", "max_angle", 2);
263 mySurfaceActor->SetQuadraticArcAngle(aQuadraticAngle);
264 myEdgeActor->SetQuadraticArcAngle(aQuadraticAngle);
266 int anElem0DSize = aResourceMgr->integerValue( "VISU", "elem0d_size", 5);
267 mySurfaceActor->GetProperty()->SetPointSize(anElem0DSize);
271 //----------------------------------------------------------------------------
273 ::~VISU_ScalarMapAct()
275 myScalarBar->Delete();
277 myPointsActor->Delete();
278 myPointSpriteActor->Delete();
280 mySurfaceActor->Delete();
281 myEdgeActor->Delete();
286 ::ShallowCopyPL(VISU_PipeLine* thePipeLine)
288 VISU_DataSetActor::ShallowCopyPL( thePipeLine );
290 myEdgeActor->GetMapper()->ScalarVisibilityOff();
292 myPointSpriteActor->DoMapperShallowCopy( thePipeLine->GetMapper(), false );
294 VISU::CopyMapper( myPointsActor->GetMapper(), thePipeLine->GetMapper(), false );
295 VISU::CopyMapper( mySurfaceActor->GetMapper(), thePipeLine->GetMapper(), false );
298 //----------------------------------------------------------------------------
301 ::SetMapperInput(vtkDataSet* theDataSet)
303 Superclass::SetMapperInput( theDataSet );
305 // myPointsActor->SetInput( theDataSet );
306 // mySurfaceActor->SetInput( theDataSet );
307 // myEdgeActor->SetInput( theDataSet );
309 if (theDataSet->IsA("vtkPolyData")) {
310 myPointSpriteActor->SetInput( myPolyDataExtractor->GetOutput() );
311 myPointsActor->SetInput( myPolyDataExtractor->GetOutput() );
312 mySurfaceActor->SetInput( myPolyDataExtractor->GetOutput() );
313 myEdgeActor->SetInput( myPolyDataExtractor->GetOutput() );
315 myPointSpriteActor->SetInput( myExtractor->GetOutput() );
316 myPointsActor->SetInput( myExtractor->GetOutput() );
317 mySurfaceActor->SetInput( myExtractor->GetOutput() );
318 myEdgeActor->SetInput( myExtractor->GetOutput() );
322 //----------------------------------------------------------------------------
325 ::SetTransform(VTKViewer_Transform* theTransform)
327 Superclass::SetTransform(theTransform);
329 myPointsActor->SetTransform(theTransform);
330 myPointSpriteActor->SetTransform(theTransform);
332 mySurfaceActor->SetTransform(theTransform);
333 myEdgeActor->SetTransform(theTransform);
336 //----------------------------------------------------------------------------
341 return myEdgeActor->GetProperty();
344 //----------------------------------------------------------------------------
347 ::SetShrinkable(bool theIsShrinkable)
349 Superclass::SetShrinkable(theIsShrinkable);
351 mySurfaceActor->SetShrinkable(theIsShrinkable);
356 ::SetShrinkFactor(vtkFloatingPointType theValue)
358 Superclass::SetShrinkFactor(theValue);
360 mySurfaceActor->SetShrinkFactor(theValue);
363 //----------------------------------------------------------------------------
368 if(myRepresentation == VTK_POINTS)
371 Superclass::SetShrink();
373 mySurfaceActor->SetShrink();
380 Superclass::UnShrink();
382 mySurfaceActor->UnShrink();
385 //----------------------------------------------------------------------------
388 ::SetFeatureEdgesAllowed(bool theIsFeatureEdgesAllowed)
390 Superclass::SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
392 mySurfaceActor->SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
397 ::SetFeatureEdgesAngle(vtkFloatingPointType theValue)
399 Superclass::SetFeatureEdgesAngle(theValue);
401 mySurfaceActor->SetFeatureEdgesAngle(theValue);
406 ::SetFeatureEdgesFlags(bool theIsFeatureEdges,
407 bool theIsBoundaryEdges,
408 bool theIsManifoldEdges,
409 bool theIsNonManifoldEdges)
411 Superclass::SetFeatureEdgesFlags(theIsFeatureEdges,
414 theIsNonManifoldEdges);
416 mySurfaceActor->SetFeatureEdgesFlags(theIsFeatureEdges,
419 theIsNonManifoldEdges);
424 ::SetFeatureEdgesColoring(bool theIsColoring)
426 Superclass::SetFeatureEdgesColoring(theIsColoring);
428 mySurfaceActor->SetFeatureEdgesColoring(theIsColoring);
431 //----------------------------------------------------------------------------
434 ::SetFeatureEdgesEnabled(bool theIsFeatureEdgesEnabled)
436 if(theIsFeatureEdgesEnabled && myRepresentation == VTK_POINTS)
439 Superclass::SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
441 mySurfaceActor->SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
444 //----------------------------------------------------------------------------
447 ::SetOpacity(vtkFloatingPointType theValue)
449 mySurfaceActor->GetProperty()->SetOpacity(theValue);
451 vtkFloatingPointType aPointSpriteOpacity = theValue > 0.0 ? 1.0 : 0.0;
452 myPointSpriteActor->GetPointSpriteMapper()->SetPointSpriteOpacity(aPointSpriteOpacity);
459 return mySurfaceActor->GetProperty()->GetOpacity();
462 //----------------------------------------------------------------------------
465 ::SetLineWidth(vtkFloatingPointType theLineWidth)
467 mySurfaceActor->GetProperty()->SetLineWidth(theLineWidth);
471 VISU_ScalarMapAct::GetLineWidth()
473 return mySurfaceActor->GetProperty()->GetLineWidth();
476 //----------------------------------------------------------------------------
479 ::DeepCopy(VISU_Actor *theActor)
481 if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
482 Superclass::DeepCopy(theActor);
483 SetBarVisibility(anActor->GetBarVisibility());
484 myPointSpriteActor->DeepCopy( anActor->myPointSpriteActor );
485 SetShading(anActor->IsShading());
490 //----------------------------------------------------------------------------
493 ::AddToRender(vtkRenderer* theRenderer)
495 Superclass::AddToRender(theRenderer);
497 myPointSpriteActor->SetInteractor( myInteractor );
500 theRenderer->AddActor2D(myScalarBar);
503 //----------------------------------------------------------------------------
506 ::RemoveFromRender(vtkRenderer* theRenderer)
509 theRenderer->RemoveActor(myScalarBar);
511 if ( vtkWindow* aWindow = theRenderer->GetRenderWindow() ) {
512 myPointSpriteActor->ReleaseGraphicsResources( aWindow );
513 myPointsActor->ReleaseGraphicsResources( aWindow );
514 mySurfaceActor->ReleaseGraphicsResources( aWindow );
515 myEdgeActor->ReleaseGraphicsResources( aWindow );
518 Superclass::RemoveFromRender(theRenderer);
521 //----------------------------------------------------------------------------
524 ::SetVisibility(int theMode)
526 Superclass::SetVisibility( theMode );
528 myPointSpriteActor->SetVisibility( theMode );
529 myPointsActor->SetVisibility( theMode );
532 myScalarBar->SetVisibility(myBarVisibility && theMode);
535 //----------------------------------------------------------------------------
540 return myBarVisibility;
543 //----------------------------------------------------------------------------
551 //----------------------------------------------------------------------------
554 ::SetBarVisibility(bool theMode)
556 myBarVisibility = theMode;
558 myScalarBar->SetVisibility(myBarVisibility && GetVisibility());
562 //----------------------------------------------------------------------------
565 ::SetRepresentation(int theMode)
567 bool anIsShanding = IsShading();
569 Superclass::SetRepresentation(theMode);
571 if(theMode == SVTK::Representation::Surfaceframe)
572 mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
574 mySurfaceActor->SetRepresentation(theMode);
576 myPointSpriteActor->SetProperty( mySurfaceActor->GetProperty() );
577 myPointsActor->SetProperty( mySurfaceActor->GetProperty() );
579 SetShading(anIsShanding);
583 //----------------------------------------------------------------------------
586 ::SetShading(bool theOn)
588 vtkProperty* aProperty = mySurfaceActor->GetProperty();
591 aProperty->SetAmbient(0.0);
592 aProperty->SetDiffuse(1.0);
594 aProperty->SetAmbient(1.0);
595 aProperty->SetDiffuse(0.0);
599 //----------------------------------------------------------------------------
604 vtkProperty* aProperty = mySurfaceActor->GetProperty();
606 return (fabs(aProperty->GetAmbient()) < EPS && fabs(aProperty->GetDiffuse() - 1.) < EPS);
611 ::RenderOpaqueGeometry(vtkViewport *ren)
613 GetMatrix(myEdgeActor->GetUserMatrix());
614 GetMatrix(mySurfaceActor->GetUserMatrix());
615 GetMatrix(myPointsActor->GetUserMatrix());
616 GetMatrix(myPointSpriteActor->GetUserMatrix());
618 using namespace SVTK::Representation;
619 switch ( GetRepresentation() ) {
622 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
623 mySurfaceActor->RenderOpaqueGeometry(ren);
625 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
626 myEdgeActor->RenderOpaqueGeometry(ren);
630 if( myIsPointSpriteMode ) {
631 myPointSpriteActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
632 myPointSpriteActor->RenderOpaqueGeometry(ren);
634 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
635 myPointsActor->RenderOpaqueGeometry(ren);
640 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
641 mySurfaceActor->RenderOpaqueGeometry(ren);
649 #if (VTK_XVERSION < 0x050100)
650 ::RenderTranslucentGeometry(vtkViewport *ren)
652 ::RenderTranslucentPolygonalGeometry(vtkViewport *ren)
655 GetMatrix(myEdgeActor->GetUserMatrix());
656 GetMatrix(mySurfaceActor->GetUserMatrix());
658 using namespace SVTK::Representation;
659 switch ( GetRepresentation() ) {
662 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
663 #if (VTK_XVERSION < 0x050100)
664 mySurfaceActor->RenderTranslucentGeometry(ren);
666 mySurfaceActor->RenderTranslucentPolygonalGeometry(ren);
669 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
670 #if (VTK_XVERSION < 0x050100)
671 myEdgeActor->RenderTranslucentGeometry(ren);
673 myEdgeActor->RenderTranslucentPolygonalGeometry(ren);
678 if( myIsPointSpriteMode ) {
679 myPointSpriteActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
680 #if (VTK_XVERSION < 0x050100)
681 myPointSpriteActor->RenderTranslucentGeometry(ren);
683 myPointSpriteActor->RenderTranslucentPolygonalGeometry(ren);
686 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
687 #if (VTK_XVERSION < 0x050100)
688 myPointsActor->RenderTranslucentGeometry(ren);
690 myPointsActor->RenderTranslucentPolygonalGeometry(ren);
696 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
697 #if (VTK_XVERSION < 0x050100)
698 mySurfaceActor->RenderTranslucentGeometry(ren);
700 mySurfaceActor->RenderTranslucentPolygonalGeometry(ren);
707 //----------------------------------------------------------------------------
708 #if (VTK_XVERSION >= 0x050100)
711 ::HasTranslucentPolygonalGeometry()
715 using namespace SVTK::Representation;
716 switch ( GetRepresentation() ) {
718 result |= mySurfaceActor->HasTranslucentPolygonalGeometry();
719 result |= myEdgeActor->HasTranslucentPolygonalGeometry();
722 if( myIsPointSpriteMode )
723 result |= myPointSpriteActor->HasTranslucentPolygonalGeometry();
725 result |= myPointsActor->HasTranslucentPolygonalGeometry();
728 result |= mySurfaceActor->HasTranslucentPolygonalGeometry();
735 //----------------------------------------------------------------------------
740 unsigned long int aSize = Superclass::GetMemorySize();
742 aSize += myPointSpriteActor->GetMemorySize();
747 //----------------------------------------------------------------------------
749 VISU_Actor::EQuadratic2DRepresentation
751 ::GetQuadratic2DRepresentation() const
753 bool mode = (mySurfaceActor->GetQuadraticArcMode() && myEdgeActor->GetQuadraticArcMode());
755 return VISU_Actor::eArcs;
758 return VISU_Actor::eLines;
761 void VISU_ScalarMapAct::SetQuadratic2DRepresentation( VISU_Actor::EQuadratic2DRepresentation theMode )
763 Superclass::SetQuadratic2DRepresentation( theMode );
765 case VISU_Actor::eArcs:
766 mySurfaceActor->SetQuadraticArcMode(true);
767 myEdgeActor->SetQuadraticArcMode(true);
769 case VISU_Actor::eLines:
770 mySurfaceActor->SetQuadraticArcMode(false);
771 myEdgeActor->SetQuadraticArcMode(false);
778 void VISU_ScalarMapAct::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
780 Superclass::SetMarkerStd( theMarkerType, theMarkerScale );
781 myPointsActor->SetMarkerStd( theMarkerType, theMarkerScale );
783 myIsPointSpriteMode = theMarkerType == VTK::MT_POINT_SPRITE;
786 void VISU_ScalarMapAct::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
788 Superclass::SetMarkerTexture( theMarkerId, theMarkerTexture );
789 myPointsActor->SetMarkerTexture( theMarkerId, theMarkerTexture );
791 myIsPointSpriteMode = false;
795 * Set size of the 0D elements.
797 void VISU_ScalarMapAct::Set0DElemSize(vtkFloatingPointType theValue) {
798 mySurfaceActor->GetProperty()->SetPointSize(theValue);
802 * Get size of the 0D elements.
804 vtkFloatingPointType VISU_ScalarMapAct::Get0DElemSize() {
805 return mySurfaceActor->GetProperty()->GetPointSize();