Skip to content

Commit

Permalink
Python: Add support for manipulation of camera position
Browse files Browse the repository at this point in the history
* Remove debug output in the views function
* Add scripting support for Mat4d
* Add scripting support to camera
* Separate updates for background color and font size
  • Loading branch information
magnesj authored Feb 17, 2025
1 parent bf57911 commit cf2ae4f
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 23 deletions.
2 changes: 1 addition & 1 deletion ApplicationLibCode/Application/RiaGuiApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ void RiaGuiApplication::applyGuiPreferences( const RiaPreferences*
( applySettingsToAllViews || rim3dView->backgroundColor() == oldPreferences->defaultViewerBackgroundColor() ) )
{
rim3dView->setBackgroundColor( m_preferences->defaultViewerBackgroundColor() );
rim3dView->applyBackgroundColorAndFontChanges();
rim3dView->applyFontChanges();
}

if ( oldPreferences && ( applySettingsToAllViews || rim3dView->scaleZ() == oldPreferences->defaultScaleFactorZ() ) )
Expand Down
65 changes: 54 additions & 11 deletions ApplicationLibCode/ProjectDataModel/Rim3dView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "cafFrameAnimationControl.h"
#include "cafPdmFieldScriptingCapability.h"
#include "cafPdmFieldScriptingCapabilityCvfColor3.h"
#include "cafPdmFieldScriptingCapabilityCvfVec3d.h"
#include "cafPdmUiComboBoxEditor.h"
#include "cvfCamera.h"
#include "cvfModelBasicList.h"
Expand Down Expand Up @@ -112,6 +113,17 @@ Rim3dView::Rim3dView()
CAF_PDM_InitField( &m_cameraPointOfInterest, "CameraPointOfInterest", cvf::Vec3d::ZERO, "" );
m_cameraPointOfInterest.uiCapability()->setUiHidden( true );

CAF_PDM_InitScriptableFieldWithScriptKeywordNoDefault( &m_cameraPositionProxy, "CameraPositionProxy", "CameraMatrix", "Camera Matrix" );
m_cameraPositionProxy.registerGetMethod( this, &Rim3dView::cameraPosition );
m_cameraPositionProxy.registerSetMethod( this, &Rim3dView::setCameraPosition );

CAF_PDM_InitScriptableFieldWithScriptKeywordNoDefault( &m_cameraPointOfInterestProxy,
"CameraPointOfInterestProxy",
"CameraPointOfInterest",
"Camera Point of Interest" );
m_cameraPointOfInterestProxy.registerGetMethod( this, &Rim3dView::cameraPointOfInterest );
m_cameraPointOfInterestProxy.registerSetMethod( this, &Rim3dView::setCameraPointOfInterest );

CAF_PDM_InitScriptableField( &isPerspectiveView, "PerspectiveProjection", true, "Perspective Projection" );

