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 | |
| 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"; |
| 36 | } |
| 37 | |
| 38 | void CameraGeometry::dump(std::basic_ostream<char> &o) const { |
| 39 | o << "{{{" << fmt_meters(location[0]) << ", " << fmt_meters(location[1]) |
| 40 | << ", " << fmt_meters(location[2]) << "}}," << fmt_rad(heading) << ",},"; |
| 41 | } |
| 42 | |
| 43 | void DatasetInfo::dump(std::basic_ostream<char> &o) const { |
| 44 | o << "{\n" |
| 45 | << camera_id << ", " |
| 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" |
| 52 | << num_images << ",\n}"; |
| 53 | } |
| 54 | |
| 55 | void DumpCameraConstants(int camera_id, const CameraCalibration &value) { |
| 56 | std::ofstream o("y2019/vision/constants.cc"); |
| 57 | o << R"(#include "y2019/vision/constants.h" |
| 58 | |
| 59 | namespace y2019 { |
| 60 | namespace vision { |
| 61 | |
| 62 | static 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"; |
| 71 | params->intrinsics.dump(o); |
| 72 | params->geometry.dump(o); |
| 73 | params->dataset.dump(o); |
| 74 | o << "};\n"; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | o << R"( |
| 79 | const 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) { |
| 84 | o << " case " << i << ": return &camera_" << i << ";\n"; |
| 85 | } |
| 86 | } |
| 87 | o << R"( default: return nullptr; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } // namespace vision |
| 92 | } // namespace y2019 |
| 93 | )"; |
| 94 | o.close(); |
| 95 | } |
| 96 | |
| 97 | } // namespace vision |
| 98 | } // namespace y2019 |