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

mjcf parser: Fix unknown size vector parsing #2535

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed
- Fix mjcf Euler angle parsing: use xyz as a default value for eulerseq compiler option ([#2526](https://github.com/stack-of-tasks/pinocchio/pull/2526))
- Add parsing meshes with vertices for MJCF format ([#2537](https://github.com/stack-of-tasks/pinocchio/pull/2537))
- Fix mjcf parsing of keyframe qpos with newlines ([#2535](https://github.com/stack-of-tasks/pinocchio/pull/2535))

## [3.3.1] - 2024-12-13

Expand Down
5 changes: 2 additions & 3 deletions include/pinocchio/parsers/mjcf/mjcf-graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ namespace pinocchio
inline std::istringstream getConfiguredStringStream(const std::string & str)
{
std::istringstream posStream(str);
posStream.exceptions(std::ios::failbit);
posStream.exceptions(std::ios::badbit);
return posStream;
}

Expand All @@ -569,9 +569,8 @@ namespace pinocchio
std::istringstream stream = getConfiguredStringStream(str);
std::vector<double> vector;
double elem;
while (!stream.eof())
while (stream >> elem)
{
stream >> elem;
vector.push_back(elem);
}

Expand Down
32 changes: 31 additions & 1 deletion unittest/mjcf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "pinocchio/multibody/model.hpp"

#include "pinocchio/parsers/mjcf.hpp"
#include "pinocchio/parsers/mjcf/mjcf-graph.hpp"
#include "pinocchio/parsers/urdf.hpp"

#include "pinocchio/algorithm/joint-configuration.hpp"
Expand Down Expand Up @@ -923,7 +924,8 @@ BOOST_AUTO_TEST_CASE(adding_keyframes)
<key name="test"
qpos="0 0 0.596
0.988015 0 0.154359 0
0.988015 0 0.154359 0"/>
0.988015 0 0.154359 0
"/>
Comment on lines +927 to +928
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I revert this change? It's just meant as an extra test, otherwise a similar case is added in the test_get_unknown_size_vector_from_stream test

</keyframe>
</mujoco>)");

Expand Down Expand Up @@ -1357,6 +1359,34 @@ BOOST_AUTO_TEST_CASE(test_default_eulerseq)
BOOST_CHECK(graph.mapOfBodies["body"].bodyPlacement.isApprox(placement));
}

BOOST_AUTO_TEST_CASE(test_get_unknown_size_vector_from_stream)
{
const auto v = pinocchio::mjcf::details::internal::getUnknownSizeVectorFromStream("");
BOOST_CHECK(v.size() == 0);

const auto v1 = pinocchio::mjcf::details::internal::getUnknownSizeVectorFromStream("1 2 3");
BOOST_CHECK(v1.size() == 3);
Eigen::VectorXd expected(3);
expected << 1, 2, 3;
BOOST_CHECK(v1 == expected);

const auto v2 = pinocchio::mjcf::details::internal::getUnknownSizeVectorFromStream(R"(1 2 3
4 5 6)");
BOOST_CHECK(v2.size() == 6);
Eigen::VectorXd expected2(6);
expected2 << 1, 2, 3, 4, 5, 6;
BOOST_CHECK(v2 == expected2);

const auto v3 = pinocchio::mjcf::details::internal::getUnknownSizeVectorFromStream(R"(1 2 3
4 5 6
7 8 9
)");
BOOST_CHECK(v3.size() == 9);
Eigen::VectorXd expected3(9);
expected3 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
BOOST_CHECK(v3 == expected3);
}

/// @brief Test parsing a mesh with vertices
/// @param
BOOST_AUTO_TEST_CASE(parse_mesh_with_vertices)
Expand Down
Loading