1 /* 2 * Copyright (c) 2021-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 FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_LEAST_SQUARE_IMPL_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_LEAST_SQUARE_IMPL_H 18 19 #include <cstdint> 20 #include <vector> 21 22 #include "base/utils/macros.h" 23 24 namespace OHOS::Ace { 25 /** 26 * @brief Least square method of four parametres. 27 * the function template is a3 * x^3 + a2 * x^2 + a1 * x + a0 = y with four; 28 * the function template is 0 * x^3 + a2 * x^2 + a1 * x + a0 = y with three. 29 */ 30 class ACE_EXPORT LeastSquareImpl { 31 public: 32 /** 33 * @brief Construct a new Least Square Impl object. 34 * @param paramsNum the right number is 4 or 3. 35 */ LeastSquareImpl(int32_t paramsNum)36 explicit LeastSquareImpl(int32_t paramsNum) : paramsNum_(paramsNum) {} 37 38 /** 39 * @brief Construct a new Least Square Impl object. 40 * @param paramsNum the right number is 4 or 3. 41 */ LeastSquareImpl(int32_t paramsNum,int32_t countNum)42 LeastSquareImpl(int32_t paramsNum, int32_t countNum) : paramsNum_(paramsNum), countNum_(countNum) {} 43 44 LeastSquareImpl() = default; 45 ~LeastSquareImpl() = default; 46 UpdatePoint(double xVal,double yVal)47 void UpdatePoint(double xVal, double yVal) 48 { 49 isResolved_ = false; 50 xVals_.emplace_back(xVal); 51 yVals_.emplace_back(yVal); 52 } 53 54 /** 55 * @brief Set the Count Num which to compute. 56 * 57 * @param countNum the compute number. 58 */ SetCountNum(int32_t countNum)59 void SetCountNum(int32_t countNum) 60 { 61 countNum_ = countNum; 62 } 63 64 /** 65 * @brief Get the Least Square Params object 66 * 67 * @param params the four values of vector. 68 * @return true get the least square result. 69 * @return false failed to get the least square result. 70 */ 71 bool GetLeastSquareParams(std::vector<double>& params); 72 GetXVals()73 inline const std::vector<double>& GetXVals() const 74 { 75 return xVals_; 76 } 77 GetYVals()78 inline const std::vector<double>& GetYVals() const 79 { 80 return yVals_; 81 } 82 GetTrackNum()83 inline int32_t GetTrackNum() const 84 { 85 return xVals_.size(); 86 } 87 Reset()88 void Reset() 89 { 90 xVals_.clear(); 91 yVals_.clear(); 92 params_.clear(); 93 isResolved_ = false; 94 } 95 96 private: 97 std::vector<double> xVals_; 98 std::vector<double> yVals_; 99 std::vector<double> params_; 100 int32_t paramsNum_ = 4; 101 int32_t countNum_ = 4; 102 bool isResolved_ = false; 103 }; 104 } // namespace OHOS::Ace 105 106 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_LEAST_SQUARE_IMPL_H 107