Skip to content

Commit

Permalink
Replace generic macros with math equivalents
Browse files Browse the repository at this point in the history
  • Loading branch information
Repiteo committed Jun 24, 2024
1 parent c414c2b commit 67e004e
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 109 deletions.
28 changes: 13 additions & 15 deletions include/godot_cpp/core/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,31 @@ namespace godot {
#undef MAX
#undef CLAMP

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

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

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

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

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

template <typename T>
inline T clamp(T x, T minv, T maxv) {
if (x < minv) {
return minv;
}
if (x > maxv) {
return maxv;
}
return x;
constexpr T clamp(T x, T minv, T maxv) {
return x < minv ? minv : (x > maxv ? maxv : x);
}

template <typename T>
inline T min(T a, T b) {
constexpr T min(T a, T b) {
return a < b ? a : b;
}

template <typename T>
inline T max(T a, T b) {
constexpr T max(T a, T b) {
return a > b ? a : b;
}

template <typename T>
inline T sign(T x) {
return static_cast<T>(SIGN(x));
constexpr T sign(T x) {
return x > T(0) ? T(1) : (x < T(0) ? T(-1) : T(0));
}

template <typename T>
inline T abs(T x) {
return std::abs(x);
constexpr T abs(T x) {
return x == T(0) ? T(0) : (x < T(0) ? -x : x);
}

inline double deg_to_rad(double p_y) {
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/templates/hash_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class HashMap {
uint32_t old_capacity = hash_table_size_primes[capacity_index];

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

uint32_t capacity = hash_table_size_primes[capacity_index];

Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/templates/hash_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class HashSet {

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

uint32_t capacity = hash_table_size_primes[capacity_index];

Expand Down
4 changes: 2 additions & 2 deletions include/godot_cpp/templates/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ class Vector {

const Size s = size();

Size begin = CLAMP(p_begin, -s, s);
Size begin = Math::clamp(p_begin, -s, s);
if (begin < 0) {
begin += s;
}
Size end = CLAMP(p_end, -s, s);
Size end = Math::clamp(p_end, -s, s);
if (end < 0) {
end += s;
}
Expand Down
26 changes: 13 additions & 13 deletions include/godot_cpp/variant/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ struct _NO_DISCARD_ Color {

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

float cRed = MAX(0.0f, MIN(sharedexp, r));
float cGreen = MAX(0.0f, MIN(sharedexp, g));
float cBlue = MAX(0.0f, MIN(sharedexp, b));
float cRed = Math::max(0.0f, Math::min(sharedexp, r));
float cGreen = Math::max(0.0f, Math::min(sharedexp, g));
float cBlue = Math::max(0.0f, Math::min(sharedexp, b));

float cMax = MAX(cRed, MAX(cGreen, cBlue));
float cMax = Math::max(cRed, Math::max(cGreen, cBlue));

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

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

Expand Down Expand Up @@ -204,14 +204,14 @@ struct _NO_DISCARD_ Color {
operator String() const;

// For the binder.
_FORCE_INLINE_ void set_r8(int32_t r8) { r = (CLAMP(r8, 0, 255) / 255.0f); }
_FORCE_INLINE_ int32_t get_r8() const { return int32_t(CLAMP(Math::round(r * 255.0f), 0.0f, 255.0f)); }
_FORCE_INLINE_ void set_g8(int32_t g8) { g = (CLAMP(g8, 0, 255) / 255.0f); }
_FORCE_INLINE_ int32_t get_g8() const { return int32_t(CLAMP(Math::round(g * 255.0f), 0.0f, 255.0f)); }
_FORCE_INLINE_ void set_b8(int32_t b8) { b = (CLAMP(b8, 0, 255) / 255.0f); }
_FORCE_INLINE_ int32_t get_b8() const { return int32_t(CLAMP(Math::round(b * 255.0f), 0.0f, 255.0f)); }
_FORCE_INLINE_ void set_a8(int32_t a8) { a = (CLAMP(a8, 0, 255) / 255.0f); }
_FORCE_INLINE_ int32_t get_a8() const { return int32_t(CLAMP(Math::round(a * 255.0f), 0.0f, 255.0f)); }
_FORCE_INLINE_ void set_r8(int32_t r8) { r = (Math::clamp(r8, 0, 255) / 255.0f); }
_FORCE_INLINE_ int32_t get_r8() const { return int32_t(Math::clamp(Math::round(r * 255.0f), 0.0f, 255.0f)); }
_FORCE_INLINE_ void set_g8(int32_t g8) { g = (Math::clamp(g8, 0, 255) / 255.0f); }
_FORCE_INLINE_ int32_t get_g8() const { return int32_t(Math::clamp(Math::round(g * 255.0f), 0.0f, 255.0f)); }
_FORCE_INLINE_ void set_b8(int32_t b8) { b = (Math::clamp(b8, 0, 255) / 255.0f); }
_FORCE_INLINE_ int32_t get_b8() const { return int32_t(Math::clamp(Math::round(b * 255.0f), 0.0f, 255.0f)); }
_FORCE_INLINE_ void set_a8(int32_t a8) { a = (Math::clamp(a8, 0, 255) / 255.0f); }
_FORCE_INLINE_ int32_t get_a8() const { return int32_t(Math::clamp(Math::round(a * 255.0f), 0.0f, 255.0f)); }

_FORCE_INLINE_ void set_h(float p_h) { set_hsv(p_h, get_s(), get_v(), a); }
_FORCE_INLINE_ void set_s(float p_s) { set_hsv(get_h(), p_s, get_v(), a); }
Expand Down
8 changes: 4 additions & 4 deletions include/godot_cpp/variant/vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ struct _NO_DISCARD_ Vector2 {
Vector2 limit_length(const real_t p_len = 1.0) const;

Vector2 min(const Vector2 &p_vector2) const {
return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
return Vector2(Math::min(x, p_vector2.x), Math::min(y, p_vector2.y));
}

Vector2 minf(real_t p_scalar) const {
return Vector2(MIN(x, p_scalar), MIN(y, p_scalar));
return Vector2(Math::min(x, p_scalar), Math::min(y, p_scalar));
}

Vector2 max(const Vector2 &p_vector2) const {
return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y));
return Vector2(Math::max(x, p_vector2.x), Math::max(y, p_vector2.y));
}

Vector2 maxf(real_t p_scalar) const {
return Vector2(MAX(x, p_scalar), MAX(y, p_scalar));
return Vector2(Math::max(x, p_scalar), Math::max(y, p_scalar));
}

real_t distance_to(const Vector2 &p_vector2) const;
Expand Down
10 changes: 5 additions & 5 deletions include/godot_cpp/variant/vector2i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ struct _NO_DISCARD_ Vector2i {
}

Vector2i min(const Vector2i &p_vector2i) const {
return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
return Vector2i(Math::min(x, p_vector2i.x), Math::min(y, p_vector2i.y));
}

Vector2i mini(int32_t p_scalar) const {
return Vector2i(MIN(x, p_scalar), MIN(y, p_scalar));
return Vector2i(Math::min(x, p_scalar), Math::min(y, p_scalar));
}

Vector2i max(const Vector2i &p_vector2i) const {
return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
return Vector2i(Math::max(x, p_vector2i.x), Math::max(y, p_vector2i.y));
}

Vector2i maxi(int32_t p_scalar) const {
return Vector2i(MAX(x, p_scalar), MAX(y, p_scalar));
return Vector2i(Math::max(x, p_scalar), Math::max(y, p_scalar));
}

Vector2i operator+(const Vector2i &p_v) const;
Expand Down Expand Up @@ -129,7 +129,7 @@ struct _NO_DISCARD_ Vector2i {
double distance_to(const Vector2i &p_to) const;

real_t aspect() const { return width / (real_t)height; }
Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
Vector2i sign() const { return Vector2i(Math::sign(x), Math::sign(y)); }
Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
Vector2i snapped(const Vector2i &p_step) const;
Vector2i snappedi(int32_t p_step) const;
Expand Down
10 changes: 5 additions & 5 deletions include/godot_cpp/variant/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ struct _NO_DISCARD_ Vector3 {
}

Vector3 min(const Vector3 &p_vector3) const {
return Vector3(MIN(x, p_vector3.x), MIN(y, p_vector3.y), MIN(z, p_vector3.z));
return Vector3(Math::min(x, p_vector3.x), Math::min(y, p_vector3.y), Math::min(z, p_vector3.z));
}

Vector3 minf(real_t p_scalar) const {
return Vector3(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar));
return Vector3(Math::min(x, p_scalar), Math::min(y, p_scalar), Math::min(z, p_scalar));
}

Vector3 max(const Vector3 &p_vector3) const {
return Vector3(MAX(x, p_vector3.x), MAX(y, p_vector3.y), MAX(z, p_vector3.z));
return Vector3(Math::max(x, p_vector3.x), Math::max(y, p_vector3.y), Math::max(z, p_vector3.z));
}

Vector3 maxf(real_t p_scalar) const {
return Vector3(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar));
return Vector3(Math::max(x, p_scalar), Math::max(y, p_scalar), Math::max(z, p_scalar));
}

_FORCE_INLINE_ real_t length() const;
Expand Down Expand Up @@ -213,7 +213,7 @@ Vector3 Vector3::abs() const {
}

Vector3 Vector3::sign() const {
return Vector3(SIGN(x), SIGN(y), SIGN(z));
return Vector3(Math::sign(x), Math::sign(y), Math::sign(z));
}

Vector3 Vector3::floor() const {
Expand Down
10 changes: 5 additions & 5 deletions include/godot_cpp/variant/vector3i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ struct _NO_DISCARD_ Vector3i {
Vector3i::Axis max_axis_index() const;

Vector3i min(const Vector3i &p_vector3i) const {
return Vector3i(MIN(x, p_vector3i.x), MIN(y, p_vector3i.y), MIN(z, p_vector3i.z));
return Vector3i(Math::min(x, p_vector3i.x), Math::min(y, p_vector3i.y), Math::min(z, p_vector3i.z));
}

Vector3i mini(int32_t p_scalar) const {
return Vector3i(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar));
return Vector3i(Math::min(x, p_scalar), Math::min(y, p_scalar), Math::min(z, p_scalar));
}

Vector3i max(const Vector3i &p_vector3i) const {
return Vector3i(MAX(x, p_vector3i.x), MAX(y, p_vector3i.y), MAX(z, p_vector3i.z));
return Vector3i(Math::max(x, p_vector3i.x), Math::max(y, p_vector3i.y), Math::max(z, p_vector3i.z));
}

Vector3i maxi(int32_t p_scalar) const {
return Vector3i(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar));
return Vector3i(Math::max(x, p_scalar), Math::max(y, p_scalar), Math::max(z, p_scalar));
}

_FORCE_INLINE_ int64_t length_squared() const;
Expand Down Expand Up @@ -163,7 +163,7 @@ Vector3i Vector3i::abs() const {
}

Vector3i Vector3i::sign() const {
return Vector3i(SIGN(x), SIGN(y), SIGN(z));
return Vector3i(Math::sign(x), Math::sign(y), Math::sign(z));
}

/* Operators */
Expand Down
8 changes: 4 additions & 4 deletions include/godot_cpp/variant/vector4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ struct _NO_DISCARD_ Vector4 {
Vector4::Axis max_axis_index() const;

Vector4 min(const Vector4 &p_vector4) const {
return Vector4(MIN(x, p_vector4.x), MIN(y, p_vector4.y), MIN(z, p_vector4.z), MIN(w, p_vector4.w));
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));
}

Vector4 minf(real_t p_scalar) const {
return Vector4(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar), MIN(w, p_scalar));
return Vector4(Math::min(x, p_scalar), Math::min(y, p_scalar), Math::min(z, p_scalar), Math::min(w, p_scalar));
}

Vector4 max(const Vector4 &p_vector4) const {
return Vector4(MAX(x, p_vector4.x), MAX(y, p_vector4.y), MAX(z, p_vector4.z), MAX(w, p_vector4.w));
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));
}

Vector4 maxf(real_t p_scalar) const {
return Vector4(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar), MAX(w, p_scalar));
return Vector4(Math::max(x, p_scalar), Math::max(y, p_scalar), Math::max(z, p_scalar), Math::max(w, p_scalar));
}

_FORCE_INLINE_ real_t length_squared() const;
Expand Down
8 changes: 4 additions & 4 deletions include/godot_cpp/variant/vector4i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ struct _NO_DISCARD_ Vector4i {
Vector4i::Axis max_axis_index() const;

Vector4i min(const Vector4i &p_vector4i) const {
return Vector4i(MIN(x, p_vector4i.x), MIN(y, p_vector4i.y), MIN(z, p_vector4i.z), MIN(w, p_vector4i.w));
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));
}

Vector4i mini(int32_t p_scalar) const {
return Vector4i(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar), MIN(w, p_scalar));
return Vector4i(Math::min(x, p_scalar), Math::min(y, p_scalar), Math::min(z, p_scalar), Math::min(w, p_scalar));
}

Vector4i max(const Vector4i &p_vector4i) const {
return Vector4i(MAX(x, p_vector4i.x), MAX(y, p_vector4i.y), MAX(z, p_vector4i.z), MAX(w, p_vector4i.w));
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));
}

Vector4i maxi(int32_t p_scalar) const {
return Vector4i(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar), MAX(w, p_scalar));
return Vector4i(Math::max(x, p_scalar), Math::max(y, p_scalar), Math::max(z, p_scalar), Math::max(w, p_scalar));
}

