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