Skip to content

Commit

Permalink
Merge pull request #89149 from AThousandShips/math_improve_3_x
Browse files Browse the repository at this point in the history
[3.x][Core] Codestyle improvements to math types
  • Loading branch information
akien-mga authored Mar 23, 2024
2 parents 9750469 + 3fb36bf commit 02c0240
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 223 deletions.
44 changes: 22 additions & 22 deletions core/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,12 @@ Color Color::operator*(const Color &p_color) const {
a * p_color.a);
}

Color Color::operator*(const real_t &rvalue) const {
Color Color::operator*(real_t p_scalar) const {
return Color(
r * rvalue,
g * rvalue,
b * rvalue,
a * rvalue);
r * p_scalar,
g * p_scalar,
b * p_scalar,
a * p_scalar);
}

void Color::operator*=(const Color &p_color) {
Expand All @@ -511,11 +511,11 @@ void Color::operator*=(const Color &p_color) {
a = a * p_color.a;
}

void Color::operator*=(const real_t &rvalue) {
r = r * rvalue;
g = g * rvalue;
b = b * rvalue;
a = a * rvalue;
void Color::operator*=(real_t p_scalar) {
r = r * p_scalar;
g = g * p_scalar;
b = b * p_scalar;
a = a * p_scalar;
}

Color Color::operator/(const Color &p_color) const {
Expand All @@ -526,12 +526,12 @@ Color Color::operator/(const Color &p_color) const {
a / p_color.a);
}

Color Color::operator/(const real_t &rvalue) const {
Color Color::operator/(real_t p_scalar) const {
return Color(
r / rvalue,
g / rvalue,
b / rvalue,
a / rvalue);
r / p_scalar,
g / p_scalar,
b / p_scalar,
a / p_scalar);
}

void Color::operator/=(const Color &p_color) {
Expand All @@ -541,19 +541,19 @@ void Color::operator/=(const Color &p_color) {
a = a / p_color.a;
}

void Color::operator/=(const real_t &rvalue) {
if (rvalue == 0) {
void Color::operator/=(real_t p_scalar) {
if (p_scalar == 0) {
r = 1.0;
g = 1.0;
b = 1.0;
a = 1.0;
} else {
r = r / rvalue;
g = g / rvalue;
b = b / rvalue;
a = a / rvalue;
r = r / p_scalar;
g = g / p_scalar;
b = b / p_scalar;
a = a / p_scalar;
}
};
}

Color Color::operator-() const {
return Color(
Expand Down
16 changes: 8 additions & 8 deletions core/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ struct _NO_DISCARD_CLASS_ Color {
float get_v() const;
void set_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0);

_FORCE_INLINE_ float &operator[](int idx) {
return components[idx];
_FORCE_INLINE_ float &operator[](int p_idx) {
return components[p_idx];
}
_FORCE_INLINE_ const float &operator[](int idx) const {
return components[idx];
_FORCE_INLINE_ const float &operator[](int p_idx) const {
return components[p_idx];
}

Color operator+(const Color &p_color) const;
Expand All @@ -75,14 +75,14 @@ struct _NO_DISCARD_CLASS_ Color {
void operator-=(const Color &p_color);

Color operator*(const Color &p_color) const;
Color operator*(const real_t &rvalue) const;
Color operator*(real_t p_scalar) const;
void operator*=(const Color &p_color);
void operator*=(const real_t &rvalue);
void operator*=(real_t p_scalar);

Color operator/(const Color &p_color) const;
Color operator/(const real_t &rvalue) const;
Color operator/(real_t p_scalar) const;
void operator/=(const Color &p_color);
void operator/=(const real_t &rvalue);
void operator/=(real_t p_scalar);

bool is_equal_approx(const Color &p_color) const;

Expand Down
30 changes: 15 additions & 15 deletions core/hashfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static inline uint32_t hash_djb2_one_32(uint32_t p_in, uint32_t p_prev = 5381) {
return ((p_prev << 5) + p_prev) + p_in;
}

static inline uint32_t hash_one_uint64(const uint64_t p_int) {
static inline uint32_t hash_one_uint64(uint64_t p_int) {
uint64_t v = p_int;
v = (~v) + (v << 18); // v = (v << 18) - v - 1;
v = v ^ (v >> 31);
Expand Down Expand Up @@ -132,18 +132,18 @@ static inline uint64_t make_uint64_t(T p_in) {
struct HashMapHasherDefault {
static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); }
static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); }
static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); }

static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash(uint64_t(p_int)); }
static _FORCE_INLINE_ uint32_t hash(const float p_float) { return hash_djb2_one_float(p_float); }
static _FORCE_INLINE_ uint32_t hash(const double p_double) { return hash_djb2_one_float(p_double); }
static _FORCE_INLINE_ uint32_t hash(const uint32_t p_int) { return p_int; }
static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return (uint32_t)p_int; }
static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return p_int; }
static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return (uint32_t)p_int; }
static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return p_int; }
static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return (uint32_t)p_int; }
static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return (uint32_t)p_wchar; }
static _FORCE_INLINE_ uint32_t hash(uint64_t p_int) { return hash_one_uint64(p_int); }

