libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
cycle_value.hpp
1#pragma once
2
3#include <cmath>
4
5#include "libxr_def.hpp"
6
7namespace LibXR
8{
9
10using DefaultScalar = LIBXR_DEFAULT_SCALAR;
11
25template <typename Scalar = DefaultScalar>
27{
28 public:
39 CycleValue &operator=(const CycleValue &) = default;
40
50 static constexpr Scalar Calculate(Scalar value)
51 {
52 value = std::fmod(value, M_2PI);
53 if (value < 0)
54 {
55 value += M_2PI;
56 }
57 return value;
58 }
59
67 CycleValue(const Scalar &value) : value_(Calculate(value)) {}
68
76 CycleValue(const CycleValue &value) : value_(value.value_)
77 {
78 while (value_ >= M_2PI)
79 {
80 value_ -= M_2PI;
81 }
82
83 while (value_ < 0)
84 {
85 value_ += M_2PI;
86 }
87 }
88
93 CycleValue() : value_(0.0f) {}
94
104 CycleValue operator+(const Scalar &value) const { return CycleValue(value + value_); }
105
106 CycleValue operator+(const CycleValue &value) const
107 {
108 return CycleValue(value.value_ + value_);
109 }
110
120 CycleValue &operator+=(const Scalar &value)
121 {
122 value_ = Calculate(value + value_);
123 return *this;
124 }
125
126 CycleValue &operator+=(const CycleValue &value)
127 {
128 Scalar ans = value.value_ + value_;
129 while (ans >= M_2PI)
130 {
131 ans -= M_2PI;
132 }
133
134 while (ans < 0)
135 {
136 ans += M_2PI;
137 }
138
139 value_ = ans;
140 return *this;
141 }
142
153 Scalar operator-(const Scalar &raw_value) const
154 {
155 Scalar value = Calculate(raw_value);
156 Scalar ans = value_ - value;
157 while (ans >= M_PI)
158 {
159 ans -= M_2PI;
160 }
161
162 while (ans < -M_PI)
163 {
164 ans += M_2PI;
165 }
166
167 return ans;
168 }
169
170 Scalar operator-(const CycleValue &value) const
171 {
172 Scalar ans = value_ - value.value_;
173 while (ans >= M_PI)
174 {
175 ans -= M_2PI;
176 }
177
178 while (ans < -M_PI)
179 {
180 ans += M_2PI;
181 }
182
183 return ans;
184 }
185
195 CycleValue &operator-=(const Scalar &value)
196 {
197 value_ = Calculate(value_ - value);
198 return *this;
199 }
200
201 CycleValue &operator-=(const CycleValue &value)
202 {
203 Scalar ans = value_ - value.value_;
204 while (ans >= M_2PI)
205 {
206 ans -= M_2PI;
207 }
208
209 while (ans < 0)
210 {
211 ans += M_2PI;
212 }
213
214 value_ = ans;
215 return *this;
216 }
217
225 CycleValue operator-() const { return CycleValue(M_2PI - value_); }
226
234 operator Scalar() const { return this->value_; }
235
245 CycleValue &operator=(const Scalar &value)
246 {
247 value_ = Calculate(value);
248 return *this;
249 }
250
258 Scalar Value() const { return value_; }
259
260 private:
261 Scalar value_;
262};
263
264} // namespace LibXR
角度循环处理类,用于处理周期性角度计算。 A cyclic angle handling class for periodic angle calculations.
CycleValue & operator-=(const Scalar &value)
复合减法运算符重载。 Overloaded compound subtraction operator.
CycleValue & operator=(const CycleValue &)=default
赋值运算符重载。 Overloaded assignment operator.
CycleValue & operator=(const Scalar &value)
赋值运算符重载,更新角度值并归一化。 Overloaded assignment operator, updating and normalizing the angle value.
Scalar Value() const
获取当前的角度值。 Retrieves the current angle value.
Scalar value_
存储的角度值。 The stored angle value.
CycleValue operator-() const
取反运算符重载,将角度转换到 2π - value_。 Overloaded negation operator, converting the angle to 2π - value_.
Scalar operator-(const Scalar &raw_value) const
减法运算符重载,计算角度差值并归一化到 -π 到 π 之间。 Overloaded subtraction operator, computing the angle difference and no...
CycleValue(const CycleValue &value)
拷贝构造函数,确保角度值在合法范围内。 Copy constructor ensuring the angle value remains within valid limits.
CycleValue operator+(const Scalar &value) const
加法运算符重载。 Overloaded addition operator.
CycleValue & operator+=(const Scalar &value)
复合加法运算符重载。 Overloaded compound addition operator.
CycleValue(const Scalar &value)
使用给定值初始化 CycleValue。 Initializes CycleValue with a given value.
CycleValue()
默认构造函数,初始化为 0。 Default constructor initializing the angle to 0.
static constexpr Scalar Calculate(Scalar value)
计算角度值并归一化到 0 到 2π 之间。 Computes and normalizes the angle value within the range of 0 to 2π.
LibXR 命名空间
Definition ch32_gpio.hpp:9