Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: ui/aura/gestures/velocity_calculator.cc

Issue 9310031: Event smoothing in CrOS gesture recognizer. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Style tweaks, switch to scoped_array. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "ui/aura/gestures/velocity_calculator.h"
6
7 namespace aura {
8
9 VelocityCalculator::VelocityCalculator(int buffer_size)
10 : buffer_(new Point[buffer_size]) ,
11 index_(0),
12 num_valid_entries_(0),
13 buffer_size_(buffer_size),
14 x_velocity_(0),
15 y_velocity_(0),
16 velocities_stale_(false) {
17 }
18
19 float VelocityCalculator::XVelocity() {
20 if (velocities_stale_)
21 UpdateVelocity();
22 return x_velocity_;
23 }
24
25 float VelocityCalculator::YVelocity() {
26 if (velocities_stale_)
27 UpdateVelocity();
28 return y_velocity_;
29 }
30
31 void VelocityCalculator::PointSeen(int x, int y, int64 time) {
32 buffer_[index_].x = x;
33 buffer_[index_].y = y;
34 buffer_[index_].time = time;
35
36 index_ = (index_ + 1) % buffer_size_;
37 if (num_valid_entries_ < buffer_size_)
38 ++num_valid_entries_;
39
40 velocities_stale_ = true;
41 }
42
43 float VelocityCalculator::VelocitySquared() {
44 if (velocities_stale_)
45 UpdateVelocity();
46 return x_velocity_ * x_velocity_ + y_velocity_ * y_velocity_;
47 }
48
49 void VelocityCalculator::UpdateVelocity() {
50 // We don't have enough data to make a good estimate of the velocity.
51 if (num_valid_entries_ < buffer_size_)
52 return;
53
54 // Where A_i = A[i] - mean(A)
55 // x velocity = sum_i(x_i * t_i) / sum_i(t_i * t_i)
56 // This is an Ordinary Least Squares Regression
57
58 float mean_x = 0;
59 float mean_y = 0;
60 int64 mean_time = 0;
61
62 for (size_t i = 0; i < buffer_size_; ++i) {
63 mean_x += buffer_[i].x;
64 mean_y += buffer_[i].y;
65 mean_time += buffer_[i].time;
66 }
67
68 // Minimize number of divides
69 const float buffer_size_i = 1.0f / buffer_size_;
70
71 mean_x *= buffer_size_i;
72 mean_y *= buffer_size_i;
73
74 // The loss in accuracy due to rounding is insignificant compared to
75 // the error due to the resolution of the timer
76 mean_time *= buffer_size_i;
Ian Vollick 2012/02/02 23:31:10 Will this not create a floating point value that g
77
78 float xt = 0; // sum_i(x_i * t_i)
79 float yt = 0; // sum_i(y_i * t_i)
80 int64 tt = 0; // sum_i(t_i * t_i)
81
82 int64 t_i;
83
84 for (size_t i = 0; i < buffer_size_; ++i) {
85 t_i = (buffer_[i].time - mean_time);
86 xt += (buffer_[i].x - mean_x) * t_i;
87 yt += (buffer_[i].y - mean_y) * t_i;
88 tt += t_i * t_i;
89 }
90
91 // Convert time from microseconds to seconds
92 x_velocity_ = xt / (tt / 1000000.0f);
93 y_velocity_ = yt / (tt / 1000000.0f);
94 velocities_stale_ = false;
95 }
96
97 void VelocityCalculator::ClearHistory() {
98 index_ = 0;
99 num_valid_entries_ = 0;
100 x_velocity_ = 0;
101 y_velocity_ = 0;
102 velocities_stale_ = false;
103 }
104
105 } // namespace aura
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698