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 );
143 myGeomFilter->SetAppendCoincident3D(true);
147 myEventCallbackCommand->SetClientData( this );
148 myEventCallbackCommand->SetCallback( VISU_PointsDeviceActor::ProcessEvents );
152 //----------------------------------------------------------------------------
153 ~VISU_PointsDeviceActor()
155 SetInteractor( NULL );
156 myGeomFilter->Delete();
157 myEventCallbackCommand->Delete();
161 //----------------------------------------------------------------------------
164 ProcessEvents(vtkObject* theObject,
165 unsigned long theEvent,
169 if ( VISU_PointsDeviceActor* self = reinterpret_cast<VISU_PointsDeviceActor*>( theClientData ) )
170 self->OnInteractorEvent( theEvent );
174 //----------------------------------------------------------------------------
176 OnInteractorEvent(unsigned long theEvent)
178 switch ( theEvent ) {
179 case vtkCommand::CharEvent: {
180 switch( myInteractor->GetKeyCode() ) {
183 if ( !GetVisibility() )
186 static vtkFloatingPointType anIncrement = 2;
187 vtkFloatingPointType aMagnification = GetPointSpriteMapper()->GetPointSpriteMagnification();
188 vtkFloatingPointType coefficient = myInteractor->GetShiftKey() ? anIncrement : 1 / anIncrement;
190 GetPointSpriteMapper()->SetPointSpriteMagnification( aMagnification * coefficient );
192 myInteractor->CreateTimer(VTKI_TIMER_UPDATE);
206 //----------------------------------------------------------------------------
207 vtkCallbackCommand* myEventCallbackCommand;
208 vtkRenderWindowInteractor* myInteractor;
209 VTKViewer_GeometryFilter* myGeomFilter;
212 VISU_PointsDeviceActor(const VISU_PointsDeviceActor&); // Not implemented
213 void operator=(const VISU_PointsDeviceActor&); // Not implemented
216 vtkStandardNewMacro(VISU_PointsDeviceActor);
218 //----------------------------------------------------------------------------
219 vtkStandardNewMacro(VISU_ScalarMapAct);
220 static vtkFloatingPointType EPS = 1.0 / VTK_LARGE_FLOAT;
222 //----------------------------------------------------------------------------
224 ::VISU_ScalarMapAct()
226 myScalarBar = VISU_ScalarBarActor::New();
228 vtkProperty* aProperty = GetProperty();
229 aProperty->SetAmbient(1.0);
230 aProperty->SetDiffuse(0.0);
231 aProperty->SetSpecular(0.0);
233 myProperty->DeepCopy(aProperty);
235 vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
237 mySurfaceActor= SVTK_DeviceActor::New();
238 mySurfaceActor->SetRepresentation(VTKViewer::Representation::Surface);
239 mySurfaceActor->SetProperty(aProperty);
240 mySurfaceActor->SetUserMatrix(aMatrix);
241 mySurfaceActor->SetCoincident3DAllowed(true);
243 myEdgeActor = SVTK_DeviceActor::New();
244 myEdgeActor->SetRepresentation(VTKViewer::Representation::Wireframe);
245 myEdgeActor->SetUserMatrix(aMatrix);
246 myEdgeActor->GetProperty()->SetColor(255.,255.,255.);
248 myEdgeActor->SetCoincident3DAllowed(true);
250 myPointsActor = SVTK_DeviceActor::New();
251 myPointsActor->SetRepresentation(VTKViewer::Representation::Points);
252 myPointsActor->SetProperty(aProperty);
253 myPointsActor->SetUserMatrix(aMatrix);
255 myPointSpriteActor = VISU_PointsDeviceActor::New();
256 myPointSpriteActor->SetProperty(aProperty);
257 myPointSpriteActor->SetUserMatrix(aMatrix);
261 myIsPointSpriteMode = false;
263 SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
264 //Quadratic 2D elements representation
266 //----------------------------------------------------------------------------
267 int aQuadraticAngle = aResourceMgr->integerValue( "VISU", "max_angle", 2);
268 mySurfaceActor->SetQuadraticArcAngle(aQuadraticAngle);
269 myEdgeActor->SetQuadraticArcAngle(aQuadraticAngle);
271 int anElem0DSize = aResourceMgr->integerValue( "VISU", "elem0d_size", 5);
272 mySurfaceActor->GetProperty()->SetPointSize(anElem0DSize);
276 //----------------------------------------------------------------------------
278 ::~VISU_ScalarMapAct()
280 myScalarBar->Delete();
282 myPointsActor->Delete();
283 myPointSpriteActor->Delete();
285 mySurfaceActor->Delete();
286 myEdgeActor->Delete();
291 ::ShallowCopyPL(VISU_PipeLine* thePipeLine)
293 VISU_DataSetActor::ShallowCopyPL( thePipeLine );
295 myEdgeActor->GetMapper()->ScalarVisibilityOff();
297 myPointSpriteActor->DoMapperShallowCopy( thePipeLine->GetMapper(), false );
299 VISU::CopyMapper( myPointsActor->GetMapper(), thePipeLine->GetMapper(), false );
300 VISU::CopyMapper( mySurfaceActor->GetMapper(), thePipeLine->GetMapper(), false );
303 //----------------------------------------------------------------------------
306 ::SetMapperInput(vtkDataSet* theDataSet)
308 Superclass::SetMapperInput( theDataSet );
310 // myPointsActor->SetInput( theDataSet );
311 // mySurfaceActor->SetInput( theDataSet );
312 // myEdgeActor->SetInput( theDataSet );
314 if (theDataSet->IsA("vtkPolyData")) {
315 myPointSpriteActor->SetInput( myPolyDataExtractor->GetOutput() );
316 myPointsActor->SetInput( myPolyDataExtractor->GetOutput() );
317 mySurfaceActor->SetInput( myPolyDataExtractor->GetOutput() );
318 myEdgeActor->SetInput( myPolyDataExtractor->GetOutput() );
320 myPointSpriteActor->SetInput( myExtractor->GetOutput() );
321 myPointsActor->SetInput( myExtractor->GetOutput() );
322 mySurfaceActor->SetInput( myExtractor->GetOutput() );
323 myEdgeActor->SetInput( myExtractor->GetOutput() );
327 //----------------------------------------------------------------------------
330 ::SetTransform(VTKViewer_Transform* theTransform)
332 Superclass::SetTransform(theTransform);
334 myPointsActor->SetTransform(theTransform);
335 myPointSpriteActor->SetTransform(theTransform);
337 mySurfaceActor->SetTransform(theTransform);
338 myEdgeActor->SetTransform(theTransform);
341 //----------------------------------------------------------------------------
346 return myEdgeActor->GetProperty();
349 //----------------------------------------------------------------------------
352 ::SetShrinkable(bool theIsShrinkable)
354 Superclass::SetShrinkable(theIsShrinkable);
356 mySurfaceActor->SetShrinkable(theIsShrinkable);
357 myEdgeActor->SetShrinkable(theIsShrinkable);
362 ::SetShrinkFactor(vtkFloatingPointType theValue)
364 Superclass::SetShrinkFactor(theValue);
366 mySurfaceActor->SetShrinkFactor(theValue);
367 myEdgeActor->SetShrinkFactor(theValue);
370 //----------------------------------------------------------------------------
375 if(myRepresentation == VTK_POINTS)
378 Superclass::SetShrink();
380 mySurfaceActor->SetShrink();
381 myEdgeActor->SetShrink();
388 Superclass::UnShrink();
390 mySurfaceActor->UnShrink();
391 myEdgeActor->UnShrink();
394 //----------------------------------------------------------------------------
397 ::SetFeatureEdgesAllowed(bool theIsFeatureEdgesAllowed)
399 Superclass::SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
401 mySurfaceActor->SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
406 ::SetFeatureEdgesAngle(vtkFloatingPointType theValue)
408 Superclass::SetFeatureEdgesAngle(theValue);
410 mySurfaceActor->SetFeatureEdgesAngle(theValue);
415 ::SetFeatureEdgesFlags(bool theIsFeatureEdges,
416 bool theIsBoundaryEdges,
417 bool theIsManifoldEdges,
418 bool theIsNonManifoldEdges)
420 Superclass::SetFeatureEdgesFlags(theIsFeatureEdges,
423 theIsNonManifoldEdges);
425 mySurfaceActor->SetFeatureEdgesFlags(theIsFeatureEdges,
428 theIsNonManifoldEdges);
433 ::SetFeatureEdgesColoring(bool theIsColoring)
435 Superclass::SetFeatureEdgesColoring(theIsColoring);
437 mySurfaceActor->SetFeatureEdgesColoring(theIsColoring);
440 //----------------------------------------------------------------------------
443 ::SetFeatureEdgesEnabled(bool theIsFeatureEdgesEnabled)
445 if(theIsFeatureEdgesEnabled && myRepresentation == VTK_POINTS)
448 Superclass::SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
450 mySurfaceActor->SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
453 //----------------------------------------------------------------------------
456 ::SetOpacity(vtkFloatingPointType theValue)
458 mySurfaceActor->GetProperty()->SetOpacity(theValue);
460 vtkFloatingPointType aPointSpriteOpacity = theValue > 0.0 ? 1.0 : 0.0;
461 myPointSpriteActor->GetPointSpriteMapper()->SetPointSpriteOpacity(aPointSpriteOpacity);
468 return mySurfaceActor->GetProperty()->GetOpacity();
471 //----------------------------------------------------------------------------
474 ::SetLineWidth(vtkFloatingPointType theLineWidth)
476 mySurfaceActor->GetProperty()->SetLineWidth(theLineWidth);
480 VISU_ScalarMapAct::GetLineWidth()
482 return mySurfaceActor->GetProperty()->GetLineWidth();
485 //----------------------------------------------------------------------------
488 ::DeepCopy(VISU_Actor *theActor)
490 if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
491 Superclass::DeepCopy(theActor);
492 SetBarVisibility(anActor->GetBarVisibility());
493 myPointSpriteActor->DeepCopy( anActor->myPointSpriteActor );
494 SetShading(anActor->IsShading());
499 //----------------------------------------------------------------------------
502 ::AddToRender(vtkRenderer* theRenderer)
504 Superclass::AddToRender(theRenderer);
506 myPointSpriteActor->SetInteractor( myInteractor );
509 theRenderer->AddActor2D(myScalarBar);
512 //----------------------------------------------------------------------------
515 ::RemoveFromRender(vtkRenderer* theRenderer)
518 theRenderer->RemoveActor(myScalarBar);
520 if ( vtkWindow* aWindow = theRenderer->GetRenderWindow() ) {
521 myPointSpriteActor->ReleaseGraphicsResources( aWindow );
522 myPointsActor->ReleaseGraphicsResources( aWindow );
523 mySurfaceActor->ReleaseGraphicsResources( aWindow );
524 myEdgeActor->ReleaseGraphicsResources( aWindow );
527 Superclass::RemoveFromRender(theRenderer);
530 //----------------------------------------------------------------------------
533 ::SetVisibility(int theMode)
535 Superclass::SetVisibility( theMode );
537 myPointSpriteActor->SetVisibility( theMode );
538 myPointsActor->SetVisibility( theMode );
541 myScalarBar->SetVisibility(myBarVisibility && theMode);
544 //----------------------------------------------------------------------------
549 return myBarVisibility;
552 //----------------------------------------------------------------------------
560 //----------------------------------------------------------------------------
563 ::SetBarVisibility(bool theMode)
565 myBarVisibility = theMode;
567 myScalarBar->SetVisibility(myBarVisibility && GetVisibility());
571 //----------------------------------------------------------------------------
574 ::SetRepresentation(int theMode)
576 bool anIsShanding = IsShading();
578 Superclass::SetRepresentation(theMode);
580 if(theMode == VTKViewer::Representation::Surfaceframe)
581 mySurfaceActor->SetRepresentation(VTKViewer::Representation::Surface);
583 mySurfaceActor->SetRepresentation(theMode);
585 myPointSpriteActor->SetProperty( mySurfaceActor->GetProperty() );
586 myPointsActor->SetProperty( mySurfaceActor->GetProperty() );
588 SetShading(anIsShanding);
592 //----------------------------------------------------------------------------
595 ::SetShading(bool theOn)
597 vtkProperty* aProperty = mySurfaceActor->GetProperty();
600 aProperty->SetAmbient(0.0);
601 aProperty->SetDiffuse(1.0);
603 aProperty->SetAmbient(1.0);
604 aProperty->SetDiffuse(0.0);
608 //----------------------------------------------------------------------------
613 vtkProperty* aProperty = mySurfaceActor->GetProperty();
615 return (fabs(aProperty->GetAmbient()) < EPS && fabs(aProperty->GetDiffuse() - 1.) < EPS);
620 ::RenderOpaqueGeometry(vtkViewport *ren)
622 GetMatrix(myEdgeActor->GetUserMatrix());
623 GetMatrix(mySurfaceActor->GetUserMatrix());
624 GetMatrix(myPointsActor->GetUserMatrix());
625 GetMatrix(myPointSpriteActor->GetUserMatrix());
627 using namespace VTKViewer::Representation;
628 switch ( GetRepresentation() ) {
631 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
632 mySurfaceActor->RenderOpaqueGeometry(ren);
634 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
635 myEdgeActor->RenderOpaqueGeometry(ren);
639 if( myIsPointSpriteMode ) {
640 myPointSpriteActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
641 myPointSpriteActor->RenderOpaqueGeometry(ren);
643 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
644 myPointsActor->RenderOpaqueGeometry(ren);
649 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
650 mySurfaceActor->RenderOpaqueGeometry(ren);
658 #if (VTK_XVERSION < 0x050100)
659 ::RenderTranslucentGeometry(vtkViewport *ren)
661 ::RenderTranslucentPolygonalGeometry(vtkViewport *ren)
664 GetMatrix(myEdgeActor->GetUserMatrix());
665 GetMatrix(mySurfaceActor->GetUserMatrix());
667 using namespace VTKViewer::Representation;
668 switch ( GetRepresentation() ) {
671 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
672 #if (VTK_XVERSION < 0x050100)
673 mySurfaceActor->RenderTranslucentGeometry(ren);
675 mySurfaceActor->RenderTranslucentPolygonalGeometry(ren);
678 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
679 #if (VTK_XVERSION < 0x050100)
680 myEdgeActor->RenderTranslucentGeometry(ren);
682 myEdgeActor->RenderTranslucentPolygonalGeometry(ren);
687 if( myIsPointSpriteMode ) {
688 myPointSpriteActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
689 #if (VTK_XVERSION < 0x050100)
690 myPointSpriteActor->RenderTranslucentGeometry(ren);
692 myPointSpriteActor->RenderTranslucentPolygonalGeometry(ren);
695 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
696 #if (VTK_XVERSION < 0x050100)
697 myPointsActor->RenderTranslucentGeometry(ren);
699 myPointsActor->RenderTranslucentPolygonalGeometry(ren);
705 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
706 #if (VTK_XVERSION < 0x050100)
707 mySurfaceActor->RenderTranslucentGeometry(ren);
709 mySurfaceActor->RenderTranslucentPolygonalGeometry(ren);
716 //----------------------------------------------------------------------------
717 #if (VTK_XVERSION >= 0x050100)
720 ::HasTranslucentPolygonalGeometry()
724 using namespace VTKViewer::Representation;
725 switch ( GetRepresentation() ) {
727 result |= mySurfaceActor->HasTranslucentPolygonalGeometry();
728 result |= myEdgeActor->HasTranslucentPolygonalGeometry();
731 if( myIsPointSpriteMode )
732 result |= myPointSpriteActor->HasTranslucentPolygonalGeometry();
734 result |= myPointsActor->HasTranslucentPolygonalGeometry();
737 result |= mySurfaceActor->HasTranslucentPolygonalGeometry();
744 //----------------------------------------------------------------------------
749 unsigned long int aSize = Superclass::GetMemorySize();
751 aSize += myPointSpriteActor->GetMemorySize();
756 //----------------------------------------------------------------------------
758 VISU_Actor::EQuadratic2DRepresentation
760 ::GetQuadratic2DRepresentation() const
762 bool mode = (mySurfaceActor->GetQuadraticArcMode() && myEdgeActor->GetQuadraticArcMode());
764 return VISU_Actor::eArcs;
767 return VISU_Actor::eLines;
770 void VISU_ScalarMapAct::SetQuadratic2DRepresentation( VISU_Actor::EQuadratic2DRepresentation theMode )
772 Superclass::SetQuadratic2DRepresentation( theMode );
774 case VISU_Actor::eArcs:
775 mySurfaceActor->SetQuadraticArcMode(true);
776 myEdgeActor->SetQuadraticArcMode(true);
778 case VISU_Actor::eLines:
779 mySurfaceActor->SetQuadraticArcMode(false);
780 myEdgeActor->SetQuadraticArcMode(false);
787 void VISU_ScalarMapAct::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
789 Superclass::SetMarkerStd( theMarkerType, theMarkerScale );
790 myPointsActor->SetMarkerStd( theMarkerType, theMarkerScale );
792 myIsPointSpriteMode = theMarkerType == VTK::MT_POINT_SPRITE;
795 void VISU_ScalarMapAct::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
797 Superclass::SetMarkerTexture( theMarkerId, theMarkerTexture );
798 myPointsActor->SetMarkerTexture( theMarkerId, theMarkerTexture );
800 myIsPointSpriteMode = false;
804 * Set size of the 0D elements.
806 void VISU_ScalarMapAct::Set0DElemSize(vtkFloatingPointType theValue) {
807 mySurfaceActor->GetProperty()->SetPointSize(theValue);
811 * Get size of the 0D elements.
813 vtkFloatingPointType VISU_ScalarMapAct::Get0DElemSize() {
814 return mySurfaceActor->GetProperty()->GetPointSize();