-
Notifications
You must be signed in to change notification settings - Fork 290
Support Collada unit scaling in MeshShape #2152
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
70bfc74
Fix Collada unit scaling in MeshShape
jslee02 d424c1e
Fix test MeshShape path handling
jslee02 7327cd7
Merge remote-tracking branch 'origin/main' into pr-2152
jslee02 8f57474
Merge branch 'pr-2152' into fix/collada-unit-scaling
jslee02 d562c7a
Update RDT env
jslee02 8feecfa
Improve MeshShape Collada tests
jslee02 7247838
Format code
jslee02 675a1a8
Handle extensionless Collada URIs and restore fallback
jslee02 eba4e5a
Merge branch 'main' into fix/collada-unit-scaling
jslee02 71349f1
Fix AliasUriResourceRetriever target URIs
jslee02 eceab5a
Simplify Collada detection fallback
jslee02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| #include "dart/common/LocalResourceRetriever.hpp" | ||
| #include "dart/common/Uri.hpp" | ||
| #include "dart/config.hpp" | ||
| #include "dart/dynamics/AssimpInputResourceAdaptor.hpp" | ||
| #include "dart/dynamics/MeshShape.hpp" | ||
|
|
||
| #include <assimp/cimport.h> | ||
| #include <assimp/config.h> | ||
| #include <assimp/postprocess.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <fstream> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| using namespace dart; | ||
|
|
||
| namespace { | ||
|
|
||
| class AliasUriResourceRetriever final : public common::ResourceRetriever | ||
| { | ||
| public: | ||
| AliasUriResourceRetriever( | ||
| const std::string& aliasUri, const std::string& targetFilePath) | ||
| : mAliasUri(aliasUri), | ||
| mTargetUri(common::Uri::createFromPath(targetFilePath)), | ||
| mDelegate(std::make_shared<common::LocalResourceRetriever>()) | ||
| { | ||
| } | ||
|
|
||
| bool exists(const common::Uri& uri) override | ||
| { | ||
| if (isAlias(uri)) | ||
| return mDelegate->exists(mTargetUri); | ||
| return mDelegate->exists(uri); | ||
| } | ||
|
|
||
| common::ResourcePtr retrieve(const common::Uri& uri) override | ||
| { | ||
| if (isAlias(uri)) | ||
| return mDelegate->retrieve(mTargetUri); | ||
| return mDelegate->retrieve(uri); | ||
| } | ||
|
|
||
| std::string getFilePath(const common::Uri& uri) override | ||
| { | ||
| if (isAlias(uri)) | ||
| return mDelegate->getFilePath(mTargetUri); | ||
| return mDelegate->getFilePath(uri); | ||
| } | ||
|
|
||
| private: | ||
| bool isAlias(const common::Uri& uri) const | ||
| { | ||
| return uri.toString() == mAliasUri; | ||
| } | ||
|
|
||
| std::string mAliasUri; | ||
| common::Uri mTargetUri; | ||
| common::LocalResourceRetrieverPtr mDelegate; | ||
| }; | ||
|
|
||
| const aiScene* loadMeshWithOverrides( | ||
| const std::string& uri, | ||
| const common::ResourceRetrieverPtr& retriever, | ||
| bool ignoreUnitSize) | ||
| { | ||
| aiPropertyStore* propertyStore = aiCreatePropertyStore(); | ||
| aiSetImportPropertyInteger( | ||
| propertyStore, | ||
| AI_CONFIG_PP_SBP_REMOVE, | ||
| aiPrimitiveType_POINT | aiPrimitiveType_LINE); | ||
|
|
||
| #ifdef AI_CONFIG_IMPORT_COLLADA_IGNORE_UP_DIRECTION | ||
| aiSetImportPropertyInteger( | ||
| propertyStore, AI_CONFIG_IMPORT_COLLADA_IGNORE_UP_DIRECTION, 1); | ||
| #endif | ||
|
|
||
| #ifdef AI_CONFIG_IMPORT_COLLADA_IGNORE_UNIT_SIZE | ||
| if (ignoreUnitSize) { | ||
| aiSetImportPropertyInteger( | ||
| propertyStore, AI_CONFIG_IMPORT_COLLADA_IGNORE_UNIT_SIZE, 1); | ||
| } | ||
| #else | ||
| if (ignoreUnitSize) { | ||
| aiReleasePropertyStore(propertyStore); | ||
| return nullptr; | ||
| } | ||
| #endif | ||
|
|
||
| dynamics::AssimpInputResourceRetrieverAdaptor systemIO(retriever); | ||
| aiFileIO fileIO = dynamics::createFileIO(&systemIO); | ||
|
|
||
| const aiScene* scene = aiImportFileExWithProperties( | ||
| uri.c_str(), | ||
| aiProcess_GenNormals | aiProcess_Triangulate | ||
| | aiProcess_JoinIdenticalVertices | aiProcess_SortByPType | ||
| | aiProcess_OptimizeMeshes, | ||
| &fileIO, | ||
| propertyStore); | ||
|
|
||
| if (!scene) { | ||
| aiReleasePropertyStore(propertyStore); | ||
| return nullptr; | ||
| } | ||
|
|
||
| scene = aiApplyPostProcessing(scene, aiProcess_PreTransformVertices); | ||
| aiReleasePropertyStore(propertyStore); | ||
| return scene; | ||
| } | ||
|
|
||
| double readColladaUnitScale(const std::string& path) | ||
| { | ||
| std::ifstream file(path); | ||
| if (!file.is_open()) | ||
| return 1.0; | ||
|
|
||
| const std::string token = "meter=\""; | ||
| std::string buffer( | ||
| (std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); | ||
|
|
||
| const std::size_t pos = buffer.find(token); | ||
| if (pos == std::string::npos) | ||
| return 1.0; | ||
|
|
||
| const std::size_t start = pos + token.size(); | ||
| const std::size_t end = buffer.find('"', start); | ||
| if (end == std::string::npos) | ||
| return 1.0; | ||
|
|
||
| try { | ||
| return std::stod(buffer.substr(start, end - start)); | ||
| } catch (...) { | ||
| return 1.0; | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(MeshShapeTest, ColladaUnitMetadataApplied) | ||
| { | ||
| #ifndef AI_CONFIG_IMPORT_COLLADA_IGNORE_UNIT_SIZE | ||
| GTEST_SKIP() << "Assimp build does not expose unit-size control property."; | ||
| #endif | ||
|
|
||
| const std::string filePath = dart::config::dataPath("skel/kima/l-foot.dae"); | ||
| const std::string fileUri = common::Uri::createFromPath(filePath).toString(); | ||
| ASSERT_FALSE(fileUri.empty()); | ||
|
|
||
| auto retriever = std::make_shared<common::LocalResourceRetriever>(); | ||
|
|
||
| const aiScene* sceneWithUnits | ||
| = dynamics::MeshShape::loadMesh(fileUri, retriever); | ||
| ASSERT_NE(sceneWithUnits, nullptr); | ||
| const auto shapeWithUnits = std::make_shared<dynamics::MeshShape>( | ||
| Eigen::Vector3d::Ones(), sceneWithUnits); | ||
| const Eigen::Vector3d extentsWithUnits | ||
| = shapeWithUnits->getBoundingBox().computeFullExtents(); | ||
|
|
||
| const aiScene* sceneIgnoringUnits | ||
| = loadMeshWithOverrides(fileUri, retriever, true); | ||
| ASSERT_NE(sceneIgnoringUnits, nullptr); | ||
| const auto shapeIgnoringUnits = std::make_shared<dynamics::MeshShape>( | ||
| Eigen::Vector3d::Ones(), sceneIgnoringUnits); | ||
| const Eigen::Vector3d extentsIgnoringUnits | ||
| = shapeIgnoringUnits->getBoundingBox().computeFullExtents(); | ||
|
|
||
| const double unitScale = readColladaUnitScale(filePath); | ||
| ASSERT_GT(unitScale, 0.0); | ||
|
|
||
| EXPECT_TRUE(extentsWithUnits.isApprox(extentsIgnoringUnits * unitScale, 1e-6)) | ||
| << "extentsWithUnits=" << extentsWithUnits.transpose() | ||
| << ", extentsIgnoringUnits=" << extentsIgnoringUnits.transpose() | ||
| << ", unitScale=" << unitScale; | ||
| } | ||
|
|
||
| TEST(MeshShapeTest, ColladaUriWithoutExtensionStillLoads) | ||
| { | ||
| const std::string filePath = dart::config::dataPath("skel/kima/l-foot.dae"); | ||
| const std::string aliasUri = "collada-nodot://meshshape/lfoot"; | ||
|
|
||
| auto aliasRetriever | ||
| = std::make_shared<AliasUriResourceRetriever>(aliasUri, filePath); | ||
| const aiScene* aliasScene | ||
| = dynamics::MeshShape::loadMesh(aliasUri, aliasRetriever); | ||
| ASSERT_NE(aliasScene, nullptr); | ||
| const auto aliasShape = std::make_shared<dynamics::MeshShape>( | ||
| Eigen::Vector3d::Ones(), aliasScene); | ||
| const Eigen::Vector3d aliasExtents | ||
| = aliasShape->getBoundingBox().computeFullExtents(); | ||
|
|
||
| auto canonicalRetriever = std::make_shared<common::LocalResourceRetriever>(); | ||
| const aiScene* canonicalScene = dynamics::MeshShape::loadMesh( | ||
| common::Uri::createFromPath(filePath).toString(), canonicalRetriever); | ||
| ASSERT_NE(canonicalScene, nullptr); | ||
| const auto canonicalShape = std::make_shared<dynamics::MeshShape>( | ||
| Eigen::Vector3d::Ones(), canonicalScene); | ||
| const Eigen::Vector3d canonicalExtents | ||
| = canonicalShape->getBoundingBox().computeFullExtents(); | ||
|
|
||
| EXPECT_TRUE(aliasExtents.isApprox(canonicalExtents, 1e-6)) | ||
| << "aliasExtents=" << aliasExtents.transpose() | ||
| << ", canonicalExtents=" << canonicalExtents.transpose(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.