GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
math_util.h
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#ifndef DESIGNLAB_MATH_UTIL_H_
9#define DESIGNLAB_MATH_UTIL_H_
10
11#include <concepts>
12#include <iomanip>
13#include <numbers>
14#include <random>
15#include <sstream>
16#include <string>
17#include <type_traits>
18#include <vector>
19
20#include "cassert_define.h"
21#include "math_const.h"
22
28namespace designlab::math_util {
29
30template <typename T>
31concept Arithmetic = std::is_arithmetic_v<T>;
32
41template <::std::floating_point T>
42constexpr bool IsEqual(const T num1, const T num2) noexcept {
43 const T dif = num1 - num2;
44
45 return (dif <= MathConst<T>::kAllowableError &&
47}
48
54template <Arithmetic T>
55constexpr T Squared(const T num) noexcept {
56 return num * num;
57}
58
64template <Arithmetic T>
65constexpr bool CanMakeTriangle(const T a, const T b, const T c) noexcept {
66 assert(a >= 0);
67 assert(b >= 0);
68 assert(c >= 0);
69 return (a + b > c && b + c > a && c + a > b);
70}
71
79template <typename T>
80T ApproachTarget(const T& current, const T& target, float rate) {
81 assert(0 <= rate);
82 assert(rate <= 1); // 0 <= rate <= 1 でなければならない.
83
84 if (current == target) {
85 return current;
86 }
87
88 return static_cast<T>(current * (1 - rate) + target * rate);
89}
90
96template <Arithmetic T>
97T GenerateRandomNumber(T min, T max) {
98 assert(min < max); // min < max でなければならない.
99
100 std::random_device rd;
101 std::mt19937 gen(rd());
102 std::uniform_real_distribution<> dis(min, max);
103 return (T)dis(gen);
104}
105
109template <::std::floating_point T>
110constexpr T ConvertRadToDeg(const T rad) noexcept {
111 return rad * (MathConst<T>::kRoundAngle / static_cast<T>(2)) /
112 std::numbers::pi_v<float>;
113}
114
118template <::std::floating_point T>
119constexpr T ConvertDegToRad(const T deg) noexcept {
120 return deg * std::numbers::pi_v<float> /
121 (MathConst<T>::kRoundAngle / static_cast<T>(2));
122}
123
127template <::std::floating_point T>
129 angle =
131
132 if (angle < 0.0) {
134 }
135
136 return angle - MathConst<T>::kRoundAngle / 2;
137}
138
139constexpr int kDigit = 3;
140constexpr int kWidth = 10;
141
151template <::std::floating_point T>
152std::string FloatingPointNumToString(const T num, const int digit = kDigit,
153 const int width = kWidth) {
154 std::ostringstream ss;
155
156 ss << std::fixed << std::setprecision(digit);
157 ss << std::setw(width) << std::setfill(' ') << num;
158
159 return ss.str();
160}
161
162} // namespace designlab::math_util
163
164#endif // DESIGNLAB_MATH_UTIL_H_
基本的な計算を行う関数をまとめた名前空間.
T ApproachTarget(const T &current, const T &target, float rate)
目標値に値を近づける関数. 描画用なので,線形でなく,適当に値を近づける. そのため,計算に使いたいなら作り直すこと.
Definition math_util.h:80
T LimitRangeAngleDeg(T angle)
角度を -180° ~ 180° の範囲に収める関数.
Definition math_util.h:128
constexpr bool CanMakeTriangle(const T a, const T b, const T c) noexcept
3辺で三角形が作れるか調べる関数.
Definition math_util.h:65
T GenerateRandomNumber(T min, T max)
指定した範囲内の乱数を生成する.
Definition math_util.h:97
std::string FloatingPointNumToString(const T num, const int digit=kDigit, const int width=kWidth)
小数を文字列に変換する関数. C++ では C のフォーマットのように %3.3f とかで小数を文字列に変換できないため自作する.
Definition math_util.h:152
constexpr int kDigit
小数点以下の桁数.
Definition math_util.h:139
constexpr T ConvertRadToDeg(const T rad) noexcept
角度を [rad]から [deg] に変換する関数.
Definition math_util.h:110
constexpr T ConvertDegToRad(const T deg) noexcept
角度を [deg] から [rad] に変換する関数.
Definition math_util.h:119
constexpr bool IsEqual(const T num1, const T num2) noexcept
C++において,小数同士の計算は誤差が出てしまう. 誤差込みで値が等しいか調べる.
Definition math_util.h:42
constexpr T Squared(const T num) noexcept
2乗した値を返す関数.
Definition math_util.h:55
constexpr int kWidth
文字列の幅.
Definition math_util.h:140
float 型と double 型の定数を提供するクラス.
Definition math_const.h:22