blob: ebda53f3a331131c709015802ac89278aaa1250c [file] [log] [blame]
Parker Schuha4e52fb2019-02-24 18:18:15 -08001#include <fstream>
2#include <sstream>
3
Philipp Schrader790cb542023-07-05 21:06:52 -07004#include "y2019/vision/constants.h"
5
Parker Schuha4e52fb2019-02-24 18:18:15 -08006namespace 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 {
Philipp Schrader790cb542023-07-05 21:06:52 -070034 *o << " {\n " << fmt_rad(mount_angle) << ",\n "
35 << focal_length;
36 *o << ",\n " << fmt_rad(barrel_mount) << ",\n },\n";
Parker Schuha4e52fb2019-02-24 18:18:15 -080037}
38
Austin Schuh813d8d72019-03-03 21:11:53 -080039void CameraGeometry::Dump(std::basic_ostream<char> *o) const {
40 *o << " {\n {{" << fmt_meters(location[0]) << ", "
41 << fmt_meters(location[1]) << ",\n " << fmt_meters(location[2])
42 << "}},\n " << fmt_rad(heading) << ",\n },\n ";
Parker Schuha4e52fb2019-02-24 18:18:15 -080043}
44
Austin Schuh813d8d72019-03-03 21:11:53 -080045void DatasetInfo::Dump(std::basic_ostream<char> *o) const {
46 *o << "{\n " << camera_id << ",\n "
47 << "{{" << fmt_meters(to_tape_measure_start[0]) << ", "
48 << fmt_meters(to_tape_measure_start[1]) << "}},\n "
49 << "{{" << fmt_meters(tape_measure_direction[0]) << ", "
50 << fmt_meters(tape_measure_direction[1]) << "}},\n "
51 << beginning_tape_measure_reading << ",\n "
52 << "\"" << filename_prefix << "\",\n " << num_images << ",\n }";
Parker Schuha4e52fb2019-02-24 18:18:15 -080053}
54
James Kuszmaule2c71ea2019-03-04 08:14:21 -080055void DumpCameraConstants(const char *fname, int camera_id,
56 const CameraCalibration &value) {
57 std::ofstream o(fname);
Parker Schuha4e52fb2019-02-24 18:18:15 -080058 o << R"(#include "y2019/vision/constants.h"
59
60namespace y2019 {
61namespace vision {
62
63static constexpr double kInchesToMeters = 0.0254;
64)";
65
66 // Go through all the cameras and either use the existing compiled-in
67 // calibration data or the new data which was passed in.
68 for (int i = 0; i < kMaxNumCameras; ++i) {
69 auto *params = (i == camera_id) ? &value : GetCamera(i);
70 if (params) {
71 o << "\nCameraCalibration camera_" << i << " = {\n";
Austin Schuh813d8d72019-03-03 21:11:53 -080072 params->intrinsics.Dump(&o);
73 params->geometry.Dump(&o);
74 params->dataset.Dump(&o);
Parker Schuha4e52fb2019-02-24 18:18:15 -080075 o << "};\n";
76 }
77 }
78
79 o << R"(
80const CameraCalibration *GetCamera(int camera_id) {
81 switch (camera_id) {
82)";
83 for (int i = 0; i < kMaxNumCameras; ++i) {
84 if (i == camera_id || GetCamera(i) != nullptr) {
Austin Schuh813d8d72019-03-03 21:11:53 -080085 o << " case " << i << ":\n return &camera_" << i << ";\n";
Parker Schuha4e52fb2019-02-24 18:18:15 -080086 }
87 }
Austin Schuh813d8d72019-03-03 21:11:53 -080088 o << R"( default:
89 return nullptr;
Parker Schuha4e52fb2019-02-24 18:18:15 -080090 }
91}
92
93} // namespace vision
94} // namespace y2019
95)";
96 o.close();
97}
98
99} // namespace vision
100} // namespace y2019