Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
| OLD | NEW |