static _FORCE_INLINE_ uint32_t hash(int64_t p_int) { return hash(uint64_t(p_int)); }
static _FORCE_INLINE_ uint32_t hash(float p_float) { return hash_djb2_one_float(p_float); }
static _FORCE_INLINE_ uint32_t hash(double p_double) { return hash_djb2_one_float(p_double); }
static _FORCE_INLINE_ uint32_t hash(uint32_t p_int) { return p_int; }
static _FORCE_INLINE_ uint32_t hash(int32_t p_int) { return (uint32_t)p_int; }
static _FORCE_INLINE_ uint32_t hash(uint16_t p_int) { return p_int; }
static _FORCE_INLINE_ uint32_t hash(int16_t p_int) { return (uint32_t)p_int; }
static _FORCE_INLINE_ uint32_t hash(uint8_t p_int) { return p_int; }
static _FORCE_INLINE_ uint32_t hash(int8_t p_int) { return (uint32_t)p_int; }
static _FORCE_INLINE_ uint32_t hash(wchar_t p_wchar) { return (uint32_t)p_wchar; }

static _FORCE_INLINE_ uint32_t hash(const StringName &p_string_name) { return p_string_name.hash(); }
static _FORCE_INLINE_ uint32_t hash(const NodePath &p_path) { return p_path.hash(); }
Expand All @@ -157,11 +157,11 @@ struct HashMapComparatorDefault {
return p_lhs == p_rhs;
}

bool compare(const float &p_lhs, const float &p_rhs) {
bool compare(float p_lhs, float p_rhs) {
return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs));
}

