Skip to content

Commit c66ea9f

Browse files
committed
Core: Add constexpr constructors/operators to math structs
• Integrate `constexpr` on math operator tests; use `static_assert` where appropriate
1 parent 9e6ee9c commit c66ea9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1120
-1169
lines changed

core/math/aabb.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ real_t AABB::get_volume() const {
3737
return size.x * size.y * size.z;
3838
}
3939

40-
bool AABB::operator==(const AABB &p_rval) const {
41-
return ((position == p_rval.position) && (size == p_rval.size));
42-
}
43-
44-
bool AABB::operator!=(const AABB &p_rval) const {
45-
return ((position != p_rval.position) || (size != p_rval.size));
46-
}
47-
4840
void AABB::merge_with(const AABB &p_aabb) {
4941
#ifdef MATH_CHECKS
5042
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) {

core/math/aabb.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ struct [[nodiscard]] AABB {
5858
const Vector3 &get_size() const { return size; }
5959
void set_size(const Vector3 &p_size) { size = p_size; }
6060

61-
bool operator==(const AABB &p_rval) const;
62-
bool operator!=(const AABB &p_rval) const;
61+
constexpr bool operator==(const AABB &p_rval) const {
62+
return position == p_rval.position && size == p_rval.size;
63+
}
64+
constexpr bool operator!=(const AABB &p_rval) const {
65+
return position != p_rval.position || size != p_rval.size;
66+
}
6367

6468
bool is_equal_approx(const AABB &p_aabb) const;
6569
bool is_same(const AABB &p_aabb) const;
@@ -129,8 +133,8 @@ struct [[nodiscard]] AABB {
129133

130134
operator String() const;
131135

132-
_FORCE_INLINE_ AABB() {}
133-
inline AABB(const Vector3 &p_pos, const Vector3 &p_size) :
136+
AABB() = default;
137+
constexpr AABB(const Vector3 &p_pos, const Vector3 &p_size) :
134138
position(p_pos),
135139
size(p_size) {
136140
}

core/math/audio_frame.h

+35-34
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static const float AUDIO_MIN_PEAK_DB = -200.0f; // linear_to_db(AUDIO_PEAK_OFFSE
5252
struct AudioFrame {
5353
// Left and right samples.
5454
union {
55+
// NOLINTBEGIN(modernize-use-default-member-init)
5556
struct {
5657
float left;
5758
float right;
@@ -63,6 +64,7 @@ struct AudioFrame {
6364
};
6465
#endif
6566
float levels[2] = { 0.0 };
67+
// NOLINTEND(modernize-use-default-member-init)
6668
};
6769

6870
_ALWAYS_INLINE_ const float &operator[](int p_idx) const {
@@ -74,46 +76,46 @@ struct AudioFrame {
7476
return levels[p_idx];
7577
}
7678

77-
_ALWAYS_INLINE_ AudioFrame operator+(const AudioFrame &p_frame) const { return AudioFrame(left + p_frame.left, right + p_frame.right); }
78-
_ALWAYS_INLINE_ AudioFrame operator-(const AudioFrame &p_frame) const { return AudioFrame(left - p_frame.left, right - p_frame.right); }
79-
_ALWAYS_INLINE_ AudioFrame operator*(const AudioFrame &p_frame) const { return AudioFrame(left * p_frame.left, right * p_frame.right); }
80-
_ALWAYS_INLINE_ AudioFrame operator/(const AudioFrame &p_frame) const { return AudioFrame(left / p_frame.left, right / p_frame.right); }
79+
constexpr AudioFrame operator+(const AudioFrame &p_frame) const { return AudioFrame(left + p_frame.left, right + p_frame.right); }
80+
constexpr AudioFrame operator-(const AudioFrame &p_frame) const { return AudioFrame(left - p_frame.left, right - p_frame.right); }
81+
constexpr AudioFrame operator*(const AudioFrame &p_frame) const { return AudioFrame(left * p_frame.left, right * p_frame.right); }
82+
constexpr AudioFrame operator/(const AudioFrame &p_frame) const { return AudioFrame(left / p_frame.left, right / p_frame.right); }
8183

82-
_ALWAYS_INLINE_ AudioFrame operator+(float p_sample) const { return AudioFrame(left + p_sample, right + p_sample); }
83-
_ALWAYS_INLINE_ AudioFrame operator-(float p_sample) const { return AudioFrame(left - p_sample, right - p_sample); }
84-
_ALWAYS_INLINE_ AudioFrame operator*(float p_sample) const { return AudioFrame(left * p_sample, right * p_sample); }
85-
_ALWAYS_INLINE_ AudioFrame operator/(float p_sample) const { return AudioFrame(left / p_sample, right / p_sample); }
84+
constexpr AudioFrame operator+(float p_sample) const { return AudioFrame(left + p_sample, right + p_sample); }
85+
constexpr AudioFrame operator-(float p_sample) const { return AudioFrame(left - p_sample, right - p_sample); }
86+
constexpr AudioFrame operator*(float p_sample) const { return AudioFrame(left * p_sample, right * p_sample); }
87+
constexpr AudioFrame operator/(float p_sample) const { return AudioFrame(left / p_sample, right / p_sample); }
8688

87-
_ALWAYS_INLINE_ void operator+=(const AudioFrame &p_frame) {
89+
constexpr void operator+=(const AudioFrame &p_frame) {
8890
left += p_frame.left;
8991
right += p_frame.right;
9092
}
91-
_ALWAYS_INLINE_ void operator-=(const AudioFrame &p_frame) {
93+
constexpr void operator-=(const AudioFrame &p_frame) {
9294
left -= p_frame.left;
9395
right -= p_frame.right;
9496
}
95-
_ALWAYS_INLINE_ void operator*=(const AudioFrame &p_frame) {
97+
constexpr void operator*=(const AudioFrame &p_frame) {
9698
left *= p_frame.left;
9799
right *= p_frame.right;
98100
}
99-
_ALWAYS_INLINE_ void operator/=(const AudioFrame &p_frame) {
101+
constexpr void operator/=(const AudioFrame &p_frame) {
100102
left /= p_frame.left;
101103
right /= p_frame.right;
102104
}
103105

104-
_ALWAYS_INLINE_ void operator+=(float p_sample) {
106+
constexpr void operator+=(float p_sample) {
105107
left += p_sample;
106108
right += p_sample;
107109
}
108-
_ALWAYS_INLINE_ void operator-=(float p_sample) {
110+
constexpr void operator-=(float p_sample) {
109111
left -= p_sample;
110112
right -= p_sample;
111113
}
112-
_ALWAYS_INLINE_ void operator*=(float p_sample) {
114+
constexpr void operator*=(float p_sample) {
113115
left *= p_sample;
114116
right *= p_sample;
115117
}
116-
_ALWAYS_INLINE_ void operator/=(float p_sample) {
118+
constexpr void operator/=(float p_sample) {
117119
left /= p_sample;
118120
right /= p_sample;
119121
}
@@ -132,40 +134,39 @@ struct AudioFrame {
132134
return res;
133135
}
134136

135-
_ALWAYS_INLINE_ AudioFrame(float p_left, float p_right) {
136-
left = p_left;
137-
right = p_right;
138-
}
139-
_ALWAYS_INLINE_ AudioFrame(const AudioFrame &p_frame) {
140-
left = p_frame.left;
141-
right = p_frame.right;
142-
}
137+
// NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
138+
constexpr AudioFrame(float p_left, float p_right) :
139+
left(p_left), right(p_right) {}
140+
constexpr AudioFrame(const AudioFrame &p_frame) :
141+
left(p_frame.left), right(p_frame.right) {}
142+
// NOLINTEND(cppcoreguidelines-pro-type-member-init)
143143

144-
_ALWAYS_INLINE_ void operator=(const AudioFrame &p_frame) {
144+
constexpr void operator=(const AudioFrame &p_frame) {
145145
left = p_frame.left;
146146
right = p_frame.right;
147147
}
148148

149-
_ALWAYS_INLINE_ operator Vector2() const {
149+
constexpr operator Vector2() const {
150150
return Vector2(left, right);
151151
}
152152

153-
_ALWAYS_INLINE_ AudioFrame(const Vector2 &p_v2) {
154-
left = p_v2.x;
155-
right = p_v2.y;
156-
}
157-
_ALWAYS_INLINE_ AudioFrame() {}
153+
// NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
154+
constexpr AudioFrame(const Vector2 &p_v2) :
155+
left(p_v2.x), right(p_v2.y) {}
156+
constexpr AudioFrame() :
157+
left(0), right(0) {}
158+
// NOLINTEND(cppcoreguidelines-pro-type-member-init)
158159
};
159160

160-
_ALWAYS_INLINE_ AudioFrame operator*(float p_scalar, const AudioFrame &p_frame) {
161+
constexpr AudioFrame operator*(float p_scalar, const AudioFrame &p_frame) {
161162
return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar);
162163
}
163164

164-
_ALWAYS_INLINE_ AudioFrame operator*(int32_t p_scalar, const AudioFrame &p_frame) {
165+
constexpr AudioFrame operator*(int32_t p_scalar, const AudioFrame &p_frame) {
165166
return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar);
166167
}
167168

168-
_ALWAYS_INLINE_ AudioFrame operator*(int64_t p_scalar, const AudioFrame &p_frame) {
169+
constexpr AudioFrame operator*(int64_t p_scalar, const AudioFrame &p_frame) {
169170
return AudioFrame(p_frame.left * p_scalar, p_frame.right * p_scalar);
170171
}
171172

core/math/basis.cpp

-16
Original file line numberDiff line numberDiff line change
@@ -707,22 +707,6 @@ bool Basis::is_finite() const {
707707
return rows[0].is_finite() && rows[1].is_finite() && rows[2].is_finite();
708708
}
709709

710-
bool Basis::operator==(const Basis &p_matrix) const {
711-
for (int i = 0; i < 3; i++) {
712-
for (int j = 0; j < 3; j++) {
713-
if (rows[i][j] != p_matrix.rows[i][j]) {
714-
return false;
715-
}
716-
}
717-
}
718-
719-
return true;
720-
}
721-
722-
bool Basis::operator!=(const Basis &p_matrix) const {
723-
return (!(*this == p_matrix));
724-
}
725-
726710
Basis::operator String() const {
727711
return "[X: " + get_column(0).operator String() +
728712
", Y: " + get_column(1).operator String() +

core/math/basis.h

+49-27
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ struct [[nodiscard]] Basis {
4040
Vector3(0, 0, 1)
4141
};
4242

43-
_FORCE_INLINE_ const Vector3 &operator[](int p_row) const {
43+
constexpr const Vector3 &operator[](int p_row) const {
4444
return rows[p_row];
4545
}
46-
_FORCE_INLINE_ Vector3 &operator[](int p_row) {
46+
constexpr Vector3 &operator[](int p_row) {
4747
return rows[p_row];
4848
}
4949

@@ -123,21 +123,21 @@ struct [[nodiscard]] Basis {
123123
bool is_same(const Basis &p_basis) const;
124124
bool is_finite() const;
125125

126-
bool operator==(const Basis &p_matrix) const;
127-
bool operator!=(const Basis &p_matrix) const;
126+
constexpr bool operator==(const Basis &p_matrix) const;
127+
constexpr bool operator!=(const Basis &p_matrix) const;
128128

129129
_FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
130130
_FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
131131
_FORCE_INLINE_ void operator*=(const Basis &p_matrix);
132132
_FORCE_INLINE_ Basis operator*(const Basis &p_matrix) const;
133-
_FORCE_INLINE_ void operator+=(const Basis &p_matrix);
134-
_FORCE_INLINE_ Basis operator+(const Basis &p_matrix) const;
135-
_FORCE_INLINE_ void operator-=(const Basis &p_matrix);
136-
_FORCE_INLINE_ Basis operator-(const Basis &p_matrix) const;
137-
_FORCE_INLINE_ void operator*=(real_t p_val);
138-
_FORCE_INLINE_ Basis operator*(real_t p_val) const;
139-
_FORCE_INLINE_ void operator/=(real_t p_val);
140-
_FORCE_INLINE_ Basis operator/(real_t p_val) const;
133+
constexpr void operator+=(const Basis &p_matrix);
134+
constexpr Basis operator+(const Basis &p_matrix) const;
135+
constexpr void operator-=(const Basis &p_matrix);
136+
constexpr Basis operator-(const Basis &p_matrix) const;
137+
constexpr void operator*=(real_t p_val);
138+
constexpr Basis operator*(real_t p_val) const;
139+
constexpr void operator/=(real_t p_val);
140+
constexpr Basis operator/(real_t p_val) const;
141141

142142
bool is_orthogonal() const;
143143
bool is_orthonormal() const;
@@ -204,9 +204,12 @@ struct [[nodiscard]] Basis {
204204
rows[0].z * p_m[0].y + rows[1].z * p_m[1].y + rows[2].z * p_m[2].y,
205205
rows[0].z * p_m[0].z + rows[1].z * p_m[1].z + rows[2].z * p_m[2].z);
206206
}
207-
Basis(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz) {
208-
set(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz);
209-
}
207+
constexpr Basis(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz) :
208+
rows{
209+
{ p_xx, p_xy, p_xz },
210+
{ p_yx, p_yy, p_yz },
211+
{ p_zx, p_zy, p_zz },
212+
} {}
210213

211214
void orthonormalize();
212215
Basis orthonormalized() const;
@@ -230,17 +233,36 @@ struct [[nodiscard]] Basis {
230233
Basis(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_angle, p_scale); }
231234
static Basis from_scale(const Vector3 &p_scale);
232235

233-
_FORCE_INLINE_ Basis(const Vector3 &p_x_axis, const Vector3 &p_y_axis, const Vector3 &p_z_axis) {
234-
set_columns(p_x_axis, p_y_axis, p_z_axis);
235-
}
236+
constexpr Basis(const Vector3 &p_x_axis, const Vector3 &p_y_axis, const Vector3 &p_z_axis) :
237+
rows{
238+
{ p_x_axis.x, p_y_axis.x, p_z_axis.x },
239+
{ p_x_axis.y, p_y_axis.y, p_z_axis.y },
240+
{ p_x_axis.z, p_y_axis.z, p_z_axis.z },
241+
} {}
236242

237-
_FORCE_INLINE_ Basis() {}
243+
Basis() = default;
238244

239245
private:
240246
// Helper method.
241247
void _set_diagonal(const Vector3 &p_diag);
242248
};
243249

250+
constexpr bool Basis::operator==(const Basis &p_matrix) const {
251+
for (int i = 0; i < 3; i++) {
252+
for (int j = 0; j < 3; j++) {
253+
if (rows[i][j] != p_matrix.rows[i][j]) {
254+
return false;
255+
}
256+
}
257+
}
258+
259+
return true;
260+
}
261+
262+
constexpr bool Basis::operator!=(const Basis &p_matrix) const {
263+
return (!(*this == p_matrix));
264+
}
265+
244266
_FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) {
245267
set(
246268
p_matrix.tdotx(rows[0]), p_matrix.tdoty(rows[0]), p_matrix.tdotz(rows[0]),
@@ -255,49 +277,49 @@ _FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const {
255277
p_matrix.tdotx(rows[2]), p_matrix.tdoty(rows[2]), p_matrix.tdotz(rows[2]));
256278
}
257279

258-
_FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) {
280+
constexpr void Basis::operator+=(const Basis &p_matrix) {
259281
rows[0] += p_matrix.rows[0];
260282
rows[1] += p_matrix.rows[1];
261283
rows[2] += p_matrix.rows[2];
262284
}
263285

264-
_FORCE_INLINE_ Basis Basis::operator+(const Basis &p_matrix) const {
286+
constexpr Basis Basis::operator+(const Basis &p_matrix) const {
265287
Basis ret(*this);
266288
ret += p_matrix;
267289
return ret;
268290
}
269291

270-
_FORCE_INLINE_ void Basis::operator-=(const Basis &p_matrix) {
292+
constexpr void Basis::operator-=(const Basis &p_matrix) {
271293
rows[0] -= p_matrix.rows[0];
272294
rows[1] -= p_matrix.rows[1];
273295
rows[2] -= p_matrix.rows[2];
274296
}
275297

276-
_FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const {
298+
constexpr Basis Basis::operator-(const Basis &p_matrix) const {
277299
Basis ret(*this);
278300
ret -= p_matrix;
279301
return ret;
280302
}
281303

282-
_FORCE_INLINE_ void Basis::operator*=(real_t p_val) {
304+
constexpr void Basis::operator*=(real_t p_val) {
283305
rows[0] *= p_val;
284306
rows[1] *= p_val;
285307
rows[2] *= p_val;
286308
}
287309

288-
_FORCE_INLINE_ Basis Basis::operator*(real_t p_val) const {
310+
constexpr Basis Basis::operator*(real_t p_val) const {
289311
Basis ret(*this);
290312
ret *= p_val;
291313
return ret;
292314
}
293315

294-
_FORCE_INLINE_ void Basis::operator/=(real_t p_val) {
316+
constexpr void Basis::operator/=(real_t p_val) {
295317
rows[0] /= p_val;
296318
rows[1] /= p_val;
297319
rows[2] /= p_val;
298320
}
299321

300-
_FORCE_INLINE_ Basis Basis::operator/(real_t p_val) const {
322+
constexpr Basis Basis::operator/(real_t p_val) const {
301323
Basis ret(*this);
302324
ret /= p_val;
303325
return ret;

core/math/bvh.h

-2
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,6 @@ class BVH_Manager {
434434
return;
435435
}
436436

437-
BOUNDS bb;
438-
439437
typename BVHTREE_CLASS::CullParams params;
440438

441439
params.result_count_overall = 0;

0 commit comments

Comments
 (0)