1#ifndef DANDELION_UTILS_MATH_HPP
2#define DANDELION_UTILS_MATH_HPP
17const Eigen::Matrix3f
I3f = Eigen::Matrix3f::Identity();
19const Eigen::Matrix4f
I4f = Eigen::Matrix4f::Identity();
33 return 3.141592653589793238462643383279;
40 return 3.14159265358f;
71inline constexpr T
squ(T x)
82inline constexpr T
clamp(T low, T high, T value)
84 return std::max(low, std::min(high, value));
96inline constexpr Eigen::Vector<T, 4>
to_vec4(Eigen::Vector<T, 3> vec3)
98 return Eigen::Vector<T, 4>(vec3.x(), vec3.y(), vec3.z(),
static_cast<T
>(0.0));
108inline Eigen::Vector3f
reflect(
const Eigen::Vector3f& I,
const Eigen::Vector3f& N)
110 return I - 2 * I.dot(N) * N;
121 if (x >
static_cast<T
>(0.0)) {
122 return static_cast<T
>(1.0);
124 if (x <
static_cast<T
>(0.0)) {
125 return static_cast<T
>(-1.0);
127 return static_cast<T
>(0.0);
152 const T test = x * z + w * y;
153 constexpr T threshold =
static_cast<T
>(0.5 - 1e-6);
154 T x_rad, y_rad, z_rad;
155 if (std::abs(test) > threshold) {
156 x_rad =
static_cast<T
>(0.0);
157 y_rad =
sign(test) *
pi<T>() /
static_cast<T
>(2.0);
158 z_rad =
sign(test) *
static_cast<T
>(2.0) * std::atan2(x, w);
161 std::atan2(
static_cast<T
>(-2.0) * (y * z - w * x),
squ(w) -
squ(x) -
squ(y) +
squ(z));
162 y_rad = std::asin(
static_cast<T
>(2.0) * (x * z + w * y));
164 std::atan2(
static_cast<T
>(-2.0) * (x * y - w * z),
squ(w) +
squ(x) -
squ(y) -
squ(z));
166 const T x_angle =
degrees(x_rad);
167 const T y_angle =
degrees(y_rad);
168 const T z_angle =
degrees(z_rad);
169 return std::make_tuple(x_angle, y_angle, z_angle);
std::tuple< T, T, T > quaternion_to_ZYX_euler(T w, T x, T y, T z)
将旋转的四元数表示形式转换为 ZYX 欧拉角表示形式。
定义 math.hpp:150
Eigen::Vector3f reflect(const Eigen::Vector3f &I, const Eigen::Vector3f &N)
求向量 关于向量 的反射。
定义 math.hpp:108
constexpr T clamp(T low, T high, T value)
将一个数截断在给定的上下界之间
定义 math.hpp:82
constexpr Eigen::Vector< T, 4 > to_vec4(Eigen::Vector< T, 3 > vec3)
将代表方向的三维向量转换为它的齐次坐标形式。
定义 math.hpp:96
constexpr T degrees(T radians)
将弧度转换为角度。
定义 math.hpp:60
T pi()
返回 float 或 double 类型的 值。
constexpr T sign(T x)
符号函数,正数的求值结果为 1,负数为 -1,零的求值结果是 0.
定义 math.hpp:119
constexpr T squ(T x)
求一个数的平方。
定义 math.hpp:71
constexpr T radians(T degrees)
将角度转换为弧度。
定义 math.hpp:49
constexpr double pi< double >()
定义 math.hpp:31
const Eigen::Matrix4f I4f
定义 math.hpp:19
constexpr float pi< float >()
定义 math.hpp:38
const Eigen::Matrix3f I3f
定义 math.hpp:17