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) | |
sadrul
2012/02/02 22:04:06
Wha. This isn't webkit land! :p
| |
12 , buffer_size_(buffer_size) | |
13 , x_velocity_(0) | |
14 , y_velocity_(0) | |
15 , velocities_stale_(false) { | |
16 buffer_ = new Point[buffer_size]; | |
17 } | |
18 | |
19 VelocityCalculator::~VelocityCalculator() { | |
20 delete buffer_; | |
21 } | |
22 | |
23 float VelocityCalculator::XVelocity() { | |
24 if (velocities_stale_) | |
25 UpdateVelocity(); | |
26 return x_velocity_; | |
27 } | |
28 | |
29 float VelocityCalculator::YVelocity() { | |
30 if (velocities_stale_) | |
31 UpdateVelocity(); | |
32 return y_velocity_; | |
33 } | |
34 | |
35 void VelocityCalculator::PointSeen(int x, int y, int64 time) { | |
36 buffer_[index_].x = x; | |
37 buffer_[index_].y = y; | |
38 buffer_[index_].time = time; | |
39 | |
40 index_ = (index_ + 1) % buffer_size_; | |
41 if (num_valid_entries_ < buffer_size_) | |
42 ++num_valid_entries_; | |
43 | |
44 velocities_stale_ = true; | |
45 } | |
46 | |
47 float VelocityCalculator::VelocitySquared() { | |
48 if (velocities_stale_) | |
49 UpdateVelocity(); | |
50 return x_velocity_ * x_velocity_ + y_velocity_ * y_velocity_; | |
51 } | |
52 | |
53 void VelocityCalculator::UpdateVelocity() { | |
54 // We don't have enough data to make a good estimate of the velocity. | |
55 if (num_valid_entries_ < buffer_size_) | |
56 return; | |
57 | |
58 // Where A_i = A[i] - mean(A) | |
59 // x velocity = sum_i(x_i * t_i) / sum_i(t_i * t_i) | |
60 // This is an Ordinary Least Squares Regression | |
61 | |
62 float mean_x = 0; | |
63 float mean_y = 0; | |
64 int64 mean_time = 0; | |
65 | |
66 for (size_t i = 0; i < buffer_size_; ++i) { | |
67 mean_x += buffer_[i].x; | |
68 mean_y += buffer_[i].y; | |
69 mean_time += buffer_[i].time; | |
70 } | |
71 | |
72 // Minimize number of divides | |
73 const float buffer_size_i = 1.0f / buffer_size_; | |
74 | |
75 mean_x *= buffer_size_i; | |
76 mean_y *= buffer_size_i; | |
77 | |
78 // The loss in accuracy due to rounding is insignificant compared to | |
79 // the error due to the resolution of the timer | |
80 mean_time *= buffer_size_i; | |
81 | |
82 float xt = 0; // sum_i(x_i * t_i) | |
83 float yt = 0; // sum_i(y_i * t_i) | |
84 int64 tt = 0; // sum_i(t_i * t_i) | |
85 | |
86 int64 t_i; | |
87 | |
88 for (size_t i = 0; i < buffer_size_; ++i) { | |
89 t_i = (buffer_[i].time - mean_time); | |
90 xt += (buffer_[i].x - mean_x) * t_i; | |
91 yt += (buffer_[i].y - mean_y) * t_i; | |
92 tt += t_i * t_i; | |
93 } | |
94 | |
95 // Convert time from microseconds to seconds | |
96 x_velocity_ = xt / (tt / 1000000.0f); | |
97 y_velocity_ = yt / (tt / 1000000.0f); | |
98 velocities_stale_ = false; | |
99 } | |
100 | |
101 void VelocityCalculator::ClearHistory() { | |
102 index_ = 0; | |
103 num_valid_entries_ = 0; | |
104 x_velocity_ = 0; | |
105 y_velocity_ = 0; | |
106 velocities_stale_ = false; | |
107 } | |
108 | |
109 } // namespace aura | |
OLD | NEW |