Skip to content

Commit 2ec7434

Browse files
committed
Replace generic macros with math equivalents
1 parent c414c2b commit 2ec7434

21 files changed

+107
-109
lines changed

include/godot_cpp/core/math.hpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,31 @@ namespace godot {
5555
#undef MAX
5656
#undef CLAMP
5757

58-
// Generic ABS function, for math uses please use Math::abs.
58+
// DEPRECATED. Use `Math::abs` instead.
5959
template <typename T>
6060
constexpr T ABS(T m_v) {
6161
return m_v < 0 ? -m_v : m_v;
6262
}
6363

64+
// DEPRECATED. Use `Math::sign` instead.
6465
template <typename T>
6566
constexpr const T SIGN(const T m_v) {
6667
return m_v == 0 ? 0.0f : (m_v < 0 ? -1.0f : +1.0f);
6768
}
6869

70+
// DEPRECATED. Use `Math::min` instead.
6971
template <typename T, typename T2>
7072
constexpr auto MIN(const T m_a, const T2 m_b) {
7173
return m_a < m_b ? m_a : m_b;
7274
}
7375

76+
// DEPRECATED. Use `Math::max` instead.
7477
template <typename T, typename T2>
7578
constexpr auto MAX(const T m_a, const T2 m_b) {
7679
return m_a > m_b ? m_a : m_b;
7780
}
7881

82+
// DEPRECATED. Use `Math::clamp` instead.
7983
template <typename T, typename T2, typename T3>
8084
constexpr auto CLAMP(const T m_a, const T2 m_min, const T3 m_max) {
8185
return m_a < m_min ? m_min : (m_a > m_max ? m_max : m_a);
@@ -539,34 +543,28 @@ inline float bezier_interpolate(float p_start, float p_control_1, float p_contro
539543
}
540544

541545
template <typename T>
542-
inline T clamp(T x, T minv, T maxv) {
543-
if (x < minv) {
544-
return minv;
545-
}
546-
if (x > maxv) {
547-
return maxv;
548-
}
549-
return x;
546+
constexpr T clamp(T x, T minv, T maxv) {
547+
return x < minv ? minv : (x > maxv ? maxv : x);
550548
}
551549

552550
template <typename T>
553-
inline T min(T a, T b) {
551+
constexpr T min(T a, T b) {
554552
return a < b ? a : b;
555553
}
556554

557555
template <typename T>
558-
inline T max(T a, T b) {
556+
constexpr T max(T a, T b) {
559557
return a > b ? a : b;
560558
}
561559

562560
template <typename T>
563-
inline T sign(T x) {
564-
return static_cast<T>(SIGN(x));
561+
constexpr T sign(T x) {
562+
return x > T(0) ? T(1) : (x < T(0) ? T(-1) : T(0));
565563
}
566564

567565
template <typename T>
568-
inline T abs(T x) {
569-
return std::abs(x);
566+
constexpr T abs(T x) {
567+
return x == T(0) ? T(0) : (x < T(0) ? -x : x);
570568
}
571569

572570
inline double deg_to_rad(double p_y) {

include/godot_cpp/templates/hash_map.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class HashMap {
160160
uint32_t old_capacity = hash_table_size_primes[capacity_index];
161161

162162
// Capacity can't be 0.
163-
capacity_index = MAX((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
163+
capacity_index = Math::max((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
164164

165165
uint32_t capacity = hash_table_size_primes[capacity_index];
166166

include/godot_cpp/templates/hash_set.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class HashSet {
141141

142142
void _resize_and_rehash(uint32_t p_new_capacity_index) {
143143
// Capacity can't be 0.
144-
capacity_index = MAX((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
144+
capacity_index = Math::max((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
145145

146146
uint32_t capacity = hash_table_size_primes[capacity_index];
147147

include/godot_cpp/templates/vector.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ class Vector {
165165

166166
const Size s = size();
167167

168-
Size begin = CLAMP(p_begin, -s, s);
168+
Size begin = Math::clamp(p_begin, -s, s);
169169
if (begin < 0) {
170170
begin += s;
171171
}
172-
Size end = CLAMP(p_end, -s, s);
172+
Size end = Math::clamp(p_end, -s, s);
173173
if (end < 0) {
174174
end += s;
175175
}

include/godot_cpp/variant/color.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ struct _NO_DISCARD_ Color {
135135

136136
float sharedexp = 65408.000f; // Result of: ((pow2to9 - 1.0f) / pow2to9) * powf(2.0f, 31.0f - 15.0f)
137137

138-
float cRed = MAX(0.0f, MIN(sharedexp, r));
139-
float cGreen = MAX(0.0f, MIN(sharedexp, g));
140-
float cBlue = MAX(0.0f, MIN(sharedexp, b));
138+
float cRed = Math::max(0.0f, Math::min(sharedexp, r));
139+
float cGreen = Math::max(0.0f, Math::min(sharedexp, g));
140+
float cBlue = Math::max(0.0f, Math::min(sharedexp, b));
141141

142-
float cMax = MAX(cRed, MAX(cGreen, cBlue));
142+
float cMax = Math::max(cRed, Math::max(cGreen, cBlue));
143143

144-
float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / (real_t)Math_LN2)) + 1.0f + B;
144+
float expp = Math::max(-B - 1.0f, floor(Math::log(cMax) / (real_t)Math_LN2)) + 1.0f + B;
145145

146146
float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f);
147147

@@ -204,14 +204,14 @@ struct _NO_DISCARD_ Color {
204204
operator String() const;
205205

206206
// For the binder.
207-
_FORCE_INLINE_ void set_r8(int32_t r8) { r = (CLAMP(r8, 0, 255) / 255.0f); }
208-
_FORCE_INLINE_ int32_t get_r8() const { return int32_t(CLAMP(Math::round(r * 255.0f), 0.0f, 255.0f)); }
209-
_FORCE_INLINE_ void set_g8(int32_t g8) { g = (CLAMP(g8, 0, 255) / 255.0f); }
210-
_FORCE_INLINE_ int32_t get_g8() const { return int32_t(CLAMP(Math::round(g * 255.0f), 0.0f, 255.0f)); }
211-
_FORCE_INLINE_ void set_b8(int32_t b8) { b = (CLAMP(b8, 0, 255) / 255.0f); }
212-
_FORCE_INLINE_ int32_t get_b8() const { return int32_t(CLAMP(Math::round(b * 255.0f), 0.0f, 255.0f)); }
213-
_FORCE_INLINE_ void set_a8(int32_t a8) { a = (CLAMP(a8, 0, 255) / 255.0f); }
214-
_FORCE_INLINE_ int32_t get_a8() const { return int32_t(CLAMP(Math::round(a * 255.0f), 0.0f, 255.0f)); }
207+
_FORCE_INLINE_ void set_r8(int32_t r8) { r = (Math::clamp(r8, 0, 255) / 255.0f); }
208+
_FORCE_INLINE_ int32_t get_r8() const { return int32_t(Math::clamp(Math::round(r * 255.0f), 0.0f, 255.0f)); }
209+
_FORCE_INLINE_ void set_g8(int32_t g8) { g = (Math::clamp(g8, 0, 255) / 255.0f); }
210+
_FORCE_INLINE_ int32_t get_g8() const { return int32_t(Math::clamp(Math::round(g * 255.0f), 0.0f, 255.0f)); }
211+
_FORCE_INLINE_ void set_b8(int32_t b8) { b = (Math::clamp(b8, 0, 255) / 255.0f); }
212+
_FORCE_INLINE_ int32_t get_b8() const { return int32_t(Math::clamp(Math::round(b * 255.0f), 0.0f, 255.0f)); }
213+
_FORCE_INLINE_ void set_a8(int32_t a8) { a = (Math::clamp(a8, 0, 255) / 255.0f); }
214+
_FORCE_INLINE_ int32_t get_a8() const { return int32_t(Math::clamp(Math::round(a * 255.0f), 0.0f, 255.0f)); }
215215

216216
_FORCE_INLINE_ void set_h(float p_h) { set_hsv(p_h, get_s(), get_v(), a); }
217217
_FORCE_INLINE_ void set_s(float p_s) { set_hsv(get_h(), p_s, get_v(), a); }

include/godot_cpp/variant/vector2.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,19 @@ struct _NO_DISCARD_ Vector2 {
8888
Vector2 limit_length(const real_t p_len = 1.0) const;
8989

9090
Vector2 min(const Vector2 &p_vector2) const {
91-
return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
91+
return Vector2(Math::min(x, p_vector2.x), Math::min(y, p_vector2.y));
9292
}
9393

9494
Vector2 minf(real_t p_scalar) const {
95-
return Vector2(MIN(x, p_scalar), MIN(y, p_scalar));
95+
return Vector2(Math::min(x, p_scalar), Math::min(y, p_scalar));
9696
}
9797

9898
Vector2 max(const Vector2 &p_vector2) const {
99-
return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y));
99+
return Vector2(Math::max(x, p_vector2.x), Math::max(y, p_vector2.y));
100100
}
101101

102102
Vector2 maxf(real_t p_scalar) const {
103-
return Vector2(MAX(x, p_scalar), MAX(y, p_scalar));
103+
return Vector2(Math::max(x, p_scalar), Math::max(y, p_scalar));
104104
}
105105

106106
real_t distance_to(const Vector2 &p_vector2) const;

include/godot_cpp/variant/vector2i.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,19 @@ struct _NO_DISCARD_ Vector2i {
8080
}
8181

8282
Vector2i min(const Vector2i &p_vector2i) const {
83-
return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
83+
return Vector2i(Math::min(x, p_vector2i.x), Math::min(y, p_vector2i.y));
8484
}
8585

8686
Vector2i mini(int32_t p_scalar) const {
87-
return Vector2i(MIN(x, p_scalar), MIN(y, p_scalar));
87+
return Vector2i(Math::min(x, p_scalar), Math::min(y, p_scalar));
8888
}
8989

9090
Vector2i max(const Vector2i &p_vector2i) const {
91-
return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
91+
return Vector2i(Math::max(x, p_vector2i.x), Math::max(y, p_vector2i.y));
9292
}
9393

9494
Vector2i maxi(int32_t p_scalar) const {
95-
return Vector2i(MAX(x, p_scalar), MAX(y, p_scalar));
95+
return Vector2i(Math::max(x, p_scalar), Math::max(y, p_scalar));
9696
}
9797

9898
Vector2i operator+(const Vector2i &p_v) const;
@@ -129,7 +129,7 @@ struct _NO_DISCARD_ Vector2i {
129129
double distance_to(const Vector2i &p_to) const;
130130

131131
real_t aspect() const { return width / (real_t)height; }
132-
Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
132+
Vector2i sign() const { return Vector2i(Math::sign(x), Math::sign(y)); }
133133
Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
134134
Vector2i snapped(const Vector2i &p_step) const;
135135
Vector2i snappedi(int32_t p_step) const;

include/godot_cpp/variant/vector3.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ struct _NO_DISCARD_ Vector3 {
7979
}
8080

8181
Vector3 min(const Vector3 &p_vector3) const {
82-
return Vector3(MIN(x, p_vector3.x), MIN(y, p_vector3.y), MIN(z, p_vector3.z));
82+
return Vector3(Math::min(x, p_vector3.x), Math::min(y, p_vector3.y), Math::min(z, p_vector3.z));
8383
}
8484

8585
Vector3 minf(real_t p_scalar) const {
86-
return Vector3(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar));
86+
return Vector3(Math::min(x, p_scalar), Math::min(y, p_scalar), Math::min(z, p_scalar));
8787
}
8888

8989
Vector3 max(const Vector3 &p_vector3) const {
90-
return Vector3(MAX(x, p_vector3.x), MAX(y, p_vector3.y), MAX(z, p_vector3.z));
90+
return Vector3(Math::max(x, p_vector3.x), Math::max(y, p_vector3.y), Math::max(z, p_vector3.z));
9191
}
9292

9393
Vector3 maxf(real_t p_scalar) const {
94-
return Vector3(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar));
94+
return Vector3(Math::max(x, p_scalar), Math::max(y, p_scalar), Math::max(z, p_scalar));
9595
}
9696

9797
_FORCE_INLINE_ real_t length() const;
@@ -213,7 +213,7 @@ Vector3 Vector3::abs() const {
213213
}
214214

215215
Vector3 Vector3::sign() const {
216-
return Vector3(SIGN(x), SIGN(y), SIGN(z));
216+
return Vector3(Math::sign(x), Math::sign(y), Math::sign(z));
217217
}
218218

219219
Vector3 Vector3::floor() const {

include/godot_cpp/variant/vector3i.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@ struct _NO_DISCARD_ Vector3i {
7272
Vector3i::Axis max_axis_index() const;
7373

7474
Vector3i min(const Vector3i &p_vector3i) const {
75-
return Vector3i(MIN(x, p_vector3i.x), MIN(y, p_vector3i.y), MIN(z, p_vector3i.z));
75+
return Vector3i(Math::min(x, p_vector3i.x), Math::min(y, p_vector3i.y), Math::min(z, p_vector3i.z));
7676
}
7777

7878
Vector3i mini(int32_t p_scalar) const {
79-
return Vector3i(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar));
79+
return Vector3i(Math::min(x, p_scalar), Math::min(y, p_scalar), Math::min(z, p_scalar));
8080
}
8181

8282
Vector3i max(const Vector3i &p_vector3i) const {
83-
return Vector3i(MAX(x, p_vector3i.x), MAX(y, p_vector3i.y), MAX(z, p_vector3i.z));
83+
return Vector3i(Math::max(x, p_vector3i.x), Math::max(y, p_vector3i.y), Math::max(z, p_vector3i.z));
8484
}
8585

8686
Vector3i maxi(int32_t p_scalar) const {
87-
return Vector3i(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar));
87+
return Vector3i(Math::max(x, p_scalar), Math::max(y, p_scalar), Math::max(z, p_scalar));
8888
}
8989

9090
_FORCE_INLINE_ int64_t length_squared() const;
@@ -163,7 +163,7 @@ Vector3i Vector3i::abs() const {
163163
}
164164

165165
Vector3i Vector3i::sign() const {
166-
return Vector3i(SIGN(x), SIGN(y), SIGN(z));
166+
return Vector3i(Math::sign(x), Math::sign(y), Math::sign(z));
167167
}
168168

169169
/* Operators */

include/godot_cpp/variant/vector4.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,19 @@ struct _NO_DISCARD_ Vector4 {
7171
Vector4::Axis max_axis_index() const;
7272

7373
Vector4 min(const Vector4 &p_vector4) const {
74-
return Vector4(MIN(x, p_vector4.x), MIN(y, p_vector4.y), MIN(z, p_vector4.z), MIN(w, p_vector4.w));
74+
return Vector4(Math::min(x, p_vector4.x), Math::min(y, p_vector4.y), Math::min(z, p_vector4.z), Math::min(w, p_vector4.w));
7575
}
7676

7777
Vector4 minf(real_t p_scalar) const {
78-
return Vector4(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar), MIN(w, p_scalar));
78+
return Vector4(Math::min(x, p_scalar), Math::min(y, p_scalar), Math::min(z, p_scalar), Math::min(w, p_scalar));
7979
}
8080

8181
Vector4 max(const Vector4 &p_vector4) const {
82-
return Vector4(MAX(x, p_vector4.x), MAX(y, p_vector4.y), MAX(z, p_vector4.z), MAX(w, p_vector4.w));
82+
return Vector4(Math::max(x, p_vector4.x), Math::max(y, p_vector4.y), Math::max(z, p_vector4.z), Math::max(w, p_vector4.w));
8383
}
8484

8585
Vector4 maxf(real_t p_scalar) const {
86-
return Vector4(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar), MAX(w, p_scalar));
86+
return Vector4(Math::max(x, p_scalar), Math::max(y, p_scalar), Math::max(z, p_scalar), Math::max(w, p_scalar));
8787
}
8888

8989
_FORCE_INLINE_ real_t length_squared() const;

include/godot_cpp/variant/vector4i.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,19 @@ struct _NO_DISCARD_ Vector4i {
7474
Vector4i::Axis max_axis_index() const;
7575

7676
Vector4i min(const Vector4i &p_vector4i) const {
77-
return Vector4i(MIN(x, p_vector4i.x), MIN(y, p_vector4i.y), MIN(z, p_vector4i.z), MIN(w, p_vector4i.w));
77+
return Vector4i(Math::min(x, p_vector4i.x), Math::min(y, p_vector4i.y), Math::min(z, p_vector4i.z), Math::min(w, p_vector4i.w));
7878
}
7979

8080
Vector4i mini(int32_t p_scalar) const {
81-
return Vector4i(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar), MIN(w, p_scalar));
81+
return Vector4i(Math::min(x, p_scalar), Math::min(y, p_scalar), Math::min(z, p_scalar), Math::min(w, p_scalar));
8282
}
8383

8484
Vector4i max(const Vector4i &p_vector4i) const {
85-
return Vector4i(MAX(x, p_vector4i.x), MAX(y, p_vector4i.y), MAX(z, p_vector4i.z), MAX(w, p_vector4i.w));
85+
return Vector4i(Math::max(x, p_vector4i.x), Math::max(y, p_vector4i.y), Math::max(z, p_vector4i.z), Math::max(w, p_vector4i.w));
8686
}
8787

8888
Vector4i maxi(int32_t p_scalar) const {
89-
return Vector4i(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar), MAX(w, p_scalar));
89+
return Vector4i(Math::max(x, p_scalar), Math::max(y, p_scalar), Math::max(z, p_scalar), Math::max(w, p_scalar));
9090
}
9191

9292
_FORCE_INLINE_ int64_t length_squared() const;

src/variant/basis.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ Vector3 Basis::get_scale_abs() const {
292292
}
293293

294294
Vector3 Basis::get_scale_local() const {
295-
real_t det_sign = SIGN(determinant());
295+
real_t det_sign = Math::sign(determinant());
296296
return det_sign * Vector3(rows[0].length(), rows[1].length(), rows[2].length());
297297
}
298298

@@ -318,7 +318,7 @@ Vector3 Basis::get_scale() const {
318318
// matrix elements.
319319
//
320320
// The rotation part of this decomposition is returned by get_rotation* functions.
321-
real_t det_sign = SIGN(determinant());
321+
real_t det_sign = Math::sign(determinant());
322322
return det_sign * get_scale_abs();
323323
}
324324

@@ -416,7 +416,7 @@ void Basis::rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction)
416416
const Vector3 axis = p_start_direction.cross(p_end_direction).normalized();
417417
if (axis.length_squared() != 0) {
418418
real_t dot = p_start_direction.dot(p_end_direction);
419-
dot = CLAMP(dot, -1.0f, 1.0f);
419+
dot = Math::clamp(dot, -1.0f, 1.0f);
420420
const real_t angle_rads = Math::acos(dot);
421421
set_axis_angle(axis, angle_rads);
422422
}
@@ -829,7 +829,7 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
829829

830830
r_axis = Vector3(x, y, z);
831831
// CLAMP to avoid NaN if the value passed to acos is not in [0,1].
832-
r_angle = Math::acos(CLAMP((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2, (real_t)0.0, (real_t)1.0));
832+
r_angle = Math::acos(Math::clamp((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2, (real_t)0.0, (real_t)1.0));
833833
}
834834

835835
void Basis::set_quaternion(const Quaternion &p_quaternion) {

src/variant/color.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ uint64_t Color::to_rgba64() const {
109109

110110
String _to_hex(float p_val) {
111111
int v = Math::round(p_val * 255.0f);
112-
v = CLAMP(v, 0, 255);
112+
v = Math::clamp(v, 0, 255);
113113
String ret;
114114

115115
for (int i = 0; i < 2; i++) {
@@ -246,10 +246,10 @@ bool Color::is_equal_approx(const Color &p_color) const {
246246

247247
Color Color::clamp(const Color &p_min, const Color &p_max) const {
248248
return Color(
249-
CLAMP(r, p_min.r, p_max.r),
250-
CLAMP(g, p_min.g, p_max.g),
251-
CLAMP(b, p_min.b, p_max.b),
252-
CLAMP(a, p_min.a, p_max.a));
249+
Math::clamp(r, p_min.r, p_max.r),
250+
Math::clamp(g, p_min.g, p_max.g),
251+
Math::clamp(b, p_min.b, p_max.b),
252+
Math::clamp(a, p_min.a, p_max.a));
253253
}
254254

255255
void Color::invert() {

src/variant/quaternion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace godot {
3737

3838
real_t Quaternion::angle_to(const Quaternion &p_to) const {
3939
real_t d = dot(p_to);
40-
return Math::acos(CLAMP(d * d * 2 - 1, -1, 1));
40+
return Math::acos(Math::clamp<real_t>(d * d * 2 - 1, -1, 1));
4141
}
4242

4343
// get_euler_xyz returns a vector containing the Euler angles in the format

0 commit comments

Comments
 (0)