Skip to content

Commit acf77e8

Browse files
authored
Merge pull request #2403 from MegMll/topic/mjcf_warning_and_root_joint
Parser/mjcf : Add warning and fix root joint of mjcf models
2 parents 70eebe8 + d0ab92b commit acf77e8

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
### Fixed
1010
- Fix linkage of Boost.Serialization on Windows ([#2400](https://github.com/stack-of-tasks/pinocchio/pull/2400))
11+
- Fix mjcf parser appending of inertias at root joint ([#2403](https://github.com/stack-of-tasks/pinocchio/pull/2403))
1112

1213
## [3.2.0] - 2024-08-27
1314

src/parsers/mjcf/mjcf-graph.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -500,15 +500,29 @@ namespace pinocchio
500500
namespace fs = boost::filesystem;
501501
MjcfTexture text;
502502
auto file = el.get_optional<std::string>("<xmlattr>.file");
503-
if (!file)
504-
throw std::invalid_argument("Only textures with files are supported");
505-
506-
fs::path filePath(*file);
507-
std::string name = getName(el, filePath);
503+
auto name_ = el.get_optional<std::string>("<xmlattr>.name");
504+
auto type = el.get_optional<std::string>("<xmlattr>.type");
508505

509-
text.filePath =
510-
updatePath(compilerInfo.strippath, compilerInfo.texturedir, modelPath, filePath).string();
506+
std::string name;
507+
if (name_)
508+
name = *name_;
509+
else if (type && *type == "skybox")
510+
name = *type;
511+
if (!file)
512+
{
513+
std::cout << "Warning - Only texture with files are supported" << std::endl;
514+
if (name.empty())
515+
throw std::invalid_argument("Textures need a name.");
516+
}
517+
else
518+
{
519+
fs::path filePath(*file);
520+
name = getName(el, filePath);
511521

522+
text.filePath =
523+
updatePath(compilerInfo.strippath, compilerInfo.texturedir, modelPath, filePath)
524+
.string();
525+
}
512526
auto str_v = el.get_optional<std::string>("<xmlattr>.type");
513527
if (str_v)
514528
text.textType = *str_v;
@@ -785,15 +799,12 @@ namespace pinocchio
785799
{
786800

787801
FrameIndex parentFrameId = 0;
788-
Inertia inert = Inertia::Zero();
789802
if (!currentBody.bodyParent.empty())
790-
{
791803
parentFrameId = urdfVisitor.getBodyId(currentBody.bodyParent);
792-
inert = currentBody.bodyInertia;
793-
}
804+
794805
// get body pose in body parent
795806
const SE3 bodyPose = currentBody.bodyPlacement;
796-
807+
Inertia inert = currentBody.bodyInertia;
797808
SE3 jointInParent = bodyPose * joint.jointPlacement;
798809
bodyInJoint = joint.jointPlacement.inverse();
799810
UrdfVisitor::JointType jType;
@@ -1036,7 +1047,8 @@ namespace pinocchio
10361047
// get name and inertia of first root link
10371048
std::string rootLinkName = bodiesList.at(0);
10381049
MjcfBody rootBody = mapOfBodies.find(rootLinkName)->second;
1039-
urdfVisitor.addRootJoint(rootBody.bodyInertia, rootLinkName);
1050+
if (rootBody.jointChildren.size() == 0)
1051+
urdfVisitor.addRootJoint(rootBody.bodyInertia, rootLinkName);
10401052

10411053
fillReferenceConfig(rootBody);
10421054
for (const auto & entry : bodiesList)

unittest/mjcf.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,39 @@ BOOST_AUTO_TEST_CASE(adding_keyframes)
940940
BOOST_CHECK(vect_model == vect_ref);
941941
}
942942

943+
// Test on which joints inertias are append
944+
BOOST_AUTO_TEST_CASE(joint_and_inertias)
945+
{
946+
typedef pinocchio::SE3::Vector3 Vector3;
947+
typedef pinocchio::SE3::Matrix3 Matrix3;
948+
949+
std::istringstream xmlData(R"(
950+
<mujoco model="testKeyFrame">
951+
<worldbody>
952+
<body name="body1">
953+
<freejoint/>
954+
<inertial mass="0.629769" pos="-0.041018 -0.00014 0.049974"
955+
diaginertia="0.00315 0.00388 0.004285"/>
956+
</body>
957+
</worldbody>
958+
</mujoco>)");
959+
960+
auto namefile = createTempFile(xmlData);
961+
962+
pinocchio::Model model_m;
963+
pinocchio::mjcf::buildModel(namefile.name(), model_m);
964+
965+
BOOST_CHECK(model_m.inertias[0].isApprox(pinocchio::Inertia::Zero()));
966+
967+
Matrix3 inertia_matrix = Eigen::Matrix3d::Zero();
968+
inertia_matrix(0, 0) = 0.00315;
969+
inertia_matrix(1, 1) = 0.00388;
970+
inertia_matrix(2, 2) = 0.004285;
971+
pinocchio::Inertia real_inertia(0.629769, Vector3(-0.041018, -0.00014, 0.049974), inertia_matrix);
972+
973+
BOOST_CHECK(model_m.inertias[1].isApprox(real_inertia));
974+
}
975+
943976
// Test reference positions and how it's included in keyframe
944977
BOOST_AUTO_TEST_CASE(reference_positions)
945978
{

0 commit comments

Comments
 (0)