double defaultScaleFactor = preferences->defaultScaleFactorZ();
Expand Down Expand Up @@ -829,6 +841,8 @@ void Rim3dView::setupBeforeSave()
{
if ( m_viewer )
{
// The update of these fields is also done in cameraPosition() and cameraPointOfInterest(). When the
// project is saved to file, these functions are not used, so we need to update the fields here.
m_cameraPosition = m_viewer->mainCamera()->viewMatrix();
m_cameraPointOfInterest = m_viewer->pointOfInterest();
}
Expand Down Expand Up @@ -993,17 +1007,22 @@ void Rim3dView::fieldChangedByUi( const caf::PdmFieldHandle* changedField, const
m_viewer->update();
}
}
else if ( changedField == &m_backgroundColor || changedField == &m_fontSize )
else if ( changedField == &m_backgroundColor )
{
if ( changedField == &m_fontSize )
if ( viewer() != nullptr )
{
auto fontHolderChildren = descendantsOfType<caf::FontHolderInterface>();
for ( auto fontHolder : fontHolderChildren )
{
fontHolder->updateFonts();
}
viewer()->mainCamera()->viewport()->setClearColor( cvf::Color4f( backgroundColor() ) );
}
this->applyBackgroundColorAndFontChanges();
this->scheduleCreateDisplayModelAndRedraw();
}
else if ( changedField == &m_fontSize )
{
auto fontHolderChildren = descendantsOfType<caf::FontHolderInterface>();
for ( auto fontHolder : fontHolderChildren )
{
fontHolder->updateFonts();
}
this->applyFontChanges();
this->updateConnectedEditors();
}
else if ( changedField == &maximumFrameRate )
Expand Down Expand Up @@ -1035,6 +1054,13 @@ void Rim3dView::fieldChangedByUi( const caf::PdmFieldHandle* changedField, const
m_viewer->update();
}
}
else if ( changedField == &m_cameraPositionProxy || changedField == &m_cameraPointOfInterestProxy )
{
if ( m_viewer )
{
m_viewer->repaint();
}
}
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1342,11 +1368,10 @@ void Rim3dView::setShowGridBox( bool showGridBox )
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void Rim3dView::applyBackgroundColorAndFontChanges()
void Rim3dView::applyFontChanges()
{
if ( viewer() != nullptr )
{
viewer()->mainCamera()->viewport()->setClearColor( cvf::Color4f( backgroundColor() ) );
viewer()->updateFonts( fontSize() );
}
updateGridBoxData();
Expand All @@ -1370,7 +1395,7 @@ int Rim3dView::fontSize() const
//--------------------------------------------------------------------------------------------------
void Rim3dView::updateFonts()
{
applyBackgroundColorAndFontChanges();
applyFontChanges();
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1510,6 +1535,11 @@ QList<caf::PdmOptionItemInfo> Rim3dView::calculateValueOptions( const caf::PdmFi
//--------------------------------------------------------------------------------------------------
cvf::Mat4d Rim3dView::cameraPosition() const
{
if ( m_viewer && m_viewer->mainCamera() )
{
m_cameraPosition = m_viewer->mainCamera()->viewMatrix();
}

return m_cameraPosition();
}

Expand All @@ -1518,6 +1548,11 @@ cvf::Mat4d Rim3dView::cameraPosition() const
//--------------------------------------------------------------------------------------------------
cvf::Vec3d Rim3dView::cameraPointOfInterest() const
{
if ( m_viewer )
{
m_cameraPointOfInterest = m_viewer->pointOfInterest();
}

return m_cameraPointOfInterest();
}

Expand Down Expand Up @@ -1545,6 +1580,10 @@ QWidget* Rim3dView::viewWidget()
void Rim3dView::setCameraPosition( const cvf::Mat4d& cameraPosition )
{
m_cameraPosition = cameraPosition;
if ( m_viewer && m_viewer->mainCamera() )
{
m_viewer->mainCamera()->setViewMatrix( m_cameraPosition );
}
}

//--------------------------------------------------------------------------------------------------
Expand All @@ -1553,6 +1592,10 @@ void Rim3dView::setCameraPosition( const cvf::Mat4d& cameraPosition )
void Rim3dView::setCameraPointOfInterest( const cvf::Vec3d& cameraPointOfInterest )
{
m_cameraPointOfInterest = cameraPointOfInterest;
if ( m_viewer )
{
m_viewer->setPointOfInterest( m_cameraPointOfInterest );
}
}

//--------------------------------------------------------------------------------------------------
Expand Down
13 changes: 9 additions & 4 deletions ApplicationLibCode/ProjectDataModel/Rim3dView.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ class Rim3dView : public RimViewWindow, public RiuViewerToViewInterface, public

void setBackgroundColor( const cvf::Color3f& newBackgroundColor );
cvf::Color3f backgroundColor() const override; // Implementation of RiuViewerToViewInterface
void applyBackgroundColorAndFontChanges();

int fontSize() const override;
void updateFonts() override;
void applyFontChanges();

void disableLighting( bool disable );
bool isLightingDisabled() const;
Expand Down Expand Up @@ -281,7 +281,6 @@ class Rim3dView : public RimViewWindow, public RiuViewerToViewInterface, public
cvf::ref<cvf::ModelBasicList> m_screenSpaceModel;

caf::PdmField<double> m_scaleZ;
caf::PdmField<double> m_customScaleZ;

caf::PdmChildField<RimAnnotationInViewCollection*> m_annotationCollection;

Expand Down Expand Up @@ -329,13 +328,19 @@ class Rim3dView : public RimViewWindow, public RiuViewerToViewInterface, public
caf::PdmField<int> m_id;
caf::PdmChildField<RimViewNameConfig*> m_nameConfig;
caf::PdmField<bool> m_disableLighting;
caf::PdmField<cvf::Mat4d> m_cameraPosition;
caf::PdmField<cvf::Vec3d> m_cameraPointOfInterest;
caf::PdmField<cvf::Color3f> m_backgroundColor;
caf::PdmField<bool> m_showGridBox;
caf::PdmField<bool> m_showZScaleLabel;
caf::PdmPtrField<Rim3dView*> m_comparisonView;

// Camera position and point of interest. The member variables are mutable to allow for setting them from const methods.
// The camera position and point of interest can change rapidly as the user interacts with the 3D view. Only update the Pdm field values
// when the application requests the camera position or point of interest.
mutable caf::PdmField<cvf::Mat4d> m_cameraPosition;
mutable caf::PdmField<cvf::Vec3d> m_cameraPointOfInterest;
caf::PdmProxyValueField<cvf::Vec3d> m_cameraPointOfInterestProxy;
caf::PdmProxyValueField<cvf::Mat4d> m_cameraPositionProxy;

caf::PdmField<bool> m_useCustomAnnotationStrategy;
caf::PdmField<caf::AppEnum<RivAnnotationTools::LabelPositionStrategy>> m_annotationStrategy;
caf::PdmField<int> m_annotationCountHint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,53 @@ void PdmFieldScriptingCapabilityIOHandler<cvf::Vector3<double>>::readFromField(

PdmFieldScriptingCapabilityIOHandler<std::vector<double>>::readFromField( fieldVectorValue, outputStream, quoteStrings );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmFieldScriptingCapabilityIOHandler<cvf::Matrix4<double>>::writeToField( cvf::Matrix4<double>& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted )
{
std::vector<double> fieldVectorValue;
PdmFieldScriptingCapabilityIOHandler<std::vector<double>>::writeToField( fieldVectorValue,
inputStream,
errorMessageContainer,
stringsAreQuoted );
if ( fieldVectorValue.size() == 16u )
{
for ( int row = 0; row < 4; ++row )
{
for ( int col = 0; col < 4; ++col )
{
fieldValue( row, col ) = fieldVectorValue[row * 4 + col];
}
}
}
else
{
QString errMsg = QString( "Expected 16 values, got %1" ).arg( fieldVectorValue.size() );
errorMessageContainer->addError( errMsg );
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmFieldScriptingCapabilityIOHandler<cvf::Matrix4<double>>::readFromField( const cvf::Matrix4<double>& fieldValue,
QTextStream& outputStream,
bool quoteStrings,
bool quoteNonBuiltin )
{
std::vector<double> fieldVectorValue( 16u );
for ( int row = 0; row < 4; ++row )
{
for ( int col = 0; col < 4; ++col )
{
fieldVectorValue[row * 4 + col] = fieldValue( row, col );
}
}

PdmFieldScriptingCapabilityIOHandler<std::vector<double>>::readFromField( fieldVectorValue, outputStream, quoteStrings );
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "cafPdmFieldScriptingCapability.h"

#include "cvfMatrix4.h"
#include "cvfVector3.h"

namespace caf
Expand All @@ -53,4 +54,18 @@ struct PdmFieldScriptingCapabilityIOHandler<cvf::Vector3<double>>
bool quoteStrings = true,
bool quoteNonBuiltins = false );
};

template <>
struct PdmFieldScriptingCapabilityIOHandler<cvf::Matrix4<double>>
{
static void writeToField( cvf::Matrix4<double>& fieldValue,
QTextStream& inputStream,
PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true );
static void readFromField( const cvf::Matrix4<double>& fieldValue,
QTextStream& outputStream,
bool quoteStrings = true,
bool quoteNonBuiltins = false );
};

} // namespace caf
1 change: 1 addition & 0 deletions Fwk/AppFwk/cafPdmScripting/cafPdmPythonGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ QString PdmPythonGenerator::dataTypeString( const PdmFieldHandle* field, bool us
#ifndef CAF_EXCLUDE_CVF
builtins[QString::fromStdString( typeid( cvf::Vec3d ).name() )] = "List[float]";
builtins[QString::fromStdString( typeid( cvf::Color3f ).name() )] = "str";
builtins[QString::fromStdString( typeid( cvf::Mat4d ).name() )] = "List[float]";
#endif

bool foundBuiltin = false;
Expand Down
1 change: 0 additions & 1 deletion GrpcInterface/Python/rips/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ def views(self):
views = project.views()
views_for_case = []
for view_object in views:
view_object.print_object_info()
if view_object.id == self.id:
views_for_case.append(view_object)
return views_for_case
Expand Down
7 changes: 1 addition & 6 deletions GrpcInterface/RiaGrpcPdmObjectService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "RiaGrpcCallbacks.h"
#include "RiaGrpcHelper.h"

#include "Rim3dView.h"
#include "RimEclipseResultDefinition.h"
#include "RimProject.h"

Expand All @@ -31,6 +30,7 @@
#include "cafPdmObjectMethod.h"
#include "cafPdmObjectScriptingCapability.h"
#include "cafPdmObjectScriptingCapabilityRegister.h"
#include "cafPdmProxyValueField.h"

using namespace rips;

Expand Down Expand Up @@ -448,11 +448,6 @@ grpc::Status RiaGrpcPdmObjectService::UpdateExistingPdmObject( grpc::ServerConte
matchingObject->updateAllRequiredEditors();
RimProject::current()->scheduleCreateDisplayModelAndRedrawAllViews();

Rim3dView* view = dynamic_cast<Rim3dView*>( matchingObject );
if ( view )
{
view->applyBackgroundColorAndFontChanges();
}
return grpc::Status::OK;
}
return grpc::Status( grpc::NOT_FOUND, "PdmObject not found" );
Expand Down

0 comments on commit cf2ae4f

Please sign in to comment.