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);
352 myEdgeActor->SetShrinkable(theIsShrinkable);
357 ::SetShrinkFactor(vtkFloatingPointType theValue)
359 Superclass::SetShrinkFactor(theValue);
361 mySurfaceActor->SetShrinkFactor(theValue);
362 myEdgeActor->SetShrinkFactor(theValue);
365 //----------------------------------------------------------------------------
370 if(myRepresentation == VTK_POINTS)
373 Superclass::SetShrink();
375 mySurfaceActor->SetShrink();
376 myEdgeActor->SetShrink();
383 Superclass::UnShrink();
385 mySurfaceActor->UnShrink();
386 myEdgeActor->UnShrink();
389 //----------------------------------------------------------------------------
392 ::SetFeatureEdgesAllowed(bool theIsFeatureEdgesAllowed)
394 Superclass::SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
396 mySurfaceActor->SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
401 ::SetFeatureEdgesAngle(vtkFloatingPointType theValue)
403 Superclass::SetFeatureEdgesAngle(theValue);
405 mySurfaceActor->SetFeatureEdgesAngle(theValue);
410 ::SetFeatureEdgesFlags(bool theIsFeatureEdges,
411 bool theIsBoundaryEdges,
412 bool theIsManifoldEdges,
413 bool theIsNonManifoldEdges)
415 Superclass::SetFeatureEdgesFlags(theIsFeatureEdges,
418 theIsNonManifoldEdges);
420 mySurfaceActor->SetFeatureEdgesFlags(theIsFeatureEdges,
423 theIsNonManifoldEdges);
428 ::SetFeatureEdgesColoring(bool theIsColoring)
430 Superclass::SetFeatureEdgesColoring(theIsColoring);
432 mySurfaceActor->SetFeatureEdgesColoring(theIsColoring);
435 //----------------------------------------------------------------------------
438 ::SetFeatureEdgesEnabled(bool theIsFeatureEdgesEnabled)
440 if(theIsFeatureEdgesEnabled && myRepresentation == VTK_POINTS)
443 Superclass::SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
445 mySurfaceActor->SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
448 //----------------------------------------------------------------------------
451 ::SetOpacity(vtkFloatingPointType theValue)
453 mySurfaceActor->GetProperty()->SetOpacity(theValue);
455 vtkFloatingPointType aPointSpriteOpacity = theValue > 0.0 ? 1.0 : 0.0;
456 myPointSpriteActor->GetPointSpriteMapper()->SetPointSpriteOpacity(aPointSpriteOpacity);
463 return mySurfaceActor->GetProperty()->GetOpacity();
466 //----------------------------------------------------------------------------
469 ::SetLineWidth(vtkFloatingPointType theLineWidth)
471 mySurfaceActor->GetProperty()->SetLineWidth(theLineWidth);
475 VISU_ScalarMapAct::GetLineWidth()
477 return mySurfaceActor->GetProperty()->GetLineWidth();
480 //----------------------------------------------------------------------------
483 ::DeepCopy(VISU_Actor *theActor)
485 if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
486 Superclass::DeepCopy(theActor);
487 SetBarVisibility(anActor->GetBarVisibility());
488 myPointSpriteActor->DeepCopy( anActor->myPointSpriteActor );
489 SetShading(anActor->IsShading());
494 //----------------------------------------------------------------------------
497 ::AddToRender(vtkRenderer* theRenderer)
499 Superclass::AddToRender(theRenderer);
501 myPointSpriteActor->SetInteractor( myInteractor );
504 theRenderer->AddActor2D(myScalarBar);
507 //----------------------------------------------------------------------------
510 ::RemoveFromRender(vtkRenderer* theRenderer)
513 theRenderer->RemoveActor(myScalarBar);
515 if ( vtkWindow* aWindow = theRenderer->GetRenderWindow() ) {
516 myPointSpriteActor->ReleaseGraphicsResources( aWindow );
517 myPointsActor->ReleaseGraphicsResources( aWindow );
518 mySurfaceActor->ReleaseGraphicsResources( aWindow );
519 myEdgeActor->ReleaseGraphicsResources( aWindow );
522 Superclass::RemoveFromRender(theRenderer);
525 //----------------------------------------------------------------------------
528 ::SetVisibility(int theMode)
530 Superclass::SetVisibility( theMode );
532 myPointSpriteActor->SetVisibility( theMode );
533 myPointsActor->SetVisibility( theMode );
536 myScalarBar->SetVisibility(myBarVisibility && theMode);
539 //----------------------------------------------------------------------------
544 return myBarVisibility;
547 //----------------------------------------------------------------------------
555 //----------------------------------------------------------------------------
558 ::SetBarVisibility(bool theMode)
560 myBarVisibility = theMode;
562 myScalarBar->SetVisibility(myBarVisibility && GetVisibility());
566 //----------------------------------------------------------------------------
569 ::SetRepresentation(int theMode)
571 bool anIsShanding = IsShading();
573 Superclass::SetRepresentation(theMode);
575 if(theMode == SVTK::Representation::Surfaceframe)
576 mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
578 mySurfaceActor->SetRepresentation(theMode);
580 myPointSpriteActor->SetProperty( mySurfaceActor->GetProperty() );
581 myPointsActor->SetProperty( mySurfaceActor->GetProperty() );
583 SetShading(anIsShanding);
587 //----------------------------------------------------------------------------
590 ::SetShading(bool theOn)
592 vtkProperty* aProperty = mySurfaceActor->GetProperty();
595 aProperty->SetAmbient(0.0);
596 aProperty->SetDiffuse(1.0);
598 aProperty->SetAmbient(1.0);
599 aProperty->SetDiffuse(0.0);
603 //----------------------------------------------------------------------------
608 vtkProperty* aProperty = mySurfaceActor->GetProperty();
610 return (fabs(aProperty->GetAmbient()) < EPS && fabs(aProperty->GetDiffuse() - 1.) < EPS);
615 ::RenderOpaqueGeometry(vtkViewport *ren)
617 GetMatrix(myEdgeActor->GetUserMatrix());
618 GetMatrix(mySurfaceActor->GetUserMatrix());
619 GetMatrix(myPointsActor->GetUserMatrix());
620 GetMatrix(myPointSpriteActor->GetUserMatrix());
622 using namespace SVTK::Representation;
623 switch ( GetRepresentation() ) {
626 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
627 mySurfaceActor->RenderOpaqueGeometry(ren);
629 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
630 myEdgeActor->RenderOpaqueGeometry(ren);
634 if( myIsPointSpriteMode ) {
635 myPointSpriteActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
636 myPointSpriteActor->RenderOpaqueGeometry(ren);
638 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
639 myPointsActor->RenderOpaqueGeometry(ren);
644 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
645 mySurfaceActor->RenderOpaqueGeometry(ren);
653 #if (VTK_XVERSION < 0x050100)
654 ::RenderTranslucentGeometry(vtkViewport *ren)
656 ::RenderTranslucentPolygonalGeometry(vtkViewport *ren)
659 GetMatrix(myEdgeActor->GetUserMatrix());
660 GetMatrix(mySurfaceActor->GetUserMatrix());
662 using namespace SVTK::Representation;
663 switch ( GetRepresentation() ) {
666 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
667 #if (VTK_XVERSION < 0x050100)
668 mySurfaceActor->RenderTranslucentGeometry(ren);
670 mySurfaceActor->RenderTranslucentPolygonalGeometry(ren);
673 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
674 #if (VTK_XVERSION < 0x050100)
675 myEdgeActor->RenderTranslucentGeometry(ren);
677 myEdgeActor->RenderTranslucentPolygonalGeometry(ren);
682 if( myIsPointSpriteMode ) {
683 myPointSpriteActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
684 #if (VTK_XVERSION < 0x050100)
685 myPointSpriteActor->RenderTranslucentGeometry(ren);
687 myPointSpriteActor->RenderTranslucentPolygonalGeometry(ren);
690 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
691 #if (VTK_XVERSION < 0x050100)
692 myPointsActor->RenderTranslucentGeometry(ren);
694 myPointsActor->RenderTranslucentPolygonalGeometry(ren);
700 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
701 #if (VTK_XVERSION < 0x050100)
702 mySurfaceActor->RenderTranslucentGeometry(ren);
704 mySurfaceActor->RenderTranslucentPolygonalGeometry(ren);
711 //----------------------------------------------------------------------------
712 #if (VTK_XVERSION >= 0x050100)
715 ::HasTranslucentPolygonalGeometry()
719 using namespace SVTK::Representation;
720 switch ( GetRepresentation() ) {
722 result |= mySurfaceActor->HasTranslucentPolygonalGeometry();
723 result |= myEdgeActor->HasTranslucentPolygonalGeometry();
726 if( myIsPointSpriteMode )
727 result |= myPointSpriteActor->HasTranslucentPolygonalGeometry();
729 result |= myPointsActor->HasTranslucentPolygonalGeometry();
732 result |= mySurfaceActor->HasTranslucentPolygonalGeometry();
739 //----------------------------------------------------------------------------
744 unsigned long int aSize = Superclass::GetMemorySize();
746 aSize += myPointSpriteActor->GetMemorySize();
751 //----------------------------------------------------------------------------
753 VISU_Actor::EQuadratic2DRepresentation
755 ::GetQuadratic2DRepresentation() const
757 bool mode = (mySurfaceActor->GetQuadraticArcMode() && myEdgeActor->GetQuadraticArcMode());
759 return VISU_Actor::eArcs;
762 return VISU_Actor::eLines;
765 void VISU_ScalarMapAct::SetQuadratic2DRepresentation( VISU_Actor::EQuadratic2DRepresentation theMode )
767 Superclass::SetQuadratic2DRepresentation( theMode );
769 case VISU_Actor::eArcs:
770 mySurfaceActor->SetQuadraticArcMode(true);
771 myEdgeActor->SetQuadraticArcMode(true);
773 case VISU_Actor::eLines:
774 mySurfaceActor->SetQuadraticArcMode(false);
775 myEdgeActor->SetQuadraticArcMode(false);
782 void VISU_ScalarMapAct::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
784 Superclass::SetMarkerStd( theMarkerType, theMarkerScale );
785 myPointsActor->SetMarkerStd( theMarkerType, theMarkerScale );
787 myIsPointSpriteMode = theMarkerType == VTK::MT_POINT_SPRITE;
790 void VISU_ScalarMapAct::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
792 Superclass::SetMarkerTexture( theMarkerId, theMarkerTexture );
793 myPointsActor->SetMarkerTexture( theMarkerId, theMarkerTexture );
795 myIsPointSpriteMode = false;
799 * Set size of the 0D elements.
801 void VISU_ScalarMapAct::Set0DElemSize(vtkFloatingPointType theValue) {
802 mySurfaceActor->GetProperty()->SetPointSize(theValue);
806 * Get size of the 0D elements.
808 vtkFloatingPointType VISU_ScalarMapAct::Get0DElemSize() {
809 return mySurfaceActor->GetProperty()->GetPointSize();