blob: e2ed92ef92b7293698bae102e2418ed9ed8ce4a8 [file] [log] [blame]
Brian Silverman2ccf8c52016-03-15 00:22:26 -04001#ifndef Y2016_VISION_STEREO_GEOMETRY_H_
2#define Y2016_VISION_STEREO_GEOMETRY_H_
3
4#include <string>
5
John Park33858a32018-09-28 23:05:48 -07006#include "aos/logging/logging.h"
Parker Schuh2cd173d2017-01-28 00:12:01 -08007#include "aos/vision/math/vector.h"
Brian Silverman2ccf8c52016-03-15 00:22:26 -04008#include "y2016/vision/calibration.pb.h"
9
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080010namespace y2016::vision {
Brian Silverman2ccf8c52016-03-15 00:22:26 -040011
12// Returns the contents of the calibration file which are embedded into the
13// code.
14CalibrationFile EmbeddedCalibrationFile();
15
16// Returns the embedded Calibration for the given robot_name or LOG(FATAL)s.
17Calibration FindCalibrationForRobotOrDie(
18 const ::std::string &robot_name, const CalibrationFile &calibration_file);
19
20class StereoGeometry {
21 public:
22 StereoGeometry(const ::std::string &robot_name)
23 : calibration_(FindCalibrationForRobotOrDie(robot_name,
24 EmbeddedCalibrationFile())) {}
25
26 // Returns the forward world distance in meters corresponding to the given
27 // distance in pixels between the two cameras.
28 double PixelToWorldDistance(double pixel_distance) {
29 return (calibration_.center_center_dist() *
30 calibration_.camera_image_width() * calibration_.focal_length() /
31 (pixel_distance + calibration_.center_center_skew())) +
32 calibration_.measure_camera_offset();
33 }
34
35 // Converts locations on both cameras to the distance between the locations
36 // and both angles.
37 // Returns (via pointers) the distance to the target in meters, the horizontal
38 // angle to the target in radians, and the vertical angle to the target in
39 // radians.
40 // TODO(Brian): Figure out which way is positive on the angles.
41 void Process(const ::aos::vision::Vector<2> &center0,
42 const ::aos::vision::Vector<2> &center1, double *distance,
43 double *horizontal_angle, double *vertical_angle) {
44 *distance = PixelToWorldDistance(center1.x() - center0.x());
45 const double average_x = (center0.x() + center1.x()) / 2.0 -
46 calibration_.camera_image_width() / 2.0;
47 const double average_y = (center0.y() + center1.y()) / 2.0 -
48 calibration_.camera_image_height() / 2.0;
49
50 *horizontal_angle =
51 std::atan2(average_x, calibration_.camera_image_width() *
52 calibration_.focal_length());
53 *vertical_angle = std::atan2(average_y, calibration_.camera_image_width() *
54 calibration_.focal_length());
55 }
56
57 const Calibration &calibration() const { return calibration_; }
58
59 private:
60 Calibration calibration_;
61};
62
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080063} // namespace y2016::vision
Brian Silverman2ccf8c52016-03-15 00:22:26 -040064
65#endif // Y2016_VISION_STEREO_GEOMETRY_H_