Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 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); |
| 29 | ~LinearPredictor() override; |
| 30 | |
| 31 | const char* GetName() const override; |
| 32 | |
| 33 | // Reset the predictor to initial state. |
| 34 | void Reset() override; |
| 35 | |
| 36 | // Store current input in queue. |
| 37 | void Update(const InputData& new_input) override; |
| 38 | |
| 39 | // Return if there is enough data in the queue to generate prediction. |
| 40 | bool HasPrediction() const override; |
| 41 | |
| 42 | // Generate the prediction based on stored points and given time_stamp. |
| 43 | // Return false if no prediction available. |
Ella Ge | f7afddc | 2019-10-21 23:37:48 | [diff] [blame] | 44 | std::unique_ptr<InputData> GeneratePrediction( |
Joao Victor Almeida | 371702c | 2021-01-08 18:41:56 | [diff] [blame^] | 45 | base::TimeTicks predict_time, |
| 46 | base::TimeDelta frame_interval) override; |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 47 | |
Luis Sanchez Padilla | ca0e4b51 | 2019-07-31 23:16:32 | [diff] [blame] | 48 | // Return the average time delta in the event queue. |
| 49 | base::TimeDelta TimeInterval() const override; |
| 50 | |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 51 | // Return the number of events needed to compute a prediction |
| 52 | size_t NumberOfEventsNeeded(); |
| 53 | |
| 54 | private: |
Ella Ge | f7afddc | 2019-10-21 23:37:48 | [diff] [blame] | 55 | gfx::PointF GeneratePredictionFirstOrder(float pred_dt) const; |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 56 | |
Ella Ge | f7afddc | 2019-10-21 23:37:48 | [diff] [blame] | 57 | gfx::PointF GeneratePredictionSecondOrder(float pred_dt) const; |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 58 | |
| 59 | // Store the last events received |
| 60 | std::deque<InputData> events_queue_; |
| 61 | |
| 62 | // Store the equation order of the predictor |
| 63 | // The enum value also represents the number of events needed to compute the |
| 64 | // prediction |
| 65 | EquationOrder equation_order_; |
| 66 | |
| 67 | // Store the current velocity of the 2 last events |
| 68 | gfx::Vector2dF cur_velocity_; |
| 69 | |
| 70 | // Store the last velocity of the 2 last past events |
| 71 | gfx::Vector2dF last_velocity_; |
| 72 | |
| 73 | // Store the current delta time between the last 2 events |
| 74 | float events_dt_; |
| 75 | |
| 76 | DISALLOW_COPY_AND_ASSIGN(LinearPredictor); |
| 77 | }; |
| 78 | |
Mario Bianucci | 926a2d9 | 2020-07-28 02:04:32 | [diff] [blame] | 79 | } // namespace ui |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 80 | |
Mario Bianucci | 926a2d9 | 2020-07-28 02:04:32 | [diff] [blame] | 81 | #endif // UI_BASE_PREDICTION_LINEAR_PREDICTOR_H_ |