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"
38 #include <SALOME_ExtractGeometry.h>
39 #include <SALOME_ExtractPolyDataGeometry.h>
41 #include <vtkObjectFactory.h>
42 #include <vtkRenderer.h>
43 #include <vtkProperty.h>
44 #include <vtkMatrix4x4.h>
45 #include <vtkMapper.h>
46 #include <vtkDataSetMapper.h>
48 #include <vtkRenderWindowInteractor.h>
49 #include <vtkCallbackCommand.h>
50 #include <vtkRenderWindow.h>
51 #include <vtkImageData.h>
54 //============================================================================
55 class VISU_PointsDeviceActor: public VISU_GaussDeviceActorBase
58 vtkTypeMacro(VISU_PointsDeviceActor, VISU_GaussDeviceActorBase);
61 VISU_PointsDeviceActor*
65 //----------------------------------------------------------------------------
68 SetInput(vtkDataSet* theDataSet)
70 myGeomFilter->SetInput( theDataSet );
74 //----------------------------------------------------------------------------
76 SetInteractor(vtkRenderWindowInteractor* theInteractor)
78 if(theInteractor == myInteractor)
82 myInteractor->RemoveObserver(myEventCallbackCommand);
85 theInteractor->AddObserver(vtkCommand::CharEvent,
86 myEventCallbackCommand,
89 myInteractor = theInteractor;
93 //----------------------------------------------------------------------------
95 DoMapperShallowCopy( vtkMapper* theMapper,
98 Superclass::DoMapperShallowCopy( theMapper, theIsCopyInput );
100 vtkDataSet* aDataSet = theMapper->GetInput();
101 vtkFloatingPointType aScaleFactor = VISU_DeformedShapePL::GetScaleFactor( aDataSet );
103 GetPointSpriteMapper()->SetAverageCellSize( aScaleFactor );
107 //----------------------------------------------------------------------------
109 DeepCopy( VISU_PointsDeviceActor *theActor )
111 VISU::CopyPointSpriteDataMapper( GetPointSpriteMapper(), theActor->GetPointSpriteMapper(), false );
115 //----------------------------------------------------------------------------
116 VISU_PointsDeviceActor():
117 myGeomFilter( VTKViewer_GeometryFilter::New() ),
118 myEventCallbackCommand( vtkCallbackCommand::New() ),
121 myGeomFilter->SetInside(true);
123 VISU_OpenGLPointSpriteMapper* aMapper = VISU_OpenGLPointSpriteMapper::New();
124 aMapper->SetInput( myGeomFilter->GetOutput() );
126 std::string aRootDir( getenv( "VISU_ROOT_DIR") );
127 std::string aMainTexture = aRootDir + "/share/salome/resources/visu/sprite_texture.bmp";
128 std::string anAlphaTexture = aRootDir + "/share/salome/resources/visu/sprite_alpha.bmp";
129 VISU::TTextureValue aTextureValue = VISU::GetTexture( aMainTexture, anAlphaTexture );
130 aMapper->SetImageData( aTextureValue.GetPointer() );
132 aMapper->SetUseLookupTableScalarRange(true);
133 aMapper->SetColorModeToMapScalars();
134 aMapper->SetScalarVisibility(true);
136 SetPointSpriteMapper( aMapper );
140 myEventCallbackCommand->SetClientData( this );
141 myEventCallbackCommand->SetCallback( VISU_PointsDeviceActor::ProcessEvents );
145 //----------------------------------------------------------------------------
146 ~VISU_PointsDeviceActor()
148 SetInteractor( NULL );
149 myGeomFilter->Delete();
150 myEventCallbackCommand->Delete();
154 //----------------------------------------------------------------------------
157 ProcessEvents(vtkObject* theObject,
158 unsigned long theEvent,
162 if ( VISU_PointsDeviceActor* self = reinterpret_cast<VISU_PointsDeviceActor*>( theClientData ) )
163 self->OnInteractorEvent( theEvent );
167 //----------------------------------------------------------------------------
169 OnInteractorEvent(unsigned long theEvent)
171 switch ( theEvent ) {
172 case vtkCommand::CharEvent: {
173 switch( myInteractor->GetKeyCode() ) {
176 if ( !GetVisibility() )
179 static vtkFloatingPointType anIncrement = 2;
180 vtkFloatingPointType aMagnification = GetPointSpriteMapper()->GetPointSpriteMagnification();
181 vtkFloatingPointType coefficient = myInteractor->GetShiftKey() ? anIncrement : 1 / anIncrement;
183 GetPointSpriteMapper()->SetPointSpriteMagnification( aMagnification * coefficient );
185 myInteractor->CreateTimer(VTKI_TIMER_UPDATE);
199 //----------------------------------------------------------------------------
200 vtkCallbackCommand* myEventCallbackCommand;
201 vtkRenderWindowInteractor* myInteractor;
202 VTKViewer_GeometryFilter* myGeomFilter;
205 VISU_PointsDeviceActor(const VISU_PointsDeviceActor&); // Not implemented
206 void operator=(const VISU_PointsDeviceActor&); // Not implemented
209 vtkStandardNewMacro(VISU_PointsDeviceActor);
211 //----------------------------------------------------------------------------
212 vtkStandardNewMacro(VISU_ScalarMapAct);
213 static vtkFloatingPointType EPS = 1.0 / VTK_LARGE_FLOAT;
215 //----------------------------------------------------------------------------
217 ::VISU_ScalarMapAct()
219 myScalarBar = VISU_ScalarBarActor::New();
221 vtkProperty* aProperty = GetProperty();
222 aProperty->SetAmbient(1.0);
223 aProperty->SetDiffuse(0.0);
224 aProperty->SetSpecular(0.0);
226 myProperty->DeepCopy(aProperty);
228 vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
230 mySurfaceActor= SVTK_DeviceActor::New();
231 mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
232 mySurfaceActor->SetProperty(aProperty);
233 mySurfaceActor->SetUserMatrix(aMatrix);
235 myEdgeActor = SVTK_DeviceActor::New();
236 myEdgeActor->SetRepresentation(SVTK::Representation::Wireframe);
237 myEdgeActor->SetUserMatrix(aMatrix);
238 myEdgeActor->GetProperty()->SetColor(255.,255.,255.);
240 myPointsActor = VISU_PointsDeviceActor::New();
241 myPointsActor->SetProperty(aProperty);
242 myPointsActor->SetUserMatrix(aMatrix);
247 //----------------------------------------------------------------------------
249 ::~VISU_ScalarMapAct()
251 myScalarBar->Delete();
253 myPointsActor->Delete();
255 mySurfaceActor->Delete();
256 myEdgeActor->Delete();
261 ::ShallowCopyPL(VISU_PipeLine* thePipeLine)
263 VISU_DataSetActor::ShallowCopyPL( thePipeLine );
265 myEdgeActor->GetMapper()->ScalarVisibilityOff();
267 myPointsActor->DoMapperShallowCopy( thePipeLine->GetMapper(), false );
269 VISU::CopyMapper( mySurfaceActor->GetMapper(), thePipeLine->GetMapper(), false );
272 //----------------------------------------------------------------------------
275 ::SetMapperInput(vtkDataSet* theDataSet)
277 Superclass::SetMapperInput( theDataSet );
279 // myPointsActor->SetInput( theDataSet );
280 // mySurfaceActor->SetInput( theDataSet );
281 // myEdgeActor->SetInput( theDataSet );
283 if (theDataSet->IsA("vtkPolyData")) {
284 myPointsActor->SetInput( myPolyDataExtractor->GetOutput() );
285 mySurfaceActor->SetInput( myPolyDataExtractor->GetOutput() );
286 myEdgeActor->SetInput( myPolyDataExtractor->GetOutput() );
288 myPointsActor->SetInput( myExtractor->GetOutput() );
289 mySurfaceActor->SetInput( myExtractor->GetOutput() );
290 myEdgeActor->SetInput( myExtractor->GetOutput() );
294 //----------------------------------------------------------------------------
297 ::SetTransform(VTKViewer_Transform* theTransform)
299 Superclass::SetTransform(theTransform);
301 myPointsActor->SetTransform(theTransform);
303 mySurfaceActor->SetTransform(theTransform);
304 myEdgeActor->SetTransform(theTransform);
307 //----------------------------------------------------------------------------
310 ::SetShrinkable(bool theIsShrinkable)
312 Superclass::SetShrinkable(theIsShrinkable);
314 mySurfaceActor->SetShrinkable(theIsShrinkable);
319 ::SetShrinkFactor(vtkFloatingPointType theValue)
321 Superclass::SetShrinkFactor(theValue);
323 mySurfaceActor->SetShrinkFactor(theValue);
326 //----------------------------------------------------------------------------
331 if(myRepresentation == VTK_POINTS)
334 Superclass::SetShrink();
336 mySurfaceActor->SetShrink();
343 Superclass::UnShrink();
345 mySurfaceActor->UnShrink();
348 //----------------------------------------------------------------------------
351 ::SetFeatureEdgesAllowed(bool theIsFeatureEdgesAllowed)
353 Superclass::SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
355 mySurfaceActor->SetFeatureEdgesAllowed(theIsFeatureEdgesAllowed);
360 ::SetFeatureEdgesAngle(vtkFloatingPointType theValue)
362 Superclass::SetFeatureEdgesAngle(theValue);
364 mySurfaceActor->SetFeatureEdgesAngle(theValue);
369 ::SetFeatureEdgesFlags(bool theIsFeatureEdges,
370 bool theIsBoundaryEdges,
371 bool theIsManifoldEdges,
372 bool theIsNonManifoldEdges)
374 Superclass::SetFeatureEdgesFlags(theIsFeatureEdges,
377 theIsNonManifoldEdges);
379 mySurfaceActor->SetFeatureEdgesFlags(theIsFeatureEdges,
382 theIsNonManifoldEdges);
387 ::SetFeatureEdgesColoring(bool theIsColoring)
389 Superclass::SetFeatureEdgesColoring(theIsColoring);
391 mySurfaceActor->SetFeatureEdgesColoring(theIsColoring);
394 //----------------------------------------------------------------------------
397 ::SetFeatureEdgesEnabled(bool theIsFeatureEdgesEnabled)
399 if(theIsFeatureEdgesEnabled && myRepresentation == VTK_POINTS)
402 Superclass::SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
404 mySurfaceActor->SetFeatureEdgesEnabled(theIsFeatureEdgesEnabled);
407 //----------------------------------------------------------------------------
410 ::SetOpacity(vtkFloatingPointType theValue)
412 mySurfaceActor->GetProperty()->SetOpacity(theValue);
419 return mySurfaceActor->GetProperty()->GetOpacity();
422 //----------------------------------------------------------------------------
425 ::SetLineWidth(vtkFloatingPointType theLineWidth)
427 mySurfaceActor->GetProperty()->SetLineWidth(theLineWidth);
431 VISU_ScalarMapAct::GetLineWidth()
433 return mySurfaceActor->GetProperty()->GetLineWidth();
436 //----------------------------------------------------------------------------
439 ::DeepCopy(VISU_Actor *theActor)
441 if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
442 Superclass::DeepCopy(theActor);
443 SetBarVisibility(anActor->GetBarVisibility());
444 myPointsActor->DeepCopy( anActor->myPointsActor );
445 SetShading(anActor->IsShading());
450 //----------------------------------------------------------------------------
453 ::AddToRender(vtkRenderer* theRenderer)
455 Superclass::AddToRender(theRenderer);
457 myPointsActor->SetInteractor( myInteractor );
460 theRenderer->AddActor2D(myScalarBar);
463 //----------------------------------------------------------------------------
466 ::RemoveFromRender(vtkRenderer* theRenderer)
469 theRenderer->RemoveActor(myScalarBar);
471 if ( vtkWindow* aWindow = theRenderer->GetRenderWindow() ) {
472 myPointsActor->ReleaseGraphicsResources( aWindow );
473 mySurfaceActor->ReleaseGraphicsResources( aWindow );
474 myEdgeActor->ReleaseGraphicsResources( aWindow );
477 Superclass::RemoveFromRender(theRenderer);
480 //----------------------------------------------------------------------------
483 ::SetVisibility(int theMode)
485 Superclass::SetVisibility( theMode );
487 myPointsActor->SetVisibility( theMode );
490 myScalarBar->SetVisibility(myBarVisibility && theMode);
493 //----------------------------------------------------------------------------
498 return myBarVisibility;
501 //----------------------------------------------------------------------------
509 //----------------------------------------------------------------------------
512 ::SetBarVisibility(bool theMode)
514 myBarVisibility = theMode;
516 myScalarBar->SetVisibility(myBarVisibility);
520 //----------------------------------------------------------------------------
523 ::SetRepresentation(int theMode)
525 bool anIsShanding = IsShading();
527 Superclass::SetRepresentation(theMode);
529 if(theMode == SVTK::Representation::Surfaceframe)
530 mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
532 mySurfaceActor->SetRepresentation(theMode);
534 myPointsActor->SetProperty( mySurfaceActor->GetProperty() );
536 SetShading(anIsShanding);
540 //----------------------------------------------------------------------------
543 ::SetShading(bool theOn)
545 vtkProperty* aProperty = mySurfaceActor->GetProperty();
548 aProperty->SetAmbient(0.0);
549 aProperty->SetDiffuse(1.0);
551 aProperty->SetAmbient(1.0);
552 aProperty->SetDiffuse(0.0);
556 //----------------------------------------------------------------------------
561 vtkProperty* aProperty = mySurfaceActor->GetProperty();
563 return (fabs(aProperty->GetAmbient()) < EPS && fabs(aProperty->GetDiffuse() - 1.) < EPS);
568 ::RenderOpaqueGeometry(vtkViewport *ren)
570 GetMatrix(myEdgeActor->GetUserMatrix());
571 GetMatrix(mySurfaceActor->GetUserMatrix());
572 GetMatrix(myPointsActor->GetUserMatrix());
574 using namespace SVTK::Representation;
575 switch ( GetRepresentation() ) {
578 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
579 mySurfaceActor->RenderOpaqueGeometry(ren);
581 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
582 myEdgeActor->RenderOpaqueGeometry(ren);
586 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
587 myPointsActor->RenderOpaqueGeometry(ren);
591 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
592 mySurfaceActor->RenderOpaqueGeometry(ren);
600 ::RenderTranslucentGeometry(vtkViewport *ren)
602 GetMatrix(myEdgeActor->GetUserMatrix());
603 GetMatrix(mySurfaceActor->GetUserMatrix());
605 using namespace SVTK::Representation;
606 switch ( GetRepresentation() ) {
609 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
610 mySurfaceActor->RenderTranslucentGeometry(ren);
612 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
613 myEdgeActor->RenderTranslucentGeometry(ren);
617 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
618 myPointsActor->RenderTranslucentGeometry(ren);
622 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
623 mySurfaceActor->RenderTranslucentGeometry(ren);
629 //----------------------------------------------------------------------------
634 unsigned long int aSize = Superclass::GetMemorySize();
636 aSize += myPointsActor->GetMemorySize();
641 //----------------------------------------------------------------------------