Re-introduce build-in type code for core types
This commit is contained in:
@@ -92,6 +92,23 @@
|
||||
#define unlikely(x) x
|
||||
#endif
|
||||
|
||||
#ifdef REAL_T_IS_DOUBLE
|
||||
typedef double real_t;
|
||||
#else
|
||||
typedef float real_t;
|
||||
#endif
|
||||
|
||||
// Generic swap template.
|
||||
#ifndef SWAP
|
||||
#define SWAP(m_x, m_y) __swap_tmpl((m_x), (m_y))
|
||||
template <class T>
|
||||
inline void __swap_tmpl(T &x, T &y) {
|
||||
T aux = x;
|
||||
x = y;
|
||||
y = aux;
|
||||
}
|
||||
#endif // SWAP
|
||||
|
||||
// Home-made index sequence trick, so it can be used everywhere without the costly include of std::tuple.
|
||||
// https://stackoverflow.com/questions/15014096/c-index-of-type-during-variadic-template-expansion
|
||||
template <size_t... Is>
|
||||
|
||||
424
include/godot_cpp/core/math.hpp
Normal file
424
include/godot_cpp/core/math.hpp
Normal file
@@ -0,0 +1,424 @@
|
||||
#ifndef GODOT_MATH_H
|
||||
#define GODOT_MATH_H
|
||||
|
||||
#include <godot_cpp/core/defs.hpp>
|
||||
|
||||
#include <godot/gdnative_interface.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace godot {
|
||||
namespace Math {
|
||||
|
||||
// This epsilon should match the one used by Godot for consistency.
|
||||
// Using `f` when `real_t` is float.
|
||||
#define CMP_EPSILON 0.00001f
|
||||
#define CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON)
|
||||
|
||||
// This epsilon is for values related to a unit size (scalar or vector len).
|
||||
#ifdef PRECISE_MATH_CHECKS
|
||||
#define UNIT_EPSILON 0.00001
|
||||
#else
|
||||
// Tolerate some more floating point error normally.
|
||||
#define UNIT_EPSILON 0.001
|
||||
#endif
|
||||
|
||||
#define Math_SQRT12 0.7071067811865475244008443621048490
|
||||
#define Math_SQRT2 1.4142135623730950488016887242
|
||||
#define Math_LN2 0.6931471805599453094172321215
|
||||
#define Math_PI 3.1415926535897932384626433833
|
||||
#define Math_TAU 6.2831853071795864769252867666
|
||||
#define Math_E 2.7182818284590452353602874714
|
||||
#define Math_INF INFINITY
|
||||
#define Math_NAN NAN
|
||||
|
||||
// Functions reproduced as in Godot's source code `math_funcs.h`.
|
||||
// Some are overloads to automatically support changing real_t into either double or float in the way Godot does.
|
||||
|
||||
inline double fmod(double p_x, double p_y) {
|
||||
return ::fmod(p_x, p_y);
|
||||
}
|
||||
inline float fmod(float p_x, float p_y) {
|
||||
return ::fmodf(p_x, p_y);
|
||||
}
|
||||
|
||||
inline double fposmod(double p_x, double p_y) {
|
||||
double value = Math::fmod(p_x, p_y);
|
||||
if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
|
||||
value += p_y;
|
||||
}
|
||||
value += 0.0;
|
||||
return value;
|
||||
}
|
||||
inline float fposmod(float p_x, float p_y) {
|
||||
float value = Math::fmod(p_x, p_y);
|
||||
if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
|
||||
value += p_y;
|
||||
}
|
||||
value += 0.0;
|
||||
return value;
|
||||
}
|
||||
|
||||
inline float fposmodp(float p_x, float p_y) {
|
||||
float value = Math::fmod(p_x, p_y);
|
||||
if (value < 0) {
|
||||
value += p_y;
|
||||
}
|
||||
value += 0.0;
|
||||
return value;
|
||||
}
|
||||
inline double fposmodp(double p_x, double p_y) {
|
||||
double value = Math::fmod(p_x, p_y);
|
||||
if (value < 0) {
|
||||
value += p_y;
|
||||
}
|
||||
value += 0.0;
|
||||
return value;
|
||||
}
|
||||
|
||||
inline double floor(double p_x) {
|
||||
return ::floor(p_x);
|
||||
}
|
||||
inline float floor(float p_x) {
|
||||
return ::floorf(p_x);
|
||||
}
|
||||
|
||||
inline double ceil(double p_x) {
|
||||
return ::ceil(p_x);
|
||||
}
|
||||
inline float ceil(float p_x) {
|
||||
return ::ceilf(p_x);
|
||||
}
|
||||
|
||||
inline double exp(double p_x) {
|
||||
return ::exp(p_x);
|
||||
}
|
||||
inline float exp(float p_x) {
|
||||
return ::expf(p_x);
|
||||
}
|
||||
|
||||
inline double sin(double p_x) {
|
||||
return ::sin(p_x);
|
||||
}
|
||||
inline float sin(float p_x) {
|
||||
return ::sinf(p_x);
|
||||
}
|
||||
|
||||
inline double cos(double p_x) {
|
||||
return ::cos(p_x);
|
||||
}
|
||||
inline float cos(float p_x) {
|
||||
return ::cosf(p_x);
|
||||
}
|
||||
|
||||
inline double tan(double p_x) {
|
||||
return ::tan(p_x);
|
||||
}
|
||||
inline float tan(float p_x) {
|
||||
return ::tanf(p_x);
|
||||
}
|
||||
|
||||
inline double sinh(double p_x) {
|
||||
return ::sinh(p_x);
|
||||
}
|
||||
inline float sinh(float p_x) {
|
||||
return ::sinhf(p_x);
|
||||
}
|
||||
|
||||
inline float sinc(float p_x) {
|
||||
return p_x == 0 ? 1 : ::sin(p_x) / p_x;
|
||||
}
|
||||
inline double sinc(double p_x) {
|
||||
return p_x == 0 ? 1 : ::sin(p_x) / p_x;
|
||||
}
|
||||
|
||||
inline float sincn(float p_x) {
|
||||
return sinc(Math_PI * p_x);
|
||||
}
|
||||
inline double sincn(double p_x) {
|
||||
return sinc(Math_PI * p_x);
|
||||
}
|
||||
|
||||
inline double cosh(double p_x) {
|
||||
return ::cosh(p_x);
|
||||
}
|
||||
inline float cosh(float p_x) {
|
||||
return ::coshf(p_x);
|
||||
}
|
||||
|
||||
inline double tanh(double p_x) {
|
||||
return ::tanh(p_x);
|
||||
}
|
||||
inline float tanh(float p_x) {
|
||||
return ::tanhf(p_x);
|
||||
}
|
||||
|
||||
inline double asin(double p_x) {
|
||||
return ::asin(p_x);
|
||||
}
|
||||
inline float asin(float p_x) {
|
||||
return ::asinf(p_x);
|
||||
}
|
||||
|
||||
inline double acos(double p_x) {
|
||||
return ::acos(p_x);
|
||||
}
|
||||
inline float acos(float p_x) {
|
||||
return ::acosf(p_x);
|
||||
}
|
||||
|
||||
inline double atan(double p_x) {
|
||||
return ::atan(p_x);
|
||||
}
|
||||
inline float atan(float p_x) {
|
||||
return ::atanf(p_x);
|
||||
}
|
||||
|
||||
inline double atan2(double p_y, double p_x) {
|
||||
return ::atan2(p_y, p_x);
|
||||
}
|
||||
inline float atan2(float p_y, float p_x) {
|
||||
return ::atan2f(p_y, p_x);
|
||||
}
|
||||
|
||||
inline double sqrt(double p_x) {
|
||||
return ::sqrt(p_x);
|
||||
}
|
||||
inline float sqrt(float p_x) {
|
||||
return ::sqrtf(p_x);
|
||||
}
|
||||
|
||||
inline double pow(double p_x, double p_y) {
|
||||
return ::pow(p_x, p_y);
|
||||
}
|
||||
inline float pow(float p_x, float p_y) {
|
||||
return ::powf(p_x, p_y);
|
||||
}
|
||||
|
||||
inline double log(double p_x) {
|
||||
return ::log(p_x);
|
||||
}
|
||||
inline float log(float p_x) {
|
||||
return ::logf(p_x);
|
||||
}
|
||||
|
||||
inline float lerp(float minv, float maxv, float t) {
|
||||
return minv + t * (maxv - minv);
|
||||
}
|
||||
inline double lerp(double minv, double maxv, double t) {
|
||||
return minv + t * (maxv - minv);
|
||||
}
|
||||
|
||||
inline double lerp_angle(double p_from, double p_to, double p_weight) {
|
||||
double difference = fmod(p_to - p_from, Math_TAU);
|
||||
double distance = fmod(2.0 * difference, Math_TAU) - difference;
|
||||
return p_from + distance * p_weight;
|
||||
}
|
||||
inline float lerp_angle(float p_from, float p_to, float p_weight) {
|
||||
float difference = fmod(p_to - p_from, (float)Math_TAU);
|
||||
float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
|
||||
return p_from + distance * p_weight;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T clamp(T x, T minv, T maxv) {
|
||||
if (x < minv) {
|
||||
return minv;
|
||||
}
|
||||
if (x > maxv) {
|
||||
return maxv;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T min(T a, T b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T max(T a, T b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T sign(T x) {
|
||||
return static_cast<T>(x < 0 ? -1 : 1);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T abs(T x) {
|
||||
return std::abs(x);
|
||||
}
|
||||
|
||||
inline double deg2rad(double p_y) {
|
||||
return p_y * Math_PI / 180.0;
|
||||
}
|
||||
inline float deg2rad(float p_y) {
|
||||
return p_y * static_cast<float>(Math_PI) / 180.f;
|
||||
}
|
||||
|
||||
inline double rad2deg(double p_y) {
|
||||
return p_y * 180.0 / Math_PI;
|
||||
}
|
||||
inline float rad2deg(float p_y) {
|
||||
return p_y * 180.f / static_cast<float>(Math_PI);
|
||||
}
|
||||
|
||||
inline double inverse_lerp(double p_from, double p_to, double p_value) {
|
||||
return (p_value - p_from) / (p_to - p_from);
|
||||
}
|
||||
inline float inverse_lerp(float p_from, float p_to, float p_value) {
|
||||
return (p_value - p_from) / (p_to - p_from);
|
||||
}
|
||||
|
||||
inline double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) {
|
||||
return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
|
||||
}
|
||||
inline float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) {
|
||||
return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
|
||||
}
|
||||
|
||||
inline bool is_equal_approx(real_t a, real_t b) {
|
||||
// Check for exact equality first, required to handle "infinity" values.
|
||||
if (a == b) {
|
||||
return true;
|
||||
}
|
||||
// Then check for approximate equality.
|
||||
real_t tolerance = CMP_EPSILON * std::abs(a);
|
||||
if (tolerance < CMP_EPSILON) {
|
||||
tolerance = CMP_EPSILON;
|
||||
}
|
||||
return std::abs(a - b) < tolerance;
|
||||
}
|
||||
|
||||
inline bool is_equal_approx(real_t a, real_t b, real_t tolerance) {
|
||||
// Check for exact equality first, required to handle "infinity" values.
|
||||
if (a == b) {
|
||||
return true;
|
||||
}
|
||||
// Then check for approximate equality.
|
||||
return std::abs(a - b) < tolerance;
|
||||
}
|
||||
|
||||
inline bool is_zero_approx(real_t s) {
|
||||
return std::abs(s) < CMP_EPSILON;
|
||||
}
|
||||
|
||||
inline double smoothstep(double p_from, double p_to, double p_weight) {
|
||||
if (is_equal_approx(static_cast<real_t>(p_from), static_cast<real_t>(p_to))) {
|
||||
return p_from;
|
||||
}
|
||||
double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0);
|
||||
return x * x * (3.0 - 2.0 * x);
|
||||
}
|
||||
inline float smoothstep(float p_from, float p_to, float p_weight) {
|
||||
if (is_equal_approx(p_from, p_to)) {
|
||||
return p_from;
|
||||
}
|
||||
float x = clamp((p_weight - p_from) / (p_to - p_from), 0.0f, 1.0f);
|
||||
return x * x * (3.0f - 2.0f * x);
|
||||
}
|
||||
|
||||
inline double move_toward(double p_from, double p_to, double p_delta) {
|
||||
return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
|
||||
}
|
||||
|
||||
inline float move_toward(float p_from, float p_to, float p_delta) {
|
||||
return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
|
||||
}
|
||||
|
||||
inline double linear2db(double p_linear) {
|
||||
return log(p_linear) * 8.6858896380650365530225783783321;
|
||||
}
|
||||
inline float linear2db(float p_linear) {
|
||||
return log(p_linear) * 8.6858896380650365530225783783321f;
|
||||
}
|
||||
|
||||
inline double db2linear(double p_db) {
|
||||
return exp(p_db * 0.11512925464970228420089957273422);
|
||||
}
|
||||
inline float db2linear(float p_db) {
|
||||
return exp(p_db * 0.11512925464970228420089957273422f);
|
||||
}
|
||||
|
||||
inline double round(double p_val) {
|
||||
return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
|
||||
}
|
||||
inline float round(float p_val) {
|
||||
return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f);
|
||||
}
|
||||
|
||||
inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
|
||||
int64_t range = max - min;
|
||||
return range == 0 ? min : min + ((((value - min) % range) + range) % range);
|
||||
}
|
||||
|
||||
inline float wrapf(real_t value, real_t min, real_t max) {
|
||||
const real_t range = max - min;
|
||||
return is_zero_approx(range) ? min : value - (range * floor((value - min) / range));
|
||||
}
|
||||
|
||||
inline float stepify(float p_value, float p_step) {
|
||||
if (p_step != 0) {
|
||||
p_value = floor(p_value / p_step + 0.5f) * p_step;
|
||||
}
|
||||
return p_value;
|
||||
}
|
||||
inline double stepify(double p_value, double p_step) {
|
||||
if (p_step != 0) {
|
||||
p_value = floor(p_value / p_step + 0.5) * p_step;
|
||||
}
|
||||
return p_value;
|
||||
}
|
||||
|
||||
inline unsigned int next_power_of_2(unsigned int x) {
|
||||
|
||||
if (x == 0)
|
||||
return 0;
|
||||
|
||||
--x;
|
||||
x |= x >> 1;
|
||||
x |= x >> 2;
|
||||
x |= x >> 4;
|
||||
x |= x >> 8;
|
||||
x |= x >> 16;
|
||||
|
||||
return ++x;
|
||||
}
|
||||
|
||||
// This function should be as fast as possible and rounding mode should not matter.
|
||||
inline int fast_ftoi(float a) {
|
||||
static int b;
|
||||
|
||||
#if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
|
||||
b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5));
|
||||
|
||||
#elif defined(_MSC_VER) && _MSC_VER < 1800
|
||||
__asm fld a __asm fistp b
|
||||
/*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
|
||||
// use AT&T inline assembly style, document that
|
||||
// we use memory as output (=m) and input (m)
|
||||
__asm__ __volatile__ (
|
||||
"flds %1 \n\t"
|
||||
"fistpl %0 \n\t"
|
||||
: "=m" (b)
|
||||
: "m" (a));*/
|
||||
|
||||
#else
|
||||
b = lrintf(a); //assuming everything but msvc 2012 or earlier has lrint
|
||||
#endif
|
||||
return b;
|
||||
}
|
||||
|
||||
inline double snapped(double p_value, double p_step) {
|
||||
if (p_step != 0) {
|
||||
p_value = Math::floor(p_value / p_step + 0.5) * p_step;
|
||||
}
|
||||
return p_value;
|
||||
}
|
||||
|
||||
} // namespace Math
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_MATH_H
|
||||
430
include/godot_cpp/variant/aabb.hpp
Normal file
430
include/godot_cpp/variant/aabb.hpp
Normal file
@@ -0,0 +1,430 @@
|
||||
#ifndef GODOT_AABB_HPP
|
||||
#define GODOT_AABB_HPP
|
||||
|
||||
#include <godot_cpp/core/error_macros.hpp>
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
#include <godot_cpp/variant/plane.hpp>
|
||||
#include <godot_cpp/variant/vector3.hpp>
|
||||
|
||||
/**
|
||||
* AABB / AABB (Axis Aligned Bounding Box)
|
||||
* This is implemented by a point (position) and the box size
|
||||
*/
|
||||
|
||||
namespace godot {
|
||||
|
||||
class AABB {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
Vector3 position;
|
||||
Vector3 size;
|
||||
|
||||
real_t get_area() const; /// get area
|
||||
inline bool has_no_area() const {
|
||||
return (size.x <= 0 || size.y <= 0 || size.z <= 0);
|
||||
}
|
||||
|
||||
inline bool has_no_surface() const {
|
||||
return (size.x <= 0 && size.y <= 0 && size.z <= 0);
|
||||
}
|
||||
|
||||
const Vector3 &get_position() const { return position; }
|
||||
void set_position(const Vector3 &p_pos) { position = p_pos; }
|
||||
const Vector3 &get_size() const { return size; }
|
||||
void set_size(const Vector3 &p_size) { size = p_size; }
|
||||
|
||||
bool operator==(const AABB &p_rval) const;
|
||||
bool operator!=(const AABB &p_rval) const;
|
||||
|
||||
bool is_equal_approx(const AABB &p_aabb) const;
|
||||
inline bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap
|
||||
inline bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap
|
||||
inline bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this
|
||||
|
||||
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;
|
||||
inline bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const;
|
||||
|
||||
inline bool intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const;
|
||||
inline bool inside_convex_shape(const Plane *p_planes, int p_plane_count) const;
|
||||
bool intersects_plane(const Plane &p_plane) const;
|
||||
|
||||
inline bool has_point(const Vector3 &p_point) const;
|
||||
inline Vector3 get_support(const Vector3 &p_normal) const;
|
||||
|
||||
Vector3 get_longest_axis() const;
|
||||
int get_longest_axis_index() const;
|
||||
inline real_t get_longest_axis_size() const;
|
||||
|
||||
Vector3 get_shortest_axis() const;
|
||||
int get_shortest_axis_index() const;
|
||||
inline real_t get_shortest_axis_size() const;
|
||||
|
||||
AABB grow(real_t p_by) const;
|
||||
inline void grow_by(real_t p_amount);
|
||||
|
||||
void get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const;
|
||||
inline Vector3 get_endpoint(int p_point) const;
|
||||
|
||||
AABB expand(const Vector3 &p_vector) const;
|
||||
inline void project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const;
|
||||
inline void expand_to(const Vector3 &p_vector); /** expand to contain a point if necessary */
|
||||
|
||||
inline AABB abs() const {
|
||||
return AABB(Vector3(position.x + Math::min(size.x, (real_t)0), position.y + Math::min(size.y, (real_t)0), position.z + Math::min(size.z, (real_t)0)), size.abs());
|
||||
}
|
||||
|
||||
inline void quantize(real_t p_unit);
|
||||
inline AABB quantized(real_t p_unit) const;
|
||||
|
||||
inline void set_end(const Vector3 &p_end) {
|
||||
size = p_end - position;
|
||||
}
|
||||
|
||||
inline Vector3 get_end() const {
|
||||
return position + size;
|
||||
}
|
||||
|
||||
operator String() const;
|
||||
|
||||
inline AABB() {}
|
||||
inline AABB(const Vector3 &p_pos, const Vector3 &p_size) :
|
||||
position(p_pos),
|
||||
size(p_size) {
|
||||
}
|
||||
};
|
||||
|
||||
inline bool AABB::intersects(const AABB &p_aabb) const {
|
||||
if (position.x >= (p_aabb.position.x + p_aabb.size.x)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.x + size.x) <= p_aabb.position.x) {
|
||||
return false;
|
||||
}
|
||||
if (position.y >= (p_aabb.position.y + p_aabb.size.y)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.y + size.y) <= p_aabb.position.y) {
|
||||
return false;
|
||||
}
|
||||
if (position.z >= (p_aabb.position.z + p_aabb.size.z)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.z + size.z) <= p_aabb.position.z) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool AABB::intersects_inclusive(const AABB &p_aabb) const {
|
||||
if (position.x > (p_aabb.position.x + p_aabb.size.x)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.x + size.x) < p_aabb.position.x) {
|
||||
return false;
|
||||
}
|
||||
if (position.y > (p_aabb.position.y + p_aabb.size.y)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.y + size.y) < p_aabb.position.y) {
|
||||
return false;
|
||||
}
|
||||
if (position.z > (p_aabb.position.z + p_aabb.size.z)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.z + size.z) < p_aabb.position.z) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool AABB::encloses(const AABB &p_aabb) const {
|
||||
Vector3 src_min = position;
|
||||
Vector3 src_max = position + size;
|
||||
Vector3 dst_min = p_aabb.position;
|
||||
Vector3 dst_max = p_aabb.position + p_aabb.size;
|
||||
|
||||
return (
|
||||
(src_min.x <= dst_min.x) &&
|
||||
(src_max.x > dst_max.x) &&
|
||||
(src_min.y <= dst_min.y) &&
|
||||
(src_max.y > dst_max.y) &&
|
||||
(src_min.z <= dst_min.z) &&
|
||||
(src_max.z > dst_max.z));
|
||||
}
|
||||
|
||||
Vector3 AABB::get_support(const Vector3 &p_normal) const {
|
||||
Vector3 half_extents = size * 0.5;
|
||||
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_endpoint(int p_point) const {
|
||||
switch (p_point) {
|
||||
case 0:
|
||||
return Vector3(position.x, position.y, position.z);
|
||||
case 1:
|
||||
return Vector3(position.x, position.y, position.z + size.z);
|
||||
case 2:
|
||||
return Vector3(position.x, position.y + size.y, position.z);
|
||||
case 3:
|
||||
return Vector3(position.x, position.y + size.y, position.z + size.z);
|
||||
case 4:
|
||||
return Vector3(position.x + size.x, position.y, position.z);
|
||||
case 5:
|
||||
return Vector3(position.x + size.x, position.y, position.z + size.z);
|
||||
case 6:
|
||||
return Vector3(position.x + size.x, position.y + size.y, position.z);
|
||||
case 7:
|
||||
return Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
|
||||
}
|
||||
|
||||
ERR_FAIL_V(Vector3());
|
||||
}
|
||||
|
||||
bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const {
|
||||
Vector3 half_extents = size * 0.5;
|
||||
Vector3 ofs = position + half_extents;
|
||||
|
||||
for (int i = 0; i < p_plane_count; i++) {
|
||||
const Plane &p = p_planes[i];
|
||||
Vector3 point(
|
||||
(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);
|
||||
point += ofs;
|
||||
if (p.is_point_over(point)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure all points in the shape aren't fully separated from the AABB on
|
||||
// each axis.
|
||||
int bad_point_counts_positive[3] = { 0 };
|
||||
int bad_point_counts_negative[3] = { 0 };
|
||||
|
||||
for (int k = 0; k < 3; k++) {
|
||||
for (int i = 0; i < p_point_count; i++) {
|
||||
if (p_points[i].coord[k] > ofs.coord[k] + half_extents.coord[k]) {
|
||||
bad_point_counts_positive[k]++;
|
||||
}
|
||||
if (p_points[i].coord[k] < ofs.coord[k] - half_extents.coord[k]) {
|
||||
bad_point_counts_negative[k]++;
|
||||
}
|
||||
}
|
||||
|
||||
if (bad_point_counts_negative[k] == p_point_count) {
|
||||
return false;
|
||||
}
|
||||
if (bad_point_counts_positive[k] == p_point_count) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const {
|
||||
Vector3 half_extents = size * 0.5;
|
||||
Vector3 ofs = position + half_extents;
|
||||
|
||||
for (int i = 0; i < p_plane_count; i++) {
|
||||
const Plane &p = p_planes[i];
|
||||
Vector3 point(
|
||||
(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);
|
||||
point += ofs;
|
||||
if (p.is_point_over(point)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AABB::has_point(const Vector3 &p_point) const {
|
||||
if (p_point.x < position.x) {
|
||||
return false;
|
||||
}
|
||||
if (p_point.y < position.y) {
|
||||
return false;
|
||||
}
|
||||
if (p_point.z < position.z) {
|
||||
return false;
|
||||
}
|
||||
if (p_point.x > position.x + size.x) {
|
||||
return false;
|
||||
}
|
||||
if (p_point.y > position.y + size.y) {
|
||||
return false;
|
||||
}
|
||||
if (p_point.z > position.z + size.z) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void AABB::expand_to(const Vector3 &p_vector) {
|
||||
Vector3 begin = position;
|
||||
Vector3 end = position + size;
|
||||
|
||||
if (p_vector.x < begin.x) {
|
||||
begin.x = p_vector.x;
|
||||
}
|
||||
if (p_vector.y < begin.y) {
|
||||
begin.y = p_vector.y;
|
||||
}
|
||||
if (p_vector.z < begin.z) {
|
||||
begin.z = p_vector.z;
|
||||
}
|
||||
|
||||
if (p_vector.x > end.x) {
|
||||
end.x = p_vector.x;
|
||||
}
|
||||
if (p_vector.y > end.y) {
|
||||
end.y = p_vector.y;
|
||||
}
|
||||
if (p_vector.z > end.z) {
|
||||
end.z = p_vector.z;
|
||||
}
|
||||
|
||||
position = begin;
|
||||
size = end - begin;
|
||||
}
|
||||
|
||||
void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const {
|
||||
Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5);
|
||||
Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z);
|
||||
|
||||
real_t length = p_plane.normal.abs().dot(half_extents);
|
||||
real_t distance = p_plane.distance_to(center);
|
||||
r_min = distance - length;
|
||||
r_max = distance + length;
|
||||
}
|
||||
|
||||
inline real_t AABB::get_longest_axis_size() const {
|
||||
real_t max_size = size.x;
|
||||
|
||||
if (size.y > max_size) {
|
||||
max_size = size.y;
|
||||
}
|
||||
|
||||
if (size.z > max_size) {
|
||||
max_size = size.z;
|
||||
}
|
||||
|
||||
return max_size;
|
||||
}
|
||||
|
||||
inline real_t AABB::get_shortest_axis_size() const {
|
||||
real_t max_size = size.x;
|
||||
|
||||
if (size.y < max_size) {
|
||||
max_size = size.y;
|
||||
}
|
||||
|
||||
if (size.z < max_size) {
|
||||
max_size = size.z;
|
||||
}
|
||||
|
||||
return max_size;
|
||||
}
|
||||
|
||||
bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const {
|
||||
real_t divx = 1.0 / p_dir.x;
|
||||
real_t divy = 1.0 / p_dir.y;
|
||||
real_t divz = 1.0 / p_dir.z;
|
||||
|
||||
Vector3 upbound = position + size;
|
||||
real_t tmin, tmax, tymin, tymax, tzmin, tzmax;
|
||||
if (p_dir.x >= 0) {
|
||||
tmin = (position.x - p_from.x) * divx;
|
||||
tmax = (upbound.x - p_from.x) * divx;
|
||||
} else {
|
||||
tmin = (upbound.x - p_from.x) * divx;
|
||||
tmax = (position.x - p_from.x) * divx;
|
||||
}
|
||||
if (p_dir.y >= 0) {
|
||||
tymin = (position.y - p_from.y) * divy;
|
||||
tymax = (upbound.y - p_from.y) * divy;
|
||||
} else {
|
||||
tymin = (upbound.y - p_from.y) * divy;
|
||||
tymax = (position.y - p_from.y) * divy;
|
||||
}
|
||||
if ((tmin > tymax) || (tymin > tmax)) {
|
||||
return false;
|
||||
}
|
||||
if (tymin > tmin) {
|
||||
tmin = tymin;
|
||||
}
|
||||
if (tymax < tmax) {
|
||||
tmax = tymax;
|
||||
}
|
||||
if (p_dir.z >= 0) {
|
||||
tzmin = (position.z - p_from.z) * divz;
|
||||
tzmax = (upbound.z - p_from.z) * divz;
|
||||
} else {
|
||||
tzmin = (upbound.z - p_from.z) * divz;
|
||||
tzmax = (position.z - p_from.z) * divz;
|
||||
}
|
||||
if ((tmin > tzmax) || (tzmin > tmax)) {
|
||||
return false;
|
||||
}
|
||||
if (tzmin > tmin) {
|
||||
tmin = tzmin;
|
||||
}
|
||||
if (tzmax < tmax) {
|
||||
tmax = tzmax;
|
||||
}
|
||||
return ((tmin < t1) && (tmax > t0));
|
||||
}
|
||||
|
||||
void AABB::grow_by(real_t p_amount) {
|
||||
position.x -= p_amount;
|
||||
position.y -= p_amount;
|
||||
position.z -= p_amount;
|
||||
size.x += 2.0 * p_amount;
|
||||
size.y += 2.0 * p_amount;
|
||||
size.z += 2.0 * p_amount;
|
||||
}
|
||||
|
||||
void AABB::quantize(real_t p_unit) {
|
||||
size += position;
|
||||
|
||||
position.x -= Math::fposmodp(position.x, p_unit);
|
||||
position.y -= Math::fposmodp(position.y, p_unit);
|
||||
position.z -= Math::fposmodp(position.z, p_unit);
|
||||
|
||||
size.x -= Math::fposmodp(size.x, p_unit);
|
||||
size.y -= Math::fposmodp(size.y, p_unit);
|
||||
size.z -= Math::fposmodp(size.z, p_unit);
|
||||
|
||||
size.x += p_unit;
|
||||
size.y += p_unit;
|
||||
size.z += p_unit;
|
||||
|
||||
size -= position;
|
||||
}
|
||||
|
||||
AABB AABB::quantized(real_t p_unit) const {
|
||||
AABB ret = *this;
|
||||
ret.quantize(p_unit);
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_AABB_HPP
|
||||
310
include/godot_cpp/variant/basis.hpp
Normal file
310
include/godot_cpp/variant/basis.hpp
Normal file
@@ -0,0 +1,310 @@
|
||||
#ifndef GODOT_BASIS_HPP
|
||||
#define GODOT_BASIS_HPP
|
||||
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
#include <godot_cpp/variant/quaternion.hpp>
|
||||
#include <godot_cpp/variant/vector3.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Basis {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
Vector3 elements[3] = {
|
||||
Vector3(1, 0, 0),
|
||||
Vector3(0, 1, 0),
|
||||
Vector3(0, 0, 1)
|
||||
};
|
||||
|
||||
inline const Vector3 &operator[](int axis) const {
|
||||
return elements[axis];
|
||||
}
|
||||
inline Vector3 &operator[](int axis) {
|
||||
return elements[axis];
|
||||
}
|
||||
|
||||
void invert();
|
||||
void transpose();
|
||||
|
||||
Basis inverse() const;
|
||||
Basis transposed() const;
|
||||
|
||||
inline real_t determinant() const;
|
||||
|
||||
void from_z(const Vector3 &p_z);
|
||||
|
||||
inline Vector3 get_axis(int p_axis) const {
|
||||
// get actual basis axis (elements is transposed for performance)
|
||||
return Vector3(elements[0][p_axis], elements[1][p_axis], elements[2][p_axis]);
|
||||
}
|
||||
inline void set_axis(int p_axis, const Vector3 &p_value) {
|
||||
// get actual basis axis (elements is transposed for performance)
|
||||
elements[0][p_axis] = p_value.x;
|
||||
elements[1][p_axis] = p_value.y;
|
||||
elements[2][p_axis] = p_value.z;
|
||||
}
|
||||
|
||||
void rotate(const Vector3 &p_axis, real_t p_phi);
|
||||
Basis rotated(const Vector3 &p_axis, real_t p_phi) const;
|
||||
|
||||
void rotate_local(const Vector3 &p_axis, real_t p_phi);
|
||||
Basis rotated_local(const Vector3 &p_axis, real_t p_phi) const;
|
||||
|
||||
void rotate(const Vector3 &p_euler);
|
||||
Basis rotated(const Vector3 &p_euler) const;
|
||||
|
||||
void rotate(const Quaternion &p_quat);
|
||||
Basis rotated(const Quaternion &p_quat) const;
|
||||
|
||||
Vector3 get_rotation_euler() 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_quat() const;
|
||||
Vector3 get_rotation() const { return get_rotation_euler(); };
|
||||
|
||||
Vector3 rotref_posscale_decomposition(Basis &rotref) const;
|
||||
|
||||
Vector3 get_euler_xyz() const;
|
||||
void set_euler_xyz(const Vector3 &p_euler);
|
||||
|
||||
Vector3 get_euler_xzy() const;
|
||||
void set_euler_xzy(const Vector3 &p_euler);
|
||||
|
||||
Vector3 get_euler_yzx() const;
|
||||
void set_euler_yzx(const Vector3 &p_euler);
|
||||
|
||||
Vector3 get_euler_yxz() const;
|
||||
void set_euler_yxz(const Vector3 &p_euler);
|
||||
|
||||
Vector3 get_euler_zxy() const;
|
||||
void set_euler_zxy(const Vector3 &p_euler);
|
||||
|
||||
Vector3 get_euler_zyx() const;
|
||||
void set_euler_zyx(const Vector3 &p_euler);
|
||||
|
||||
Quaternion get_quat() const;
|
||||
void set_quat(const Quaternion &p_quat);
|
||||
|
||||
Vector3 get_euler() const { return get_euler_yxz(); }
|
||||
void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); }
|
||||
|
||||
void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const;
|
||||
void set_axis_angle(const Vector3 &p_axis, real_t p_phi);
|
||||
|
||||
void scale(const Vector3 &p_scale);
|
||||
Basis scaled(const Vector3 &p_scale) const;
|
||||
|
||||
void scale_local(const Vector3 &p_scale);
|
||||
Basis scaled_local(const Vector3 &p_scale) const;
|
||||
|
||||
void make_scale_uniform();
|
||||
float get_uniform_scale() const;
|
||||
|
||||
Vector3 get_scale() const;
|
||||
Vector3 get_scale_abs() const;
|
||||
Vector3 get_scale_local() const;
|
||||
|
||||
void set_axis_angle_scale(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale);
|
||||
void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale);
|
||||
void set_quat_scale(const Quaternion &p_quat, const Vector3 &p_scale);
|
||||
|
||||
// transposed dot products
|
||||
inline real_t tdotx(const Vector3 &v) const {
|
||||
return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2];
|
||||
}
|
||||
inline real_t tdoty(const Vector3 &v) const {
|
||||
return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2];
|
||||
}
|
||||
inline real_t tdotz(const Vector3 &v) const {
|
||||
return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2];
|
||||
}
|
||||
|
||||
bool is_equal_approx(const Basis &p_basis) const;
|
||||
|
||||
bool operator==(const Basis &p_matrix) const;
|
||||
bool operator!=(const Basis &p_matrix) const;
|
||||
|
||||
inline Vector3 xform(const Vector3 &p_vector) const;
|
||||
inline Vector3 xform_inv(const Vector3 &p_vector) const;
|
||||
inline void operator*=(const Basis &p_matrix);
|
||||
inline Basis operator*(const Basis &p_matrix) const;
|
||||
inline void operator+=(const Basis &p_matrix);
|
||||
inline Basis operator+(const Basis &p_matrix) const;
|
||||
inline void operator-=(const Basis &p_matrix);
|
||||
inline Basis operator-(const Basis &p_matrix) const;
|
||||
inline void operator*=(real_t p_val);
|
||||
inline Basis operator*(real_t p_val) const;
|
||||
|
||||
int get_orthogonal_index() const;
|
||||
void set_orthogonal_index(int p_index);
|
||||
|
||||
void set_diagonal(const Vector3 &p_diag);
|
||||
|
||||
bool is_orthogonal() const;
|
||||
bool is_diagonal() const;
|
||||
bool is_rotation() const;
|
||||
|
||||
Basis slerp(const Basis &p_to, const real_t &p_weight) const;
|
||||
void rotate_sh(real_t *p_values);
|
||||
|
||||
operator String() const;
|
||||
|
||||
/* create / set */
|
||||
|
||||
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) {
|
||||
elements[0][0] = xx;
|
||||
elements[0][1] = xy;
|
||||
elements[0][2] = xz;
|
||||
elements[1][0] = yx;
|
||||
elements[1][1] = yy;
|
||||
elements[1][2] = yz;
|
||||
elements[2][0] = zx;
|
||||
elements[2][1] = zy;
|
||||
elements[2][2] = zz;
|
||||
}
|
||||
inline void set(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) {
|
||||
set_axis(0, p_x);
|
||||
set_axis(1, p_y);
|
||||
set_axis(2, p_z);
|
||||
}
|
||||
inline Vector3 get_column(int i) const {
|
||||
return Vector3(elements[0][i], elements[1][i], elements[2][i]);
|
||||
}
|
||||
|
||||
inline Vector3 get_row(int i) const {
|
||||
return Vector3(elements[i][0], elements[i][1], elements[i][2]);
|
||||
}
|
||||
inline Vector3 get_main_diagonal() const {
|
||||
return Vector3(elements[0][0], elements[1][1], elements[2][2]);
|
||||
}
|
||||
|
||||
inline void set_row(int i, const Vector3 &p_row) {
|
||||
elements[i][0] = p_row.x;
|
||||
elements[i][1] = p_row.y;
|
||||
elements[i][2] = p_row.z;
|
||||
}
|
||||
|
||||
inline void set_zero() {
|
||||
elements[0].zero();
|
||||
elements[1].zero();
|
||||
elements[2].zero();
|
||||
}
|
||||
|
||||
inline Basis transpose_xform(const Basis &m) const {
|
||||
return Basis(
|
||||
elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x,
|
||||
elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y,
|
||||
elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z,
|
||||
elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x,
|
||||
elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y,
|
||||
elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z,
|
||||
elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x,
|
||||
elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y,
|
||||
elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * 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);
|
||||
}
|
||||
|
||||
void orthonormalize();
|
||||
Basis orthonormalized() const;
|
||||
|
||||
#ifdef MATH_CHECKS
|
||||
bool is_symmetric() const;
|
||||
#endif
|
||||
Basis diagonalize();
|
||||
|
||||
operator Quaternion() const { return get_quat(); }
|
||||
|
||||
Basis(const Quaternion &p_quat) { set_quat(p_quat); };
|
||||
Basis(const Quaternion &p_quat, const Vector3 &p_scale) { set_quat_scale(p_quat, p_scale); }
|
||||
|
||||
Basis(const Vector3 &p_euler) { set_euler(p_euler); }
|
||||
Basis(const Vector3 &p_euler, const Vector3 &p_scale) { set_euler_scale(p_euler, p_scale); }
|
||||
|
||||
Basis(const Vector3 &p_axis, real_t p_phi) { set_axis_angle(p_axis, p_phi); }
|
||||
Basis(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_phi, p_scale); }
|
||||
|
||||
inline Basis(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2) {
|
||||
elements[0] = row0;
|
||||
elements[1] = row1;
|
||||
elements[2] = row2;
|
||||
}
|
||||
|
||||
inline Basis() {}
|
||||
};
|
||||
|
||||
inline void Basis::operator*=(const Basis &p_matrix) {
|
||||
set(
|
||||
p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
|
||||
p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
|
||||
p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
|
||||
}
|
||||
|
||||
inline Basis Basis::operator*(const Basis &p_matrix) const {
|
||||
return Basis(
|
||||
p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
|
||||
p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
|
||||
p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
|
||||
}
|
||||
|
||||
inline void Basis::operator+=(const Basis &p_matrix) {
|
||||
elements[0] += p_matrix.elements[0];
|
||||
elements[1] += p_matrix.elements[1];
|
||||
elements[2] += p_matrix.elements[2];
|
||||
}
|
||||
|
||||
inline Basis Basis::operator+(const Basis &p_matrix) const {
|
||||
Basis ret(*this);
|
||||
ret += p_matrix;
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline void Basis::operator-=(const Basis &p_matrix) {
|
||||
elements[0] -= p_matrix.elements[0];
|
||||
elements[1] -= p_matrix.elements[1];
|
||||
elements[2] -= p_matrix.elements[2];
|
||||
}
|
||||
|
||||
inline Basis Basis::operator-(const Basis &p_matrix) const {
|
||||
Basis ret(*this);
|
||||
ret -= p_matrix;
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline void Basis::operator*=(real_t p_val) {
|
||||
elements[0] *= p_val;
|
||||
elements[1] *= p_val;
|
||||
elements[2] *= p_val;
|
||||
}
|
||||
|
||||
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(
|
||||
elements[0].dot(p_vector),
|
||||
elements[1].dot(p_vector),
|
||||
elements[2].dot(p_vector));
|
||||
}
|
||||
|
||||
Vector3 Basis::xform_inv(const Vector3 &p_vector) const {
|
||||
return Vector3(
|
||||
(elements[0][0] * p_vector.x) + (elements[1][0] * p_vector.y) + (elements[2][0] * p_vector.z),
|
||||
(elements[0][1] * p_vector.x) + (elements[1][1] * p_vector.y) + (elements[2][1] * p_vector.z),
|
||||
(elements[0][2] * p_vector.x) + (elements[1][2] * p_vector.y) + (elements[2][2] * p_vector.z));
|
||||
}
|
||||
|
||||
real_t Basis::determinant() const {
|
||||
return elements[0][0] * (elements[1][1] * elements[2][2] - elements[2][1] * elements[1][2]) -
|
||||
elements[1][0] * (elements[0][1] * elements[2][2] - elements[2][1] * elements[0][2]) +
|
||||
elements[2][0] * (elements[0][1] * elements[1][2] - elements[1][1] * elements[0][2]);
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_BASIS_HPP
|
||||
257
include/godot_cpp/variant/color.hpp
Normal file
257
include/godot_cpp/variant/color.hpp
Normal file
@@ -0,0 +1,257 @@
|
||||
#ifndef GODOT_COLOR_HPP
|
||||
#define GODOT_COLOR_HPP
|
||||
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class String;
|
||||
|
||||
class Color {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
union {
|
||||
struct {
|
||||
float r;
|
||||
float g;
|
||||
float b;
|
||||
float a;
|
||||
};
|
||||
float components[4] = { 0, 0, 0, 1.0 };
|
||||
};
|
||||
|
||||
uint32_t to_rgba32() const;
|
||||
uint32_t to_argb32() const;
|
||||
uint32_t to_abgr32() const;
|
||||
uint64_t to_rgba64() const;
|
||||
uint64_t to_argb64() const;
|
||||
uint64_t to_abgr64() const;
|
||||
float get_h() const;
|
||||
float get_s() const;
|
||||
float get_v() const;
|
||||
void set_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0);
|
||||
|
||||
inline float &operator[](int p_idx) {
|
||||
return components[p_idx];
|
||||
}
|
||||
inline const float &operator[](int p_idx) const {
|
||||
return components[p_idx];
|
||||
}
|
||||
|
||||
bool operator==(const Color &p_color) const {
|
||||
return (r == p_color.r && g == p_color.g && b == p_color.b && a == p_color.a);
|
||||
}
|
||||
bool operator!=(const Color &p_color) const {
|
||||
return (r != p_color.r || g != p_color.g || b != p_color.b || a != p_color.a);
|
||||
}
|
||||
|
||||
Color operator+(const Color &p_color) const;
|
||||
void operator+=(const Color &p_color);
|
||||
|
||||
Color operator-() const;
|
||||
Color operator-(const Color &p_color) const;
|
||||
void operator-=(const Color &p_color);
|
||||
|
||||
Color operator*(const Color &p_color) const;
|
||||
Color operator*(float p_scalar) const;
|
||||
void operator*=(const Color &p_color);
|
||||
void operator*=(float p_scalar);
|
||||
|
||||
Color operator/(const Color &p_color) const;
|
||||
Color operator/(float p_scalar) const;
|
||||
void operator/=(const Color &p_color);
|
||||
void operator/=(float p_scalar);
|
||||
|
||||
bool is_equal_approx(const Color &p_color) const;
|
||||
|
||||
void invert();
|
||||
Color inverted() const;
|
||||
|
||||
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));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
inline Color darkened(float p_amount) const {
|
||||
Color res = *this;
|
||||
res.r = res.r * (1.0f - p_amount);
|
||||
res.g = res.g * (1.0f - p_amount);
|
||||
res.b = res.b * (1.0f - p_amount);
|
||||
return res;
|
||||
}
|
||||
|
||||
inline Color lightened(float p_amount) const {
|
||||
Color res = *this;
|
||||
res.r = res.r + (1.0f - res.r) * p_amount;
|
||||
res.g = res.g + (1.0f - res.g) * p_amount;
|
||||
res.b = res.b + (1.0f - res.b) * p_amount;
|
||||
return res;
|
||||
}
|
||||
|
||||
inline uint32_t to_rgbe9995() const {
|
||||
const float pow2to9 = 512.0f;
|
||||
const float B = 15.0f;
|
||||
const float N = 9.0f;
|
||||
|
||||
float sharedexp = 65408.000f; // Result of: ((pow2to9 - 1.0f) / pow2to9) * powf(2.0f, 31.0f - 15.0f)
|
||||
|
||||
float cRed = Math::max(0.0f, Math::min(sharedexp, r));
|
||||
float cGreen = Math::max(0.0f, Math::min(sharedexp, g));
|
||||
float cBlue = Math::max(0.0f, Math::min(sharedexp, b));
|
||||
|
||||
float cMax = Math::max(cRed, Math::max(cGreen, cBlue));
|
||||
|
||||
float expp = Math::max(-B - 1.0f, Math::floor(Math::log(cMax) / (float)Math_LN2)) + 1.0f + B;
|
||||
|
||||
float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f);
|
||||
|
||||
float exps = expp + 1.0f;
|
||||
|
||||
if (0.0 <= 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);
|
||||
}
|
||||
|
||||
inline Color blend(const Color &p_over) const {
|
||||
Color res;
|
||||
float sa = 1.0 - p_over.a;
|
||||
res.a = a * sa + p_over.a;
|
||||
if (res.a == 0) {
|
||||
return Color(0, 0, 0, 0);
|
||||
} else {
|
||||
res.r = (r * a * sa + p_over.r * p_over.a) / res.a;
|
||||
res.g = (g * a * sa + p_over.g * p_over.a) / res.a;
|
||||
res.b = (b * a * sa + p_over.b * p_over.a) / res.a;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
inline Color to_linear() const {
|
||||
return Color(
|
||||
r < 0.04045 ? r * (1.0 / 12.92) : Math::pow((r + 0.055) * (1.0 / (1 + 0.055)), 2.4),
|
||||
g < 0.04045 ? g * (1.0 / 12.92) : Math::pow((g + 0.055) * (1.0 / (1 + 0.055)), 2.4),
|
||||
b < 0.04045 ? b * (1.0 / 12.92) : Math::pow((b + 0.055) * (1.0 / (1 + 0.055)), 2.4),
|
||||
a);
|
||||
}
|
||||
inline Color to_srgb() const {
|
||||
return Color(
|
||||
r < 0.0031308 ? 12.92 * r : (1.0 + 0.055) * Math::pow(r, 1.0f / 2.4f) - 0.055,
|
||||
g < 0.0031308 ? 12.92 * g : (1.0 + 0.055) * Math::pow(g, 1.0f / 2.4f) - 0.055,
|
||||
b < 0.0031308 ? 12.92 * b : (1.0 + 0.055) * Math::pow(b, 1.0f / 2.4f) - 0.055, a);
|
||||
}
|
||||
|
||||
static Color hex(uint32_t p_hex);
|
||||
static Color hex64(uint64_t p_hex);
|
||||
static Color html(const String &p_rgba);
|
||||
static bool html_is_valid(const String &p_color);
|
||||
static Color named(const String &p_name);
|
||||
static Color named(const String &p_name, const Color &p_default);
|
||||
static int find_named_color(const String &p_name);
|
||||
static int get_named_color_count();
|
||||
static String get_named_color_name(int p_idx);
|
||||
static Color get_named_color(int p_idx);
|
||||
static Color from_string(const String &p_string, const Color &p_default);
|
||||
String to_html(bool p_alpha = true) const;
|
||||
static Color from_hsv(float p_h, float p_s, float p_v, float p_a);
|
||||
static Color from_rgbe9995(uint32_t p_rgbe);
|
||||
|
||||
inline bool operator<(const Color &p_color) const; //used in set keys
|
||||
operator String() const;
|
||||
|
||||
// For the binder.
|
||||
inline void set_r8(int32_t r8) { r = (Math::clamp(r8, 0, 255) / 255.0); }
|
||||
inline int32_t get_r8() const { return int32_t(Math::clamp(r * 255.0, 0.0, 255.0)); }
|
||||
inline void set_g8(int32_t g8) { g = (Math::clamp(g8, 0, 255) / 255.0); }
|
||||
inline int32_t get_g8() const { return int32_t(Math::clamp(g * 255.0, 0.0, 255.0)); }
|
||||
inline void set_b8(int32_t b8) { b = (Math::clamp(b8, 0, 255) / 255.0); }
|
||||
inline int32_t get_b8() const { return int32_t(Math::clamp(b * 255.0, 0.0, 255.0)); }
|
||||
inline void set_a8(int32_t a8) { a = (Math::clamp(a8, 0, 255) / 255.0); }
|
||||
inline int32_t get_a8() const { return int32_t(Math::clamp(a * 255.0, 0.0, 255.0)); }
|
||||
|
||||
inline void set_h(float p_h) { set_hsv(p_h, get_s(), get_v()); }
|
||||
inline void set_s(float p_s) { set_hsv(get_h(), p_s, get_v()); }
|
||||
inline void set_v(float p_v) { set_hsv(get_h(), get_s(), p_v); }
|
||||
|
||||
inline Color() {}
|
||||
|
||||
/**
|
||||
* RGBA construct parameters.
|
||||
* Alpha is not optional as otherwise we can't bind the RGB version for scripting.
|
||||
*/
|
||||
inline Color(float p_r, float p_g, float p_b, float p_a) {
|
||||
r = p_r;
|
||||
g = p_g;
|
||||
b = p_b;
|
||||
a = p_a;
|
||||
}
|
||||
|
||||
/**
|
||||
* RGB construct parameters.
|
||||
*/
|
||||
inline Color(float p_r, float p_g, float p_b) {
|
||||
r = p_r;
|
||||
g = p_g;
|
||||
b = p_b;
|
||||
a = 1.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a Color from another Color, but with the specified alpha value.
|
||||
*/
|
||||
inline Color(const Color &p_c, float p_a) {
|
||||
r = p_c.r;
|
||||
g = p_c.g;
|
||||
b = p_c.b;
|
||||
a = p_a;
|
||||
}
|
||||
|
||||
Color(const String &p_code) {
|
||||
if (html_is_valid(p_code)) {
|
||||
*this = html(p_code);
|
||||
} else {
|
||||
*this = named(p_code);
|
||||
}
|
||||
}
|
||||
|
||||
Color(const String &p_code, float p_a) {
|
||||
*this = Color(p_code);
|
||||
a = p_a;
|
||||
}
|
||||
};
|
||||
|
||||
bool Color::operator<(const Color &p_color) const {
|
||||
if (r == p_color.r) {
|
||||
if (g == p_color.g) {
|
||||
if (b == p_color.b) {
|
||||
return (a < p_color.a);
|
||||
} else {
|
||||
return (b < p_color.b);
|
||||
}
|
||||
} else {
|
||||
return g < p_color.g;
|
||||
}
|
||||
} else {
|
||||
return r < p_color.r;
|
||||
}
|
||||
}
|
||||
|
||||
inline Color operator*(float p_scalar, const Color &p_color) {
|
||||
return p_color * p_scalar;
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_COLOR_HPP
|
||||
158
include/godot_cpp/variant/color_names.inc.hpp
Normal file
158
include/godot_cpp/variant/color_names.inc.hpp
Normal file
@@ -0,0 +1,158 @@
|
||||
namespace godot {
|
||||
|
||||
struct NamedColor {
|
||||
const char *name;
|
||||
Color color;
|
||||
};
|
||||
|
||||
static NamedColor named_colors[] = {
|
||||
{ "aliceblue", Color(0.94, 0.97, 1.00) },
|
||||
{ "antiquewhite", Color(0.98, 0.92, 0.84) },
|
||||
{ "aqua", Color(0.00, 1.00, 1.00) },
|
||||
{ "aquamarine", Color(0.50, 1.00, 0.83) },
|
||||
{ "azure", Color(0.94, 1.00, 1.00) },
|
||||
{ "beige", Color(0.96, 0.96, 0.86) },
|
||||
{ "bisque", Color(1.00, 0.89, 0.77) },
|
||||
{ "black", Color(0.00, 0.00, 0.00) },
|
||||
{ "blanchedalmond", Color(1.00, 0.92, 0.80) },
|
||||
{ "blue", Color(0.00, 0.00, 1.00) },
|
||||
{ "blueviolet", Color(0.54, 0.17, 0.89) },
|
||||
{ "brown", Color(0.65, 0.16, 0.16) },
|
||||
{ "burlywood", Color(0.87, 0.72, 0.53) },
|
||||
{ "cadetblue", Color(0.37, 0.62, 0.63) },
|
||||
{ "chartreuse", Color(0.50, 1.00, 0.00) },
|
||||
{ "chocolate", Color(0.82, 0.41, 0.12) },
|
||||
{ "coral", Color(1.00, 0.50, 0.31) },
|
||||
{ "cornflower", Color(0.39, 0.58, 0.93) },
|
||||
{ "cornsilk", Color(1.00, 0.97, 0.86) },
|
||||
{ "crimson", Color(0.86, 0.08, 0.24) },
|
||||
{ "cyan", Color(0.00, 1.00, 1.00) },
|
||||
{ "darkblue", Color(0.00, 0.00, 0.55) },
|
||||
{ "darkcyan", Color(0.00, 0.55, 0.55) },
|
||||
{ "darkgoldenrod", Color(0.72, 0.53, 0.04) },
|
||||
{ "darkgray", Color(0.66, 0.66, 0.66) },
|
||||
{ "darkgreen", Color(0.00, 0.39, 0.00) },
|
||||
{ "darkkhaki", Color(0.74, 0.72, 0.42) },
|
||||
{ "darkmagenta", Color(0.55, 0.00, 0.55) },
|
||||
{ "darkolivegreen", Color(0.33, 0.42, 0.18) },
|
||||
{ "darkorange", Color(1.00, 0.55, 0.00) },
|
||||
{ "darkorchid", Color(0.60, 0.20, 0.80) },
|
||||
{ "darkred", Color(0.55, 0.00, 0.00) },
|
||||
{ "darksalmon", Color(0.91, 0.59, 0.48) },
|
||||
{ "darkseagreen", Color(0.56, 0.74, 0.56) },
|
||||
{ "darkslateblue", Color(0.28, 0.24, 0.55) },
|
||||
{ "darkslategray", Color(0.18, 0.31, 0.31) },
|
||||
{ "darkturquoise", Color(0.00, 0.81, 0.82) },
|
||||
{ "darkviolet", Color(0.58, 0.00, 0.83) },
|
||||
{ "deeppink", Color(1.00, 0.08, 0.58) },
|
||||
{ "deepskyblue", Color(0.00, 0.75, 1.00) },
|
||||
{ "dimgray", Color(0.41, 0.41, 0.41) },
|
||||
{ "dodgerblue", Color(0.12, 0.56, 1.00) },
|
||||
{ "firebrick", Color(0.70, 0.13, 0.13) },
|
||||
{ "floralwhite", Color(1.00, 0.98, 0.94) },
|
||||
{ "forestgreen", Color(0.13, 0.55, 0.13) },
|
||||
{ "fuchsia", Color(1.00, 0.00, 1.00) },
|
||||
{ "gainsboro", Color(0.86, 0.86, 0.86) },
|
||||
{ "ghostwhite", Color(0.97, 0.97, 1.00) },
|
||||
{ "gold", Color(1.00, 0.84, 0.00) },
|
||||
{ "goldenrod", Color(0.85, 0.65, 0.13) },
|
||||
{ "gray", Color(0.75, 0.75, 0.75) },
|
||||
{ "green", Color(0.00, 1.00, 0.00) },
|
||||
{ "greenyellow", Color(0.68, 1.00, 0.18) },
|
||||
{ "honeydew", Color(0.94, 1.00, 0.94) },
|
||||
{ "hotpink", Color(1.00, 0.41, 0.71) },
|
||||
{ "indianred", Color(0.80, 0.36, 0.36) },
|
||||
{ "indigo", Color(0.29, 0.00, 0.51) },
|
||||
{ "ivory", Color(1.00, 1.00, 0.94) },
|
||||
{ "khaki", Color(0.94, 0.90, 0.55) },
|
||||
{ "lavender", Color(0.90, 0.90, 0.98) },
|
||||
{ "lavenderblush", Color(1.00, 0.94, 0.96) },
|
||||
{ "lawngreen", Color(0.49, 0.99, 0.00) },
|
||||
{ "lemonchiffon", Color(1.00, 0.98, 0.80) },
|
||||
{ "lightblue", Color(0.68, 0.85, 0.90) },
|
||||
{ "lightcoral", Color(0.94, 0.50, 0.50) },
|
||||
{ "lightcyan", Color(0.88, 1.00, 1.00) },
|
||||
{ "lightgoldenrod", Color(0.98, 0.98, 0.82) },
|
||||
{ "lightgray", Color(0.83, 0.83, 0.83) },
|
||||
{ "lightgreen", Color(0.56, 0.93, 0.56) },
|
||||
{ "lightpink", Color(1.00, 0.71, 0.76) },
|
||||
{ "lightsalmon", Color(1.00, 0.63, 0.48) },
|
||||
{ "lightseagreen", Color(0.13, 0.70, 0.67) },
|
||||
{ "lightskyblue", Color(0.53, 0.81, 0.98) },
|
||||
{ "lightslategray", Color(0.47, 0.53, 0.60) },
|
||||
{ "lightsteelblue", Color(0.69, 0.77, 0.87) },
|
||||
{ "lightyellow", Color(1.00, 1.00, 0.88) },
|
||||
{ "lime", Color(0.00, 1.00, 0.00) },
|
||||
{ "limegreen", Color(0.20, 0.80, 0.20) },
|
||||
{ "linen", Color(0.98, 0.94, 0.90) },
|
||||
{ "magenta", Color(1.00, 0.00, 1.00) },
|
||||
{ "maroon", Color(0.69, 0.19, 0.38) },
|
||||
{ "mediumaquamarine", Color(0.40, 0.80, 0.67) },
|
||||
{ "mediumblue", Color(0.00, 0.00, 0.80) },
|
||||
{ "mediumorchid", Color(0.73, 0.33, 0.83) },
|
||||
{ "mediumpurple", Color(0.58, 0.44, 0.86) },
|
||||
{ "mediumseagreen", Color(0.24, 0.70, 0.44) },
|
||||
{ "mediumslateblue", Color(0.48, 0.41, 0.93) },
|
||||
{ "mediumspringgreen", Color(0.00, 0.98, 0.60) },
|
||||
{ "mediumturquoise", Color(0.28, 0.82, 0.80) },
|
||||
{ "mediumvioletred", Color(0.78, 0.08, 0.52) },
|
||||
{ "midnightblue", Color(0.10, 0.10, 0.44) },
|
||||
{ "mintcream", Color(0.96, 1.00, 0.98) },
|
||||
{ "mistyrose", Color(1.00, 0.89, 0.88) },
|
||||
{ "moccasin", Color(1.00, 0.89, 0.71) },
|
||||
{ "navajowhite", Color(1.00, 0.87, 0.68) },
|
||||
{ "navyblue", Color(0.00, 0.00, 0.50) },
|
||||
{ "oldlace", Color(0.99, 0.96, 0.90) },
|
||||
{ "olive", Color(0.50, 0.50, 0.00) },
|
||||
{ "olivedrab", Color(0.42, 0.56, 0.14) },
|
||||
{ "orange", Color(1.00, 0.65, 0.00) },
|
||||
{ "orangered", Color(1.00, 0.27, 0.00) },
|
||||
{ "orchid", Color(0.85, 0.44, 0.84) },
|
||||
{ "palegoldenrod", Color(0.93, 0.91, 0.67) },
|
||||
{ "palegreen", Color(0.60, 0.98, 0.60) },
|
||||
{ "paleturquoise", Color(0.69, 0.93, 0.93) },
|
||||
{ "palevioletred", Color(0.86, 0.44, 0.58) },
|
||||
{ "papayawhip", Color(1.00, 0.94, 0.84) },
|
||||
{ "peachpuff", Color(1.00, 0.85, 0.73) },
|
||||
{ "peru", Color(0.80, 0.52, 0.25) },
|
||||
{ "pink", Color(1.00, 0.75, 0.80) },
|
||||
{ "plum", Color(0.87, 0.63, 0.87) },
|
||||
{ "powderblue", Color(0.69, 0.88, 0.90) },
|
||||
{ "purple", Color(0.63, 0.13, 0.94) },
|
||||
{ "rebeccapurple", Color(0.40, 0.20, 0.60) },
|
||||
{ "red", Color(1.00, 0.00, 0.00) },
|
||||
{ "rosybrown", Color(0.74, 0.56, 0.56) },
|
||||
{ "royalblue", Color(0.25, 0.41, 0.88) },
|
||||
{ "saddlebrown", Color(0.55, 0.27, 0.07) },
|
||||
{ "salmon", Color(0.98, 0.50, 0.45) },
|
||||
{ "sandybrown", Color(0.96, 0.64, 0.38) },
|
||||
{ "seagreen", Color(0.18, 0.55, 0.34) },
|
||||
{ "seashell", Color(1.00, 0.96, 0.93) },
|
||||
{ "sienna", Color(0.63, 0.32, 0.18) },
|
||||
{ "silver", Color(0.75, 0.75, 0.75) },
|
||||
{ "skyblue", Color(0.53, 0.81, 0.92) },
|
||||
{ "slateblue", Color(0.42, 0.35, 0.80) },
|
||||
{ "slategray", Color(0.44, 0.50, 0.56) },
|
||||
{ "snow", Color(1.00, 0.98, 0.98) },
|
||||
{ "springgreen", Color(0.00, 1.00, 0.50) },
|
||||
{ "steelblue", Color(0.27, 0.51, 0.71) },
|
||||
{ "tan", Color(0.82, 0.71, 0.55) },
|
||||
{ "teal", Color(0.00, 0.50, 0.50) },
|
||||
{ "thistle", Color(0.85, 0.75, 0.85) },
|
||||
{ "tomato", Color(1.00, 0.39, 0.28) },
|
||||
{ "transparent", Color(1.00, 1.00, 1.00, 0.00) },
|
||||
{ "turquoise", Color(0.25, 0.88, 0.82) },
|
||||
{ "violet", Color(0.93, 0.51, 0.93) },
|
||||
{ "webgray", Color(0.50, 0.50, 0.50) },
|
||||
{ "webgreen", Color(0.00, 0.50, 0.00) },
|
||||
{ "webmaroon", Color(0.50, 0.00, 0.00) },
|
||||
{ "webpurple", Color(0.50, 0.00, 0.50) },
|
||||
{ "wheat", Color(0.96, 0.87, 0.70) },
|
||||
{ "white", Color(1.00, 1.00, 1.00) },
|
||||
{ "whitesmoke", Color(0.96, 0.96, 0.96) },
|
||||
{ "yellow", Color(1.00, 1.00, 0.00) },
|
||||
{ "yellowgreen", Color(0.60, 0.80, 0.20) },
|
||||
{ nullptr, Color() },
|
||||
};
|
||||
|
||||
} // namespace godot
|
||||
106
include/godot_cpp/variant/plane.hpp
Normal file
106
include/godot_cpp/variant/plane.hpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#ifndef GODOT_PLANE_HPP
|
||||
#define GODOT_PLANE_HPP
|
||||
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
#include <godot_cpp/variant/vector3.hpp>
|
||||
#include <godot_cpp/classes/global_constants.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Plane {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
Vector3 normal;
|
||||
real_t d = 0;
|
||||
|
||||
void set_normal(const Vector3 &p_normal);
|
||||
inline Vector3 get_normal() const { return normal; }; ///Point is coplanar, CMP_EPSILON for precision
|
||||
|
||||
void normalize();
|
||||
Plane normalized() const;
|
||||
|
||||
/* Plane-Point operations */
|
||||
|
||||
inline Vector3 center() const { return normal * d; }
|
||||
Vector3 get_any_perpendicular_normal() const;
|
||||
|
||||
inline bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane
|
||||
inline real_t distance_to(const Vector3 &p_point) const;
|
||||
inline bool has_point(const Vector3 &p_point, real_t _epsilon = CMP_EPSILON) const;
|
||||
|
||||
/* intersections */
|
||||
|
||||
bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result = nullptr) const;
|
||||
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const;
|
||||
bool intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const;
|
||||
|
||||
inline Vector3 project(const Vector3 &p_point) const {
|
||||
return p_point - normal * distance_to(p_point);
|
||||
}
|
||||
|
||||
/* misc */
|
||||
|
||||
Plane operator-() const { return Plane(-normal, -d); }
|
||||
bool is_equal_approx(const Plane &p_plane) const;
|
||||
bool is_equal_approx_any_side(const Plane &p_plane) const;
|
||||
|
||||
inline bool operator==(const Plane &p_plane) const;
|
||||
inline bool operator!=(const Plane &p_plane) const;
|
||||
operator String() const;
|
||||
|
||||
inline Plane() {}
|
||||
inline Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) :
|
||||
normal(p_a, p_b, p_c),
|
||||
d(p_d) {}
|
||||
|
||||
inline Plane(const Vector3 &p_normal, real_t p_d);
|
||||
inline Plane(const Vector3 &p_point, const Vector3 &p_normal);
|
||||
inline Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE);
|
||||
};
|
||||
|
||||
bool Plane::is_point_over(const Vector3 &p_point) const {
|
||||
return (normal.dot(p_point) > d);
|
||||
}
|
||||
|
||||
real_t Plane::distance_to(const Vector3 &p_point) const {
|
||||
return (normal.dot(p_point) - d);
|
||||
}
|
||||
|
||||
bool Plane::has_point(const Vector3 &p_point, real_t _epsilon) const {
|
||||
real_t dist = normal.dot(p_point) - d;
|
||||
dist = Math::abs(dist);
|
||||
return (dist <= _epsilon);
|
||||
}
|
||||
|
||||
Plane::Plane(const Vector3 &p_normal, real_t p_d) :
|
||||
normal(p_normal),
|
||||
d(p_d) {
|
||||
}
|
||||
|
||||
Plane::Plane(const Vector3 &p_point, const Vector3 &p_normal) :
|
||||
normal(p_normal),
|
||||
d(p_normal.dot(p_point)) {
|
||||
}
|
||||
|
||||
Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir) {
|
||||
if (p_dir == CLOCKWISE) {
|
||||
normal = (p_point1 - p_point3).cross(p_point1 - p_point2);
|
||||
} else {
|
||||
normal = (p_point1 - p_point2).cross(p_point1 - p_point3);
|
||||
}
|
||||
|
||||
normal.normalize();
|
||||
d = normal.dot(p_point1);
|
||||
}
|
||||
|
||||
bool Plane::operator==(const Plane &p_plane) const {
|
||||
return normal == p_plane.normal && d == p_plane.d;
|
||||
}
|
||||
|
||||
bool Plane::operator!=(const Plane &p_plane) const {
|
||||
return normal != p_plane.normal || d != p_plane.d;
|
||||
}
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_PLANE_HPP
|
||||
212
include/godot_cpp/variant/quaternion.hpp
Normal file
212
include/godot_cpp/variant/quaternion.hpp
Normal file
@@ -0,0 +1,212 @@
|
||||
#ifndef GODOT_QUAT_HPP
|
||||
#define GODOT_QUAT_HPP
|
||||
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
#include <godot_cpp/variant/vector3.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Quaternion {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
union {
|
||||
struct {
|
||||
real_t x;
|
||||
real_t y;
|
||||
real_t z;
|
||||
real_t w;
|
||||
};
|
||||
real_t components[4] = { 0, 0, 0, 1.0 };
|
||||
};
|
||||
|
||||
inline real_t &operator[](int idx) {
|
||||
return components[idx];
|
||||
}
|
||||
inline const real_t &operator[](int idx) const {
|
||||
return components[idx];
|
||||
}
|
||||
inline real_t length_squared() const;
|
||||
bool is_equal_approx(const Quaternion &p_quat) const;
|
||||
real_t length() const;
|
||||
void normalize();
|
||||
Quaternion normalized() const;
|
||||
bool is_normalized() const;
|
||||
Quaternion inverse() const;
|
||||
inline real_t dot(const Quaternion &p_q) const;
|
||||
|
||||
Vector3 get_euler_xyz() const;
|
||||
Vector3 get_euler_yxz() const;
|
||||
Vector3 get_euler() const { return get_euler_yxz(); };
|
||||
|
||||
Quaternion slerp(const Quaternion &p_to, const real_t &p_weight) const;
|
||||
Quaternion slerpni(const Quaternion &p_to, const real_t &p_weight) const;
|
||||
Quaternion cubic_slerp(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const;
|
||||
|
||||
inline void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
|
||||
r_angle = 2 * Math::acos(w);
|
||||
real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
|
||||
r_axis.x = x * r;
|
||||
r_axis.y = y * r;
|
||||
r_axis.z = z * r;
|
||||
}
|
||||
|
||||
void operator*=(const Quaternion &p_q);
|
||||
Quaternion operator*(const Quaternion &p_q) const;
|
||||
|
||||
Quaternion operator*(const Vector3 &v) const {
|
||||
return Quaternion(w * v.x + y * v.z - z * v.y,
|
||||
w * v.y + z * v.x - x * v.z,
|
||||
w * v.z + x * v.y - y * v.x,
|
||||
-x * v.x - y * v.y - z * v.z);
|
||||
}
|
||||
|
||||
inline Vector3 xform(const Vector3 &v) const {
|
||||
#ifdef MATH_CHECKS
|
||||
ERR_FAIL_COND_V(!is_normalized(), v);
|
||||
#endif
|
||||
Vector3 u(x, y, z);
|
||||
Vector3 uv = u.cross(v);
|
||||
return v + ((uv * w) + u.cross(uv)) * ((real_t)2);
|
||||
}
|
||||
|
||||
inline Vector3 xform_inv(const Vector3 &v) const {
|
||||
return inverse().xform(v);
|
||||
}
|
||||
|
||||
inline void operator+=(const Quaternion &p_q);
|
||||
inline void operator-=(const Quaternion &p_q);
|
||||
inline void operator*=(const real_t &s);
|
||||
inline void operator/=(const real_t &s);
|
||||
inline Quaternion operator+(const Quaternion &q2) const;
|
||||
inline Quaternion operator-(const Quaternion &q2) const;
|
||||
inline Quaternion operator-() const;
|
||||
inline Quaternion operator*(const real_t &s) const;
|
||||
inline Quaternion operator/(const real_t &s) const;
|
||||
|
||||
inline bool operator==(const Quaternion &p_quat) const;
|
||||
inline bool operator!=(const Quaternion &p_quat) const;
|
||||
|
||||
operator String() const;
|
||||
|
||||
inline Quaternion() {}
|
||||
|
||||
inline Quaternion(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) {
|
||||
}
|
||||
|
||||
Quaternion(const Vector3 &p_axis, real_t p_angle);
|
||||
|
||||
Quaternion(const Vector3 &p_euler);
|
||||
|
||||
Quaternion(const Quaternion &p_q) :
|
||||
x(p_q.x),
|
||||
y(p_q.y),
|
||||
z(p_q.z),
|
||||
w(p_q.w) {
|
||||
}
|
||||
|
||||
Quaternion &operator=(const Quaternion &p_q) {
|
||||
x = p_q.x;
|
||||
y = p_q.y;
|
||||
z = p_q.z;
|
||||
w = p_q.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Quaternion(const Vector3 &v0, const Vector3 &v1) // shortest arc
|
||||
{
|
||||
Vector3 c = v0.cross(v1);
|
||||
real_t d = v0.dot(v1);
|
||||
|
||||
if (d < -1.0 + CMP_EPSILON) {
|
||||
x = 0;
|
||||
y = 1;
|
||||
z = 0;
|
||||
w = 0;
|
||||
} else {
|
||||
real_t s = Math::sqrt((1.0 + d) * 2.0);
|
||||
real_t rs = 1.0 / s;
|
||||
|
||||
x = c.x * rs;
|
||||
y = c.y * rs;
|
||||
z = c.z * rs;
|
||||
w = s * 0.5;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
real_t Quaternion::dot(const Quaternion &p_q) const {
|
||||
return x * p_q.x + y * p_q.y + z * p_q.z + w * p_q.w;
|
||||
}
|
||||
|
||||
real_t Quaternion::length_squared() const {
|
||||
return dot(*this);
|
||||
}
|
||||
|
||||
void Quaternion::operator+=(const Quaternion &p_q) {
|
||||
x += p_q.x;
|
||||
y += p_q.y;
|
||||
z += p_q.z;
|
||||
w += p_q.w;
|
||||
}
|
||||
|
||||
void Quaternion::operator-=(const Quaternion &p_q) {
|
||||
x -= p_q.x;
|
||||
y -= p_q.y;
|
||||
z -= p_q.z;
|
||||
w -= p_q.w;
|
||||
}
|
||||
|
||||
void Quaternion::operator*=(const real_t &s) {
|
||||
x *= s;
|
||||
y *= s;
|
||||
z *= s;
|
||||
w *= s;
|
||||
}
|
||||
|
||||
void Quaternion::operator/=(const real_t &s) {
|
||||
*this *= 1.0 / s;
|
||||
}
|
||||
|
||||
Quaternion Quaternion::operator+(const Quaternion &q2) const {
|
||||
const Quaternion &q1 = *this;
|
||||
return Quaternion(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w);
|
||||
}
|
||||
|
||||
Quaternion Quaternion::operator-(const Quaternion &q2) const {
|
||||
const Quaternion &q1 = *this;
|
||||
return Quaternion(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w);
|
||||
}
|
||||
|
||||
Quaternion Quaternion::operator-() const {
|
||||
const Quaternion &q2 = *this;
|
||||
return Quaternion(-q2.x, -q2.y, -q2.z, -q2.w);
|
||||
}
|
||||
|
||||
Quaternion Quaternion::operator*(const real_t &s) const {
|
||||
return Quaternion(x * s, y * s, z * s, w * s);
|
||||
}
|
||||
|
||||
Quaternion Quaternion::operator/(const real_t &s) const {
|
||||
return *this * (1.0 / s);
|
||||
}
|
||||
|
||||
bool Quaternion::operator==(const Quaternion &p_quat) const {
|
||||
return x == p_quat.x && y == p_quat.y && z == p_quat.z && w == p_quat.w;
|
||||
}
|
||||
|
||||
bool Quaternion::operator!=(const Quaternion &p_quat) const {
|
||||
return x != p_quat.x || y != p_quat.y || z != p_quat.z || w != p_quat.w;
|
||||
}
|
||||
|
||||
inline Quaternion operator*(const real_t &p_real, const Quaternion &p_quat) {
|
||||
return p_quat * p_real;
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_QUAT_HPP
|
||||
312
include/godot_cpp/variant/rect2.hpp
Normal file
312
include/godot_cpp/variant/rect2.hpp
Normal file
@@ -0,0 +1,312 @@
|
||||
|
||||
#ifndef GODOT_RECT2_HPP
|
||||
#define GODOT_RECT2_HPP
|
||||
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
#include <godot_cpp/variant/vector2.hpp>
|
||||
#include <godot_cpp/classes/global_constants.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
struct Transform2D;
|
||||
|
||||
class Rect2 {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
Point2 position;
|
||||
Size2 size;
|
||||
|
||||
const Vector2 &get_position() const { return position; }
|
||||
void set_position(const Vector2 &p_pos) { position = p_pos; }
|
||||
const Vector2 &get_size() const { return size; }
|
||||
void set_size(const Vector2 &p_size) { size = p_size; }
|
||||
|
||||
real_t get_area() const { return size.width * size.height; }
|
||||
|
||||
inline bool intersects(const Rect2 &p_rect, const bool p_include_borders = false) const {
|
||||
if (p_include_borders) {
|
||||
if (position.x > (p_rect.position.x + p_rect.size.width)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.x + size.width) < p_rect.position.x) {
|
||||
return false;
|
||||
}
|
||||
if (position.y > (p_rect.position.y + p_rect.size.height)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.y + size.height) < p_rect.position.y) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (position.x >= (p_rect.position.x + p_rect.size.width)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.x + size.width) <= p_rect.position.x) {
|
||||
return false;
|
||||
}
|
||||
if (position.y >= (p_rect.position.y + p_rect.size.height)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.y + size.height) <= p_rect.position.y) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline real_t distance_to(const Vector2 &p_point) const {
|
||||
real_t dist = 0.0;
|
||||
bool inside = true;
|
||||
|
||||
if (p_point.x < position.x) {
|
||||
real_t d = position.x - p_point.x;
|
||||
dist = d;
|
||||
inside = false;
|
||||
}
|
||||
if (p_point.y < position.y) {
|
||||
real_t d = position.y - p_point.y;
|
||||
dist = inside ? d : Math::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);
|
||||
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);
|
||||
inside = false;
|
||||
}
|
||||
|
||||
if (inside) {
|
||||
return 0;
|
||||
} else {
|
||||
return dist;
|
||||
}
|
||||
}
|
||||
|
||||
bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
|
||||
|
||||
bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = nullptr, Point2 *r_normal = nullptr) const;
|
||||
|
||||
inline bool encloses(const Rect2 &p_rect) const {
|
||||
return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
|
||||
((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) &&
|
||||
((p_rect.position.y + p_rect.size.y) <= (position.y + size.y));
|
||||
}
|
||||
|
||||
inline bool has_no_area() const {
|
||||
return (size.x <= 0 || size.y <= 0);
|
||||
}
|
||||
|
||||
// Returns the instersection 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;
|
||||
|
||||
if (!intersects(new_rect)) {
|
||||
return Rect2();
|
||||
}
|
||||
|
||||
new_rect.position.x = Math::max(p_rect.position.x, position.x);
|
||||
new_rect.position.y = Math::max(p_rect.position.y, position.y);
|
||||
|
||||
Point2 p_rect_end = p_rect.position + p_rect.size;
|
||||
Point2 end = position + size;
|
||||
|
||||
new_rect.size.x = Math::min(p_rect_end.x, end.x) - new_rect.position.x;
|
||||
new_rect.size.y = Math::min(p_rect_end.y, end.y) - new_rect.position.y;
|
||||
|
||||
return new_rect;
|
||||
}
|
||||
|
||||
inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
|
||||
|
||||
Rect2 new_rect;
|
||||
|
||||
new_rect.position.x = Math::min(p_rect.position.x, position.x);
|
||||
new_rect.position.y = Math::min(p_rect.position.y, position.y);
|
||||
|
||||
new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x);
|
||||
new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y);
|
||||
|
||||
new_rect.size = new_rect.size - new_rect.position; //make relative again
|
||||
|
||||
return new_rect;
|
||||
}
|
||||
inline bool has_point(const Point2 &p_point) const {
|
||||
if (p_point.x < position.x) {
|
||||
return false;
|
||||
}
|
||||
if (p_point.y < position.y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (p_point.x >= (position.x + size.x)) {
|
||||
return false;
|
||||
}
|
||||
if (p_point.y >= (position.y + size.y)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
bool is_equal_approx(const Rect2 &p_rect) const;
|
||||
|
||||
bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
|
||||
bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
|
||||
|
||||
inline Rect2 grow(real_t p_amount) const {
|
||||
Rect2 g = *this;
|
||||
g.position.x -= p_amount;
|
||||
g.position.y -= p_amount;
|
||||
g.size.width += p_amount * 2;
|
||||
g.size.height += p_amount * 2;
|
||||
return g;
|
||||
}
|
||||
|
||||
inline Rect2 grow_side(Side p_side, real_t p_amount) const {
|
||||
Rect2 g = *this;
|
||||
g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
|
||||
(SIDE_TOP == p_side) ? p_amount : 0,
|
||||
(SIDE_RIGHT == p_side) ? p_amount : 0,
|
||||
(SIDE_BOTTOM == p_side) ? p_amount : 0);
|
||||
return g;
|
||||
}
|
||||
|
||||
inline Rect2 grow_side_bind(uint32_t p_side, real_t p_amount) const {
|
||||
return grow_side(Side(p_side), p_amount);
|
||||
}
|
||||
|
||||
inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const {
|
||||
Rect2 g = *this;
|
||||
g.position.x -= p_left;
|
||||
g.position.y -= p_top;
|
||||
g.size.width += p_left + p_right;
|
||||
g.size.height += p_top + p_bottom;
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
inline Rect2 expand(const Vector2 &p_vector) const {
|
||||
Rect2 r = *this;
|
||||
r.expand_to(p_vector);
|
||||
return r;
|
||||
}
|
||||
|
||||
inline void expand_to(const Vector2 &p_vector) { //in place function for speed
|
||||
|
||||
Vector2 begin = position;
|
||||
Vector2 end = position + size;
|
||||
|
||||
if (p_vector.x < begin.x) {
|
||||
begin.x = p_vector.x;
|
||||
}
|
||||
if (p_vector.y < begin.y) {
|
||||
begin.y = p_vector.y;
|
||||
}
|
||||
|
||||
if (p_vector.x > end.x) {
|
||||
end.x = p_vector.x;
|
||||
}
|
||||
if (p_vector.y > end.y) {
|
||||
end.y = p_vector.y;
|
||||
}
|
||||
|
||||
position = begin;
|
||||
size = end - begin;
|
||||
}
|
||||
|
||||
inline Rect2 abs() const {
|
||||
return Rect2(Point2(position.x + Math::min(size.x, (real_t)0), position.y + Math::min(size.y, (real_t)0)), size.abs());
|
||||
}
|
||||
|
||||
Vector2 get_support(const Vector2 &p_normal) const {
|
||||
Vector2 half_extents = size * 0.5;
|
||||
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;
|
||||
}
|
||||
|
||||
inline bool intersects_filled_polygon(const Vector2 *p_points, int p_point_count) const {
|
||||
Vector2 center = position + size * 0.5;
|
||||
int side_plus = 0;
|
||||
int side_minus = 0;
|
||||
Vector2 end = position + size;
|
||||
|
||||
int i_f = p_point_count - 1;
|
||||
for (int i = 0; i < p_point_count; i++) {
|
||||
const Vector2 &a = p_points[i_f];
|
||||
const Vector2 &b = p_points[i];
|
||||
i_f = i;
|
||||
|
||||
Vector2 r = (b - a);
|
||||
float l = r.length();
|
||||
if (l == 0.0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//check inside
|
||||
Vector2 tg = r.orthogonal();
|
||||
float s = tg.dot(center) - tg.dot(a);
|
||||
if (s < 0.0) {
|
||||
side_plus++;
|
||||
} else {
|
||||
side_minus++;
|
||||
}
|
||||
|
||||
//check ray box
|
||||
r /= l;
|
||||
Vector2 ir(1.0 / r.x, 1.0 / r.y);
|
||||
|
||||
// lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
|
||||
// r.org is origin of ray
|
||||
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));
|
||||
|
||||
// if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
|
||||
if (tmax < 0 || tmin > tmax || tmin >= l) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (side_plus * side_minus == 0) {
|
||||
return true; //all inside
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline void set_end(const Vector2 &p_end) {
|
||||
size = p_end - position;
|
||||
}
|
||||
|
||||
inline Vector2 get_end() const {
|
||||
return position + size;
|
||||
}
|
||||
|
||||
operator String() const;
|
||||
|
||||
Rect2() {}
|
||||
Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
|
||||
position(Point2(p_x, p_y)),
|
||||
size(Size2(p_width, p_height)) {
|
||||
}
|
||||
Rect2(const Point2 &p_pos, const Size2 &p_size) :
|
||||
position(p_pos),
|
||||
size(p_size) {
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_RECT2_HPP
|
||||
198
include/godot_cpp/variant/rect2i.hpp
Normal file
198
include/godot_cpp/variant/rect2i.hpp
Normal file
@@ -0,0 +1,198 @@
|
||||
#ifndef GODOT_RECT2I_HPP
|
||||
#define GODOT_RECT2I_HPP
|
||||
|
||||
#include <godot_cpp/variant/rect2.hpp>
|
||||
#include <godot_cpp/variant/vector2i.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Rect2i {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
Point2i position;
|
||||
Size2i size;
|
||||
|
||||
const Point2i &get_position() const { return position; }
|
||||
void set_position(const Point2i &p_position) { position = p_position; }
|
||||
const Size2i &get_size() const { return size; }
|
||||
void set_size(const Size2i &p_size) { size = p_size; }
|
||||
|
||||
int get_area() const { return size.width * size.height; }
|
||||
|
||||
inline bool intersects(const Rect2i &p_rect) const {
|
||||
if (position.x > (p_rect.position.x + p_rect.size.width)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.x + size.width) < p_rect.position.x) {
|
||||
return false;
|
||||
}
|
||||
if (position.y > (p_rect.position.y + p_rect.size.height)) {
|
||||
return false;
|
||||
}
|
||||
if ((position.y + size.height) < p_rect.position.y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool encloses(const Rect2i &p_rect) const {
|
||||
return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
|
||||
((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
|
||||
((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
|
||||
}
|
||||
|
||||
inline bool has_no_area() const {
|
||||
return (size.x <= 0 || size.y <= 0);
|
||||
}
|
||||
|
||||
// Returns the instersection 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;
|
||||
|
||||
if (!intersects(new_rect)) {
|
||||
return Rect2i();
|
||||
}
|
||||
|
||||
new_rect.position.x = Math::max(p_rect.position.x, position.x);
|
||||
new_rect.position.y = Math::max(p_rect.position.y, position.y);
|
||||
|
||||
Point2i p_rect_end = p_rect.position + p_rect.size;
|
||||
Point2i end = position + size;
|
||||
|
||||
new_rect.size.x = (int)(Math::min(p_rect_end.x, end.x) - new_rect.position.x);
|
||||
new_rect.size.y = (int)(Math::min(p_rect_end.y, end.y) - new_rect.position.y);
|
||||
|
||||
return new_rect;
|
||||
}
|
||||
|
||||
inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
|
||||
|
||||
Rect2i new_rect;
|
||||
|
||||
new_rect.position.x = Math::min(p_rect.position.x, position.x);
|
||||
new_rect.position.y = Math::min(p_rect.position.y, position.y);
|
||||
|
||||
new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x);
|
||||
new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y);
|
||||
|
||||
new_rect.size = new_rect.size - new_rect.position; //make relative again
|
||||
|
||||
return new_rect;
|
||||
}
|
||||
bool has_point(const Point2i &p_point) const {
|
||||
if (p_point.x < position.x) {
|
||||
return false;
|
||||
}
|
||||
if (p_point.y < position.y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (p_point.x >= (position.x + size.x)) {
|
||||
return false;
|
||||
}
|
||||
if (p_point.y >= (position.y + size.y)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; }
|
||||
bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; }
|
||||
|
||||
Rect2i grow(int p_amount) const {
|
||||
Rect2i g = *this;
|
||||
g.position.x -= p_amount;
|
||||
g.position.y -= p_amount;
|
||||
g.size.width += p_amount * 2;
|
||||
g.size.height += p_amount * 2;
|
||||
return g;
|
||||
}
|
||||
|
||||
inline Rect2i grow_side(Side p_side, int p_amount) const {
|
||||
Rect2i g = *this;
|
||||
g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
|
||||
(SIDE_TOP == p_side) ? p_amount : 0,
|
||||
(SIDE_RIGHT == p_side) ? p_amount : 0,
|
||||
(SIDE_BOTTOM == p_side) ? p_amount : 0);
|
||||
return g;
|
||||
}
|
||||
|
||||
inline Rect2i grow_side_bind(uint32_t p_side, int p_amount) const {
|
||||
return grow_side(Side(p_side), p_amount);
|
||||
}
|
||||
|
||||
inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const {
|
||||
Rect2i g = *this;
|
||||
g.position.x -= p_left;
|
||||
g.position.y -= p_top;
|
||||
g.size.width += p_left + p_right;
|
||||
g.size.height += p_top + p_bottom;
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
inline Rect2i expand(const Vector2i &p_vector) const {
|
||||
Rect2i r = *this;
|
||||
r.expand_to(p_vector);
|
||||
return r;
|
||||
}
|
||||
|
||||
inline void expand_to(const Point2i &p_vector) {
|
||||
Point2i begin = position;
|
||||
Point2i end = position + size;
|
||||
|
||||
if (p_vector.x < begin.x) {
|
||||
begin.x = p_vector.x;
|
||||
}
|
||||
if (p_vector.y < begin.y) {
|
||||
begin.y = p_vector.y;
|
||||
}
|
||||
|
||||
if (p_vector.x > end.x) {
|
||||
end.x = p_vector.x;
|
||||
}
|
||||
if (p_vector.y > end.y) {
|
||||
end.y = p_vector.y;
|
||||
}
|
||||
|
||||
position = begin;
|
||||
size = end - begin;
|
||||
}
|
||||
|
||||
inline Rect2i abs() const {
|
||||
return Rect2i(Point2i(position.x + Math::min(size.x, 0), position.y + Math::min(size.y, 0)), size.abs());
|
||||
}
|
||||
|
||||
inline void set_end(const Vector2i &p_end) {
|
||||
size = p_end - position;
|
||||
}
|
||||
|
||||
inline Vector2i get_end() const {
|
||||
return position + size;
|
||||
}
|
||||
|
||||
operator String() const { return String(position) + ", " + String(size); }
|
||||
|
||||
operator Rect2() const { return Rect2(position, size); }
|
||||
|
||||
Rect2i() {}
|
||||
Rect2i(const Rect2 &p_r2) :
|
||||
position(p_r2.position),
|
||||
size(p_r2.size) {
|
||||
}
|
||||
Rect2i(int p_x, int p_y, int p_width, int p_height) :
|
||||
position(Point2i(p_x, p_y)),
|
||||
size(Size2i(p_width, p_height)) {
|
||||
}
|
||||
Rect2i(const Point2i &p_pos, const Size2i &p_size) :
|
||||
position(p_pos),
|
||||
size(p_size) {
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_RECT2I_HPP
|
||||
216
include/godot_cpp/variant/transform2d.hpp
Normal file
216
include/godot_cpp/variant/transform2d.hpp
Normal file
@@ -0,0 +1,216 @@
|
||||
#ifndef GODOT_TRANSFORM2D_HPP
|
||||
#define GODOT_TRANSFORM2D_HPP
|
||||
|
||||
#include <godot_cpp/core/error_macros.hpp>
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
#include <godot_cpp/variant/packed_vector2_array.hpp>
|
||||
#include <godot_cpp/variant/rect2.hpp>
|
||||
#include <godot_cpp/variant/vector2.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Transform2D {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
// Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
|
||||
// M = (elements[0][0] elements[1][0])
|
||||
// (elements[0][1] elements[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 elements[i].
|
||||
// Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to elements[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.
|
||||
|
||||
Vector2 elements[3];
|
||||
|
||||
inline real_t tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
|
||||
inline real_t tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
|
||||
|
||||
const Vector2 &operator[](int p_idx) const { return elements[p_idx]; }
|
||||
Vector2 &operator[](int p_idx) { return elements[p_idx]; }
|
||||
|
||||
inline Vector2 get_axis(int p_axis) const {
|
||||
ERR_FAIL_INDEX_V(p_axis, 3, Vector2());
|
||||
return elements[p_axis];
|
||||
}
|
||||
inline void set_axis(int p_axis, const Vector2 &p_vec) {
|
||||
ERR_FAIL_INDEX(p_axis, 3);
|
||||
elements[p_axis] = p_vec;
|
||||
}
|
||||
|
||||
void invert();
|
||||
Transform2D inverse() const;
|
||||
|
||||
void affine_invert();
|
||||
Transform2D affine_inverse() const;
|
||||
|
||||
void set_rotation(real_t p_rot);
|
||||
real_t get_rotation() const;
|
||||
real_t get_skew() const;
|
||||
void set_skew(float p_angle);
|
||||
inline void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
|
||||
inline void set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, float p_skew);
|
||||
void rotate(real_t p_phi);
|
||||
|
||||
void scale(const Size2 &p_scale);
|
||||
void scale_basis(const Size2 &p_scale);
|
||||
void translate(real_t p_tx, real_t p_ty);
|
||||
void translate(const Vector2 &p_translation);
|
||||
|
||||
real_t basis_determinant() const;
|
||||
|
||||
Size2 get_scale() const;
|
||||
void set_scale(const Size2 &p_scale);
|
||||
|
||||
inline const Vector2 &get_origin() const { return elements[2]; }
|
||||
inline void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; }
|
||||
|
||||
Transform2D scaled(const Size2 &p_scale) const;
|
||||
Transform2D basis_scaled(const Size2 &p_scale) const;
|
||||
Transform2D translated(const Vector2 &p_offset) const;
|
||||
Transform2D rotated(real_t p_phi) const;
|
||||
|
||||
Transform2D untranslated() const;
|
||||
|
||||
void orthonormalize();
|
||||
Transform2D orthonormalized() const;
|
||||
bool is_equal_approx(const Transform2D &p_transform) const;
|
||||
|
||||
bool operator==(const Transform2D &p_transform) const;
|
||||
bool operator!=(const Transform2D &p_transform) const;
|
||||
|
||||
void operator*=(const Transform2D &p_transform);
|
||||
Transform2D operator*(const Transform2D &p_transform) const;
|
||||
|
||||
Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
|
||||
|
||||
inline Vector2 basis_xform(const Vector2 &p_vec) const;
|
||||
inline Vector2 basis_xform_inv(const Vector2 &p_vec) const;
|
||||
inline Vector2 xform(const Vector2 &p_vec) const;
|
||||
inline Vector2 xform_inv(const Vector2 &p_vec) const;
|
||||
inline Rect2 xform(const Rect2 &p_rect) const;
|
||||
inline Rect2 xform_inv(const Rect2 &p_rect) const;
|
||||
inline PackedVector2Array xform(const PackedVector2Array &p_array) const;
|
||||
inline PackedVector2Array xform_inv(const PackedVector2Array &p_array) const;
|
||||
|
||||
operator String() const;
|
||||
|
||||
Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) {
|
||||
elements[0][0] = xx;
|
||||
elements[0][1] = xy;
|
||||
elements[1][0] = yx;
|
||||
elements[1][1] = yy;
|
||||
elements[2][0] = ox;
|
||||
elements[2][1] = oy;
|
||||
}
|
||||
|
||||
Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) {
|
||||
elements[0] = p_x;
|
||||
elements[1] = p_y;
|
||||
elements[2] = p_origin;
|
||||
}
|
||||
|
||||
Transform2D(real_t p_rot, const Vector2 &p_pos);
|
||||
Transform2D() {
|
||||
elements[0][0] = 1.0;
|
||||
elements[1][1] = 1.0;
|
||||
}
|
||||
};
|
||||
|
||||
Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
|
||||
return Vector2(
|
||||
tdotx(p_vec),
|
||||
tdoty(p_vec));
|
||||
}
|
||||
|
||||
Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const {
|
||||
return Vector2(
|
||||
elements[0].dot(p_vec),
|
||||
elements[1].dot(p_vec));
|
||||
}
|
||||
|
||||
Vector2 Transform2D::xform(const Vector2 &p_vec) const {
|
||||
return Vector2(
|
||||
tdotx(p_vec),
|
||||
tdoty(p_vec)) +
|
||||
elements[2];
|
||||
}
|
||||
|
||||
Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
|
||||
Vector2 v = p_vec - elements[2];
|
||||
|
||||
return Vector2(
|
||||
elements[0].dot(v),
|
||||
elements[1].dot(v));
|
||||
}
|
||||
|
||||
Rect2 Transform2D::xform(const Rect2 &p_rect) const {
|
||||
Vector2 x = elements[0] * p_rect.size.x;
|
||||
Vector2 y = elements[1] * p_rect.size.y;
|
||||
Vector2 pos = xform(p_rect.position);
|
||||
|
||||
Rect2 new_rect;
|
||||
new_rect.position = pos;
|
||||
new_rect.expand_to(pos + x);
|
||||
new_rect.expand_to(pos + y);
|
||||
new_rect.expand_to(pos + x + y);
|
||||
return new_rect;
|
||||
}
|
||||
|
||||
void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
|
||||
elements[0][0] = Math::cos(p_rot) * p_scale.x;
|
||||
elements[1][1] = Math::cos(p_rot) * p_scale.y;
|
||||
elements[1][0] = -Math::sin(p_rot) * p_scale.y;
|
||||
elements[0][1] = Math::sin(p_rot) * p_scale.x;
|
||||
}
|
||||
|
||||
void Transform2D::set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, float p_skew) {
|
||||
elements[0][0] = Math::cos(p_rot) * p_scale.x;
|
||||
elements[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
|
||||
elements[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;
|
||||
elements[0][1] = Math::sin(p_rot) * p_scale.x;
|
||||
}
|
||||
|
||||
Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
|
||||
Vector2 ends[4] = {
|
||||
xform_inv(p_rect.position),
|
||||
xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
|
||||
xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
|
||||
xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y))
|
||||
};
|
||||
|
||||
Rect2 new_rect;
|
||||
new_rect.position = ends[0];
|
||||
new_rect.expand_to(ends[1]);
|
||||
new_rect.expand_to(ends[2]);
|
||||
new_rect.expand_to(ends[3]);
|
||||
|
||||
return new_rect;
|
||||
}
|
||||
|
||||
PackedVector2Array Transform2D::xform(const PackedVector2Array &p_array) const {
|
||||
PackedVector2Array array;
|
||||
array.resize(p_array.size());
|
||||
|
||||
for (int i = 0; i < p_array.size(); ++i) {
|
||||
array[i] = xform(p_array[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
PackedVector2Array Transform2D::xform_inv(const PackedVector2Array &p_array) const {
|
||||
PackedVector2Array array;
|
||||
array.resize(p_array.size());
|
||||
|
||||
for (int i = 0; i < p_array.size(); ++i) {
|
||||
array[i] = xform_inv(p_array[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_TRANSFORM2D_HPP
|
||||
204
include/godot_cpp/variant/transform3d.hpp
Normal file
204
include/godot_cpp/variant/transform3d.hpp
Normal file
@@ -0,0 +1,204 @@
|
||||
#ifndef GODOT_TRANSFORM3D_HPP
|
||||
#define GODOT_TRANSFORM3D_HPP
|
||||
|
||||
#include <godot_cpp/variant/aabb.hpp>
|
||||
#include <godot_cpp/variant/basis.hpp>
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
#include <godot_cpp/variant/packed_vector3_array.hpp>
|
||||
#include <godot_cpp/variant/plane.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Transform3D {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
Basis basis;
|
||||
Vector3 origin;
|
||||
|
||||
void invert();
|
||||
Transform3D inverse() const;
|
||||
|
||||
void affine_invert();
|
||||
Transform3D affine_inverse() const;
|
||||
|
||||
Transform3D rotated(const Vector3 &p_axis, real_t p_phi) const;
|
||||
|
||||
void rotate(const Vector3 &p_axis, real_t p_phi);
|
||||
void rotate_basis(const Vector3 &p_axis, real_t p_phi);
|
||||
|
||||
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 scale(const Vector3 &p_scale);
|
||||
Transform3D scaled(const Vector3 &p_scale) const;
|
||||
void scale_basis(const Vector3 &p_scale);
|
||||
void translate(real_t p_tx, real_t p_ty, real_t p_tz);
|
||||
void translate(const Vector3 &p_translation);
|
||||
Transform3D translated(const Vector3 &p_translation) const;
|
||||
|
||||
const Basis &get_basis() const { return basis; }
|
||||
void set_basis(const Basis &p_basis) { basis = p_basis; }
|
||||
|
||||
const Vector3 &get_origin() const { return origin; }
|
||||
void set_origin(const Vector3 &p_origin) { origin = p_origin; }
|
||||
|
||||
void orthonormalize();
|
||||
Transform3D orthonormalized() const;
|
||||
bool is_equal_approx(const Transform3D &p_transform) const;
|
||||
|
||||
bool operator==(const Transform3D &p_transform) const;
|
||||
bool operator!=(const Transform3D &p_transform) const;
|
||||
|
||||
inline Vector3 xform(const Vector3 &p_vector) const;
|
||||
inline Vector3 xform_inv(const Vector3 &p_vector) const;
|
||||
|
||||
inline Plane xform(const Plane &p_plane) const;
|
||||
inline Plane xform_inv(const Plane &p_plane) const;
|
||||
|
||||
inline AABB xform(const AABB &p_aabb) const;
|
||||
inline AABB xform_inv(const AABB &p_aabb) const;
|
||||
|
||||
inline PackedVector3Array xform(const PackedVector3Array &p_array) const;
|
||||
inline PackedVector3Array xform_inv(const PackedVector3Array &p_array) const;
|
||||
|
||||
void operator*=(const Transform3D &p_transform);
|
||||
Transform3D operator*(const Transform3D &p_transform) const;
|
||||
|
||||
Transform3D interpolate_with(const Transform3D &p_transform, real_t p_c) const;
|
||||
|
||||
inline Transform3D inverse_xform(const Transform3D &t) const {
|
||||
Vector3 v = t.origin - origin;
|
||||
return Transform3D(basis.transpose_xform(t.basis),
|
||||
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;
|
||||
}
|
||||
|
||||
operator String() const;
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
inline Vector3 Transform3D::xform(const Vector3 &p_vector) const {
|
||||
return Vector3(
|
||||
basis[0].dot(p_vector) + origin.x,
|
||||
basis[1].dot(p_vector) + origin.y,
|
||||
basis[2].dot(p_vector) + origin.z);
|
||||
}
|
||||
|
||||
inline Vector3 Transform3D::xform_inv(const Vector3 &p_vector) const {
|
||||
Vector3 v = p_vector - origin;
|
||||
|
||||
return Vector3(
|
||||
(basis.elements[0][0] * v.x) + (basis.elements[1][0] * v.y) + (basis.elements[2][0] * v.z),
|
||||
(basis.elements[0][1] * v.x) + (basis.elements[1][1] * v.y) + (basis.elements[2][1] * v.z),
|
||||
(basis.elements[0][2] * v.x) + (basis.elements[1][2] * v.y) + (basis.elements[2][2] * v.z));
|
||||
}
|
||||
|
||||
inline Plane Transform3D::xform(const Plane &p_plane) const {
|
||||
Vector3 point = p_plane.normal * p_plane.d;
|
||||
Vector3 point_dir = point + p_plane.normal;
|
||||
point = xform(point);
|
||||
point_dir = xform(point_dir);
|
||||
|
||||
Vector3 normal = point_dir - point;
|
||||
normal.normalize();
|
||||
real_t d = normal.dot(point);
|
||||
|
||||
return Plane(normal, d);
|
||||
}
|
||||
|
||||
inline Plane Transform3D::xform_inv(const Plane &p_plane) const {
|
||||
Vector3 point = p_plane.normal * p_plane.d;
|
||||
Vector3 point_dir = point + p_plane.normal;
|
||||
point = xform_inv(point);
|
||||
point_dir = xform_inv(point_dir);
|
||||
|
||||
Vector3 normal = point_dir - point;
|
||||
normal.normalize();
|
||||
real_t d = normal.dot(point);
|
||||
|
||||
return Plane(normal, d);
|
||||
}
|
||||
|
||||
inline AABB Transform3D::xform(const AABB &p_aabb) const {
|
||||
/* http://dev.theomader.com/transform-bounding-boxes/ */
|
||||
Vector3 min = p_aabb.position;
|
||||
Vector3 max = p_aabb.position + p_aabb.size;
|
||||
Vector3 tmin, tmax;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
tmin[i] = tmax[i] = origin[i];
|
||||
for (int j = 0; j < 3; j++) {
|
||||
real_t e = basis[i][j] * min[j];
|
||||
real_t f = basis[i][j] * max[j];
|
||||
if (e < f) {
|
||||
tmin[i] += e;
|
||||
tmax[i] += f;
|
||||
} else {
|
||||
tmin[i] += f;
|
||||
tmax[i] += e;
|
||||
}
|
||||
}
|
||||
}
|
||||
AABB r_aabb;
|
||||
r_aabb.position = tmin;
|
||||
r_aabb.size = tmax - tmin;
|
||||
return r_aabb;
|
||||
}
|
||||
|
||||
inline AABB Transform3D::xform_inv(const AABB &p_aabb) const {
|
||||
/* define vertices */
|
||||
Vector3 vertices[8] = {
|
||||
Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z),
|
||||
Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z),
|
||||
Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z),
|
||||
Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z),
|
||||
Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z),
|
||||
Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z),
|
||||
Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z),
|
||||
Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z)
|
||||
};
|
||||
|
||||
AABB ret;
|
||||
|
||||
ret.position = xform_inv(vertices[0]);
|
||||
|
||||
for (int i = 1; i < 8; i++) {
|
||||
ret.expand_to(xform_inv(vertices[i]));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
PackedVector3Array Transform3D::xform(const PackedVector3Array &p_array) const {
|
||||
PackedVector3Array array;
|
||||
array.resize(p_array.size());
|
||||
|
||||
for (int i = 0; i < p_array.size(); ++i) {
|
||||
array[i] = xform(p_array[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
PackedVector3Array Transform3D::xform_inv(const PackedVector3Array &p_array) const {
|
||||
PackedVector3Array array;
|
||||
array.resize(p_array.size());
|
||||
|
||||
for (int i = 0; i < p_array.size(); ++i) {
|
||||
array[i] = xform_inv(p_array[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_TRANSFORM_HPP
|
||||
236
include/godot_cpp/variant/vector2.hpp
Normal file
236
include/godot_cpp/variant/vector2.hpp
Normal file
@@ -0,0 +1,236 @@
|
||||
#ifndef GODOT_VECTOR2_HPP
|
||||
#define GODOT_VECTOR2_HPP
|
||||
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Vector2i;
|
||||
|
||||
class Vector2 {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
enum Axis {
|
||||
AXIS_X,
|
||||
AXIS_Y,
|
||||
};
|
||||
|
||||
union {
|
||||
real_t x = 0;
|
||||
real_t width;
|
||||
};
|
||||
union {
|
||||
real_t y = 0;
|
||||
real_t height;
|
||||
};
|
||||
|
||||
inline real_t &operator[](int p_idx) {
|
||||
return p_idx ? y : x;
|
||||
}
|
||||
inline const real_t &operator[](int p_idx) const {
|
||||
return p_idx ? y : x;
|
||||
}
|
||||
|
||||
void normalize();
|
||||
Vector2 normalized() const;
|
||||
bool is_normalized() const;
|
||||
|
||||
real_t length() const;
|
||||
real_t length_squared() const;
|
||||
|
||||
Vector2 min(const Vector2 &p_vector2) const {
|
||||
return Vector2(Math::min(x, p_vector2.x), Math::min(y, p_vector2.y));
|
||||
}
|
||||
|
||||
Vector2 max(const Vector2 &p_vector2) const {
|
||||
return Vector2(Math::max(x, p_vector2.x), Math::max(y, p_vector2.y));
|
||||
}
|
||||
|
||||
real_t distance_to(const Vector2 &p_vector2) const;
|
||||
real_t distance_squared_to(const Vector2 &p_vector2) const;
|
||||
real_t angle_to(const Vector2 &p_vector2) const;
|
||||
real_t angle_to_point(const Vector2 &p_vector2) const;
|
||||
inline Vector2 direction_to(const Vector2 &p_to) const;
|
||||
|
||||
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 posmodv(const Vector2 &p_modv) const;
|
||||
Vector2 project(const Vector2 &p_to) const;
|
||||
|
||||
Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
|
||||
|
||||
Vector2 clamped(real_t p_len) const;
|
||||
|
||||
inline Vector2 lerp(const Vector2 &p_to, real_t p_weight) const;
|
||||
inline Vector2 slerp(const Vector2 &p_to, real_t p_weight) const;
|
||||
Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const;
|
||||
Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
|
||||
|
||||
Vector2 slide(const Vector2 &p_normal) const;
|
||||
Vector2 bounce(const Vector2 &p_normal) const;
|
||||
Vector2 reflect(const Vector2 &p_normal) const;
|
||||
|
||||
bool is_equal_approx(const Vector2 &p_v) const;
|
||||
|
||||
Vector2 operator+(const Vector2 &p_v) const;
|
||||
void operator+=(const Vector2 &p_v);
|
||||
Vector2 operator-(const Vector2 &p_v) const;
|
||||
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/(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-() const;
|
||||
|
||||
bool operator==(const Vector2 &p_vec2) const;
|
||||
bool operator!=(const Vector2 &p_vec2) const;
|
||||
|
||||
bool operator<(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y < p_vec2.y) : (x < p_vec2.x); }
|
||||
bool operator>(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y > p_vec2.y) : (x > p_vec2.x); }
|
||||
bool operator<=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
|
||||
bool operator>=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
|
||||
|
||||
real_t angle() const;
|
||||
|
||||
inline Vector2 abs() const {
|
||||
return Vector2(Math::abs(x), Math::abs(y));
|
||||
}
|
||||
|
||||
Vector2 rotated(real_t p_by) const;
|
||||
Vector2 orthogonal() const {
|
||||
return Vector2(y, -x);
|
||||
}
|
||||
|
||||
Vector2 sign() const;
|
||||
Vector2 floor() const;
|
||||
Vector2 ceil() const;
|
||||
Vector2 round() const;
|
||||
Vector2 snapped(const Vector2 &p_by) const;
|
||||
real_t aspect() const { return width / height; }
|
||||
|
||||
operator String() const;
|
||||
|
||||
inline Vector2() {}
|
||||
inline Vector2(real_t p_x, real_t p_y) {
|
||||
x = p_x;
|
||||
y = p_y;
|
||||
}
|
||||
};
|
||||
|
||||
inline Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
|
||||
return p_vec - *this * (dot(p_vec) - p_d);
|
||||
}
|
||||
|
||||
inline Vector2 operator*(float p_scalar, const Vector2 &p_vec) {
|
||||
return p_vec * p_scalar;
|
||||
}
|
||||
|
||||
inline Vector2 operator*(double p_scalar, const Vector2 &p_vec) {
|
||||
return p_vec * p_scalar;
|
||||
}
|
||||
|
||||
inline Vector2 operator*(int32_t p_scalar, const Vector2 &p_vec) {
|
||||
return p_vec * p_scalar;
|
||||
}
|
||||
|
||||
inline Vector2 operator*(int64_t p_scalar, const Vector2 &p_vec) {
|
||||
return p_vec * p_scalar;
|
||||
}
|
||||
|
||||
inline Vector2 Vector2::operator+(const Vector2 &p_v) const {
|
||||
return Vector2(x + p_v.x, y + p_v.y);
|
||||
}
|
||||
|
||||
inline void Vector2::operator+=(const Vector2 &p_v) {
|
||||
x += p_v.x;
|
||||
y += p_v.y;
|
||||
}
|
||||
|
||||
inline Vector2 Vector2::operator-(const Vector2 &p_v) const {
|
||||
return Vector2(x - p_v.x, y - p_v.y);
|
||||
}
|
||||
|
||||
inline void Vector2::operator-=(const Vector2 &p_v) {
|
||||
x -= p_v.x;
|
||||
y -= p_v.y;
|
||||
}
|
||||
|
||||
inline Vector2 Vector2::operator*(const Vector2 &p_v1) const {
|
||||
return Vector2(x * p_v1.x, y * p_v1.y);
|
||||
}
|
||||
|
||||
inline Vector2 Vector2::operator*(const real_t &rvalue) const {
|
||||
return Vector2(x * rvalue, y * rvalue);
|
||||
}
|
||||
|
||||
inline void Vector2::operator*=(const real_t &rvalue) {
|
||||
x *= rvalue;
|
||||
y *= rvalue;
|
||||
}
|
||||
|
||||
inline Vector2 Vector2::operator/(const Vector2 &p_v1) const {
|
||||
return Vector2(x / p_v1.x, y / p_v1.y);
|
||||
}
|
||||
|
||||
inline Vector2 Vector2::operator/(const real_t &rvalue) const {
|
||||
return Vector2(x / rvalue, y / rvalue);
|
||||
}
|
||||
|
||||
inline void Vector2::operator/=(const real_t &rvalue) {
|
||||
x /= rvalue;
|
||||
y /= rvalue;
|
||||
}
|
||||
|
||||
inline Vector2 Vector2::operator-() const {
|
||||
return Vector2(-x, -y);
|
||||
}
|
||||
|
||||
inline bool Vector2::operator==(const Vector2 &p_vec2) const {
|
||||
return x == p_vec2.x && y == p_vec2.y;
|
||||
}
|
||||
|
||||
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, real_t p_weight) const {
|
||||
Vector2 res = *this;
|
||||
|
||||
res.x += (p_weight * (p_to.x - x));
|
||||
res.y += (p_weight * (p_to.y - y));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const {
|
||||
#ifdef MATH_CHECKS
|
||||
ERR_FAIL_COND_V(!is_normalized(), Vector2());
|
||||
#endif
|
||||
real_t theta = angle_to(p_to);
|
||||
return rotated(theta * p_weight);
|
||||
}
|
||||
|
||||
Vector2 Vector2::direction_to(const Vector2 &p_to) const {
|
||||
Vector2 ret(p_to.x - x, p_to.y - y);
|
||||
ret.normalize();
|
||||
return ret;
|
||||
}
|
||||
|
||||
typedef Vector2 Size2;
|
||||
typedef Vector2 Point2;
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_VECTOR2_HPP
|
||||
102
include/godot_cpp/variant/vector2i.hpp
Normal file
102
include/godot_cpp/variant/vector2i.hpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#ifndef GODOT_VECTOR2I_HPP
|
||||
#define GODOT_VECTOR2I_HPP
|
||||
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
#include <godot_cpp/variant/vector2.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Vector2i {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
enum Axis {
|
||||
AXIS_X,
|
||||
AXIS_Y,
|
||||
};
|
||||
|
||||
union {
|
||||
int32_t x = 0;
|
||||
int32_t width;
|
||||
};
|
||||
union {
|
||||
int32_t y = 0;
|
||||
int32_t height;
|
||||
};
|
||||
|
||||
inline int32_t &operator[](int p_idx) {
|
||||
return p_idx ? y : x;
|
||||
}
|
||||
inline const int32_t &operator[](int p_idx) const {
|
||||
return p_idx ? y : x;
|
||||
}
|
||||
|
||||
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/(const Vector2i &p_v1) const;
|
||||
Vector2i operator/(const int32_t &rvalue) const;
|
||||
void operator/=(const int32_t &rvalue);
|
||||
|
||||
Vector2i operator%(const Vector2i &p_v1) const;
|
||||
Vector2i operator%(const int32_t &rvalue) const;
|
||||
void operator%=(const int32_t &rvalue);
|
||||
|
||||
Vector2i operator-() const;
|
||||
bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
|
||||
bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
|
||||
|
||||
bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
|
||||
bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
|
||||
|
||||
bool operator==(const Vector2i &p_vec2) const;
|
||||
bool operator!=(const Vector2i &p_vec2) const;
|
||||
|
||||
real_t aspect() const { return width / (real_t)height; }
|
||||
Vector2i sign() const { return Vector2i(Math::sign(x), Math::sign(y)); }
|
||||
Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
|
||||
|
||||
operator String() const;
|
||||
|
||||
operator Vector2() const { return Vector2(x, y); }
|
||||
|
||||
inline Vector2i() {}
|
||||
inline Vector2i(const Vector2 &p_vec2) {
|
||||
x = (int32_t)p_vec2.x;
|
||||
y = (int32_t)p_vec2.y;
|
||||
}
|
||||
inline Vector2i(int32_t p_x, int32_t p_y) {
|
||||
x = p_x;
|
||||
y = p_y;
|
||||
}
|
||||
};
|
||||
|
||||
inline Vector2i operator*(const int32_t &p_scalar, const Vector2i &p_vector) {
|
||||
return p_vector * p_scalar;
|
||||
}
|
||||
|
||||
inline Vector2i operator*(const int64_t &p_scalar, const Vector2i &p_vector) {
|
||||
return p_vector * p_scalar;
|
||||
}
|
||||
|
||||
inline Vector2i operator*(const float &p_scalar, const Vector2i &p_vector) {
|
||||
return p_vector * p_scalar;
|
||||
}
|
||||
|
||||
inline Vector2i operator*(const double &p_scalar, const Vector2i &p_vector) {
|
||||
return p_vector * p_scalar;
|
||||
}
|
||||
|
||||
typedef Vector2i Size2i;
|
||||
typedef Vector2i Point2i;
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_VECTOR2I_HPP
|
||||
408
include/godot_cpp/variant/vector3.hpp
Normal file
408
include/godot_cpp/variant/vector3.hpp
Normal file
@@ -0,0 +1,408 @@
|
||||
#ifndef GODOT_VECTOR3_HPP
|
||||
#define GODOT_VECTOR3_HPP
|
||||
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Basis;
|
||||
class Vector3i;
|
||||
|
||||
class Vector3 {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
enum Axis {
|
||||
AXIS_X,
|
||||
AXIS_Y,
|
||||
AXIS_Z,
|
||||
};
|
||||
|
||||
union {
|
||||
struct {
|
||||
real_t x;
|
||||
real_t y;
|
||||
real_t z;
|
||||
};
|
||||
|
||||
real_t coord[3] = { 0 };
|
||||
};
|
||||
|
||||
inline const real_t &operator[](int p_axis) const {
|
||||
return coord[p_axis];
|
||||
}
|
||||
|
||||
inline real_t &operator[](int p_axis) {
|
||||
return coord[p_axis];
|
||||
}
|
||||
|
||||
void set_axis(int p_axis, real_t p_value);
|
||||
real_t get_axis(int p_axis) const;
|
||||
|
||||
int min_axis() const;
|
||||
int max_axis() const;
|
||||
|
||||
inline real_t length() const;
|
||||
inline real_t length_squared() const;
|
||||
|
||||
inline void normalize();
|
||||
inline Vector3 normalized() const;
|
||||
inline bool is_normalized() const;
|
||||
inline Vector3 inverse() const;
|
||||
|
||||
inline void zero();
|
||||
|
||||
void snap(Vector3 p_val);
|
||||
Vector3 snapped(Vector3 p_val) const;
|
||||
|
||||
void rotate(const Vector3 &p_axis, real_t p_phi);
|
||||
Vector3 rotated(const Vector3 &p_axis, real_t p_phi) const;
|
||||
|
||||
/* Static Methods between 2 vector3s */
|
||||
|
||||
inline Vector3 lerp(const Vector3 &p_to, real_t p_weight) const;
|
||||
inline Vector3 slerp(const Vector3 &p_to, real_t p_weight) const;
|
||||
Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const;
|
||||
Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const;
|
||||
|
||||
inline Vector3 cross(const Vector3 &p_b) const;
|
||||
inline real_t dot(const Vector3 &p_b) const;
|
||||
Basis outer(const Vector3 &p_b) const;
|
||||
Basis to_diagonal_matrix() const;
|
||||
|
||||
inline Vector3 abs() const;
|
||||
inline Vector3 floor() const;
|
||||
inline Vector3 sign() const;
|
||||
inline Vector3 ceil() const;
|
||||
inline Vector3 round() const;
|
||||
|
||||
inline real_t distance_to(const Vector3 &p_to) const;
|
||||
inline real_t distance_squared_to(const Vector3 &p_to) const;
|
||||
|
||||
inline Vector3 posmod(const real_t p_mod) const;
|
||||
inline Vector3 posmodv(const Vector3 &p_modv) const;
|
||||
inline Vector3 project(const Vector3 &p_to) const;
|
||||
|
||||
inline real_t angle_to(const Vector3 &p_to) const;
|
||||
inline Vector3 direction_to(const Vector3 &p_to) const;
|
||||
|
||||
inline Vector3 slide(const Vector3 &p_normal) const;
|
||||
inline Vector3 bounce(const Vector3 &p_normal) const;
|
||||
inline Vector3 reflect(const Vector3 &p_normal) const;
|
||||
|
||||
bool is_equal_approx(const Vector3 &p_v) const;
|
||||
|
||||
/* Operators */
|
||||
|
||||
inline Vector3 &operator+=(const Vector3 &p_v);
|
||||
inline Vector3 operator+(const Vector3 &p_v) const;
|
||||
inline Vector3 &operator-=(const Vector3 &p_v);
|
||||
inline Vector3 operator-(const Vector3 &p_v) const;
|
||||
inline Vector3 &operator*=(const Vector3 &p_v);
|
||||
inline Vector3 operator*(const Vector3 &p_v) const;
|
||||
inline Vector3 &operator/=(const Vector3 &p_v);
|
||||
inline Vector3 operator/(const Vector3 &p_v) const;
|
||||
|
||||
inline Vector3 &operator*=(real_t p_scalar);
|
||||
inline Vector3 operator*(real_t p_scalar) const;
|
||||
inline Vector3 &operator/=(real_t p_scalar);
|
||||
inline Vector3 operator/(real_t p_scalar) const;
|
||||
|
||||
inline Vector3 operator-() const;
|
||||
|
||||
inline bool operator==(const Vector3 &p_v) const;
|
||||
inline bool operator!=(const Vector3 &p_v) const;
|
||||
inline bool operator<(const Vector3 &p_v) const;
|
||||
inline bool operator<=(const Vector3 &p_v) const;
|
||||
inline bool operator>(const Vector3 &p_v) const;
|
||||
inline bool operator>=(const Vector3 &p_v) const;
|
||||
|
||||
operator String() const;
|
||||
operator Vector3i() const;
|
||||
|
||||
inline Vector3() {}
|
||||
inline Vector3(real_t p_x, real_t p_y, real_t p_z) {
|
||||
x = p_x;
|
||||
y = p_y;
|
||||
z = p_z;
|
||||
}
|
||||
Vector3(const Vector3i &p_ivec);
|
||||
};
|
||||
|
||||
Vector3 Vector3::cross(const Vector3 &p_b) const {
|
||||
Vector3 ret(
|
||||
(y * p_b.z) - (z * p_b.y),
|
||||
(z * p_b.x) - (x * p_b.z),
|
||||
(x * p_b.y) - (y * p_b.x));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
real_t Vector3::dot(const Vector3 &p_b) const {
|
||||
return x * p_b.x + y * p_b.y + z * p_b.z;
|
||||
}
|
||||
|
||||
Vector3 Vector3::abs() const {
|
||||
return Vector3(Math::abs(x), Math::abs(y), Math::abs(z));
|
||||
}
|
||||
|
||||
Vector3 Vector3::sign() const {
|
||||
return Vector3(Math::sign(x), Math::sign(y), Math::sign(z));
|
||||
}
|
||||
|
||||
Vector3 Vector3::floor() const {
|
||||
return Vector3(Math::floor(x), Math::floor(y), Math::floor(z));
|
||||
}
|
||||
|
||||
Vector3 Vector3::ceil() const {
|
||||
return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z));
|
||||
}
|
||||
|
||||
Vector3 Vector3::round() const {
|
||||
return Vector3(Math::round(x), Math::round(y), Math::round(z));
|
||||
}
|
||||
|
||||
Vector3 Vector3::lerp(const Vector3 &p_to, 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::slerp(const Vector3 &p_to, real_t p_weight) const {
|
||||
real_t theta = angle_to(p_to);
|
||||
return rotated(cross(p_to).normalized(), theta * p_weight);
|
||||
}
|
||||
|
||||
real_t Vector3::distance_to(const Vector3 &p_to) const {
|
||||
return (p_to - *this).length();
|
||||
}
|
||||
|
||||
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 {
|
||||
return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
|
||||
}
|
||||
|
||||
Vector3 Vector3::posmodv(const Vector3 &p_modv) const {
|
||||
return Vector3(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z));
|
||||
}
|
||||
|
||||
Vector3 Vector3::project(const Vector3 &p_to) const {
|
||||
return p_to * (dot(p_to) / p_to.length_squared());
|
||||
}
|
||||
|
||||
real_t Vector3::angle_to(const Vector3 &p_to) const {
|
||||
return Math::atan2(cross(p_to).length(), dot(p_to));
|
||||
}
|
||||
|
||||
Vector3 Vector3::direction_to(const Vector3 &p_to) const {
|
||||
Vector3 ret(p_to.x - x, p_to.y - y, p_to.z - z);
|
||||
ret.normalize();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Operators */
|
||||
|
||||
Vector3 &Vector3::operator+=(const Vector3 &p_v) {
|
||||
x += p_v.x;
|
||||
y += p_v.y;
|
||||
z += p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
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 Vector3 &p_v) {
|
||||
x -= p_v.x;
|
||||
y -= p_v.y;
|
||||
z -= p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
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 Vector3 &p_v) {
|
||||
x *= p_v.x;
|
||||
y *= p_v.y;
|
||||
z *= p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
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 Vector3 &p_v) {
|
||||
x /= p_v.x;
|
||||
y /= p_v.y;
|
||||
z /= p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
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*=(real_t p_scalar) {
|
||||
x *= p_scalar;
|
||||
y *= p_scalar;
|
||||
z *= p_scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) {
|
||||
return p_vec * p_scalar;
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator*(real_t p_scalar) const {
|
||||
return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
|
||||
}
|
||||
|
||||
Vector3 &Vector3::operator/=(real_t p_scalar) {
|
||||
x /= p_scalar;
|
||||
y /= p_scalar;
|
||||
z /= p_scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator/(real_t p_scalar) const {
|
||||
return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator-() const {
|
||||
return Vector3(-x, -y, -z);
|
||||
}
|
||||
|
||||
bool Vector3::operator==(const Vector3 &p_v) const {
|
||||
return x == p_v.x && y == p_v.y && z == p_v.z;
|
||||
}
|
||||
|
||||
bool Vector3::operator!=(const Vector3 &p_v) const {
|
||||
return x != p_v.x || y != p_v.y || z != p_v.z;
|
||||
}
|
||||
|
||||
bool Vector3::operator<(const Vector3 &p_v) const {
|
||||
if (x == p_v.x) {
|
||||
if (y == p_v.y) {
|
||||
return z < p_v.z;
|
||||
}
|
||||
return y < p_v.y;
|
||||
}
|
||||
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;
|
||||
}
|
||||
return y > p_v.y;
|
||||
}
|
||||
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;
|
||||
}
|
||||
return y < p_v.y;
|
||||
}
|
||||
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;
|
||||
}
|
||||
return y > p_v.y;
|
||||
}
|
||||
return x > p_v.x;
|
||||
}
|
||||
|
||||
inline Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
|
||||
return p_a.cross(p_b);
|
||||
}
|
||||
|
||||
inline real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) {
|
||||
return p_a.dot(p_b);
|
||||
}
|
||||
|
||||
real_t Vector3::length() const {
|
||||
real_t x2 = x * x;
|
||||
real_t y2 = y * y;
|
||||
real_t z2 = z * z;
|
||||
|
||||
return Math::sqrt(x2 + y2 + z2);
|
||||
}
|
||||
|
||||
real_t Vector3::length_squared() const {
|
||||
real_t x2 = x * x;
|
||||
real_t y2 = y * y;
|
||||
real_t z2 = z * z;
|
||||
|
||||
return x2 + y2 + z2;
|
||||
}
|
||||
|
||||
void Vector3::normalize() {
|
||||
real_t lengthsq = length_squared();
|
||||
if (lengthsq == 0) {
|
||||
x = y = z = 0;
|
||||
} else {
|
||||
real_t length = Math::sqrt(lengthsq);
|
||||
x /= length;
|
||||
y /= length;
|
||||
z /= length;
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 Vector3::normalized() const {
|
||||
Vector3 v = *this;
|
||||
v.normalize();
|
||||
return v;
|
||||
}
|
||||
|
||||
bool Vector3::is_normalized() const {
|
||||
// use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
|
||||
return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON);
|
||||
}
|
||||
|
||||
Vector3 Vector3::inverse() const {
|
||||
return Vector3(1.0 / x, 1.0 / y, 1.0 / z);
|
||||
}
|
||||
|
||||
void Vector3::zero() {
|
||||
x = y = z = 0;
|
||||
}
|
||||
|
||||
// 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(!p_normal.is_normalized(), Vector3());
|
||||
#endif
|
||||
return *this - p_normal * this->dot(p_normal);
|
||||
}
|
||||
|
||||
Vector3 Vector3::bounce(const Vector3 &p_normal) const {
|
||||
return -reflect(p_normal);
|
||||
}
|
||||
|
||||
Vector3 Vector3::reflect(const Vector3 &p_normal) const {
|
||||
#ifdef MATH_CHECKS
|
||||
ERR_FAIL_COND_V(!p_normal.is_normalized(), Vector3());
|
||||
#endif
|
||||
return 2.0 * p_normal * this->dot(p_normal) - *this;
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_VECTOR3_HPP
|
||||
255
include/godot_cpp/variant/vector3i.hpp
Normal file
255
include/godot_cpp/variant/vector3i.hpp
Normal file
@@ -0,0 +1,255 @@
|
||||
#ifndef GODOT_VECTOR3I_HPP
|
||||
#define GODOT_VECTOR3I_HPP
|
||||
|
||||
#include <godot_cpp/core/math.hpp>
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Vector3i {
|
||||
public:
|
||||
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
|
||||
|
||||
enum Axis {
|
||||
AXIS_X,
|
||||
AXIS_Y,
|
||||
AXIS_Z,
|
||||
};
|
||||
|
||||
union {
|
||||
struct {
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
int32_t z;
|
||||
};
|
||||
|
||||
int32_t coord[3] = { 0 };
|
||||
};
|
||||
|
||||
inline const int32_t &operator[](int p_axis) const {
|
||||
return coord[p_axis];
|
||||
}
|
||||
|
||||
inline int32_t &operator[](int p_axis) {
|
||||
return coord[p_axis];
|
||||
}
|
||||
|
||||
void set_axis(int p_axis, int32_t p_value);
|
||||
int32_t get_axis(int p_axis) const;
|
||||
|
||||
int min_axis() const;
|
||||
int max_axis() const;
|
||||
|
||||
inline void zero();
|
||||
|
||||
inline Vector3i abs() const;
|
||||
inline Vector3i sign() const;
|
||||
|
||||
/* Operators */
|
||||
|
||||
inline Vector3i &operator+=(const Vector3i &p_v);
|
||||
inline Vector3i operator+(const Vector3i &p_v) const;
|
||||
inline Vector3i &operator-=(const Vector3i &p_v);
|
||||
inline Vector3i operator-(const Vector3i &p_v) const;
|
||||
inline Vector3i &operator*=(const Vector3i &p_v);
|
||||
inline Vector3i operator*(const Vector3i &p_v) const;
|
||||
inline Vector3i &operator/=(const Vector3i &p_v);
|
||||
inline Vector3i operator/(const Vector3i &p_v) const;
|
||||
inline Vector3i &operator%=(const Vector3i &p_v);
|
||||
inline Vector3i operator%(const Vector3i &p_v) const;
|
||||
|
||||
inline Vector3i &operator*=(int32_t p_scalar);
|
||||
inline Vector3i operator*(int32_t p_scalar) const;
|
||||
inline Vector3i &operator/=(int32_t p_scalar);
|
||||
inline Vector3i operator/(int32_t p_scalar) const;
|
||||
inline Vector3i &operator%=(int32_t p_scalar);
|
||||
inline Vector3i operator%(int32_t p_scalar) const;
|
||||
|
||||
inline Vector3i operator-() const;
|
||||
|
||||
inline bool operator==(const Vector3i &p_v) const;
|
||||
inline bool operator!=(const Vector3i &p_v) const;
|
||||
inline bool operator<(const Vector3i &p_v) const;
|
||||
inline bool operator<=(const Vector3i &p_v) const;
|
||||
inline bool operator>(const Vector3i &p_v) const;
|
||||
inline bool operator>=(const Vector3i &p_v) const;
|
||||
|
||||
operator String() const;
|
||||
|
||||
inline Vector3i() {}
|
||||
inline Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) {
|
||||
x = p_x;
|
||||
y = p_y;
|
||||
z = p_z;
|
||||
}
|
||||
};
|
||||
|
||||
Vector3i Vector3i::abs() const {
|
||||
return Vector3i(Math::abs(x), Math::abs(y), Math::abs(z));
|
||||
}
|
||||
|
||||
Vector3i Vector3i::sign() const {
|
||||
return Vector3i(Math::sign(x), Math::sign(y), Math::sign(z));
|
||||
}
|
||||
|
||||
/* Operators */
|
||||
|
||||
Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
|
||||
x += p_v.x;
|
||||
y += p_v.y;
|
||||
z += p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
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 Vector3i &p_v) {
|
||||
x -= p_v.x;
|
||||
y -= p_v.y;
|
||||
z -= p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
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 Vector3i &p_v) {
|
||||
x *= p_v.x;
|
||||
y *= p_v.y;
|
||||
z *= p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
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 Vector3i &p_v) {
|
||||
x /= p_v.x;
|
||||
y /= p_v.y;
|
||||
z /= p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
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 Vector3i &p_v) {
|
||||
x %= p_v.x;
|
||||
y %= p_v.y;
|
||||
z %= p_v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
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*=(int32_t p_scalar) {
|
||||
x *= p_scalar;
|
||||
y *= p_scalar;
|
||||
z *= p_scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vector3i operator*(int32_t p_scalar, const Vector3i &p_vec) {
|
||||
return p_vec * p_scalar;
|
||||
}
|
||||
|
||||
Vector3i Vector3i::operator*(int32_t p_scalar) const {
|
||||
return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar);
|
||||
}
|
||||
|
||||
Vector3i &Vector3i::operator/=(int32_t p_scalar) {
|
||||
x /= p_scalar;
|
||||
y /= p_scalar;
|
||||
z /= p_scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3i Vector3i::operator/(int32_t p_scalar) const {
|
||||
return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar);
|
||||
}
|
||||
|
||||
Vector3i &Vector3i::operator%=(int32_t p_scalar) {
|
||||
x %= p_scalar;
|
||||
y %= p_scalar;
|
||||
z %= p_scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3i Vector3i::operator%(int32_t p_scalar) const {
|
||||
return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar);
|
||||
}
|
||||
|
||||
Vector3i Vector3i::operator-() const {
|
||||
return Vector3i(-x, -y, -z);
|
||||
}
|
||||
|
||||
bool Vector3i::operator==(const Vector3i &p_v) const {
|
||||
return (x == p_v.x && y == p_v.y && z == p_v.z);
|
||||
}
|
||||
|
||||
bool Vector3i::operator!=(const Vector3i &p_v) const {
|
||||
return (x != p_v.x || y != p_v.y || z != p_v.z);
|
||||
}
|
||||
|
||||
bool Vector3i::operator<(const Vector3i &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 Vector3i::operator>(const Vector3i &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 Vector3i::operator<=(const Vector3i &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 Vector3i::operator>=(const Vector3i &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;
|
||||
}
|
||||
}
|
||||
|
||||
void Vector3i::zero() {
|
||||
x = y = z = 0;
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
|
||||
#endif // GODOT_VECTOR3I_HPP
|
||||
Reference in New Issue
Block a user