blob: a2922a0d0f1d9a656b642a703ab96ddf545e634d [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
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
59namespace y2019 {
60namespace vision {
61
62static constexpr double kInchesToMeters = 0.0254;
63)";
64
65 // Go through all the cameras and either use the existing compiled-in
66 // calibration data or the new data which was passed in.
67 for (int i = 0; i < kMaxNumCameras; ++i) {
68 auto *params = (i == camera_id) ? &value : GetCamera(i);
69 if (params) {
70 o << "\nCameraCalibration camera_" << i << " = {\n";
Austin Schuh813d8d72019-03-03 21:11:53 -080071 params->intrinsics.Dump(&o);
72 params->geometry.Dump(&o);
73 params->dataset.Dump(&o);
Parker Schuha4e52fb2019-02-24 18:18:15 -080074 o << "};\n";
75 }
76 }
77
78 o << R"(
79const CameraCalibration *GetCamera(int camera_id) {
80 switch (camera_id) {
81)";
82 for (int i = 0; i < kMaxNumCameras; ++i) {
83 if (i == camera_id || GetCamera(i) != nullptr) {
Austin Schuh813d8d72019-03-03 21:11:53 -080084 o << " case " << i << ":\n return &camera_" << i << ";\n";
Parker Schuha4e52fb2019-02-24 18:18:15 -080085 }
86 }
Austin Schuh813d8d72019-03-03 21:11:53 -080087 o << R"( default:
88 return nullptr;
Parker Schuha4e52fb2019-02-24 18:18:15 -080089 }
90}
91
92} // namespace vision
93} // namespace y2019
94)";
95 o.close();
96}
97
98} // namespace vision
99} // namespace y2019