• 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 <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 
PopFrontPoint()54     void PopFrontPoint()
55     {
56         if (!xVals_.empty() && !yVals_.empty()) {
57             xVals_.erase(xVals_.begin());
58             yVals_.erase(yVals_.begin());
59         }
60     }
61 
62     /**
63      * @brief Set the Count Num which to compute.
64      *
65      * @param countNum the compute number.
66      */
SetCountNum(int32_t countNum)67     void SetCountNum(int32_t countNum)
68     {
69         countNum_ = countNum;
70     }
71 
72     /**
73      * @brief Get the Least Square Params object
74      *
75      * @param params the four values of vector.
76      * @return true get the least square result.
77      * @return false failed to get the least square result.
78      */
79     bool GetLeastSquareParams(std::vector<double>& params);
80 
GetXVals()81     inline const std::vector<double>& GetXVals() const
82     {
83         return xVals_;
84     }
85 
GetYVals()86     inline const std::vector<double>& GetYVals() const
87     {
88         return yVals_;
89     }
90 
GetTrackNum()91     inline int32_t GetTrackNum() const
92     {
93         return xVals_.size();
94     }
95 
Reset()96     void Reset()
97     {
98         xVals_.clear();
99         yVals_.clear();
100         params_.clear();
101         isResolved_ = false;
102     }
103 
104 private:
105     std::vector<double> xVals_;
106     std::vector<double> yVals_;
107     std::vector<double> params_;
108     int32_t paramsNum_ = 4;
109     int32_t countNum_ = 4;
110     bool isResolved_ = false;
111 };
112 } // namespace OHOS::Ace
113 
114 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_LEAST_SQUARE_IMPL_H
115