Go to the documentation of this file.00001 #include <ros/ros.h>
00002 #include <object_msgs_tools/ObjectFunctions.h>
00003 #include <eigen_conversions/eigen_msg.h>
00004
00005 using object_msgs_tools::ObjectFunctions;
00006
00007 bool ObjectFunctions::getObjectPose(const object_msgs::Object& object, geometry_msgs::PoseStamped& pose)
00008 {
00009 if ((object.primitive_origin == object_msgs::Object::ORIGIN_UNDEFINED) &&
00010 (object.mesh_origin == object_msgs::Object::ORIGIN_UNDEFINED))
00011 {
00012 return false;
00013 }
00014
00015 bool success = false;
00016 if (object.primitive_origin != object_msgs::Object::ORIGIN_UNDEFINED)
00017 {
00018 success = getPoseFromFields(object.header,object.primitive_origin,
00019 object.primitive_poses, object.origin, pose);
00020 }
00021 else
00022 {
00023 success = getPoseFromFields(object.header,object.mesh_origin,
00024 object.mesh_poses, object.origin, pose);
00025 }
00026 return success;
00027 }
00028
00029 bool ObjectFunctions::getPoseFromFields(const std_msgs::Header& object_header, int idx,
00030 const std::vector<geometry_msgs::Pose>& poses,
00031 const geometry_msgs::Pose & origin,
00032 geometry_msgs::PoseStamped& pose)
00033 {
00034 if (idx == object_msgs::Object::ORIGIN_UNDEFINED)
00035 return false;
00036
00037 if (idx == object_msgs::Object::ORIGIN_AVERAGE)
00038 {
00039 pose.pose.position = getAveragePointFrom(poses);
00040 pose.pose.orientation = origin.orientation;
00041 }
00042 else if (idx == object_msgs::Object::ORIGIN_CUSTOM)
00043 {
00044 pose.pose = origin;
00045 }
00046 else if (idx > 0)
00047 {
00048 if (poses.size() <= idx)
00049 {
00050 ROS_ERROR_STREAM("ObjectFunctions: Inconsistent object, "<<
00051 "has less primitive poses than required (" <<
00052 poses.size()<<", required "<<idx <<")");
00053 return false;
00054 }
00055 pose.pose = poses[idx];
00056 }
00057 else
00058 {
00059 ROS_ERROR_STREAM("Unknown mode of Object::primitive_origin or Object::mesh_origin: "<<idx);
00060 return false;
00061 }
00062 pose.header = object_header;
00063 return true;
00064 }
00065 geometry_msgs::Point ObjectFunctions::getAveragePointFrom(const std::vector<geometry_msgs::Pose>& poses)
00066 {
00067 Eigen::Vector3d p;
00068 for (int i=0; i < poses.size(); ++i)
00069 {
00070 Eigen::Vector3d _p;
00071 const geometry_msgs::Pose& pi = poses[i];
00072 tf::pointMsgToEigen(pi.position,_p);
00073 p+=_p;
00074 }
00075 geometry_msgs::Point ret;
00076 tf::pointEigenToMsg(p,ret);
00077 return ret;
00078 }