ed_sensor_integration
fitter_viz.h
Go to the documentation of this file.
1 #ifndef ED_SENSOR_INTEGRATION_TOOLS_FITTER_VIZ_H_
2 #define ED_SENSOR_INTEGRATION_TOOLS_FITTER_VIZ_H_
3 
4 #include <ed/entity.h>
5 #include <ed/world_model.h>
6 
7 #include <ed/kinect/fitter.h>
8 
9 #include <geolib/math_types.h>
10 
11 #include <opencv2/highgui/highgui.hpp>
12 
13 
14 // visualization parameters
15 int canvas_width = 800; // pixels
16 int canvas_height = 600; // pixels
17 float canvas_resolution = 100; // pixels per meter
18 
20 int sensor_y = canvas_height * 8/10;
21 cv::Point sensorlocation(sensor_x, sensor_y);
22 
23 cv::Scalar sensor_colour(0, 255, 0); // green
24 cv::Scalar entity_colour(0, 255, 255); // yellow
25 cv::Scalar fitted_colour(243, 192, 15); // blue
26 cv::Scalar measurement_colour(161, 17, 187); // purple
27 
39 void drawLine(const cv::Mat& canvas, geo::Vec2 point1, geo::Vec2 point2, geo::Transform2 pose, float resolution, int origin_x, int origin_y, cv::Scalar colour)
40 {
41  // computing points relative to the pose (in meters)
42  geo::Vec2 rel_point1 = pose * point1;
43  geo::Vec2 rel_point2 = pose * point2;
44 
45  // position to pixels
46  int x_p1 = origin_x + floor(rel_point1.x * resolution);
47  int y_p1 = origin_y - floor(rel_point1.y * resolution);
48  int x_p2 = origin_x + floor(rel_point2.x * resolution);
49  int y_p2 = origin_y - floor(rel_point2.y * resolution);
50 
51  if (x_p1 < 0 || x_p2 < 0|| x_p1 >= canvas.cols || x_p2 >= canvas.cols)
52  {
53  std::cout << "Entity: x-coordinate out of range" << std::endl;
54  return;
55  }
56  if (y_p1 < 0 || y_p2 < 0|| y_p1 >= canvas.rows || y_p2 >= canvas.rows)
57  {
58  std::cout << "Entity: y-coordinate out of range" << std::endl;
59  return;
60  }
61 
62  // paint to screen
63  cv::Point point1_p(x_p1, y_p1);
64  cv::Point point2_p(x_p2, y_p2);
65  cv::line(canvas, point1_p, point2_p, colour, 1);
66 }
67 
78 void drawShape2D(const cv::Mat& canvas, const Shape2D& shape, geo::Transform2 pose, float resolution, int origin_x, int origin_y, cv::Scalar colour)
79 {
80  for (unsigned int i=0; i < shape.size(); i++)
81  {
82  for (unsigned int j=0; j < shape[i].size()-1; j++)
83  {
84  drawLine(canvas, shape[i][j], shape[i][j+1], pose, resolution, origin_x, origin_y, colour);
85  }
86  drawLine(canvas, shape[i][0], shape[i][shape[0].size()-1], pose, resolution, origin_x, origin_y, colour);
87  }
88 
89  // paint entity center
90 
91  int x_p_ent = origin_x + floor(pose.t.x * resolution);
92  int y_p_ent = origin_y - floor(pose.t.y * resolution);
93 
94  cv::Point Entity_center(x_p_ent, y_p_ent);
95  cv::circle(canvas, Entity_center, 3, colour, cv::FILLED);
96 }
97 
115  geo::Transform2 fitted_pose, FitterData fitterdata, bool estimateEntityPose)
116 {
117  // visualize fitting
118  cv::Mat canvas = cv::Mat(canvas_height, canvas_width, CV_8UC3, cv::Scalar(0, 0, 0));
119 
120  // paint to screen the location of the sensor
121  cv::circle(canvas, sensorlocation, 3, sensor_colour, cv::FILLED);
122 
123  // paint entity (from worldmodel)
124  geo::Transform2 relpose = sensor_pose.inverse() * entity_pose;
126 
127  if (estimateEntityPose)
128  {
129  // paint fitted entity
130  geo::Transform2 fitted_relpose = sensor_pose.inverse() * fitted_pose;
131  drawShape2D(canvas, entity.shape_2d, fitted_relpose, canvas_resolution, sensor_x, sensor_y, fitted_colour);
132  }
133  else
134  {
135  std::cout << "Fitted entity not printed in image because of a fitter error" << std::endl;
136  }
137 
138  // paint sensor_ranges
139  uint nranges = fitterdata.sensor_ranges.size();
140  uint half_nranges = nranges/2;
141  float fx = fitterdata.fx;
142  for (unsigned int i = 0; i < nranges; ++i)
143  {
144  float x_m = fitterdata.sensor_ranges[i] * ((static_cast< float >(i) - half_nranges) / fx);
145  float y_m = fitterdata.sensor_ranges[i];
146 
147  // postion to pixels
148  int x_p = sensor_x + floor(x_m * canvas_resolution);
149  int y_p = sensor_y - floor(y_m * canvas_resolution);
150 
151  if (x_p < 0 || x_p >= canvas_width || y_p < 0 || y_p >= canvas_height)
152  continue;
153  if (fitterdata.sensor_ranges[i] == 0) // filter out sensor_ranges equal to zero
154  continue;
155 
156  // paint to screen
157  cv::Point centerCircle(x_p, y_p);
158  cv::circle(canvas, centerCircle, 2, measurement_colour, cv::FILLED);
159  }
160 
161  return canvas;
162 }
163 
164 #endif // ED_SENSOR_INTEGRATION_TOOLS_FITTER_VIZ_H_
entity_colour
cv::Scalar entity_colour(0, 255, 255)
geo::Vec2T::y
T y
std::vector
FitterData::fx
float fx
Definition: fitter.h:71
std::vector::size
T size(T... args)
geo::Vec2T::x
T x
EntityRepresentation2D::shape_2d
Shape2D shape_2d
Definition: fitter.h:58
canvas_height
int canvas_height
Definition: fitter_viz.h:16
FitterData
The FitterData struct contains the downprojected (hence 2D) sensor readings as well as the sensor pos...
Definition: fitter.h:68
EntityRepresentation2D
The EntityRepresentation2D struct contains the (downprojected) shape that is used for the fitting.
Definition: fitter.h:55
geo::Transform2T::t
Vec2T< T > t
std::cout
drawShape2D
void drawShape2D(const cv::Mat &canvas, const Shape2D &shape, geo::Transform2 pose, float resolution, int origin_x, int origin_y, cv::Scalar colour)
drawShape2D draw a 2D shape defined in 2D metric space on a canvas.
Definition: fitter_viz.h:78
canvas_width
int canvas_width
Definition: fitter_viz.h:15
sensorlocation
cv::Point sensorlocation(sensor_x, sensor_y)
geo::Transform2T
sensor_colour
cv::Scalar sensor_colour(0, 255, 0)
sensor_x
int sensor_x
Definition: fitter_viz.h:19
drawLine
void drawLine(const cv::Mat &canvas, geo::Vec2 point1, geo::Vec2 point2, geo::Transform2 pose, float resolution, int origin_x, int origin_y, cv::Scalar colour)
drawLine, draw a line in 2D metric space on a canvas
Definition: fitter_viz.h:39
visualizeFitting
cv::Mat visualizeFitting(EntityRepresentation2D entity, geo::Transform2 sensor_pose, geo::Transform2 entity_pose, geo::Transform2 fitted_pose, FitterData fitterdata, bool estimateEntityPose)
Definition: fitter_viz.h:114
world_model.h
std::endl
T endl(T... args)
fitter.h
canvas_resolution
float canvas_resolution
Definition: fitter_viz.h:17
math_types.h
entity.h
sensor_y
int sensor_y
Definition: fitter_viz.h:20
geo::Transform2T::inverse
Transform2T inverse() const
measurement_colour
cv::Scalar measurement_colour(161, 17, 187)
geo::Vec2T
FitterData::sensor_ranges
std::vector< double > sensor_ranges
Definition: fitter.h:70
fitted_colour
cv::Scalar fitted_colour(243, 192, 15)