Skip to content

Commit 101ae38

Browse files
authored
Merge pull request #2047 from marcozee03/sync-godot-math-structs
Sync math structs by adding constexpr constructors/operators
2 parents 5ed72a0 + 93bcf21 commit 101ae38

32 files changed

Lines changed: 1484 additions & 1213 deletions

‎include/godot_cpp/core/math.hpp‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,32 @@ constexpr int64_t division_round_up(int64_t p_num, int64_t p_den) {
295295
constexpr uint64_t division_round_up(uint64_t p_num, uint64_t p_den) {
296296
return (p_num + p_den - 1) / p_den;
297297
}
298-
298+
GODOT_MSVC_WARNING_PUSH_AND_IGNORE(4146) // Unary minus operator applied to unsigned type, result still unsigned
299+
constexpr int32_t division_no_overflow(int32_t p_num, int32_t p_den) {
300+
if (unlikely(p_den == -1)) {
301+
return int32_t(-uint32_t(p_num));
302+
}
303+
return p_num / p_den;
304+
}
305+
constexpr int64_t division_no_overflow(int64_t p_num, int64_t p_den) {
306+
if (unlikely(p_den == -1)) {
307+
return int64_t(-uint64_t(p_num));
308+
}
309+
return p_num / p_den;
310+
}
311+
GODOT_MSVC_WARNING_POP
312+
constexpr int32_t modulo_no_overflow(int32_t p_num, int32_t p_den) {
313+
if (unlikely(p_den == -1)) {
314+
return 0;
315+
}
316+
return p_num % p_den;
317+
}
318+
constexpr int64_t modulo_no_overflow(int64_t p_num, int64_t p_den) {
319+
if (unlikely(p_den == -1)) {
320+
return 0;
321+
}
322+
return p_num % p_den;
323+
}
299324
constexpr bool is_finite(double p_val) {
300325
return !is_nan(p_val) && !is_inf(p_val);
301326
}

‎include/godot_cpp/variant/aabb.hpp‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ struct [[nodiscard]] AABB {
6060
const Vector3 &get_size() const { return size; }
6161
void set_size(const Vector3 &p_size) { size = p_size; }
6262

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

6670
bool is_equal_approx(const AABB &p_aabb) const;
71+
bool is_same(const AABB &p_aabb) const;
6772
bool is_finite() const;
6873
_FORCE_INLINE_ bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap
6974
_FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap
@@ -128,10 +133,10 @@ struct [[nodiscard]] AABB {
128133
return position + (size * 0.5f);
129134
}
130135

131-
operator String() const;
136+
explicit operator String() const;
132137

133-
_FORCE_INLINE_ AABB() {}
134-
inline AABB(const Vector3 &p_pos, const Vector3 &p_size) :
138+
AABB() = default;
139+
constexpr AABB(const Vector3 &p_pos, const Vector3 &p_size) :
135140
position(p_pos),
136141
size(p_size) {
137142
}
@@ -273,10 +278,10 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, con
273278

274279
for (int k = 0; k < 3; k++) {
275280
for (int i = 0; i < p_point_count; i++) {
276-
if (p_points[i].coord[k] > ofs.coord[k] + half_extents.coord[k]) {
281+
if (p_points[i][k] > ofs[k] + half_extents[k]) {
277282
bad_point_counts_positive[k]++;
278283
}
279-
if (p_points[i].coord[k] < ofs.coord[k] - half_extents.coord[k]) {
284+
if (p_points[i][k] < ofs[k] - half_extents[k]) {
280285
bad_point_counts_negative[k]++;
281286
}
282287
}
@@ -497,4 +502,6 @@ AABB AABB::quantized(real_t p_unit) const {
497502
return ret;
498503
}
499504

505+
template <>
506+
struct is_zero_constructible<AABB> : std::true_type {};
500507
} // namespace godot

‎include/godot_cpp/variant/basis.hpp‎

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,20 @@
3737
namespace godot {
3838

3939
struct [[nodiscard]] Basis {
40+
static const Basis FLIP_X;
41+
static const Basis FLIP_Y;
42+
static const Basis FLIP_Z;
43+
4044
Vector3 rows[3] = {
4145
Vector3(1, 0, 0),
4246
Vector3(0, 1, 0),
4347
Vector3(0, 0, 1)
4448
};
4549

46-
_FORCE_INLINE_ const Vector3 &operator[](int p_row) const {
50+
constexpr const Vector3 &operator[](int p_row) const {
4751
return rows[p_row];
4852
}
49-
_FORCE_INLINE_ Vector3 &operator[](int p_row) {
53+
constexpr Vector3 &operator[](int p_row) {
5054
return rows[p_row];
5155
}
5256

@@ -77,7 +81,7 @@ struct [[nodiscard]] Basis {
7781

7882
void rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction);
7983

80-
Vector3 rotref_posscale_decomposition(Basis &rotref) const;
84+
Vector3 rotref_posscale_decomposition(Basis &r_rotref) const;
8185

8286
Vector3 get_euler(EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) const;
8387
void set_euler(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ);
@@ -123,23 +127,24 @@ struct [[nodiscard]] Basis {
123127
}
124128

125129
bool is_equal_approx(const Basis &p_basis) const;
130+
bool is_same(const Basis &p_basis) const;
126131
bool is_finite() const;
127132

128-
bool operator==(const Basis &p_matrix) const;
129-
bool operator!=(const Basis &p_matrix) const;
133+
constexpr bool operator==(const Basis &p_matrix) const;
134+
constexpr bool operator!=(const Basis &p_matrix) const;
130135

131136
_FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
132137
_FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
133138
_FORCE_INLINE_ void operator*=(const Basis &p_matrix);
134139
_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-=(const Basis &p_matrix);
138-
_FORCE_INLINE_ Basis operator-(const Basis &p_matrix) const;
139-
_FORCE_INLINE_ void operator*=(real_t p_val);
140-
_FORCE_INLINE_ Basis operator*(real_t p_val) const;
141-
_FORCE_INLINE_ void operator/=(real_t p_val);
142-
_FORCE_INLINE_ Basis operator/(real_t p_val) const;
140+
constexpr void operator+=(const Basis &p_matrix);
141+
constexpr Basis operator+(const Basis &p_matrix) const;
142+
constexpr void operator-=(const Basis &p_matrix);
143+
constexpr Basis operator-(const Basis &p_matrix) const;
144+
constexpr void operator*=(real_t p_val);
145+
constexpr Basis operator*(real_t p_val) const;
146+
constexpr void operator/=(real_t p_val);
147+
constexpr Basis operator/(real_t p_val) const;
143148

144149
bool is_orthogonal() const;
145150
bool is_orthonormal() const;
@@ -151,7 +156,7 @@ struct [[nodiscard]] Basis {
151156
Basis slerp(const Basis &p_to, real_t p_weight) const;
152157
void rotate_sh(real_t *p_values);
153158

154-
operator String() const;
159+
explicit operator String() const;
155160

156161
/* create / set */
157162

@@ -206,9 +211,12 @@ struct [[nodiscard]] Basis {
206211
rows[0].z * p_m[0].y + rows[1].z * p_m[1].y + rows[2].z * p_m[2].y,
207212
rows[0].z * p_m[0].z + rows[1].z * p_m[1].z + rows[2].z * p_m[2].z);
208213
}
209-
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) {
210-
set(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz);
211-
}
214+
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) :
215+
rows{
216+
{ p_xx, p_xy, p_xz },
217+
{ p_yx, p_yy, p_yz },
218+
{ p_zx, p_zy, p_zz },
219+
} {}
212220

213221
void orthonormalize();
214222
Basis orthonormalized() const;
@@ -223,7 +231,7 @@ struct [[nodiscard]] Basis {
223231

224232
operator Quaternion() const { return get_quaternion(); }
225233

226-
static Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false);
234+
static Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3::UP, bool p_use_model_front = false);
227235

228236
Basis(const Quaternion &p_quaternion) { set_quaternion(p_quaternion); }
229237
Basis(const Quaternion &p_quaternion, const Vector3 &p_scale) { set_quaternion_scale(p_quaternion, p_scale); }
@@ -232,17 +240,40 @@ struct [[nodiscard]] Basis {
232240
Basis(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_angle, p_scale); }
233241
static Basis from_scale(const Vector3 &p_scale);
234242

235-
_FORCE_INLINE_ Basis(const Vector3 &p_x_axis, const Vector3 &p_y_axis, const Vector3 &p_z_axis) {
236-
set_columns(p_x_axis, p_y_axis, p_z_axis);
237-
}
243+
constexpr Basis(const Vector3 &p_x_axis, const Vector3 &p_y_axis, const Vector3 &p_z_axis) :
244+
rows{
245+
{ p_x_axis.x, p_y_axis.x, p_z_axis.x },
246+
{ p_x_axis.y, p_y_axis.y, p_z_axis.y },
247+
{ p_x_axis.z, p_y_axis.z, p_z_axis.z },
248+
} {}
238249

239-
_FORCE_INLINE_ Basis() {}
250+
Basis() = default;
240251

241252
private:
242253
// Helper method.
243254
void _set_diagonal(const Vector3 &p_diag);
244255
};
245256

257+
inline constexpr Basis Basis::FLIP_X = { { -1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
258+
inline constexpr Basis Basis::FLIP_Y = { { 1, 0, 0 }, { 0, -1, 0 }, { 0, 0, 1 } };
259+
inline constexpr Basis Basis::FLIP_Z = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, -1 } };
260+
261+
constexpr bool Basis::operator==(const Basis &p_matrix) const {
262+
for (int i = 0; i < 3; i++) {
263+
for (int j = 0; j < 3; j++) {
264+
if (rows[i][j] != p_matrix.rows[i][j]) {
265+
return false;
266+
}
267+
}
268+
}
269+
270+
return true;
271+
}
272+
273+
constexpr bool Basis::operator!=(const Basis &p_matrix) const {
274+
return (!(*this == p_matrix));
275+
}
276+
246277
_FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) {
247278
set(
248279
p_matrix.tdotx(rows[0]), p_matrix.tdoty(rows[0]), p_matrix.tdotz(rows[0]),
@@ -257,49 +288,49 @@ _FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const {
257288
p_matrix.tdotx(rows[2]), p_matrix.tdoty(rows[2]), p_matrix.tdotz(rows[2]));
258289
}
259290

260-
_FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) {
291+
constexpr void Basis::operator+=(const Basis &p_matrix) {
261292
rows[0] += p_matrix.rows[0];
262293
rows[1] += p_matrix.rows[1];
263294
rows[2] += p_matrix.rows[2];
264295
}
265296

266-
_FORCE_INLINE_ Basis Basis::operator+(const Basis &p_matrix) const {
297+
constexpr Basis Basis::operator+(const Basis &p_matrix) const {
267298
Basis ret(*this);
268299
ret += p_matrix;
269300
return ret;
270301
}
271302

272-
_FORCE_INLINE_ void Basis::operator-=(const Basis &p_matrix) {
303+
constexpr void Basis::operator-=(const Basis &p_matrix) {
273304
rows[0] -= p_matrix.rows[0];
274305
rows[1] -= p_matrix.rows[1];
275306
rows[2] -= p_matrix.rows[2];
276307
}
277308

278-
_FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const {
309+
constexpr Basis Basis::operator-(const Basis &p_matrix) const {
279310
Basis ret(*this);
280311
ret -= p_matrix;
281312
return ret;
282313
}
283314

284-
_FORCE_INLINE_ void Basis::operator*=(real_t p_val) {
315+
constexpr void Basis::operator*=(real_t p_val) {
285316
rows[0] *= p_val;
286317
rows[1] *= p_val;
287318
rows[2] *= p_val;
288319
}
289320

290-
_FORCE_INLINE_ Basis Basis::operator*(real_t p_val) const {
321+
constexpr Basis Basis::operator*(real_t p_val) const {
291322
Basis ret(*this);
292323
ret *= p_val;
293324
return ret;
294325
}
295326

296-
_FORCE_INLINE_ void Basis::operator/=(real_t p_val) {
327+
constexpr void Basis::operator/=(real_t p_val) {
297328
rows[0] /= p_val;
298329
rows[1] /= p_val;
299330
rows[2] /= p_val;
300331
}
301332

302-
_FORCE_INLINE_ Basis Basis::operator/(real_t p_val) const {
333+
constexpr Basis Basis::operator/(real_t p_val) const {
303334
Basis ret(*this);
304335
ret /= p_val;
305336
return ret;

0 commit comments

Comments
 (0)