Skip to content

Commit

Permalink
Fix bug that caused iDynTree::ModelParserOptions to be ignored (#1191)
Browse files Browse the repository at this point in the history
  • Loading branch information
traversaro authored Jul 5, 2024
1 parent bf15223 commit 07fac3a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

cmake_minimum_required(VERSION 3.16)

project(iDynTree VERSION 12.3.2
project(iDynTree VERSION 12.3.3
LANGUAGES C CXX)

# Disable in source build, unless Eclipse is used
Expand Down
2 changes: 1 addition & 1 deletion src/model_io/codecs/include/private/URDFDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class iDynTree::URDFDocument: public iDynTree::XMLDocument {
} m_buffers;

public:
explicit URDFDocument(XMLParserState& parserState);
explicit URDFDocument(XMLParserState& parserState, const iDynTree::ModelParserOptions& parserOptions);
virtual ~URDFDocument();

iDynTree::ModelParserOptions& options();
Expand Down
17 changes: 11 additions & 6 deletions src/model_io/codecs/src/ModelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ namespace iDynTree
{
// Allocate parser
std::shared_ptr<XMLParser> parser = std::make_shared<XMLParser>();
parser->setDocumentFactory([](XMLParserState& state){
return std::shared_ptr<XMLDocument>(new URDFDocument(state));
});
auto parserOptions = this->m_pimpl->m_options;
auto documentFactoryWithOptions = [parserOptions](XMLParserState& state){
return std::shared_ptr<XMLDocument>(new URDFDocument(state, parserOptions));
};
parser->setDocumentFactory(documentFactoryWithOptions);
parser->setPackageDirs(packageDirs);
if (!parser->parseXMLFile(filename)) {
reportError("ModelLoader", "loadModelFromFile", "Error in parsing model from URDF.");
Expand All @@ -106,9 +108,12 @@ namespace iDynTree
{
// Allocate parser
std::shared_ptr<XMLParser> parser = std::make_shared<XMLParser>();
parser->setDocumentFactory([](XMLParserState& state){
return std::shared_ptr<XMLDocument>(new URDFDocument(state));
});
auto parserOptions = this->m_pimpl->m_options;
auto documentFactoryWithOptions = [parserOptions](XMLParserState& state){
return std::shared_ptr<XMLDocument>(new URDFDocument(state, parserOptions));
};

parser->setDocumentFactory(documentFactoryWithOptions);
parser->setPackageDirs(packageDirs);
if (!parser->parseXMLString(modelString)) {
reportError("ModelLoader", "loadModelFromString", "Error in parsing model from URDF.");
Expand Down
4 changes: 2 additions & 2 deletions src/model_io/codecs/src/URDFDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace iDynTree {
const std::unordered_map<std::string, MaterialElement::MaterialInfo>& materialDatabase,
ModelSolidShapes &modelGeometries);

URDFDocument::URDFDocument(XMLParserState& parserState)
: XMLDocument(parserState) {}
URDFDocument::URDFDocument(XMLParserState& parserState, const iDynTree::ModelParserOptions& options)
: XMLDocument(parserState), m_options(options) {}

iDynTree::ModelParserOptions& URDFDocument::options() { return m_options; }

Expand Down
54 changes: 54 additions & 0 deletions src/model_io/codecs/tests/URDFModelImportUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,59 @@ void checkDuplicateJointsReturnsError() {

}

void checkaddSensorFramesAsAdditionalFramesOption() {
std::string urdf = R"(
<robot name="robot">
<link name="link_1">
<inertial>
<mass value="1"/>
</inertial>
</link>
<link name="link_2">
<inertial>
<mass value="2"/>
</inertial>
</link>
<joint name="joint_1" type="fixed">
<origin xyz="0.0 0.0 0.0"/>
<axis xyz="0 0 1"/>
<parent link="link_1"/>
<child link="link_2"/>
<limit lower="-1.0" upper="1.0"/>
</joint>
<sensor name="l_leg_ft" type="force_torque">
<parent joint="joint_1"/>
<force_torque>
<frame>sensor</frame>
<measure_direction>child_to_parent</measure_direction>
</force_torque>
<origin rpy="2.220446049250313e-16 -2.220446049250314e-16 2.220446049250313e-16" xyz="0.0 -1.3877787807814457e-17 0.0"/>
</sensor>
</robot>
)";

// Let's first load with the option disabled
{
iDynTree::ModelLoader mdlLoader;
iDynTree::ModelParserOptions parserOptions;
parserOptions.addSensorFramesAsAdditionalFrames = false;
mdlLoader.setParsingOptions(parserOptions);
ASSERT_IS_TRUE(mdlLoader.loadModelFromString(urdf));
ASSERT_IS_FALSE(mdlLoader.model().isFrameNameUsed("l_leg_ft"));
}

// Then with the option enabled
{
iDynTree::ModelLoader mdlLoader;
iDynTree::ModelParserOptions parserOptions;
parserOptions.addSensorFramesAsAdditionalFrames = true;
mdlLoader.setParsingOptions(parserOptions);
ASSERT_IS_TRUE(mdlLoader.loadModelFromString(urdf));
ASSERT_IS_TRUE(mdlLoader.model().isFrameNameUsed("l_leg_ft"));
}

}

int main()
{
checkURDF(getAbsModelPath("/simple_model.urdf"),1,0,0,1, 1, 0, "link1");
Expand All @@ -270,6 +323,7 @@ int main()
checkLimitsForJointsAreDefinedFromFileName(getAbsModelPath("iCubGenova02.urdf"));

checkLoadReducedModelOrderIsKept(getAbsModelPath("iCubGenova02.urdf"));
checkaddSensorFramesAsAdditionalFramesOption();

return EXIT_SUCCESS;
}

0 comments on commit 07fac3a

Please sign in to comment.