Skip to content

Commit 3bdef2b

Browse files
committed
Refactor joint properties data structure (moveit#110)
- Refactored joint_properties to use a map of maps for streamlined access to joint properties - Removed JointProperty struct as information contained is redundant. - Updated tests to account for new getJointProperties signature
1 parent f009ddc commit 3bdef2b

File tree

5 files changed

+26
-38
lines changed

5 files changed

+26
-38
lines changed

include/srdfdom/model.h

+11-20
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,6 @@ class Model
201201
return name_;
202202
}
203203

204-
// Some joints may have additional properties.
205-
struct JointProperty
206-
{
207-
/// The name of the joint that this property belongs to
208-
std::string joint_name_;
209-
210-
/// The name of the property
211-
std::string property_name_;
212-
213-
/// The value of the property. Type not specified.
214-
std::string value_;
215-
};
216-
217204
/// Get the list of links that should have collision checking disabled by default (and only selectively enabled)
218205
const std::vector<std::string>& getNoDefaultCollisionLinks() const
219206
{
@@ -269,20 +256,22 @@ class Model
269256
}
270257

271258
/// Get the joint properties for a particular joint (empty vector if none)
272-
const std::vector<JointProperty>& getJointProperties(const std::string& joint_name) const
259+
const std::map<std::string, std::string>& getJointProperties(const std::string& joint_name) const
273260
{
274-
std::map<std::string, std::vector<JointProperty>>::const_iterator iter = joint_properties_.find(joint_name);
261+
auto iter = joint_properties_.find(joint_name);
262+
275263
if (iter == joint_properties_.end())
276264
{
277265
// We return a standard empty vector here rather than insert a new empty vector
278266
// into the map in order to keep the method const
279-
return empty_vector_;
267+
return empty_map_;
280268
}
269+
281270
return iter->second;
282271
}
283272

284273
/// Get the joint properties list
285-
const std::map<std::string, std::vector<JointProperty>>& getJointProperties() const
274+
const std::map<std::string, std::map<std::string, std::string>>& getJointProperties() const
286275
{
287276
return joint_properties_;
288277
}
@@ -314,10 +303,12 @@ class Model
314303
std::vector<CollisionPair> enabled_collision_pairs_;
315304
std::vector<CollisionPair> disabled_collision_pairs_;
316305
std::vector<PassiveJoint> passive_joints_;
317-
std::map<std::string, std::vector<JointProperty>> joint_properties_;
318306

319-
// Empty joint property vector
320-
static const std::vector<JointProperty> empty_vector_;
307+
// Map the name of the joint to a second map that maps joint property name to joint property
308+
std::map<std::string, std::map<std::string, std::string>> joint_properties_;
309+
310+
// Empty joint property map
311+
static const std::map<std::string, std::string> empty_map_;
321312
};
322313
typedef std::shared_ptr<Model> ModelSharedPtr;
323314
typedef std::shared_ptr<const Model> ModelConstSharedPtr;

include/srdfdom/srdf_writer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class SRDFWriter
197197
std::vector<Model::CollisionPair> disabled_collision_pairs_;
198198
std::vector<Model::CollisionPair> enabled_collision_pairs_;
199199
std::vector<Model::PassiveJoint> passive_joints_;
200-
std::map<std::string, std::vector<srdf::Model::JointProperty>> joint_properties_;
200+
std::map<std::string, std::map<std::string, std::string>> joint_properties_;
201201

202202
// Store the SRDF Model for updating the kinematic_model
203203
ModelSharedPtr srdf_model_;

src/model.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
using namespace tinyxml2;
4747

48-
const std::vector<srdf::Model::JointProperty> srdf::Model::empty_vector_;
48+
const std::map<std::string, std::string> srdf::Model::empty_map_;
4949

