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