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

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: Minor naming fixes, comments. 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 buffer_.reserve(buffer_size);
14 }
15
16 VelocityCalculator::~VelocityCalculator() {
17 }
18
19 void VelocityCalculator::PointSeen(float x, float y, double time) {
20 buffer_[index_].x = x;
21 buffer_[index_].y = y;
22 buffer_[index_].time = time;
23
24 index_ = (index_ + 1) % buffer_size_;
25 if (num_valid_entries_ < buffer_size_)
26 ++num_valid_entries_;
27 }
28
29 void VelocityCalculator::Velocity(float* x, float* y) {
30 // We don't have enough data to make a good estimate of the velocity.
31 if (num_valid_entries_ < buffer_size_) {
32 *x = 0;
33 *y = 0;
34 return;
35 }
36
37 // Where A_i = A[i] - mean(A)
38 // x velocity = sum_i(x_i * t_i) / sum_i(t_i * t_i)
39 // This is an Ordinary Least Squares Regression
40
41 float mean_x = 0;
42 float mean_y = 0;
43 float mean_time = 0;
44
45 for (size_t i = 0; i < buffer_size_; ++i) {
46 mean_x += buffer_[i].x;
47 mean_y += buffer_[i].y;
48 mean_time += buffer_[i].time;
49 }
50
51 mean_x /= buffer_size_;
52 mean_y /= buffer_size_;
53 mean_time /= buffer_size_;
54
55 float xt = 0; // sum_i(x_i * t_i)
56 float yt = 0; // sum_i(y_i * t_i)
57 float tt = 0; // sum_i(t_i * t_i)
rjkroege 2012/02/02 15:58:37 you are using doubles somewhere else. and here, yo
58
59 for (size_t i = 0; i < buffer_size_; ++i) {
60 xt += (buffer_[i].x - mean_x) * (buffer_[i].time - mean_time);
61 yt += (buffer_[i].y - mean_y) * (buffer_[i].time - mean_time);
62 tt += (buffer_[i].time - mean_time) * (buffer_[i].time - mean_time);
rjkroege 2012/02/02 15:58:37 this algorithm can be numerically unstable. See: h
63 }
64
65 *x = xt / tt;
66 *y = yt / tt;
67 }
68
69 void VelocityCalculator::ClearHistory() {
70 index_ = 0;
71 num_valid_entries_ = 0;
72 }
73
74 } // namespace aura
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698