xref: /aosp_15_r20/external/libchrome-gestures/include/box_filter_interpreter.h (revision aed3e5085e770be5b69ce25295ecf6ddf906af95)
1 // Copyright 2012 The ChromiumOS Authors
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 <map>
6 #include <memory>
7 #include <gtest/gtest.h>  // for FRIEND_TEST
8 
9 #include "include/filter_interpreter.h"
10 #include "include/finger_metrics.h"
11 #include "include/gestures.h"
12 #include "include/prop_registry.h"
13 #include "include/tracer.h"
14 
15 #ifndef GESTURES_BOX_FILTER_INTERPRETER_H_
16 #define GESTURES_BOX_FILTER_INTERPRETER_H_
17 
18 namespace gestures {
19 
20 // This filter interpreter applies simple "box" algorithm to fingers as they
21 // pass through the filter. The purpose is to filter noisy input.
22 // The algorithm is: For each axis:
23 // - For each input point, compare X distance to previous output point for
24 //   the finger.
25 // - If the X distance is under box_width_ / 2, report the old location.
26 // - Report the new point shifted box_width_ / 2 toward the previous
27 //   output point.
28 // - Apply box_height_ to Y distance.
29 //
30 // The way to think about this is that there is a box around the previous
31 // output point with a width and height of box_width_. If a new point is
32 // within that box, report the old location (don't move the box). If the new
33 // point is outside, shift the box over to include the new point, then report
34 // the new center of the box.
35 
36 class BoxFilterInterpreter : public FilterInterpreter, public PropertyDelegate {
37   FRIEND_TEST(BoxFilterInterpreterTest, SimpleTest);
38   FRIEND_TEST(BoxFilterInterpreterTest, ZeroSizeBoxTest);
39  public:
40   // Takes ownership of |next|:
41   BoxFilterInterpreter(PropRegistry* prop_reg, Interpreter* next,
42                        Tracer* tracer);
~BoxFilterInterpreter()43   virtual ~BoxFilterInterpreter() {}
44 
45  protected:
46   virtual void SyncInterpretImpl(HardwareState& hwstate, stime_t* timeout);
47 
48  private:
49   std::map<short, FingerState> previous_output_;
50 
51   DoubleProperty box_width_;
52   DoubleProperty box_height_;
53 };
54 
55 }  // namespace gestures
56 
57 #endif  // GESTURES_BOX_FILTER_INTERPRETER_H_
58