From 4990d1a63477e87a3d58e40663e0a09cfccc5621 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Sat, 15 Feb 2025 12:12:18 +0100 Subject: [PATCH] Python: Add support for camera manipulation in a view --- .../CMakeLists_files.cmake | 2 + .../ProjectDataModelCommands/Rimc3dView.cpp | 185 ++++++++++++++++++ .../ProjectDataModelCommands/Rimc3dView.h | 82 ++++++++ 3 files changed, 269 insertions(+) create mode 100644 ApplicationLibCode/ProjectDataModelCommands/Rimc3dView.cpp create mode 100644 ApplicationLibCode/ProjectDataModelCommands/Rimc3dView.h diff --git a/ApplicationLibCode/ProjectDataModelCommands/CMakeLists_files.cmake b/ApplicationLibCode/ProjectDataModelCommands/CMakeLists_files.cmake index 40c771e640..2ea1dcb643 100644 --- a/ApplicationLibCode/ProjectDataModelCommands/CMakeLists_files.cmake +++ b/ApplicationLibCode/ProjectDataModelCommands/CMakeLists_files.cmake @@ -27,6 +27,7 @@ set(SOURCE_GROUP_HEADER_FILES ${CMAKE_CURRENT_LIST_DIR}/RimcEclipseStatisticsCase.h ${CMAKE_CURRENT_LIST_DIR}/RimcIdenticalGridCaseGroup.h ${CMAKE_CURRENT_LIST_DIR}/RimcPressureTable.h + ${CMAKE_CURRENT_LIST_DIR}/Rimc3dView.h ) set(SOURCE_GROUP_SOURCE_FILES @@ -58,6 +59,7 @@ set(SOURCE_GROUP_SOURCE_FILES ${CMAKE_CURRENT_LIST_DIR}/RimcEclipseStatisticsCase.cpp ${CMAKE_CURRENT_LIST_DIR}/RimcIdenticalGridCaseGroup.cpp ${CMAKE_CURRENT_LIST_DIR}/RimcPressureTable.cpp + ${CMAKE_CURRENT_LIST_DIR}/Rimc3dView.cpp ) list(APPEND CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES}) diff --git a/ApplicationLibCode/ProjectDataModelCommands/Rimc3dView.cpp b/ApplicationLibCode/ProjectDataModelCommands/Rimc3dView.cpp new file mode 100644 index 0000000000..03bb7d93eb --- /dev/null +++ b/ApplicationLibCode/ProjectDataModelCommands/Rimc3dView.cpp @@ -0,0 +1,185 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2025 Equinor ASA +// +// ResInsight is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU General Public License at +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#include "Rimc3dView.h" + +#include "Rim3dView.h" + +#include "RiuViewer.h" + +#include "cvfCamera.h" + +#include "cafPdmFieldScriptingCapability.h" +#include "cafPdmObjectScriptingCapability.h" + +#include "cafPdmFieldScriptingCapabilityCvfVec3d.h" + +CAF_PDM_OBJECT_METHOD_SOURCE_INIT( Rim3dView, Rimc3dView_cameraVectors, "CameraVectors" ); + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +Rimc3dView_cameraVectors::Rimc3dView_cameraVectors( caf::PdmObjectHandle* self ) + : caf::PdmObjectMethod( self ) +{ + CAF_PDM_InitObject( "Get Camera Vectors for View", "", "", "Export a surface to file" ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +caf::PdmObjectHandle* Rimc3dView_cameraVectors::execute() +{ + auto dataObject = new RimcCameraVectors(); + + if ( auto view = self() ) + { + if ( view->viewer() && view->viewer()->mainCamera() ) + { + cvf::Vec3d eye = view->viewer()->mainCamera()->position(); + cvf::Vec3d up = view->viewer()->mainCamera()->up(); + + auto pointOfInterest = view->viewer()->pointOfInterest(); + + dataObject->setEye( eye ); + dataObject->setUp( up ); + dataObject->setReferencePoint( pointOfInterest ); + } + } + + return dataObject; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool Rimc3dView_cameraVectors::resultIsPersistent() const +{ + return false; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::unique_ptr Rimc3dView_cameraVectors::defaultResult() const +{ + return std::unique_ptr( new RimcCameraVectors() ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool Rimc3dView_cameraVectors::isNullptrValidResult() const +{ + return true; +} + +CAF_PDM_SOURCE_INIT( RimcCameraVectors, "RimcCameraVectors" ); +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RimcCameraVectors::RimcCameraVectors() +{ + CAF_PDM_InitScriptableObject( "Camera Vectors" ); + + CAF_PDM_InitScriptableFieldNoDefault( &m_eye, "Eye", "Eye" ); + CAF_PDM_InitScriptableFieldNoDefault( &m_up, "Up", "Up" ); + CAF_PDM_InitScriptableFieldNoDefault( &m_referencePoint, "ReferencePoint", "Reference point" ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimcCameraVectors::setEye( const cvf::Vec3d& eye ) +{ + m_eye = eye; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimcCameraVectors::setUp( const cvf::Vec3d& up ) +{ + m_up = up; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimcCameraVectors::setReferencePoint( const cvf::Vec3d& referencePoint ) +{ + m_referencePoint = referencePoint; +} + +CAF_PDM_OBJECT_METHOD_SOURCE_INIT( Rim3dView, Rimc3dView_setCameraVectors, "set_camera_vectors" ); + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +Rimc3dView_setCameraVectors::Rimc3dView_setCameraVectors( caf::PdmObjectHandle* self ) + : caf::PdmObjectMethod( self ) +{ + CAF_PDM_InitScriptableFieldNoDefault( &m_eye, "Eye", "Eye" ); + CAF_PDM_InitScriptableFieldNoDefault( &m_up, "Up", "Up" ); + CAF_PDM_InitScriptableFieldNoDefault( &m_referencePoint, "ReferencePoint", "Reference point" ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +caf::PdmObjectHandle* Rimc3dView_setCameraVectors::execute() +{ + if ( auto view = self() ) + { + if ( view->viewer() && view->viewer()->mainCamera() ) + { + view->viewer()->mainCamera()->setFromLookAt( m_eye(), m_referencePoint(), m_up() ); + + // Investigate if the following code is required + // view->viewer()->setPointOfInterest( m_referencePoint() ); + + view->updateDisplayModelForCurrentTimeStepAndRedraw(); + } + } + + return nullptr; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool Rimc3dView_setCameraVectors::resultIsPersistent() const +{ + return false; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::unique_ptr Rimc3dView_setCameraVectors::defaultResult() const +{ + return nullptr; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool Rimc3dView_setCameraVectors::isNullptrValidResult() const +{ + return true; +} diff --git a/ApplicationLibCode/ProjectDataModelCommands/Rimc3dView.h b/ApplicationLibCode/ProjectDataModelCommands/Rimc3dView.h new file mode 100644 index 0000000000..d190526691 --- /dev/null +++ b/ApplicationLibCode/ProjectDataModelCommands/Rimc3dView.h @@ -0,0 +1,82 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2025 Equinor ASA +// +// ResInsight is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU General Public License at +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include "cafPdmField.h" +#include "cafPdmObjectHandle.h" +#include "cafPdmObjectMethod.h" + +#include "cvfVector3.h" + +//================================================================================================== +/// +//================================================================================================== +class RimcCameraVectors : public caf::PdmObject +{ + CAF_PDM_HEADER_INIT; + +public: + RimcCameraVectors(); + + void setEye( const cvf::Vec3d& eye ); + void setUp( const cvf::Vec3d& up ); + void setReferencePoint( const cvf::Vec3d& referencePoint ); + +private: + caf::PdmField m_eye; + caf::PdmField m_up; + caf::PdmField m_referencePoint; +}; + +//================================================================================================== +/// +//================================================================================================== +class Rimc3dView_cameraVectors : public caf::PdmObjectMethod +{ + CAF_PDM_HEADER_INIT; + +public: + Rimc3dView_cameraVectors( caf::PdmObjectHandle* self ); + + caf::PdmObjectHandle* execute() override; + bool resultIsPersistent() const override; + std::unique_ptr defaultResult() const override; + bool isNullptrValidResult() const override; +}; + +//================================================================================================== +/// +//================================================================================================== +class Rimc3dView_setCameraVectors : public caf::PdmObjectMethod +{ + CAF_PDM_HEADER_INIT; + +public: + Rimc3dView_setCameraVectors( caf::PdmObjectHandle* self ); + + caf::PdmObjectHandle* execute() override; + bool resultIsPersistent() const override; + std::unique_ptr defaultResult() const override; + bool isNullptrValidResult() const override; + +private: + caf::PdmField m_eye; + caf::PdmField m_up; + caf::PdmField m_referencePoint; +};