xref: /aosp_15_r20/frameworks/native/include/input/CoordinateFilter.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /**
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <chrono>
20 
21 #include <input/Input.h>
22 #include <input/OneEuroFilter.h>
23 
24 namespace android {
25 
26 /**
27  * Pair of OneEuroFilters that independently filter X and Y coordinates. Both filters share the same
28  * constructor's parameters. The minimum cutoff frequency is the base cutoff frequency, that is, the
29  * resulting cutoff frequency in the absence of signal's speed. Likewise, beta is a scaling factor
30  * of the signal's speed that sets how much the signal's speed contributes to the resulting cutoff
31  * frequency. The adaptive cutoff frequency criterion is f_c = f_c_min + β|̇x_filtered|
32  */
33 class CoordinateFilter {
34 public:
35     explicit CoordinateFilter(float minCutoffFreq, float beta);
36 
37     /**
38      * Filters in place only the AXIS_X and AXIS_Y fields from coords. Each call to filter must
39      * provide a timestamp strictly greater than the timestamp of the previous call. The first time
40      * this method is invoked no filtering takes place. Subsequent calls do overwrite `coords` with
41      * filtered data.
42      *
43      * @param timestamp The timestamps at which to filter. It must be greater than the one passed in
44      * the previous call.
45      * @param coords Coordinates to be overwritten by the corresponding filtered coordinates.
46      */
47     void filter(std::chrono::nanoseconds timestamp, PointerCoords& coords);
48 
49 private:
50     OneEuroFilter mXFilter;
51     OneEuroFilter mYFilter;
52 };
53 
54 } // namespace android