
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.
86 lines
1.7 KiB
C++
86 lines
1.7 KiB
C++
#include "Vector3.hpp"
|
|
|
|
#include "String.hpp"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "Basis.hpp"
|
|
|
|
namespace godot {
|
|
|
|
bool Vector3::operator<(const Vector3 &p_v) const {
|
|
if (x == p_v.x) {
|
|
if (y == p_v.y)
|
|
return z < p_v.z;
|
|
else
|
|
return y < p_v.y;
|
|
} else {
|
|
return x < p_v.x;
|
|
}
|
|
}
|
|
|
|
bool Vector3::operator<=(const Vector3 &p_v) const {
|
|
if (x == p_v.x) {
|
|
if (y == p_v.y)
|
|
return z <= p_v.z;
|
|
else
|
|
return y < p_v.y;
|
|
} else {
|
|
return x < p_v.x;
|
|
}
|
|
}
|
|
|
|
Vector3 Vector3::cubic_interpolate(const Vector3 &b, const Vector3 &pre_a, const Vector3 &post_b, const real_t t) const {
|
|
Vector3 p0 = pre_a;
|
|
Vector3 p1 = *this;
|
|
Vector3 p2 = b;
|
|
Vector3 p3 = post_b;
|
|
|
|
real_t t2 = t * t;
|
|
real_t t3 = t2 * t;
|
|
|
|
Vector3 out;
|
|
out = ((p1 * 2.0) +
|
|
(-p0 + p2) * t +
|
|
(p0 * 2.0 - p1 * 5.0 + p2 * 4 - p3) * t2 +
|
|
(-p0 + p1 * 3.0 - p2 * 3.0 + p3) * t3) *
|
|
0.5;
|
|
return out;
|
|
}
|
|
|
|
Basis Vector3::outer(const Vector3 &b) const {
|
|
Vector3 row0(x * b.x, x * b.y, x * b.z);
|
|
Vector3 row1(y * b.x, y * b.y, y * b.z);
|
|
Vector3 row2(z * b.x, z * b.y, z * b.z);
|
|
return Basis(row0, row1, row2);
|
|
}
|
|
|
|
int Vector3::max_axis() const {
|
|
return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
|
|
}
|
|
|
|
int Vector3::min_axis() const {
|
|
return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
|
|
}
|
|
|
|
void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) {
|
|
*this = Basis(p_axis, p_phi).xform(*this);
|
|
}
|
|
|
|
// this is ugly as well, but hey, I'm a simple man
|
|
#define _ugly_stepify(val, step) (step != 0 ? ::floor(val / step + 0.5) * step : val)
|
|
|
|
void Vector3::snap(real_t p_val) {
|
|
x = _ugly_stepify(x, p_val);
|
|
y = _ugly_stepify(y, p_val);
|
|
z = _ugly_stepify(z, p_val);
|
|
}
|
|
|
|
#undef _ugly_stepify
|
|
|
|
Vector3::operator String() const {
|
|
return String::num(x) + ", " + String::num(y) + ", " + String::num(z);
|
|
}
|
|
|
|
} // namespace godot
|