geolib2
test_geolib_lrf.cpp
Go to the documentation of this file.
1 // GeoLib
2 #include "geolib/Box.h"
4 
5 // OpenCV
6 #include <opencv2/highgui/highgui.hpp>
7 #include <opencv2/imgproc/imgproc.hpp>
8 
9 #include <math.h>
10 #include <vector>
11 
12 
13 std::vector<cv::Point2i> getObjectCornerPoints(unsigned int x0, unsigned int y0,
14  double x_obj, double y_obj, double L, double W, double obj_yaw,
15  double pix_per_m)
16 {
17  // Due to difference in convention
18  obj_yaw = -obj_yaw;
19 
20  // Return variable
21  std::vector<cv::Point2i> pts_bb(4);
22 
23  // For efficiency purposes
24  double cos_th = std::cos(obj_yaw);
25  double sin_th = std::sin(obj_yaw);
26 
27  // Initial corner points
28  int x1 = -L/2*pix_per_m;
29  int x2 = L/2*pix_per_m;
30  int y1 = -W/2*pix_per_m;
31  int y2 = W/2*pix_per_m;
32 
33  // Compensate for object pose
34  int xt1 = x0 + x_obj*pix_per_m + cos_th*x1+sin_th*y1;
35  int xt2 = x0 + x_obj*pix_per_m + cos_th*x2+sin_th*y1;
36  int xt3 = x0 + x_obj*pix_per_m + cos_th*x2+sin_th*y2;
37  int xt4 = x0 + x_obj*pix_per_m + cos_th*x1+sin_th*y2;
38  int yt1 = y0 - (y_obj*pix_per_m - sin_th*x1+cos_th*y1);
39  int yt2 = y0 - (y_obj*pix_per_m - sin_th*x2+cos_th*y1);
40  int yt3 = y0 - (y_obj*pix_per_m - sin_th*x2+cos_th*y2);
41  int yt4 = y0 - (y_obj*pix_per_m - sin_th*x1+cos_th*y2);
42 
43  // Add points to the vector
44  pts_bb[0] = cv::Point2i(xt1, yt1);
45  pts_bb[1] = cv::Point2i(xt2, yt2);
46  pts_bb[2] = cv::Point2i(xt3, yt3);
47  pts_bb[3] = cv::Point2i(xt4, yt4);
48 
49  return pts_bb;
50 }
51 
52 
53 int main(int argc, char** argv)
54 {
55  // Determine pose
56  double x = 2;
57  double y = 0.5;
58  double yaw = M_PI_2;
59 
60  // Determine bounding box size
61  double W = 1.0;
62  double L = 2.0;
63  double H = 1.0;
64 
65  // LRF
66  unsigned int n_beams = 7;
67  double angle_min = -90.0/180.0*M_PI;
68  double angle_max = 90.0/180.0*M_PI;
69 
70  if (argc >= 2)
71  {
72  for (int i = 1; i<argc; ++i)
73  {
74  if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h")
75  {
76  std::cout << "Usage: test_geolib_lrf [NUM_BEAMS] [X Y YAW] [WIDTH DEPTH]" << std::endl;
77  return 0;
78  }
79  }
80  n_beams = std::atoi(argv[1]);
81  if (argc >= 5)
82  {
83  x = std::atof(argv[2]);
84  y = std::atof(argv[3]);
85  yaw = std::atof(argv[4]);
86  }
87  if (argc >= 7)
88  {
89  W = std::atof(argv[5]);
90  L = std::atof(argv[6]);
91  }
92  }
93 
94  std::cout << "Using " << n_beams << " number of beams" << std::endl;
95 
96  // Geolib box
97  geo::Box obj_bounding_box(geo::Vector3(-L/2, -W/2, -H/2), geo::Vector3(L/2, W/2, H/2));
98 
99  // Geolib pose
100  geo::Pose3D obj_pose(x, y, 0, 0, 0, yaw);
101 
102 
103  // Instantiate laser range finder model
104  geo::LaserRangeFinder lrf_model;
105  lrf_model.setNumBeams(n_beams);
106  lrf_model.setAngleLimits(angle_min, angle_max);
107  lrf_model.setRangeLimits(0.01, 100);
108 
109  // Set render options
111  opt.setMesh(obj_bounding_box.getMesh(), obj_pose);
112 
113  // Render and store ranges in a new vector
114  std::vector<double> ranges;
115  ranges.resize(n_beams, 0);
116  geo::LaserRangeFinder::RenderResult render_result(ranges);
117  lrf_model.render(opt, render_result);
118 
119  // Image and associated settings
120  unsigned int xsize = 501, ysize = 501, pix_per_m = 100;
121  cv::Mat img(ysize, xsize, CV_8UC3, cv::Scalar(255,255,255)); // White background
122  unsigned int x0 = xsize/10, y0 = ysize/2;
123  cv::Scalar clr_render(0,0,255); // red
124  cv::Scalar clr_origin(0,255,0); // green
125 
126  // Draw object
128  contours[0] = getObjectCornerPoints(x0, y0, x, y, L, W, yaw, pix_per_m);
129  cv::fillPoly(img, contours, cv::Scalar(150,150,150));
130 
131  // Draw laser data
132  cv::Point origin(x0, y0);
133  double th = angle_min;
134  // Angle increment
135  double angle_incr = lrf_model.getAngleIncrement();
136 
137  std::cout << "Beam angles and their scan point coordinates: " << std::endl;
138  for (unsigned int i=0; i < render_result.ranges.size(); ++i, th += angle_incr)
139  {
140  double r_render = render_result.ranges[i]*pix_per_m;
141  if (r_render<0.01)
142  r_render = 10.0*pix_per_m; // make sure all beams are drawn
143  cv::Point end_point(x0+r_render*std::cos(th), y0-r_render*std::sin(th));
144  // rendered scan point
145  cv::circle(img, end_point, 2, clr_render, 2);
146  // rendered beam
147  cv::line(img, origin, end_point, cv::Scalar(0,0,0));
148 
149  std::cout << i << ": " << th << std::endl;
150  std::cout << lrf_model.polarTo2D(th, render_result.ranges[i]) << std::endl;
151  }
152  // Draw origin
153  cv::circle(img, origin, 3, clr_origin, 2);
154 
155  // Show image
156  cv::putText(img, "origin", cv::Point(0,20), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, clr_origin);
157  cv::putText(img, "rendered point", cv::Point(0,40), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, clr_render);
158  cv::imshow("Rendered data and object", img);
159  cv::waitKey();
160 
161  return 0;
162 }
163 
geo::LaserRangeFinder::setAngleLimits
void setAngleLimits(double min, double max)
Definition: LaserRangeFinder.cpp:285
std::vector::resize
T resize(T... args)
std::string
main
int main(int argc, char **argv)
Definition: test_geolib_lrf.cpp:53
geo::LaserRangeFinder::setRangeLimits
void setRangeLimits(double min, double max)
Definition: LaserRangeFinder.h:67
std::cos
T cos(T... args)
std::atof
T atof(T... args)
vector
std::vector::size
T size(T... args)
geo::Box
Definition: Box.h:15
geo::Transform3T
Definition: math_types.h:19
geo::Shape::getMesh
virtual const Mesh & getMesh() const
return the mesh defining the shape
Definition: Shape.cpp:425
geo::LaserRangeFinder::render
void render(const geo::LaserRangeFinder::RenderOptions &options, geo::LaserRangeFinder::RenderResult &res) const
Definition: LaserRangeFinder.cpp:142
geo::LaserRangeFinder::polarTo2D
static geo::Vector3 polarTo2D(double angle, double range)
Definition: LaserRangeFinder.cpp:366
std::cout
geo::LaserRangeFinder::RenderResult::ranges
std::vector< double > & ranges
Definition: LaserRangeFinder.h:49
std::atoi
T atoi(T... args)
geo::LaserRangeFinder::setNumBeams
void setNumBeams(uint n)
Definition: LaserRangeFinder.cpp:293
getObjectCornerPoints
std::vector< cv::Point2i > getObjectCornerPoints(unsigned int x0, unsigned int y0, double x_obj, double y_obj, double L, double W, double obj_yaw, double pix_per_m)
Definition: test_geolib_lrf.cpp:13
geo::LaserRangeFinder::RenderOptions::setMesh
void setMesh(const geo::Mesh &mesh, const geo::Pose3D &pose)
Definition: LaserRangeFinder.h:21
geo::Vector3
Definition: matrix.h:12
geo::LaserRangeFinder::getAngleIncrement
double getAngleIncrement() const
Angle increment between two beams.
Definition: LaserRangeFinder.cpp:319
std::sin
T sin(T... args)
geo::LaserRangeFinder
Definition: LaserRangeFinder.h:13
std::endl
T endl(T... args)
geo::LaserRangeFinder::RenderOptions
Definition: LaserRangeFinder.h:17
Box.h
LaserRangeFinder.h
geo::LaserRangeFinder::RenderResult
Definition: LaserRangeFinder.h:36