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 <numbers>
13#include <random>
14#include <sstream>
15#include <string>
16#include <type_traits>
17#include <vector>
18
19#include <iomanip>
20
21#include "cassert_define.h"
22#include "math_const.h"
23
24
25
32{
33
34template <typename T> concept Arithmetic = std::is_arithmetic_v<T>;
35
44template <::std::floating_point T>
45constexpr bool IsEqual(const T num1, const T num2) noexcept
46{
47 const T dif = num1 - num2;
48
50}
51
52
58template <Arithmetic T>
59constexpr T Squared(const T num) noexcept { return num * num; }
60
61
67template <Arithmetic T>
68constexpr bool CanMakeTriangle(const T a, const T b, const T c) noexcept
69{
70 assert(a >= 0);
71 assert(b >= 0);
72 assert(c >= 0);
73 return (a + b > c && b + c > a && c + a > b);
74}
75
76
84template <typename T>
85T ApproachTarget(const T& current, const T& target, float rate)
86{
87 assert(0 <= rate);
88 assert(rate <= 1); // 0 <= rate <= 1 でなければならない.
89
90 if (current == target) { return current; }
91
92 return static_cast<T>(current * (1 - rate) + target * rate);
93}
94
95
101template <Arithmetic T>
102T GenerateRandomNumber(T min, T max)
103{
104 assert(min < max); // min < max でなければならない.
105
106 std::random_device rd;
107 std::mt19937 gen(rd());
108 std::uniform_real_distribution<> dis(min, max);
109 return (T)dis(gen);
110}
111
112
116template <::std::floating_point T>
117constexpr T ConvertRadToDeg(const T rad) noexcept
118{
119 return rad * (MathConst<T>::kRoundAngle / static_cast<T>(2)) / std::numbers::pi_v<float>;
120}
121
125template <::std::floating_point T>
126constexpr T ConvertDegToRad(const T deg) noexcept
127{
128 return deg * std::numbers::pi_v<float> / (MathConst<T>::kRoundAngle / static_cast<T>(2));
129}
130
134template <::std::floating_point T>
136{
137 angle = fmod(angle + MathConst<T>::kRoundAngle / 2, MathConst<T>::kRoundAngle);
138
139 if (angle < 0.0)
140 {
142 }
143
144 return angle - MathConst<T>::kRoundAngle / 2;
145}
146
147
148constexpr int kDigit = 3;
149constexpr int kWidth = 10;
150
160template <::std::floating_point T>
162 const T num, const int digit = kDigit, const int width = kWidth)
163{
164 std::ostringstream ss;
165
166 ss << std::fixed << std::setprecision(digit);
167 ss << std::setw(width) << std::setfill(' ') << num;
168
169 return ss.str();
170}
171
172
173} // namespace designlab::math_util
174
175
176#endif // DESIGNLAB_MATH_UTIL_H_
基本的な計算を行う関数をまとめた名前空間.
T ApproachTarget(const T &current, const T &target, float rate)
目標値に値を近づける関数. 描画用なので,線形でなく,適当に値を近づける. そのため,計算に使いたいなら作り直すこと.
Definition math_util.h:85
T LimitRangeAngleDeg(T angle)
角度を -180° ~ 180° の範囲に収める関数.
Definition math_util.h:135
constexpr bool CanMakeTriangle(const T a, const T b, const T c) noexcept
3辺で三角形が作れるか調べる関数.
Definition math_util.h:68
T GenerateRandomNumber(T min, T max)
指定した範囲内の乱数を生成する.
Definition math_util.h:102
std::string FloatingPointNumToString(const T num, const int digit=kDigit, const int width=kWidth)
小数を文字列に変換する関数. C++ では C のフォーマットのように %3.3f とかで小数を文字列に変換できないため自作する.
Definition math_util.h:161
constexpr int kDigit
小数点以下の桁数.
Definition math_util.h:148
constexpr T ConvertRadToDeg(const T rad) noexcept
角度を [rad]から [deg] に変換する関数.
Definition math_util.h:117
constexpr T ConvertDegToRad(const T deg) noexcept
角度を [deg] から [rad] に変換する関数.
Definition math_util.h:126
constexpr bool IsEqual(const T num1, const T num2) noexcept
C++において,小数同士の計算は誤差が出てしまう. 誤差込みで値が等しいか調べる.
Definition math_util.h:45
constexpr T Squared(const T num) noexcept
2乗した値を返す関数.
Definition math_util.h:59
constexpr int kWidth
文字列の幅.
Definition math_util.h:149
float 型と double 型の定数を提供するクラス.
Definition math_const.h:25