Different previous velocity calculation

Instead of using the instantaneous velocity as the value for the
v[i-1], use the velocity equivalent of the energy accumulated so
far. This should act as a kind of a low-pass filter, and results
in lower velocity values than the current version of the algorithm.

Bug: 35412046
Test: native and CTS
1) /data/nativetest64/libinput_tests/VelocityTracker_test
This test currently fails, and we may need to modify the
expected velocity values to get it to pass with this version
of the algorithm
2) bit CtsViewTestCases:.VelocityTrackerTest
This test passes

Change-Id: I358cfd8161ec3d423a4f3126c9bcec6de1020490
diff --git a/libs/input/VelocityTracker.cpp b/libs/input/VelocityTracker.cpp
index a914408..dd18c54 100644
--- a/libs/input/VelocityTracker.cpp
+++ b/libs/input/VelocityTracker.cpp
@@ -1080,11 +1080,15 @@
  * Note that approach 2) is sensitive to the proper ordering of the data in time, since
  * the boundary condition must be applied to the oldest sample to be accurate.
  */
+static float kineticEnergyToVelocity(float work) {
+    static constexpr float sqrt2 = 1.41421356237;
+    return (work < 0 ? -1.0 : 1.0) * sqrtf(fabsf(work)) * sqrt2;
+}
+
 static float calculateImpulseVelocity(const nsecs_t* t, const float* x, size_t count) {
     // The input should be in reversed time order (most recent sample at index i=0)
     // t[i] is in nanoseconds, but due to FP arithmetic, convert to seconds inside this function
     static constexpr float NANOS_PER_SECOND = 1E-9;
-    static constexpr float sqrt2 = 1.41421356237;
 
     if (count < 2) {
         return 0; // if 0 or 1 points, velocity is zero
@@ -1101,21 +1105,19 @@
     }
     // Guaranteed to have at least 3 points here
     float work = 0;
-    float vprev, vcurr; // v[i-1] and v[i], respectively
-    vprev = 0;
     for (size_t i = count - 1; i > 0 ; i--) { // start with the oldest sample and go forward in time
         if (t[i] == t[i-1]) {
             ALOGE("Events have identical time stamps t=%" PRId64 ", skipping sample", t[i]);
             continue;
         }
-        vcurr = (x[i] - x[i-1]) / (NANOS_PER_SECOND * (t[i] - t[i-1]));
+        float vprev = kineticEnergyToVelocity(work); // v[i-1]
+        float vcurr = (x[i] - x[i-1]) / (NANOS_PER_SECOND * (t[i] - t[i-1])); // v[i]
         work += (vcurr - vprev) * fabsf(vcurr);
         if (i == count - 1) {
             work *= 0.5; // initial condition, case 2) above
         }
-        vprev = vcurr;
     }
-    return (work < 0 ? -1.0 : 1.0) * sqrtf(fabsf(work)) * sqrt2;
+    return kineticEnergyToVelocity(work);
 }
 
 bool ImpulseVelocityTrackerStrategy::getEstimator(uint32_t id,