• 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 #include <chrono>
17 #include "velocity_tracker.h"
18 
19 using namespace std;
20 using namespace std::chrono;
21 
22 namespace OHOS::uitest {
UpdateTouchEvent(const TouchEventInfo & event,bool end)23 void VelocityTracker::UpdateTouchEvent(const TouchEventInfo& event, bool end)
24 {
25     isVelocityDone_ = false;
26     currentTrackPoint_ = event;
27     if (isFirstPoint_) {
28         downTrackPoint_ = firstTrackPoint_;
29         firstPosition_ = event.GetOffset();
30         firstTimePoint_ = event.time;
31         firstTrackPoint_ = event;
32         isFirstPoint_ = false;
33     } else {
34         delta_ = event.GetOffset() - lastPosition_;
35     }
36     std::chrono::duration<double> diffTime = event.time - lastTimePoint_;
37     lastTimePoint_ = event.time;
38     lastPosition_ = event.GetOffset() ;
39     lastTrackPoint_ = event;
40     static const double range = 0.05;
41     if (delta_.IsZero() && end && (diffTime.count() < range)) {
42         return;
43     }
44     // nanoseconds duration to seconds.
45     std::chrono::duration<double> duration = event.time - firstTrackPoint_.time;
46     seconds = duration.count();
47     xAxis_.UpdatePoint(seconds, event.wx);
48     yAxis_.UpdatePoint(seconds, event.wy);
49 }
50 
UpdateVelocity()51 void VelocityTracker::UpdateVelocity()
52 {
53     if (isVelocityDone_) {
54         return;
55     }
56     // the least square method three params curve is 0 * x^3 + a2 * x^2 + a1 * x + a0
57     // the velocity is 2 * a2 * x + a1;
58     static const int32_t linearParam = 2;
59     std::vector<double> xAxis { 3, 0 };
60     auto xValue = xAxis_.GetTVals().back();
61     double xVelocity = 0.0;
62     if (xAxis_.GetLSMParams(xAxis)) {
63         xVelocity = linearParam * xAxis[ZERO] * xValue + xAxis[ONE];
64     }
65     std::vector<double> yAxis { 3, 0 };
66     auto yValue = yAxis_.GetTVals().back();
67     double yVelocity = 0.0;
68     if (yAxis_.GetLSMParams(yAxis)) {
69         yVelocity = linearParam * yAxis[ZERO] * yValue + yAxis[ONE];
70     }
71     velocity_.SetOffsetPerSecond({ xVelocity, yVelocity });
72     isVelocityDone_ = true;
73 }
74 } // namespace OHOS::uitest
75