Skip to content

Commit f3089bc

Browse files
authored
--[BE] - SensorAttributes/Managers refactor Part 1 (#2502)
* --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.
1 parent 9d75557 commit f3089bc

29 files changed

+2457
-47
lines changed

src/esp/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,20 @@ set(
278278
metadata/attributes/AttributesEnumMaps.cpp
279279
metadata/attributes/AbstractObjectAttributes.h
280280
metadata/attributes/AbstractObjectAttributes.cpp
281+
metadata/attributes/AbstractSensorAttributes.h
282+
metadata/attributes/AbstractSensorAttributes.cpp
283+
metadata/attributes/AbstractVisualSensorAttributes.h
284+
metadata/attributes/AbstractVisualSensorAttributes.cpp
281285
metadata/attributes/ArticulatedObjectAttributes.h
282286
metadata/attributes/ArticulatedObjectAttributes.cpp
287+
metadata/attributes/AudioSensorAttributes.h
288+
metadata/attributes/AudioSensorAttributes.cpp
289+
metadata/attributes/CameraSensorAttributes.h
290+
metadata/attributes/CameraSensorAttributes.cpp
291+
metadata/attributes/CustomSensorAttributes.h
292+
metadata/attributes/CustomSensorAttributes.cpp
293+
metadata/attributes/CubeMapSensorAttributes.h
294+
metadata/attributes/CubeMapSensorAttributes.cpp
283295
metadata/attributes/LightLayoutAttributes.h
284296
metadata/attributes/LightLayoutAttributes.cpp
285297
metadata/attributes/MarkerSets.h
@@ -319,6 +331,8 @@ set(
319331
metadata/managers/SceneDatasetAttributesManager.cpp
320332
metadata/managers/SemanticAttributesManager.h
321333
metadata/managers/SemanticAttributesManager.cpp
334+
metadata/managers/SensorAttributesManager.h
335+
metadata/managers/SensorAttributesManager.cpp
322336
metadata/managers/StageAttributesManager.h
323337
metadata/managers/StageAttributesManager.cpp
324338
metadata/MetadataMediator.h

src/esp/bindings/SensorBindings.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ void initSensorBindings(py::module& m) {
5353
// TODO fill out other SensorTypes
5454
// ==== enum SensorType ====
5555
py::enum_<SensorType>(m, "SensorType")
56-
.value("NONE", SensorType::None)
56+
.value("NONE", SensorType::Unspecified)
57+
.value("CUSTOM", SensorType::Custom)
5758
.value("COLOR", SensorType::Color)
5859
.value("DEPTH", SensorType::Depth)
5960
.value("SEMANTIC", SensorType::Semantic)
6061
.value("AUDIO", SensorType::Audio);
6162

6263
py::enum_<SensorSubType>(m, "SensorSubType")
63-
.value("NONE", SensorSubType::None)
64+
.value("NONE", SensorSubType::Unspecified)
65+
.value("CUSTOM", SensorSubType::Custom)
6466
.value("PINHOLE", SensorSubType::Pinhole)
6567
.value("ORTHOGRAPHIC", SensorSubType::Orthographic)
6668
.value("FISHEYE", SensorSubType::Fisheye)
@@ -70,6 +72,7 @@ void initSensorBindings(py::module& m) {
7072
// NOTE : esp::sensor::SemanticSensorTarget is an alias for
7173
// esp::scene::SceneNodeSemanticDataIDX.
7274
py::enum_<SemanticSensorTarget>(m, "SemanticSensorTarget")
75+
.value("DRAWABLE_ID", SemanticSensorTarget::DrawableID)
7376
.value("SEMANTIC_ID", SemanticSensorTarget::SemanticID)
7477
.value("OBJECT_ID", SemanticSensorTarget::ObjectID);
7578

src/esp/metadata/MetadataMediator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ void MetadataMediator::buildAttributesManagers() {
1919

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

22+
sensorAttributesManager_ = managers::SensorAttributesManager::create();
23+
2224
sceneDatasetAttributesManager_ =
2325
managers::SceneDatasetAttributesManager::create(
2426
physicsAttributesManager_, pbrShaderAttributesManager_);

src/esp/metadata/MetadataMediator.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "esp/metadata/managers/PhysicsAttributesManager.h"
2020
#include "esp/metadata/managers/SceneDatasetAttributesManager.h"
2121
#include "esp/metadata/managers/SemanticAttributesManager.h"
22+
#include "esp/metadata/managers/SensorAttributesManager.h"
2223
#include "esp/metadata/managers/StageAttributesManager.h"
2324
#include "esp/sim/SimulatorConfiguration.h"
2425

@@ -198,6 +199,15 @@ class MetadataMediator {
198199
return pbrShaderAttributesManager_;
199200
} // getPbrShaderAttributesManager
200201

202+
/**
203+
* @brief Return manager for construction and access to Sensor attributes;
204+
* @return A shared pointer to the current @ref esp::metadata::managers::SensorAttributesManager.
205+
*/
206+
const managers::SensorAttributesManager::ptr& getSensorAttributesManager()
207+
const {
208+
return sensorAttributesManager_;
209+
} // getSensorAttributesManager
210+
201211
/**
202212
* @brief Return manager for construction and access to
203213
* @ref esp::attributes::SceneInstanceAttributes for current dataset.
@@ -683,6 +693,12 @@ class MetadataMediator {
683693
managers::PbrShaderAttributesManager::ptr pbrShaderAttributesManager_ =
684694
nullptr;
685695

696+
/**
697+
* @brief Manages all the SensorAttributes configuration settings, used to
698+
* build sensors, independent of loaded datasets.
699+
*/
700+
managers::SensorAttributesManager::ptr sensorAttributesManager_ = nullptr;
701+
686702
public:
687703
ESP_SMART_POINTERS(MetadataMediator)
688704
}; // namespace metadata
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) Meta Platforms, Inc. and its affiliates.
2+
// This source code is licensed under the MIT license found in the
3+
// LICENSE file in the root directory of this source tree.
4+
5+
#include "AbstractSensorAttributes.h"
6+
namespace esp {
7+
namespace metadata {
8+
namespace attributes {
9+
10+
AbstractSensorAttributes::AbstractSensorAttributes(
11+
const std::string& attributesClassKey,
12+
const std::string& handle)
13+
: AbstractAttributes(attributesClassKey, handle) {
14+
init("position", Mn::Vector3{0.0, 1.5, 0.0});
15+
init("orientation", Mn::Vector3{0.0, 0.0, 0.0});
16+
init("noise_model", "None");
17+
18+
initTranslated("sensor_type",
19+
getSensorTypeName(sensor::SensorType::Unspecified));
20+
initTranslated("sensor_subtype",
21+
getSensorSubTypeName(sensor::SensorSubType::Unspecified));
22+
} // AbstractSensorAttributes ctor
23+
24+
void AbstractSensorAttributes::populateWithSensorSpec(
25+
const sensor::SensorSpec::ptr& spec) {
26+
setPosition(spec->position);
27+
setOrientation(spec->orientation);
28+
setNoiseModel(spec->noiseModel);
29+
setSensorTypeEnum(spec->sensorType);
30+
setSensorSubTypeEnum(spec->sensorSubType);
31+
} // AbstractSensorAttributes::populateWithSensorSpec
32+
33+
void AbstractSensorAttributes::writeValuesToJson(
34+
io::JsonGenericValue& jsonObj,
35+
io::JsonAllocator& allocator) const {
36+
// write AbstractSensorAttributes to JSON
37+
writeValueToJson("position", jsonObj, allocator);
38+
writeValueToJson("orientation", jsonObj, allocator);
39+
writeValueToJson("noise_model", jsonObj, allocator);
40+
writeValueToJson("sensor_type", jsonObj, allocator);
41+
writeValueToJson("sensor_subtype", jsonObj, allocator);
42+
43+
// call child-class-specific
44+
writeValuesToJsonInternal(jsonObj, allocator);
45+
} // AbstractSensorAttributes::writeValuesToJson
46+
47+
std::string AbstractSensorAttributes::getObjectInfoHeaderInternal() const {
48+
return "Position XYZ,Orientation XYZ,Noise Model,Sensor "
49+
"Type,Sensor Subtype," +
50+
getAbstractSensorInfoHeaderInternal();
51+
} // AbstractSensorAttributes::getObjectInfoHeaderInternal
52+
53+
std::string AbstractSensorAttributes::getObjectInfoInternal() const {
54+
return Cr::Utility::formatString("{},{},{},{},{},{}", getAsString("position"),
55+
getAsString("orientation"), getNoiseModel(),
56+
getSensorTypeName(getSensorType()),
57+
getSensorSubTypeName(getSensorSubType()),
58+
getAbstractSensorInfoInternal());
59+
} // AbstractSensorAttributes::getObjectInfoInternal
60+
61+
} // namespace attributes
62+
} // namespace metadata
63+
} // namespace esp
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
// Copyright (c) Meta Platforms, Inc. and its affiliates.
2+
// This source code is licensed under the MIT license found in the
3+
// LICENSE file in the root directory of this source tree.
4+
5+
#ifndef ESP_METADATA_ATTRIBUTES_ABSTRACTSENSORATTRIBUTES_H_
6+
#define ESP_METADATA_ATTRIBUTES_ABSTRACTSENSORATTRIBUTES_H_
7+
8+
#include "AbstractAttributes.h"
9+
10+
namespace esp {
11+
namespace metadata {
12+
namespace attributes {
13+
14+
/**
15+
* @brief Attributes object holding the descriptions of a Sensor object
16+
*/
17+
class AbstractSensorAttributes : public AbstractAttributes {
18+
public:
19+
AbstractSensorAttributes(const std::string& classKey,
20+
const std::string& handle);
21+
22+
/**
23+
* @brief Populate this attributes from an appropriate @ref sensor::SensorSpec.
24+
* @todo Remove when SensorSpecs are removed
25+
*
26+
*/
27+
virtual void populateWithSensorSpec(const sensor::SensorSpec::ptr& spec);
28+
29+
/** @brief Set the position of the sensor. */
30+
void setPosition(const Magnum::Vector3& position) {
31+
set("position", position);
32+
}
33+
34+
/** @brief Get the position of the sensor. */
35+
Magnum::Vector3 getPosition() const {
36+
return get<Magnum::Vector3>("position");
37+
}
38+
39+
/** @brief Set the position of the sensor. */
40+
void setOrientation(const Magnum::Vector3& orientation) {
41+
set("orientation", orientation);
42+
}
43+
44+
/** @brief Get the position of the sensor. */
45+
Magnum::Vector3 getOrientation() const {
46+
return get<Magnum::Vector3>("orientation");
47+
}
48+
49+
/** @brief Sets the noise model to use for this sensor. */
50+
void setNoiseModel(const std::string& noise_model) {
51+
set("noise_model", noise_model);
52+
}
53+
54+
/** @brief Gets the noise model to use for this sensor. */
55+
std::string getNoiseModel() const { return get<std::string>("noise_model"); }
56+
57+
/**
58+
* @brief Set the sensor type for this sensor.
59+
*/
60+
void setSensorType(const std::string& sensorType) {
61+
// force to lowercase to check if present
62+
const std::string sensorTypeLC = Cr::Utility::String::lowercase(sensorType);
63+
auto mapIter = SensorTypeNamesMap.find(sensorTypeLC);
64+
if (mapIter == SensorTypeNamesMap.end()) {
65+
ESP_ERROR(Mn::Debug::Flag::NoSpace)
66+
<< "'" << sensorType
67+
<< "' is an illegal value for "
68+
"AbstractSensorAttributes::setSensorType, so current value "
69+
<< get<std::string>("sensor_type") << " not changed.";
70+
return;
71+
}
72+
setTranslated("sensor_type", sensorType);
73+
} // setSensorType
74+
75+
/**
76+
* @brief Set the sensor type for this sensor as specified by given @ref SensorType
77+
*/
78+
void setSensorTypeEnum(sensor::SensorType sensorTypeEnum) {
79+
// force to lowercase to check if present
80+
const std::string sensorType = getSensorTypeName(sensorTypeEnum);
81+
auto mapIter = SensorTypeNamesMap.find(sensorType);
82+
83+
ESP_CHECK(mapIter != SensorTypeNamesMap.end(),
84+
"Illegal SensorType enum value given"
85+
<< static_cast<int>(sensorTypeEnum) << ":" << sensorType
86+
<< "attempted to be initialized in AbstractSensorAttributes:"
87+
<< getHandle() << ". Aborting.");
88+
89+
setTranslated("sensor_type", sensorType);
90+
} // setSensorTypeEnum
91+
92+
/**
93+
* @brief Get the sensor type for this sensor.
94+
*/
95+
sensor::SensorType getSensorType() const {
96+
const std::string val =
97+
Cr::Utility::String::lowercase(get<std::string>("sensor_type"));
98+
auto mapIter = SensorTypeNamesMap.find(val);
99+
if (mapIter != SensorTypeNamesMap.end()) {
100+
return mapIter->second;
101+
}
102+
// This should never get to here. It would mean that this field was set
103+
// to an invalid value somehow.
104+
return sensor::SensorType::Unspecified;
105+
}
106+
107+
/**
108+
* @brief Set the sensor type for this sensor.
109+
*/
110+
void setSensorSubType(const std::string& sensorSubType) {
111+
// force to lowercase to check if present
112+
const std::string sensorTypeLC =
113+
Cr::Utility::String::lowercase(sensorSubType);
114+
auto mapIter = SensorSubTypeNamesMap.find(sensorTypeLC);
115+
if (mapIter == SensorSubTypeNamesMap.end()) {
116+
ESP_ERROR(Mn::Debug::Flag::NoSpace)
117+
<< "'" << sensorSubType
118+
<< "' is an illegal value for "
119+
"AbstractSensorAttributes::setSensorSubType, so current value "
120+
<< get<std::string>("sensor_subtype") << " not changed.";
121+
return;
122+
}
123+
setTranslated("sensor_subtype", sensorSubType);
124+
} // setSensorSubType
125+
126+
/**
127+
* @brief Set the sensor subtype for this sensor as specified by given @ref SensorSubType
128+
*/
129+
void setSensorSubTypeEnum(sensor::SensorSubType sensorSubTypeEnum) {
130+
// force to lowercase to check if present
131+
const std::string sensorSubType = getSensorSubTypeName(sensorSubTypeEnum);
132+
auto mapIter = SensorSubTypeNamesMap.find(sensorSubType);
133+
134+
ESP_CHECK(mapIter != SensorSubTypeNamesMap.end(),
135+
"Illegal SensorSubType enum value given"
136+
<< static_cast<int>(sensorSubTypeEnum) << ":" << sensorSubType
137+
<< "attempted to be initialized in AbstractSensorAttributes:"
138+
<< getHandle() << ". Aborting.");
139+
140+
setTranslated("sensor_subtype", sensorSubType);
141+
} // setSensorSubTypeEnum
142+
143+
/**
144+
* @brief Get the sensor subtype for this sensor.
145+
*/
146+
sensor::SensorSubType getSensorSubType() const {
147+
const std::string val =
148+
Cr::Utility::String::lowercase(get<std::string>("sensor_subtype"));
149+
auto mapIter = SensorSubTypeNamesMap.find(val);
150+
if (mapIter != SensorSubTypeNamesMap.end()) {
151+
return mapIter->second;
152+
}
153+
// This should never get to here. It would mean that this field was set
154+
// to an invalid value somehow.
155+
return sensor::SensorSubType::Unspecified;
156+
} // getSensorSubType
157+
158+
/**
159+
* @brief Populate a json object with all the first-level values held in this
160+
* configuration. Default is overridden to handle special cases for
161+
* AbstractSensorAttributes and deriving (i.e. AudioSensorAttributes or
162+
* VisualSensorAttributes) classes.
163+
*/
164+
void writeValuesToJson(io::JsonGenericValue& jsonObj,
165+
io::JsonAllocator& allocator) const override;
166+
167+
protected:
168+
/**
169+
* @brief Write child-class-specific values to json object
170+
*
171+
*/
172+
virtual void writeValuesToJsonInternal(
173+
CORRADE_UNUSED io::JsonGenericValue& jsonObj,
174+
CORRADE_UNUSED io::JsonAllocator& allocator) const {}
175+
176+
/**
177+
* @brief Retrieve a comma-separated string holding the header values for the
178+
* info returned for this managed object, type-specific.
179+
*/
180+
181+
std::string getObjectInfoHeaderInternal() const override;
182+
/**
183+
* @brief get AbstractSensorAttributes-specific info header
184+
*/
185+
virtual std::string getAbstractSensorInfoHeaderInternal() const {
186+
return "";
187+
};
188+
189+
/**
190+
* @brief Retrieve a comma-separated informational string about the contents
191+
* of this managed object.
192+
*/
193+
std::string getObjectInfoInternal() const override;
194+
/**
195+
* @brief get AbstractSensorAttributes specific info for csv string
196+
*/
197+
virtual std::string getAbstractSensorInfoInternal() const { return ""; };
198+
199+
public:
200+
ESP_SMART_POINTERS(AbstractSensorAttributes)
201+
}; // class AbstractSensorAttributes
202+
203+
} // namespace attributes
204+
} // namespace metadata
205+
} // namespace esp
206+
207+
#endif // ESP_METADATA_ATTRIBUTES_ABSTRACTSENSORATTRIBUTES_H_

0 commit comments

Comments
 (0)