bool compare(const double &p_lhs, const double &p_rhs) {
bool compare(double p_lhs, double p_rhs) {
return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs));
}
};
Expand Down
8 changes: 4 additions & 4 deletions core/math/basis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ Vector3 Basis::get_scale() const {
// Decomposes a Basis into a rotation-reflection matrix (an element of the group O(3)) and a positive scaling matrix as B = O.S.
// Returns the rotation-reflection matrix via reference argument, and scaling information is returned as a Vector3.
// This (internal) function is too specific and named too ugly to expose to users, and probably there's no need to do so.
Vector3 Basis::rotref_posscale_decomposition(Basis &rotref) const {
Vector3 Basis::rotref_posscale_decomposition(Basis &r_rotref) const {
#ifdef MATH_CHECKS
ERR_FAIL_COND_V(determinant() == 0, Vector3());

Expand All @@ -287,10 +287,10 @@ Vector3 Basis::rotref_posscale_decomposition(Basis &rotref) const {
#endif
Vector3 scale = get_scale();
Basis inv_scale = Basis().scaled(scale.inverse()); // this will also absorb the sign of scale
rotref = (*this) * inv_scale;
r_rotref = (*this) * inv_scale;

#ifdef MATH_CHECKS
ERR_FAIL_COND_V(!rotref.is_orthogonal(), Vector3());
ERR_FAIL_COND_V(!r_rotref.is_orthogonal(), Vector3());
#endif
return scale.abs();
}
Expand Down Expand Up @@ -1009,7 +1009,7 @@ void Basis::set_diagonal(const Vector3 &p_diag) {
elements[2][2] = p_diag.z;
}

Basis Basis::slerp(const Basis &p_to, const real_t &p_weight) const {
Basis Basis::slerp(const Basis &p_to, real_t p_weight) const {
//consider scale
Quat from(*this);
Quat to(p_to);
Expand Down
106 changes: 53 additions & 53 deletions core/math/basis.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class _NO_DISCARD_CLASS_ Basis {
Vector3(0, 0, 1)
};

_FORCE_INLINE_ const Vector3 &operator[](int axis) const {
return elements[axis];
_FORCE_INLINE_ const Vector3 &operator[](int p_axis) const {
return elements[p_axis];
}
_FORCE_INLINE_ Vector3 &operator[](int axis) {
return elements[axis];
_FORCE_INLINE_ Vector3 &operator[](int p_axis) {
return elements[p_axis];
}

void invert();
Expand All @@ -60,11 +60,11 @@ class _NO_DISCARD_CLASS_ Basis {
void from_z(const Vector3 &p_z);

_FORCE_INLINE_ Vector3 get_axis(int p_axis) const {
// get actual basis axis (elements is transposed for performance)
// Get actual basis axis (elements is transposed for performance).
return Vector3(elements[0][p_axis], elements[1][p_axis], elements[2][p_axis]);
}
_FORCE_INLINE_ void set_axis(int p_axis, const Vector3 &p_value) {
// get actual basis axis (elements is transposed for performance)
// Get actual basis axis (elements is transposed for performance).
elements[0][p_axis] = p_value.x;
elements[1][p_axis] = p_value.y;
elements[2][p_axis] = p_value.z;
Expand All @@ -86,9 +86,9 @@ class _NO_DISCARD_CLASS_ Basis {
void get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const;
void get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const;
Quat get_rotation_quat() const;
Vector3 get_rotation() const { return get_rotation_euler(); };
Vector3 get_rotation() const { return get_rotation_euler(); }

Vector3 rotref_posscale_decomposition(Basis &rotref) const;
Vector3 rotref_posscale_decomposition(Basis &r_rotref) const;

Vector3 get_euler_xyz() const;
void set_euler_xyz(const Vector3 &p_euler);
Expand Down Expand Up @@ -132,20 +132,20 @@ class _NO_DISCARD_CLASS_ Basis {
void set_quat_scale(const Quat &p_quat, const Vector3 &p_scale);

// transposed dot products
_FORCE_INLINE_ real_t tdotx(const Vector3 &v) const {
return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2];
_FORCE_INLINE_ real_t tdotx(const Vector3 &p_v) const {
return elements[0][0] * p_v[0] + elements[1][0] * p_v[1] + elements[2][0] * p_v[2];
}
_FORCE_INLINE_ real_t tdoty(const Vector3 &v) const {
return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2];
_FORCE_INLINE_ real_t tdoty(const Vector3 &p_v) const {
return elements[0][1] * p_v[0] + elements[1][1] * p_v[1] + elements[2][1] * p_v[2];
}
_FORCE_INLINE_ real_t tdotz(const Vector3 &v) const {
return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2];
_FORCE_INLINE_ real_t tdotz(const Vector3 &p_v) const {
return elements[0][2] * p_v[0] + elements[1][2] * p_v[1] + elements[2][2] * p_v[2];
}

bool is_equal_approx(const Basis &p_basis) const;
// For complicated reasons, the second argument is always discarded. See #45062.
bool is_equal_approx(const Basis &a, const Basis &b) const { return is_equal_approx(a); }
bool is_equal_approx_ratio(const Basis &a, const Basis &b, real_t p_epsilon = UNIT_EPSILON) const;
bool is_equal_approx(const Basis &p_a, const Basis &p_b) const { return is_equal_approx(p_a); }
bool is_equal_approx_ratio(const Basis &p_a, const Basis &p_b, real_t p_epsilon = UNIT_EPSILON) const;

bool operator==(const Basis &p_matrix) const;
bool operator!=(const Basis &p_matrix) const;
Expand All @@ -170,44 +170,44 @@ class _NO_DISCARD_CLASS_ Basis {
bool is_diagonal() const;
bool is_rotation() const;

Basis slerp(const Basis &p_to, const real_t &p_weight) const;
_FORCE_INLINE_ Basis lerp(const Basis &p_to, const real_t &p_weight) const;
Basis slerp(const Basis &p_to, real_t p_weight) const;
_FORCE_INLINE_ Basis lerp(const Basis &p_to, real_t p_weight) const;

operator String() const;

/* create / set */

_FORCE_INLINE_ void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
elements[0][0] = xx;
elements[0][1] = xy;
elements[0][2] = xz;
elements[1][0] = yx;
elements[1][1] = yy;
elements[1][2] = yz;
elements[2][0] = zx;
elements[2][1] = zy;
elements[2][2] = zz;
_FORCE_INLINE_ void set(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) {
elements[0][0] = p_xx;
elements[0][1] = p_xy;
elements[0][2] = p_xz;
elements[1][0] = p_yx;
elements[1][1] = p_yy;
elements[1][2] = p_yz;
elements[2][0] = p_zx;
elements[2][1] = p_zy;
elements[2][2] = p_zz;
}
_FORCE_INLINE_ void set(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) {
set_axis(0, p_x);
set_axis(1, p_y);
set_axis(2, p_z);
}
_FORCE_INLINE_ Vector3 get_column(int i) const {
return Vector3(elements[0][i], elements[1][i], elements[2][i]);
_FORCE_INLINE_ Vector3 get_column(int p_i) const {
return Vector3(elements[0][p_i], elements[1][p_i], elements[2][p_i]);
}

_FORCE_INLINE_ Vector3 get_row(int i) const {
return Vector3(elements[i][0], elements[i][1], elements[i][2]);
_FORCE_INLINE_ Vector3 get_row(int p_i) const {
return Vector3(elements[p_i][0], elements[p_i][1], elements[p_i][2]);
}
_FORCE_INLINE_ Vector3 get_main_diagonal() const {
return Vector3(elements[0][0], elements[1][1], elements[2][2]);
}

_FORCE_INLINE_ void set_row(int i, const Vector3 &p_row) {
elements[i][0] = p_row.x;
elements[i][1] = p_row.y;
elements[i][2] = p_row.z;
_FORCE_INLINE_ void set_row(int p_i, const Vector3 &p_row) {
elements[p_i][0] = p_row.x;
elements[p_i][1] = p_row.y;
elements[p_i][2] = p_row.z;
}

_FORCE_INLINE_ void set_zero() {
Expand All @@ -216,20 +216,20 @@ class _NO_DISCARD_CLASS_ Basis {
elements[2].zero();
}

_FORCE_INLINE_ Basis transpose_xform(const Basis &m) const {
_FORCE_INLINE_ Basis transpose_xform(const Basis &p_m) const {
return Basis(
elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x,
elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y,
elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z,
elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x,
elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y,
elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z,
elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x,
elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y,
elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z);
elements[0].x * p_m[0].x + elements[1].x * p_m[1].x + elements[2].x * p_m[2].x,
elements[0].x * p_m[0].y + elements[1].x * p_m[1].y + elements[2].x * p_m[2].y,
elements[0].x * p_m[0].z + elements[1].x * p_m[1].z + elements[2].x * p_m[2].z,
elements[0].y * p_m[0].x + elements[1].y * p_m[1].x + elements[2].y * p_m[2].x,
elements[0].y * p_m[0].y + elements[1].y * p_m[1].y + elements[2].y * p_m[2].y,
elements[0].y * p_m[0].z + elements[1].y * p_m[1].z + elements[2].y * p_m[2].z,
elements[0].z * p_m[0].x + elements[1].z * p_m[1].x + elements[2].z * p_m[2].x,
elements[0].z * p_m[0].y + elements[1].z * p_m[1].y + elements[2].z * p_m[2].y,
elements[0].z * p_m[0].z + elements[1].z * p_m[1].z + elements[2].z * p_m[2].z);
}
Basis(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
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) {
set(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz);
}

void orthonormalize();
Expand Down Expand Up @@ -263,10 +263,10 @@ class _NO_DISCARD_CLASS_ Basis {
Basis(const Vector3 &p_axis, real_t p_angle) { set_axis_angle(p_axis, p_angle); }
Basis(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_angle, p_scale); }

_FORCE_INLINE_ Basis(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2) {
elements[0] = row0;
elements[1] = row1;
elements[2] = row2;
_FORCE_INLINE_ Basis(const Vector3 &p_row0, const Vector3 &p_row1, const Vector3 &p_row2) {
elements[0] = p_row0;
elements[1] = p_row1;
elements[2] = p_row2;
}

_FORCE_INLINE_ Basis() {}
Expand Down Expand Up @@ -342,7 +342,7 @@ real_t Basis::determinant() const {
elements[2][0] * (elements[0][1] * elements[1][2] - elements[1][1] * elements[0][2]);
}

Basis Basis::lerp(const Basis &p_to, const real_t &p_weight) const {
Basis Basis::lerp(const Basis &p_to, real_t p_weight) const {
Basis b;
b.elements[0] = elements[0].linear_interpolate(p_to.elements[0], p_weight);
b.elements[1] = elements[1].linear_interpolate(p_to.elements[1], p_weight);
Expand Down
Loading

0 comments on commit 02c0240

Please sign in to comment.