Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Mario Bianucci | 926a2d9 | 2020-07-28 02:04:32 | [diff] [blame] | 5 | #ifndef UI_BASE_PREDICTION_LINEAR_PREDICTOR_H_ |
| 6 | #define UI_BASE_PREDICTION_LINEAR_PREDICTOR_H_ |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 7 | |
| 8 | #include <deque> |
| 9 | |
Mario Bianucci | 926a2d9 | 2020-07-28 02:04:32 | [diff] [blame] | 10 | #include "base/component_export.h" |
| 11 | #include "ui/base/prediction/input_predictor.h" |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 12 | |
Mario Bianucci | 926a2d9 | 2020-07-28 02:04:32 | [diff] [blame] | 13 | namespace ui { |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 14 | |
| 15 | // This class use a linear model for prediction |
| 16 | // You can choose between a first order equation: |
| 17 | // pred_p = last_p + velocity*pred_time |
| 18 | // and a second order equation: |
| 19 | // pred_p = last_p + velocity*pred_dt + 0.5*acceleration*pred_dt^2 |
| 20 | |
Mario Bianucci | 926a2d9 | 2020-07-28 02:04:32 | [diff] [blame] | 21 | class COMPONENT_EXPORT(UI_BASE_PREDICTION) LinearPredictor |
| 22 | : public InputPredictor { |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 23 | public: |
| 24 | // Used to dissociate the order of the equation used but also used to |
| 25 | // define the number of events needed by each model |
| 26 | enum class EquationOrder : size_t { kFirstOrder = 2, kSecondOrder = 3 }; |
| 27 | |
| 28 | explicit LinearPredictor(EquationOrder order); |
Peter Boström | c8c1235 | 2021-09-21 23:37:15 | [diff] [blame] | 29 | |
| 30 | LinearPredictor(const LinearPredictor&) = delete; |
| 31 | LinearPredictor& operator=(const LinearPredictor&) = delete; |
| 32 | |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 33 | ~LinearPredictor() override; |
| 34 | |
| 35 | const char* GetName() const override; |
| 36 | |
| 37 | // Reset the predictor to initial state. |
| 38 | void Reset() override; |
| 39 | |
| 40 | // Store current input in queue. |
| 41 | void Update(const InputData& new_input) override; |
| 42 | |
| 43 | // Return if there is enough data in the queue to generate prediction. |
| 44 | bool HasPrediction() const override; |
| 45 | |
| 46 | // Generate the prediction based on stored points and given time_stamp. |
| 47 | // Return false if no prediction available. |
Ella Ge | f7afddc | 2019-10-21 23:37:48 | [diff] [blame] | 48 | std::unique_ptr<InputData> GeneratePrediction( |
Joao Victor Almeida | 371702c | 2021-01-08 18:41:56 | [diff] [blame] | 49 | base::TimeTicks predict_time, |
| 50 | base::TimeDelta frame_interval) override; |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 51 | |
Luis Sanchez Padilla | ca0e4b51 | 2019-07-31 23:16:32 | [diff] [blame] | 52 | // Return the average time delta in the event queue. |
| 53 | base::TimeDelta TimeInterval() const override; |
| 54 | |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 55 | // Return the number of events needed to compute a prediction |
| 56 | size_t NumberOfEventsNeeded(); |
| 57 | |
| 58 | private: |
Ella Ge | f7afddc | 2019-10-21 23:37:48 | [diff] [blame] | 59 | gfx::PointF GeneratePredictionFirstOrder(float pred_dt) const; |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 60 | |
Ella Ge | f7afddc | 2019-10-21 23:37:48 | [diff] [blame] | 61 | gfx::PointF GeneratePredictionSecondOrder(float pred_dt) const; |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 62 | |
| 63 | // Store the last events received |
| 64 | std::deque<InputData> events_queue_; |
| 65 | |
| 66 | // Store the equation order of the predictor |
| 67 | // The enum value also represents the number of events needed to compute the |
| 68 | // prediction |
| 69 | EquationOrder equation_order_; |
| 70 | |
| 71 | // Store the current velocity of the 2 last events |
| 72 | gfx::Vector2dF cur_velocity_; |
| 73 | |
| 74 | // Store the last velocity of the 2 last past events |
| 75 | gfx::Vector2dF last_velocity_; |
| 76 | |
| 77 | // Store the current delta time between the last 2 events |
| 78 | float events_dt_; |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 79 | }; |
| 80 | |
Mario Bianucci | 926a2d9 | 2020-07-28 02:04:32 | [diff] [blame] | 81 | } // namespace ui |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 82 | |
Mario Bianucci | 926a2d9 | 2020-07-28 02:04:32 | [diff] [blame] | 83 | #endif // UI_BASE_PREDICTION_LINEAR_PREDICTOR_H_ |