Make Basis look column-major while retaining a row-major representation
Per https://github.com/godotengine/godot/issues/14553: Godot stores Basis in row-major layout for more change for efficiently taking advantage of SIMD instructions, but in scripts Basis looks like and is accessible in a column-major format. This change modifies the C++ binding so that from the script's perspective Basis does look like if it was column-major while retaining a row-major in-memory representation. This is achieved using a set of helper template classes which allow accessing individual columns whose components are non-continues in memory as if it was a Vector3 type. This ensures script interface compatibility without needing to transpose the Basis every time it is passed over the script-engine boundary. Also made most of the Vector2 and Vector3 class interfaces inlined in the process for increased performance. While unrelated (but didn't want to file a separate PR for it), this change adds the necessary flags to have debug symbol information under MSVC. Fixes #241.
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
#ifndef VECTOR3_H
|
||||
#define VECTOR3_H
|
||||
|
||||
#include <gdnative/vector3.h>
|
||||
|
||||
#include "Defs.hpp"
|
||||
|
||||
#include "String.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Basis;
|
||||
@@ -24,80 +28,192 @@ struct Vector3 {
|
||||
real_t z;
|
||||
};
|
||||
|
||||
real_t coord[3];
|
||||
real_t coord[3]; // Not for direct access, use [] operator instead
|
||||
};
|
||||
|
||||
Vector3(real_t x, real_t y, real_t z);
|
||||
inline Vector3(real_t x, real_t y, real_t z) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
Vector3();
|
||||
inline Vector3() {
|
||||
this->x = 0;
|
||||
this->y = 0;
|
||||
this->z = 0;
|
||||
}
|
||||
|
||||
const real_t &operator[](int p_axis) const;
|
||||
inline const real_t &operator[](int p_axis) const {
|
||||
return coord[p_axis];
|
||||
}
|
||||
|
||||
real_t &operator[](int p_axis);
|
||||
inline real_t &operator[](int p_axis) {
|
||||
return coord[p_axis];
|
||||
}
|
||||
|
||||
Vector3 &operator+=(const Vector3 &p_v);
|
||||
inline Vector3 &operator+=(const Vector3 &p_v) {
|
||||
x += p_v.x;
|
||||
y += p_v.y;
|
||||
z += p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 operator+(const Vector3 &p_v) const;
|
||||
inline Vector3 operator+(const Vector3 &p_v) const {
|
||||
Vector3 v = *this;
|
||||
v += p_v;
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector3 &operator-=(const Vector3 &p_v);
|
||||
inline Vector3 &operator-=(const Vector3 &p_v) {
|
||||
x -= p_v.x;
|
||||
y -= p_v.y;
|
||||
z -= p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 operator-(const Vector3 &p_v) const;
|
||||
inline Vector3 operator-(const Vector3 &p_v) const {
|
||||
Vector3 v = *this;
|
||||
v -= p_v;
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector3 &operator*=(const Vector3 &p_v);
|
||||
inline Vector3 &operator*=(const Vector3 &p_v) {
|
||||
x *= p_v.x;
|
||||
y *= p_v.y;
|
||||
z *= p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 operator*(const Vector3 &p_v) const;
|
||||
inline Vector3 operator*(const Vector3 &p_v) const {
|
||||
Vector3 v = *this;
|
||||
v *= p_v;
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector3 &operator/=(const Vector3 &p_v);
|
||||
inline Vector3 &operator/=(const Vector3 &p_v) {
|
||||
x /= p_v.x;
|
||||
y /= p_v.y;
|
||||
z /= p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 operator/(const Vector3 &p_v) const;
|
||||
inline Vector3 operator/(const Vector3 &p_v) const {
|
||||
Vector3 v = *this;
|
||||
v /= p_v;
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector3 &operator*=(real_t p_scalar);
|
||||
inline Vector3 &operator*=(real_t p_scalar) {
|
||||
*this *= Vector3(p_scalar, p_scalar, p_scalar);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 operator*(real_t p_scalar) const;
|
||||
inline Vector3 operator*(real_t p_scalar) const {
|
||||
Vector3 v = *this;
|
||||
v *= p_scalar;
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector3 &operator/=(real_t p_scalar);
|
||||
inline Vector3 &operator/=(real_t p_scalar) {
|
||||
*this /= Vector3(p_scalar, p_scalar, p_scalar);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 operator/(real_t p_scalar) const;
|
||||
inline Vector3 operator/(real_t p_scalar) const {
|
||||
Vector3 v = *this;
|
||||
v /= p_scalar;
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector3 operator-() const;
|
||||
inline Vector3 operator-() const {
|
||||
return Vector3(-x, -y, -z);
|
||||
}
|
||||
|
||||
bool operator==(const Vector3 &p_v) const;
|
||||
inline bool operator==(const Vector3 &p_v) const {
|
||||
return (x == p_v.x && y == p_v.y && z == p_v.z);
|
||||
}
|
||||
|
||||
bool operator!=(const Vector3 &p_v) const;
|
||||
inline bool operator!=(const Vector3 &p_v) const {
|
||||
return (x != p_v.x || y != p_v.y || z != p_v.z);
|
||||
}
|
||||
|
||||
bool operator<(const Vector3 &p_v) const;
|
||||
|
||||
bool operator<=(const Vector3 &p_v) const;
|
||||
|
||||
Vector3 abs() const;
|
||||
inline Vector3 abs() const {
|
||||
return Vector3(::fabs(x), ::fabs(y), ::fabs(z));
|
||||
}
|
||||
|
||||
Vector3 ceil() const;
|
||||
inline Vector3 ceil() const {
|
||||
return Vector3(::ceil(x), ::ceil(y), ::ceil(z));
|
||||
}
|
||||
|
||||
Vector3 cross(const Vector3 &b) const;
|
||||
inline Vector3 cross(const Vector3 &b) const {
|
||||
Vector3 ret(
|
||||
(y * b.z) - (z * b.y),
|
||||
(z * b.x) - (x * b.z),
|
||||
(x * b.y) - (y * b.x));
|
||||
|
||||
Vector3 linear_interpolate(const Vector3 &p_b, real_t p_t) const;
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline Vector3 linear_interpolate(const Vector3 &p_b, real_t p_t) const {
|
||||
return Vector3(
|
||||
x + (p_t * (p_b.x - x)),
|
||||
y + (p_t * (p_b.y - y)),
|
||||
z + (p_t * (p_b.z - z)));
|
||||
}
|
||||
|
||||
Vector3 cubic_interpolate(const Vector3 &b, const Vector3 &pre_a, const Vector3 &post_b, const real_t t) const;
|
||||
|
||||
Vector3 bounce(const Vector3 &p_normal) const;
|
||||
Vector3 bounce(const Vector3 &p_normal) const {
|
||||
return -reflect(p_normal);
|
||||
}
|
||||
|
||||
real_t length() const;
|
||||
inline real_t length() const {
|
||||
real_t x2 = x * x;
|
||||
real_t y2 = y * y;
|
||||
real_t z2 = z * z;
|
||||
|
||||
real_t length_squared() const;
|
||||
return ::sqrt(x2 + y2 + z2);
|
||||
}
|
||||
|
||||
real_t distance_squared_to(const Vector3 &b) const;
|
||||
inline real_t length_squared() const {
|
||||
real_t x2 = x * x;
|
||||
real_t y2 = y * y;
|
||||
real_t z2 = z * z;
|
||||
|
||||
real_t distance_to(const Vector3 &b) const;
|
||||
return x2 + y2 + z2;
|
||||
}
|
||||
|
||||
real_t dot(const Vector3 &b) const;
|
||||
inline real_t distance_squared_to(const Vector3 &b) const {
|
||||
return (b - *this).length_squared();
|
||||
}
|
||||
|
||||
real_t angle_to(const Vector3 &b) const;
|
||||
inline real_t distance_to(const Vector3 &b) const {
|
||||
return (b - *this).length();
|
||||
}
|
||||
|
||||
Vector3 floor() const;
|
||||
inline real_t dot(const Vector3 &b) const {
|
||||
return x * b.x + y * b.y + z * b.z;
|
||||
}
|
||||
|
||||
Vector3 inverse() const;
|
||||
inline real_t angle_to(const Vector3 &b) const {
|
||||
return std::atan2(cross(b).length(), dot(b));
|
||||
}
|
||||
|
||||
bool is_normalized() const;
|
||||
inline Vector3 floor() const {
|
||||
return Vector3(::floor(x), ::floor(y), ::floor(z));
|
||||
}
|
||||
|
||||
inline Vector3 inverse() const {
|
||||
return Vector3(1.f / x, 1.f / y, 1.f / z);
|
||||
}
|
||||
|
||||
inline bool is_normalized() const {
|
||||
return std::abs(length_squared() - 1.f) < 0.00001f;
|
||||
}
|
||||
|
||||
Basis outer(const Vector3 &b) const;
|
||||
|
||||
@@ -105,21 +221,46 @@ struct Vector3 {
|
||||
|
||||
int min_axis() const;
|
||||
|
||||
void normalize();
|
||||
inline void normalize() {
|
||||
real_t l = length();
|
||||
if (l == 0) {
|
||||
x = y = z = 0;
|
||||
} else {
|
||||
x /= l;
|
||||
y /= l;
|
||||
z /= l;
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 normalized() const;
|
||||
inline Vector3 normalized() const {
|
||||
Vector3 v = *this;
|
||||
v.normalize();
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector3 reflect(const Vector3 &by) const;
|
||||
inline Vector3 reflect(const Vector3 &by) const {
|
||||
return by - *this * this->dot(by) * 2.f;
|
||||
}
|
||||
|
||||
Vector3 rotated(const Vector3 &axis, const real_t phi) const;
|
||||
inline Vector3 rotated(const Vector3 &axis, const real_t phi) const {
|
||||
Vector3 v = *this;
|
||||
v.rotate(axis, phi);
|
||||
return v;
|
||||
}
|
||||
|
||||
void rotate(const Vector3 &p_axis, real_t p_phi);
|
||||
|
||||
Vector3 slide(const Vector3 &by) const;
|
||||
inline Vector3 slide(const Vector3 &by) const {
|
||||
return by - *this * this->dot(by);
|
||||
}
|
||||
|
||||
void snap(real_t p_val);
|
||||
|
||||
Vector3 snapped(const float by);
|
||||
inline Vector3 snapped(const float by) {
|
||||
Vector3 v = *this;
|
||||
v.snap(by);
|
||||
return v;
|
||||
}
|
||||
|
||||
operator String() const;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user