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

Unified 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: Cache velocity data in VelocityCalculator. Created 8 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: ui/aura/gestures/velocity_calculator.cc
diff --git a/ui/aura/gestures/velocity_calculator.cc b/ui/aura/gestures/velocity_calculator.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e0e071378c4c915df1263135b2264018b931853e
--- /dev/null
+++ b/ui/aura/gestures/velocity_calculator.cc
@@ -0,0 +1,109 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/aura/gestures/velocity_calculator.h"
+
+namespace aura {
+
+VelocityCalculator::VelocityCalculator(int buffer_size)
+ : index_(0)
+ , num_valid_entries_(0)
sadrul 2012/02/02 22:04:06 Wha. This isn't webkit land! :p
+ , buffer_size_(buffer_size)
+ , x_velocity_(0)
+ , y_velocity_(0)
+ , velocities_stale_(false) {
+ buffer_ = new Point[buffer_size];
+}
+
+VelocityCalculator::~VelocityCalculator() {
+ delete buffer_;
+}
+
+float VelocityCalculator::XVelocity() {
+ if (velocities_stale_)
+ UpdateVelocity();
+ return x_velocity_;
+}
+
+float VelocityCalculator::YVelocity() {
+ if (velocities_stale_)
+ UpdateVelocity();
+ return y_velocity_;
+}
+
+void VelocityCalculator::PointSeen(int x, int y, int64 time) {
+ buffer_[index_].x = x;
+ buffer_[index_].y = y;
+ buffer_[index_].time = time;
+
+ index_ = (index_ + 1) % buffer_size_;
+ if (num_valid_entries_ < buffer_size_)
+ ++num_valid_entries_;
+
+ velocities_stale_ = true;
+}
+
+float VelocityCalculator::VelocitySquared() {
+ if (velocities_stale_)
+ UpdateVelocity();
+ return x_velocity_ * x_velocity_ + y_velocity_ * y_velocity_;
+}
+
+void VelocityCalculator::UpdateVelocity() {
+ // We don't have enough data to make a good estimate of the velocity.
+ if (num_valid_entries_ < buffer_size_)
+ return;
+
+ // Where A_i = A[i] - mean(A)
+ // x velocity = sum_i(x_i * t_i) / sum_i(t_i * t_i)
+ // This is an Ordinary Least Squares Regression
+
+ float mean_x = 0;
+ float mean_y = 0;
+ int64 mean_time = 0;
+
+ for (size_t i = 0; i < buffer_size_; ++i) {
+ mean_x += buffer_[i].x;
+ mean_y += buffer_[i].y;
+ mean_time += buffer_[i].time;
+ }
+
+ // Minimize number of divides
+ const float buffer_size_i = 1.0f / buffer_size_;
+
+ mean_x *= buffer_size_i;
+ mean_y *= buffer_size_i;
+
+ // The loss in accuracy due to rounding is insignificant compared to
+ // the error due to the resolution of the timer
+ mean_time *= buffer_size_i;
+
+ float xt = 0; // sum_i(x_i * t_i)
+ float yt = 0; // sum_i(y_i * t_i)
+ int64 tt = 0; // sum_i(t_i * t_i)
+
+ int64 t_i;
+
+ for (size_t i = 0; i < buffer_size_; ++i) {
+ t_i = (buffer_[i].time - mean_time);
+ xt += (buffer_[i].x - mean_x) * t_i;
+ yt += (buffer_[i].y - mean_y) * t_i;
+ tt += t_i * t_i;
+ }
+
+ // Convert time from microseconds to seconds
+ x_velocity_ = xt / (tt / 1000000.0f);
+ y_velocity_ = yt / (tt / 1000000.0f);
+ velocities_stale_ = false;
+}
+
+void VelocityCalculator::ClearHistory() {
+ index_ = 0;
+ num_valid_entries_ = 0;
+ x_velocity_ = 0;
+ y_velocity_ = 0;
+ velocities_stale_ = false;
+}
+
+} // namespace aura

Powered by Google App Engine
This is Rietveld 408576698