UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
Math.hpp
[詳解]
1//
2// 数学定数、関数
3//
4// Copyright (c) 2022 udonrobo
5//
6
7#pragma once
8
9#include <math.h>
10#include <Udon/Types/Range.hpp>
11
12#ifdef abs
13# undef abs
14
15template <typename T>
16inline constexpr T abs(const T& rhs)
17{
18 return rhs < 0 ? -rhs : rhs;
19}
20
21#endif
22
23namespace Udon
24{
25
27 constexpr double Pi = 3.1415926535897932384626433832795; // std::numbers::pi_v<double> (C++20)
28
30 constexpr double HalfPi = 1.5707963267948966192313216916398; // std::numbers::pi_v<double> / 2 (C++20)
31
33 constexpr double TwoPi = 6.283185307179586476925286766559; // std::numbers::pi_v<double> * 2 (C++20)
34
36 constexpr double DegToRad = 0.017453292519943295769236907684886; // std::numbers::pi_v<double> / 180 (C++20)
37
39 constexpr double RadToDeg = 57.295779513082320876798154814105; // 180 / std::numbers::pi_v<double> (C++20)
40
41}; // namespace Udon
42
43namespace Udon
44{
45
47 template <typename A, typename B>
48 inline constexpr auto
49 Min(const A& lhs, const B& rhs) -> decltype(lhs, rhs)
50 {
51 return lhs < rhs ? lhs : rhs;
52 }
53
55 template <typename A, typename B>
56 inline constexpr auto
57 Max(const A& lhs, const B& rhs) -> decltype(lhs, rhs)
58 {
59 return lhs > rhs ? lhs : rhs;
60 }
61
67 template <typename A, typename B, typename C>
68 inline constexpr A
69 Constrain(const A& amt, const B& low, const C& high)
70 {
71 return Min(Max(amt, low), high);
72 }
73
78 template <typename A, typename B, typename C = B>
79 inline constexpr A
80 Constrain(const A& amt, const Udon::Range<B, C>& range)
81 {
82 return Min(Max(amt, range.min), range.max);
83 }
84
88 template <typename T>
89 inline constexpr T
90 ToRadians(const T& rhs)
91 {
92 return rhs * DegToRad;
93 }
94
98 template <typename T>
99 inline constexpr T
100 ToDegrees(const T& rhs)
101 {
102 return rhs * RadToDeg;
103 }
104
108 template <typename T>
109 inline constexpr T
110 Abs(const T& rhs)
111 {
112 return rhs < 0 ? -rhs : rhs;
113 }
114
118 template <typename T>
119 inline constexpr T
120 Sq(const T& x)
121 {
122 return x * x;
123 }
124
126 inline constexpr int
127 Floor(double x)
128 {
129 return x - (int)x < 0 ? (int)x - 1 : (int)x;
130 }
131
133 inline constexpr int
134 Round(double x)
135 {
136 return x - (int)x >= 0.5 ? (int)x + 1 : (int)x;
137 }
138
140 inline constexpr int
141 Ceil(double x)
142 {
143 return x - (int)x > 0 ? (int)x + 1 : (int)x;
144 }
145
147 inline constexpr double
148 Map(const double value, const double inputMin, const double inputMax, const double outputMin, const double outputMax)
149 {
150 return (value - inputMin) * (outputMax - outputMin) / (inputMax - inputMin) + outputMin;
151 }
152
153 // inline bool
154 // FloatEq(const double l, const double r)
155 // {
156 // return
157 // }
158
165 inline double
166 Normalized(double value, double min, double max)
167 {
168 const auto cycle = max - min;
169
170 const auto modValue = fmod((value - min), cycle) + min;
171
172 if (modValue < min)
173 {
174 return modValue + cycle;
175 }
176 else
177 {
178 return modValue;
179 }
180 }
181
186 inline double
187 Hypotenuse(double x, double y)
188 {
189 return sqrt(x * x + y * y);
190 }
191
192} // namespace Udon
Definition Bit.hpp:12
constexpr T Abs(const T &rhs)
絶対値を返す (std::abs)
Definition Math.hpp:110
constexpr auto Max(const A &lhs, const B &rhs) -> decltype(lhs, rhs)
2つの値のうち大きい方を返す (std::max)
Definition Math.hpp:57
constexpr T ToRadians(const T &rhs)
度数法の角度を弧度法に変換する
Definition Math.hpp:90
double Hypotenuse(double x, double y)
三平方の定理を用いて、2辺の長さから斜辺の長さを求める
Definition Math.hpp:187
constexpr double Pi
π
Definition Math.hpp:27
constexpr T Sq(const T &x)
二乗を求める (std::pow(x, 2))
Definition Math.hpp:120
constexpr int Round(double x)
四捨五入 (std::round)
Definition Math.hpp:134
constexpr double DegToRad
度数法から弧度法に変換する係数
Definition Math.hpp:36
double Normalized(double value, double min, double max)
値を正規化する
Definition Math.hpp:166
constexpr double RadToDeg
弧度法から度数法に変換する係数
Definition Math.hpp:39
constexpr T ToDegrees(const T &rhs)
弧度法の角度を度数法に変換する
Definition Math.hpp:100
constexpr A Constrain(const A &amt, const B &low, const C &high)
値を指定された範囲内に収める (std::clamp)
Definition Math.hpp:69
constexpr int Floor(double x)
小数点切り捨て (std::floor)
Definition Math.hpp:127
constexpr auto Min(const A &lhs, const B &rhs) -> decltype(lhs, rhs)
2つの値のうち小さい方を返す (std::min)
Definition Math.hpp:49
constexpr double HalfPi
π/2
Definition Math.hpp:30
constexpr double TwoPi
π*2
Definition Math.hpp:33
constexpr int Ceil(double x)
小数点切り上げ (std::ceil)
Definition Math.hpp:141
constexpr double Map(const double value, const double inputMin, const double inputMax, const double outputMin, const double outputMax)
数値をある範囲から別の範囲に再マッピングする
Definition Math.hpp:148
範囲を表す型
Definition Range.hpp:8
MinT min
最小値
Definition Range.hpp:9
MaxT max
最大値
Definition Range.hpp:10