• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 VELOCITY_TRACKER_H
17 #define VELOCITY_TRACKER_H
18 #include <regex>
19 #include <cmath>
20 #include <atomic>
21 #include "least_square_impl.h"
22 #include "touch_event.h"
23 #include "offset.h"
24 #include "velocity.h"
25 
26 namespace OHOS::uitest {
27 enum class Axis {
28     VERTICAL = 0,
29     HORIZONTAL,
30     FREE,
31     NONE,
32 };
33 class VelocityTracker final {
34 public:
35     VelocityTracker() = default;
VelocityTracker(Axis mainAxis)36     explicit VelocityTracker(Axis mainAxis) : mainAxis_(mainAxis) {}
37     ~VelocityTracker() = default;
38 
39     static constexpr int TIME_INDEX = 1000;
TrackResets()40     void TrackResets()
41     {
42         lastTrackPoint_.Resets();
43     }
44 
45     void UpdateTouchEvent(const TouchEventInfo& event, bool end = false);
46 
SetMainVelocity(double mainVelocity)47     void SetMainVelocity(double mainVelocity)
48     {
49         mainVelocity_ = mainVelocity;
50     }
51 
GetStepCount()52     int GetStepCount() const
53     {
54         return xAxis_.GetPVals().size();
55     }
56 
UpdateStepLength()57     void UpdateStepLength()
58     {
59         stepCount = GetStepCount();
60         std::vector<double> xs = xAxis_.GetPVals();
61         std::vector<double> ys = yAxis_.GetPVals();
62         if (stepCount == 1) {
63             return;
64         }
65         if (stepCount < useToCount) {
66             useToCount = stepCount;
67         }
68         for (int i = 1; i < useToCount; i++) {
69             totalDelta_ += Offset(xs.at(stepCount - i), ys.at(stepCount - i)) - \
70                             Offset(xs.at(stepCount - i - 1), ys.at(stepCount - i - 1));
71         }
72         stepLength = (totalDelta_ / (useToCount - 1)).GetDistance();
73     }
74 
GetStepLength()75     int GetStepLength()
76     {
77         UpdateStepLength();
78         return stepLength;
79     }
80 
GetPosition()81     const Point& GetPosition() const
82     {
83         return lastPosition_;
84     }
85 
GetFirstPosition()86     const Point& GetFirstPosition() const
87     {
88         return firstPosition_;
89     }
90 
GetDelta()91     const Offset& GetDelta() const
92     {
93         return delta_;
94     }
95 
GetVelo()96     const Velocity& GetVelo()
97     {
98         UpdateVelocity();
99         return velocity_;
100     }
101 
GetMainVelocity()102     double GetMainVelocity() const
103     {
104         return mainVelocity_;
105     }
106 
GetMaxAxis()107     Axis GetMaxAxis()
108     {
109         UpdateVelocity();
110         if (fabs(velocity_.GetVeloX()) > fabs(velocity_.GetVeloY())) {
111             maxAxis_ = Axis::HORIZONTAL;
112         } else {
113             maxAxis_ = Axis::VERTICAL;
114         }
115         return maxAxis_;
116     }
117 
GetMainAxisDeltaPos()118     double GetMainAxisDeltaPos() const
119     {
120         switch (mainAxis_) {
121             case Axis::FREE:
122                 return delta_.GetDistance();
123             case Axis::HORIZONTAL:
124                 return delta_.GetX();
125             case Axis::VERTICAL:
126                 return delta_.GetY();
127             default:
128                 return 0.0;
129         }
130     }
131 
GetMainAxisVelocity()132     double GetMainAxisVelocity()
133     {
134         UpdateVelocity();
135         switch (mainAxis_) {
136             case Axis::FREE:
137                 mainVelocity_ =  velocity_.GetVeloValue();
138                 break;
139             case Axis::HORIZONTAL:
140                 mainVelocity_ = velocity_.GetVeloX();
141                 break;
142             case Axis::VERTICAL:
143                 mainVelocity_ = velocity_.GetVeloY();
144                 break;
145             default:
146                 mainVelocity_ = 0.0;
147         }
148         return mainVelocity_;
149     }
150 
GetLastTrackPoint()151     TouchEventInfo& GetLastTrackPoint()
152     {
153         return lastTrackPoint_;
154     }
155 
SetClickInterVal(double interVal)156     void SetClickInterVal(double interVal)
157     {
158         clickInterVal = interVal;
159     }
160 
GetLastTimePoint()161     TimeStamp GetLastTimePoint()
162     {
163         return lastTimePoint_;
164     }
165 
GetAxisSize()166     int GetAxisSize()
167     {
168         return xAxis_.GetTVals().size();
169     }
170 
GetPreTime(int num)171     double GetPreTime(int num)
172     {
173         return xAxis_.GetTVals().at(xAxis_.GetTVals().size() - num);
174     }
175 
GetPreX(int num)176     double GetPreX(int num)
177     {
178         return xAxis_.GetPVals().at(xAxis_.GetPVals().size() - num);
179     }
180 
GetPreY(int num)181     double GetPreY(int num)
182     {
183         return yAxis_.GetPVals().at(yAxis_.GetPVals().size() - num);
184     }
185 
186 private:
187     void UpdateVelocity();
188     Axis mainAxis_ { Axis::FREE };
189     Axis maxAxis_  {Axis::VERTICAL };
190     TouchEventInfo lastTrackPoint_;
191     Point firstPosition_;
192     Point lastPosition_;
193     Offset totalDelta_;
194     Velocity velocity_;
195     double mainVelocity_ = 0.0;
196     Offset delta_;
197     bool isFirstPoint_ = true;
198     int useToCount = 4;
199     int stepLength = 0;
200     int stepCount = 0;
201     TimeStamp firstTimePoint_;
202     TimeStamp lastTimePoint_;
203     LeastSquareImpl xAxis_ { 3, 5 };
204     LeastSquareImpl yAxis_ { 3, 5 };
205     bool isVelocityDone_ = false;
206     double clickInterVal = 0;
207 };
208 } // namespace OHOS::uitest
209 #endif // VELOCITY_TRACKER_H