1 // Copyright (c) 2012 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.
5 #include "ui/events/gestures/velocity_calculator.h"
9 VelocityCalculator::VelocityCalculator(int buffer_size
)
10 : buffer_(new Point
[buffer_size
]) ,
12 num_valid_entries_(0),
13 buffer_size_(buffer_size
),
16 velocities_stale_(false) {
19 VelocityCalculator::~VelocityCalculator() {}
21 float VelocityCalculator::XVelocity() {
22 if (velocities_stale_
)
27 float VelocityCalculator::YVelocity() {
28 if (velocities_stale_
)
33 void VelocityCalculator::PointSeen(float x
, float y
, int64 time
) {
34 buffer_
[index_
].x
= x
;
35 buffer_
[index_
].y
= y
;
36 buffer_
[index_
].time
= time
;
38 index_
= (index_
+ 1) % buffer_size_
;
39 if (num_valid_entries_
< buffer_size_
)
42 velocities_stale_
= true;
45 float VelocityCalculator::VelocitySquared() {
46 if (velocities_stale_
)
48 return x_velocity_
* x_velocity_
+ y_velocity_
* y_velocity_
;
51 void VelocityCalculator::UpdateVelocity() {
52 // We don't have enough data to make a good estimate of the velocity.
53 if (num_valid_entries_
< 2)
56 // Where A_i = A[i] - mean(A)
57 // x velocity = sum_i(x_i * t_i) / sum_i(t_i * t_i)
58 // This is an Ordinary Least Squares Regression.
64 for (size_t i
= 0; i
< num_valid_entries_
; ++i
) {
65 mean_x
+= buffer_
[i
].x
;
66 mean_y
+= buffer_
[i
].y
;
67 mean_time
+= buffer_
[i
].time
;
70 // Minimize number of divides.
71 const float num_valid_entries_i
= 1.0f
/ num_valid_entries_
;
73 mean_x
*= num_valid_entries_i
;
74 mean_y
*= num_valid_entries_i
;
76 // The loss in accuracy due to rounding is insignificant compared to
77 // the error due to the resolution of the timer.
78 // Use integer division to avoid the cast to double, which would cause
79 // VelocityCalculatorTest.IsAccurateWithLargeTimes to fail.
80 mean_time
/= num_valid_entries_
;
82 float xt
= 0; // sum_i(x_i * t_i)
83 float yt
= 0; // sum_i(y_i * t_i)
84 int64 tt
= 0; // sum_i(t_i * t_i)
88 for (size_t i
= 0; i
< num_valid_entries_
; ++i
) {
89 t_i
= (buffer_
[i
].time
- mean_time
);
90 xt
+= (buffer_
[i
].x
- mean_x
) * t_i
;
91 yt
+= (buffer_
[i
].y
- mean_y
) * t_i
;
96 // Convert time from microseconds to seconds.
97 x_velocity_
= xt
/ (tt
/ 1000000.0f
);
98 y_velocity_
= yt
/ (tt
/ 1000000.0f
);
103 velocities_stale_
= false;
106 void VelocityCalculator::ClearHistory() {
108 num_valid_entries_
= 0;
111 velocities_stale_
= false;