common.hpp
Go to the documentation of this file.
1 /*
2  * common.hpp
3  *
4  * Created on: Apr 11, 2013
5  * Author: jorge
6  */
7 
8 #ifndef COMMON_HPP_
9 #define COMMON_HPP_
10 
11 
12 #include <tf/tf.h>
13 
14 #include <geometry_msgs/Pose.h>
15 #include <geometry_msgs/PoseStamped.h>
16 #include <ecl/config/macros.hpp>
17 
18 namespace mtk
19 {
20 
21 // Template functions
22 
23 template <typename T> std::string nb2str(T x)
24 {
25  return static_cast<std::ostringstream*>( &(std::ostringstream() << x) )->str();
26 }
27 
28 template <typename T> T median(const std::vector<T>& v) {
29  // Return the median element of a vector
30  std::vector<T> c = v; // clone, as nth_element will short half of the vector
31  std::nth_element(c.begin(), c.begin() + c.size()/2, c.end());
32  return c[c.size()/2];
33 };
34 
35 template <typename T> T mean(const std::vector<T>& v)
36 {
37  T acc = 0;
38  for (unsigned int i = 0; i < v.size(); i++)
39  acc += v[i];
40  return acc/v.size();
41 };
42 
43 template <typename T> T variance(const std::vector<T>& v)
44 {
45  T acc = 0;
46  T avg = mean(v);
47  for (unsigned int i = 0; i < v.size(); i++)
48  acc += std::pow(v[i] - avg, 2);
49  return acc/(v.size() - 1);
50 };
51 
52 template <typename T> T std_dev(const std::vector<T>& v)
53 {
54  return std::sqrt(variance(v));
55 }
56 
57 
58 template <typename T> T sign(T x)
59 {
60  return x > 0.0 ? +1.0 : x < 0.0 ? -1.0 : 0.0;
61 }
62 
63 // Other functions
64 
65 void tf2pose(const tf::Transform& tf, geometry_msgs::Pose& pose);
66 void tf2pose(const tf::StampedTransform& tf, geometry_msgs::PoseStamped& pose);
67 
68 void pose2tf(const geometry_msgs::Pose& pose, tf::Transform& tf);
69 void pose2tf(const geometry_msgs::PoseStamped& pose, tf::StampedTransform& tf);
70 
71 std::string vector2str3D(const geometry_msgs::Vector3& vector);
72 std::string vector2str3D(const geometry_msgs::Vector3Stamped& vector);
73 std::string point2str2D(const geometry_msgs::Point& point);
74 std::string point2str2D(const geometry_msgs::PointStamped& point);
75 std::string point2str3D(const geometry_msgs::Point& point);
76 std::string point2str3D(const geometry_msgs::PointStamped& point);
77 std::string pose2str2D(const geometry_msgs::Pose& pose);
78 std::string pose2str2D(const geometry_msgs::PoseStamped& pose);
79 std::string pose2str3D(const geometry_msgs::Pose& pose);
80 std::string pose2str3D(const geometry_msgs::PoseStamped& pose);
81 
82 // Deprecated, as you cannot call more than once in a single sentence; use the xxx2str2D/3D cousins instead
83 #include <ecl/config/macros.hpp>
84 ECL_DEPRECATED const char* point2str(const geometry_msgs::Point& point);
85 ECL_DEPRECATED const char* pose2str(const geometry_msgs::Pose& pose);
86 ECL_DEPRECATED const char* pose2str(const geometry_msgs::PoseStamped& pose);
87 
88 
89 } /* namespace mtk */
90 
91 #endif /* COMMON_HPP_ */
#define ECL_DEPRECATED
T median(const std::vector< T > &v)
Definition: common.hpp:28
void pose2tf(const geometry_msgs::Pose &pose, tf::Transform &tf)
Definition: common.cpp:31
T mean(const std::vector< T > &v)
Definition: common.hpp:35
Definition: common.hpp:18
T variance(const std::vector< T > &v)
Definition: common.hpp:43
std::string pose2str2D(const geometry_msgs::Pose &pose)
Definition: common.cpp:99
std::string pose2str3D(const geometry_msgs::Pose &pose)
Definition: common.cpp:110
std::string nb2str(T x)
Definition: common.hpp:23
std::string vector2str3D(const geometry_msgs::Vector3 &vector)
Definition: common.cpp:66
std::string point2str3D(const geometry_msgs::Point &point)
Definition: common.cpp:88
T sign(T x)
Definition: common.hpp:58
T std_dev(const std::vector< T > &v)
Definition: common.hpp:52
void tf2pose(const tf::Transform &tf, geometry_msgs::Pose &pose)
Definition: common.cpp:16
ECL_DEPRECATED const char * pose2str(const geometry_msgs::Pose &pose)
Definition: common.cpp:54
ECL_DEPRECATED const char * point2str(const geometry_msgs::Point &point)
Definition: common.cpp:48
std::string point2str2D(const geometry_msgs::Point &point)
Definition: common.cpp:77


yocs_math_toolkit
Author(s): Jorge Santos
autogenerated on Mon Jun 10 2019 15:53:40