File: linear_predictor.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (129 lines) | stat: -rw-r--r-- 4,555 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/prediction/linear_predictor.h"

#include <algorithm>

#include "ui/base/ui_base_features.h"

namespace ui {

LinearPredictor::LinearPredictor(EquationOrder order) {
  equation_order_ = order;
}

LinearPredictor::~LinearPredictor() {}

const char* LinearPredictor::GetName() const {
  return equation_order_ == EquationOrder::kFirstOrder
             ? features::kPredictorNameLinearFirst
             : features::kPredictorNameLinearSecond;
}

void LinearPredictor::Reset() {
  events_queue_.clear();
}

size_t LinearPredictor::NumberOfEventsNeeded() {
  return static_cast<size_t>(equation_order_);
}

void LinearPredictor::Update(const InputData& new_input) {
  // The last input received is at least kMaxDeltaTime away, we consider it
  // is a new trajectory
  if (!events_queue_.empty() &&
      new_input.time_stamp - events_queue_.back().time_stamp > kMaxTimeDelta) {
    Reset();
  }

  // Queue the new event and keep only the number of last events needed
  events_queue_.push_back(new_input);
  if (events_queue_.size() > static_cast<size_t>(equation_order_)) {
    events_queue_.pop_front();
  }

  // Compute the current velocity
  if (events_queue_.size() >= static_cast<size_t>(EquationOrder::kFirstOrder)) {
    // Even if cur_velocity is empty the first time, last_velocity is only
    // used when 3 events are in the queue, so it will be initialized
    last_velocity_.set_x(cur_velocity_.x());
    last_velocity_.set_y(cur_velocity_.y());

    // We have at least 2 events to compute the current velocity
    // Get delta time between the last 2 events
    // Note: this delta time is also used to compute the acceleration term
    events_dt_ = (events_queue_.at(events_queue_.size() - 1).time_stamp -
                  events_queue_.at(events_queue_.size() - 2).time_stamp)
                     .InMillisecondsF();

    // Get delta pos between the last 2 events
    gfx::Vector2dF delta_pos = events_queue_.at(events_queue_.size() - 1).pos -
                               events_queue_.at(events_queue_.size() - 2).pos;

    // Get the velocity
    if (events_dt_ > 0) {
      cur_velocity_.set_x(ScaleVector2d(delta_pos, 1.0 / events_dt_).x());
      cur_velocity_.set_y(ScaleVector2d(delta_pos, 1.0 / events_dt_).y());
    } else {
      cur_velocity_.set_x(0);
      cur_velocity_.set_y(0);
    }
  }
}

bool LinearPredictor::HasPrediction() const {
  // Even if the current equation is second order, we still can predict a
  // first order
  return events_queue_.size() >=
         static_cast<size_t>(EquationOrder::kFirstOrder);
}

std::unique_ptr<InputPredictor::InputData> LinearPredictor::GeneratePrediction(
    base::TimeTicks predict_time,
    base::TimeDelta frame_interval) {
  if (!HasPrediction())
    return nullptr;

  float pred_dt =
      (predict_time - events_queue_.back().time_stamp).InMillisecondsF();

  // Compute the prediction
  // Note : a first order prediction is computed when only 2 events are
  // available in the second order predictor
  if (equation_order_ == EquationOrder::kSecondOrder && events_dt_ > 0 &&
      events_queue_.size() ==
          static_cast<size_t>(EquationOrder::kSecondOrder)) {
    // Add the acceleration term to the current result
    return std::make_unique<InputData>(GeneratePredictionSecondOrder(pred_dt),
                                       predict_time);
  }
  return std::make_unique<InputData>(GeneratePredictionFirstOrder(pred_dt),
                                     predict_time);
}

gfx::PointF LinearPredictor::GeneratePredictionFirstOrder(float pred_dt) const {
  return events_queue_.back().pos + ScaleVector2d(cur_velocity_, pred_dt);
}

gfx::PointF LinearPredictor::GeneratePredictionSecondOrder(
    float pred_dt) const {
  DCHECK(equation_order_ == EquationOrder::kSecondOrder);

  gfx::Vector2dF acceleration =
      ScaleVector2d(cur_velocity_ - last_velocity_, 1.0 / events_dt_);
  return events_queue_.back().pos + ScaleVector2d(cur_velocity_, pred_dt) +
         ScaleVector2d(acceleration, 0.5 * pred_dt * pred_dt);
}

base::TimeDelta LinearPredictor::TimeInterval() const {
  if (events_queue_.size() > 1) {
    return std::max(kMinTimeInterval, (events_queue_.back().time_stamp -
                                       events_queue_.front().time_stamp) /
                                          (events_queue_.size() - 1));
  }
  return kTimeInterval;
}

}  // namespace ui