From de8a09650791bf3c0940a230f6762f37bdd5a5d5 Mon Sep 17 00:00:00 2001 From: John Turner <7strbass@gmail.com> Date: Fri, 13 Dec 2024 11:46:01 -0500 Subject: [PATCH] --set up base class and class templates for wrappers. --- src/esp/CMakeLists.txt | 3 +- src/esp/sensor/Sensor.h | 15 ++ .../sensor/sensorManagers/SensorBaseManager.h | 2 +- .../sensor/sensorManagers/SensorManager.cpp | 20 +++ src/esp/sensor/sensorManagers/SensorManager.h | 25 +++ .../sensorWrappers/ManagedAudioSensor.h | 57 +++++++ .../sensor/sensorWrappers/ManagedSensorBase.h | 90 +++++----- .../sensorWrappers/ManagedSensorTemplates.h | 157 ++++++++++++++++++ 8 files changed, 323 insertions(+), 46 deletions(-) create mode 100644 src/esp/sensor/sensorManagers/SensorManager.cpp create mode 100644 src/esp/sensor/sensorManagers/SensorManager.h create mode 100644 src/esp/sensor/sensorWrappers/ManagedAudioSensor.h create mode 100644 src/esp/sensor/sensorWrappers/ManagedSensorTemplates.h diff --git a/src/esp/CMakeLists.txt b/src/esp/CMakeLists.txt index 7b40df61b5..e84490c7ce 100644 --- a/src/esp/CMakeLists.txt +++ b/src/esp/CMakeLists.txt @@ -469,7 +469,8 @@ set( sensor/EquirectangularSensor.h sensor/FisheyeSensor.cpp sensor/FisheyeSensor.h - sensor/sensorManagers/SensorBaseManager.h + sensor/sensorManagers/SensorManager.cpp + sensor/sensorManagers/SensorManager.h sensor/sensorWrappers/ManagedSensorBase.h sensor/Sensor.cpp sensor/Sensor.h diff --git a/src/esp/sensor/Sensor.h b/src/esp/sensor/Sensor.h index 80d279b635..55aac0ea3d 100644 --- a/src/esp/sensor/Sensor.h +++ b/src/esp/sensor/Sensor.h @@ -120,6 +120,16 @@ class Sensor : public Magnum::SceneGraph::AbstractFeature3D { */ void setTransformationFromSpec(); + /** + * @brief The unique handle assigned to this sensor at creation + */ + std::string getSensorHandle() const { return sensorHandle_; } + + /** + * @brief The unique ID assigned to this sensor at creation + */ + int getSensorID() const { return sensorId_; } + /** * @brief Draws an observation to the frame buffer using simulator's renderer, * then reads the observation to the sensor's memory buffer @@ -150,6 +160,11 @@ class Sensor : public Magnum::SceneGraph::AbstractFeature3D { virtual bool displayObservation(sim::Simulator& sim) = 0; protected: + // unique handle for sensor built when sensor is constructed. + std::string sensorHandle_; + + // unique id for sensor set when sensor is built. + int sensorId_ = ID_UNDEFINED; SensorSpec::ptr spec_ = nullptr; core::Buffer::ptr buffer_ = nullptr; diff --git a/src/esp/sensor/sensorManagers/SensorBaseManager.h b/src/esp/sensor/sensorManagers/SensorBaseManager.h index 6c1b248776..7ac870f288 100644 --- a/src/esp/sensor/sensorManagers/SensorBaseManager.h +++ b/src/esp/sensor/sensorManagers/SensorBaseManager.h @@ -95,7 +95,7 @@ class SensorBaseManager managedSensorTypeConstructorMap_.find(sensorTypeName); if (mgdSensorTypeCtorMapIter == managedSensorTypeConstructorMap_.end()) { ESP_ERROR(Mn::Debug::Flag::NoSpace) - << "<" << this->sensorType_ << "> Unknown constructor type " + << "<" << this->objectType_ << "> Unknown constructor type " << sensorTypeName << ", so initNewObject aborted."; return nullptr; } diff --git a/src/esp/sensor/sensorManagers/SensorManager.cpp b/src/esp/sensor/sensorManagers/SensorManager.cpp new file mode 100644 index 0000000000..3360aaa8a8 --- /dev/null +++ b/src/esp/sensor/sensorManagers/SensorManager.cpp @@ -0,0 +1,20 @@ +// Copyright (c) Meta Platforms, Inc. and its affiliates. +// This source code is licensed under the MIT license found in the +// LICENSE file in the root directory of this source tree. + +#include "SensorManager.h" + +namespace esp { +namespace sensor { + +SensorManager::SensorManager() + : SensorBaseManager::SensorBaseManager( + "Sensor") { + this->copyConstructorMap_["ManagedAudioSensor"] = + &SensorManager::createObjCopyCtorMapEntry< + esp::sensor::ManagedAudioSensor>; + +} // SensorManager ctor + +} // namespace sensor +} // namespace esp diff --git a/src/esp/sensor/sensorManagers/SensorManager.h b/src/esp/sensor/sensorManagers/SensorManager.h new file mode 100644 index 0000000000..f884639a76 --- /dev/null +++ b/src/esp/sensor/sensorManagers/SensorManager.h @@ -0,0 +1,25 @@ +// Copyright (c) Meta Platforms, Inc. and its affiliates. +// This source code is licensed under the MIT license found in the +// LICENSE file in the root directory of this source tree. + +#ifndef ESP_SENSOR_SENSORMANAGER_H_ +#define ESP_SENSOR_SENSORMANAGER_H_ + +#include "SensorBaseManager.h" +#include "esp/sensor/sensorWrappers/ManagedAudioSensor.h" +#include "esp/sensor/sensorWrappers/ManagedSensorBase.h" + +namespace esp { +namespace sensor { + +class SensorManager : public SensorBaseManager { + public: + explicit SensorManager(); + + public: + ESP_SMART_POINTERS(SensorManager) +}; // class SensorManager + +} // namespace sensor +} // namespace esp +#endif // ESP_SENSOR_SENSORMANAGER_H_ diff --git a/src/esp/sensor/sensorWrappers/ManagedAudioSensor.h b/src/esp/sensor/sensorWrappers/ManagedAudioSensor.h new file mode 100644 index 0000000000..dc47c3a7d8 --- /dev/null +++ b/src/esp/sensor/sensorWrappers/ManagedAudioSensor.h @@ -0,0 +1,57 @@ +// Copyright (c) Meta Platforms, Inc. and its affiliates. +// This source code is licensed under the MIT license found in the +// LICENSE file in the root directory of this source tree. + +#ifndef ESP_SENSOR_MANAGEDAUDIOSENSOR_H_ +#define ESP_SENSOR_MANAGEDAUDIOSENSOR_H_ + +#include +#include + +#include "esp/sensor/AudioSensor.h" +#include "esp/sensor/sensorWrappers/ManagedSensorTemplates.h" + +namespace Cr = Corrade; +namespace Mn = Magnum; +namespace esp { +namespace sensor { + +/** + * @brief Class for wrapper for sensor objects of all kinds to + * enable Managed Container access. + */ +class ManagedAudioSensor + : public esp::sensor::AbstractManagedSensor { + public: + explicit ManagedAudioSensor() + : AbstractManagedSensor::AbstractManagedSensor( + "ManagedAudioSensor") {} + + // TODO Add appropriate audio sensor getters/setters here + + protected: + /** + * @brief Retrieve a comma-separated string holding the header values for + * the info returned for this managed object, type-specific. + */ + + std::string getSensorObjInfoHeaderInternal() const override { + return "Output Directory"; + } + /** + * @brief Specialization-specific extension of getObjectInfo, comma + * separated info ideal for saving to csv + */ + std::string getSensorObjInfoInternal( + CORRADE_UNUSED std::shared_ptr& sp) const override { + // TODO provide info stream for sensors + return ""; + } + + public: + ESP_SMART_POINTERS(ManagedAudioSensor) +}; // class ManagedAudioSensor +} // namespace sensor +} // namespace esp + +#endif // ESP_SENSOR_MANAGEDAUDIOSENSOR_H_ diff --git a/src/esp/sensor/sensorWrappers/ManagedSensorBase.h b/src/esp/sensor/sensorWrappers/ManagedSensorBase.h index c3f064397f..a5f13fa5d2 100644 --- a/src/esp/sensor/sensorWrappers/ManagedSensorBase.h +++ b/src/esp/sensor/sensorWrappers/ManagedSensorBase.h @@ -5,11 +5,12 @@ #ifndef ESP_SENSOR_MANAGEDSENSORBASE_H_ #define ESP_SENSOR_MANAGEDSENSORBASE_H_ -#include +#include #include #include "esp/core/managedContainers/AbstractManagedObject.h" #include "esp/sensor/Sensor.h" +#include "esp/sensor/VisualSensor.h" namespace Cr = Corrade; namespace Mn = Magnum; @@ -17,61 +18,60 @@ namespace esp { namespace sensor { /** - * @brief Base class template for wrapper for sensor objects of all kinds to - * enable Managed Container access. + * @brief Base class for wrappers of sensor objects of all kinds to enable + * Managed Container access. */ -template -class AbstractManagedSensor +class ManagedSensorBase : public esp::core::managedContainers::AbstractManagedObject { public: - static_assert(std::is_base_of::value, - "AbstractManagedSensor :: Managed sensor object type must be " - "derived from esp::sensor::Sensor"); - - typedef std::weak_ptr WeakObjRef; - - explicit AbstractManagedSensor(const std::string& classKey) { - AbstractManagedSensor::setClassKey(classKey); + explicit ManagedSensorBase(const std::string& classKey) { + ManagedSensorBase::setClassKey(classKey); } - - void setObjectRef(const std::shared_ptr& objRef) { weakObjRef_ = objRef; } - - ~AbstractManagedSensor() override = default; + ~ManagedSensorBase() override = default; + /** + * @brief Get the instancing class of the ManagedSensorBase instance. Should + * only be set from implementer's constructor. Used as key in constructor + * function pointer maps in @ref + * esp::core::managedContainers::ManagedContainer. + */ + std::string getClassKey() const override { return classKey_; } /** - * @brief Retrieve a comma-separated string holding the header values for the - * info returned for this managed object. + * @brief Managed Sensor objects manage their own handles, so this is + * currently unsettable. */ - std::string getObjectInfoHeader() const override { - return "Type," + getSensorObjInfoHeaderInternal(); - } + void setHandle(CORRADE_UNUSED const std::string& name) override {} /** - * @brief Retrieve a comma-separated informational string about the contents - * of this managed object. + * @brief Retrieve this Managed Sensor object's unique handle. */ - std::string getObjectInfo() const override { - if (auto sp = this->getObjectReference()) { - namespace CrUt = Cr::Utility; - return Cr::Utility::formatString("{},{},", classKey_, - getSensorObjInfoInternal(sp)); + std::string getHandle() const override { + if (auto sp = getObjectReferenceInternal()) { + return sp->getSensorHandle(); } - return Cr::Utility::formatString("Unknown classkey {},", classKey_); + return ""; } - protected: /** - * @brief Retrieve a comma-separated string holding the header values for - * the info returned for this managed object, type-specific. + * @brief Managed Sensor objects manage their own IDs, so this is + * unsettable. */ + void setID(CORRADE_UNUSED int ID) override {} - virtual std::string getSensorObjInfoHeaderInternal() const = 0; /** - * @brief Specialization-specific extension of getObjectInfo, comma - * separated info ideal for saving to csv + * @brief Retrieve this object's unique ID. */ - virtual std::string getSensorObjInfoInternal( - std::shared_ptr& sp) const = 0; + int getID() const override { + if (auto sp = getObjectReferenceInternal()) { + return sp->getSensorID(); + } + return ID_UNDEFINED; + } // getID() + + protected: + void setObjectRefInternal(const std::shared_ptr& objRef) { + weakObjRef_ = objRef; + } /** * @brief This function accesses the underlying shared pointer of this @@ -79,15 +79,16 @@ class AbstractManagedSensor * @return Either a shared pointer of this wrapper's object, or nullptr if * DNE. */ - std::shared_ptr inline getObjectReference() const { - std::shared_ptr sp = weakObjRef_.lock(); + template + std::shared_ptr inline getObjectReferenceInternal() const { + std::shared_ptr sp = weakObjRef_.lock(); if (!sp) { // TODO: Verify object is removed from manager here? ESP_ERROR() - << "This sensor object no longer exists. Please delete any variable " + << "This sensor object no longer exists. Please delete any variable " "references."; } - return sp; + return std::static_pointer_cast(sp); } // getObjectReference /** @@ -105,7 +106,7 @@ class AbstractManagedSensor * @brief Weak ref to object. If user has copy of this wrapper but object * has been deleted, this will be nullptr. */ - WeakObjRef weakObjRef_{}; + std::weak_ptr weakObjRef_{}; /** * @brief Name of instancing class responsible for this managed object @@ -113,8 +114,9 @@ class AbstractManagedSensor std::string classKey_; public: - ESP_SMART_POINTERS(AbstractManagedSensor) + ESP_SMART_POINTERS(ManagedSensorBase) }; // class AbstractManagedSensor + } // namespace sensor } // namespace esp diff --git a/src/esp/sensor/sensorWrappers/ManagedSensorTemplates.h b/src/esp/sensor/sensorWrappers/ManagedSensorTemplates.h new file mode 100644 index 0000000000..31e874c0de --- /dev/null +++ b/src/esp/sensor/sensorWrappers/ManagedSensorTemplates.h @@ -0,0 +1,157 @@ +// Copyright (c) Meta Platforms, Inc. and its affiliates. +// This source code is licensed under the MIT license found in the +// LICENSE file in the root directory of this source tree. + +#ifndef ESP_SENSOR_MANAGEDSENSORTEMPLATES_H_ +#define ESP_SENSOR_MANAGEDSENSORTEMPLATES_H_ + +#include +#include + +#include "ManagedSensorBase.h" +#include "esp/sensor/Sensor.h" +#include "esp/sensor/VisualSensor.h" + +namespace Cr = Corrade; +namespace Mn = Magnum; +namespace esp { +namespace sensor { + +/** + * @brief Base class template for wrapper for sensor objects of all kinds to + * enable Managed Container access. + */ +template +class AbstractManagedSensor : public ManagedSensorBase { + public: + static_assert(std::is_base_of::value, + "AbstractManagedSensor :: Managed sensor object type must be " + "derived from esp::sensor::Sensor"); + + typedef std::weak_ptr WeakObjRef; + + explicit AbstractManagedSensor(const std::string& classKey) + : ManagedSensorBase(classKey) {} + + void setObjectRef(const std::shared_ptr& objRef) { + setObjectRefInternal(objRef); + } + + /** + * @brief Return whether this is a visual sensor or not + */ + bool isVisualSensor() const { + if (auto sp = this->getObjectReference()) { + return sp->isVisualSensor(); + } + return false; + } + + /** + * @brief Retrieve a comma-separated string holding the header values for the + * info returned for this managed object. + */ + std::string getObjectInfoHeader() const override { + return "Type," + getSensorObjInfoHeaderInternal(); + } + + /** + * @brief Retrieve a comma-separated informational string about the contents + * of this managed object. + */ + std::string getObjectInfo() const override { + if (auto sp = this->getObjectReference()) { + return Cr::Utility::formatString("{},{},", classKey_, + getSensorObjInfoInternal(sp)); + } + return Cr::Utility::formatString("Unknown classkey {},", classKey_); + } + + protected: + /** + * @brief Retrieve a comma-separated string holding the header values for + * the info returned for this managed object, type-specific. + */ + + virtual std::string getSensorObjInfoHeaderInternal() const = 0; + /** + * @brief Specialization-specific extension of getObjectInfo, comma + * separated info ideal for saving to csv + */ + virtual std::string getSensorObjInfoInternal( + std::shared_ptr& sp) const = 0; + + /** + * @brief This function accesses the underlying shared pointer of this + * object's @p weakObjRef_ if it exists; if not, it provides a message. + * @return Either a shared pointer of this wrapper's object, or nullptr if + * DNE. + */ + std::shared_ptr inline getObjectReference() const { + return getObjectReferenceInternal(); + } // getObjectReference + + public: + ESP_SMART_POINTERS(AbstractManagedSensor) +}; // class AbstractManagedSensor + +/** + * @brief Base class template for wrapper for visual sensor objects of all kinds + * that extends AbstractManagedSensorAccess + */ + +template +class AbstractManagedVisualSensor + : public esp::sensor::AbstractManagedSensor { + public: + static_assert(std::is_base_of::value, + "AbstractManagedVisualSensor :: Managed visual sensor object " + "type must be derived from esp::sensor::VisualSensor"); + + explicit AbstractManagedVisualSensor(const std::string& classKey) + : AbstractManagedSensor(classKey) {} + // TODO Add appropriate abstract visual sensor getters/setters here + + protected: + /** + * @brief Retrieve a comma-separated string holding the header values for + * the info returned for this managed object, type-specific. + */ + + std::string getSensorObjInfoHeaderInternal() const override { + return "" + getVisualSensorObjInfoHeaderInternal(); + } + /** + * @brief Retrieve a comma-separated string holding the header values for + * the info returned for this managed visual sensor, type-specific. + */ + + virtual std::string getVisualSensorObjInfoHeaderInternal() const = 0; + /** + * @brief Specialization-specific extension of getObjectInfo, comma + * separated info ideal for saving to csv + */ + std::string getSensorObjInfoInternal( + CORRADE_UNUSED std::shared_ptr& sp) const override { + // TODO provide info stream for sensors + std::string res = Cr::Utility::formatString( + "{},{}", "VisualSensor", getVisualSensorObjInfoInternal()); + + return res; + } + + /** + * @brief Specialization-specific extension of getSensorObjInfoInternal, comma + * separated info ideal for saving to csv + */ + virtual std::string getVisualSensorObjInfoInternal( + std::shared_ptr& sp) const = 0; + + public: + ESP_SMART_POINTERS(AbstractManagedVisualSensor) +}; // class AbstractManagedVisualSensor + +} // namespace sensor +} // namespace esp + +#endif // ESP_SENSOR_MANAGEDSENSORTEMPLATES_H_