5050
bool srdf::Model::isValidJoint(const urdf::ModelInterface& urdf_model, const std::string& name) const
5151
{
@@ -612,6 +612,9 @@ void srdf::Model::loadJointProperties(const urdf::ModelInterface& urdf_model, XM
612612
const char* jname = prop_xml->Attribute("joint_name");
613613
const char* pname = prop_xml->Attribute("property_name");
614614
const char* pval = prop_xml->Attribute("value");
615+
616+
std::string jname_str = boost::trim_copy(std::string(jname));
617+
615618
if (!jname)
616619
{
617620
CONSOLE_BRIDGE_logError("joint_property is missing a joint name");
@@ -628,18 +631,13 @@ void srdf::Model::loadJointProperties(const urdf::ModelInterface& urdf_model, XM
628631
continue;
629632
}
630633

631-
JointProperty jp;
632-
jp.joint_name_ = boost::trim_copy(std::string(jname));
633-
jp.property_name_ = boost::trim_copy(std::string(pname));
634-
jp.value_ = std::string(pval);
635-
636-
if (!isValidJoint(urdf_model, jp.joint_name_))
634+
if (!isValidJoint(urdf_model, jname_str))
637635
{
638636
CONSOLE_BRIDGE_logError("Property defined for a joint '%s' that is not known to the URDF. Ignoring.",
639-
jp.joint_name_.c_str());
637+
jname_str.c_str());
640638
continue;
641639
}
642-
joint_properties_[jp.joint_name_].push_back(jp);
640+
joint_properties_[jname_str][boost::trim_copy(std::string(pname))] = std::string(pval);
643641
}
644642
}
645643

src/srdf_writer.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,9 @@ void SRDFWriter::createJointPropertiesXML(tinyxml2::XMLElement* root)
518518
for (const auto& joint_property : joint_properties.second)
519519
{
520520
XMLElement* p_joint = doc->NewElement("joint_property");
521-
p_joint->SetAttribute("joint_name", joint_property.joint_name_.c_str());
522-
p_joint->SetAttribute("property_name", joint_property.property_name_.c_str());
523-
p_joint->SetAttribute("value", joint_property.value_.c_str());
521+
p_joint->SetAttribute("joint_name", joint_properties.first.c_str());
522+
p_joint->SetAttribute("property_name", joint_property.first.c_str());
523+
p_joint->SetAttribute("value", joint_property.second.c_str());
524524
root->InsertEndChild(p_joint);
525525
}
526526
}

test/test_parser.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,17 @@ TEST(TestCpp, testComplex)
179179
EXPECT_TRUE(s.getEndEffectors()[index].parent_link_ == "r_wrist_roll_link");
180180

181181
// Joint Properties
182-
const std::vector<srdf::Model::JointProperty>& gripper_props = s.getJointProperties("r_gripper_tool_joint");
182+
const std::map<std::string, std::string>& gripper_props = s.getJointProperties("r_gripper_tool_joint");
183183
EXPECT_EQ(gripper_props.size(), 0u);
184184

185185
// When parsing, this made up joint that is not present in the URDF is expected to print an error
186186
// AND the property should not be made available in the srdf::Model
187-
const std::vector<srdf::Model::JointProperty>& made_up_props = s.getJointProperties("made_up_joint");
187+
const std::map<std::string, std::string>& made_up_props = s.getJointProperties("made_up_joint");
188188
EXPECT_EQ(made_up_props.size(), 0u);
189189

190-
const std::vector<srdf::Model::JointProperty>& world_props = s.getJointProperties("world_joint");
190+
const std::map<std::string, std::string>& world_props = s.getJointProperties("world_joint");
191191
ASSERT_EQ(world_props.size(), 1u);
192-
EXPECT_EQ(world_props[0].property_name_, "angular_distance_weight");
193-
EXPECT_EQ(world_props[0].value_, "0.5");
192+
EXPECT_EQ(world_props.at("angular_distance_weight"), "0.5");
194193
}
195194

196195
TEST(TestCpp, testReadWrite)

0 commit comments

Comments
 (0)