This is an example of usage of the convert() function for conversion between different vector types. It may be run after building mrs_lib by executing rosrun mrs_lib vector_converter_example
.
#include <geometry_msgs/Vector3.h>
#include <Eigen/Dense>
#include <pcl/point_types.h>
{
double e1, e2, e3;
};
{
namespace impl
{
std::tuple<double, double, double> convertFrom(
const MyPoint& in)
{
return {in.
e1, in.
e2, in.
e3};
}
void convertTo(
MyPoint& ret,
const double x,
const double y,
const double z)
{
ret.e1 = x;
ret.e2 = y;
ret.e3 = z;
}
}
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnarrowing"
#pragma GCC diagnostic pop
int main()
{
const double xo = rand(), yo = rand(), zo = rand();
const auto from = mrs_lib::impl::convertTo<pcl::PointXYZ>(xo, yo, zo);
const auto to = mrs_lib::convert<Eigen::Vector3d>(from);
const auto to2 = mrs_lib::convert<geometry_msgs::Vector3>(to);
std::cout << "Converted from type " << typeid(decltype(from)).name() << " with value:" << std::endl << from << std::endl;
std::cout << " to type " << typeid(decltype(to)).name() << " with value:" << std::endl << to << std::endl << std::endl;
std::cout << " and then to type " << typeid(decltype(to2)).name() << " with value:" << std::endl << to2 << std::endl;
struct MyPointOK
{
double x, y, z;
};
const auto custom1 = mrs_lib::convert<MyPointOK>(from);
std::cout << " and to new type " << typeid(decltype(custom1)).name() << " with value:" << std::endl << custom1.x << std::endl << custom1.y << std::endl << custom1.z << std::endl << std::endl;
const auto custom2 = mrs_lib::convert<MyPoint>(from);
std::cout << " and to new type " << typeid(decltype(custom2)).name() << " with value:" << std::endl << custom2.e1 << std::endl << custom2.e2 << std::endl << custom2.e3 << std::endl << std::endl;
}