1 // VISU OBJECT : interactive object for VISU entities implementation
3 // Copyright (C) 2003 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
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"
39 #include <vtkObjectFactory.h>
40 #include <vtkRenderer.h>
41 #include <vtkProperty.h>
42 #include <vtkMatrix4x4.h>
43 #include <vtkMapper.h>
44 #include <vtkDataSetMapper.h>
46 #include <vtkRenderWindowInteractor.h>
47 #include <vtkCallbackCommand.h>
48 #include <vtkRenderWindow.h>
49 #include <vtkImageData.h>
52 //============================================================================
53 class VISU_PointsDeviceActor: public VISU_GaussDeviceActorBase
56 vtkTypeMacro(VISU_PointsDeviceActor, VISU_GaussDeviceActorBase);
59 VISU_PointsDeviceActor*
63 //----------------------------------------------------------------------------
66 SetInput(vtkDataSet* theDataSet)
68 myGeomFilter->SetInput( theDataSet );
72 //----------------------------------------------------------------------------
74 SetInteractor(vtkRenderWindowInteractor* theInteractor)
76 if(theInteractor == myInteractor)
80 myInteractor->RemoveObserver(myEventCallbackCommand);
83 theInteractor->AddObserver(vtkCommand::CharEvent,
84 myEventCallbackCommand,
87 myInteractor = theInteractor;
91 //----------------------------------------------------------------------------
93 DoMapperShallowCopy( vtkMapper* theMapper,
96 Superclass::DoMapperShallowCopy( theMapper, theIsCopyInput );
98 vtkDataSet* aDataSet = theMapper->GetInput();
99 vtkFloatingPointType aScaleFactor = VISU_DeformedShapePL::GetScaleFactor( aDataSet );
101 GetPointSpriteMapper()->SetAverageCellSize( aScaleFactor );
105 //----------------------------------------------------------------------------
107 DeepCopy( VISU_PointsDeviceActor *theActor )
109 VISU::CopyPointSpriteDataMapper( GetPointSpriteMapper(), theActor->GetPointSpriteMapper(), false );
113 //----------------------------------------------------------------------------
114 VISU_PointsDeviceActor():
115 myGeomFilter( VTKViewer_GeometryFilter::New() ),
116 myEventCallbackCommand( vtkCallbackCommand::New() ),
119 myGeomFilter->SetInside(true);
121 VISU_OpenGLPointSpriteMapper* aMapper = VISU_OpenGLPointSpriteMapper::New();
122 aMapper->SetInput( myGeomFilter->GetOutput() );
124 std::string aRootDir( getenv( "VISU_ROOT_DIR") );
125 std::string aMainTexture = aRootDir + "/share/salome/resources/visu/sprite_texture.bmp";
126 std::string anAlphaTexture = aRootDir + "/share/salome/resources/visu/sprite_alpha.bmp";
127 VISU::TTextureValue aTextureValue = VISU::GetTexture( aMainTexture, anAlphaTexture );
128 aMapper->SetImageData( aTextureValue.GetPointer() );
130 aMapper->SetUseLookupTableScalarRange(true);
131 aMapper->SetColorModeToMapScalars();
132 aMapper->SetScalarVisibility(true);
134 SetPointSpriteMapper( aMapper );
138 myEventCallbackCommand->SetClientData( this );
139 myEventCallbackCommand->SetCallback( VISU_PointsDeviceActor::ProcessEvents );
143 //----------------------------------------------------------------------------
144 ~VISU_PointsDeviceActor()
146 SetInteractor( NULL );
147 myGeomFilter->Delete();
148 myEventCallbackCommand->Delete();
152 //----------------------------------------------------------------------------
155 ProcessEvents(vtkObject* theObject,
156 unsigned long theEvent,
160 if ( VISU_PointsDeviceActor* self = reinterpret_cast<VISU_PointsDeviceActor*>( theClientData ) )
161 self->OnInteractorEvent( theEvent );
165 //----------------------------------------------------------------------------
167 OnInteractorEvent(unsigned long theEvent)
169 switch ( theEvent ) {
170 case vtkCommand::CharEvent: {
171 switch( myInteractor->GetKeyCode() ) {
174 if ( !GetVisibility() )
177 static vtkFloatingPointType anIncrement = 2;
178 vtkFloatingPointType aMagnification = GetPointSpriteMapper()->GetPointSpriteMagnification();
179 vtkFloatingPointType coefficient = myInteractor->GetShiftKey() ? anIncrement : 1 / anIncrement;
181 GetPointSpriteMapper()->SetPointSpriteMagnification( aMagnification * coefficient );
183 myInteractor->CreateTimer(VTKI_TIMER_UPDATE);
197 //----------------------------------------------------------------------------
198 vtkCallbackCommand* myEventCallbackCommand;
199 vtkRenderWindowInteractor* myInteractor;
200 VTKViewer_GeometryFilter* myGeomFilter;
203 VISU_PointsDeviceActor(const VISU_PointsDeviceActor&); // Not implemented
204 void operator=(const VISU_PointsDeviceActor&); // Not implemented
207 vtkStandardNewMacro(VISU_PointsDeviceActor);
209 //----------------------------------------------------------------------------
210 vtkStandardNewMacro(VISU_ScalarMapAct);
211 static vtkFloatingPointType EPS = 1.0 / VTK_LARGE_FLOAT;
213 //----------------------------------------------------------------------------
215 ::VISU_ScalarMapAct()
217 myScalarBar = VISU_ScalarBarActor::New();
219 vtkProperty* aProperty = GetProperty();
220 aProperty->SetAmbient(1.0);
221 aProperty->SetDiffuse(0.0);
222 aProperty->SetSpecular(0.0);
224 myProperty->DeepCopy(aProperty);
226 vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
228 mySurfaceActor= SVTK_DeviceActor::New();
229 mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
230 mySurfaceActor->SetProperty(aProperty);
231 mySurfaceActor->SetUserMatrix(aMatrix);
233 myEdgeActor = SVTK_DeviceActor::New();
234 myEdgeActor->SetRepresentation(SVTK::Representation::Wireframe);
235 myEdgeActor->SetUserMatrix(aMatrix);
236 myEdgeActor->GetProperty()->SetColor(255.,255.,255.);
238 myPointsActor = VISU_PointsDeviceActor::New();
239 myPointsActor->SetProperty(aProperty);
240 myPointsActor->SetUserMatrix(aMatrix);
245 //----------------------------------------------------------------------------
247 ::~VISU_ScalarMapAct()
249 myScalarBar->Delete();
251 myPointsActor->Delete();
253 mySurfaceActor->Delete();
254 myEdgeActor->Delete();
259 ::ShallowCopyPL(VISU_PipeLine* thePipeLine)
261 VISU_Actor::ShallowCopyPL( thePipeLine );
263 myEdgeActor->GetMapper()->ScalarVisibilityOff();
265 myPointsActor->DoMapperShallowCopy( thePipeLine->GetMapper(), false );
267 VISU::CopyMapper( mySurfaceActor->GetMapper(), thePipeLine->GetMapper(), false );
270 //----------------------------------------------------------------------------
273 ::SetMapperInput(vtkDataSet* theDataSet)
275 Superclass::SetMapperInput( theDataSet );
277 myPointsActor->SetInput( theDataSet );
279 mySurfaceActor->SetInput( theDataSet );
280 myEdgeActor->SetInput( theDataSet );
283 //----------------------------------------------------------------------------
286 ::SetTransform(VTKViewer_Transform* theTransform)
288 Superclass::SetTransform(theTransform);
290 myPointsActor->SetTransform(theTransform);
292 mySurfaceActor->SetTransform(theTransform);
293 myEdgeActor->SetTransform(theTransform);
296 //----------------------------------------------------------------------------
299 ::SetShrinkable(bool theIsShrinkable)
301 Superclass::SetShrinkable(theIsShrinkable);
303 mySurfaceActor->SetShrinkable(theIsShrinkable);
308 ::SetShrinkFactor(vtkFloatingPointType theValue)
310 Superclass::SetShrinkFactor(theValue);
312 mySurfaceActor->SetShrinkFactor(theValue);
315 //----------------------------------------------------------------------------
320 if(myRepresentation == VTK_POINTS)
323 Superclass::SetShrink();
325 mySurfaceActor->SetShrink();
332 Superclass::UnShrink();
334 mySurfaceActor->UnShrink();
337 //----------------------------------------------------------------------------
340 ::SetOpacity(vtkFloatingPointType theValue)
342 mySurfaceActor->GetProperty()->SetOpacity(theValue);
349 return mySurfaceActor->GetProperty()->GetOpacity();
352 //----------------------------------------------------------------------------
355 ::SetLineWidth(vtkFloatingPointType theLineWidth)
357 mySurfaceActor->GetProperty()->SetLineWidth(theLineWidth);
361 VISU_ScalarMapAct::GetLineWidth()
363 return mySurfaceActor->GetProperty()->GetLineWidth();
366 //----------------------------------------------------------------------------
369 ::DeepCopy(VISU_Actor *theActor)
371 if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
372 Superclass::DeepCopy(theActor);
373 SetBarVisibility(anActor->GetBarVisibility());
374 myPointsActor->DeepCopy( anActor->myPointsActor );
375 SetShading(anActor->IsShading());
380 //----------------------------------------------------------------------------
383 ::AddToRender(vtkRenderer* theRenderer)
385 Superclass::AddToRender(theRenderer);
387 myPointsActor->SetInteractor( myInteractor );
390 theRenderer->AddActor2D(myScalarBar);
393 //----------------------------------------------------------------------------
396 ::RemoveFromRender(vtkRenderer* theRenderer)
399 theRenderer->RemoveActor(myScalarBar);
401 if ( vtkWindow* aWindow = theRenderer->GetRenderWindow() ) {
402 myPointsActor->ReleaseGraphicsResources( aWindow );
403 mySurfaceActor->ReleaseGraphicsResources( aWindow );
404 myEdgeActor->ReleaseGraphicsResources( aWindow );
407 Superclass::RemoveFromRender(theRenderer);
410 //----------------------------------------------------------------------------
413 ::SetVisibility(int theMode)
415 Superclass::SetVisibility( theMode );
417 myPointsActor->SetVisibility( theMode );
420 myScalarBar->SetVisibility(myBarVisibility && theMode);
423 //----------------------------------------------------------------------------
428 return myBarVisibility;
431 //----------------------------------------------------------------------------
439 //----------------------------------------------------------------------------
442 ::SetBarVisibility(bool theMode)
444 myBarVisibility = theMode;
446 myScalarBar->SetVisibility(myBarVisibility);
450 //----------------------------------------------------------------------------
453 ::SetRepresentation(int theMode)
455 bool anIsShanding = IsShading();
457 Superclass::SetRepresentation(theMode);
459 if(theMode == SVTK::Representation::Surfaceframe)
460 mySurfaceActor->SetRepresentation(SVTK::Representation::Surface);
462 mySurfaceActor->SetRepresentation(theMode);
464 myPointsActor->SetProperty( mySurfaceActor->GetProperty() );
466 SetShading(anIsShanding);
470 //----------------------------------------------------------------------------
473 ::SetShading(bool theOn)
475 vtkProperty* aProperty = mySurfaceActor->GetProperty();
478 aProperty->SetAmbient(0.0);
479 aProperty->SetDiffuse(1.0);
481 aProperty->SetAmbient(1.0);
482 aProperty->SetDiffuse(0.0);
486 //----------------------------------------------------------------------------
491 vtkProperty* aProperty = mySurfaceActor->GetProperty();
493 return (fabs(aProperty->GetAmbient()) < EPS && fabs(aProperty->GetDiffuse() - 1.) < EPS);
498 ::RenderOpaqueGeometry(vtkViewport *ren)
500 GetMatrix(myEdgeActor->GetUserMatrix());
501 GetMatrix(mySurfaceActor->GetUserMatrix());
502 GetMatrix(myPointsActor->GetUserMatrix());
504 using namespace SVTK::Representation;
505 switch ( GetRepresentation() ) {
508 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
509 mySurfaceActor->RenderOpaqueGeometry(ren);
511 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
512 myEdgeActor->RenderOpaqueGeometry(ren);
516 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
517 myPointsActor->RenderOpaqueGeometry(ren);
521 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
522 mySurfaceActor->RenderOpaqueGeometry(ren);
530 ::RenderTranslucentGeometry(vtkViewport *ren)
532 GetMatrix(myEdgeActor->GetUserMatrix());
533 GetMatrix(mySurfaceActor->GetUserMatrix());
535 using namespace SVTK::Representation;
536 switch ( GetRepresentation() ) {
539 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
540 mySurfaceActor->RenderTranslucentGeometry(ren);
542 myEdgeActor->SetAllocatedRenderTime(this->AllocatedRenderTime/2.0,ren);
543 myEdgeActor->RenderTranslucentGeometry(ren);
547 myPointsActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
548 myPointsActor->RenderTranslucentGeometry(ren);
552 mySurfaceActor->SetAllocatedRenderTime(this->AllocatedRenderTime,ren);
553 mySurfaceActor->RenderTranslucentGeometry(ren);
559 //----------------------------------------------------------------------------
564 unsigned long int aSize = Superclass::GetMemorySize();
566 aSize += myPointsActor->GetMemorySize();
571 //----------------------------------------------------------------------------