Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef ESTIMATE_TRANSFORM_H_
00038 #define ESTIMATE_TRANSFORM_H_
00039
00040 #include <vector>
00041 #include <type_traits>
00042 #include <cmath>
00043
00044 #include <ros/console.h>
00045 #include <ros/assert.h>
00046 #include <nav_msgs/OccupancyGrid.h>
00047 #include <geometry_msgs/Pose.h>
00048 #include <tf/transform_datatypes.h>
00049
00050 #include <opencv2/core/utility.hpp>
00051
00052 namespace combine_grids
00053 {
00078 template <typename ForwardIt, typename OutputIt>
00079 size_t estimateGridTransform(ForwardIt grids_begin, ForwardIt grids_end,
00080 OutputIt transforms_begin,
00081 double confidence = 1.0);
00082
00083 namespace internal
00084 {
00100 size_t opencvEstimateTransform(const std::vector<cv::Mat>& images,
00101 std::vector<cv::Mat>& transforms,
00102 double confidence);
00103
00104 }
00105 }
00106
00107
00108
00109 namespace combine_grids
00110 {
00111 template <typename ForwardIt, typename OutputIt>
00112 size_t estimateGridTransform(ForwardIt grids_begin, ForwardIt grids_end,
00113 OutputIt transforms_begin, double confidence)
00114 {
00115 static_assert(std::is_assignable<nav_msgs::OccupancyGrid&,
00116 decltype(*grids_begin)>::value,
00117 "grids_begin must point to nav_msgs::OccupancyGrid data");
00118
00119 static_assert(std::is_assignable<geometry_msgs::Pose&,
00120 decltype(*transforms_begin)>::value,
00121 "transforms_begin must point to geometry_msgs::Pose data");
00122
00123 std::vector<cv::Mat> images;
00124 std::vector<cv::Mat> transforms;
00125
00126 ROS_DEBUG("estimating transformations between grids");
00127
00128
00129
00130 ROS_DEBUG("generating opencv stub images");
00131 images.reserve(std::distance(grids_begin, grids_end));
00132 for (ForwardIt it = grids_begin; it != grids_end; ++it) {
00133 nav_msgs::OccupancyGrid& it_ref = *it;
00134
00135 if (it_ref.data.empty()) {
00136 continue;
00137 }
00138
00139 images.emplace_back(it_ref.info.height, it_ref.info.width, CV_8UC1,
00140 it_ref.data.data());
00141 }
00142
00143 size_t num_estimates =
00144 internal::opencvEstimateTransform(images, transforms, confidence);
00145
00146 auto transform_it = transforms_begin;
00147 auto grid_it = grids_begin;
00148 for (auto& transform : transforms) {
00149 nav_msgs::OccupancyGrid& grid = *grid_it;
00150 geometry_msgs::Pose& output_transform =
00151 *transform_it;
00152 if (transform.empty()) {
00153
00154 output_transform = geometry_msgs::Pose();
00155 } else {
00156 double translation_x = transform.at<double>(0, 2);
00157 double translation_y = transform.at<double>(1, 2);
00158 double rotation_rad =
00159 std::atan2(transform.at<double>(0, 1), transform.at<double>(1, 1));
00160
00161
00162
00163
00164 output_transform.position.x = 1. * translation_x * grid.info.resolution;
00165 output_transform.position.y = 1. * translation_y * grid.info.resolution;
00166 output_transform.position.z = 0;
00167 output_transform.orientation =
00168 tf::createQuaternionMsgFromYaw(1. * rotation_rad);
00169 }
00170 ++grid_it;
00171 ++transform_it;
00172 }
00173
00174 return num_estimates;
00175 }
00176
00177 }
00178
00179 #endif // ESTIMATE_TRANSFORM_H_