Skip to content

Commit

Permalink
--[BE] - SensorAttributes/Managers refactor Part 1 (#2502)
Browse files Browse the repository at this point in the history
* --init
* --enum maps to convert sensor enums to/from strings
* --better organization; complete AbstractSensorAttr setup
* --add audio and abstract visual sensor stubs
* --add config support for Vector2i
* --Sensor enum maps; AbstractVisualSensorAttributes
* --complete audio sensor attributes
* --add CameraSensorAttributes
* --add CubeMap-based attributes
* --use correct class name
* --SensorAttributesManager initial commit.
* --remove reference to "unique_id" in the attributes. The handle will fulfill this role.
* --add attributes for Custom sensors
* --rename 'none' to 'unspecified' in SensorType and SensorSubType
* --type check for template function
* --expanded SensorAttributes creation code
* --add 'custom' entry for SensorType along with SensorSubType
* --add code to populate attributes from appropriate spec
This code will be removed when SensorSpecs are removed.
* --give sensorattributes unique handle/registration key.
* --verify works for both audio enabled and disabled.
* --expand sensor attributes manager functions
* --Define CustomSensorAttributes to be held within a subconfig similar to user_defined.
* --add bindings for custom sensor-related enums
* --add ref to SensorAttributesManager in MM
* --fix missing method, incorrect map name;
* --hide instantiation temporarily to stop test linking error.
* --appropriately mark unused arguments
* --remove unused include
* --some scene-lib-related refactors
* --create sensorAttributesManager in MM
* --add sensor attributes and manager filenames to cmakelist
* --minor - stub out sensor tests
* --test building all types of sensor attributes from sensor specs
* --set a 1 cm default fisheye focal length.
  • Loading branch information
jturner65 authored Dec 9, 2024
1 parent 9d75557 commit f3089bc
Show file tree
Hide file tree
Showing 29 changed files with 2,457 additions and 47 deletions.
14 changes: 14 additions & 0 deletions src/esp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,20 @@ set(
metadata/attributes/AttributesEnumMaps.cpp
metadata/attributes/AbstractObjectAttributes.h
metadata/attributes/AbstractObjectAttributes.cpp
metadata/attributes/AbstractSensorAttributes.h
metadata/attributes/AbstractSensorAttributes.cpp
metadata/attributes/AbstractVisualSensorAttributes.h
metadata/attributes/AbstractVisualSensorAttributes.cpp
metadata/attributes/ArticulatedObjectAttributes.h
metadata/attributes/ArticulatedObjectAttributes.cpp
metadata/attributes/AudioSensorAttributes.h
metadata/attributes/AudioSensorAttributes.cpp
metadata/attributes/CameraSensorAttributes.h
metadata/attributes/CameraSensorAttributes.cpp
metadata/attributes/CustomSensorAttributes.h
metadata/attributes/CustomSensorAttributes.cpp
metadata/attributes/CubeMapSensorAttributes.h
metadata/attributes/CubeMapSensorAttributes.cpp
metadata/attributes/LightLayoutAttributes.h
metadata/attributes/LightLayoutAttributes.cpp
metadata/attributes/MarkerSets.h
Expand Down Expand Up @@ -319,6 +331,8 @@ set(
metadata/managers/SceneDatasetAttributesManager.cpp
metadata/managers/SemanticAttributesManager.h
metadata/managers/SemanticAttributesManager.cpp
metadata/managers/SensorAttributesManager.h
metadata/managers/SensorAttributesManager.cpp
metadata/managers/StageAttributesManager.h
metadata/managers/StageAttributesManager.cpp
metadata/MetadataMediator.h
Expand Down
7 changes: 5 additions & 2 deletions src/esp/bindings/SensorBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ void initSensorBindings(py::module& m) {
// TODO fill out other SensorTypes
// ==== enum SensorType ====
py::enum_<SensorType>(m, "SensorType")
.value("NONE", SensorType::None)
.value("NONE", SensorType::Unspecified)
.value("CUSTOM", SensorType::Custom)
.value("COLOR", SensorType::Color)
.value("DEPTH", SensorType::Depth)
.value("SEMANTIC", SensorType::Semantic)
.value("AUDIO", SensorType::Audio);

py::enum_<SensorSubType>(m, "SensorSubType")
.value("NONE", SensorSubType::None)
.value("NONE", SensorSubType::Unspecified)
.value("CUSTOM", SensorSubType::Custom)
.value("PINHOLE", SensorSubType::Pinhole)
.value("ORTHOGRAPHIC", SensorSubType::Orthographic)
.value("FISHEYE", SensorSubType::Fisheye)
Expand All @@ -70,6 +72,7 @@ void initSensorBindings(py::module& m) {
// NOTE : esp::sensor::SemanticSensorTarget is an alias for
// esp::scene::SceneNodeSemanticDataIDX.
py::enum_<SemanticSensorTarget>(m, "SemanticSensorTarget")
.value("DRAWABLE_ID", SemanticSensorTarget::DrawableID)
.value("SEMANTIC_ID", SemanticSensorTarget::SemanticID)
.value("OBJECT_ID", SemanticSensorTarget::ObjectID);

Expand Down
2 changes: 2 additions & 0 deletions src/esp/metadata/MetadataMediator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ void MetadataMediator::buildAttributesManagers() {

pbrShaderAttributesManager_ = managers::PbrShaderAttributesManager::create();

sensorAttributesManager_ = managers::SensorAttributesManager::create();

sceneDatasetAttributesManager_ =
managers::SceneDatasetAttributesManager::create(
physicsAttributesManager_, pbrShaderAttributesManager_);
Expand Down
16 changes: 16 additions & 0 deletions src/esp/metadata/MetadataMediator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "esp/metadata/managers/PhysicsAttributesManager.h"
#include "esp/metadata/managers/SceneDatasetAttributesManager.h"
#include "esp/metadata/managers/SemanticAttributesManager.h"
#include "esp/metadata/managers/SensorAttributesManager.h"
#include "esp/metadata/managers/StageAttributesManager.h"
#include "esp/sim/SimulatorConfiguration.h"

Expand Down Expand Up @@ -198,6 +199,15 @@ class MetadataMediator {
return pbrShaderAttributesManager_;
} // getPbrShaderAttributesManager

/**
* @brief Return manager for construction and access to Sensor attributes;
* @return A shared pointer to the current @ref esp::metadata::managers::SensorAttributesManager.
*/
const managers::SensorAttributesManager::ptr& getSensorAttributesManager()
const {
return sensorAttributesManager_;
} // getSensorAttributesManager

/**
* @brief Return manager for construction and access to
* @ref esp::attributes::SceneInstanceAttributes for current dataset.
Expand Down Expand Up @@ -683,6 +693,12 @@ class MetadataMediator {
managers::PbrShaderAttributesManager::ptr pbrShaderAttributesManager_ =
nullptr;

/**
* @brief Manages all the SensorAttributes configuration settings, used to
* build sensors, independent of loaded datasets.
*/
managers::SensorAttributesManager::ptr sensorAttributesManager_ = nullptr;

public:
ESP_SMART_POINTERS(MetadataMediator)
}; // namespace metadata
Expand Down
63 changes: 63 additions & 0 deletions src/esp/metadata/attributes/AbstractSensorAttributes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 "AbstractSensorAttributes.h"
namespace esp {
namespace metadata {
namespace attributes {

AbstractSensorAttributes::AbstractSensorAttributes(
const std::string& attributesClassKey,
const std::string& handle)
: AbstractAttributes(attributesClassKey, handle) {
init("position", Mn::Vector3{0.0, 1.5, 0.0});
init("orientation", Mn::Vector3{0.0, 0.0, 0.0});
init("noise_model", "None");

initTranslated("sensor_type",
getSensorTypeName(sensor::SensorType::Unspecified));
initTranslated("sensor_subtype",
getSensorSubTypeName(sensor::SensorSubType::Unspecified));
} // AbstractSensorAttributes ctor

void AbstractSensorAttributes::populateWithSensorSpec(
const sensor::SensorSpec::ptr& spec) {
setPosition(spec->position);
setOrientation(spec->orientation);
setNoiseModel(spec->noiseModel);
setSensorTypeEnum(spec->sensorType);
setSensorSubTypeEnum(spec->sensorSubType);
} // AbstractSensorAttributes::populateWithSensorSpec

void AbstractSensorAttributes::writeValuesToJson(
io::JsonGenericValue& jsonObj,
io::JsonAllocator& allocator) const {
// write AbstractSensorAttributes to JSON
writeValueToJson("position", jsonObj, allocator);
writeValueToJson("orientation", jsonObj, allocator);
writeValueToJson("noise_model", jsonObj, allocator);
writeValueToJson("sensor_type", jsonObj, allocator);
writeValueToJson("sensor_subtype", jsonObj, allocator);

// call child-class-specific
writeValuesToJsonInternal(jsonObj, allocator);
} // AbstractSensorAttributes::writeValuesToJson

std::string AbstractSensorAttributes::getObjectInfoHeaderInternal() const {
return "Position XYZ,Orientation XYZ,Noise Model,Sensor "
"Type,Sensor Subtype," +
getAbstractSensorInfoHeaderInternal();
} // AbstractSensorAttributes::getObjectInfoHeaderInternal

std::string AbstractSensorAttributes::getObjectInfoInternal() const {
return Cr::Utility::formatString("{},{},{},{},{},{}", getAsString("position"),
getAsString("orientation"), getNoiseModel(),
getSensorTypeName(getSensorType()),
getSensorSubTypeName(getSensorSubType()),
getAbstractSensorInfoInternal());
} // AbstractSensorAttributes::getObjectInfoInternal

} // namespace attributes
} // namespace metadata
} // namespace esp
207 changes: 207 additions & 0 deletions src/esp/metadata/attributes/AbstractSensorAttributes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// 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_METADATA_ATTRIBUTES_ABSTRACTSENSORATTRIBUTES_H_
#define ESP_METADATA_ATTRIBUTES_ABSTRACTSENSORATTRIBUTES_H_

#include "AbstractAttributes.h"

namespace esp {
namespace metadata {
namespace attributes {

/**
* @brief Attributes object holding the descriptions of a Sensor object
*/
class AbstractSensorAttributes : public AbstractAttributes {
public:
AbstractSensorAttributes(const std::string& classKey,
const std::string& handle);

/**
* @brief Populate this attributes from an appropriate @ref sensor::SensorSpec.
* @todo Remove when SensorSpecs are removed
*
*/
virtual void populateWithSensorSpec(const sensor::SensorSpec::ptr& spec);

/** @brief Set the position of the sensor. */
void setPosition(const Magnum::Vector3& position) {
set("position", position);
}

/** @brief Get the position of the sensor. */
Magnum::Vector3 getPosition() const {
return get<Magnum::Vector3>("position");
}

/** @brief Set the position of the sensor. */
void setOrientation(const Magnum::Vector3& orientation) {
set("orientation", orientation);
}

/** @brief Get the position of the sensor. */
Magnum::Vector3 getOrientation() const {
return get<Magnum::Vector3>("orientation");
}

/** @brief Sets the noise model to use for this sensor. */
void setNoiseModel(const std::string& noise_model) {
set("noise_model", noise_model);
}

/** @brief Gets the noise model to use for this sensor. */
std::string getNoiseModel() const { return get<std::string>("noise_model"); }

/**
* @brief Set the sensor type for this sensor.
*/
void setSensorType(const std::string& sensorType) {
// force to lowercase to check if present
const std::string sensorTypeLC = Cr::Utility::String::lowercase(sensorType);
auto mapIter = SensorTypeNamesMap.find(sensorTypeLC);
if (mapIter == SensorTypeNamesMap.end()) {
ESP_ERROR(Mn::Debug::Flag::NoSpace)
<< "'" << sensorType
<< "' is an illegal value for "
"AbstractSensorAttributes::setSensorType, so current value "
<< get<std::string>("sensor_type") << " not changed.";
return;
}
setTranslated("sensor_type", sensorType);
} // setSensorType

/**
* @brief Set the sensor type for this sensor as specified by given @ref SensorType
*/
void setSensorTypeEnum(sensor::SensorType sensorTypeEnum) {
// force to lowercase to check if present
const std::string sensorType = getSensorTypeName(sensorTypeEnum);
auto mapIter = SensorTypeNamesMap.find(sensorType);

ESP_CHECK(mapIter != SensorTypeNamesMap.end(),
"Illegal SensorType enum value given"
<< static_cast<int>(sensorTypeEnum) << ":" << sensorType
<< "attempted to be initialized in AbstractSensorAttributes:"
<< getHandle() << ". Aborting.");

setTranslated("sensor_type", sensorType);
} // setSensorTypeEnum

/**
* @brief Get the sensor type for this sensor.
*/
sensor::SensorType getSensorType() const {
const std::string val =
Cr::Utility::String::lowercase(get<std::string>("sensor_type"));
auto mapIter = SensorTypeNamesMap.find(val);
if (mapIter != SensorTypeNamesMap.end()) {
return mapIter->second;
}
// This should never get to here. It would mean that this field was set
// to an invalid value somehow.
return sensor::SensorType::Unspecified;
}

/**
* @brief Set the sensor type for this sensor.
*/
void setSensorSubType(const std::string& sensorSubType) {
// force to lowercase to check if present
const std::string sensorTypeLC =
Cr::Utility::String::lowercase(sensorSubType);
auto mapIter = SensorSubTypeNamesMap.find(sensorTypeLC);
if (mapIter == SensorSubTypeNamesMap.end()) {
ESP_ERROR(Mn::Debug::Flag::NoSpace)
<< "'" << sensorSubType
<< "' is an illegal value for "
"AbstractSensorAttributes::setSensorSubType, so current value "
<< get<std::string>("sensor_subtype") << " not changed.";
return;
}
setTranslated("sensor_subtype", sensorSubType);
} // setSensorSubType

/**
* @brief Set the sensor subtype for this sensor as specified by given @ref SensorSubType
*/
void setSensorSubTypeEnum(sensor::SensorSubType sensorSubTypeEnum) {
// force to lowercase to check if present
const std::string sensorSubType = getSensorSubTypeName(sensorSubTypeEnum);
auto mapIter = SensorSubTypeNamesMap.find(sensorSubType);

ESP_CHECK(mapIter != SensorSubTypeNamesMap.end(),
"Illegal SensorSubType enum value given"
<< static_cast<int>(sensorSubTypeEnum) << ":" << sensorSubType
<< "attempted to be initialized in AbstractSensorAttributes:"
<< getHandle() << ". Aborting.");

setTranslated("sensor_subtype", sensorSubType);
} // setSensorSubTypeEnum

/**
* @brief Get the sensor subtype for this sensor.
*/
sensor::SensorSubType getSensorSubType() const {
const std::string val =
Cr::Utility::String::lowercase(get<std::string>("sensor_subtype"));
auto mapIter = SensorSubTypeNamesMap.find(val);
if (mapIter != SensorSubTypeNamesMap.end()) {
return mapIter->second;
}
// This should never get to here. It would mean that this field was set
// to an invalid value somehow.
return sensor::SensorSubType::Unspecified;
} // getSensorSubType

/**
* @brief Populate a json object with all the first-level values held in this
* configuration. Default is overridden to handle special cases for
* AbstractSensorAttributes and deriving (i.e. AudioSensorAttributes or
* VisualSensorAttributes) classes.
*/
void writeValuesToJson(io::JsonGenericValue& jsonObj,
io::JsonAllocator& allocator) const override;

protected:
/**
* @brief Write child-class-specific values to json object
*
*/
virtual void writeValuesToJsonInternal(
CORRADE_UNUSED io::JsonGenericValue& jsonObj,
CORRADE_UNUSED io::JsonAllocator& allocator) const {}

/**
* @brief Retrieve a comma-separated string holding the header values for the
* info returned for this managed object, type-specific.
*/

std::string getObjectInfoHeaderInternal() const override;
/**
* @brief get AbstractSensorAttributes-specific info header
*/
virtual std::string getAbstractSensorInfoHeaderInternal() const {
return "";
};

/**
* @brief Retrieve a comma-separated informational string about the contents
* of this managed object.
*/
std::string getObjectInfoInternal() const override;
/**
* @brief get AbstractSensorAttributes specific info for csv string
*/
virtual std::string getAbstractSensorInfoInternal() const { return ""; };

public:
ESP_SMART_POINTERS(AbstractSensorAttributes)
}; // class AbstractSensorAttributes

} // namespace attributes
} // namespace metadata
} // namespace esp

#endif // ESP_METADATA_ATTRIBUTES_ABSTRACTSENSORATTRIBUTES_H_
Loading

0 comments on commit f3089bc

Please sign in to comment.