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