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