From b4849eae8ea1a7992ba44bc5efb3cacf302de3aa Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 12 Jun 2024 17:44:18 +0200 Subject: [PATCH] added Timer module to py bindings --- src/core/scripting/VRPyMath.cpp | 8 ++++++++ src/core/scripting/VRPyMath.h | 5 +++++ src/core/scripting/VRSceneGlobals.cpp | 5 +++++ src/core/scripting/VRSceneGlobals.h | 1 + src/core/scripting/VRSceneModules.cpp | 1 + src/core/utils/VRTimer.cpp | 1 + src/core/utils/VRTimer.h | 4 ++++ src/core/utils/VRUtilsFwd.h | 3 +-- src/core/utils/system/VRSystem.cpp | 2 +- 9 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/core/scripting/VRPyMath.cpp b/src/core/scripting/VRPyMath.cpp index d1a07f3ff..5c8c9e9a9 100644 --- a/src/core/scripting/VRPyMath.cpp +++ b/src/core/scripting/VRPyMath.cpp @@ -592,6 +592,7 @@ PyObject* VRPyLine::intersect(VRPyLine* self, PyObject *args) { // expression bindings +simpleVRPyType(Timer, New_ptr); simplePyType(Expression, New_named_ptr); simplePyType(MathExpression, New_named_ptr); simplePyType(TSDF, VRPyTSDF::New ); @@ -758,6 +759,13 @@ PyMethodDef VRPyDatarow::methods[] = { {NULL} /* Sentinel */ }; +PyMethodDef VRPyTimer::methods[] = { + {"start", PyWrap( Timer, start, "start timer", void ) }, + {"stop", PyWrap( Timer, stop, "stop timer", double ) }, + {"reset", PyWrap( Timer, reset, "reset timer", void ) }, + {NULL} /* Sentinel */ +}; + PyMethodDef VRPyExpression::methods[] = { {"set", PyWrap2( Expression, set, "Set expression", void, string ) }, {"segment", PyWrap2( Expression, segment, "Build tree structure", void ) }, diff --git a/src/core/scripting/VRPyMath.h b/src/core/scripting/VRPyMath.h index 881231e82..40d6cc17c 100644 --- a/src/core/scripting/VRPyMath.h +++ b/src/core/scripting/VRPyMath.h @@ -15,6 +15,7 @@ #include "core/math/OSGMathFwd.h" #include "core/utils/xml.h" #include "core/utils/VRSpreadsheet.h" +#include "core/utils/VRTimer.h" #include #include @@ -113,6 +114,10 @@ struct VRPyLine : VRPyBaseT { static PyObject* pos(VRPyLine* self); static PyObject* dir(VRPyLine* self); }; + +struct VRPyTimer : VRPyBaseT { + static PyMethodDef methods[]; +}; struct VRPyExpression : VRPyBaseT { static PyMethodDef methods[]; diff --git a/src/core/scripting/VRSceneGlobals.cpp b/src/core/scripting/VRSceneGlobals.cpp index fa9ce5e67..483910976 100644 --- a/src/core/scripting/VRSceneGlobals.cpp +++ b/src/core/scripting/VRSceneGlobals.cpp @@ -83,6 +83,7 @@ PyMethodDef VRSceneGlobals::methods[] = { {"getSky", (PyCFunction)VRSceneGlobals::getSky, METH_NOARGS, "Get sky module" }, {"getSoundManager", (PyCFunction)VRSceneGlobals::getSoundManager, METH_NOARGS, "Get sound manager module" }, {"getFrame", (PyCFunction)VRSceneGlobals::getFrame, METH_NOARGS, "Get current frame number" }, + {"getFPS", (PyCFunction)VRSceneGlobals::getFPS, METH_NOARGS, "Get current fps" }, {"getScript", (PyCFunction)VRSceneGlobals::getScript, METH_VARARGS, "Get python script by name" }, {"importScene", (PyCFunction)VRSceneGlobals::importScene, METH_VARARGS, "Import scene" }, {"getActiveCamera", (PyCFunction)VRSceneGlobals::getActiveCamera, METH_NOARGS, "Get active camera" }, @@ -124,6 +125,10 @@ PyObject* VRSceneGlobals::getFrame(VRSceneGlobals* self) { return PyInt_FromLong(VRGlobals::CURRENT_FRAME); } +PyObject* VRSceneGlobals::getFPS(VRSceneGlobals* self) { + return PyFloat_FromDouble(VRGlobals::FRAME_RATE.fps); +} + PyObject* VRSceneGlobals::getActiveCamera(VRSceneGlobals* self) { return VRPyTypeCaster::cast( VRScene::getCurrent()->getActiveCamera() ); } diff --git a/src/core/scripting/VRSceneGlobals.h b/src/core/scripting/VRSceneGlobals.h index eb8f73d8c..b8fd69307 100644 --- a/src/core/scripting/VRSceneGlobals.h +++ b/src/core/scripting/VRSceneGlobals.h @@ -43,6 +43,7 @@ class VRSceneGlobals: public VRPyBase { static PyObject* getSky(VRSceneGlobals* self); static PyObject* getSoundManager(VRSceneGlobals* self); static PyObject* getFrame(VRSceneGlobals* self); + static PyObject* getFPS(VRSceneGlobals* self); static PyObject* getScript(VRSceneGlobals* self, PyObject *args); static PyObject* importScene(VRSceneGlobals* self, PyObject *args); static PyObject* getActiveCamera(VRSceneGlobals* self); diff --git a/src/core/scripting/VRSceneModules.cpp b/src/core/scripting/VRSceneModules.cpp index 434459b6c..f174a0e1a 100644 --- a/src/core/scripting/VRSceneModules.cpp +++ b/src/core/scripting/VRSceneModules.cpp @@ -342,6 +342,7 @@ void VRSceneModules::setup(VRScriptManager* sm, PyObject* pModVR) { #ifndef WITHOUT_LAPACKE_BLAS sm->registerModule("PCA", pModVR, 0, "Math"); #endif + sm->registerModule("Timer", pModVR, 0, "Math"); sm->registerModule("PID", pModVR, 0, "Math"); sm->registerModule("Patch", pModVR, 0, "Math"); diff --git a/src/core/utils/VRTimer.cpp b/src/core/utils/VRTimer.cpp index 258a86262..78b3b7ea5 100644 --- a/src/core/utils/VRTimer.cpp +++ b/src/core/utils/VRTimer.cpp @@ -7,6 +7,7 @@ #endif using namespace std; +using namespace OSG; VRTimer::VRTimer() { start(); } VRTimerPtr VRTimer::create() { return VRTimerPtr(new VRTimer()); } diff --git a/src/core/utils/VRTimer.h b/src/core/utils/VRTimer.h index 29289de7e..c23a14331 100644 --- a/src/core/utils/VRTimer.h +++ b/src/core/utils/VRTimer.h @@ -5,6 +5,8 @@ #include #include "core/utils/VRUtilsFwd.h" +namespace OSG { + class VRTimer { private: struct timer { @@ -32,4 +34,6 @@ class VRTimer { static double getBeacon(std::string); }; +} + #endif // VRTIMER_H_INCLUDED diff --git a/src/core/utils/VRUtilsFwd.h b/src/core/utils/VRUtilsFwd.h index 43ddd5d8b..e7d38fd4a 100644 --- a/src/core/utils/VRUtilsFwd.h +++ b/src/core/utils/VRUtilsFwd.h @@ -11,9 +11,8 @@ ptrTemplateFwd(VRCallbackWrapper, VRCallbackStrWrapper, std::string); ptrTemplateFwd(VRCallbackWrapper, VRCallbackPyWrapper, PyObject*); -ptrFwd(VRTimer); - namespace OSG { + ptrFwd(VRTimer); ptrFwd(VRVisualLayer); ptrFwd(VRProgress); ptrFwd(VREncryption); diff --git a/src/core/utils/system/VRSystem.cpp b/src/core/utils/system/VRSystem.cpp index da4e418de..c3366a0be 100644 --- a/src/core/utils/system/VRSystem.cpp +++ b/src/core/utils/system/VRSystem.cpp @@ -316,7 +316,7 @@ void doFrameSleep(double tFrame, double fps) { // efficient sleep double precisionBuffer = 0;//1.5; if (sT-precisionBuffer > 0) { - VRTimer timer; + OSG::VRTimer timer; timer.start(); duration T((sT-precisionBuffer)*1000); std::this_thread::sleep_for(T);