-
Notifications
You must be signed in to change notification settings - Fork 152
fix error handling in material parsing #122
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
base: rolling
Are you sure you want to change the base?
Changes from all commits
2e9aaca
0ca9f22
f7b6caf
f8b1870
44bfb21
1ece7f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -91,6 +91,19 @@ bool assignMaterial(const VisualSharedPtr& visual, ModelInterfaceSharedPtr& mode | |||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| bool operator==(const Material& lhs, const Material& rhs) | ||||||
| { | ||||||
| return lhs.texture_filename == rhs.texture_filename && | ||||||
| lhs.color.r == rhs.color.r && | ||||||
| lhs.color.g == rhs.color.g && | ||||||
| lhs.color.b == rhs.color.b && | ||||||
| lhs.color.a == rhs.color.a; | ||||||
| } | ||||||
| inline bool operator!=(const Material& lhs, const Material& rhs) | ||||||
| { | ||||||
| return !(lhs == rhs); | ||||||
| } | ||||||
|
|
||||||
| ModelInterfaceSharedPtr parseURDF(const std::string &xml_string) | ||||||
| { | ||||||
| ModelInterfaceSharedPtr model(new ModelInterface); | ||||||
|
|
@@ -152,27 +165,31 @@ ModelInterfaceSharedPtr parseURDF(const std::string &xml_string) | |||||
| MaterialSharedPtr material; | ||||||
| material.reset(new Material); | ||||||
|
|
||||||
| bool success; | ||||||
| try { | ||||||
| parseMaterial(*material, material_xml, false); // material needs to be fully defined here | ||||||
| if (model->getMaterial(material->name)) | ||||||
| { | ||||||
| CONSOLE_BRIDGE_logError("material '%s' is not unique.", material->name.c_str()); | ||||||
| material.reset(); | ||||||
| model.reset(); | ||||||
| return model; | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| model->materials_.insert(make_pair(material->name,material)); | ||||||
| CONSOLE_BRIDGE_logDebug("urdfdom: successfully added a new material '%s'", material->name.c_str()); | ||||||
| } | ||||||
| } | ||||||
| catch (ParseError &/*e*/) { | ||||||
| success = parseMaterial(*material, material_xml, false); // material needs to be fully defined here | ||||||
| } catch(ParseError & /*e*/) { | ||||||
| CONSOLE_BRIDGE_logError("material xml is not initialized correctly"); | ||||||
| success = false; | ||||||
| } | ||||||
|
|
||||||
| if (success && material) { | ||||||
| if (const MaterialSharedPtr& other = model->getMaterial(material->name); | ||||||
| other && *material != *other) | ||||||
| { | ||||||
| CONSOLE_BRIDGE_logError("material '%s' is not unique.", material->name.c_str()); | ||||||
| success = false; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (!success) { | ||||||
| material.reset(); | ||||||
| model.reset(); | ||||||
| return model; | ||||||
| } | ||||||
|
|
||||||
| model->materials_.insert(make_pair(material->name,material)); | ||||||
| CONSOLE_BRIDGE_logDebug("urdfdom: successfully added a new material '%s'", material->name.c_str()); | ||||||
| } | ||||||
|
|
||||||
| // Get all Link elements | ||||||
|
|
@@ -181,37 +198,41 @@ ModelInterfaceSharedPtr parseURDF(const std::string &xml_string) | |||||
| LinkSharedPtr link; | ||||||
| link.reset(new Link); | ||||||
|
|
||||||
| bool success; | ||||||
| try { | ||||||
| parseLink(*link, link_xml, version); | ||||||
| if (model->getLink(link->name)) | ||||||
| { | ||||||
| CONSOLE_BRIDGE_logError("link '%s' is not unique.", link->name.c_str()); | ||||||
| model.reset(); | ||||||
| return model; | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| // set link visual(s) material | ||||||
| CONSOLE_BRIDGE_logDebug("urdfdom: setting link '%s' material", link->name.c_str()); | ||||||
| if (link->visual) | ||||||
| { | ||||||
| assignMaterial(link->visual, model, link->name.c_str()); | ||||||
| } | ||||||
| for (const auto& visual : link->visual_array) | ||||||
| { | ||||||
| assignMaterial(visual, model, link->name.c_str()); | ||||||
| } | ||||||
|
|
||||||
| model->links_.insert(make_pair(link->name,link)); | ||||||
| CONSOLE_BRIDGE_logDebug("urdfdom: successfully added a new link '%s'", link->name.c_str()); | ||||||
| } | ||||||
| success = parseLink(*link, link_xml, version); | ||||||
| } catch (ParseError & /*e*/) { | ||||||
| success = false; | ||||||
| } | ||||||
| catch (ParseError &/*e*/) { | ||||||
|
|
||||||
| if (!success) { | ||||||
| CONSOLE_BRIDGE_logError("link xml is not initialized correctly"); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| model.reset(); | ||||||
| return model; | ||||||
| } | ||||||
|
|
||||||
| if (model->getLink(link->name)) | ||||||
| { | ||||||
| CONSOLE_BRIDGE_logError("link '%s' is not unique.", link->name.c_str()); | ||||||
| model.reset(); | ||||||
| return model; | ||||||
| } | ||||||
|
|
||||||
| // set link visual(s) material | ||||||
| CONSOLE_BRIDGE_logDebug("urdfdom: setting link '%s' material", link->name.c_str()); | ||||||
| if (link->visual) | ||||||
| { | ||||||
| assignMaterial(link->visual, model, link->name.c_str()); | ||||||
| } | ||||||
| for (const auto& visual : link->visual_array) | ||||||
| { | ||||||
| assignMaterial(visual, model, link->name.c_str()); | ||||||
| } | ||||||
|
|
||||||
| model->links_.insert(make_pair(link->name, link)); | ||||||
| CONSOLE_BRIDGE_logDebug("urdfdom: successfully added a new link '%s'", link->name.c_str()); | ||||||
| } | ||||||
|
|
||||||
| if (model->links_.empty()){ | ||||||
| CONSOLE_BRIDGE_logError("No link elements found in urdf file"); | ||||||
| model.reset(); | ||||||
|
|
@@ -224,26 +245,28 @@ ModelInterfaceSharedPtr parseURDF(const std::string &xml_string) | |||||
| JointSharedPtr joint; | ||||||
| joint.reset(new Joint); | ||||||
|
|
||||||
| if (parseJoint(*joint, joint_xml, version)) | ||||||
| { | ||||||
| if (model->getJoint(joint->name)) | ||||||
| { | ||||||
| CONSOLE_BRIDGE_logError("joint '%s' is not unique.", joint->name.c_str()); | ||||||
| model.reset(); | ||||||
| return model; | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| model->joints_.insert(make_pair(joint->name,joint)); | ||||||
| CONSOLE_BRIDGE_logDebug("urdfdom: successfully added a new joint '%s'", joint->name.c_str()); | ||||||
| } | ||||||
| bool success; | ||||||
| try { | ||||||
| success = parseJoint(*joint, joint_xml, version); | ||||||
| } catch(ParseError & /*e*/) { | ||||||
| success = false; | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
|
|
||||||
| if (!success) { | ||||||
| CONSOLE_BRIDGE_logError("joint xml is not initialized correctly"); | ||||||
| model.reset(); | ||||||
| return model; | ||||||
| } | ||||||
|
|
||||||
| if (model->getJoint(joint->name)) | ||||||
| { | ||||||
| CONSOLE_BRIDGE_logError("joint '%s' is not unique.", joint->name.c_str()); | ||||||
| model.reset(); | ||||||
| return model; | ||||||
| } | ||||||
|
|
||||||
| model->joints_.insert(make_pair(joint->name,joint)); | ||||||
| CONSOLE_BRIDGE_logDebug("urdfdom: successfully added a new joint '%s'", joint->name.c_str()); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,19 +27,6 @@ bool quat_are_near(urdf::Rotation left, urdf::Rotation right) | |
| std::abs(l[3] + r[3]) < epsilon); | ||
| } | ||
|
|
||
| std::ostream &operator<<(std::ostream &os, const urdf::Rotation& rot) | ||
| { | ||
| double roll, pitch, yaw; | ||
| double x, y, z, w; | ||
| rot.getRPY(roll, pitch, yaw); | ||
| rot.getQuaternion(x, y, z, w); | ||
| os << std::setprecision(9) | ||
| << "x: " << x << " y: " << y << " z: " << z << " w: " << w | ||
| << " roll: " << roll << " pitch: " << pitch << " yaw: "<< yaw; | ||
| return os; | ||
| } | ||
|
Comment on lines
-30
to
-40
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to remove this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was part of @clalancette's cleanup branch, which I built upon. The commit message and motivation was: "quiet down the tests" 😉 |
||
|
|
||
|
|
||
| void check_get_set_rpy_is_idempotent(double x, double y, double z, double w) | ||
| { | ||
| urdf::Rotation rot0; | ||
|
|
@@ -48,12 +35,6 @@ void check_get_set_rpy_is_idempotent(double x, double y, double z, double w) | |
| rot0.getRPY(roll, pitch, yaw); | ||
| urdf::Rotation rot1; | ||
| rot1.setFromRPY(roll, pitch, yaw); | ||
| if (true) { | ||
| std::cout << "\n" | ||
| << "before " << rot0 << "\n" | ||
| << "after " << rot1 << "\n" | ||
| << "ok " << quat_are_near(rot0, rot1) << "\n"; | ||
| } | ||
| EXPECT_TRUE(quat_are_near(rot0, rot1)); | ||
| } | ||
|
|
||
|
|
@@ -66,12 +47,6 @@ void check_get_set_rpy_is_idempotent_from_rpy(double r, double p, double y) | |
| urdf::Rotation rot1; | ||
| rot1.setFromRPY(roll, pitch, yaw); | ||
| bool ok = quat_are_near(rot0, rot1); | ||
| if (!ok) { | ||
| std::cout << "initial rpy: " << r << " " << p << " " << y << "\n" | ||
| << "before " << rot0 << "\n" | ||
| << "after " << rot1 << "\n" | ||
| << "ok " << ok << "\n"; | ||
| } | ||
|
Comment on lines
-69
to
-74
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
| EXPECT_TRUE(ok); | ||
| } | ||
|
|
||
|
|
@@ -592,7 +567,6 @@ TEST(URDF_UNIT_TEST, parse_link_doubles) | |
| EXPECT_EQ(0.908, urdf->links_["l1"]->inertial->izz); | ||
| } | ||
|
|
||
|
|
||
| TEST(URDF_UNIT_TEST, parse_color_doubles) | ||
| { | ||
| std::string joint_str = | ||
|
|
@@ -666,6 +640,67 @@ TEST(URDF_UNIT_TEST, parse_color_doubles) | |
| EXPECT_EQ(0.908, urdf->links_["l1"]->inertial->izz); | ||
| } | ||
|
|
||
| TEST(URDF_UNIT_TEST, material_no_name) | ||
| { | ||
| std::string joint_str = | ||
| "<robot name=\"test\">" | ||
| " <material/>" | ||
| " <link name=\"l1\"/>" | ||
| "</robot>"; | ||
| urdf::ModelInterfaceSharedPtr urdf = urdf::parseURDF(joint_str); | ||
| EXPECT_EQ(nullptr, urdf); | ||
| } | ||
|
|
||
| TEST(URDF_UNIT_TEST, materials_no_rgb) | ||
| { | ||
| std::string urdf_str = | ||
| "<robot name=\"test\">" | ||
| " <material name=\"red\"/>" | ||
| " <link name=\"dummy\"/>" | ||
| "</robot>"; | ||
| urdf::ModelInterfaceSharedPtr urdf = urdf::parseURDF(urdf_str); | ||
| EXPECT_EQ(nullptr, urdf); // material missing RGB | ||
| } | ||
|
|
||
| TEST(URDF_UNIT_TEST, duplicate_materials) | ||
| { | ||
| std::string urdf_str = | ||
| "<robot name=\"test\">" | ||
| " <material name=\"red\">" | ||
| " <color rgba=\"1 0 0 1\"/>" | ||
| " </material>" | ||
| " <material name=\"red\">" | ||
| " <color rgba=\"1 0 0 1\"/>" | ||
| " </material>" | ||
| " <link name=\"dummy\"/>" | ||
| "</robot>"; | ||
|
|
||
| urdf::ModelInterfaceSharedPtr urdf = urdf::parseURDF(urdf_str); | ||
| EXPECT_TRUE(static_cast<bool>(urdf)); // identical materials are fine | ||
|
rhaschke marked this conversation as resolved.
|
||
|
|
||
| urdf_str = | ||
| "<robot name=\"test\">" | ||
| " <material name=\"red\">" | ||
| " <color rgba=\"1 0 0 1\"/>" | ||
| " </material>" | ||
| " <material name=\"red\">" | ||
| " <color rgba=\"0 1 0 1\"/>" | ||
| " </material>" | ||
| " <link name=\"dummy\"/>" | ||
| "</robot>"; | ||
| urdf = urdf::parseURDF(urdf_str); | ||
| EXPECT_EQ(nullptr, urdf); // different materials cause failure | ||
| } | ||
|
|
||
| TEST(URDF_UNIT_TEST, link_no_name) | ||
| { | ||
| std::string urdf_str = | ||
| "<robot name=\"test\">" | ||
| " <link/>" | ||
| "</robot>"; | ||
| urdf::ModelInterfaceSharedPtr urdf = urdf::parseURDF(urdf_str); | ||
| EXPECT_EQ(nullptr, urdf); | ||
| } | ||
|
|
||
| int main(int argc, char **argv) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't change the original error message for consistency. If you want to change it anyway, I suggest a more useful message:
The information that the error is originating from an exception (rather than the return value) is not relevant to a user, I think.