blob: 272246e750864c6416ba4562d3c0706d23136702 [file] [log] [blame]
Parker Schuha4e52fb2019-02-24 18:18:15 -08001#include "y2019/vision/constants.h"
2
3#include <fstream>
4#include <sstream>
5
6namespace y2019 {
7namespace vision {
8
9namespace {
10// 64 should be enough for any mortal.
11constexpr int kMaxNumCameras = 64;
12constexpr double kInchesToMeters = 0.0254;
13} // namespace
14
15static std::string fmt_rad(double v) {
16 std::stringstream ss;
17 if (v == 0.0) {
18 ss << "0.0";
19 } else {
20 ss << v * 180.0 / M_PI << " / 180.0 * M_PI";
21 }
22 return ss.str();
23}
24
25static std::string fmt_meters(double v) {
26 if (v == 0.0) return "0.0";
27 if (v == 1.0) return "kInchesToMeters";
28 std::stringstream ss;
29 ss << v / kInchesToMeters << " * kInchesToMeters";
30 return ss.str();
31}
32
Austin Schuh813d8d72019-03-03 21:11:53 -080033void IntrinsicParams::Dump(std::basic_ostream<char> *o) const {
34 *o << " {\n " << fmt_rad(mount_angle) << ", " << focal_length;
35 *o << ", " << fmt_rad(barrel_mount) << ",\n },\n";
Parker Schuha4e52fb2019-02-24 18:18:15 -080036}
37
Austin Schuh813d8d72019-03-03 21:11:53 -080038void CameraGeometry::Dump(std::basic_ostream<char> *o) const {
39 *o << " {\n {{" << fmt_meters(location[0]) << ", "
40 << fmt_meters(location[1]) << ",\n " << fmt_meters(location[2])
41 << "}},\n " << fmt_rad(heading) << ",\n },\n ";
Parker Schuha4e52fb2019-02-24 18:18:15 -080042}
43
Austin Schuh813d8d72019-03-03 21:11:53 -080044void DatasetInfo::Dump(std::basic_ostream<char> *o) const {
45 *o << "{\n " << camera_id << ",\n "
46 << "{{" << fmt_meters(to_tape_measure_start[0]) << ", "
47 << fmt_meters(to_tape_measure_start[1]) << "}},\n "
48 << "{{" << fmt_meters(tape_measure_direction[0]) << ", "
49 << fmt_meters(tape_measure_direction[1]) << "}},\n "
50 << beginning_tape_measure_reading << ",\n "
51 << "\"" << filename_prefix << "\",\n " << num_images << ",\n }";
Parker Schuha4e52fb2019-02-24 18:18:15 -080052}
53
54void DumpCameraConstants(int camera_id, const CameraCalibration &value) {
55 std::ofstream o("y2019/vision/constants.cc");
56 o << R"(#include "y2019/vision/constants.h"
57
58namespace y2019 {
59namespace vision {
60
61static constexpr double kInchesToMeters = 0.0254;
62)";
63
64 // Go through all the cameras and either use the existing compiled-in
65 // calibration data or the new data which was passed in.
66 for (int i = 0; i < kMaxNumCameras; ++i) {
67 auto *params = (i == camera_id) ? &value : GetCamera(i);
68 if (params) {
69 o << "\nCameraCalibration camera_" << i << " = {\n";
Austin Schuh813d8d72019-03-03 21:11:53 -080070 params->intrinsics.Dump(&o);
71 params->geometry.Dump(&o);
72 params->dataset.Dump(&o);
Parker Schuha4e52fb2019-02-24 18:18:15 -080073 o << "};\n";
74 }
75 }
76
77 o << R"(
78const CameraCalibration *GetCamera(int camera_id) {
79 switch (camera_id) {
80)";
81 for (int i = 0; i < kMaxNumCameras; ++i) {
82 if (i == camera_id || GetCamera(i) != nullptr) {
Austin Schuh813d8d72019-03-03 21:11:53 -080083 o << " case " << i << ":\n return &camera_" << i << ";\n";
Parker Schuha4e52fb2019-02-24 18:18:15 -080084 }
85 }
Austin Schuh813d8d72019-03-03 21:11:53 -080086 o << R"( default:
87 return nullptr;
Parker Schuha4e52fb2019-02-24 18:18:15 -080088 }
89}
90
91} // namespace vision
92} // namespace y2019
93)";
94 o.close();
95}
96
97} // namespace vision
98} // namespace y2019