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

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: Time stored in microseconds. All velocity logic moved from GesturePoint to VelocityCalculator. 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 : index_(0)
11 , num_valid_entries_(0)
12 , buffer_size_(buffer_size)
13 , x_velocity_(0)
14 , y_velocity_(0) {
15 buffer_ = new Point[buffer_size];
16 }
17
18 VelocityCalculator::~VelocityCalculator() {
19 delete buffer_;
20 }
21
22 void VelocityCalculator::PointSeen(int x, int y, int64 time) {
23 buffer_[index_].x = x;
24 buffer_[index_].y = y;
25 buffer_[index_].time = time;
26
27 index_ = (index_ + 1) % buffer_size_;
28 if (num_valid_entries_ < buffer_size_)
29 ++num_valid_entries_;
30
31 UpdateVelocity();
sadrul 2012/02/02 20:34:01 I kind of liked the way you did it in the first pa
32 }
33
34 float VelocityCalculator::VelocitySquared() const {
35 return x_velocity_ * x_velocity_ + y_velocity_ * y_velocity_;
36 }
37
38 void VelocityCalculator::UpdateVelocity() {
39 // We don't have enough data to make a good estimate of the velocity.
40 if (num_valid_entries_ < buffer_size_)
41 return;
42
43 // Where A_i = A[i] - mean(A)
44 // x velocity = sum_i(x_i * t_i) / sum_i(t_i * t_i)
45 // This is an Ordinary Least Squares Regression
46
47 float mean_x = 0;
48 float mean_y = 0;
49 int64 mean_time = 0;
50
51 for (size_t i = 0; i < buffer_size_; ++i) {
52 mean_x += buffer_[i].x;
53 mean_y += buffer_[i].y;
54 mean_time += buffer_[i].time;
55 }
56
57 mean_x /= buffer_size_;
rjkroege 2012/02/02 20:24:17 compute the reciprocal once and then multiply?
58 mean_y /= buffer_size_;
59
60 // The loss in accuracy due to rounding is insignificant compared to
61 // the error due to the resolution of the timer
62 mean_time /= buffer_size_;
63
64 float xt = 0; // sum_i(x_i * t_i)
65 float yt = 0; // sum_i(y_i * t_i)
66 int64 tt = 0; // sum_i(t_i * t_i)
67
68 int64 t_i;
69
70 for (size_t i = 0; i < buffer_size_; ++i) {
71 t_i = (buffer_[i].time - mean_time);
72 xt += (buffer_[i].x - mean_x) * t_i;
73 yt += (buffer_[i].y - mean_y) * t_i;
74 tt += t_i * t_i;
75 }
76
77 // Convert time from microseconds to seconds
78 x_velocity_ = xt / (tt / 1000000.0f);
79 y_velocity_ = yt / (tt / 1000000.0f);
80 }
81
82 void VelocityCalculator::ClearHistory() {
83 index_ = 0;
84 num_valid_entries_ = 0;
85 x_velocity_ = 0;
86 y_velocity_ = 0;
87 }
88
89 } // namespace aura
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698