forked from hrnr/m-explore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting_helpers.h
112 lines (95 loc) · 3.29 KB
/
testing_helpers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef TESTING_HELPERS_H_
#define TESTING_HELPERS_H_
#include <nav_msgs/OccupancyGrid.h>
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
#include <opencv2/core/utility.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/imgcodecs.hpp>
#include <random>
const float resolution = 0.05f;
nav_msgs::OccupancyGridConstPtr loadMap(const std::string& filename);
void saveMap(const std::string& filename,
const nav_msgs::OccupancyGridConstPtr& map);
std::tuple<double, double, double> randomAngleTxTy();
tf2::Transform randomTransform();
cv::Mat randomTransformMatrix();
/* map_server is really bad. until there is no replacement I will implement it
* by myself */
template <typename InputIt>
std::vector<nav_msgs::OccupancyGridConstPtr> loadMaps(InputIt filenames_begin,
InputIt filenames_end)
{
std::vector<nav_msgs::OccupancyGridConstPtr> result;
for (InputIt it = filenames_begin; it != filenames_end; ++it) {
result.emplace_back(loadMap(*it));
}
return result;
}
nav_msgs::OccupancyGridConstPtr loadMap(const std::string& filename)
{
cv::Mat lookUpTable(1, 256, CV_8S);
signed char* p = lookUpTable.ptr<signed char>();
p[254] = 0;
p[205] = -1;
p[0] = 100;
cv::Mat img = cv::imread(filename, cv::IMREAD_GRAYSCALE);
if (img.empty()) {
throw std::runtime_error("could not load map");
}
nav_msgs::OccupancyGridPtr grid{new nav_msgs::OccupancyGrid()};
grid->info.width = static_cast<uint>(img.size().width);
grid->info.height = static_cast<uint>(img.size().height);
grid->info.resolution = resolution;
grid->data.resize(static_cast<size_t>(img.size().area()));
cv::Mat grid_view(img.size(), CV_8S,
const_cast<signed char*>(grid->data.data()));
cv::LUT(img, lookUpTable, grid_view);
return grid;
}
void saveMap(const std::string& filename,
const nav_msgs::OccupancyGridConstPtr& map)
{
cv::Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.ptr();
for (int i = 0; i < 255; ++i) {
if (i >= 0 && i < 10)
p[i] = 254;
else
p[i] = 0;
}
p[255] = 205;
cv::Mat img(map->info.height, map->info.width, CV_8S,
const_cast<signed char*>(map->data.data()));
cv::Mat out_img;
cv::LUT(img, lookUpTable, out_img);
cv::imwrite(filename, out_img);
}
std::tuple<double, double, double> randomAngleTxTy()
{
static std::mt19937_64 g(156468754 /*magic*/);
std::uniform_real_distribution<double> rotation_dis(0., 2 * std::acos(-1));
std::uniform_real_distribution<double> translation_dis(-1000, 1000);
return std::tuple<double, double, double>(rotation_dis(g), translation_dis(g),
translation_dis(g));
}
tf2::Transform randomTransform()
{
double angle, tx, ty;
std::tie(angle, tx, ty) = randomAngleTxTy();
tf2::Transform transform;
tf2::Quaternion rotation;
rotation.setEuler(0., 0., angle);
transform.setRotation(rotation);
transform.setOrigin(tf2::Vector3(tx, ty, 0.));
return transform;
}
cv::Mat randomTransformMatrix()
{
double angle, tx, ty;
std::tie(angle, tx, ty) = randomAngleTxTy();
cv::Mat transform =
(cv::Mat_<double>(3, 3) << std::cos(angle), -std::sin(angle), tx,
std::sin(angle), std::cos(angle), ty, 0., 0., 1.);
return transform;
}
#endif // TESTING_HELPERS_H_