Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--Attributes/Managers Maintenance : Separate Monolithic ObjectAttributes; Remove unnecessary abstract method; Build JSON From String #2177

Merged
merged 7 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/esp/assets/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ namespace Mn = Magnum;

namespace esp {

using metadata::attributes::AbstractObjectAttributes;
using metadata::attributes::CubePrimitiveAttributes;
using metadata::attributes::ObjectAttributes;
using metadata::attributes::ObjectInstanceShaderType;
Expand Down
2 changes: 2 additions & 0 deletions src/esp/bindings/AttributesBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#include <Magnum/PythonBindings.h>

#include "esp/core/managedContainers/AbstractManagedObject.h"
#include "esp/metadata/attributes/AbstractObjectAttributes.h"
#include "esp/metadata/attributes/AttributesBase.h"
#include "esp/metadata/attributes/LightLayoutAttributes.h"
#include "esp/metadata/attributes/ObjectAttributes.h"
#include "esp/metadata/attributes/PbrShaderAttributes.h"
#include "esp/metadata/attributes/PhysicsManagerAttributes.h"
#include "esp/metadata/attributes/PrimitiveAssetAttributes.h"
#include "esp/metadata/attributes/SceneInstanceAttributes.h"
#include "esp/metadata/attributes/StageAttributes.h"

namespace py = pybind11;
using py::literals::operator""_a;
Expand Down
2 changes: 2 additions & 0 deletions src/esp/bindings/AttributesManagersBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#include <Magnum/Magnum.h>
#include <Magnum/PythonBindings.h>

#include "esp/metadata/attributes/AbstractObjectAttributes.h"
#include "esp/metadata/attributes/LightLayoutAttributes.h"
#include "esp/metadata/attributes/ObjectAttributes.h"
#include "esp/metadata/attributes/StageAttributes.h"

#include "esp/metadata/managers/AssetAttributesManager.h"
#include "esp/metadata/managers/AttributesManagerBase.h"
Expand Down
103 changes: 96 additions & 7 deletions src/esp/core/managedContainers/ManagedFileBasedContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ class ManagedFileBasedContainer : public ManagedContainer<T, Access> {
return this->postCreateRegister(std::move(attr), registerObject);
} // ManagedFileBasedContainer::createObjectFromJSONFile

/**
* @brief Creates an instance of a managed object from a JSON string.
*
* @param docName name of potential document to load
* @param docString string of data expected to be in the expected format.
* Assumes this is a valid JSON string, and fails if it is not.
* @param registerObject whether to add this managed object to the
* library. If the user is going to edit this managed object, this should be
* false - any subsequent editing will require re-registration. Defaults to
* true.
* @return a reference to the desired managed object, or nullptr if fails.
*/
ManagedFileIOPtr createObjectFromJSONString(const std::string& docName,
const std::string& docString,
bool registerObject = true) {
std::unique_ptr<io::JsonDocument> docConfig{};
bool success = this->verifyParseDocString(docName, docString, docConfig);
if (!success) {
ESP_ERROR(Mn::Debug::Flag::NoSpace)
<< "<" << this->objectType_ << "> : Failure parsing string named `"
<< docName << "` into a JSON object, so unable to create object.";
return nullptr;
}
// convert doc to const value
const io::JsonGenericValue config = docConfig->GetObject();
ManagedFileIOPtr attr = this->buildManagedObjectFromDoc(docName, config);
return this->postCreateRegister(std::move(attr), registerObject);
} // ManagedFileBasedContainer::createObjectFromJSONString

/**
* @brief Method to load a Managed Object's data from a file. If the file
* type is not supported by specialization of this method, this method
Expand Down Expand Up @@ -321,15 +350,15 @@ class ManagedFileBasedContainer : public ManagedContainer<T, Access> {
}

/**
* @brief Verify passd @p filename is legal document of type T. Returns
* @brief Verify passd @p filename is legal document of type U. Returns
* loaded document in passed argument if successful. This requires
* appropriate specialization for each type name, so if this method is
* executed it means no appropriate specialization exists for passed type of
* document.
* appropriate specialization for each type name, so if this speciric method
jturner65 marked this conversation as resolved.
Show resolved Hide resolved
* is executed it means no appropriate specialization exists for passed type
* of document.
*
* @tparam type of document
* @tparam type of document to load
* @param filename name of potential document to load
* @param resDoc a reference to the document to be parsed.
* @param resDoc a reference to the document to be loaded into.
* @return whether document has been loaded successfully or not
*/
template <class U>
Expand All @@ -341,9 +370,10 @@ class ManagedFileBasedContainer : public ManagedContainer<T, Access> {
<< "` failed due to unsupported file type `" << typeid(U).name() << "`";
return false;
} // ManagedContainerBase::verifyLoadDocument

/**
* @brief Verify passed @p filename is legal json document, return loaded
* document or nullptr if fails
* document or nullptr if fails.
*
* @param filename name of potential json document to load
* @param jsonDoc a reference to the json document to be parsed
Expand All @@ -352,6 +382,47 @@ class ManagedFileBasedContainer : public ManagedContainer<T, Access> {
bool verifyLoadDocument(const std::string& filename,
std::unique_ptr<io::JsonDocument>& jsonDoc);

/**
* @brief Verify passd @p docString represents a legal document of type U.
* Returns parsed document in passed argument @p resDoc if successful. This
* requires appropriate specialization for each type name/type of destination
* document, so if this specific method is executed it means no appropriate
* specialization exists for passed type U of document.
*
* @tparam type of document to parse into
* @param docName name of potential document to load
* @param docString string of data expected to be in the expected format of
* documents of type U
* @param resDoc a reference to the document the @docString should be parsed
* into.
* @return whether document has been parsed successfully or not
*/
template <class U>
bool verifyParseDocString(const std::string& docName,
const std::string& docString,
CORRADE_UNUSED std::unique_ptr<U>& resDoc) {
// by here always fail - means document type U is unsupported
ESP_ERROR(Mn::Debug::Flag::NoSpace)
<< "<" << this->objectType_ << "> : Parse string named `" << docName
<< "` failed due to unsupported parse destination document type `"
<< typeid(U).name() << "`";
return false;
} // ManagedContainerBase::verifyParseDocString

/**
* @brief Verify passed @p docString , referenced by @p docName is a valid
* json document, return parsed document or nullptr if fails.
*
* @param docName name of potential json document to parse from @p docString
* @param docString string of JSON to attempt to parse
* @param jsonDoc a reference to the json document to be parsed
* @return whether document has been loaded successfully or not
*
*/
bool verifyParseDocString(const std::string& docName,
const std::string& docString,
std::unique_ptr<io::JsonDocument>& jsonDoc);

/**
* @brief Will build a new file name for @p filename by replacing the
* existing extension(s) with the passed @p fileTypeExt, if it is missing.
Expand Down Expand Up @@ -482,6 +553,24 @@ bool ManagedFileBasedContainer<T, Access>::verifyLoadDocument(
}
} // ManagedFileBasedContainer<T, Access>::verifyLoadDocument

template <class T, ManagedObjectAccess Access>
bool ManagedFileBasedContainer<T, Access>::verifyParseDocString(
const std::string& docName,
const std::string& docString,
std::unique_ptr<io::JsonDocument>& jsonDoc) {
try {
jsonDoc =
std::make_unique<io::JsonDocument>(io::parseJsonString(docString));
} catch (...) {
ESP_ERROR(Mn::Debug::Flag::NoSpace)
<< "<" << this->objectType_
<< "> : Failed to parse string referenced by `" << docName
<< "` into JSON.";
return false;
}
return true;
} // ManagedFileBasedContainer<T, Access>::verifyParseDocString

template <class T, ManagedObjectAccess Access>
bool ManagedFileBasedContainer<T, Access>::saveManagedObjectToFile(
const ManagedFileIOPtr& managedObject,
Expand Down
6 changes: 5 additions & 1 deletion src/esp/metadata/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ set(
attributes/AttributesBase.cpp
attributes/AttributesEnumMaps.h
attributes/AttributesEnumMaps.cpp
attributes/AbstractObjectAttributes.h
attributes/AbstractObjectAttributes.cpp
attributes/LightLayoutAttributes.h
attributes/LightLayoutAttributes.cpp
attributes/ObjectAttributes.h
Expand All @@ -22,8 +24,10 @@ set(
attributes/SceneInstanceAttributes.cpp
attributes/SceneDatasetAttributes.h
attributes/SceneDatasetAttributes.cpp
attributes/StageAttributes.h
attributes/StageAttributes.cpp
managers/AttributesManagerBase.h
managers/AbstractObjectAttributesManagerBase.h
managers/AbstractObjectAttributesManager.h
managers/AssetAttributesManager.h
managers/AssetAttributesManager.cpp
managers/LightLayoutAttributesManager.h
Expand Down
2 changes: 1 addition & 1 deletion src/esp/metadata/MetadataUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <Corrade/Utility/String.h>

#include "esp/io/Io.h"
#include "esp/metadata/attributes/ObjectAttributes.h"
#include "esp/metadata/attributes/AbstractObjectAttributes.h"

namespace esp {
namespace metadata {
Expand Down
84 changes: 84 additions & 0 deletions src/esp/metadata/attributes/AbstractObjectAttributes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 "AbstractObjectAttributes.h"
namespace esp {
namespace metadata {
namespace attributes {

AbstractObjectAttributes::AbstractObjectAttributes(
const std::string& attributesClassKey,
const std::string& handle)
: AbstractAttributes(attributesClassKey, handle) {
setFrictionCoefficient(0.5);
setRollingFrictionCoefficient(0.0);
setSpinningFrictionCoefficient(0.0);
setRestitutionCoefficient(0.1);
setScale({1.0, 1.0, 1.0});
setCollisionAssetSize({1.0, 1.0, 1.0});
setMargin(0.04);
setOrientUp({0, 1, 0});
setOrientFront({0, 0, -1});
setUseFrameForAllOrientation(true);
// default rendering and collisions will be mesh for physics objects and
// scenes. Primitive-based objects do not currently support mesh collisions,
// however, due to issues with how non-triangle meshes (i.e. wireframes) are
// handled in @ref GenericMeshData::setMeshData
setRenderAssetIsPrimitive(false);
setCollisionAssetIsPrimitive(false);
setUseMeshCollision(true);
setIsCollidable(true);
setIsVisible(true);
setUnitsToMeters(1.0);
setRenderAssetHandle("");
setCollisionAssetHandle("");
} // AbstractObjectAttributes ctor

std::string AbstractObjectAttributes::getObjectInfoHeaderInternal() const {
return "Render Asset Handle,Collision Asset Handle,Scale,Margin,Up XYZ,"
"Front XYZ,Units to M,Friction Coefficient,Restitution "
"Coefficient,Current Shader Type," +
getAbstractObjectInfoHeaderInternal();
}

std::string AbstractObjectAttributes::getObjectInfoInternal() const {
return Cr::Utility::formatString(
"{},{},{},{},{},{},{},{},{},{},{}", getRenderAssetHandle(),
getCollisionAssetHandle(), getAsString("scale"), getAsString("margin"),
getAsString("orient_up"), getAsString("orient_front"),
getAsString("units_to_meters"), getAsString("friction_coefficient"),
getAsString("rolling_friction_coefficient"),
getAsString("spinning_friction_coefficient"),
getAsString("restitution_coefficient"),
getShaderTypeName(getShaderType()), getAbstractObjectInfoInternal());
} // AbstractObjectAttributes::getObjectInfoInternal

void AbstractObjectAttributes::writeValuesToJson(
io::JsonGenericValue& jsonObj,
io::JsonAllocator& allocator) const {
// write AbstractObjectAttributes values to json
writeValueToJson("scale", jsonObj, allocator);
writeValueToJson("margin", jsonObj, allocator);
writeValueToJson("is_collidable", jsonObj, allocator);
writeValueToJson("orient_up", "up", jsonObj, allocator);
writeValueToJson("orient_front", "front", jsonObj, allocator);
writeValueToJson("units_to_meters", jsonObj, allocator);
writeValueToJson("is_visible", jsonObj, allocator);
writeValueToJson("friction_coefficient", jsonObj, allocator);
writeValueToJson("rolling_friction_coefficient", jsonObj, allocator);
writeValueToJson("spinning_friction_coefficient", jsonObj, allocator);
writeValueToJson("restitution_coefficient", jsonObj, allocator);
writeValueToJson("render_asset", jsonObj, allocator);
writeValueToJson("collision_asset", jsonObj, allocator);
writeValueToJson("collision_asset_size", jsonObj, allocator);
writeValueToJson("shader_type", jsonObj, allocator);
writeValueToJson("force_flat_shading", jsonObj, allocator);

// call instance-specific
writeValuesToJsonInternal(jsonObj, allocator);
} // AbstractObjectAttributes::writeValuesToJson

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