From 1f78804c9649718e1ed8b2c58eb1b2046e1ea6e9 Mon Sep 17 00:00:00 2001 From: rnv Date: Wed, 22 Jul 2015 17:55:09 +0300 Subject: [PATCH] Add methods to set/get camera parameters from python. --- src/SALOME_SWIG/SALOMEGUI_Swig.cxx | 189 +++++++++++++++++++++++++++++ src/SALOME_SWIG/SALOMEGUI_Swig.hxx | 6 + src/SALOME_SWIG/SALOMEGUI_Swig.i | 7 ++ 3 files changed, 202 insertions(+) diff --git a/src/SALOME_SWIG/SALOMEGUI_Swig.cxx b/src/SALOME_SWIG/SALOMEGUI_Swig.cxx index 6a2d24926..f5255da9a 100644 --- a/src/SALOME_SWIG/SALOMEGUI_Swig.cxx +++ b/src/SALOME_SWIG/SALOMEGUI_Swig.cxx @@ -52,6 +52,10 @@ #ifndef DISABLE_VTKVIEWER #include #include + #include + + #include + #include #endif #ifndef DISABLE_PLOT2DVIEWER #include @@ -858,3 +862,188 @@ void SALOMEGUI_Swig::ViewBack() { setView( __ViewBack ); } + +/* + \fn bool SALOMEGUI_Swig::getViewParameters() + \brief Get camera parameters of the active view. + + NOTE: For the current moment implemented for VTK viewer only. + + \return \c string with the view parameters +*/ + +class TGetViewParameters: public SALOME_Event +{ +public: + typedef QString TResult; + TResult myResult; + TGetViewParameters() : myResult( "" ) {} + virtual void Execute() { + if ( LightApp_Application* anApp = getApplication() ) { + if ( SUIT_ViewWindow* window = anApp->desktop()->activeWindow() ) { +#ifndef DISABLE_VTKVIEWER + if ( SVTK_ViewWindow* svtk = dynamic_cast( window ) ) { + if ( vtkRenderer* ren = svtk->getRenderer()) { + if ( vtkCamera* camera = ren->GetActiveCamera() ) { + double pos[3], focalPnt[3], viewUp[3], scale[3], parScale; + + // save position, focal point, viewUp, scale + camera->GetPosition( pos ); + camera->GetFocalPoint( focalPnt ); + camera->GetViewUp( viewUp ); + parScale = camera->GetParallelScale(); + svtk->GetRenderer()->GetScale( scale ); + + myResult += QString("sg.setCameraPosition( %1, %2, %3 )\n").arg(pos[0]).arg(pos[1]).arg(pos[2]); + myResult += QString("sg.setCameraFocalPoint( %1, %2, %3 )\n").arg(focalPnt[0]).arg(focalPnt[1]).arg(focalPnt[2]); + myResult += QString("sg.setCameraViewUp( %1, %2, %3 )\n").arg(viewUp[0]).arg(viewUp[1]).arg(viewUp[2]); + myResult += QString("sg.setViewScale(%1, %2, %3, %4 )\n").arg(parScale).arg(scale[0]).arg(scale[1]).arg(scale[2]); + } + } + } +#endif + } + } + } +}; + +const char* SALOMEGUI_Swig::getViewParameters() { + QString result = ProcessEvent( new TGetViewParameters() ); + return result.isEmpty() ? 0 : strdup( result.toLatin1().constData() ); +} + + +/*! + \brief View parameter type. + \internal +*/ +enum { + __CameraPosition, //!< position of the active camera + __CameraFocalPoint, //!< focal point of the active camera + __CameraViewUp, //!< view up of the active camera + __ViewScale //!< scale of the view +}; + + +/*! + \brief Change the camera parameters of the current view window. + \internal + + NOTE: For the current moment implemented for VTK viewer only. + + \param parameter type of the parameter + \param values value of the parameter +*/ +static void setViewParameter( int parameter, QList& values ) { + class TEvent: public SALOME_Event { + private: + int myParameter; + QList myValues; + public: + TEvent( int parameter , QList& values ) : myParameter(parameter), myValues( values ) {} + + virtual void Execute() { + if ( LightApp_Application* anApp = getApplication() ) { + if ( SUIT_ViewWindow* window = anApp->desktop()->activeWindow() ) { + if ( SVTK_ViewWindow* svtk = dynamic_cast( window ) ) { + if ( vtkRenderer* ren = svtk->getRenderer()) { + if ( vtkCamera* camera = ren->GetActiveCamera() ) { + switch(myParameter) { + case __CameraPosition : { + if ( myValues.size() == 3 ) { + camera->SetPosition( myValues[0], myValues[1], myValues[2] ); + } + break; + } + case __CameraFocalPoint : { + if ( myValues.size() == 3 ) { + camera->SetFocalPoint( myValues[0], myValues[1], myValues[2] ); + } + break; + } + case __CameraViewUp : { + if ( myValues.size() == 3 ) { + camera->SetViewUp( myValues[0], myValues[1], myValues[2] ); + } + break; + } + case __ViewScale : { + if ( myValues.size() == 4 ) { + camera->SetParallelScale( myValues[0] ); + double scale[] = { myValues[1], myValues[2], myValues[3] }; + svtk->GetRenderer()->SetScale( scale ); + } + break; + } + default: break; + } + } + } + svtk->Repaint(); + } + } + } + } + }; + ProcessVoidEvent( new TEvent( parameter, values ) ); +} + +/*! + \brief Set camera position of the active view . + \param x - X coordinate of the camera + \param y - Y coordinate of the camera + \param z - Z coordinate of the camera +*/ +void SALOMEGUI_Swig::setCameraPosition( double x, double y, double z ) { + QList lst; + lst.push_back( x ); + lst.push_back( y ); + lst.push_back( z ); + setViewParameter( __CameraPosition, lst ); +} + +/*! + \brief Set camera focal point of the active view. + \param x - X coordinate of the focal point + \param y - Y coordinate of the focal point + \param z - Z coordinate of the focal point +*/ +void SALOMEGUI_Swig::setCameraFocalPoint( double x, double y, double z ) { + QList lst; + lst.push_back( x ); + lst.push_back( y ); + lst.push_back( z ); + setViewParameter( __CameraFocalPoint, lst ); +} + +/*! + \brief Set the view up direction for the camera. + \param x - X component of the direction vector + \param y - Y component of the direction vector + \param z - Z component of the direction vector +*/ +void SALOMEGUI_Swig::setCameraViewUp( double x, double y, double z ) { + QList lst; + lst.push_back( x ); + lst.push_back( y ); + lst.push_back( z ); + setViewParameter( __CameraViewUp, lst ); +} + +/*! + \brief Set view scale. + \param parallelScale - scaling used for a parallel projection. + \param x - X scale + \param y - Y scale + \param z - Z scale +*/ +void SALOMEGUI_Swig::setViewScale( double parallelScale, double x, double y, double z ) { + QList lst; + lst.push_back( parallelScale ); + lst.push_back( x ); + lst.push_back( y ); + lst.push_back( z ); + setViewParameter( __ViewScale, lst ); +} + + diff --git a/src/SALOME_SWIG/SALOMEGUI_Swig.hxx b/src/SALOME_SWIG/SALOMEGUI_Swig.hxx index ab4cc342d..b6aa66b30 100644 --- a/src/SALOME_SWIG/SALOMEGUI_Swig.hxx +++ b/src/SALOME_SWIG/SALOMEGUI_Swig.hxx @@ -65,6 +65,12 @@ public: void ViewRight(); void ViewFront(); void ViewBack(); + + const char* getViewParameters(); + void setCameraPosition( double x, double y, double z ); + void setCameraFocalPoint( double x, double y, double z ); + void setCameraViewUp( double x, double y, double z ); + void setViewScale( double parallelScale,double x, double y, double z ); }; #endif // SALOMEGUI_SWIG_HXX diff --git a/src/SALOME_SWIG/SALOMEGUI_Swig.i b/src/SALOME_SWIG/SALOMEGUI_Swig.i index 7c4ff4400..49e883ace 100644 --- a/src/SALOME_SWIG/SALOMEGUI_Swig.i +++ b/src/SALOME_SWIG/SALOMEGUI_Swig.i @@ -92,4 +92,11 @@ public: void ViewRight(); void ViewFront(); void ViewBack(); + + /* view parameters */ + const char* getViewParameters(); + void setCameraPosition( double x, double y, double z ); + void setCameraFocalPoint( double x, double y, double z ); + void setCameraViewUp( double x, double y, double z ); + void setViewScale( double parallelScale,double x, double y, double z ); }; -- 2.39.2