Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 1 | #ifndef FRC971_SHOOTER_INTERPOLATION_INTERPOLATION_H_ |
| 2 | #define FRC971_SHOOTER_INTERPOLATION_INTERPOLATION_H_ |
| 3 | |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 4 | #include <algorithm> |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 5 | #include <utility> |
| 6 | #include <vector> |
| 7 | |
| 8 | namespace frc971 { |
| 9 | namespace shooter_interpolation { |
| 10 | |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 11 | double Blend(double coefficient, double a1, double a2); |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 12 | |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 13 | template <typename YValue> |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 14 | class InterpolationTable { |
| 15 | public: |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 16 | using Point = ::std::pair<double, YValue>; |
Philipp Schrader | 9a83336 | 2017-04-09 22:56:16 +0000 | [diff] [blame] | 17 | InterpolationTable() = default; |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 18 | InterpolationTable(const ::std::vector<Point> &table); |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 19 | |
| 20 | // Uses the interpolation table to calculate the optimal shooter angle and |
| 21 | // power for a shot |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 22 | YValue Get(double x) const; |
| 23 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 24 | bool GetInRange(double x, YValue *type) const; |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 25 | |
| 26 | private: |
| 27 | // Contains the list of angle entries in the interpolation table |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 28 | ::std::vector<Point> table_; |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 31 | template <typename YValue> |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 32 | InterpolationTable<YValue>::InterpolationTable( |
| 33 | const ::std::vector<Point> &table) |
| 34 | : table_(table) { |
| 35 | ::std::sort(table_.begin(), table_.end(), |
| 36 | [](const Point &a, const Point &b) { return a.first < b.first; }); |
| 37 | } |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 38 | |
| 39 | template <typename YValue> |
| 40 | YValue InterpolationTable<YValue>::Get(double x) const { |
| 41 | // Points to to the smallest item such that it->first >= dist, or end() if no |
| 42 | // such item exists. |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 43 | auto it = ::std::lower_bound( |
| 44 | table_.begin(), table_.end(), x, |
| 45 | [](const Point &a, double dist) { return a.first < dist; }); |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 46 | if (it == table_.begin()) { |
| 47 | return it->second; |
| 48 | } else if (it == table_.end()) { |
| 49 | return table_.back().second; |
| 50 | } else { |
| 51 | auto x_a2 = it; |
| 52 | auto x_a1 = it - 1; |
| 53 | double x1 = x_a1->first; |
| 54 | double x2 = x_a2->first; |
| 55 | double coefficient = (x - x1) / (x2 - x1); |
| 56 | return YValue::BlendY(coefficient, x_a1->second, x_a2->second); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | template <typename YValue> |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 61 | bool InterpolationTable<YValue>::GetInRange(double x, YValue *result) const { |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 62 | // Points to to the smallest item such that it->first >= dist, or end() if no |
| 63 | // such item exists. |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 64 | auto it = ::std::lower_bound( |
| 65 | table_.begin(), table_.end(), x, |
| 66 | [](const Point &a, double dist) { return a.first < dist; }); |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 67 | if (it == table_.begin()) { |
| 68 | return false; |
| 69 | } else if (it == table_.end()) { |
| 70 | return false; |
| 71 | } else { |
| 72 | auto x_a2 = it; |
| 73 | auto x_a1 = it - 1; |
| 74 | double x1 = x_a1->first; |
| 75 | double x2 = x_a2->first; |
| 76 | double coefficient = (x - x1) / (x2 - x1); |
| 77 | *result = YValue::BlendY(coefficient, x_a1->second, x_a2->second); |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 | |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 82 | } // namespace shooter_interpolation |
| 83 | } // namespace frc971 |
| 84 | |
| 85 | #endif // FRC971_SHOOTER_INTERPOLATION_INTERPOLATION_H_ |