Synchronize most shared variant code with Godot 4.4

This commit is contained in:
David Snopek
2025-02-21 14:59:13 -06:00
parent 67ca2fbbad
commit 075b517d96
35 changed files with 4801 additions and 876 deletions

View File

@@ -537,6 +537,26 @@ inline float bezier_interpolate(float p_start, float p_control_1, float p_contro
return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0f + p_control_2 * omt * t2 * 3.0f + p_end * t3;
}
inline double bezier_derivative(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
/* Formula from Wikipedia article on Bezier curves. */
double omt = (1.0 - p_t);
double omt2 = omt * omt;
double t2 = p_t * p_t;
double d = (p_control_1 - p_start) * 3.0 * omt2 + (p_control_2 - p_control_1) * 6.0 * omt * p_t + (p_end - p_control_2) * 3.0 * t2;
return d;
}
inline float bezier_derivative(float p_start, float p_control_1, float p_control_2, float p_end, float p_t) {
/* Formula from Wikipedia article on Bezier curves. */
float omt = (1.0f - p_t);
float omt2 = omt * omt;
float t2 = p_t * p_t;
float d = (p_control_1 - p_start) * 3.0f * omt2 + (p_control_2 - p_control_1) * 6.0f * omt * p_t + (p_end - p_control_2) * 3.0f * t2;
return d;
}
template <typename T>
inline T clamp(T x, T minv, T maxv) {
if (x < minv) {

View File

@@ -72,16 +72,21 @@ struct [[nodiscard]] AABB {
AABB merge(const AABB &p_with) const;
void merge_with(const AABB &p_aabb); ///merge with another AABB
AABB intersection(const AABB &p_aabb) const; ///get box where two intersect, empty if no intersection occurs
bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip = nullptr, Vector3 *r_normal = nullptr) const;
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip = nullptr, Vector3 *r_normal = nullptr) const;
_FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const;
_FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t p_t0, real_t p_t1) const;
bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_intersection_point = nullptr, Vector3 *r_normal = nullptr) const;
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir) const {
bool inside;
return find_intersects_ray(p_from, p_dir, inside);
}
bool find_intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, bool &r_inside, Vector3 *r_intersection_point = nullptr, Vector3 *r_normal = nullptr) const;
_FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const;
_FORCE_INLINE_ bool inside_convex_shape(const Plane *p_planes, int p_plane_count) const;
bool intersects_plane(const Plane &p_plane) const;
_FORCE_INLINE_ bool has_point(const Vector3 &p_point) const;
_FORCE_INLINE_ Vector3 get_support(const Vector3 &p_normal) const;
_FORCE_INLINE_ Vector3 get_support(const Vector3 &p_direction) const;
Vector3 get_longest_axis() const;
int get_longest_axis_index() const;
@@ -208,15 +213,18 @@ inline bool AABB::encloses(const AABB &p_aabb) const {
(src_max.z >= dst_max.z));
}
Vector3 AABB::get_support(const Vector3 &p_normal) const {
Vector3 half_extents = size * 0.5f;
Vector3 ofs = position + half_extents;
return Vector3(
(p_normal.x > 0) ? half_extents.x : -half_extents.x,
(p_normal.y > 0) ? half_extents.y : -half_extents.y,
(p_normal.z > 0) ? half_extents.z : -half_extents.z) +
ofs;
Vector3 AABB::get_support(const Vector3 &p_direction) const {
Vector3 support = position;
if (p_direction.x > 0.0f) {
support.x += size.x;
}
if (p_direction.y > 0.0f) {
support.y += size.y;
}
if (p_direction.z > 0.0f) {
support.z += size.z;
}
return support;
}
Vector3 AABB::get_endpoint(int p_point) const {
@@ -402,7 +410,7 @@ inline real_t AABB::get_shortest_axis_size() const {
return max_size;
}
bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const {
bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t p_t0, real_t p_t1) const {
#ifdef MATH_CHECKS
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
@@ -453,7 +461,7 @@ bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real
if (tzmax < tmax) {
tmax = tzmax;
}
return ((tmin < t1) && (tmax > t0));
return ((tmin < p_t1) && (tmax > p_t0));
}
void AABB::grow_by(real_t p_amount) {

View File

@@ -43,11 +43,11 @@ struct [[nodiscard]] Basis {
Vector3(0, 0, 1)
};
_FORCE_INLINE_ const Vector3 &operator[](int axis) const {
return rows[axis];
_FORCE_INLINE_ const Vector3 &operator[](int p_row) const {
return rows[p_row];
}
_FORCE_INLINE_ Vector3 &operator[](int axis) {
return rows[axis];
_FORCE_INLINE_ Vector3 &operator[](int p_row) {
return rows[p_row];
}
void invert();
@@ -58,21 +58,19 @@ struct [[nodiscard]] Basis {
_FORCE_INLINE_ real_t determinant() const;
void from_z(const Vector3 &p_z);
void rotate(const Vector3 &p_axis, real_t p_angle);
Basis rotated(const Vector3 &p_axis, real_t p_angle) const;
void rotate_local(const Vector3 &p_axis, real_t p_angle);
Basis rotated_local(const Vector3 &p_axis, real_t p_angle) const;
void rotate(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ);
Basis rotated(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ) const;
void rotate(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ);
Basis rotated(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) const;
void rotate(const Quaternion &p_quaternion);
Basis rotated(const Quaternion &p_quaternion) const;
Vector3 get_euler_normalized(EulerOrder p_order = EULER_ORDER_YXZ) const;
Vector3 get_euler_normalized(EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) const;
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;
Quaternion get_rotation_quaternion() const;
@@ -81,9 +79,9 @@ struct [[nodiscard]] Basis {
Vector3 rotref_posscale_decomposition(Basis &rotref) const;
Vector3 get_euler(EulerOrder p_order = EULER_ORDER_YXZ) const;
void set_euler(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ);
static Basis from_euler(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ) {
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);
static Basis from_euler(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) {
Basis b;
b.set_euler(p_euler, p_order);
return b;
@@ -103,27 +101,25 @@ struct [[nodiscard]] Basis {
void scale_orthogonal(const Vector3 &p_scale);
Basis scaled_orthogonal(const Vector3 &p_scale) const;
void make_scale_uniform();
float get_uniform_scale() const;
real_t get_uniform_scale() const;
Vector3 get_scale() const;
Vector3 get_scale_abs() const;
Vector3 get_scale_local() const;
Vector3 get_scale_global() const;
void set_axis_angle_scale(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale);
void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale, EulerOrder p_order = EULER_ORDER_YXZ);
void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ);
void set_quaternion_scale(const Quaternion &p_quaternion, const Vector3 &p_scale);
// transposed dot products
_FORCE_INLINE_ real_t tdotx(const Vector3 &v) const {
return rows[0][0] * v[0] + rows[1][0] * v[1] + rows[2][0] * v[2];
_FORCE_INLINE_ real_t tdotx(const Vector3 &p_v) const {
return rows[0][0] * p_v[0] + rows[1][0] * p_v[1] + rows[2][0] * p_v[2];
}
_FORCE_INLINE_ real_t tdoty(const Vector3 &v) const {
return rows[0][1] * v[0] + rows[1][1] * v[1] + rows[2][1] * v[2];
_FORCE_INLINE_ real_t tdoty(const Vector3 &p_v) const {
return rows[0][1] * p_v[0] + rows[1][1] * p_v[1] + rows[2][1] * p_v[2];
}
_FORCE_INLINE_ real_t tdotz(const Vector3 &v) const {
return rows[0][2] * v[0] + rows[1][2] * v[1] + rows[2][2] * v[2];
_FORCE_INLINE_ real_t tdotz(const Vector3 &p_v) const {
return rows[0][2] * p_v[0] + rows[1][2] * p_v[1] + rows[2][2] * p_v[2];
}
bool is_equal_approx(const Basis &p_basis) const;
@@ -140,31 +136,35 @@ struct [[nodiscard]] Basis {
_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 real_t p_val);
_FORCE_INLINE_ Basis operator*(const real_t p_val) 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;
bool is_orthogonal() const;
bool is_orthonormal() const;
bool is_conformal() const;
bool is_diagonal() const;
bool is_rotation() const;
Basis lerp(const Basis &p_to, const real_t &p_weight) const;
Basis slerp(const Basis &p_to, const real_t &p_weight) const;
Basis lerp(const Basis &p_to, real_t p_weight) const;
Basis slerp(const Basis &p_to, real_t p_weight) const;
void rotate_sh(real_t *p_values);
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) {
rows[0][0] = xx;
rows[0][1] = xy;
rows[0][2] = xz;
rows[1][0] = yx;
rows[1][1] = yy;
rows[1][2] = yz;
rows[2][0] = zx;
rows[2][1] = zy;
rows[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) {
rows[0][0] = p_xx;
rows[0][1] = p_xy;
rows[0][2] = p_xz;
rows[1][0] = p_yx;
rows[1][1] = p_yy;
rows[1][2] = p_yz;
rows[2][0] = p_zx;
rows[2][1] = p_zy;
rows[2][2] = p_zz;
}
_FORCE_INLINE_ void set_columns(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) {
set_column(0, p_x);
@@ -194,20 +194,20 @@ struct [[nodiscard]] Basis {
rows[2].zero();
}
_FORCE_INLINE_ Basis transpose_xform(const Basis &m) const {
_FORCE_INLINE_ Basis transpose_xform(const Basis &p_m) const {
return Basis(
rows[0].x * m[0].x + rows[1].x * m[1].x + rows[2].x * m[2].x,
rows[0].x * m[0].y + rows[1].x * m[1].y + rows[2].x * m[2].y,
rows[0].x * m[0].z + rows[1].x * m[1].z + rows[2].x * m[2].z,
rows[0].y * m[0].x + rows[1].y * m[1].x + rows[2].y * m[2].x,
rows[0].y * m[0].y + rows[1].y * m[1].y + rows[2].y * m[2].y,
rows[0].y * m[0].z + rows[1].y * m[1].z + rows[2].y * m[2].z,
rows[0].z * m[0].x + rows[1].z * m[1].x + rows[2].z * m[2].x,
rows[0].z * m[0].y + rows[1].z * m[1].y + rows[2].z * m[2].y,
rows[0].z * m[0].z + rows[1].z * m[1].z + rows[2].z * m[2].z);
rows[0].x * p_m[0].x + rows[1].x * p_m[1].x + rows[2].x * p_m[2].x,
rows[0].x * p_m[0].y + rows[1].x * p_m[1].y + rows[2].x * p_m[2].y,
rows[0].x * p_m[0].z + rows[1].x * p_m[1].z + rows[2].x * p_m[2].z,
rows[0].y * p_m[0].x + rows[1].y * p_m[1].x + rows[2].y * p_m[2].x,
rows[0].y * p_m[0].y + rows[1].y * p_m[1].y + rows[2].y * p_m[2].y,
rows[0].y * p_m[0].z + rows[1].y * p_m[1].z + rows[2].y * p_m[2].z,
rows[0].z * p_m[0].x + rows[1].z * p_m[1].x + rows[2].z * p_m[2].x,
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 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();
@@ -281,18 +281,30 @@ _FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const {
return ret;
}
_FORCE_INLINE_ void Basis::operator*=(const real_t p_val) {
_FORCE_INLINE_ void Basis::operator*=(real_t p_val) {
rows[0] *= p_val;
rows[1] *= p_val;
rows[2] *= p_val;
}
_FORCE_INLINE_ Basis Basis::operator*(const real_t p_val) const {
_FORCE_INLINE_ 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) {
rows[0] /= p_val;
rows[1] /= p_val;
rows[2] /= p_val;
}
_FORCE_INLINE_ Basis Basis::operator/(real_t p_val) const {
Basis ret(*this);
ret /= p_val;
return ret;
}
Vector3 Basis::xform(const Vector3 &p_vector) const {
return Vector3(
rows[0].dot(p_vector),

File diff suppressed because it is too large Load Diff

View File

@@ -30,58 +30,105 @@
#pragma once
static _FORCE_INLINE_ bool is_ascii_upper_case(char32_t c) {
return (c >= 'A' && c <= 'Z');
#include "char_range.inc.hpp"
namespace godot {
#define BSEARCH_CHAR_RANGE(m_array) \
int low = 0; \
int high = sizeof(m_array) / sizeof(m_array[0]) - 1; \
int middle = (low + high) / 2; \
\
while (low <= high) { \
if (p_char < m_array[middle].start) { \
high = middle - 1; \
} else if (p_char > m_array[middle].end) { \
low = middle + 1; \
} else { \
return true; \
} \
\
middle = (low + high) / 2; \
} \
\
return false
constexpr bool is_unicode_identifier_start(char32_t p_char) {
BSEARCH_CHAR_RANGE(xid_start);
}
static _FORCE_INLINE_ bool is_ascii_lower_case(char32_t c) {
return (c >= 'a' && c <= 'z');
constexpr bool is_unicode_identifier_continue(char32_t p_char) {
BSEARCH_CHAR_RANGE(xid_continue);
}
static _FORCE_INLINE_ bool is_digit(char32_t c) {
return (c >= '0' && c <= '9');
constexpr bool is_unicode_upper_case(char32_t p_char) {
BSEARCH_CHAR_RANGE(uppercase_letter);
}
static _FORCE_INLINE_ bool is_hex_digit(char32_t c) {
return (is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
constexpr bool is_unicode_lower_case(char32_t p_char) {
BSEARCH_CHAR_RANGE(lowercase_letter);
}
static _FORCE_INLINE_ bool is_binary_digit(char32_t c) {
return (c == '0' || c == '1');
constexpr bool is_unicode_letter(char32_t p_char) {
BSEARCH_CHAR_RANGE(unicode_letter);
}
static _FORCE_INLINE_ bool is_ascii_char(char32_t c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
#undef BSEARCH_CHAR_RANGE
constexpr bool is_ascii_upper_case(char32_t p_char) {
return (p_char >= 'A' && p_char <= 'Z');
}
static _FORCE_INLINE_ bool is_ascii_alphanumeric_char(char32_t c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
constexpr bool is_ascii_lower_case(char32_t p_char) {
return (p_char >= 'a' && p_char <= 'z');
}
static _FORCE_INLINE_ bool is_ascii_identifier_char(char32_t c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
constexpr bool is_digit(char32_t p_char) {
return (p_char >= '0' && p_char <= '9');
}
static _FORCE_INLINE_ bool is_symbol(char32_t c) {
return c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t' || c == ' ');
constexpr bool is_hex_digit(char32_t p_char) {
return (is_digit(p_char) || (p_char >= 'a' && p_char <= 'f') || (p_char >= 'A' && p_char <= 'F'));
}
static _FORCE_INLINE_ bool is_control(char32_t p_char) {
constexpr bool is_binary_digit(char32_t p_char) {
return (p_char == '0' || p_char == '1');
}
constexpr bool is_ascii_alphabet_char(char32_t p_char) {
return (p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z');
}
constexpr bool is_ascii_alphanumeric_char(char32_t p_char) {
return (p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z') || (p_char >= '0' && p_char <= '9');
}
constexpr bool is_ascii_identifier_char(char32_t p_char) {
return (p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z') || (p_char >= '0' && p_char <= '9') || p_char == '_';
}
constexpr bool is_symbol(char32_t p_char) {
return p_char != '_' && ((p_char >= '!' && p_char <= '/') || (p_char >= ':' && p_char <= '@') || (p_char >= '[' && p_char <= '`') || (p_char >= '{' && p_char <= '~') || p_char == '\t' || p_char == ' ');
}
constexpr bool is_control(char32_t p_char) {
return (p_char <= 0x001f) || (p_char >= 0x007f && p_char <= 0x009f);
}
static _FORCE_INLINE_ bool is_whitespace(char32_t p_char) {
return (p_char == ' ') || (p_char == 0x00a0) || (p_char == 0x1680) || (p_char >= 0x2000 && p_char <= 0x200a) || (p_char == 0x202f) || (p_char == 0x205f) || (p_char == 0x3000) || (p_char == 0x2028) || (p_char == 0x2029) || (p_char >= 0x0009 && p_char <= 0x000d) || (p_char == 0x0085);
constexpr bool is_whitespace(char32_t p_char) {
return (p_char == ' ') || (p_char == 0x00a0) || (p_char == 0x1680) || (p_char >= 0x2000 && p_char <= 0x200b) || (p_char == 0x202f) || (p_char == 0x205f) || (p_char == 0x3000) || (p_char == 0x2028) || (p_char == 0x2029) || (p_char >= 0x0009 && p_char <= 0x000d) || (p_char == 0x0085);
}
static _FORCE_INLINE_ bool is_linebreak(char32_t p_char) {
constexpr bool is_linebreak(char32_t p_char) {
return (p_char >= 0x000a && p_char <= 0x000d) || (p_char == 0x0085) || (p_char == 0x2028) || (p_char == 0x2029);
}
static _FORCE_INLINE_ bool is_punct(char32_t p_char) {
constexpr bool is_punct(char32_t p_char) {
return (p_char >= ' ' && p_char <= '/') || (p_char >= ':' && p_char <= '@') || (p_char >= '[' && p_char <= '^') || (p_char == '`') || (p_char >= '{' && p_char <= '~') || (p_char >= 0x2000 && p_char <= 0x206f) || (p_char >= 0x3000 && p_char <= 0x303f);
}
static _FORCE_INLINE_ bool is_underscore(char32_t p_char) {
constexpr bool is_underscore(char32_t p_char) {
return (p_char == '_');
}
} // namespace godot

View File

@@ -102,12 +102,10 @@ struct [[nodiscard]] Color {
_FORCE_INLINE_ Color lerp(const Color &p_to, float p_weight) const {
Color res = *this;
res.r += (p_weight * (p_to.r - r));
res.g += (p_weight * (p_to.g - g));
res.b += (p_weight * (p_to.b - b));
res.a += (p_weight * (p_to.a - a));
res.r = Math::lerp(res.r, p_to.r, p_weight);
res.g = Math::lerp(res.g, p_to.g, p_weight);
res.b = Math::lerp(res.b, p_to.b, p_weight);
res.a = Math::lerp(res.a, p_to.a, p_weight);
return res;
}
@@ -128,33 +126,46 @@ struct [[nodiscard]] Color {
}
_FORCE_INLINE_ uint32_t to_rgbe9995() const {
const float pow2to9 = 512.0f;
const float B = 15.0f;
const float N = 9.0f;
// https://github.com/microsoft/DirectX-Graphics-Samples/blob/v10.0.19041.0/MiniEngine/Core/Color.cpp
static const float kMaxVal = float(0x1FF << 7);
static const float kMinVal = float(1.f / (1 << 16));
float sharedexp = 65408.000f; // Result of: ((pow2to9 - 1.0f) / pow2to9) * powf(2.0f, 31.0f - 15.0f)
// Clamp RGB to [0, 1.FF*2^16]
const float _r = CLAMP(r, 0.0f, kMaxVal);
const float _g = CLAMP(g, 0.0f, kMaxVal);
const float _b = CLAMP(b, 0.0f, kMaxVal);
float cRed = MAX(0.0f, MIN(sharedexp, r));
float cGreen = MAX(0.0f, MIN(sharedexp, g));
float cBlue = MAX(0.0f, MIN(sharedexp, b));
// Compute the maximum channel, no less than 1.0*2^-15
const float MaxChannel = MAX(MAX(_r, _g), MAX(_b, kMinVal));
float cMax = MAX(cRed, MAX(cGreen, cBlue));
// Take the exponent of the maximum channel (rounding up the 9th bit) and
// add 15 to it. When added to the channels, it causes the implicit '1.0'
// bit and the first 8 mantissa bits to be shifted down to the low 9 bits
// of the mantissa, rounding the truncated bits.
union {
float f;
uint32_t i;
} R, G, B, E;
float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / (real_t)Math_LN2)) + 1.0f + B;
E.f = MaxChannel;
E.i += 0x07804000; // Add 15 to the exponent and 0x4000 to the mantissa
E.i &= 0x7F800000; // Zero the mantissa
float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f);
// This shifts the 9-bit values we need into the lowest bits, rounding as
// needed. Note that if the channel has a smaller exponent than the max
// channel, it will shift even more. This is intentional.
R.f = _r + E.f;
G.f = _g + E.f;
B.f = _b + E.f;
float exps = expp + 1.0f;
// Convert the Bias to the correct exponent in the upper 5 bits.
E.i <<= 4;
E.i += 0x10000000;
if (0.0f <= sMax && sMax < pow2to9) {
exps = expp;
}
float sRed = Math::floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
float sGreen = Math::floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
float sBlue = Math::floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
return (uint32_t(Math::fast_ftoi(sRed)) & 0x1FF) | ((uint32_t(Math::fast_ftoi(sGreen)) & 0x1FF) << 9) | ((uint32_t(Math::fast_ftoi(sBlue)) & 0x1FF) << 18) | ((uint32_t(Math::fast_ftoi(exps)) & 0x1F) << 27);
// Combine the fields. RGB floats have unwanted data in the upper 9
// bits. Only red needs to mask them off because green and blue shift
// it out to the left.
return E.i | (B.i << 18U) | (G.i << 9U) | (R.i & 511U);
}
_FORCE_INLINE_ Color blend(const Color &p_over) const {
@@ -173,16 +184,16 @@ struct [[nodiscard]] Color {
_FORCE_INLINE_ Color srgb_to_linear() const {
return Color(
r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow((r + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow((g + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow((b + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow(float((r + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow(float((g + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow(float((b + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
a);
}
_FORCE_INLINE_ Color linear_to_srgb() const {
return Color(
r < 0.0031308f ? 12.92f * r : (1.0f + 0.055f) * Math::pow(r, 1.0f / 2.4f) - 0.055f,
g < 0.0031308f ? 12.92f * g : (1.0f + 0.055f) * Math::pow(g, 1.0f / 2.4f) - 0.055f,
b < 0.0031308f ? 12.92f * b : (1.0f + 0.055f) * Math::pow(b, 1.0f / 2.4f) - 0.055f, a);
r < 0.0031308f ? 12.92f * r : (1.0 + 0.055) * Math::pow(r, 1.0f / 2.4f) - 0.055,
g < 0.0031308f ? 12.92f * g : (1.0 + 0.055) * Math::pow(g, 1.0f / 2.4f) - 0.055,
b < 0.0031308f ? 12.92f * b : (1.0 + 0.055) * Math::pow(b, 1.0f / 2.4f) - 0.055, a);
}
static Color hex(uint32_t p_hex);
@@ -198,6 +209,7 @@ struct [[nodiscard]] Color {
static Color from_string(const String &p_string, const Color &p_default);
static Color from_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f);
static Color from_rgbe9995(uint32_t p_rgbe);
static Color from_rgba8(int64_t p_r8, int64_t p_g8, int64_t p_b8, int64_t p_a8 = 255);
_FORCE_INLINE_ bool operator<(const Color &p_color) const; // Used in set keys.
operator String() const;

View File

@@ -187,7 +187,6 @@ static NamedColor named_colors[] = {
{ "WHITE_SMOKE", Color::hex(0xF5F5F5FF) },
{ "YELLOW", Color::hex(0xFFFF00FF) },
{ "YELLOW_GREEN", Color::hex(0x9ACD32FF) },
{ nullptr, Color() },
};
} // namespace godot

View File

@@ -49,7 +49,7 @@ struct [[nodiscard]] Plane {
/* Plane-Point operations */
_FORCE_INLINE_ Vector3 center() const { return normal * d; }
_FORCE_INLINE_ Vector3 get_center() const { return normal * d; }
Vector3 get_any_perpendicular_normal() const;
_FORCE_INLINE_ bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane
@@ -102,7 +102,7 @@ real_t Plane::distance_to(const Vector3 &p_point) const {
bool Plane::has_point(const Vector3 &p_point, real_t p_tolerance) const {
real_t dist = normal.dot(p_point) - d;
dist = Math::abs(dist);
dist = ABS(dist);
return (dist <= p_tolerance);
}

View File

@@ -31,6 +31,7 @@
#pragma once
#include <godot_cpp/core/math.hpp>
#include <godot_cpp/templates/vector.hpp>
#include <godot_cpp/variant/vector3.hpp>
#include <godot_cpp/variant/vector4.hpp>
@@ -55,21 +56,21 @@ struct [[nodiscard]] Projection {
Vector4 columns[4];
_FORCE_INLINE_ const Vector4 &operator[](const int p_axis) const {
_FORCE_INLINE_ const Vector4 &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 4);
return columns[p_axis];
}
_FORCE_INLINE_ Vector4 &operator[](const int p_axis) {
_FORCE_INLINE_ Vector4 &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 4);
return columns[p_axis];
}
float determinant() const;
real_t determinant() const;
void set_identity();
void set_zero();
void set_light_bias();
void set_depth_correction(bool p_flip_y = true);
void set_depth_correction(bool p_flip_y = true, bool p_reverse_z = true, bool p_remap_z = true);
void set_light_atlas_rect(const Rect2 &p_rect);
void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov = false);
@@ -106,7 +107,7 @@ struct [[nodiscard]] Projection {
real_t get_fov() const;
bool is_orthogonal() const;
Array get_projection_planes(const Transform3D &p_transform) const;
Vector<Plane> get_projection_planes(const Transform3D &p_transform) const;
bool get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const;
Vector2 get_viewport_half_extents() const;
@@ -148,7 +149,7 @@ struct [[nodiscard]] Projection {
return !(*this == p_cam);
}
float get_lod_multiplier() const;
real_t get_lod_multiplier() const;
Projection();
Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w);

View File

@@ -142,15 +142,24 @@ struct [[nodiscard]] Quaternion {
}
Quaternion(const Vector3 &p_v0, const Vector3 &p_v1) { // Shortest arc.
Vector3 c = p_v0.cross(p_v1);
real_t d = p_v0.dot(p_v1);
if (d < -1.0f + (real_t)CMP_EPSILON) {
x = 0;
y = 1;
z = 0;
#ifdef MATH_CHECKS
ERR_FAIL_COND_MSG(p_v0.is_zero_approx() || p_v1.is_zero_approx(), "The vectors must not be zero.");
#endif
constexpr real_t ALMOST_ONE = 1.0f - (real_t)CMP_EPSILON;
Vector3 n0 = p_v0.normalized();
Vector3 n1 = p_v1.normalized();
real_t d = n0.dot(n1);
if (abs(d) > ALMOST_ONE) {
if (d >= 0) {
return; // Vectors are same.
}
Vector3 axis = n0.get_any_perpendicular();
x = axis.x;
y = axis.y;
z = axis.z;
w = 0;
} else {
Vector3 c = n0.cross(n1);
real_t s = Math::sqrt((1.0f + d) * 2.0f);
real_t rs = 1.0f / s;

View File

@@ -52,7 +52,7 @@ struct [[nodiscard]] Rect2 {
_FORCE_INLINE_ Vector2 get_center() const { return position + (size * 0.5f); }
inline bool intersects(const Rect2 &p_rect, const bool p_include_borders = false) const {
inline bool intersects(const Rect2 &p_rect, bool p_include_borders = false) const {
#ifdef MATH_CHECKS
if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
@@ -105,17 +105,17 @@ struct [[nodiscard]] Rect2 {
}
if (p_point.y < position.y) {
real_t d = position.y - p_point.y;
dist = inside ? d : Math::min(dist, d);
dist = inside ? d : MIN(dist, d);
inside = false;
}
if (p_point.x >= (position.x + size.x)) {
real_t d = p_point.x - (position.x + size.x);
dist = inside ? d : Math::min(dist, d);
dist = inside ? d : MIN(dist, d);
inside = false;
}
if (p_point.y >= (position.y + size.y)) {
real_t d = p_point.y - (position.y + size.y);
dist = inside ? d : Math::min(dist, d);
dist = inside ? d : MIN(dist, d);
inside = false;
}
@@ -145,7 +145,7 @@ struct [[nodiscard]] Rect2 {
return size.x > 0.0f && size.y > 0.0f;
}
// Returns the intersection between two Rect2s or an empty Rect2 if there is no intersection
// Returns the intersection between two Rect2s or an empty Rect2 if there is no intersection.
inline Rect2 intersection(const Rect2 &p_rect) const {
Rect2 new_rect = p_rect;
@@ -282,13 +282,19 @@ struct [[nodiscard]] Rect2 {
return Rect2(position + size.minf(0), size.abs());
}
Vector2 get_support(const Vector2 &p_normal) const {
Vector2 half_extents = size * 0.5f;
Vector2 ofs = position + half_extents;
return Vector2(
(p_normal.x > 0) ? -half_extents.x : half_extents.x,
(p_normal.y > 0) ? -half_extents.y : half_extents.y) +
ofs;
_FORCE_INLINE_ Rect2 round() const {
return Rect2(position.round(), size.round());
}
Vector2 get_support(const Vector2 &p_direction) const {
Vector2 support = position;
if (p_direction.x > 0.0f) {
support.x += size.x;
}
if (p_direction.y > 0.0f) {
support.y += size.y;
}
return support;
}
_FORCE_INLINE_ bool intersects_filled_polygon(const Vector2 *p_points, int p_point_count) const {
@@ -304,14 +310,14 @@ struct [[nodiscard]] Rect2 {
i_f = i;
Vector2 r = (b - a);
float l = r.length();
const real_t l = r.length();
if (l == 0.0f) {
continue;
}
// Check inside.
Vector2 tg = r.orthogonal();
float s = tg.dot(center) - tg.dot(a);
const real_t s = tg.dot(center) - tg.dot(a);
if (s < 0.0f) {
side_plus++;
} else {
@@ -327,8 +333,8 @@ struct [[nodiscard]] Rect2 {
Vector2 t13 = (position - a) * ir;
Vector2 t24 = (end - a) * ir;
float tmin = Math::max(Math::min(t13.x, t24.x), Math::min(t13.y, t24.y));
float tmax = Math::min(Math::max(t13.x, t24.x), Math::max(t13.y, t24.y));
const real_t tmin = MAX(MIN(t13.x, t24.x), MIN(t13.y, t24.y));
const real_t tmax = MIN(MAX(t13.x, t24.x), MAX(t13.y, t24.y));
// if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
if (tmax < 0 || tmin > tmax || tmin >= l) {

View File

@@ -88,7 +88,7 @@ struct [[nodiscard]] Rect2i {
return size.x > 0 && size.y > 0;
}
// Returns the intersection between two Rect2is or an empty Rect2i if there is no intersection
// Returns the intersection between two Rect2is or an empty Rect2i if there is no intersection.
inline Rect2i intersection(const Rect2i &p_rect) const {
Rect2i new_rect = p_rect;

View File

@@ -39,21 +39,24 @@ namespace godot {
class String;
struct [[nodiscard]] Transform2D {
// Warning #1: basis of Transform2D is stored differently from Basis. In terms of columns array, the basis matrix looks like "on paper":
// WARNING: The basis of Transform2D is stored differently from Basis.
// In terms of columns array, the basis matrix looks like "on paper":
// M = (columns[0][0] columns[1][0])
// (columns[0][1] columns[1][1])
// This is such that the columns, which can be interpreted as basis vectors of the coordinate system "painted" on the object, can be accessed as columns[i].
// Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to columns[1][0] here.
// This is such that the columns, which can be interpreted as basis vectors
// of the coordinate system "painted" on the object, can be accessed as columns[i].
// NOTE: This is the opposite of the indices in mathematical texts,
// meaning: $M_{12}$ in a math book corresponds to columns[1][0] here.
// This requires additional care when working with explicit indices.
// See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
// Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
// and angle is measure from +X to +Y in a clockwise-fashion.
// WARNING: Be aware that unlike 3D code, 2D code uses a left-handed coordinate system:
// Y-axis points down, and angle is measure from +X to +Y in a clockwise-fashion.
Vector2 columns[3];
_FORCE_INLINE_ real_t tdotx(const Vector2 &v) const { return columns[0][0] * v.x + columns[1][0] * v.y; }
_FORCE_INLINE_ real_t tdoty(const Vector2 &v) const { return columns[0][1] * v.x + columns[1][1] * v.y; }
_FORCE_INLINE_ real_t tdotx(const Vector2 &p_v) const { return columns[0][0] * p_v.x + columns[1][0] * p_v.y; }
_FORCE_INLINE_ real_t tdoty(const Vector2 &p_v) const { return columns[0][1] * p_v.x + columns[1][1] * p_v.y; }
const Vector2 &operator[](int p_idx) const { return columns[p_idx]; }
Vector2 &operator[](int p_idx) { return columns[p_idx]; }
@@ -64,20 +67,20 @@ struct [[nodiscard]] Transform2D {
void affine_invert();
Transform2D affine_inverse() const;
void set_rotation(const real_t p_rot);
void set_rotation(real_t p_rot);
real_t get_rotation() const;
real_t get_skew() const;
void set_skew(const real_t p_angle);
_FORCE_INLINE_ void set_rotation_and_scale(const real_t p_rot, const Size2 &p_scale);
_FORCE_INLINE_ void set_rotation_scale_and_skew(const real_t p_rot, const Size2 &p_scale, const real_t p_skew);
void rotate(const real_t p_angle);
void set_skew(real_t p_angle);
_FORCE_INLINE_ void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
_FORCE_INLINE_ void set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, real_t p_skew);
void rotate(real_t p_angle);
void scale(const Size2 &p_scale);
void scale_basis(const Size2 &p_scale);
void translate_local(const real_t p_tx, const real_t p_ty);
void translate_local(real_t p_tx, real_t p_ty);
void translate_local(const Vector2 &p_translation);
real_t basis_determinant() const;
real_t determinant() const;
Size2 get_scale() const;
void set_scale(const Size2 &p_scale);
@@ -85,18 +88,18 @@ struct [[nodiscard]] Transform2D {
_FORCE_INLINE_ const Vector2 &get_origin() const { return columns[2]; }
_FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { columns[2] = p_origin; }
Transform2D basis_scaled(const Size2 &p_scale) const;
Transform2D scaled(const Size2 &p_scale) const;
Transform2D scaled_local(const Size2 &p_scale) const;
Transform2D translated(const Vector2 &p_offset) const;
Transform2D translated_local(const Vector2 &p_offset) const;
Transform2D rotated(const real_t p_angle) const;
Transform2D rotated_local(const real_t p_angle) const;
Transform2D rotated(real_t p_angle) const;
Transform2D rotated_local(real_t p_angle) const;
Transform2D untranslated() const;
void orthonormalize();
Transform2D orthonormalized() const;
bool is_conformal() const;
bool is_equal_approx(const Transform2D &p_transform) const;
bool is_finite() const;
@@ -107,10 +110,12 @@ struct [[nodiscard]] Transform2D {
void operator*=(const Transform2D &p_transform);
Transform2D operator*(const Transform2D &p_transform) const;
void operator*=(const real_t p_val);
Transform2D operator*(const real_t p_val) const;
void operator*=(real_t p_val);
Transform2D operator*(real_t p_val) const;
void operator/=(real_t p_val);
Transform2D operator/(real_t p_val) const;
Transform2D interpolate_with(const Transform2D &p_transform, const real_t p_c) const;
Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
_FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
_FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
@@ -123,13 +128,13 @@ struct [[nodiscard]] Transform2D {
operator String() const;
Transform2D(const real_t xx, const real_t xy, const real_t yx, const real_t yy, const real_t ox, const real_t oy) {
columns[0][0] = xx;
columns[0][1] = xy;
columns[1][0] = yx;
columns[1][1] = yy;
columns[2][0] = ox;
columns[2][1] = oy;
Transform2D(real_t p_xx, real_t p_xy, real_t p_yx, real_t p_yy, real_t p_ox, real_t p_oy) {
columns[0][0] = p_xx;
columns[0][1] = p_xy;
columns[1][0] = p_yx;
columns[1][1] = p_yy;
columns[2][0] = p_ox;
columns[2][1] = p_oy;
}
Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) {
@@ -138,9 +143,9 @@ struct [[nodiscard]] Transform2D {
columns[2] = p_origin;
}
Transform2D(const real_t p_rot, const Vector2 &p_pos);
Transform2D(real_t p_rot, const Vector2 &p_pos);
Transform2D(const real_t p_rot, const Size2 &p_scale, const real_t p_skew, const Vector2 &p_pos);
Transform2D(real_t p_rot, const Size2 &p_scale, real_t p_skew, const Vector2 &p_pos);
Transform2D() {
columns[0][0] = 1.0;
@@ -188,14 +193,14 @@ Rect2 Transform2D::xform(const Rect2 &p_rect) const {
return new_rect;
}
void Transform2D::set_rotation_and_scale(const real_t p_rot, const Size2 &p_scale) {
void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
columns[0][0] = Math::cos(p_rot) * p_scale.x;
columns[1][1] = Math::cos(p_rot) * p_scale.y;
columns[1][0] = -Math::sin(p_rot) * p_scale.y;
columns[0][1] = Math::sin(p_rot) * p_scale.x;
}
void Transform2D::set_rotation_scale_and_skew(const real_t p_rot, const Size2 &p_scale, const real_t p_skew) {
void Transform2D::set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, real_t p_skew) {
columns[0][0] = Math::cos(p_rot) * p_scale.x;
columns[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
columns[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;

View File

@@ -54,8 +54,8 @@ struct [[nodiscard]] Transform3D {
void rotate(const Vector3 &p_axis, real_t p_angle);
void rotate_basis(const Vector3 &p_axis, real_t p_angle);
void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0));
Transform3D looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0)) const;
void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false);
Transform3D looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false) const;
void scale(const Vector3 &p_scale);
Transform3D scaled(const Vector3 &p_scale) const;
@@ -104,8 +104,10 @@ struct [[nodiscard]] Transform3D {
void operator*=(const Transform3D &p_transform);
Transform3D operator*(const Transform3D &p_transform) const;
void operator*=(const real_t p_val);
Transform3D operator*(const real_t p_val) const;
void operator*=(real_t p_val);
Transform3D operator*(real_t p_val) const;
void operator/=(real_t p_val);
Transform3D operator/(real_t p_val) const;
Transform3D interpolate_with(const Transform3D &p_transform, real_t p_c) const;
@@ -115,11 +117,11 @@ struct [[nodiscard]] Transform3D {
basis.xform(v));
}
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, real_t tx, real_t ty, real_t tz) {
basis.set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
origin.x = tx;
origin.y = ty;
origin.z = tz;
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, real_t p_tx, real_t p_ty, real_t p_tz) {
basis.set(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz);
origin.x = p_tx;
origin.y = p_ty;
origin.z = p_tz;
}
operator String() const;
@@ -127,7 +129,7 @@ struct [[nodiscard]] Transform3D {
Transform3D() {}
Transform3D(const Basis &p_basis, const Vector3 &p_origin = Vector3());
Transform3D(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin);
Transform3D(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, real_t ox, real_t oy, real_t oz);
Transform3D(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, real_t p_ox, real_t p_oy, real_t p_oz);
};
_FORCE_INLINE_ Vector3 Transform3D::xform(const Vector3 &p_vector) const {

View File

@@ -61,13 +61,13 @@ struct [[nodiscard]] Vector2 {
real_t coord[2] = { 0 };
};
_FORCE_INLINE_ real_t &operator[](int p_idx) {
DEV_ASSERT((unsigned int)p_idx < 2);
return coord[p_idx];
_FORCE_INLINE_ real_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 2);
return coord[p_axis];
}
_FORCE_INLINE_ const real_t &operator[](int p_idx) const {
DEV_ASSERT((unsigned int)p_idx < 2);
return coord[p_idx];
_FORCE_INLINE_ const real_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 2);
return coord[p_axis];
}
_FORCE_INLINE_ Vector2::Axis min_axis_index() const {
@@ -84,7 +84,7 @@ struct [[nodiscard]] Vector2 {
real_t length() const;
real_t length_squared() const;
Vector2 limit_length(const real_t p_len = 1.0) const;
Vector2 limit_length(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));
@@ -110,19 +110,20 @@ struct [[nodiscard]] Vector2 {
real_t dot(const Vector2 &p_other) const;
real_t cross(const Vector2 &p_other) const;
Vector2 posmod(const real_t p_mod) const;
Vector2 posmod(real_t p_mod) const;
Vector2 posmodv(const Vector2 &p_modv) const;
Vector2 project(const Vector2 &p_to) const;
Vector2 plane_project(const real_t p_d, const Vector2 &p_vec) const;
Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
_FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, const real_t p_weight) const;
_FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, const real_t p_weight) const;
_FORCE_INLINE_ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const;
_FORCE_INLINE_ Vector2 cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const;
_FORCE_INLINE_ Vector2 bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const;
_FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, real_t p_weight) const;
_FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, real_t p_weight) const;
_FORCE_INLINE_ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const;
_FORCE_INLINE_ Vector2 cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const;
_FORCE_INLINE_ Vector2 bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const;
_FORCE_INLINE_ Vector2 bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const;
Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
Vector2 move_toward(const Vector2 &p_to, real_t p_delta) const;
Vector2 slide(const Vector2 &p_normal) const;
Vector2 bounce(const Vector2 &p_normal) const;
@@ -138,16 +139,16 @@ struct [[nodiscard]] Vector2 {
void operator-=(const Vector2 &p_v);
Vector2 operator*(const Vector2 &p_v1) const;
Vector2 operator*(const real_t &rvalue) const;
void operator*=(const real_t &rvalue);
void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
Vector2 operator*(real_t p_rvalue) const;
void operator*=(real_t p_rvalue);
void operator*=(const Vector2 &p_rvalue) { *this = *this * p_rvalue; }
Vector2 operator/(const Vector2 &p_v1) const;
Vector2 operator/(const real_t &rvalue) const;
Vector2 operator/(real_t p_rvalue) const;
void operator/=(const real_t &rvalue);
void operator/=(const Vector2 &rvalue) { *this = *this / rvalue; }
void operator/=(real_t p_rvalue);
void operator/=(const Vector2 &p_rvalue) { *this = *this / p_rvalue; }
Vector2 operator-() const;
@@ -160,13 +161,13 @@ struct [[nodiscard]] Vector2 {
bool operator>=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
real_t angle() const;
static Vector2 from_angle(const real_t p_angle);
static Vector2 from_angle(real_t p_angle);
_FORCE_INLINE_ Vector2 abs() const {
return Vector2(Math::abs(x), Math::abs(y));
}
Vector2 rotated(const real_t p_by) const;
Vector2 rotated(real_t p_by) const;
Vector2 orthogonal() const {
return Vector2(y, -x);
}
@@ -185,13 +186,13 @@ struct [[nodiscard]] Vector2 {
operator Vector2i() const;
_FORCE_INLINE_ Vector2() {}
_FORCE_INLINE_ Vector2(const real_t p_x, const real_t p_y) {
_FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) {
x = p_x;
y = p_y;
}
};
_FORCE_INLINE_ Vector2 Vector2::plane_project(const real_t p_d, const Vector2 &p_vec) const {
_FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
return p_vec - *this * (dot(p_vec) - p_d);
}
@@ -217,26 +218,26 @@ _FORCE_INLINE_ Vector2 Vector2::operator*(const Vector2 &p_v1) const {
return Vector2(x * p_v1.x, y * p_v1.y);
}
_FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const {
return Vector2(x * rvalue, y * rvalue);
_FORCE_INLINE_ Vector2 Vector2::operator*(real_t p_rvalue) const {
return Vector2(x * p_rvalue, y * p_rvalue);
}
_FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) {
x *= rvalue;
y *= rvalue;
_FORCE_INLINE_ void Vector2::operator*=(real_t p_rvalue) {
x *= p_rvalue;
y *= p_rvalue;
}
_FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v1) const {
return Vector2(x / p_v1.x, y / p_v1.y);
}
_FORCE_INLINE_ Vector2 Vector2::operator/(const real_t &rvalue) const {
return Vector2(x / rvalue, y / rvalue);
_FORCE_INLINE_ Vector2 Vector2::operator/(real_t p_rvalue) const {
return Vector2(x / p_rvalue, y / p_rvalue);
}
_FORCE_INLINE_ void Vector2::operator/=(const real_t &rvalue) {
x /= rvalue;
y /= rvalue;
_FORCE_INLINE_ void Vector2::operator/=(real_t p_rvalue) {
x /= p_rvalue;
y /= p_rvalue;
}
_FORCE_INLINE_ Vector2 Vector2::operator-() const {
@@ -251,16 +252,14 @@ _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
return x != p_vec2.x || y != p_vec2.y;
}
Vector2 Vector2::lerp(const Vector2 &p_to, const real_t p_weight) const {
Vector2 Vector2::lerp(const Vector2 &p_to, real_t p_weight) const {
Vector2 res = *this;
res.x += (p_weight * (p_to.x - x));
res.y += (p_weight * (p_to.y - y));
res.x = Math::lerp(res.x, p_to.x, p_weight);
res.y = Math::lerp(res.y, p_to.y, p_weight);
return res;
}
Vector2 Vector2::slerp(const Vector2 &p_to, const real_t p_weight) const {
Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const {
real_t start_length_sq = length_squared();
real_t end_length_sq = p_to.length_squared();
if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
@@ -273,31 +272,32 @@ Vector2 Vector2::slerp(const Vector2 &p_to, const real_t p_weight) const {
return rotated(angle * p_weight) * (result_length / start_length);
}
Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const {
Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const {
Vector2 res = *this;
res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
return res;
}
Vector2 Vector2::cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const {
Vector2 Vector2::cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const {
Vector2 res = *this;
res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
return res;
}
Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const {
Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const {
Vector2 res = *this;
res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
return res;
}
/* Formula from Wikipedia article on Bezier curves. */
real_t omt = (1.0 - p_t);
real_t omt2 = omt * omt;
real_t omt3 = omt2 * omt;
real_t t2 = p_t * p_t;
real_t t3 = t2 * p_t;
return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
Vector2 Vector2::bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const {
Vector2 res = *this;
res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
return res;
}
Vector2 Vector2::direction_to(const Vector2 &p_to) const {
@@ -309,19 +309,19 @@ Vector2 Vector2::direction_to(const Vector2 &p_to) const {
// Multiplication operators required to workaround issues with LLVM using implicit conversion
// to Vector2i instead for integers where it should not.
_FORCE_INLINE_ Vector2 operator*(const float p_scalar, const Vector2 &p_vec) {
_FORCE_INLINE_ Vector2 operator*(float p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector2 operator*(const double p_scalar, const Vector2 &p_vec) {
_FORCE_INLINE_ Vector2 operator*(double p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector2 operator*(const int32_t p_scalar, const Vector2 &p_vec) {
_FORCE_INLINE_ Vector2 operator*(int32_t p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector2 operator*(const int64_t p_scalar, const Vector2 &p_vec) {
_FORCE_INLINE_ Vector2 operator*(int64_t p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}

View File

@@ -61,13 +61,13 @@ struct [[nodiscard]] Vector2i {
int32_t coord[2] = { 0 };
};
_FORCE_INLINE_ int32_t &operator[](int p_idx) {
DEV_ASSERT((unsigned int)p_idx < 2);
return coord[p_idx];
_FORCE_INLINE_ int32_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 2);
return coord[p_axis];
}
_FORCE_INLINE_ const int32_t &operator[](int p_idx) const {
DEV_ASSERT((unsigned int)p_idx < 2);
return coord[p_idx];
_FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 2);
return coord[p_axis];
}
_FORCE_INLINE_ Vector2i::Axis min_axis_index() const {
@@ -94,22 +94,30 @@ struct [[nodiscard]] Vector2i {
return Vector2i(MAX(x, p_scalar), MAX(y, p_scalar));
}
double distance_to(const Vector2i &p_to) const {
return (p_to - *this).length();
}
int64_t distance_squared_to(const Vector2i &p_to) const {
return (p_to - *this).length_squared();
}
Vector2i operator+(const Vector2i &p_v) const;
void operator+=(const Vector2i &p_v);
Vector2i operator-(const Vector2i &p_v) const;
void operator-=(const Vector2i &p_v);
Vector2i operator*(const Vector2i &p_v1) const;
Vector2i operator*(const int32_t &rvalue) const;
void operator*=(const int32_t &rvalue);
Vector2i operator*(int32_t p_rvalue) const;
void operator*=(int32_t p_rvalue);
Vector2i operator/(const Vector2i &p_v1) const;
Vector2i operator/(const int32_t &rvalue) const;
void operator/=(const int32_t &rvalue);
Vector2i operator/(int32_t p_rvalue) const;
void operator/=(int32_t p_rvalue);
Vector2i operator%(const Vector2i &p_v1) const;
Vector2i operator%(const int32_t &rvalue) const;
void operator%=(const int32_t &rvalue);
Vector2i operator%(int32_t p_rvalue) const;
void operator%=(int32_t p_rvalue);
Vector2i operator-() const;
bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
@@ -124,22 +132,19 @@ struct [[nodiscard]] Vector2i {
int64_t length_squared() const;
double length() const;
int64_t distance_squared_to(const Vector2i &p_to) const;
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 abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
Vector2i snapped(const Vector2i &p_step) const;
Vector2i snappedi(int32_t p_step) const;
Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
Vector2i clampi(int32_t p_min, int32_t p_max) const;
Vector2i snapped(const Vector2i &p_step) const;
Vector2i snappedi(int32_t p_step) const;
operator String() const;
operator Vector2() const;
inline Vector2i() {}
inline Vector2i(const int32_t p_x, const int32_t p_y) {
inline Vector2i(int32_t p_x, int32_t p_y) {
x = p_x;
y = p_y;
}
@@ -147,19 +152,19 @@ struct [[nodiscard]] Vector2i {
// Multiplication operators required to workaround issues with LLVM using implicit conversion.
_FORCE_INLINE_ Vector2i operator*(const int32_t p_scalar, const Vector2i &p_vector) {
_FORCE_INLINE_ Vector2i operator*(int32_t p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector2i operator*(const int64_t p_scalar, const Vector2i &p_vector) {
_FORCE_INLINE_ Vector2i operator*(int64_t p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector2i operator*(const float p_scalar, const Vector2i &p_vector) {
_FORCE_INLINE_ Vector2i operator*(float p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector2i operator*(const double p_scalar, const Vector2i &p_vector) {
_FORCE_INLINE_ Vector2i operator*(double p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}

View File

@@ -59,12 +59,12 @@ struct [[nodiscard]] Vector3 {
real_t coord[3] = { 0 };
};
_FORCE_INLINE_ const real_t &operator[](const int p_axis) const {
_FORCE_INLINE_ const real_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 3);
return coord[p_axis];
}
_FORCE_INLINE_ real_t &operator[](const int p_axis) {
_FORCE_INLINE_ real_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 3);
return coord[p_axis];
}
@@ -100,36 +100,38 @@ struct [[nodiscard]] Vector3 {
_FORCE_INLINE_ Vector3 normalized() const;
_FORCE_INLINE_ bool is_normalized() const;
_FORCE_INLINE_ Vector3 inverse() const;
Vector3 limit_length(const real_t p_len = 1.0) const;
Vector3 limit_length(real_t p_len = 1.0) const;
_FORCE_INLINE_ void zero();
void snap(const Vector3 p_val);
void snapf(real_t p_val);
Vector3 snapped(const Vector3 p_val) const;
Vector3 snappedf(real_t p_val) const;
void snap(const Vector3 &p_step);
void snapf(real_t p_step);
Vector3 snapped(const Vector3 &p_step) const;
Vector3 snappedf(real_t p_step) const;
void rotate(const Vector3 &p_axis, const real_t p_angle);
Vector3 rotated(const Vector3 &p_axis, const real_t p_angle) const;
void rotate(const Vector3 &p_axis, real_t p_angle);
Vector3 rotated(const Vector3 &p_axis, real_t p_angle) const;
/* Static Methods between 2 vector3s */
_FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, const real_t p_weight) const;
_FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, const real_t p_weight) const;
_FORCE_INLINE_ Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const;
_FORCE_INLINE_ Vector3 cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const;
_FORCE_INLINE_ Vector3 bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const;
_FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, real_t p_weight) const;
_FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, real_t p_weight) const;
_FORCE_INLINE_ Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const;
_FORCE_INLINE_ Vector3 cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const;
_FORCE_INLINE_ Vector3 bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const;
_FORCE_INLINE_ Vector3 bezier_derivative(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const;
Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const;
Vector3 move_toward(const Vector3 &p_to, real_t p_delta) const;
Vector2 octahedron_encode() const;
static Vector3 octahedron_decode(const Vector2 &p_oct);
Vector2 octahedron_tangent_encode(const float sign) const;
static Vector3 octahedron_tangent_decode(const Vector2 &p_oct, float *sign);
Vector2 octahedron_tangent_encode(float p_sign) const;
static Vector3 octahedron_tangent_decode(const Vector2 &p_oct, float *r_sign);
_FORCE_INLINE_ Vector3 cross(const Vector3 &p_with) const;
_FORCE_INLINE_ real_t dot(const Vector3 &p_with) const;
Basis outer(const Vector3 &p_with) const;
_FORCE_INLINE_ Vector3 get_any_perpendicular() const;
_FORCE_INLINE_ Vector3 abs() const;
_FORCE_INLINE_ Vector3 floor() const;
@@ -142,7 +144,7 @@ struct [[nodiscard]] Vector3 {
_FORCE_INLINE_ real_t distance_to(const Vector3 &p_to) const;
_FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_to) const;
_FORCE_INLINE_ Vector3 posmod(const real_t p_mod) const;
_FORCE_INLINE_ Vector3 posmod(real_t p_mod) const;
_FORCE_INLINE_ Vector3 posmodv(const Vector3 &p_modv) const;
_FORCE_INLINE_ Vector3 project(const Vector3 &p_to) const;
@@ -169,10 +171,10 @@ struct [[nodiscard]] Vector3 {
_FORCE_INLINE_ Vector3 &operator/=(const Vector3 &p_v);
_FORCE_INLINE_ Vector3 operator/(const Vector3 &p_v) const;
_FORCE_INLINE_ Vector3 &operator*=(const real_t p_scalar);
_FORCE_INLINE_ Vector3 operator*(const real_t p_scalar) const;
_FORCE_INLINE_ Vector3 &operator/=(const real_t p_scalar);
_FORCE_INLINE_ Vector3 operator/(const real_t p_scalar) const;
_FORCE_INLINE_ Vector3 &operator*=(real_t p_scalar);
_FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const;
_FORCE_INLINE_ Vector3 &operator/=(real_t p_scalar);
_FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const;
_FORCE_INLINE_ Vector3 operator-() const;
@@ -187,7 +189,7 @@ struct [[nodiscard]] Vector3 {
operator Vector3i() const;
_FORCE_INLINE_ Vector3() {}
_FORCE_INLINE_ Vector3(const real_t p_x, const real_t p_y, const real_t p_z) {
_FORCE_INLINE_ Vector3(real_t p_x, real_t p_y, real_t p_z) {
x = p_x;
y = p_y;
z = p_z;
@@ -227,14 +229,15 @@ Vector3 Vector3::round() const {
return Vector3(Math::round(x), Math::round(y), Math::round(z));
}
Vector3 Vector3::lerp(const Vector3 &p_to, const real_t p_weight) const {
return Vector3(
x + (p_weight * (p_to.x - x)),
y + (p_weight * (p_to.y - y)),
z + (p_weight * (p_to.z - z)));
Vector3 Vector3::lerp(const Vector3 &p_to, real_t p_weight) const {
Vector3 res = *this;
res.x = Math::lerp(res.x, p_to.x, p_weight);
res.y = Math::lerp(res.y, p_to.y, p_weight);
res.z = Math::lerp(res.z, p_to.z, p_weight);
return res;
}
Vector3 Vector3::slerp(const Vector3 &p_to, const real_t p_weight) const {
Vector3 Vector3::slerp(const Vector3 &p_to, real_t p_weight) const {
// This method seems more complicated than it really is, since we write out
// the internals of some methods for efficiency (mainly, checking length).
real_t start_length_sq = length_squared();
@@ -256,7 +259,7 @@ Vector3 Vector3::slerp(const Vector3 &p_to, const real_t p_weight) const {
return rotated(axis, angle * p_weight) * (result_length / start_length);
}
Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const {
Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const {
Vector3 res = *this;
res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
@@ -264,7 +267,7 @@ Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, c
return res;
}
Vector3 Vector3::cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const {
Vector3 Vector3::cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const {
Vector3 res = *this;
res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
@@ -272,17 +275,20 @@ Vector3 Vector3::cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_
return res;
}
Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const {
Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const {
Vector3 res = *this;
res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
res.z = Math::bezier_interpolate(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
return res;
}
/* Formula from Wikipedia article on Bezier curves. */
real_t omt = (1.0 - p_t);
real_t omt2 = omt * omt;
real_t omt3 = omt2 * omt;
real_t t2 = p_t * p_t;
real_t t3 = t2 * p_t;
return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
Vector3 Vector3::bezier_derivative(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const {
Vector3 res = *this;
res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
res.z = Math::bezier_derivative(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
return res;
}
real_t Vector3::distance_to(const Vector3 &p_to) const {
@@ -293,7 +299,7 @@ real_t Vector3::distance_squared_to(const Vector3 &p_to) const {
return (p_to - *this).length_squared();
}
Vector3 Vector3::posmod(const real_t p_mod) const {
Vector3 Vector3::posmod(real_t p_mod) const {
return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
}
@@ -322,6 +328,16 @@ Vector3 Vector3::direction_to(const Vector3 &p_to) const {
return ret;
}
Vector3 Vector3::get_any_perpendicular() const {
// Return the any perpendicular vector by cross product with the Vector3.RIGHT or Vector3.UP,
// whichever has the greater angle to the current vector with the sign of each element positive.
// The only essence is "to avoid being parallel to the current vector", and there is no mathematical basis for using Vector3.RIGHT and Vector3.UP,
// since it could be a different vector depending on the prior branching code Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z).
// However, it would be reasonable to use any of the axes of the basis, as it is simpler to calculate.
ERR_FAIL_COND_V_MSG(is_zero_approx(), Vector3(0, 0, 0), "The Vector3 must not be zero.");
return cross((Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z)) ? Vector3(1, 0, 0) : Vector3(0, 1, 0)).normalized();
}
/* Operators */
Vector3 &Vector3::operator+=(const Vector3 &p_v) {
@@ -368,7 +384,7 @@ Vector3 Vector3::operator/(const Vector3 &p_v) const {
return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
}
Vector3 &Vector3::operator*=(const real_t p_scalar) {
Vector3 &Vector3::operator*=(real_t p_scalar) {
x *= p_scalar;
y *= p_scalar;
z *= p_scalar;
@@ -378,34 +394,34 @@ Vector3 &Vector3::operator*=(const real_t p_scalar) {
// Multiplication operators required to workaround issues with LLVM using implicit conversion
// to Vector3i instead for integers where it should not.
_FORCE_INLINE_ Vector3 operator*(const float p_scalar, const Vector3 &p_vec) {
_FORCE_INLINE_ Vector3 operator*(float p_scalar, const Vector3 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector3 operator*(const double p_scalar, const Vector3 &p_vec) {
_FORCE_INLINE_ Vector3 operator*(double p_scalar, const Vector3 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector3 operator*(const int32_t p_scalar, const Vector3 &p_vec) {
_FORCE_INLINE_ Vector3 operator*(int32_t p_scalar, const Vector3 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector3 operator*(const int64_t p_scalar, const Vector3 &p_vec) {
_FORCE_INLINE_ Vector3 operator*(int64_t p_scalar, const Vector3 &p_vec) {
return p_vec * p_scalar;
}
Vector3 Vector3::operator*(const real_t p_scalar) const {
Vector3 Vector3::operator*(real_t p_scalar) const {
return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
}
Vector3 &Vector3::operator/=(const real_t p_scalar) {
Vector3 &Vector3::operator/=(real_t p_scalar) {
x /= p_scalar;
y /= p_scalar;
z /= p_scalar;
return *this;
}
Vector3 Vector3::operator/(const real_t p_scalar) const {
Vector3 Vector3::operator/(real_t p_scalar) const {
return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
}
@@ -519,9 +535,9 @@ void Vector3::zero() {
// slide returns the component of the vector along the given plane, specified by its normal vector.
Vector3 Vector3::slide(const Vector3 &p_normal) const {
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 " + p_normal.operator String() + " must be normalized.");
#endif
return *this - p_normal * this->dot(p_normal);
return *this - p_normal * dot(p_normal);
}
Vector3 Vector3::bounce(const Vector3 &p_normal) const {
@@ -530,9 +546,9 @@ Vector3 Vector3::bounce(const Vector3 &p_normal) const {
Vector3 Vector3::reflect(const Vector3 &p_normal) const {
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 " + p_normal.operator String() + " must be normalized.");
#endif
return 2.0f * p_normal * this->dot(p_normal) - *this;
return 2.0f * p_normal * dot(p_normal) - *this;
}
} // namespace godot

View File

@@ -57,12 +57,12 @@ struct [[nodiscard]] Vector3i {
int32_t coord[3] = { 0 };
};
_FORCE_INLINE_ const int32_t &operator[](const int p_axis) const {
_FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 3);
return coord[p_axis];
}
_FORCE_INLINE_ int32_t &operator[](const int p_axis) {
_FORCE_INLINE_ int32_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 3);
return coord[p_axis];
}
@@ -89,17 +89,17 @@ struct [[nodiscard]] Vector3i {
_FORCE_INLINE_ int64_t length_squared() const;
_FORCE_INLINE_ double length() const;
_FORCE_INLINE_ int64_t distance_squared_to(const Vector3i &p_to) const;
_FORCE_INLINE_ double distance_to(const Vector3i &p_to) const;
_FORCE_INLINE_ void zero();
_FORCE_INLINE_ Vector3i abs() const;
_FORCE_INLINE_ Vector3i sign() const;
Vector3i snapped(const Vector3i &p_step) const;
Vector3i snappedi(int32_t p_step) const;
Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
Vector3i clampi(int32_t p_min, int32_t p_max) const;
Vector3i snapped(const Vector3i &p_step) const;
Vector3i snappedi(int32_t p_step) const;
_FORCE_INLINE_ double distance_to(const Vector3i &p_to) const;
_FORCE_INLINE_ int64_t distance_squared_to(const Vector3i &p_to) const;
/* Operators */
@@ -114,12 +114,12 @@ struct [[nodiscard]] Vector3i {
_FORCE_INLINE_ Vector3i &operator%=(const Vector3i &p_v);
_FORCE_INLINE_ Vector3i operator%(const Vector3i &p_v) const;
_FORCE_INLINE_ Vector3i &operator*=(const int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator/=(const int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator/(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator%=(const int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator%(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator*=(int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator*(int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator/=(int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator/(int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator%=(int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator%(int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i operator-() const;
@@ -134,7 +134,7 @@ struct [[nodiscard]] Vector3i {
operator Vector3() const;
_FORCE_INLINE_ Vector3i() {}
_FORCE_INLINE_ Vector3i(const int32_t p_x, const int32_t p_y, const int32_t p_z) {
_FORCE_INLINE_ Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) {
x = p_x;
y = p_y;
z = p_z;
@@ -149,14 +149,6 @@ double Vector3i::length() const {
return Math::sqrt((double)length_squared());
}
int64_t Vector3i::distance_squared_to(const Vector3i &p_to) const {
return (p_to - *this).length_squared();
}
double Vector3i::distance_to(const Vector3i &p_to) const {
return (p_to - *this).length();
}
Vector3i Vector3i::abs() const {
return Vector3i(Math::abs(x), Math::abs(y), Math::abs(z));
}
@@ -165,6 +157,14 @@ Vector3i Vector3i::sign() const {
return Vector3i(SIGN(x), SIGN(y), SIGN(z));
}
double Vector3i::distance_to(const Vector3i &p_to) const {
return (p_to - *this).length();
}
int64_t Vector3i::distance_squared_to(const Vector3i &p_to) const {
return (p_to - *this).length_squared();
}
/* Operators */
Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
@@ -222,54 +222,54 @@ Vector3i Vector3i::operator%(const Vector3i &p_v) const {
return Vector3i(x % p_v.x, y % p_v.y, z % p_v.z);
}
Vector3i &Vector3i::operator*=(const int32_t p_scalar) {
Vector3i &Vector3i::operator*=(int32_t p_scalar) {
x *= p_scalar;
y *= p_scalar;
z *= p_scalar;
return *this;
}
Vector3i Vector3i::operator*(const int32_t p_scalar) const {
Vector3i Vector3i::operator*(int32_t p_scalar) const {
return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar);
}
// Multiplication operators required to workaround issues with LLVM using implicit conversion.
_FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar, const Vector3i &p_vector) {
_FORCE_INLINE_ Vector3i operator*(int32_t p_scalar, const Vector3i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector3i operator*(const int64_t p_scalar, const Vector3i &p_vector) {
_FORCE_INLINE_ Vector3i operator*(int64_t p_scalar, const Vector3i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector3i operator*(const float p_scalar, const Vector3i &p_vector) {
_FORCE_INLINE_ Vector3i operator*(float p_scalar, const Vector3i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector3i operator*(const double p_scalar, const Vector3i &p_vector) {
_FORCE_INLINE_ Vector3i operator*(double p_scalar, const Vector3i &p_vector) {
return p_vector * p_scalar;
}
Vector3i &Vector3i::operator/=(const int32_t p_scalar) {
Vector3i &Vector3i::operator/=(int32_t p_scalar) {
x /= p_scalar;
y /= p_scalar;
z /= p_scalar;
return *this;
}
Vector3i Vector3i::operator/(const int32_t p_scalar) const {
Vector3i Vector3i::operator/(int32_t p_scalar) const {
return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar);
}
Vector3i &Vector3i::operator%=(const int32_t p_scalar) {
Vector3i &Vector3i::operator%=(int32_t p_scalar) {
x %= p_scalar;
y %= p_scalar;
z %= p_scalar;
return *this;
}
Vector3i Vector3i::operator%(const int32_t p_scalar) const {
Vector3i Vector3i::operator%(int32_t p_scalar) const {
return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar);
}

View File

@@ -36,6 +36,7 @@
namespace godot {
class String;
struct Vector4i;
struct [[nodiscard]] Vector4 {
static const int AXIS_COUNT = 4;
@@ -54,15 +55,14 @@ struct [[nodiscard]] Vector4 {
real_t z;
real_t w;
};
[[deprecated("Use coord instead")]] real_t components[4];
real_t coord[4] = { 0, 0, 0, 0 };
};
_FORCE_INLINE_ real_t &operator[](const int p_axis) {
_FORCE_INLINE_ real_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 4);
return coord[p_axis];
}
_FORCE_INLINE_ const real_t &operator[](const int p_axis) const {
_FORCE_INLINE_ const real_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 4);
return coord[p_axis];
}
@@ -104,11 +104,11 @@ struct [[nodiscard]] Vector4 {
Vector4 floor() const;
Vector4 ceil() const;
Vector4 round() const;
Vector4 lerp(const Vector4 &p_to, const real_t p_weight) const;
Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const;
Vector4 cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const;
Vector4 lerp(const Vector4 &p_to, real_t p_weight) const;
Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, real_t p_weight) const;
Vector4 cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const;
Vector4 posmod(const real_t p_mod) const;
Vector4 posmod(real_t p_mod) const;
Vector4 posmodv(const Vector4 &p_modv) const;
void snap(const Vector4 &p_step);
void snapf(real_t p_step);
@@ -124,15 +124,15 @@ struct [[nodiscard]] Vector4 {
_FORCE_INLINE_ void operator-=(const Vector4 &p_vec4);
_FORCE_INLINE_ void operator*=(const Vector4 &p_vec4);
_FORCE_INLINE_ void operator/=(const Vector4 &p_vec4);
_FORCE_INLINE_ void operator*=(const real_t &s);
_FORCE_INLINE_ void operator/=(const real_t &s);
_FORCE_INLINE_ void operator*=(real_t p_s);
_FORCE_INLINE_ void operator/=(real_t p_s);
_FORCE_INLINE_ Vector4 operator+(const Vector4 &p_vec4) const;
_FORCE_INLINE_ Vector4 operator-(const Vector4 &p_vec4) const;
_FORCE_INLINE_ Vector4 operator*(const Vector4 &p_vec4) const;
_FORCE_INLINE_ Vector4 operator/(const Vector4 &p_vec4) const;
_FORCE_INLINE_ Vector4 operator-() const;
_FORCE_INLINE_ Vector4 operator*(const real_t &s) const;
_FORCE_INLINE_ Vector4 operator/(const real_t &s) const;
_FORCE_INLINE_ Vector4 operator*(real_t p_s) const;
_FORCE_INLINE_ Vector4 operator/(real_t p_s) const;
_FORCE_INLINE_ bool operator==(const Vector4 &p_vec4) const;
_FORCE_INLINE_ bool operator!=(const Vector4 &p_vec4) const;
@@ -142,28 +142,14 @@ struct [[nodiscard]] Vector4 {
_FORCE_INLINE_ bool operator<=(const Vector4 &p_vec4) const;
operator String() const;
operator Vector4i() const;
_FORCE_INLINE_ Vector4() {}
_FORCE_INLINE_ Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
x(p_x),
y(p_y),
z(p_z),
w(p_w) {
}
Vector4(const Vector4 &p_vec4) :
x(p_vec4.x),
y(p_vec4.y),
z(p_vec4.z),
w(p_vec4.w) {
}
void operator=(const Vector4 &p_vec4) {
x = p_vec4.x;
y = p_vec4.y;
z = p_vec4.z;
w = p_vec4.w;
_FORCE_INLINE_ Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
x = p_x;
y = p_y;
z = p_z;
w = p_w;
}
};
@@ -202,15 +188,15 @@ void Vector4::operator/=(const Vector4 &p_vec4) {
z /= p_vec4.z;
w /= p_vec4.w;
}
void Vector4::operator*=(const real_t &s) {
x *= s;
y *= s;
z *= s;
w *= s;
void Vector4::operator*=(real_t p_s) {
x *= p_s;
y *= p_s;
z *= p_s;
w *= p_s;
}
void Vector4::operator/=(const real_t &s) {
*this *= 1.0f / s;
void Vector4::operator/=(real_t p_s) {
*this *= 1.0f / p_s;
}
Vector4 Vector4::operator+(const Vector4 &p_vec4) const {
@@ -233,12 +219,12 @@ Vector4 Vector4::operator-() const {
return Vector4(-x, -y, -z, -w);
}
Vector4 Vector4::operator*(const real_t &s) const {
return Vector4(x * s, y * s, z * s, w * s);
Vector4 Vector4::operator*(real_t p_s) const {
return Vector4(x * p_s, y * p_s, z * p_s, w * p_s);
}
Vector4 Vector4::operator/(const real_t &s) const {
return *this * (1.0f / s);
Vector4 Vector4::operator/(real_t p_s) const {
return *this * (1.0f / p_s);
}
bool Vector4::operator==(const Vector4 &p_vec4) const {
@@ -301,19 +287,19 @@ bool Vector4::operator>=(const Vector4 &p_v) const {
return x > p_v.x;
}
_FORCE_INLINE_ Vector4 operator*(const float p_scalar, const Vector4 &p_vec) {
_FORCE_INLINE_ Vector4 operator*(float p_scalar, const Vector4 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector4 operator*(const double p_scalar, const Vector4 &p_vec) {
_FORCE_INLINE_ Vector4 operator*(double p_scalar, const Vector4 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector4 operator*(const int32_t p_scalar, const Vector4 &p_vec) {
_FORCE_INLINE_ Vector4 operator*(int32_t p_scalar, const Vector4 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector4 operator*(const int64_t p_scalar, const Vector4 &p_vec) {
_FORCE_INLINE_ Vector4 operator*(int64_t p_scalar, const Vector4 &p_vec) {
return p_vec * p_scalar;
}

View File

@@ -59,12 +59,12 @@ struct [[nodiscard]] Vector4i {
int32_t coord[4] = { 0 };
};
_FORCE_INLINE_ const int32_t &operator[](const int p_axis) const {
_FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 4);
return coord[p_axis];
}
_FORCE_INLINE_ int32_t &operator[](const int p_axis) {
_FORCE_INLINE_ int32_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 4);
return coord[p_axis];
}
@@ -91,17 +91,17 @@ struct [[nodiscard]] Vector4i {
_FORCE_INLINE_ int64_t length_squared() const;
_FORCE_INLINE_ double length() const;
_FORCE_INLINE_ int64_t distance_squared_to(const Vector4i &p_to) const;
_FORCE_INLINE_ double distance_to(const Vector4i &p_to) const;
_FORCE_INLINE_ void zero();
_FORCE_INLINE_ double distance_to(const Vector4i &p_to) const;
_FORCE_INLINE_ int64_t distance_squared_to(const Vector4i &p_to) const;
_FORCE_INLINE_ Vector4i abs() const;
_FORCE_INLINE_ Vector4i sign() const;
Vector4i snapped(const Vector4i &p_step) const;
Vector4i snappedi(int32_t p_step) const;
Vector4i clamp(const Vector4i &p_min, const Vector4i &p_max) const;
Vector4i clampi(int32_t p_min, int32_t p_max) const;
Vector4i snapped(const Vector4i &p_step) const;
Vector4i snappedi(int32_t p_step) const;
/* Operators */
@@ -116,12 +116,12 @@ struct [[nodiscard]] Vector4i {
_FORCE_INLINE_ Vector4i &operator%=(const Vector4i &p_v);
_FORCE_INLINE_ Vector4i operator%(const Vector4i &p_v) const;
_FORCE_INLINE_ Vector4i &operator*=(const int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator*(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i &operator/=(const int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator/(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i &operator%=(const int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator%(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i &operator*=(int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator*(int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i &operator/=(int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator/(int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i &operator%=(int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator%(int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i operator-() const;
@@ -137,7 +137,7 @@ struct [[nodiscard]] Vector4i {
_FORCE_INLINE_ Vector4i() {}
Vector4i(const Vector4 &p_vec4);
_FORCE_INLINE_ Vector4i(const int32_t p_x, const int32_t p_y, const int32_t p_z, const int32_t p_w) {
_FORCE_INLINE_ Vector4i(int32_t p_x, int32_t p_y, int32_t p_z, int32_t p_w) {
x = p_x;
y = p_y;
z = p_z;
@@ -153,20 +153,20 @@ double Vector4i::length() const {
return Math::sqrt((double)length_squared());
}
int64_t Vector4i::distance_squared_to(const Vector4i &p_to) const {
return (p_to - *this).length_squared();
}
double Vector4i::distance_to(const Vector4i &p_to) const {
return (p_to - *this).length();
}
int64_t Vector4i::distance_squared_to(const Vector4i &p_to) const {
return (p_to - *this).length_squared();
}
Vector4i Vector4i::abs() const {
return Vector4i(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
}
Vector4i Vector4i::sign() const {
return Vector4i(Math::sign(x), Math::sign(y), Math::sign(z), Math::sign(w));
return Vector4i(SIGN(x), SIGN(y), SIGN(z), SIGN(w));
}
/* Operators */
@@ -231,7 +231,7 @@ Vector4i Vector4i::operator%(const Vector4i &p_v) const {
return Vector4i(x % p_v.x, y % p_v.y, z % p_v.z, w % p_v.w);
}
Vector4i &Vector4i::operator*=(const int32_t p_scalar) {
Vector4i &Vector4i::operator*=(int32_t p_scalar) {
x *= p_scalar;
y *= p_scalar;
z *= p_scalar;
@@ -239,29 +239,29 @@ Vector4i &Vector4i::operator*=(const int32_t p_scalar) {
return *this;
}
Vector4i Vector4i::operator*(const int32_t p_scalar) const {
Vector4i Vector4i::operator*(int32_t p_scalar) const {
return Vector4i(x * p_scalar, y * p_scalar, z * p_scalar, w * p_scalar);
}
// Multiplication operators required to workaround issues with LLVM using implicit conversion.
_FORCE_INLINE_ Vector4i operator*(const int32_t p_scalar, const Vector4i &p_vector) {
_FORCE_INLINE_ Vector4i operator*(int32_t p_scalar, const Vector4i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector4i operator*(const int64_t p_scalar, const Vector4i &p_vector) {
_FORCE_INLINE_ Vector4i operator*(int64_t p_scalar, const Vector4i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector4i operator*(const float p_scalar, const Vector4i &p_vector) {
_FORCE_INLINE_ Vector4i operator*(float p_scalar, const Vector4i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector4i operator*(const double p_scalar, const Vector4i &p_vector) {
_FORCE_INLINE_ Vector4i operator*(double p_scalar, const Vector4i &p_vector) {
return p_vector * p_scalar;
}
Vector4i &Vector4i::operator/=(const int32_t p_scalar) {
Vector4i &Vector4i::operator/=(int32_t p_scalar) {
x /= p_scalar;
y /= p_scalar;
z /= p_scalar;
@@ -269,11 +269,11 @@ Vector4i &Vector4i::operator/=(const int32_t p_scalar) {
return *this;
}
Vector4i Vector4i::operator/(const int32_t p_scalar) const {
Vector4i Vector4i::operator/(int32_t p_scalar) const {
return Vector4i(x / p_scalar, y / p_scalar, z / p_scalar, w / p_scalar);
}
Vector4i &Vector4i::operator%=(const int32_t p_scalar) {
Vector4i &Vector4i::operator%=(int32_t p_scalar) {
x %= p_scalar;
y %= p_scalar;
z %= p_scalar;
@@ -281,7 +281,7 @@ Vector4i &Vector4i::operator%=(const int32_t p_scalar) {
return *this;
}
Vector4i Vector4i::operator%(const int32_t p_scalar) const {
Vector4i Vector4i::operator%(int32_t p_scalar) const {
return Vector4i(x % p_scalar, y % p_scalar, z % p_scalar, w % p_scalar);
}

View File

@@ -119,55 +119,75 @@ AABB AABB::intersection(const AABB &p_aabb) const {
return AABB(min, max - min);
}
bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const {
// Note that this routine returns the BACKTRACKED (i.e. behind the ray origin)
// intersection point + normal if INSIDE the AABB.
// The caller can therefore decide when INSIDE whether to use the
// backtracked intersection, or use p_from as the intersection, and
// carry on progressing without e.g. reflecting against the normal.
bool AABB::find_intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, bool &r_inside, Vector3 *r_intersection_point, Vector3 *r_normal) const {
#ifdef MATH_CHECKS
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
}
#endif
Vector3 c1, c2;
Vector3 end = position + size;
real_t near = -1e20;
real_t far = 1e20;
real_t tmin = -1e20;
real_t tmax = 1e20;
int axis = 0;
// Make sure r_inside is always initialized,
// to prevent reading uninitialized data in the client code.
r_inside = false;
for (int i = 0; i < 3; i++) {
if (p_dir[i] == 0) {
if ((p_from[i] < position[i]) || (p_from[i] > end[i])) {
return false;
}
} else { // ray not parallel to planes in this direction
c1[i] = (position[i] - p_from[i]) / p_dir[i];
c2[i] = (end[i] - p_from[i]) / p_dir[i];
real_t t1 = (position[i] - p_from[i]) / p_dir[i];
real_t t2 = (end[i] - p_from[i]) / p_dir[i];
if (c1[i] > c2[i]) {
SWAP(c1, c2);
if (t1 > t2) {
SWAP(t1, t2);
}
if (c1[i] > near) {
near = c1[i];
if (t1 >= tmin) {
tmin = t1;
axis = i;
}
if (c2[i] < far) {
far = c2[i];
if (t2 < tmax) {
if (t2 < 0) {
return false;
}
tmax = t2;
}
if ((near > far) || (far < 0)) {
if (tmin > tmax) {
return false;
}
}
}
if (r_clip) {
*r_clip = c1;
// Did the ray start from inside the box?
// In which case the intersection returned is the point of entry
// (behind the ray start) or the calling routine can use the ray origin as intersection point.
r_inside = tmin < 0;
if (r_intersection_point) {
*r_intersection_point = p_from + p_dir * tmin;
// Prevent float error by making sure the point is exactly
// on the AABB border on the relevant axis.
r_intersection_point->coord[axis] = (p_dir[axis] >= 0) ? position.coord[axis] : end.coord[axis];
}
if (r_normal) {
*r_normal = Vector3();
(*r_normal)[axis] = p_dir[axis] ? -1 : 1;
(*r_normal)[axis] = (p_dir[axis] >= 0) ? -1 : 1;
}
return true;
}
bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const {
bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_intersection_point, Vector3 *r_normal) const {
#ifdef MATH_CHECKS
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
@@ -225,8 +245,8 @@ bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector
*r_normal = normal;
}
if (r_clip) {
*r_clip = p_from + rel * min;
if (r_intersection_point) {
*r_intersection_point = p_from + rel * min;
}
return true;
@@ -412,7 +432,15 @@ Variant AABB::intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to
Variant AABB::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const {
Vector3 inters;
if (intersects_ray(p_from, p_dir, &inters)) {
bool inside = false;
if (find_intersects_ray(p_from, p_dir, inside, &inters)) {
// When inside the intersection point may be BEHIND the ray,
// so for general use we return the ray origin.
if (inside) {
return p_from;
}
return inters;
}
return Variant();

View File

@@ -29,31 +29,17 @@
/**************************************************************************/
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/basis.hpp>
#include <godot_cpp/variant/string.hpp>
using namespace godot;
#define cofac(row1, col1, row2, col2) \
(rows[row1][col1] * rows[row2][col2] - rows[row1][col2] * rows[row2][col1])
namespace godot {
void Basis::from_z(const Vector3 &p_z) {
if (Math::abs(p_z.z) > (real_t)Math_SQRT12) {
// choose p in y-z plane
real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2];
real_t k = 1.0f / Math::sqrt(a);
rows[0] = Vector3(0, -p_z[2] * k, p_z[1] * k);
rows[1] = Vector3(a * k, -p_z[0] * rows[0][2], p_z[0] * rows[0][1]);
} else {
// choose p in x-y plane
real_t a = p_z.x * p_z.x + p_z.y * p_z.y;
real_t k = 1.0f / Math::sqrt(a);
rows[0] = Vector3(-p_z.y * k, p_z.x * k, 0);
rows[1] = Vector3(-p_z.z * rows[0].y, p_z.z * rows[0].x, a * k);
}
rows[2] = p_z;
}
void Basis::invert() {
real_t co[3] = {
cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
@@ -107,13 +93,35 @@ Basis Basis::orthogonalized() const {
return c;
}
// Returns true if the basis vectors are orthogonal (perpendicular), so it has no skew or shear, and can be decomposed into rotation and scale.
// See https://en.wikipedia.org/wiki/Orthogonal_basis
bool Basis::is_orthogonal() const {
Basis identity;
Basis m = (*this) * transposed();
return m.is_equal_approx(identity);
const Vector3 x = get_column(0);
const Vector3 y = get_column(1);
const Vector3 z = get_column(2);
return Math::is_zero_approx(x.dot(y)) && Math::is_zero_approx(x.dot(z)) && Math::is_zero_approx(y.dot(z));
}
// Returns true if the basis vectors are orthonormal (orthogonal and normalized), so it has no scale, skew, or shear.
// See https://en.wikipedia.org/wiki/Orthonormal_basis
bool Basis::is_orthonormal() const {
const Vector3 x = get_column(0);
const Vector3 y = get_column(1);
const Vector3 z = get_column(2);
return Math::is_equal_approx(x.length_squared(), 1) && Math::is_equal_approx(y.length_squared(), 1) && Math::is_equal_approx(z.length_squared(), 1) && Math::is_zero_approx(x.dot(y)) && Math::is_zero_approx(x.dot(z)) && Math::is_zero_approx(y.dot(z));
}
// Returns true if the basis is conformal (orthogonal, uniform scale, preserves angles and distance ratios).
// See https://en.wikipedia.org/wiki/Conformal_linear_transformation
bool Basis::is_conformal() const {
const Vector3 x = get_column(0);
const Vector3 y = get_column(1);
const Vector3 z = get_column(2);
const real_t x_len_sq = x.length_squared();
return Math::is_equal_approx(x_len_sq, y.length_squared()) && Math::is_equal_approx(x_len_sq, z.length_squared()) && Math::is_zero_approx(x.dot(y)) && Math::is_zero_approx(x.dot(z)) && Math::is_zero_approx(y.dot(z));
}
// Returns true if the basis only has diagonal elements, so it may only have scale or flip, but no rotation, skew, or shear.
bool Basis::is_diagonal() const {
return (
Math::is_zero_approx(rows[0][1]) && Math::is_zero_approx(rows[0][2]) &&
@@ -121,8 +129,9 @@ bool Basis::is_diagonal() const {
Math::is_zero_approx(rows[2][0]) && Math::is_zero_approx(rows[2][1]));
}
// Returns true if the basis is a pure rotation matrix, so it has no scale, skew, shear, or flip.
bool Basis::is_rotation() const {
return Math::is_equal_approx(determinant(), 1, (real_t)UNIT_EPSILON) && is_orthogonal();
return is_conformal() && Math::is_equal_approx(determinant(), 1, (real_t)UNIT_EPSILON);
}
#ifdef MATH_CHECKS
@@ -257,29 +266,26 @@ void Basis::scale_orthogonal(const Vector3 &p_scale) {
Basis Basis::scaled_orthogonal(const Vector3 &p_scale) const {
Basis m = *this;
Vector3 s = Vector3(-1, -1, -1) + p_scale;
bool sign = std::signbit(s.x + s.y + s.z);
Basis b = m.orthonormalized();
s = b.xform_inv(s);
Vector3 dots;
Basis b;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
dots[j] += s[i] * Math::abs(m.get_column(i).normalized().dot(b.get_column(j)));
dots[j] += s[i] * abs(m.get_column(i).normalized().dot(b.get_column(j)));
}
}
if (sign != std::signbit(dots.x + dots.y + dots.z)) {
dots = -dots;
}
m.scale_local(Vector3(1, 1, 1) + dots);
return m;
}
float Basis::get_uniform_scale() const {
real_t Basis::get_uniform_scale() const {
return (rows[0].length() + rows[1].length() + rows[2].length()) / 3.0f;
}
void Basis::make_scale_uniform() {
float l = (rows[0].length() + rows[1].length() + rows[2].length()) / 3.0f;
for (int i = 0; i < 3; i++) {
rows[i].normalize();
rows[i] *= l;
}
}
Basis Basis::scaled_local(const Vector3 &p_scale) const {
return (*this) * Basis::from_scale(p_scale);
}
@@ -291,7 +297,7 @@ Vector3 Basis::get_scale_abs() const {
Vector3(rows[0][2], rows[1][2], rows[2][2]).length());
}
Vector3 Basis::get_scale_local() const {
Vector3 Basis::get_scale_global() const {
real_t det_sign = SIGN(determinant());
return det_sign * Vector3(rows[0].length(), rows[1].length(), rows[2].length());
}
@@ -418,7 +424,7 @@ void Basis::rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction)
real_t dot = p_start_direction.dot(p_end_direction);
dot = CLAMP(dot, -1.0f, 1.0f);
const real_t angle_rads = Math::acos(dot);
set_axis_angle(axis, angle_rads);
*this = Basis(axis, angle_rads) * (*this);
}
}
@@ -453,8 +459,13 @@ void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) cons
}
Vector3 Basis::get_euler(EulerOrder p_order) const {
// This epsilon value results in angles within a +/- 0.04 degree range being simplified/truncated.
// Based on testing, this is the largest the epsilon can be without the angle truncation becoming
// visually noticeable.
const real_t epsilon = 0.00000025;
switch (p_order) {
case EULER_ORDER_XYZ: {
case EulerOrder::EULER_ORDER_XYZ: {
// Euler angles in XYZ convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -464,8 +475,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
Vector3 euler;
real_t sy = rows[0][2];
if (sy < (1.0f - (real_t)CMP_EPSILON)) {
if (sy > -(1.0f - (real_t)CMP_EPSILON)) {
if (sy < (1.0f - epsilon)) {
if (sy > -(1.0f - epsilon)) {
// is this a pure Y rotation?
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[1][2] == 0 && rows[2][1] == 0 && rows[1][1] == 1) {
// return the simplest form (human friendlier in editor and scripts)
@@ -489,7 +500,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
}
return euler;
}
case EULER_ORDER_XZY: {
case EulerOrder::EULER_ORDER_XZY: {
// Euler angles in XZY convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -499,8 +510,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
Vector3 euler;
real_t sz = rows[0][1];
if (sz < (1.0f - (real_t)CMP_EPSILON)) {
if (sz > -(1.0f - (real_t)CMP_EPSILON)) {
if (sz < (1.0f - epsilon)) {
if (sz > -(1.0f - epsilon)) {
euler.x = Math::atan2(rows[2][1], rows[1][1]);
euler.y = Math::atan2(rows[0][2], rows[0][0]);
euler.z = Math::asin(-sz);
@@ -518,7 +529,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
}
return euler;
}
case EULER_ORDER_YXZ: {
case EulerOrder::EULER_ORDER_YXZ: {
// Euler angles in YXZ convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -530,8 +541,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
real_t m12 = rows[1][2];
if (m12 < (1 - (real_t)CMP_EPSILON)) {
if (m12 > -(1 - (real_t)CMP_EPSILON)) {
if (m12 < (1 - epsilon)) {
if (m12 > -(1 - epsilon)) {
// is this a pure X rotation?
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[0][2] == 0 && rows[2][0] == 0 && rows[0][0] == 1) {
// return the simplest form (human friendlier in editor and scripts)
@@ -556,7 +567,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
return euler;
}
case EULER_ORDER_YZX: {
case EulerOrder::EULER_ORDER_YZX: {
// Euler angles in YZX convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -566,8 +577,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
Vector3 euler;
real_t sz = rows[1][0];
if (sz < (1.0f - (real_t)CMP_EPSILON)) {
if (sz > -(1.0f - (real_t)CMP_EPSILON)) {
if (sz < (1.0f - epsilon)) {
if (sz > -(1.0f - epsilon)) {
euler.x = Math::atan2(-rows[1][2], rows[1][1]);
euler.y = Math::atan2(-rows[2][0], rows[0][0]);
euler.z = Math::asin(sz);
@@ -584,8 +595,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
euler.z = Math_PI / 2.0f;
}
return euler;
}
case EULER_ORDER_ZXY: {
} break;
case EulerOrder::EULER_ORDER_ZXY: {
// Euler angles in ZXY convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -594,8 +605,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
// -cx*sy sx cx*cy
Vector3 euler;
real_t sx = rows[2][1];
if (sx < (1.0f - (real_t)CMP_EPSILON)) {
if (sx > -(1.0f - (real_t)CMP_EPSILON)) {
if (sx < (1.0f - epsilon)) {
if (sx > -(1.0f - epsilon)) {
euler.x = Math::asin(sx);
euler.y = Math::atan2(-rows[2][0], rows[2][2]);
euler.z = Math::atan2(-rows[0][1], rows[1][1]);
@@ -612,8 +623,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
euler.z = 0;
}
return euler;
}
case EULER_ORDER_ZYX: {
} break;
case EulerOrder::EULER_ORDER_ZYX: {
// Euler angles in ZYX convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -622,8 +633,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
// -sy cy*sx cy*cx
Vector3 euler;
real_t sy = rows[2][0];
if (sy < (1.0f - (real_t)CMP_EPSILON)) {
if (sy > -(1.0f - (real_t)CMP_EPSILON)) {
if (sy < (1.0f - epsilon)) {
if (sy > -(1.0f - epsilon)) {
euler.x = Math::atan2(rows[2][1], rows[2][2]);
euler.y = Math::asin(-sy);
euler.z = Math::atan2(rows[1][0], rows[0][0]);
@@ -664,26 +675,26 @@ void Basis::set_euler(const Vector3 &p_euler, EulerOrder p_order) {
Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1);
switch (p_order) {
case EULER_ORDER_XYZ: {
case EulerOrder::EULER_ORDER_XYZ: {
*this = xmat * (ymat * zmat);
} break;
case EULER_ORDER_XZY: {
case EulerOrder::EULER_ORDER_XZY: {
*this = xmat * zmat * ymat;
} break;
case EULER_ORDER_YXZ: {
case EulerOrder::EULER_ORDER_YXZ: {
*this = ymat * xmat * zmat;
} break;
case EULER_ORDER_YZX: {
case EulerOrder::EULER_ORDER_YZX: {
*this = ymat * zmat * xmat;
} break;
case EULER_ORDER_ZXY: {
case EulerOrder::EULER_ORDER_ZXY: {
*this = zmat * xmat * ymat;
} break;
case EULER_ORDER_ZYX: {
case EulerOrder::EULER_ORDER_ZYX: {
*this = zmat * ymat * xmat;
} break;
default: {
ERR_FAIL_MSG("Invalid order parameter for set_euler(vec3,order)");
ERR_FAIL_MSG("Invalid Euler order parameter.");
}
}
}
@@ -720,7 +731,7 @@ Basis::operator String() const {
Quaternion Basis::get_quaternion() const {
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(!is_rotation(), Quaternion(), "Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quaternion() or call orthonormalized() if the Basis contains linearly independent vectors.");
ERR_FAIL_COND_V_MSG(!is_rotation(), Quaternion(), "Basis " + operator String() + " must be normalized in order to be casted to a Quaternion. Use get_rotation_quaternion() or call orthonormalized() if the Basis contains linearly independent vectors.");
#endif
/* Allow getting a quaternion from an unnormalized transform */
Basis m = *this;
@@ -828,8 +839,8 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
z = (rows[1][0] - rows[0][1]) / s;
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));
// acos does clamping.
r_angle = Math::acos((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2);
}
void Basis::set_quaternion(const Quaternion &p_quaternion) {
@@ -847,7 +858,7 @@ void Basis::set_quaternion(const Quaternion &p_quaternion) {
void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_angle) {
// Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle
#ifdef MATH_CHECKS
ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized.");
ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 " + p_axis.operator String() + " must be normalized.");
#endif
Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
real_t cosine = Math::cos(p_angle);
@@ -905,7 +916,7 @@ void Basis::_set_diagonal(const Vector3 &p_diag) {
rows[2][2] = p_diag.z;
}
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.rows[0] = rows[0].lerp(p_to.rows[0], p_weight);
b.rows[1] = rows[1].lerp(p_to.rows[1], p_weight);
@@ -914,7 +925,7 @@ Basis Basis::lerp(const Basis &p_to, const real_t &p_weight) const {
return b;
}
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
Quaternion from(*this);
Quaternion to(p_to);
@@ -1047,9 +1058,10 @@ Basis Basis::looking_at(const Vector3 &p_target, const Vector3 &p_up, bool p_use
v_z = -v_z;
}
Vector3 v_x = p_up.cross(v_z);
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(v_x.is_zero_approx(), Basis(), "The target vector and up vector can't be parallel to each other.");
#endif
if (v_x.is_zero_approx()) {
WARN_PRINT("Target and up vectors are colinear. This is not advised as it may cause unwanted rotation around local Z axis.");
v_x = p_up.get_any_perpendicular(); // Vectors are almost parallel.
}
v_x.normalize();
Vector3 v_y = v_z.cross(v_x);

View File

@@ -29,6 +29,7 @@
/**************************************************************************/
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/templates/hash_map.hpp>
#include <godot_cpp/variant/color.hpp>
#include <godot_cpp/variant/color_names.inc.hpp>
#include <godot_cpp/variant/string.hpp>
@@ -131,20 +132,20 @@ String _to_hex(float p_val) {
String Color::to_html(bool p_alpha) const {
String txt;
txt = txt + _to_hex(r);
txt = txt + _to_hex(g);
txt = txt + _to_hex(b);
txt += _to_hex(r);
txt += _to_hex(g);
txt += _to_hex(b);
if (p_alpha) {
txt = txt + _to_hex(a);
txt += _to_hex(a);
}
return txt;
}
float Color::get_h() const {
float min = Math::min(r, g);
min = Math::min(min, b);
float max = Math::max(r, g);
max = Math::max(max, b);
float min = MIN(r, g);
min = MIN(min, b);
float max = MAX(r, g);
max = MAX(max, b);
float delta = max - min;
@@ -170,10 +171,10 @@ float Color::get_h() const {
}
float Color::get_s() const {
float min = Math::min(r, g);
min = Math::min(min, b);
float max = Math::max(r, g);
max = Math::max(max, b);
float min = MIN(r, g);
min = MIN(min, b);
float max = MAX(r, g);
max = MAX(max, b);
float delta = max - min;
@@ -181,8 +182,8 @@ float Color::get_s() const {
}
float Color::get_v() const {
float max = Math::max(r, g);
max = Math::max(max, b);
float max = MAX(r, g);
max = MAX(max, b);
return max;
}
@@ -385,7 +386,6 @@ Color Color::named(const String &p_name) {
int idx = find_named_color(p_name);
if (idx == -1) {
ERR_FAIL_V_MSG(Color(), "Invalid color name: " + p_name + ".");
return Color();
}
return named_colors[idx].color;
}
@@ -400,7 +400,7 @@ Color Color::named(const String &p_name, const Color &p_default) {
int Color::find_named_color(const String &p_name) {
String name = p_name;
// Normalize name
// Normalize name.
name = name.replace(" ", "");
name = name.replace("-", "");
name = name.replace("_", "");
@@ -408,23 +408,24 @@ int Color::find_named_color(const String &p_name) {
name = name.replace(".", "");
name = name.to_upper();
int idx = 0;
while (named_colors[idx].name != nullptr) {
if (name == String(named_colors[idx].name).replace("_", "")) {
return idx;
static HashMap<String, int> named_colors_hashmap;
if (unlikely(named_colors_hashmap.is_empty())) {
const int named_color_count = get_named_color_count();
for (int i = 0; i < named_color_count; i++) {
named_colors_hashmap[String(named_colors[i].name).replace("_", "")] = i;
}
idx++;
}
const HashMap<String, int>::ConstIterator E = named_colors_hashmap.find(name);
if (E) {
return E->value;
}
return -1;
}
int Color::get_named_color_count() {
int idx = 0;
while (named_colors[idx].name != nullptr) {
idx++;
}
return idx;
return sizeof(named_colors) / sizeof(NamedColor);
}
String Color::get_named_color_name(int p_idx) {
@@ -467,6 +468,10 @@ Color Color::from_rgbe9995(uint32_t p_rgbe) {
return Color(rd, gd, bd, 1.0f);
}
Color Color::from_rgba8(int64_t p_r8, int64_t p_g8, int64_t p_b8, int64_t p_a8) {
return Color(p_r8 / 255.0f, p_g8 / 255.0f, p_b8 / 255.0f, p_a8 / 255.0f);
}
Color::operator String() const {
return "(" + String::num(r, 4) + ", " + String::num(g, 4) + ", " + String::num(b, 4) + ", " + String::num(a, 4) + ")";
}

View File

@@ -60,7 +60,7 @@ Vector3 Plane::get_any_perpendicular_normal() const {
static const Vector3 p2 = Vector3(0, 1, 0);
Vector3 p;
if (Math::abs(normal.dot(p1)) > 0.99f) { // if too similar to p1
if (ABS(normal.dot(p1)) > 0.99f) { // if too similar to p1
p = p2; // use p2
} else {
p = p1; // use p1
@@ -100,13 +100,11 @@ bool Plane::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3
Vector3 segment = p_dir;
real_t den = normal.dot(segment);
//printf("den is %i\n",den);
if (Math::is_zero_approx(den)) {
return false;
}
real_t dist = (normal.dot(p_from) - d) / den;
//printf("dist is %i\n",dist);
if (dist > (real_t)CMP_EPSILON) { //this is a ray, before the emitting pos (p_from) doesn't exist
@@ -123,13 +121,11 @@ bool Plane::intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vec
Vector3 segment = p_begin - p_end;
real_t den = normal.dot(segment);
//printf("den is %i\n",den);
if (Math::is_zero_approx(den)) {
return false;
}
real_t dist = (normal.dot(p_begin) - d) / den;
//printf("dist is %i\n",dist);
if (dist < (real_t)-CMP_EPSILON || dist > (1.0f + (real_t)CMP_EPSILON)) {
return false;

View File

@@ -39,7 +39,7 @@
namespace godot {
float Projection::determinant() const {
real_t Projection::determinant() const {
return columns[0][3] * columns[1][2] * columns[2][1] * columns[3][0] - columns[0][2] * columns[1][3] * columns[2][1] * columns[3][0] -
columns[0][3] * columns[1][1] * columns[2][2] * columns[3][0] + columns[0][1] * columns[1][3] * columns[2][2] * columns[3][0] +
columns[0][2] * columns[1][1] * columns[2][3] * columns[3][0] - columns[0][1] * columns[1][2] * columns[2][3] * columns[3][0] -
@@ -171,7 +171,7 @@ Projection Projection::perspective_znear_adjusted(real_t p_new_znear) const {
}
Plane Projection::get_projection_plane(Planes p_plane) const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
switch (p_plane) {
case PLANE_NEAR: {
@@ -404,20 +404,19 @@ void Projection::set_frustum(real_t p_size, real_t p_aspect, Vector2 p_offset, r
}
real_t Projection::get_z_far() const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
Plane new_plane = Plane(matrix[3] - matrix[2],
matrix[7] - matrix[6],
matrix[11] - matrix[10],
matrix[15] - matrix[14]);
new_plane.normal = -new_plane.normal;
new_plane.normalize();
return new_plane.d;
}
real_t Projection::get_z_near() const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
Plane new_plane = Plane(matrix[3] + matrix[2],
matrix[7] + matrix[6],
matrix[11] + matrix[10],
@@ -428,7 +427,7 @@ real_t Projection::get_z_near() const {
}
Vector2 Projection::get_viewport_half_extents() const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
///////--- Near Plane ---///////
Plane near_plane = Plane(matrix[3] + matrix[2],
matrix[7] + matrix[6],
@@ -456,7 +455,7 @@ Vector2 Projection::get_viewport_half_extents() const {
}
Vector2 Projection::get_far_plane_half_extents() const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
///////--- Far Plane ---///////
Plane far_plane = Plane(matrix[3] - matrix[2],
matrix[7] - matrix[6],
@@ -484,7 +483,7 @@ Vector2 Projection::get_far_plane_half_extents() const {
}
bool Projection::get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const {
Array planes = get_projection_planes(Transform3D());
Vector<Plane> planes = get_projection_planes(Transform3D());
const Planes intersections[8][3] = {
{ PLANE_FAR, PLANE_LEFT, PLANE_TOP },
{ PLANE_FAR, PLANE_LEFT, PLANE_BOTTOM },
@@ -509,17 +508,17 @@ bool Projection::get_endpoints(const Transform3D &p_transform, Vector3 *p_8point
return true;
}
Array Projection::get_projection_planes(const Transform3D &p_transform) const {
Vector<Plane> Projection::get_projection_planes(const Transform3D &p_transform) const {
/** Fast Plane Extraction from combined modelview/projection matrices.
* References:
* https://web.archive.org/web/20011221205252/https://www.markmorley.com/opengl/frustumculling.html
* https://web.archive.org/web/20061020020112/https://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
*/
Array planes;
Vector<Plane> planes;
planes.resize(6);
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
Plane new_plane;
@@ -532,7 +531,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[0] = p_transform.xform(new_plane);
planes.write[0] = p_transform.xform(new_plane);
///////--- Far Plane ---///////
new_plane = Plane(matrix[3] - matrix[2],
@@ -543,7 +542,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[1] = p_transform.xform(new_plane);
planes.write[1] = p_transform.xform(new_plane);
///////--- Left Plane ---///////
new_plane = Plane(matrix[3] + matrix[0],
@@ -554,7 +553,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[2] = p_transform.xform(new_plane);
planes.write[2] = p_transform.xform(new_plane);
///////--- Top Plane ---///////
new_plane = Plane(matrix[3] - matrix[1],
@@ -565,7 +564,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[3] = p_transform.xform(new_plane);
planes.write[3] = p_transform.xform(new_plane);
///////--- Right Plane ---///////
new_plane = Plane(matrix[3] - matrix[0],
@@ -576,7 +575,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[4] = p_transform.xform(new_plane);
planes.write[4] = p_transform.xform(new_plane);
///////--- Bottom Plane ---///////
new_plane = Plane(matrix[3] + matrix[1],
@@ -587,7 +586,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[5] = p_transform.xform(new_plane);
planes.write[5] = p_transform.xform(new_plane);
return planes;
}
@@ -599,101 +598,229 @@ Projection Projection::inverse() const {
}
void Projection::invert() {
int i, j, k;
int pvt_i[4], pvt_j[4]; /* Locations of pivot matrix */
real_t pvt_val; /* Value of current pivot element */
real_t hold; /* Temporary storage */
real_t determinant = 1.0f;
for (k = 0; k < 4; k++) {
/** Locate k'th pivot element **/
pvt_val = columns[k][k]; /** Initialize for search **/
pvt_i[k] = k;
pvt_j[k] = k;
for (i = k; i < 4; i++) {
for (j = k; j < 4; j++) {
if (Math::abs(columns[i][j]) > Math::abs(pvt_val)) {
pvt_i[k] = i;
pvt_j[k] = j;
pvt_val = columns[i][j];
}
}
}
// Adapted from Mesa's `src/util/u_math.c` `util_invert_mat4x4`.
// MIT licensed. Copyright 2008 VMware, Inc. Authored by Jacques Leroy.
Projection temp;
real_t *out = (real_t *)temp.columns;
real_t *m = (real_t *)columns;
/** Product of pivots, gives determinant when finished **/
determinant *= pvt_val;
if (Math::is_zero_approx(determinant)) {
return; /** Matrix is singular (zero determinant). **/
}
real_t wtmp[4][8];
real_t m0, m1, m2, m3, s;
real_t *r0, *r1, *r2, *r3;
/** "Interchange" rows (with sign change stuff) **/
i = pvt_i[k];
if (i != k) { /** If rows are different **/
for (j = 0; j < 4; j++) {
hold = -columns[k][j];
columns[k][j] = columns[i][j];
columns[i][j] = hold;
}
}
#define MAT(m, r, c) (m)[(c) * 4 + (r)]
/** "Interchange" columns **/
j = pvt_j[k];
if (j != k) { /** If columns are different **/
for (i = 0; i < 4; i++) {
hold = -columns[i][k];
columns[i][k] = columns[i][j];
columns[i][j] = hold;
}
}
r0 = wtmp[0];
r1 = wtmp[1];
r2 = wtmp[2];
r3 = wtmp[3];
/** Divide column by minus pivot value **/
for (i = 0; i < 4; i++) {
if (i != k) {
columns[i][k] /= (-pvt_val);
}
}
r0[0] = MAT(m, 0, 0);
r0[1] = MAT(m, 0, 1);
r0[2] = MAT(m, 0, 2);
r0[3] = MAT(m, 0, 3);
r0[4] = 1.0;
r0[5] = 0.0;
r0[6] = 0.0;
r0[7] = 0.0;
/** Reduce the matrix **/
for (i = 0; i < 4; i++) {
hold = columns[i][k];
for (j = 0; j < 4; j++) {
if (i != k && j != k) {
columns[i][j] += hold * columns[k][j];
}
}
}
r1[0] = MAT(m, 1, 0);
r1[1] = MAT(m, 1, 1);
r1[2] = MAT(m, 1, 2);
r1[3] = MAT(m, 1, 3);
r1[5] = 1.0;
r1[4] = 0.0;
r1[6] = 0.0;
r1[7] = 0.0;
/** Divide row by pivot **/
for (j = 0; j < 4; j++) {
if (j != k) {
columns[k][j] /= pvt_val;
}
}
r2[0] = MAT(m, 2, 0);
r2[1] = MAT(m, 2, 1);
r2[2] = MAT(m, 2, 2);
r2[3] = MAT(m, 2, 3);
r2[6] = 1.0;
r2[4] = 0.0;
r2[5] = 0.0;
r2[7] = 0.0;
/** Replace pivot by reciprocal (at last we can touch it). **/
columns[k][k] = 1.0 / pvt_val;
r3[0] = MAT(m, 3, 0);
r3[1] = MAT(m, 3, 1);
r3[2] = MAT(m, 3, 2);
r3[3] = MAT(m, 3, 3);
r3[7] = 1.0;
r3[4] = 0.0;
r3[5] = 0.0;
r3[6] = 0.0;
/* choose pivot - or die */
if (Math::abs(r3[0]) > Math::abs(r2[0])) {
SWAP(r3, r2);
}
if (Math::abs(r2[0]) > Math::abs(r1[0])) {
SWAP(r2, r1);
}
if (Math::abs(r1[0]) > Math::abs(r0[0])) {
SWAP(r1, r0);
}
ERR_FAIL_COND(0.0 == r0[0]);
/* eliminate first variable */
m1 = r1[0] / r0[0];
m2 = r2[0] / r0[0];
m3 = r3[0] / r0[0];
s = r0[1];
r1[1] -= m1 * s;
r2[1] -= m2 * s;
r3[1] -= m3 * s;
s = r0[2];
r1[2] -= m1 * s;
r2[2] -= m2 * s;
r3[2] -= m3 * s;
s = r0[3];
r1[3] -= m1 * s;
r2[3] -= m2 * s;
r3[3] -= m3 * s;
s = r0[4];
if (s != 0.0) {
r1[4] -= m1 * s;
r2[4] -= m2 * s;
r3[4] -= m3 * s;
}
s = r0[5];
if (s != 0.0) {
r1[5] -= m1 * s;
r2[5] -= m2 * s;
r3[5] -= m3 * s;
}
s = r0[6];
if (s != 0.0) {
r1[6] -= m1 * s;
r2[6] -= m2 * s;
r3[6] -= m3 * s;
}
s = r0[7];
if (s != 0.0) {
r1[7] -= m1 * s;
r2[7] -= m2 * s;
r3[7] -= m3 * s;
}
/* That was most of the work, one final pass of row/column interchange */
/* to finish */
for (k = 4 - 2; k >= 0; k--) { /* Don't need to work with 1 by 1 corner*/
i = pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */
if (i != k) { /* If rows are different */
for (j = 0; j < 4; j++) {
hold = columns[k][j];
columns[k][j] = -columns[i][j];
columns[i][j] = hold;
}
}
j = pvt_i[k]; /* Columns to swap correspond to pivot ROW */
if (j != k) { /* If columns are different */
for (i = 0; i < 4; i++) {
hold = columns[i][k];
columns[i][k] = -columns[i][j];
columns[i][j] = hold;
}
}
/* choose pivot - or die */
if (Math::abs(r3[1]) > Math::abs(r2[1])) {
SWAP(r3, r2);
}
if (Math::abs(r2[1]) > Math::abs(r1[1])) {
SWAP(r2, r1);
}
ERR_FAIL_COND(0.0 == r1[1]);
/* eliminate second variable */
m2 = r2[1] / r1[1];
m3 = r3[1] / r1[1];
r2[2] -= m2 * r1[2];
r3[2] -= m3 * r1[2];
r2[3] -= m2 * r1[3];
r3[3] -= m3 * r1[3];
s = r1[4];
if (0.0 != s) {
r2[4] -= m2 * s;
r3[4] -= m3 * s;
}
s = r1[5];
if (0.0 != s) {
r2[5] -= m2 * s;
r3[5] -= m3 * s;
}
s = r1[6];
if (0.0 != s) {
r2[6] -= m2 * s;
r3[6] -= m3 * s;
}
s = r1[7];
if (0.0 != s) {
r2[7] -= m2 * s;
r3[7] -= m3 * s;
}
/* choose pivot - or die */
if (Math::abs(r3[2]) > Math::abs(r2[2])) {
SWAP(r3, r2);
}
ERR_FAIL_COND(0.0 == r2[2]);
/* eliminate third variable */
m3 = r3[2] / r2[2];
r3[3] -= m3 * r2[3];
r3[4] -= m3 * r2[4];
r3[5] -= m3 * r2[5];
r3[6] -= m3 * r2[6];
r3[7] -= m3 * r2[7];
/* last check */
ERR_FAIL_COND(0.0 == r3[3]);
s = 1.0 / r3[3]; /* now back substitute row 3 */
r3[4] *= s;
r3[5] *= s;
r3[6] *= s;
r3[7] *= s;
m2 = r2[3]; /* now back substitute row 2 */
s = 1.0 / r2[2];
r2[4] = s * (r2[4] - r3[4] * m2);
r2[5] = s * (r2[5] - r3[5] * m2);
r2[6] = s * (r2[6] - r3[6] * m2);
r2[7] = s * (r2[7] - r3[7] * m2);
m1 = r1[3];
r1[4] -= r3[4] * m1;
r1[5] -= r3[5] * m1;
r1[6] -= r3[6] * m1;
r1[7] -= r3[7] * m1;
m0 = r0[3];
r0[4] -= r3[4] * m0;
r0[5] -= r3[5] * m0;
r0[6] -= r3[6] * m0;
r0[7] -= r3[7] * m0;
m1 = r1[2]; /* now back substitute row 1 */
s = 1.0 / r1[1];
r1[4] = s * (r1[4] - r2[4] * m1);
r1[5] = s * (r1[5] - r2[5] * m1),
r1[6] = s * (r1[6] - r2[6] * m1);
r1[7] = s * (r1[7] - r2[7] * m1);
m0 = r0[2];
r0[4] -= r2[4] * m0;
r0[5] -= r2[5] * m0;
r0[6] -= r2[6] * m0;
r0[7] -= r2[7] * m0;
m0 = r0[1]; /* now back substitute row 0 */
s = 1.0 / r0[0];
r0[4] = s * (r0[4] - r1[4] * m0);
r0[5] = s * (r0[5] - r1[5] * m0),
r0[6] = s * (r0[6] - r1[6] * m0);
r0[7] = s * (r0[7] - r1[7] * m0);
MAT(out, 0, 0) = r0[4];
MAT(out, 0, 1) = r0[5];
MAT(out, 0, 2) = r0[6];
MAT(out, 0, 3) = r0[7];
MAT(out, 1, 0) = r1[4];
MAT(out, 1, 1) = r1[5];
MAT(out, 1, 2) = r1[6];
MAT(out, 1, 3) = r1[7];
MAT(out, 2, 0) = r2[4];
MAT(out, 2, 1) = r2[5];
MAT(out, 2, 2) = r2[6];
MAT(out, 2, 3) = r2[7];
MAT(out, 3, 0) = r3[4];
MAT(out, 3, 1) = r3[5];
MAT(out, 3, 2) = r3[6];
MAT(out, 3, 3) = r3[7];
#undef MAT
*this = temp;
}
void Projection::flip_y() {
@@ -722,7 +849,8 @@ Projection Projection::operator*(const Projection &p_matrix) const {
return new_matrix;
}
void Projection::set_depth_correction(bool p_flip_y) {
void Projection::set_depth_correction(bool p_flip_y, bool p_reverse_z, bool p_remap_z) {
// p_remap_z is used to convert from OpenGL-style clip space (-1 - 1) to Vulkan style (0 - 1).
real_t *m = &columns[0][0];
m[0] = 1;
@@ -735,11 +863,11 @@ void Projection::set_depth_correction(bool p_flip_y) {
m[7] = 0.0;
m[8] = 0.0;
m[9] = 0.0;
m[10] = 0.5;
m[10] = p_remap_z ? (p_reverse_z ? -0.5 : 0.5) : (p_reverse_z ? -1.0 : 1.0);
m[11] = 0.0;
m[12] = 0.0;
m[13] = 0.0;
m[14] = 0.5;
m[14] = p_remap_z ? 0.5 : 0.0;
m[15] = 1.0;
}
@@ -786,14 +914,10 @@ void Projection::set_light_atlas_rect(const Rect2 &p_rect) {
}
Projection::operator String() const {
String str;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
str = str + String((j > 0) ? ", " : "\n") + rtos(columns[i][j]);
}
}
return str;
return "[X: " + columns[0].operator String() +
", Y: " + columns[1].operator String() +
", Z: " + columns[2].operator String() +
", W: " + columns[3].operator String() + "]";
}
real_t Projection::get_aspect() const {
@@ -812,7 +936,7 @@ bool Projection::is_orthogonal() const {
}
real_t Projection::get_fov() const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
Plane right_plane = Plane(matrix[3] - matrix[0],
matrix[7] - matrix[4],
@@ -834,13 +958,13 @@ real_t Projection::get_fov() const {
}
}
float Projection::get_lod_multiplier() const {
real_t Projection::get_lod_multiplier() const {
if (is_orthogonal()) {
return get_viewport_half_extents().x;
} else {
float zn = get_z_near();
float width = get_viewport_half_extents().x * 2.0;
return 1.0 / (zn / width);
const real_t zn = get_z_near();
const real_t width = get_viewport_half_extents().x * 2.0f;
return 1.0f / (zn / width);
}
// Usage is lod_size / (lod_distance * multiplier) < threshold

View File

@@ -194,11 +194,11 @@ Quaternion Quaternion::spherical_cubic_interpolate(const Quaternion &p_b, const
post_q = Basis(post_q).get_rotation_quaternion();
// Flip quaternions to shortest path if necessary.
bool flip1 = Math::sign(from_q.dot(pre_q));
bool flip1 = std::signbit(from_q.dot(pre_q));
pre_q = flip1 ? -pre_q : pre_q;
bool flip2 = Math::sign(from_q.dot(to_q));
bool flip2 = std::signbit(from_q.dot(to_q));
to_q = flip2 ? -to_q : to_q;
bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : Math::sign(to_q.dot(post_q));
bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : std::signbit(to_q.dot(post_q));
post_q = flip3 ? -post_q : post_q;
// Calc by Expmap in from_q space.
@@ -245,11 +245,11 @@ Quaternion Quaternion::spherical_cubic_interpolate_in_time(const Quaternion &p_b
post_q = Basis(post_q).get_rotation_quaternion();
// Flip quaternions to shortest path if necessary.
bool flip1 = Math::sign(from_q.dot(pre_q));
bool flip1 = std::signbit(from_q.dot(pre_q));
pre_q = flip1 ? -pre_q : pre_q;
bool flip2 = Math::sign(from_q.dot(to_q));
bool flip2 = std::signbit(from_q.dot(to_q));
to_q = flip2 ? -to_q : to_q;
bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : Math::sign(to_q.dot(post_q));
bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : std::signbit(to_q.dot(post_q));
post_q = flip3 ? -post_q : post_q;
// Calc by Expmap in from_q space.

View File

@@ -211,31 +211,31 @@ next4:
real_t mina = maxa;
real_t dp = p_xform.columns[0].dot(xf_points2[1]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
dp = p_xform.columns[0].dot(xf_points2[2]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
dp = p_xform.columns[0].dot(xf_points2[3]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
real_t maxb = p_xform.columns[0].dot(xf_points[0]);
real_t minb = maxb;
dp = p_xform.columns[0].dot(xf_points[1]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
dp = p_xform.columns[0].dot(xf_points[2]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
dp = p_xform.columns[0].dot(xf_points[3]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
if (mina > maxb) {
return false;
@@ -248,31 +248,31 @@ next4:
mina = maxa;
dp = p_xform.columns[1].dot(xf_points2[1]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
dp = p_xform.columns[1].dot(xf_points2[2]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
dp = p_xform.columns[1].dot(xf_points2[3]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
maxb = p_xform.columns[1].dot(xf_points[0]);
minb = maxb;
dp = p_xform.columns[1].dot(xf_points[1]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
dp = p_xform.columns[1].dot(xf_points[2]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
dp = p_xform.columns[1].dot(xf_points[3]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
if (mina > maxb) {
return false;
@@ -285,7 +285,7 @@ next4:
}
Rect2::operator String() const {
return "[P: " + position.operator String() + ", S: " + size + "]";
return "[P: " + position.operator String() + ", S: " + size.operator String() + "]";
}
Rect2::operator Rect2i() const {

View File

@@ -48,7 +48,7 @@ Transform2D Transform2D::inverse() const {
}
void Transform2D::affine_invert() {
real_t det = basis_determinant();
real_t det = determinant();
#ifdef MATH_CHECKS
ERR_FAIL_COND(det == 0);
#endif
@@ -67,17 +67,17 @@ Transform2D Transform2D::affine_inverse() const {
return inv;
}
void Transform2D::rotate(const real_t p_angle) {
void Transform2D::rotate(real_t p_angle) {
*this = Transform2D(p_angle, Vector2()) * (*this);
}
real_t Transform2D::get_skew() const {
real_t det = basis_determinant();
real_t det = determinant();
return Math::acos(columns[0].normalized().dot(SIGN(det) * columns[1].normalized())) - (real_t)Math_PI * 0.5f;
}
void Transform2D::set_skew(const real_t p_angle) {
real_t det = basis_determinant();
void Transform2D::set_skew(real_t p_angle) {
real_t det = determinant();
columns[1] = SIGN(det) * columns[0].rotated(((real_t)Math_PI * 0.5f + p_angle)).normalized() * columns[1].length();
}
@@ -85,7 +85,7 @@ real_t Transform2D::get_rotation() const {
return Math::atan2(columns[0].y, columns[0].x);
}
void Transform2D::set_rotation(const real_t p_rot) {
void Transform2D::set_rotation(real_t p_rot) {
Size2 scale = get_scale();
real_t cr = Math::cos(p_rot);
real_t sr = Math::sin(p_rot);
@@ -96,7 +96,7 @@ void Transform2D::set_rotation(const real_t p_rot) {
set_scale(scale);
}
Transform2D::Transform2D(const real_t p_rot, const Vector2 &p_pos) {
Transform2D::Transform2D(real_t p_rot, const Vector2 &p_pos) {
real_t cr = Math::cos(p_rot);
real_t sr = Math::sin(p_rot);
columns[0][0] = cr;
@@ -106,7 +106,7 @@ Transform2D::Transform2D(const real_t p_rot, const Vector2 &p_pos) {
columns[2] = p_pos;
}
Transform2D::Transform2D(const real_t p_rot, const Size2 &p_scale, const real_t p_skew, const Vector2 &p_pos) {
Transform2D::Transform2D(real_t p_rot, const Size2 &p_scale, real_t p_skew, const Vector2 &p_pos) {
columns[0][0] = Math::cos(p_rot) * p_scale.x;
columns[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
columns[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;
@@ -115,7 +115,7 @@ Transform2D::Transform2D(const real_t p_rot, const Size2 &p_scale, const real_t
}
Size2 Transform2D::get_scale() const {
real_t det_sign = Math::sign(basis_determinant());
real_t det_sign = SIGN(determinant());
return Size2(columns[0].length(), det_sign * columns[1].length());
}
@@ -138,7 +138,7 @@ void Transform2D::scale_basis(const Size2 &p_scale) {
columns[1][1] *= p_scale.y;
}
void Transform2D::translate_local(const real_t p_tx, const real_t p_ty) {
void Transform2D::translate_local(real_t p_tx, real_t p_ty) {
translate_local(Vector2(p_tx, p_ty));
}
@@ -153,7 +153,7 @@ void Transform2D::orthonormalize() {
Vector2 y = columns[1];
x.normalize();
y = (y - x * (x.dot(y)));
y = y - x * x.dot(y);
y.normalize();
columns[0] = x;
@@ -161,9 +161,21 @@ void Transform2D::orthonormalize() {
}
Transform2D Transform2D::orthonormalized() const {
Transform2D on = *this;
on.orthonormalize();
return on;
Transform2D ortho = *this;
ortho.orthonormalize();
return ortho;
}
bool Transform2D::is_conformal() const {
// Non-flipped case.
if (Math::is_equal_approx(columns[0][0], columns[1][1]) && Math::is_equal_approx(columns[0][1], -columns[1][0])) {
return true;
}
// Flipped case.
if (Math::is_equal_approx(columns[0][0], -columns[1][1]) && Math::is_equal_approx(columns[0][1], columns[1][0])) {
return true;
}
return false;
}
bool Transform2D::is_equal_approx(const Transform2D &p_transform) const {
@@ -223,12 +235,6 @@ Transform2D Transform2D::operator*(const Transform2D &p_transform) const {
return t;
}
Transform2D Transform2D::basis_scaled(const Size2 &p_scale) const {
Transform2D copy = *this;
copy.scale_basis(p_scale);
return copy;
}
Transform2D Transform2D::scaled(const Size2 &p_scale) const {
// Equivalent to left multiplication
Transform2D copy = *this;
@@ -257,67 +263,52 @@ Transform2D Transform2D::translated_local(const Vector2 &p_offset) const {
return Transform2D(columns[0], columns[1], columns[2] + basis_xform(p_offset));
}
Transform2D Transform2D::rotated(const real_t p_angle) const {
Transform2D Transform2D::rotated(real_t p_angle) const {
// Equivalent to left multiplication
return Transform2D(p_angle, Vector2()) * (*this);
}
Transform2D Transform2D::rotated_local(const real_t p_angle) const {
Transform2D Transform2D::rotated_local(real_t p_angle) const {
// Equivalent to right multiplication
return (*this) * Transform2D(p_angle, Vector2()); // Could be optimized, because origin transform can be skipped.
}
real_t Transform2D::basis_determinant() const {
real_t Transform2D::determinant() const {
return columns[0].x * columns[1].y - columns[0].y * columns[1].x;
}
Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, const real_t p_c) const {
//extract parameters
Vector2 p1 = get_origin();
Vector2 p2 = p_transform.get_origin();
real_t r1 = get_rotation();
real_t r2 = p_transform.get_rotation();
Size2 s1 = get_scale();
Size2 s2 = p_transform.get_scale();
//slerp rotation
Vector2 v1(Math::cos(r1), Math::sin(r1));
Vector2 v2(Math::cos(r2), Math::sin(r2));
real_t dot = v1.dot(v2);
dot = Math::clamp(dot, (real_t)-1.0, (real_t)1.0);
Vector2 v;
if (dot > 0.9995f) {
v = v1.lerp(v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues
} else {
real_t angle = p_c * Math::acos(dot);
Vector2 v3 = (v2 - v1 * dot).normalized();
v = v1 * Math::cos(angle) + v3 * Math::sin(angle);
}
//construct matrix
Transform2D res(v.angle(), p1.lerp(p2, p_c));
res.scale_basis(s1.lerp(s2, p_c));
return res;
Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t p_weight) const {
return Transform2D(
Math::lerp_angle(get_rotation(), p_transform.get_rotation(), p_weight),
get_scale().lerp(p_transform.get_scale(), p_weight),
Math::lerp_angle(get_skew(), p_transform.get_skew(), p_weight),
get_origin().lerp(p_transform.get_origin(), p_weight));
}
void Transform2D::operator*=(const real_t p_val) {
void Transform2D::operator*=(real_t p_val) {
columns[0] *= p_val;
columns[1] *= p_val;
columns[2] *= p_val;
}
Transform2D Transform2D::operator*(const real_t p_val) const {
Transform2D Transform2D::operator*(real_t p_val) const {
Transform2D ret(*this);
ret *= p_val;
return ret;
}
void Transform2D::operator/=(real_t p_val) {
columns[0] /= p_val;
columns[1] /= p_val;
columns[2] /= p_val;
}
Transform2D Transform2D::operator/(real_t p_val) const {
Transform2D ret(*this);
ret /= p_val;
return ret;
}
Transform2D::operator String() const {
return "[X: " + columns[0].operator String() +
", Y: " + columns[1].operator String() +

View File

@@ -78,20 +78,20 @@ void Transform3D::rotate_basis(const Vector3 &p_axis, real_t p_angle) {
basis.rotate(p_axis, p_angle);
}
Transform3D Transform3D::looking_at(const Vector3 &p_target, const Vector3 &p_up) const {
Transform3D Transform3D::looking_at(const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) const {
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(origin.is_equal_approx(p_target), Transform3D(), "The transform's origin and target can't be equal.");
#endif
Transform3D t = *this;
t.basis = Basis::looking_at(p_target - origin, p_up);
t.basis = Basis::looking_at(p_target - origin, p_up, p_use_model_front);
return t;
}
void Transform3D::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up) {
void Transform3D::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) {
#ifdef MATH_CHECKS
ERR_FAIL_COND_MSG(p_eye.is_equal_approx(p_target), "The eye and target vectors can't be equal.");
#endif
basis = Basis::looking_at(p_target - p_eye, p_up);
basis = Basis::looking_at(p_target - p_eye, p_up, p_use_model_front);
origin = p_eye;
}
@@ -198,17 +198,28 @@ Transform3D Transform3D::operator*(const Transform3D &p_transform) const {
return t;
}
void Transform3D::operator*=(const real_t p_val) {
void Transform3D::operator*=(real_t p_val) {
origin *= p_val;
basis *= p_val;
}
Transform3D Transform3D::operator*(const real_t p_val) const {
Transform3D Transform3D::operator*(real_t p_val) const {
Transform3D ret(*this);
ret *= p_val;
return ret;
}
void Transform3D::operator/=(real_t p_val) {
basis /= p_val;
origin /= p_val;
}
Transform3D Transform3D::operator/(real_t p_val) const {
Transform3D ret(*this);
ret /= p_val;
return ret;
}
Transform3D::operator String() const {
return "[X: " + basis.get_column(0).operator String() +
", Y: " + basis.get_column(1).operator String() +
@@ -228,9 +239,9 @@ Transform3D::Transform3D(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &
basis.set_column(2, p_z);
}
Transform3D::Transform3D(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, real_t ox, real_t oy, real_t oz) {
basis = Basis(xx, xy, xz, yx, yy, yz, zx, zy, zz);
origin = Vector3(ox, oy, oz);
Transform3D::Transform3D(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, real_t p_ox, real_t p_oy, real_t p_oz) {
basis = Basis(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz);
origin = Vector3(p_ox, p_oy, p_oz);
}
} // namespace godot

View File

@@ -39,7 +39,7 @@ real_t Vector2::angle() const {
return Math::atan2(y, x);
}
Vector2 Vector2::from_angle(const real_t p_angle) {
Vector2 Vector2::from_angle(real_t p_angle) {
return Vector2(Math::cos(p_angle), Math::sin(p_angle));
}
@@ -111,7 +111,7 @@ Vector2 Vector2::round() const {
return Vector2(Math::round(x), Math::round(y));
}
Vector2 Vector2::rotated(const real_t p_by) const {
Vector2 Vector2::rotated(real_t p_by) const {
real_t sine = Math::sin(p_by);
real_t cosi = Math::cos(p_by);
return Vector2(
@@ -119,7 +119,7 @@ Vector2 Vector2::rotated(const real_t p_by) const {
x * sine + y * cosi);
}
Vector2 Vector2::posmod(const real_t p_mod) const {
Vector2 Vector2::posmod(real_t p_mod) const {
return Vector2(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod));
}
@@ -155,7 +155,7 @@ Vector2 Vector2::snappedf(real_t p_step) const {
Math::snapped(y, p_step));
}
Vector2 Vector2::limit_length(const real_t p_len) const {
Vector2 Vector2::limit_length(real_t p_len) const {
const real_t l = length();
Vector2 v = *this;
if (l > 0 && p_len < l) {
@@ -166,7 +166,7 @@ Vector2 Vector2::limit_length(const real_t p_len) const {
return v;
}
Vector2 Vector2::move_toward(const Vector2 &p_to, const real_t p_delta) const {
Vector2 Vector2::move_toward(const Vector2 &p_to, real_t p_delta) const {
Vector2 v = *this;
Vector2 vd = p_to - v;
real_t len = vd.length();
@@ -176,9 +176,9 @@ Vector2 Vector2::move_toward(const Vector2 &p_to, const real_t p_delta) const {
// slide returns the component of the vector along the given plane, specified by its normal vector.
Vector2 Vector2::slide(const Vector2 &p_normal) const {
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized.");
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 " + p_normal.operator String() + "must be normalized.");
#endif
return *this - p_normal * this->dot(p_normal);
return *this - p_normal * dot(p_normal);
}
Vector2 Vector2::bounce(const Vector2 &p_normal) const {
@@ -187,9 +187,9 @@ Vector2 Vector2::bounce(const Vector2 &p_normal) const {
Vector2 Vector2::reflect(const Vector2 &p_normal) const {
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized.");
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 " + p_normal.operator String() + "must be normalized.");
#endif
return 2.0f * p_normal * this->dot(p_normal) - *this;
return 2.0f * p_normal * dot(p_normal) - *this;
}
bool Vector2::is_equal_approx(const Vector2 &p_v) const {
@@ -205,7 +205,7 @@ bool Vector2::is_finite() const {
}
Vector2::operator String() const {
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ")";
return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ")";
}
Vector2::operator Vector2i() const {

View File

@@ -35,18 +35,6 @@
namespace godot {
Vector2i Vector2i::snapped(const Vector2i &p_step) const {
return Vector2i(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y));
}
Vector2i Vector2i::snappedi(int32_t p_step) const {
return Vector2i(
Math::snapped(x, p_step),
Math::snapped(y, p_step));
}
Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const {
return Vector2i(
CLAMP(x, p_min.x, p_max.x),
@@ -59,6 +47,18 @@ Vector2i Vector2i::clampi(int32_t p_min, int32_t p_max) const {
CLAMP(y, p_min, p_max));
}
Vector2i Vector2i::snapped(const Vector2i &p_step) const {
return Vector2i(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y));
}
Vector2i Vector2i::snappedi(int32_t p_step) const {
return Vector2i(
Math::snapped(x, p_step),
Math::snapped(y, p_step));
}
int64_t Vector2i::length_squared() const {
return x * (int64_t)x + y * (int64_t)y;
}
@@ -67,14 +67,6 @@ double Vector2i::length() const {
return Math::sqrt((double)length_squared());
}
int64_t Vector2i::distance_squared_to(const Vector2i &p_to) const {
return (p_to - *this).length_squared();
}
double Vector2i::distance_to(const Vector2i &p_to) const {
return (p_to - *this).length();
}
Vector2i Vector2i::operator+(const Vector2i &p_v) const {
return Vector2i(x + p_v.x, y + p_v.y);
}
@@ -97,39 +89,39 @@ Vector2i Vector2i::operator*(const Vector2i &p_v1) const {
return Vector2i(x * p_v1.x, y * p_v1.y);
}
Vector2i Vector2i::operator*(const int32_t &rvalue) const {
return Vector2i(x * rvalue, y * rvalue);
Vector2i Vector2i::operator*(int32_t p_rvalue) const {
return Vector2i(x * p_rvalue, y * p_rvalue);
}
void Vector2i::operator*=(const int32_t &rvalue) {
x *= rvalue;
y *= rvalue;
void Vector2i::operator*=(int32_t p_rvalue) {
x *= p_rvalue;
y *= p_rvalue;
}
Vector2i Vector2i::operator/(const Vector2i &p_v1) const {
return Vector2i(x / p_v1.x, y / p_v1.y);
}
Vector2i Vector2i::operator/(const int32_t &rvalue) const {
return Vector2i(x / rvalue, y / rvalue);
Vector2i Vector2i::operator/(int32_t p_rvalue) const {
return Vector2i(x / p_rvalue, y / p_rvalue);
}
void Vector2i::operator/=(const int32_t &rvalue) {
x /= rvalue;
y /= rvalue;
void Vector2i::operator/=(int32_t p_rvalue) {
x /= p_rvalue;
y /= p_rvalue;
}
Vector2i Vector2i::operator%(const Vector2i &p_v1) const {
return Vector2i(x % p_v1.x, y % p_v1.y);
}
Vector2i Vector2i::operator%(const int32_t &rvalue) const {
return Vector2i(x % rvalue, y % rvalue);
Vector2i Vector2i::operator%(int32_t p_rvalue) const {
return Vector2i(x % p_rvalue, y % p_rvalue);
}
void Vector2i::operator%=(const int32_t &rvalue) {
x %= rvalue;
y %= rvalue;
void Vector2i::operator%=(int32_t p_rvalue) {
x %= p_rvalue;
y %= p_rvalue;
}
Vector2i Vector2i::operator-() const {

View File

@@ -37,11 +37,11 @@
namespace godot {
void Vector3::rotate(const Vector3 &p_axis, const real_t p_angle) {
void Vector3::rotate(const Vector3 &p_axis, real_t p_angle) {
*this = Basis(p_axis, p_angle).xform(*this);
}
Vector3 Vector3::rotated(const Vector3 &p_axis, const real_t p_angle) const {
Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_angle) const {
Vector3 r = *this;
r.rotate(p_axis, p_angle);
return r;
@@ -61,31 +61,31 @@ Vector3 Vector3::clampf(real_t p_min, real_t p_max) const {
CLAMP(z, p_min, p_max));
}
void Vector3::snap(const Vector3 p_step) {
void Vector3::snap(const Vector3 &p_step) {
x = Math::snapped(x, p_step.x);
y = Math::snapped(y, p_step.y);
z = Math::snapped(z, p_step.z);
}
Vector3 Vector3::snapped(const Vector3 &p_step) const {
Vector3 v = *this;
v.snap(p_step);
return v;
}
void Vector3::snapf(real_t p_step) {
x = Math::snapped(x, p_step);
y = Math::snapped(y, p_step);
z = Math::snapped(z, p_step);
}
Vector3 Vector3::snapped(const Vector3 p_step) const {
Vector3 v = *this;
v.snap(p_step);
return v;
}
Vector3 Vector3::snappedf(real_t p_step) const {
Vector3 v = *this;
v.snapf(p_step);
return v;
}
Vector3 Vector3::limit_length(const real_t p_len) const {
Vector3 Vector3::limit_length(real_t p_len) const {
const real_t l = length();
Vector3 v = *this;
if (l > 0 && p_len < l) {
@@ -96,7 +96,7 @@ Vector3 Vector3::limit_length(const real_t p_len) const {
return v;
}
Vector3 Vector3::move_toward(const Vector3 &p_to, const real_t p_delta) const {
Vector3 Vector3::move_toward(const Vector3 &p_to, real_t p_delta) const {
Vector3 v = *this;
Vector3 vd = p_to - v;
real_t len = vd.length();
@@ -122,23 +122,25 @@ Vector2 Vector3::octahedron_encode() const {
Vector3 Vector3::octahedron_decode(const Vector2 &p_oct) {
Vector2 f(p_oct.x * 2.0f - 1.0f, p_oct.y * 2.0f - 1.0f);
Vector3 n(f.x, f.y, 1.0f - Math::abs(f.x) - Math::abs(f.y));
float t = CLAMP(-n.z, 0.0f, 1.0f);
const real_t t = CLAMP(-n.z, 0.0f, 1.0f);
n.x += n.x >= 0 ? -t : t;
n.y += n.y >= 0 ? -t : t;
return n.normalized();
}
Vector2 Vector3::octahedron_tangent_encode(const float sign) const {
Vector2 res = this->octahedron_encode();
Vector2 Vector3::octahedron_tangent_encode(float p_sign) const {
const real_t bias = 1.0f / (real_t)32767.0f;
Vector2 res = octahedron_encode();
res.y = MAX(res.y, bias);
res.y = res.y * 0.5f + 0.5f;
res.y = sign >= 0.0f ? res.y : 1 - res.y;
res.y = p_sign >= 0.0f ? res.y : 1 - res.y;
return res;
}
Vector3 Vector3::octahedron_tangent_decode(const Vector2 &p_oct, float *sign) {
Vector3 Vector3::octahedron_tangent_decode(const Vector2 &p_oct, float *r_sign) {
Vector2 oct_compressed = p_oct;
oct_compressed.y = oct_compressed.y * 2 - 1;
*sign = oct_compressed.y >= 0.0f ? 1.0f : -1.0f;
*r_sign = oct_compressed.y >= 0.0f ? 1.0f : -1.0f;
oct_compressed.y = Math::abs(oct_compressed.y);
Vector3 res = Vector3::octahedron_decode(oct_compressed);
return res;
@@ -165,7 +167,7 @@ bool Vector3::is_finite() const {
}
Vector3::operator String() const {
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ")";
return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ", " + String::num_real(z, true) + ")";
}
Vector3::operator Vector3i() const {

View File

@@ -35,20 +35,6 @@
namespace godot {
Vector3i Vector3i::snapped(const Vector3i &p_step) const {
return Vector3i(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y),
Math::snapped(z, p_step.z));
}
Vector3i Vector3i::snappedi(int32_t p_step) const {
return Vector3i(
Math::snapped(x, p_step),
Math::snapped(y, p_step),
Math::snapped(z, p_step));
}
Vector3i::Axis Vector3i::min_axis_index() const {
return x < y ? (x < z ? Vector3i::AXIS_X : Vector3i::AXIS_Z) : (y < z ? Vector3i::AXIS_Y : Vector3i::AXIS_Z);
}
@@ -71,6 +57,20 @@ Vector3i Vector3i::clampi(int32_t p_min, int32_t p_max) const {
CLAMP(z, p_min, p_max));
}
Vector3i Vector3i::snapped(const Vector3i &p_step) const {
return Vector3i(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y),
Math::snapped(z, p_step.z));
}
Vector3i Vector3i::snappedi(int32_t p_step) const {
return Vector3i(
Math::snapped(x, p_step),
Math::snapped(y, p_step),
Math::snapped(z, p_step));
}
Vector3i::operator String() const {
return "(" + itos(x) + ", " + itos(y) + ", " + itos(z) + ")";
}

View File

@@ -117,7 +117,7 @@ Vector4 Vector4::abs() const {
}
Vector4 Vector4::sign() const {
return Vector4(Math::sign(x), Math::sign(y), Math::sign(z), Math::sign(w));
return Vector4(SIGN(x), SIGN(y), SIGN(z), SIGN(w));
}
Vector4 Vector4::floor() const {
@@ -132,15 +132,16 @@ Vector4 Vector4::round() const {
return Vector4(Math::round(x), Math::round(y), Math::round(z), Math::round(w));
}
Vector4 Vector4::lerp(const Vector4 &p_to, const real_t p_weight) const {
return Vector4(
x + (p_weight * (p_to.x - x)),
y + (p_weight * (p_to.y - y)),
z + (p_weight * (p_to.z - z)),
w + (p_weight * (p_to.w - w)));
Vector4 Vector4::lerp(const Vector4 &p_to, real_t p_weight) const {
Vector4 res = *this;
res.x = Math::lerp(res.x, p_to.x, p_weight);
res.y = Math::lerp(res.y, p_to.y, p_weight);
res.z = Math::lerp(res.z, p_to.z, p_weight);
res.w = Math::lerp(res.w, p_to.w, p_weight);
return res;
}
Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const {
Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, real_t p_weight) const {
Vector4 res = *this;
res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
@@ -149,7 +150,7 @@ Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, c
return res;
}
Vector4 Vector4::cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const {
Vector4 Vector4::cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const {
Vector4 res = *this;
res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
@@ -158,7 +159,7 @@ Vector4 Vector4::cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_
return res;
}
Vector4 Vector4::posmod(const real_t p_mod) const {
Vector4 Vector4::posmod(real_t p_mod) const {
return Vector4(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod), Math::fposmod(w, p_mod));
}
@@ -213,9 +214,13 @@ Vector4 Vector4::clampf(real_t p_min, real_t p_max) const {
}
Vector4::operator String() const {
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")";
return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ", " + String::num_real(z, true) + ", " + String::num_real(w, true) + ")";
}
static_assert(sizeof(Vector4) == 4 * sizeof(real_t));
Vector4::operator Vector4i() const {
return Vector4i(x, y, z, w);
}
} // namespace godot

View File

@@ -35,22 +35,6 @@
namespace godot {
Vector4i Vector4i::snapped(const Vector4i &p_step) const {
return Vector4i(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y),
Math::snapped(z, p_step.z),
Math::snapped(w, p_step.w));
}
Vector4i Vector4i::snappedi(int32_t p_step) const {
return Vector4i(
Math::snapped(x, p_step),
Math::snapped(y, p_step),
Math::snapped(z, p_step),
Math::snapped(w, p_step));
}
Vector4i::Axis Vector4i::min_axis_index() const {
uint32_t min_index = 0;
int32_t min_value = x;
@@ -91,6 +75,22 @@ Vector4i Vector4i::clampi(int32_t p_min, int32_t p_max) const {
CLAMP(w, p_min, p_max));
}
Vector4i Vector4i::snapped(const Vector4i &p_step) const {
return Vector4i(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y),
Math::snapped(z, p_step.z),
Math::snapped(w, p_step.w));
}
Vector4i Vector4i::snappedi(int32_t p_step) const {
return Vector4i(
Math::snapped(x, p_step),
Math::snapped(y, p_step),
Math::snapped(z, p_step),
Math::snapped(w, p_step));
}
Vector4i::operator String() const {
return "(" + itos(x) + ", " + itos(y) + ", " + itos(z) + ", " + itos(w) + ")";
}