Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 1 | #include "y2019/vision/constants.h" |
| 2 | |
| 3 | #include <fstream> |
| 4 | #include <sstream> |
| 5 | |
| 6 | namespace y2019 { |
| 7 | namespace vision { |
| 8 | |
| 9 | namespace { |
| 10 | // 64 should be enough for any mortal. |
| 11 | constexpr int kMaxNumCameras = 64; |
| 12 | constexpr double kInchesToMeters = 0.0254; |
| 13 | } // namespace |
| 14 | |
| 15 | static 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 | |
| 25 | static 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 Schuh | 813d8d7 | 2019-03-03 21:11:53 -0800 | [diff] [blame] | 33 | void 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 Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Austin Schuh | 813d8d7 | 2019-03-03 21:11:53 -0800 | [diff] [blame] | 38 | void 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 Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Austin Schuh | 813d8d7 | 2019-03-03 21:11:53 -0800 | [diff] [blame] | 44 | void 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 Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void DumpCameraConstants(int camera_id, const CameraCalibration &value) { |
| 55 | std::ofstream o("y2019/vision/constants.cc"); |
| 56 | o << R"(#include "y2019/vision/constants.h" |
| 57 | |
| 58 | namespace y2019 { |
| 59 | namespace vision { |
| 60 | |
| 61 | static 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 Schuh | 813d8d7 | 2019-03-03 21:11:53 -0800 | [diff] [blame] | 70 | params->intrinsics.Dump(&o); |
| 71 | params->geometry.Dump(&o); |
| 72 | params->dataset.Dump(&o); |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 73 | o << "};\n"; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | o << R"( |
| 78 | const 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 Schuh | 813d8d7 | 2019-03-03 21:11:53 -0800 | [diff] [blame] | 83 | o << " case " << i << ":\n return &camera_" << i << ";\n"; |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
Austin Schuh | 813d8d7 | 2019-03-03 21:11:53 -0800 | [diff] [blame] | 86 | o << R"( default: |
| 87 | return nullptr; |
Parker Schuh | a4e52fb | 2019-02-24 18:18:15 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | } // namespace vision |
| 92 | } // namespace y2019 |
| 93 | )"; |
| 94 | o.close(); |
| 95 | } |
| 96 | |
| 97 | } // namespace vision |
| 98 | } // namespace y2019 |