1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_VELOCITY_TRACKER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_VELOCITY_TRACKER_H
18 
19 #include "base/geometry/axis.h"
20 #include "base/geometry/least_square_impl.h"
21 #include "base/geometry/offset.h"
22 #include "core/event/touch_event.h"
23 #include "core/gestures/velocity.h"
24 
25 namespace OHOS::Ace {
26 
27 class VelocityTracker final {
28 public:
29     VelocityTracker() = default;
VelocityTracker(Axis mainAxis)30     explicit VelocityTracker(Axis mainAxis) : mainAxis_(mainAxis) {}
31     ~VelocityTracker() = default;
32 
33     static constexpr int32_t LEAST_SQUARE_PARAM_NUM = 3;
34     static constexpr int32_t POINT_NUMBER = 5;
35 
Reset()36     void Reset()
37     {
38         lastPosition_.Reset();
39         velocity_.Reset();
40         delta_.Reset();
41         isFirstPoint_ = true;
42         xAxis_.Reset();
43         yAxis_.Reset();
44     }
45 
46     void UpdateTouchPoint(const TouchEvent& event, bool end = false);
47 
48     void UpdateTrackerPoint(double x, double y, const TimeStamp& time, bool end = false);
49 
GetFirstTrackPoint()50     const TouchEvent& GetFirstTrackPoint() const
51     {
52         return firstTrackPoint_;
53     }
54 
GetCurrentTrackPoint()55     const TouchEvent& GetCurrentTrackPoint() const
56     {
57         return currentTrackPoint_;
58     }
59 
GetPosition()60     const Offset& GetPosition() const
61     {
62         return lastPosition_;
63     }
64 
GetDelta()65     const Offset& GetDelta() const
66     {
67         return delta_;
68     }
69 
GetVelocity()70     const Velocity& GetVelocity()
71     {
72         UpdateVelocity();
73         return velocity_;
74     }
75 
SetMainAxis(Axis axis)76     void SetMainAxis(Axis axis)
77     {
78         mainAxis_ = axis;
79     }
80 
GetMainAxisPos()81     double GetMainAxisPos() const
82     {
83         switch (mainAxis_) {
84             case Axis::FREE:
85                 return lastPosition_.GetDistance();
86             case Axis::HORIZONTAL:
87                 return lastPosition_.GetX();
88             case Axis::VERTICAL:
89                 return lastPosition_.GetY();
90             default:
91                 return 0.0;
92         }
93     }
94 
GetMainAxisDeltaPos()95     double GetMainAxisDeltaPos() const
96     {
97         switch (mainAxis_) {
98             case Axis::FREE:
99                 return delta_.GetDistance();
100             case Axis::HORIZONTAL:
101                 return delta_.GetX();
102             case Axis::VERTICAL:
103                 return delta_.GetY();
104             default:
105                 return 0.0;
106         }
107     }
108 
GetMainAxisVelocity()109     double GetMainAxisVelocity()
110     {
111         UpdateVelocity();
112         switch (mainAxis_) {
113             case Axis::FREE:
114                 return velocity_.GetVelocityValue();
115             case Axis::HORIZONTAL:
116                 return velocity_.GetVelocityX();
117             case Axis::VERTICAL:
118                 return velocity_.GetVelocityY();
119             default:
120                 return 0.0;
121         }
122     }
123 
124     void DumpVelocityPoints() const;
125 
126 private:
127     void UpdateVelocity();
128 
129     Axis mainAxis_ { Axis::FREE };
130     TouchEvent firstTrackPoint_;
131     TouchEvent currentTrackPoint_;
132     Offset lastPosition_;
133     Velocity velocity_;
134     Offset delta_;
135     Offset offset_;
136     bool isFirstPoint_ = true;
137     TimeStamp lastTimePoint_;
138     TimeStamp firstPointTime_;
139     LeastSquareImpl xAxis_ { LEAST_SQUARE_PARAM_NUM, POINT_NUMBER };
140     LeastSquareImpl yAxis_ { LEAST_SQUARE_PARAM_NUM, POINT_NUMBER };
141     bool isVelocityDone_ = false;
142 };
143 
144 } // namespace OHOS::Ace
145 
146 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_VELOCITY_TRACKER_H
147