utils.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  */
00030 
00031 
00038 #ifndef POSE_GRAPH_UTILS_H
00039 #define POSE_GRAPH_UTILS_H
00040 
00041 
00042 #include <ros/assert.h>
00043 #include <map>
00044 #include <string>
00045 #include <boost/lexical_cast.hpp>
00046 #include <pose_graph/geometry.h>
00047 #include <pose_graph/transforms.h>
00048 #include <geometry_msgs/Pose2D.h>
00049 #include <boost/format.hpp>
00050 #include <Eigen3/Dense>
00051 #include <numeric>
00052 
00053 namespace pose_graph
00054 {
00055 namespace gm=geometry_msgs;
00056 
00057 using std::map;
00058 using std::pair;
00059 using std::string;
00060 
00061 using boost::format;
00062 
00063 
00064 // Check if a container contains a key
00065 template <class Key, class Container>
00066 bool contains (const Container& container, const Key& key)
00067 {
00068   return container.find(key)!=container.end();
00069 }
00070 
00071 
00072 
00073 // Get value corresponding to a key in a map
00074 template <class K, class V, class C, class A>
00075 const V& keyValue (const map<K, V, C, A>& m, const K& key)
00076 {
00077   typename map<K, V, C, A>::const_iterator pos = m.find(key);
00078   ROS_ASSERT_MSG (pos!=m.end(), "Map did not contain a key that it was expected to");
00079   return pos->second;
00080 }
00081 
00082 
00083 /************************************************************
00084  * toString
00085  ************************************************************/
00086 
00087 inline
00088 string toString (const gm::Pose2D& pose)
00089 {
00090   return (format("[%.2f, %.2f, %.2f]") % pose.x % pose.y % pose.theta).str();
00091 }
00092 
00093 inline
00094 string toString (const gm::Point& p)
00095 {
00096   return (format("[%.2f, %.2f]") % p.x % p.y).str();
00097 }
00098 
00099 inline
00100 string toString (const Eigen3::Affine3d& trans)
00101 {
00102   const Eigen3::Vector3d v(trans.translation());
00103   const Eigen3::Quaterniond q(trans.rotation());
00104   return (format("[[%.2f, %.2f, %.2f], [%.2f, %.2f, %.2f, %.2f]]") %
00105           v.x() % v.y() % v.z() % q.x() % q.y() % q.z() % q.w()).str();
00106 }
00107 
00108 inline
00109 string toString2D (const gm::Pose& pose)
00110 {
00111   gm::Pose2D p = projectToPose2D(pose);
00112   
00113   return (format("(%2f, %2f, %2f)") % p.x % p.y %
00114           p.theta).str();
00115 }
00116 
00117 inline
00118 void poseEigenToMsg(const Eigen3::Affine3d &e, geometry_msgs::Pose &m)
00119 {
00120   m.position.x = e.translation()[0];
00121   m.position.y = e.translation()[1];
00122   m.position.z = e.translation()[2];
00123   Eigen3::Quaterniond q = (Eigen3::Quaterniond)e.linear();
00124   m.orientation.x = q.x();
00125   m.orientation.y = q.y();
00126   m.orientation.z = q.z();
00127   m.orientation.w = q.w();
00128   if (m.orientation.w < 0) {
00129     m.orientation.x *= -1;
00130     m.orientation.y *= -1;
00131     m.orientation.z *= -1;
00132     m.orientation.w *= -1;
00133   }
00134 }
00135 
00136 
00137 inline
00138 string toString2D (const Eigen3::Affine3d& trans)
00139 {
00140   gm::Pose p;
00141   poseEigenToMsg(trans, p);
00142   return toString2D(p);
00143 }
00144 
00145 
00146 inline
00147 string toString (const PoseConstraint& constraint)
00148 {
00149   const Eigen3::Vector3d v(constraint.translation);
00150   const Eigen3::Quaterniond q(constraint.rotation);
00151   return (format("[[%.2f, %.2f, %.2f], [%.2f, %.2f, %.2f, %.2f]]") %
00152           v.x() % v.y() % v.z() % q.x() % q.x() % q.y() % q.w()).str();
00153 }
00154   
00155 
00156 inline
00157 string toString (const gm::Pose& pose)
00158 {
00159   return (format("[(%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f, %.2f)]") %
00160           pose.position.x % pose.position.y % pose.position.z %
00161           pose.orientation.x % pose.orientation.y % pose.orientation.z % pose.orientation.w).str();
00162 }
00163 
00164 
00165 template <class T>
00166 std::string concatenate (const std::string& s, const T& x)
00167 {
00168   return s + std::string(" ") + boost::lexical_cast<std::string>(x);
00169 }
00170 
00171 
00172 template <class T>
00173 std::string toString (const std::set<T>& set)
00174 {
00175   const std::string s = std::accumulate(set.begin(), set.end(), std::string(""), concatenate<T>);
00176   return std::string("[") + s + std::string("]");
00177 }
00178 
00179 template <class T>
00180 std::string toString (const std::vector<T>& v)
00181 {
00182   const std::string s = std::accumulate(v.begin(), v.end(), std::string(""), concatenate<T>);
00183   return std::string("[") + s + std::string("]");
00184 }
00185 
00186 inline
00187 std::string toString (const PrecisionMatrix& m)
00188 {
00189   return (format("(x-x: %.2f, y-y: %.2f, x-y: %.2f, th-th: %.2f)") % m(0,0) % m(1,1) % m(0,1) % m(5,5)).str();
00190 }
00191 
00192 
00193 
00194 
00195 } // namespace
00196 
00197 
00198 #endif // Include guard


pose_graph
Author(s): Bhaskara Marthi
autogenerated on Tue Jan 7 2014 11:17:15