candlewick 0.1.0
A renderer
Loading...
Searching...
No Matches
math_types.h
Go to the documentation of this file.
1#pragma once
2#include <Eigen/Core>
3#include <SDL3/SDL_stdinc.h>
4
5namespace candlewick {
6
7using Float2 = Eigen::Vector2f;
8using Float3 = Eigen::Vector3f;
9using Float4 = Eigen::Vector4f;
10using Mat3f = Eigen::Matrix3f;
11using Mat4f = Eigen::Matrix4f;
12using Vec3u8 = Eigen::Matrix<Uint8, 3, 1>;
13using Vec4u8 = Eigen::Matrix<Uint8, 4, 1>;
14
15using FrustumCornersType = std::array<Float3, 8ul>;
16
17using GpuVec2 = Eigen::Matrix<float, 2, 1, Eigen::DontAlign>;
18using GpuVec3 = Eigen::Matrix<float, 3, 1, Eigen::DontAlign>;
19using GpuVec4 = Eigen::Matrix<float, 4, 1, Eigen::DontAlign>;
20// adapter type instead of typedef, to fit GLSL layout
21struct GpuMat3 {
22 /* implicit */ GpuMat3(Eigen::Matrix3f value) : _data() {
23 _data.topRows<3>() = value;
24 }
25 operator auto &() { return _data; }
26 operator const auto &() const { return _data; }
27
28private:
29 Eigen::Matrix<float, 4, 3, Eigen::DontAlign> _data;
30};
31using GpuMat4 = Eigen::Matrix<float, 4, 4, Eigen::ColMajor | Eigen::DontAlign>;
32
33namespace constants {
34 inline constexpr double Pi = 3.1415926535897932;
35 inline constexpr float Pif = 3.141592654f;
36 inline constexpr double Pi_2 = 1.5707963267948966;
37 inline constexpr float Pi_2f = 1.5707963267f;
38} // namespace constants
39
40inline constexpr double deg2rad(double t) { return t * constants::Pi / 180.0; }
41inline constexpr float deg2rad(float t) { return t * constants::Pif / 180.0f; }
42inline constexpr float rad2deg(float t) { return t * 180.0f / constants::Pif; }
43
51
52template <std::floating_point T> struct Rad;
53template <std::floating_point T> struct Deg;
54
58template <std::floating_point T> struct Rad {
59 constexpr Rad() : _value(static_cast<T>(0.)) {}
60 constexpr Rad(T value) : _value(value) {}
61 constexpr Rad(Deg<T> value) : _value(deg2rad(T(value))) {}
62 constexpr operator T &() { return _value; }
63 constexpr operator T() const { return _value; }
64 /* implicit */ constexpr operator T *() { return &_value; }
65 template <typename U> constexpr bool operator==(const Rad<U> &other) const {
66 return _value == other._value;
67 }
68 template <typename U> constexpr bool operator==(const Deg<U> &other) const {
69 return (*this) == Rad(other);
70 }
71
72private:
73 T _value;
74};
75template <std::floating_point T> Rad(T) -> Rad<T>;
76
80template <std::floating_point T> struct Deg {
81 constexpr Deg() : _value(static_cast<T>(0.)) {}
82 constexpr Deg(T value) : _value(value) {}
83 constexpr Deg(Rad<T> value) : _value(rad2deg(T(value))) {}
84 constexpr operator T &() { return _value; }
85 constexpr operator T() const { return _value; }
86 /* implicit */ constexpr operator T *() { return &_value; }
87 template <typename U> constexpr bool operator==(const Deg<U> &other) const {
88 return _value == other._value;
89 }
90 template <typename U> constexpr bool operator==(const Rad<U> &other) const {
91 return _value == Deg(other);
92 }
93
94private:
95 T _value;
96};
97template <std::floating_point T> Deg(T) -> Deg<T>;
98
99template <std::floating_point T>
100constexpr Rad<T> operator*(const Rad<T> &left, const T &right) {
101 return Rad<T>{T(left) * right};
102}
103
104template <std::floating_point T>
105constexpr Rad<T> operator*(const T &left, const Rad<T> &right) {
106 return Rad<T>{left * T(right)};
107}
108
109inline constexpr auto operator""_radf(long double t) {
110 return Rad<float>(static_cast<float>(t));
111}
112
113inline constexpr auto operator""_rad(long double t) {
114 return Rad<double>(static_cast<double>(t));
115}
116
117inline constexpr auto operator""_deg(long double t) {
118 return Deg<double>{static_cast<double>(t)};
119}
120
121inline constexpr auto operator""_degf(long double t) {
122 return Deg<float>{static_cast<float>(t)};
123}
124
127
128extern template struct Rad<float>;
129extern template struct Deg<float>;
130
132
133Vec3u8 hexToRgbi(unsigned long hex);
134
135Vec4u8 hexToRgbai(unsigned long hex);
136
137inline Float3 hexToRgbf(unsigned long hex) {
138 return hexToRgbi(hex).cast<float>() / 255.f;
139};
140
141inline Float4 hexToRgbaf(unsigned long hex) {
142 return hexToRgbai(hex).cast<float>() / 255.f;
143};
144
145inline Float3 operator""_rgbf(unsigned long long hex) { return hexToRgbf(hex); }
146
147inline Float4 operator""_rgbaf(unsigned long long hex) {
148 return hexToRgbaf(hex);
149}
150
151inline Eigen::Vector3d operator""_rgb(unsigned long long hex) {
152 return hexToRgbi(hex).cast<double>() / 255.;
153}
154
155inline Eigen::Vector4d operator""_rgba(unsigned long long hex) {
156 return hexToRgbai(hex).cast<double>() / 255.;
157}
158
159namespace math {
160 constexpr Uint32 roundUpTo16(Uint32 value) {
161 Uint32 q = value / 16;
162 Uint32 r = value % 16;
163 if (r == 0)
164 return value;
165 return (q + 1) * 16u;
166 }
167
168 inline Mat3f computeNormalMatrix(const Mat4f &M) {
169 return M.topLeftCorner<3, 3>().inverse().transpose();
170 }
171} // namespace math
172
173class Mesh;
174
177using OpaqueCastable = std::tuple<const Mesh &, Mat4f>;
178
179} // namespace candlewick
Deg(T) -> Deg< T >
Deg< float > Degf
Definition math_types.h:126
constexpr Rad< T > operator*(const Rad< T > &left, const T &right)
Definition math_types.h:100
Rad(T) -> Rad< T >
Rad< float > Radf
Definition math_types.h:125
Definition math_types.h:33
constexpr float Pi_2f
Definition math_types.h:37
constexpr double Pi_2
Definition math_types.h:36
constexpr float Pif
Definition math_types.h:35
constexpr double Pi
Definition math_types.h:34
constexpr Uint32 roundUpTo16(Uint32 value)
Definition math_types.h:160
Mat3f computeNormalMatrix(const Eigen::Affine3f &M)
Definition Camera.h:11
Definition Camera.h:8
Eigen::Vector3f Float3
Definition math_types.h:8
Eigen::Matrix< float, 2, 1, Eigen::DontAlign > GpuVec2
Definition math_types.h:17
Eigen::Matrix< float, 4, 4, Eigen::ColMajor|Eigen::DontAlign > GpuMat4
Definition math_types.h:31
Eigen::Matrix< Uint8, 3, 1 > Vec3u8
Definition math_types.h:12
Eigen::Matrix3f Mat3f
Definition math_types.h:10
constexpr float rad2deg(float t)
Definition math_types.h:42
constexpr double deg2rad(double t)
Definition math_types.h:40
Float3 hexToRgbf(unsigned long hex)
Definition math_types.h:137
Vec4u8 hexToRgbai(unsigned long hex)
Eigen::Matrix< float, 3, 1, Eigen::DontAlign > GpuVec3
Definition math_types.h:18
Eigen::Vector4f Float4
Definition math_types.h:9
Eigen::Matrix< Uint8, 4, 1 > Vec4u8
Definition math_types.h:13
Float4 hexToRgbaf(unsigned long hex)
Definition math_types.h:141
Vec3u8 hexToRgbi(unsigned long hex)
std::tuple< const Mesh &, Mat4f > OpaqueCastable
Intermediary argument type for shadow-casting or opaque objects. For use in depth or light pre-passes...
Definition math_types.h:177
Eigen::Matrix< float, 4, 1, Eigen::DontAlign > GpuVec4
Definition math_types.h:19
std::array< Float3, 8ul > FrustumCornersType
Definition math_types.h:15
Eigen::Vector2f Float2
Definition math_types.h:7
Eigen::Matrix4f Mat4f
Definition math_types.h:11
Strong type for floating-point variables representing angles (in degrees).
Definition math_types.h:80
constexpr Deg()
Definition math_types.h:81
constexpr Deg(Rad< T > value)
Definition math_types.h:83
constexpr bool operator==(const Deg< U > &other) const
Definition math_types.h:87
constexpr bool operator==(const Rad< U > &other) const
Definition math_types.h:90
constexpr Deg(T value)
Definition math_types.h:82
GpuMat3(Eigen::Matrix3f value)
Definition math_types.h:22
Strong type for floating-point variables representing angles (in radians).
Definition math_types.h:58
constexpr bool operator==(const Rad< U > &other) const
Definition math_types.h:65
constexpr bool operator==(const Deg< U > &other) const
Definition math_types.h:68
constexpr Rad(T value)
Definition math_types.h:60
constexpr Rad(Deg< T > value)
Definition math_types.h:61
constexpr Rad()
Definition math_types.h:59