From 93e144788238d6a2403ea48807fcd6fc6dd89ffd Mon Sep 17 00:00:00 2001 From: aclegg3 Date: Fri, 3 Nov 2023 12:01:27 -0700 Subject: [PATCH 1/2] expose ArticulatedObject creation_attributes --- src/esp/bindings/PhysicsObjectBindings.cpp | 5 +++++ src/esp/metadata/URDFParser.h | 11 ++++++++++ src/esp/physics/ArticulatedObject.h | 20 +++++++++++++++++++ .../bullet/BulletArticulatedObject.cpp | 1 + .../objectWrappers/ManagedArticulatedObject.h | 8 ++++++++ 5 files changed, 45 insertions(+) diff --git a/src/esp/bindings/PhysicsObjectBindings.cpp b/src/esp/bindings/PhysicsObjectBindings.cpp index 190aa58177..f3d3d7d29f 100644 --- a/src/esp/bindings/PhysicsObjectBindings.cpp +++ b/src/esp/bindings/PhysicsObjectBindings.cpp @@ -357,6 +357,11 @@ void declareArticulatedObjectWrapper(py::module& m, AbstractManagedPhysicsObject, std::shared_ptr>(m, classStrPrefix.c_str()) + .def_property_readonly( + "creation_attributes", + &ManagedArticulatedObject::getInitializationAttributes, + ("Get a copy of the attributes used to create this " + objType + ".") + .c_str()) .def_property_readonly( "global_scale", &ManagedArticulatedObject::getGlobalScale, R"(The uniform global scaling applied to this object during import.)") diff --git a/src/esp/metadata/URDFParser.h b/src/esp/metadata/URDFParser.h index 2a352c0c49..8e485155f6 100644 --- a/src/esp/metadata/URDFParser.h +++ b/src/esp/metadata/URDFParser.h @@ -418,6 +418,17 @@ class Model { return initializationAttributes_->getUserConfiguration(); } + /** + * @brief Get a copy of the template used to initialize this object. + * + * @return A copy of the @ref esp::metadata::attributes::ArticulatedObjectAttributes + * template used to create this object. + */ + std::shared_ptr + getInitializationAttributes() const { + return initializationAttributes_; + }; + protected: /** * @brief Json-based attributes defining characteristics of this model not diff --git a/src/esp/physics/ArticulatedObject.h b/src/esp/physics/ArticulatedObject.h index 95e5386cf3..c210e563bd 100644 --- a/src/esp/physics/ArticulatedObject.h +++ b/src/esp/physics/ArticulatedObject.h @@ -852,6 +852,20 @@ class ArticulatedObject : public esp::physics::PhysicsObjectBase { metadata::attributes::SceneAOInstanceAttributes>(); } + /** + * @brief Get a copy of the template used to initialize this object. + * + * @return A copy of the @ref esp::metadata::attributes::ArticulatedObjectAttributes + * template used to create this object. + */ + std::shared_ptr + getInitializationAttributes() const { + if (!initializationAttributes_) { + return nullptr; + } + return initializationAttributes_; + }; + protected: /** * @brief Used to synchronize simulator's notion of the object state @@ -884,6 +898,12 @@ class ArticulatedObject : public esp::physics::PhysicsObjectBase { //! Cache the global scaling from the source model. Set during import. float globalScale_ = 1.0; + /** + * @brief Saved attributes when the object was initialized. + */ + metadata::attributes::ArticulatedObjectAttributes::ptr + initializationAttributes_ = nullptr; + public: ESP_SMART_POINTERS(ArticulatedObject) }; diff --git a/src/esp/physics/bullet/BulletArticulatedObject.cpp b/src/esp/physics/bullet/BulletArticulatedObject.cpp index 01f130262d..c0c61a637a 100644 --- a/src/esp/physics/bullet/BulletArticulatedObject.cpp +++ b/src/esp/physics/bullet/BulletArticulatedObject.cpp @@ -193,6 +193,7 @@ void BulletArticulatedObject::initializeFromURDF( } // set user config attributes from model. setUserAttributes(urdfModel->getUserConfiguration()); + initializationAttributes_ = urdfModel->getInitializationAttributes(); // in case the base transform is not zero by default syncPose(); diff --git a/src/esp/physics/objectWrappers/ManagedArticulatedObject.h b/src/esp/physics/objectWrappers/ManagedArticulatedObject.h index 8b7aa309a6..ead84a7a8d 100644 --- a/src/esp/physics/objectWrappers/ManagedArticulatedObject.h +++ b/src/esp/physics/objectWrappers/ManagedArticulatedObject.h @@ -24,6 +24,14 @@ class ManagedArticulatedObject : AbstractManagedPhysicsObject( classKey) {} + std::shared_ptr + getInitializationAttributes() const { + if (auto sp = this->getObjectReference()) { + return sp->getInitializationAttributes(); + } + return nullptr; + } // getInitializationAttributes() + float getGlobalScale() const { if (auto sp = getObjectReference()) { return sp->getGlobalScale(); From 7930bf65db4751bb577a779487333b3a92c2b50c Mon Sep 17 00:00:00 2001 From: aclegg3 Date: Mon, 6 Nov 2023 08:44:05 -0800 Subject: [PATCH 2/2] refactor to treat initializationAttributes in a standard way --- src/esp/physics/ArticulatedObject.h | 12 ++-------- src/esp/physics/PhysicsObjectBase.h | 20 +++++++++++++++++ src/esp/physics/RigidBase.h | 23 +++----------------- src/esp/physics/RigidObject.h | 2 +- src/esp/physics/RigidStage.h | 3 ++- src/esp/physics/bullet/BulletRigidObject.cpp | 7 +++--- src/esp/physics/bullet/BulletRigidStage.cpp | 14 +++++++----- 7 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/esp/physics/ArticulatedObject.h b/src/esp/physics/ArticulatedObject.h index c210e563bd..43d44362a6 100644 --- a/src/esp/physics/ArticulatedObject.h +++ b/src/esp/physics/ArticulatedObject.h @@ -860,10 +860,8 @@ class ArticulatedObject : public esp::physics::PhysicsObjectBase { */ std::shared_ptr getInitializationAttributes() const { - if (!initializationAttributes_) { - return nullptr; - } - return initializationAttributes_; + return PhysicsObjectBase::getInitializationAttributes< + metadata::attributes::ArticulatedObjectAttributes>(); }; protected: @@ -898,12 +896,6 @@ class ArticulatedObject : public esp::physics::PhysicsObjectBase { //! Cache the global scaling from the source model. Set during import. float globalScale_ = 1.0; - /** - * @brief Saved attributes when the object was initialized. - */ - metadata::attributes::ArticulatedObjectAttributes::ptr - initializationAttributes_ = nullptr; - public: ESP_SMART_POINTERS(ArticulatedObject) }; diff --git a/src/esp/physics/PhysicsObjectBase.h b/src/esp/physics/PhysicsObjectBase.h index d2881e484d..94d12dcac2 100644 --- a/src/esp/physics/PhysicsObjectBase.h +++ b/src/esp/physics/PhysicsObjectBase.h @@ -86,6 +86,20 @@ class PhysicsObjectBase : public Magnum::SceneGraph::AbstractFeature3D { return static_cast( Magnum::SceneGraph::AbstractFeature3D::object()); } + + /** @brief Get a copy of the template used to initialize this object + * or scene. + * @return A copy of the initialization template used to create this object + * instance or nullptr if no template exists. + */ + template + std::shared_ptr getInitializationAttributes() const { + if (!initializationAttributes_) { + return nullptr; + } + return T::create(*(static_cast(initializationAttributes_.get()))); + } + /** * @brief Get the @ref MotionType of the object. See @ref * setMotionType. @@ -547,6 +561,12 @@ class PhysicsObjectBase : public Magnum::SceneGraph::AbstractFeature3D { */ core::config::Configuration::ptr userAttributes_ = nullptr; + /** + * @brief Saved attributes when the object was initialized. + */ + metadata::attributes::AbstractAttributes::ptr initializationAttributes_ = + nullptr; + private: /** * @brief This object's instancing attributes, if any were used during its diff --git a/src/esp/physics/RigidBase.h b/src/esp/physics/RigidBase.h index f7d3ce9c57..411395e121 100644 --- a/src/esp/physics/RigidBase.h +++ b/src/esp/physics/RigidBase.h @@ -302,19 +302,6 @@ class RigidBase : public esp::physics::PhysicsObjectBase { metadata::attributes::SceneObjectInstanceAttributes>(); } - /** @brief Get a copy of the template used to initialize this object - * or scene. - * @return A copy of the initialization template used to create this object - * instance or nullptr if no template exists. - */ - template - std::shared_ptr getInitializationAttributes() const { - if (!initializationAttributes_) { - return nullptr; - } - return T::create(*(static_cast(initializationAttributes_.get()))); - } - /** @brief Get the scalar linear damping coefficient of the object. Only used * for dervied dynamic implementations of @ref RigidObject. * @return The scalar linear damping coefficient of the object. @@ -378,7 +365,9 @@ class RigidBase : public esp::physics::PhysicsObjectBase { * @return The scaling for the object relative to its initially loaded meshes. */ virtual Magnum::Vector3 getScale() const { - return initializationAttributes_->getScale(); + return PhysicsObjectBase::getInitializationAttributes< + metadata::attributes::AbstractObjectAttributes>() + ->getScale(); } /** @@ -451,12 +440,6 @@ class RigidBase : public esp::physics::PhysicsObjectBase { */ bool isCollidable_ = false; - /** - * @brief Saved attributes when the object was initialized. - */ - metadata::attributes::AbstractObjectAttributes::ptr - initializationAttributes_ = nullptr; - public: ESP_SMART_POINTERS(RigidBase) }; // class RigidBase diff --git a/src/esp/physics/RigidObject.h b/src/esp/physics/RigidObject.h index ca348b6f89..613afb77ab 100644 --- a/src/esp/physics/RigidObject.h +++ b/src/esp/physics/RigidObject.h @@ -127,7 +127,7 @@ class RigidObject : public RigidBase { */ std::shared_ptr getInitializationAttributes() const { - return RigidBase::getInitializationAttributes< + return PhysicsObjectBase::getInitializationAttributes< metadata::attributes::ObjectAttributes>(); }; diff --git a/src/esp/physics/RigidStage.h b/src/esp/physics/RigidStage.h index 792cdf2836..1b8a52e6c6 100644 --- a/src/esp/physics/RigidStage.h +++ b/src/esp/physics/RigidStage.h @@ -47,9 +47,10 @@ class RigidStage : public RigidBase { */ std::shared_ptr getInitializationAttributes() const { - return RigidBase::getInitializationAttributes< + return PhysicsObjectBase::getInitializationAttributes< metadata::attributes::StageAttributes>(); }; + /** * @brief Finalize the creation of this @ref RigidStage * @return whether successful finalization. diff --git a/src/esp/physics/bullet/BulletRigidObject.cpp b/src/esp/physics/bullet/BulletRigidObject.cpp index 6cc07d7e1b..8e31e6ed17 100644 --- a/src/esp/physics/bullet/BulletRigidObject.cpp +++ b/src/esp/physics/bullet/BulletRigidObject.cpp @@ -94,11 +94,12 @@ bool BulletRigidObject::constructCollisionShape() { //! Iterate through all mesh components for one object //! The components are combined into a convex compound shape bObjectShape_ = std::make_unique(); + auto initAttr = PhysicsObjectBase::getInitializationAttributes< + metadata::attributes::ObjectAttributes>(); // collision mesh/asset handle - const std::string collisionAssetHandle = - initializationAttributes_->getCollisionAssetHandle(); + const std::string collisionAssetHandle = initAttr->getCollisionAssetHandle(); - if (!initializationAttributes_->getUseMeshCollision()) { + if (!initAttr->getUseMeshCollision()) { // if using prim collider get appropriate bullet collision primitive // attributes and build bullet collision shape auto primAttributes = diff --git a/src/esp/physics/bullet/BulletRigidStage.cpp b/src/esp/physics/bullet/BulletRigidStage.cpp index ce20d34986..ddd288b60e 100644 --- a/src/esp/physics/bullet/BulletRigidStage.cpp +++ b/src/esp/physics/bullet/BulletRigidStage.cpp @@ -67,9 +67,10 @@ void BulletRigidStage::setCollidable(bool collidable) { void BulletRigidStage::constructAndAddCollisionObjects() { if (bStaticCollisionObjects_.empty()) { + auto initAttr = PhysicsObjectBase::getInitializationAttributes< + metadata::attributes::StageAttributes>(); // construct the objects first time - const auto collisionAssetHandle = - initializationAttributes_->getCollisionAssetHandle(); + const auto collisionAssetHandle = initAttr->getCollisionAssetHandle(); const std::vector& meshGroup = resMgr_.getCollisionMesh(collisionAssetHandle); @@ -80,9 +81,8 @@ void BulletRigidStage::constructAndAddCollisionObjects() { constructBulletSceneFromMeshes(Magnum::Matrix4{}, meshGroup, metaData.root); for (auto& object : bStaticCollisionObjects_) { - object->setFriction(initializationAttributes_->getFrictionCoefficient()); - object->setRestitution( - initializationAttributes_->getRestitutionCoefficient()); + object->setFriction(initAttr->getFrictionCoefficient()); + object->setRestitution(initAttr->getRestitutionCoefficient()); collisionObjToObjIds_->emplace(object.get(), objectId_); } } @@ -141,7 +141,9 @@ void BulletRigidStage::constructBulletSceneFromMeshes( std::unique_ptr meshShape = std::make_unique(indexedVertexArray.get(), true); - meshShape->setMargin(initializationAttributes_->getMargin()); + auto initAttr = PhysicsObjectBase::getInitializationAttributes< + metadata::attributes::StageAttributes>(); + meshShape->setMargin(initAttr->getMargin()); meshShape->setLocalScaling( btVector3{transformFromLocalToWorld .scaling()}); // scale is a property of the shape