1 /* 2 * Copyright (c) 2024 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_RESAMPLE_ALGO_H 16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_RESAMPLE_ALGO_H 17 #include <vector> 18 #include "core/event/axis_event.h" 19 20 namespace OHOS::Ace { 21 namespace { 22 constexpr int32_t RESAMPLE_COORD_TIME_THRESHOLD = 20 * 1000 * 1000; 23 constexpr uint64_t INTERPOLATION_THRESHOLD = 100 * 1000 * 1000; // 100ms 24 constexpr uint64_t ONE_S_IN_NS = 1 * 1000 * 1000 * 1000; 25 } 26 27 struct AvgPoint { 28 float x = {}; 29 float y = {}; 30 uint64_t time = {}; 31 float windowX = {}; 32 float windowY = {}; 33 34 AvgPoint& operator+=(const AvgPoint &other) 35 { 36 x += other.x; 37 y += other.y; 38 windowX += other.windowX; 39 windowY += other.windowY; 40 time += other.time; 41 return *this; 42 } 43 44 AvgPoint& operator/=(float divisor) 45 { 46 if (NearEqual(divisor, 0.0f)) { 47 return *this; 48 } 49 x /= divisor; 50 y /= divisor; 51 windowX /= divisor; 52 windowY /= divisor; 53 time /= static_cast<uint64_t>(divisor); 54 return *this; 55 } 56 }; 57 58 struct ResamplePoint { 59 float x{}; 60 float y{}; 61 float inputXDeltaSlope{}; 62 float inputYDeltaSlope{}; 63 }; 64 65 class ResampleAlgo final { 66 public: 67 ResampleAlgo() = delete; 68 69 ~ResampleAlgo() = delete; 70 71 static AvgPoint GetAvgPoint(const std::vector<PointerEvent>&& events, 72 bool isScreen); 73 74 static ResamplePoint LinearInterpolation(const AvgPoint& history, const AvgPoint& current, 75 uint64_t nanoTimeStamp); 76 77 static ResamplePoint GetResampleCoord(const std::vector<PointerEvent>&& history, 78 const std::vector<PointerEvent>&& current, uint64_t nanoTimeStamp, 79 bool isScreen); 80 }; 81 } // namespace OHOS::Ace 82 #endif