blob: 52db9d552b98387edc1a01fe3f8ff46f4257cacd [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -08006namespace y2019::vision {
Parker Schuha4e52fb2019-02-24 18:18:15 -08007
8namespace {
9// 64 should be enough for any mortal.
10constexpr int kMaxNumCameras = 64;
11constexpr double kInchesToMeters = 0.0254;
12} // namespace
13
14static std::string fmt_rad(double v) {
15 std::stringstream ss;
16 if (v == 0.0) {
17 ss << "0.0";
18 } else {
19 ss << v * 180.0 / M_PI << " / 180.0 * M_PI";
20 }
21 return ss.str();
22}
23
24static std::string fmt_meters(double v) {
25 if (v == 0.0) return "0.0";
26 if (v == 1.0) return "kInchesToMeters";
27 std::stringstream ss;
28 ss << v / kInchesToMeters << " * kInchesToMeters";
29 return ss.str();
30}
31
Austin Schuh813d8d72019-03-03 21:11:53 -080032void IntrinsicParams::Dump(std::basic_ostream<char> *o) const {
Philipp Schrader790cb542023-07-05 21:06:52 -070033 *o << " {\n " << fmt_rad(mount_angle) << ",\n "
34 << focal_length;
35 *o << ",\n " << 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
James Kuszmaule2c71ea2019-03-04 08:14:21 -080054void DumpCameraConstants(const char *fname, int camera_id,
55 const CameraCalibration &value) {
56 std::ofstream o(fname);
Parker Schuha4e52fb2019-02-24 18:18:15 -080057 o << R"(#include "y2019/vision/constants.h"
58
Stephan Pleinesf63bde82024-01-13 15:59:33 -080059namespace y2019::vision {
Parker Schuha4e52fb2019-02-24 18:18:15 -080060
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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080091} // namespace y2019::vision
Parker Schuha4e52fb2019-02-24 18:18:15 -080092)";
93 o.close();
94}
95
Stephan Pleinesf63bde82024-01-13 15:59:33 -080096} // namespace y2019::vision