Work on Vector3.

This commit is contained in:
Relintai 2022-03-14 12:44:44 +01:00
parent 084762a516
commit c41714b837
3 changed files with 145 additions and 99 deletions

View File

@ -38,7 +38,7 @@ struct Vector2i;
static const int VECTOR2_AXIS_COUNT = 2; static const int VECTOR2_AXIS_COUNT = 2;
enum Vector2Axis { enum Vector2Axis {
VECTOR2_AXIS_X, VECTOR2_AXIS_X = 0,
VECTOR2_AXIS_Y, VECTOR2_AXIS_Y,
}; };

View File

@ -30,8 +30,9 @@
#include "vector3.h" #include "vector3.h"
#include "core/math/basis.h" //#include "core/math/basis.h"
/*
void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) { void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) {
*this = Basis(p_axis, p_phi).xform(*this); *this = Basis(p_axis, p_phi).xform(*this);
} }
@ -154,3 +155,4 @@ bool Vector3::is_equal_approx(const Vector3 &p_v) const {
Vector3::operator String() const { Vector3::operator String() const {
return (rtos(x) + ", " + rtos(y) + ", " + rtos(z)); return (rtos(x) + ", " + rtos(y) + ", " + rtos(z));
} }
*/

View File

@ -32,19 +32,19 @@
#define VECTOR3_H #define VECTOR3_H
#include "core/math/math_funcs.h" #include "core/math/math_funcs.h"
#include "core/ustring.h" //#include "core/ustring.h"
class Basis; struct Basis;
struct _NO_DISCARD_CLASS_ Vector3 { static const int VECTOR3_AXIS_COUNT = 3;
static const int AXIS_COUNT = 3;
enum Axis { enum Vector3Axis {
AXIS_X, VECTOR3_AXIS_X = 0,
AXIS_Y, VECTOR3_AXIS_Y,
AXIS_Z, VECTOR3_AXIS_Z,
}; };
typedef struct _NO_DISCARD_CLASS_ Vector3 {
union { union {
struct { struct {
real_t x; real_t x;
@ -54,32 +54,68 @@ struct _NO_DISCARD_CLASS_ Vector3 {
real_t coord[3]; real_t coord[3];
}; };
} Vector3;
_FORCE_INLINE_ const real_t &operator[](int p_axis) const { extern _FORCE_INLINE_ void vector3_set_axis(Vector3 *v, int p_axis, real_t p_value) {
DEV_ASSERT((unsigned int)p_axis < 3); v->coord[p_axis] = p_value;
return coord[p_axis];
} }
extern _FORCE_INLINE_ const real_t vector3_get_axis(const Vector3 *v, int p_axis) {
//DEV_ASSERT((unsigned int)p_axis < 3);
return v->coord[p_axis];
}
/*
_FORCE_INLINE_ real_t &operator[](int p_axis) { _FORCE_INLINE_ real_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 3); DEV_ASSERT((unsigned int)p_axis < 3);
return coord[p_axis]; return coord[p_axis];
} }
*/
void set_axis(int p_axis, real_t p_value); extern _FORCE_INLINE_ int vector3_min_axis(const Vector3 *v) {
real_t get_axis(int p_axis) const; return v->x < v->y ? (v->x < v->z ? 0 : 2) : (v->y < v->z ? 1 : 2);
_FORCE_INLINE_ void set_all(real_t p_value) {
x = y = z = p_value;
} }
_FORCE_INLINE_ int min_axis() const { extern _FORCE_INLINE_ int vector3_max_axis(const Vector3 *v) {
return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2); return v->x < v->y ? (v->y < v->z ? 2 : 1) : (v->x < v->z ? 2 : 0);
} }
_FORCE_INLINE_ int max_axis() const { extern _FORCE_INLINE_ void vector3_set_all(Vector3 *self, real_t p_value) {
return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0); self->x = self->y = self->z = p_value;
} }
extern _FORCE_INLINE_ void vector3_set(Vector3 *self, real_t p_value_x, real_t p_value_y, real_t p_value_z) {
self->x = p_value_x;
self->y = p_value_y;
self->z = p_value_z;
}
extern _FORCE_INLINE_ Vector3 vector3_create(real_t p_value_x, real_t p_value_y, real_t p_value_z) {
Vector3 v;
v.x = p_value_x;
v.y = p_value_y;
v.z = p_value_z;
return v;
}
extern _FORCE_INLINE_ Vector3 vector3_createv(const Vector3 *other) {
Vector3 v;
v.x = other->x;
v.y = other->y;
v.z = other->z;
return v;
}
/*
_FORCE_INLINE_ real_t length() const; _FORCE_INLINE_ real_t length() const;
_FORCE_INLINE_ real_t length_squared() const; _FORCE_INLINE_ real_t length_squared() const;
@ -97,7 +133,7 @@ struct _NO_DISCARD_CLASS_ Vector3 {
void rotate(const Vector3 &p_axis, real_t p_phi); void rotate(const Vector3 &p_axis, real_t p_phi);
Vector3 rotated(const Vector3 &p_axis, real_t p_phi) const; Vector3 rotated(const Vector3 &p_axis, real_t p_phi) const;
/* Static Methods between 2 vector3s */ // Static Methods between 2 vector3s
_FORCE_INLINE_ Vector3 linear_interpolate(const Vector3 &p_to, real_t p_weight) const; _FORCE_INLINE_ Vector3 linear_interpolate(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 slerp(const Vector3 &p_to, real_t p_weight) const;
@ -134,7 +170,7 @@ struct _NO_DISCARD_CLASS_ Vector3 {
bool is_equal_approx(const Vector3 &p_v) const; bool is_equal_approx(const Vector3 &p_v) const;
inline bool is_equal_approx(const Vector3 &p_v, real_t p_tolerance) const; inline bool is_equal_approx(const Vector3 &p_v, real_t p_tolerance) const;
/* Operators */ // Operators
_FORCE_INLINE_ Vector3 &operator+=(const Vector3 &p_v); _FORCE_INLINE_ Vector3 &operator+=(const Vector3 &p_v);
_FORCE_INLINE_ Vector3 operator+(const Vector3 &p_v) const; _FORCE_INLINE_ Vector3 operator+(const Vector3 &p_v) const;
@ -166,9 +202,15 @@ struct _NO_DISCARD_CLASS_ Vector3 {
y = p_y; y = p_y;
z = p_z; z = p_z;
} }
_FORCE_INLINE_ Vector3() { x = y = z = 0; } _FORCE_INLINE_ Vector3() {
}; x = y = z = 0;
}
*/
//----
/*
Vector3 Vector3::cross(const Vector3 &p_b) const { Vector3 Vector3::cross(const Vector3 &p_b) const {
Vector3 ret( Vector3 ret(
(y * p_b.z) - (z * p_b.y), (y * p_b.z) - (z * p_b.y),
@ -251,7 +293,7 @@ Vector3 Vector3::direction_to(const Vector3 &p_to) const {
return ret; return ret;
} }
/* Operators */ // Operators
Vector3 &Vector3::operator+=(const Vector3 &p_v) { Vector3 &Vector3::operator+=(const Vector3 &p_v) {
x += p_v.x; x += p_v.x;
@ -459,4 +501,6 @@ bool Vector3::is_equal_approx(const Vector3 &p_v, real_t p_tolerance) const {
return Math::is_equal_approx(x, p_v.x, p_tolerance) && Math::is_equal_approx(y, p_v.y, p_tolerance) && Math::is_equal_approx(z, p_v.z, p_tolerance); return Math::is_equal_approx(x, p_v.x, p_tolerance) && Math::is_equal_approx(y, p_v.y, p_tolerance) && Math::is_equal_approx(z, p_v.z, p_tolerance);
} }
*/
#endif // VECTOR3_H #endif // VECTOR3_H