libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
float_encoder.hpp
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5#include <stdexcept>
6
7namespace LibXR
8{
9
20template <int Bits, typename Scalar = float>
22{
23 public:
24 static_assert(Bits > 0 && Bits < 32, "Bits must be between 1 and 31");
25
30 static constexpr uint32_t MaxInt() { return (1u << Bits) - 1; }
31
40
49 {
50 Scalar clamped = std::fmin(std::fmax(value, min_), max_);
52 return static_cast<uint32_t>(std::round(norm * MaxInt()));
53 }
54
63 {
64 Scalar norm = static_cast<Scalar>(encoded) / MaxInt();
65 return min_ + norm * range_;
66 }
67
68 private:
72};
73
74} // namespace LibXR
浮点数编码器,将浮点值映射到定长无符号整数。 A generic float encoder mapping a float value to N-bit unsigned integer.
Scalar range_
区间范围(max - min)
uint32_t Encode(Scalar value) const
编码:将浮点数映射为无符号整数。 Encodes a float to unsigned integer in range [0, 2^Bits - 1].
Scalar Decode(uint32_t encoded) const
解码:将无符号整数还原为浮点值。 Decodes an unsigned integer to float.
static constexpr uint32_t MaxInt()
获取最大可编码的整数值。 Returns the maximum encodable unsigned integer.
FloatEncoder(Scalar min, Scalar max)
构造函数,设置映射区间 [min, max]。 Constructor specifying the float range [min, max].
Scalar min_
浮点区间最小值
Scalar max_
浮点区间最大值
LibXR Color Control Library / LibXR终端颜色控制库
Definition esp_gpio.hpp:8
constexpr auto max(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最大值
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值