GKD.RoboCtrl
载入中...
搜索中...
未找到
utils.hpp
浏览该文件的文档.
1
6#pragma once
7
8#include <concepts>
9#include <cstdint>
10#include <cstring>
11#include <chrono>
12#include <cassert>
13#include <cmath>
14#include <numbers>
15#include <type_traits>
16
17#include "utils/concepts.hpp"
18
19namespace roboctrl{
20
22using fp32 = float;
24using fp64 = double;
25
30template<typename T>
31constexpr T Pi = std::numbers::pi_v<T>;
32
34constexpr auto Pi_f = Pi<fp32>;
35
36
41template<typename T>
42 requires std::is_arithmetic_v<T>
43struct vector{
44 T x;
45 T y;
46
50 T norm() const{
51 return std::sqrt(x * x + y * y);
52 }
53
58 return *this / norm();
59 }
60};
61
62using vectori = vector<int>;
63using vectorf = vector<float>;
64
66template<typename T>
67vector<T> operator + (vector<T> a,vector<T> b){
68 return {a.x+b.x,a.y+b.y};
69
70}
71
73template<typename T>
74vector<T> operator - (vector<T> a,vector<T> b){
75 return {a.x-b.x,a.y-b.y};
76}
77
79template<typename T>
80vector<T> operator * (vector<T> a,T b){
81 return {a.x * b,a.y * b};
82}
83
85template<typename T>
86vector<T> operator / (vector<T> a,T b){
87 return {a.x / b,a.y / b};
88}
89
94namespace utils{
95
99constexpr inline fp32 rad_format(fp32 ang){
100 fp32 ans = fmodf(ang + Pi_f, Pi_f * 2.f);
101 return (ans < 0.f) ? ans + Pi_f : ans - Pi_f;
102}
103
107template<typename C>
109 requires(C c) {
110 { c.data() } -> std::convertible_to<const std::byte*>;
111 { c.size() } -> std::convertible_to<std::size_t>;
112 };
113
120template<package T, byte_container C>
121T from_bytes(const C& bytes) {
122 assert(bytes.size() == sizeof(T));
123 T t;
124 std::memcpy(&t, bytes.data(), sizeof(T));
125 return t;
126}
127
135template<package T, typename C>
136requires requires(C& c) {
137 { c.data() } -> std::convertible_to<std::byte*>;
138 { c.size() } -> std::convertible_to<std::size_t>;
139}
140void to_bytes(const T& t, C& container) {
141 assert(container.size() >= sizeof(T));
142 std::memcpy(container.data(), &t, sizeof(T));
143}
147inline auto now() {
148 static const auto init_time = std::chrono::steady_clock::now();
149 return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - init_time);
150}
151
155inline constexpr uint16_t make_u16(uint16_t high, uint16_t low) noexcept {
156 return (static_cast<uint16_t>(high) << 8) | static_cast<uint16_t>(low);
157}
158
162inline constexpr int16_t make_i16(int16_t high, int16_t low) noexcept {
163 return (static_cast<int16_t>(high) << 8) | static_cast<int16_t>(low);
164}
165
169inline constexpr std::byte to_byte(std::integral auto v) noexcept{
170 return static_cast<std::byte>(v);
171}
172
173template<typename T>
175
176template<typename Ret, typename Arg>
177struct function_arg<Ret(*)(Arg)> { using type = Arg; };
178
179template<typename Ret, typename Class, typename Arg>
180struct function_arg<Ret(Class::*)(Arg) const> { using type = Arg; };
181
182template<typename Fn>
183using function_arg_t = typename function_arg<decltype(&Fn::operator())>::type;
184
185}
186}
描述具有 data() 与 size() 接口的字节容器。
Definition utils.hpp:108
公共Concept与元类型工具。
asio::awaitable< T > awaitable
协程任务类型。
Definition async.hpp:42
constexpr std::byte to_byte(std::integral auto v) noexcept
辅助将整数转换为 std::byte。
Definition utils.hpp:169
constexpr int16_t make_i16(int16_t high, int16_t low) noexcept
将高低字节组合成有符号 16 位整数。
Definition utils.hpp:162
T from_bytes(const C &bytes)
将字节容器内容反序列化为平凡类型。
Definition utils.hpp:121
void to_bytes(const T &t, C &container)
将平凡类型序列化到指定容器。
Definition utils.hpp:140
constexpr uint16_t make_u16(uint16_t high, uint16_t low) noexcept
将高低字节组合成无符号 16 位整数。
Definition utils.hpp:155
auto now()
记录程序启动后的纳秒级时间戳。
Definition utils.hpp:147
constexpr fp32 rad_format(fp32 ang)
将角度限制到[-Pi,Pi]范围内
Definition utils.hpp:99
简单的二维向量类型。
Definition utils.hpp:43
T norm() const
获取向量欧氏范数。
Definition utils.hpp:50
vector normalized() const
获取该向量的单位化结果。
Definition utils.hpp:57
constexpr T Pi
常量 Pi 的模板实例。
Definition utils.hpp:31
double fp64
双精度浮点别名。
Definition utils.hpp:24
constexpr auto Pi_f
单精度浮点类型的 Pi 常量。
Definition utils.hpp:34
float fp32
单精度浮点别名。
Definition utils.hpp:22