velocity_tracker.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2014 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. #ifndef UI_EVENTS_GESTURE_DETECTION_VELOCITY_TRACKER_H_
  5. #define UI_EVENTS_GESTURE_DETECTION_VELOCITY_TRACKER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/time/time.h"
  9. #include "ui/events/gesture_detection/bitset_32.h"
  10. namespace ui {
  11. class MotionEvent;
  12. class VelocityTrackerStrategy;
  13. namespace {
  14. struct Estimator;
  15. struct Position;
  16. }
  17. // Port of VelocityTracker from Android
  18. // * platform/frameworks/native/include/input/VelocityTracker.h
  19. // * Change-Id: I4983db61b53e28479fc90d9211fafff68f7f49a6
  20. // * Please update the Change-Id as upstream Android changes are pulled.
  21. class VelocityTracker {
  22. public:
  23. enum {
  24. // The maximum number of pointers to use when computing the velocity.
  25. // Note that the supplied MotionEvent may expose more than 16 pointers, but
  26. // at most |MAX_POINTERS| will be used.
  27. MAX_POINTERS = 16,
  28. };
  29. enum Strategy {
  30. // 1st order least squares. Quality: POOR.
  31. // Frequently underfits the touch data especially when the finger
  32. // accelerates or changes direction. Often underestimates velocity. The
  33. // direction is overly influenced by historical touch points.
  34. LSQ1,
  35. // 2nd order least squares. Quality: VERY GOOD.
  36. // Pretty much ideal, but can be confused by certain kinds of touch data,
  37. // particularly if the panel has a tendency to generate delayed, duplicate
  38. // or jittery touch coordinates when the finger is released. This is the
  39. // default velocity tracker strategy. Although other strategies are
  40. // available for testing and comparison purposes, this is the strategy that
  41. // applications will actually use. Be very careful when adjusting the
  42. // default strategy because it can dramatically affect (often in a bad way)
  43. // the user experience.
  44. LSQ2,
  45. // The same as LSQ2, but reports 0 if the direction of the velocity returned
  46. // is sufficiently different from the primary direction of movement of the
  47. // touches contributing to the velocity.
  48. LSQ2_RESTRICTED,
  49. // 3rd order least squares. Quality: UNUSABLE.
  50. // Frequently overfits the touch data yielding wildly divergent estimates
  51. // of the velocity when the finger is released.
  52. LSQ3,
  53. // 2nd order weighted least squares, delta weighting.
  54. // Quality: EXPERIMENTAL
  55. WLSQ2_DELTA,
  56. // 2nd order weighted least squares, central weighting.
  57. // Quality: EXPERIMENTAL
  58. WLSQ2_CENTRAL,
  59. // 2nd order weighted least squares, recent weighting.
  60. // Quality: EXPERIMENTAL
  61. WLSQ2_RECENT,
  62. // 1st order integrating filter. Quality: GOOD.
  63. // Not as good as 'lsq2' because it cannot estimate acceleration but it is
  64. // more tolerant of errors. Like 'lsq1', this strategy tends to
  65. // underestimate
  66. // the velocity of a fling but this strategy tends to respond to changes in
  67. // direction more quickly and accurately.
  68. INT1,
  69. // 2nd order integrating filter. Quality: EXPERIMENTAL.
  70. // For comparison purposes only. Unlike 'int1' this strategy can compensate
  71. // for acceleration but it typically overestimates the effect.
  72. INT2,
  73. STRATEGY_MAX = INT2,
  74. // The default velocity tracker strategy.
  75. STRATEGY_DEFAULT = LSQ2,
  76. };
  77. // Creates a velocity tracker using the specified strategy.
  78. // If strategy is NULL, uses the default strategy for the platform.
  79. explicit VelocityTracker(Strategy strategy);
  80. VelocityTracker(const VelocityTracker&) = delete;
  81. VelocityTracker& operator=(const VelocityTracker&) = delete;
  82. ~VelocityTracker();
  83. // Resets the velocity tracker state.
  84. void Clear();
  85. // Adds movement information for all pointers in a MotionEvent, including
  86. // historical samples.
  87. void AddMovement(const MotionEvent& event);
  88. // Gets the velocity of the specified pointer id in position units per second.
  89. // Returns false and sets the velocity components to zero if there is
  90. // insufficient movement information for the pointer.
  91. bool GetVelocity(uint32_t id, float* outVx, float* outVy) const;
  92. // Gets the active pointer id, or -1 if none.
  93. inline int32_t GetActivePointerId() const { return active_pointer_id_; }
  94. // Gets a bitset containing all pointer ids from the most recent movement.
  95. inline BitSet32 GetCurrentPointerIdBits() const {
  96. return current_pointer_id_bits_;
  97. }
  98. private:
  99. // Resets the velocity tracker state for specific pointers.
  100. // Call this method when some pointers have changed and may be reusing
  101. // an id that was assigned to a different pointer earlier.
  102. void ClearPointers(BitSet32 id_bits);
  103. // Adds movement information for a set of pointers.
  104. // The id_bits bitfield specifies the pointer ids of the pointers whose
  105. // positions
  106. // are included in the movement.
  107. // The positions array contains position information for each pointer in order
  108. // by
  109. // increasing id. Its size should be equal to the number of one bits in
  110. // id_bits.
  111. void AddMovement(const base::TimeTicks& event_time,
  112. BitSet32 id_bits,
  113. const Position* positions);
  114. // Gets an estimator for the recent movements of the specified pointer id.
  115. // Returns false if the pointer velocity is unknown.
  116. bool GetEstimator(uint32_t id, Estimator* out_estimator) const;
  117. base::TimeTicks last_event_time_;
  118. BitSet32 current_pointer_id_bits_;
  119. int32_t active_pointer_id_;
  120. std::unique_ptr<VelocityTrackerStrategy> strategy_;
  121. };
  122. } // namespace ui
  123. #endif // UI_EVENTS_GESTURE_DETECTION_VELOCITY_TRACKER_H_