Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion include/godot_cpp/core/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,32 @@ constexpr int64_t division_round_up(int64_t p_num, int64_t p_den) {
constexpr uint64_t division_round_up(uint64_t p_num, uint64_t p_den) {
return (p_num + p_den - 1) / p_den;
}

GODOT_MSVC_WARNING_PUSH_AND_IGNORE(4146) // Unary minus operator applied to unsigned type, result still unsigned
constexpr int32_t division_no_overflow(int32_t p_num, int32_t p_den) {
if (unlikely(p_den == -1)) {
return int32_t(-uint32_t(p_num));
}
return p_num / p_den;
}
constexpr int64_t division_no_overflow(int64_t p_num, int64_t p_den) {
if (unlikely(p_den == -1)) {
return int64_t(-uint64_t(p_num));
}
return p_num / p_den;
}
GODOT_MSVC_WARNING_POP
constexpr int32_t modulo_no_overflow(int32_t p_num, int32_t p_den) {
if (unlikely(p_den == -1)) {
return 0;
}
return p_num % p_den;
}
constexpr int64_t modulo_no_overflow(int64_t p_num, int64_t p_den) {
if (unlikely(p_den == -1)) {
return 0;
}
return p_num % p_den;
}
constexpr bool is_finite(double p_val) {
return !is_nan(p_val) && !is_inf(p_val);
}
Expand Down
21 changes: 14 additions & 7 deletions include/godot_cpp/variant/aabb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ struct [[nodiscard]] AABB {
const Vector3 &get_size() const { return size; }
void set_size(const Vector3 &p_size) { size = p_size; }

bool operator==(const AABB &p_rval) const;
bool operator!=(const AABB &p_rval) const;
constexpr bool operator==(const AABB &p_rval) const {
return position == p_rval.position && size == p_rval.size;
}
constexpr bool operator!=(const AABB &p_rval) const {
return position != p_rval.position || size != p_rval.size;
}

bool is_equal_approx(const AABB &p_aabb) const;
bool is_same(const AABB &p_aabb) const;
bool is_finite() const;
_FORCE_INLINE_ bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap
_FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap
Expand Down Expand Up @@ -128,10 +133,10 @@ struct [[nodiscard]] AABB {
return position + (size * 0.5f);
}

operator String() const;
explicit operator String() const;

_FORCE_INLINE_ AABB() {}
inline AABB(const Vector3 &p_pos, const Vector3 &p_size) :
AABB() = default;
constexpr AABB(const Vector3 &p_pos, const Vector3 &p_size) :
position(p_pos),
size(p_size) {
}
Expand Down Expand Up @@ -273,10 +278,10 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, con

for (int k = 0; k < 3; k++) {
for (int i = 0; i < p_point_count; i++) {
if (p_points[i].coord[k] > ofs.coord[k] + half_extents.coord[k]) {
if (p_points[i][k] > ofs[k] + half_extents[k]) {
bad_point_counts_positive[k]++;
}
if (p_points[i].coord[k] < ofs.coord[k] - half_extents.coord[k]) {
if (p_points[i][k] < ofs[k] - half_extents[k]) {
bad_point_counts_negative[k]++;
}
}
Expand Down Expand Up @@ -497,4 +502,6 @@ AABB AABB::quantized(real_t p_unit) const {
return ret;
}

template <>
struct is_zero_constructible<AABB> : std::true_type {};
} // namespace godot
91 changes: 61 additions & 30 deletions include/godot_cpp/variant/basis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@
namespace godot {

struct [[nodiscard]] Basis {
static const Basis FLIP_X;
static const Basis FLIP_Y;
static const Basis FLIP_Z;

Vector3 rows[3] = {
Vector3(1, 0, 0),
Vector3(0, 1, 0),
Vector3(0, 0, 1)
};

_FORCE_INLINE_ const Vector3 &operator[](int p_row) const {
constexpr const Vector3 &operator[](int p_row) const {
return rows[p_row];
}
_FORCE_INLINE_ Vector3 &operator[](int p_row) {
constexpr Vector3 &operator[](int p_row) {
return rows[p_row];
}

Expand Down Expand Up @@ -77,7 +81,7 @@ struct [[nodiscard]] Basis {

void rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction);

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

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

bool is_equal_approx(const Basis &p_basis) const;
bool is_same(const Basis &p_basis) const;
bool is_finite() const;

bool operator==(const Basis &p_matrix) const;
bool operator!=(const Basis &p_matrix) const;
constexpr bool operator==(const Basis &p_matrix) const;
constexpr bool operator!=(const Basis &p_matrix) const;

_FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
_FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
_FORCE_INLINE_ void operator*=(const Basis &p_matrix);
_FORCE_INLINE_ Basis operator*(const Basis &p_matrix) const;
_FORCE_INLINE_ void operator+=(const Basis &p_matrix);
_FORCE_INLINE_ Basis operator+(const Basis &p_matrix) const;
_FORCE_INLINE_ void operator-=(const Basis &p_matrix);
_FORCE_INLINE_ Basis operator-(const Basis &p_matrix) const;
_FORCE_INLINE_ void operator*=(real_t p_val);
_FORCE_INLINE_ Basis operator*(real_t p_val) const;
_FORCE_INLINE_ void operator/=(real_t p_val);
_FORCE_INLINE_ Basis operator/(real_t p_val) const;
constexpr void operator+=(const Basis &p_matrix);
constexpr Basis operator+(const Basis &p_matrix) const;
constexpr void operator-=(const Basis &p_matrix);
constexpr Basis operator-(const Basis &p_matrix) const;
constexpr void operator*=(real_t p_val);
constexpr Basis operator*(real_t p_val) const;
constexpr void operator/=(real_t p_val);
constexpr Basis operator/(real_t p_val) const;

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

operator String() const;
explicit operator String() const;

/* create / set */

Expand Down Expand Up @@ -206,9 +211,12 @@ struct [[nodiscard]] Basis {
rows[0].z * p_m[0].y + rows[1].z * p_m[1].y + rows[2].z * p_m[2].y,
rows[0].z * p_m[0].z + rows[1].z * p_m[1].z + rows[2].z * p_m[2].z);
}
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);
}
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) :
rows{
{ p_xx, p_xy, p_xz },
{ p_yx, p_yy, p_yz },
{ p_zx, p_zy, p_zz },
} {}

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

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

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

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

_FORCE_INLINE_ Basis(const Vector3 &p_x_axis, const Vector3 &p_y_axis, const Vector3 &p_z_axis) {
set_columns(p_x_axis, p_y_axis, p_z_axis);
}
constexpr Basis(const Vector3 &p_x_axis, const Vector3 &p_y_axis, const Vector3 &p_z_axis) :
rows{
{ p_x_axis.x, p_y_axis.x, p_z_axis.x },
{ p_x_axis.y, p_y_axis.y, p_z_axis.y },
{ p_x_axis.z, p_y_axis.z, p_z_axis.z },
} {}

_FORCE_INLINE_ Basis() {}
Basis() = default;

private:
// Helper method.
void _set_diagonal(const Vector3 &p_diag);
};

inline constexpr Basis Basis::FLIP_X = { { -1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
inline constexpr Basis Basis::FLIP_Y = { { 1, 0, 0 }, { 0, -1, 0 }, { 0, 0, 1 } };
inline constexpr Basis Basis::FLIP_Z = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, -1 } };

constexpr bool Basis::operator==(const Basis &p_matrix) const {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (rows[i][j] != p_matrix.rows[i][j]) {
return false;
}
}
}

return true;
}

constexpr bool Basis::operator!=(const Basis &p_matrix) const {
return (!(*this == p_matrix));
}

_FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) {
set(
p_matrix.tdotx(rows[0]), p_matrix.tdoty(rows[0]), p_matrix.tdotz(rows[0]),
Expand All @@ -257,49 +288,49 @@ _FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const {
p_matrix.tdotx(rows[2]), p_matrix.tdoty(rows[2]), p_matrix.tdotz(rows[2]));
}

_FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) {
constexpr void Basis::operator+=(const Basis &p_matrix) {
rows[0] += p_matrix.rows[0];
rows[1] += p_matrix.rows[1];
rows[2] += p_matrix.rows[2];
}

_FORCE_INLINE_ Basis Basis::operator+(const Basis &p_matrix) const {
constexpr Basis Basis::operator+(const Basis &p_matrix) const {
Basis ret(*this);
ret += p_matrix;
return ret;
}

_FORCE_INLINE_ void Basis::operator-=(const Basis &p_matrix) {
constexpr void Basis::operator-=(const Basis &p_matrix) {
rows[0] -= p_matrix.rows[0];
rows[1] -= p_matrix.rows[1];
rows[2] -= p_matrix.rows[2];
}

_FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const {
constexpr Basis Basis::operator-(const Basis &p_matrix) const {
Basis ret(*this);
ret -= p_matrix;
return ret;
}

_FORCE_INLINE_ void Basis::operator*=(real_t p_val) {
constexpr void Basis::operator*=(real_t p_val) {
rows[0] *= p_val;
rows[1] *= p_val;
rows[2] *= p_val;
}

_FORCE_INLINE_ Basis Basis::operator*(real_t p_val) const {
constexpr Basis Basis::operator*(real_t p_val) const {
Basis ret(*this);
ret *= p_val;
return ret;
}

_FORCE_INLINE_ void Basis::operator/=(real_t p_val) {
constexpr void Basis::operator/=(real_t p_val) {
rows[0] /= p_val;
rows[1] /= p_val;
rows[2] /= p_val;
}

_FORCE_INLINE_ Basis Basis::operator/(real_t p_val) const {
constexpr Basis Basis::operator/(real_t p_val) const {
Basis ret(*this);
ret /= p_val;
return ret;
Expand Down
Loading
Loading