_FORCE_INLINE_ int64_t length_squared() const;
Expand Down
8 changes: 4 additions & 4 deletions src/variant/basis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ Vector3 Basis::get_scale_abs() const {
}

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

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

Expand Down Expand Up @@ -416,7 +416,7 @@ void Basis::rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction)
const Vector3 axis = p_start_direction.cross(p_end_direction).normalized();
if (axis.length_squared() != 0) {
real_t dot = p_start_direction.dot(p_end_direction);
dot = CLAMP(dot, -1.0f, 1.0f);
dot = Math::clamp(dot, -1.0f, 1.0f);
const real_t angle_rads = Math::acos(dot);
set_axis_angle(axis, angle_rads);
}
Expand Down Expand Up @@ -829,7 +829,7 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {

r_axis = Vector3(x, y, z);
// CLAMP to avoid NaN if the value passed to acos is not in [0,1].
r_angle = Math::acos(CLAMP((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2, (real_t)0.0, (real_t)1.0));
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));
}

void Basis::set_quaternion(const Quaternion &p_quaternion) {
Expand Down
10 changes: 5 additions & 5 deletions src/variant/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ uint64_t Color::to_rgba64() const {

String _to_hex(float p_val) {
int v = Math::round(p_val * 255.0f);
v = CLAMP(v, 0, 255);
v = Math::clamp(v, 0, 255);
String ret;

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

Color Color::clamp(const Color &p_min, const Color &p_max) const {
return Color(
CLAMP(r, p_min.r, p_max.r),
CLAMP(g, p_min.g, p_max.g),
CLAMP(b, p_min.b, p_max.b),
CLAMP(a, p_min.a, p_max.a));
Math::clamp(r, p_min.r, p_max.r),
Math::clamp(g, p_min.g, p_max.g),
Math::clamp(b, p_min.b, p_max.b),
Math::clamp(a, p_min.a, p_max.a));
}

void Color::invert() {
Expand Down
2 changes: 1 addition & 1 deletion src/variant/quaternion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace godot {

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

// get_euler_xyz returns a vector containing the Euler angles in the format
Expand Down
Loading

0 comments on commit 67e004e

Please sign in to comment.