summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
Diffstat (limited to 'core/math')
-rw-r--r--core/math/aabb.cpp16
-rw-r--r--core/math/aabb.h4
-rw-r--r--core/math/geometry_3d.h48
-rw-r--r--core/math/plane.cpp26
-rw-r--r--core/math/plane.h7
-rw-r--r--core/math/random_number_generator.h18
-rw-r--r--core/math/random_pcg.h4
-rw-r--r--core/math/rect2.h8
-rw-r--r--core/math/vector2.h8
9 files changed, 85 insertions, 54 deletions
diff --git a/core/math/aabb.cpp b/core/math/aabb.cpp
index f5c667dab0..e868ebc7c8 100644
--- a/core/math/aabb.cpp
+++ b/core/math/aabb.cpp
@@ -31,6 +31,7 @@
#include "aabb.h"
#include "core/print_string.h"
+#include "core/variant.h"
real_t AABB::get_area() const {
return size.x * size.y * size.z;
@@ -375,6 +376,21 @@ void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const {
}
}
+Variant AABB::intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const {
+ Vector3 inters;
+ if (intersects_segment(p_from, p_to, &inters)) {
+ return inters;
+ }
+ return Variant();
+}
+Variant AABB::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const {
+ Vector3 inters;
+ if (intersects_ray(p_from, p_dir, &inters)) {
+ return inters;
+ }
+ return Variant();
+}
+
AABB::operator String() const {
return String() + position + " - " + size;
}
diff --git a/core/math/aabb.h b/core/math/aabb.h
index bd1f3a1a36..8c08754e1c 100644
--- a/core/math/aabb.h
+++ b/core/math/aabb.h
@@ -39,6 +39,7 @@
* AABB / AABB (Axis Aligned Bounding Box)
* This is implemented by a point (position) and the box size
*/
+class Variant;
class AABB {
public:
@@ -103,6 +104,9 @@ public:
return AABB(Vector3(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0), position.z + MIN(size.z, 0)), size.abs());
}
+ Variant intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const;
+ Variant intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const;
+
operator String() const;
_FORCE_INLINE_ AABB() {}
diff --git a/core/math/geometry_3d.h b/core/math/geometry_3d.h
index 6bbf518141..11cac8f108 100644
--- a/core/math/geometry_3d.h
+++ b/core/math/geometry_3d.h
@@ -636,54 +636,6 @@ public:
void optimize_vertices();
};
- _FORCE_INLINE_ static int get_uv84_normal_bit(const Vector3 &p_vector) {
- int lat = Math::fast_ftoi(Math::floor(Math::acos(p_vector.dot(Vector3(0, 1, 0))) * 4.0 / Math_PI + 0.5));
-
- if (lat == 0) {
- return 24;
- } else if (lat == 4) {
- return 25;
- }
-
- int lon = Math::fast_ftoi(Math::floor((Math_PI + Math::atan2(p_vector.x, p_vector.z)) * 8.0 / (Math_PI * 2.0) + 0.5)) % 8;
-
- return lon + (lat - 1) * 8;
- }
-
- _FORCE_INLINE_ static int get_uv84_normal_bit_neighbors(int p_idx) {
- if (p_idx == 24) {
- return 1 | 2 | 4 | 8;
- } else if (p_idx == 25) {
- return (1 << 23) | (1 << 22) | (1 << 21) | (1 << 20);
- } else {
- int ret = 0;
- if ((p_idx % 8) == 0) {
- ret |= (1 << (p_idx + 7));
- } else {
- ret |= (1 << (p_idx - 1));
- }
- if ((p_idx % 8) == 7) {
- ret |= (1 << (p_idx - 7));
- } else {
- ret |= (1 << (p_idx + 1));
- }
-
- int mask = ret | (1 << p_idx);
- if (p_idx < 8) {
- ret |= 24;
- } else {
- ret |= mask >> 8;
- }
-
- if (p_idx >= 16) {
- ret |= 25;
- } else {
- ret |= mask << 8;
- }
-
- return ret;
- }
- }
static MeshData build_convex_mesh(const Vector<Plane> &p_planes);
static Vector<Plane> build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis = Vector3::AXIS_Z);
static Vector<Plane> build_box_planes(const Vector3 &p_extents);
diff --git a/core/math/plane.cpp b/core/math/plane.cpp
index 4200484c59..ae2021d2f6 100644
--- a/core/math/plane.cpp
+++ b/core/math/plane.cpp
@@ -31,6 +31,7 @@
#include "plane.h"
#include "core/math/math_funcs.h"
+#include "core/variant.h"
void Plane::set_normal(const Vector3 &p_normal) {
normal = p_normal;
@@ -138,6 +139,31 @@ bool Plane::intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vec
return true;
}
+Variant Plane::intersect_3_bind(const Plane &p_plane1, const Plane &p_plane2) const {
+ Vector3 inters;
+ if (intersect_3(p_plane1, p_plane2, &inters)) {
+ return inters;
+ } else {
+ return Variant();
+ }
+}
+Variant Plane::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const {
+ Vector3 inters;
+ if (intersects_ray(p_from, p_dir, &inters)) {
+ return inters;
+ } else {
+ return Variant();
+ }
+}
+Variant Plane::intersects_segment_bind(const Vector3 &p_begin, const Vector3 &p_end) const {
+ Vector3 inters;
+ if (intersects_segment(p_begin, p_end, &inters)) {
+ return inters;
+ } else {
+ return Variant();
+ }
+}
+
/* misc */
bool Plane::is_equal_approx_any_side(const Plane &p_plane) const {
diff --git a/core/math/plane.h b/core/math/plane.h
index 70a6111edd..1386b0a2cb 100644
--- a/core/math/plane.h
+++ b/core/math/plane.h
@@ -33,6 +33,8 @@
#include "core/math/vector3.h"
+class Variant;
+
class Plane {
public:
Vector3 normal;
@@ -59,6 +61,11 @@ public:
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;
+ // For Variant bindings.
+ Variant intersect_3_bind(const Plane &p_plane1, const Plane &p_plane2) const;
+ Variant intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const;
+ Variant intersects_segment_bind(const Vector3 &p_begin, const Vector3 &p_end) const;
+
_FORCE_INLINE_ Vector3 project(const Vector3 &p_point) const {
return p_point - normal * distance_to(p_point);
}
diff --git a/core/math/random_number_generator.h b/core/math/random_number_generator.h
index 920308e597..2e7941b345 100644
--- a/core/math/random_number_generator.h
+++ b/core/math/random_number_generator.h
@@ -37,9 +37,9 @@
class RandomNumberGenerator : public Reference {
GDCLASS(RandomNumberGenerator, Reference);
+protected:
RandomPCG randbase;
-protected:
static void _bind_methods();
public:
@@ -58,12 +58,18 @@ public:
_FORCE_INLINE_ real_t randfn(real_t mean = 0.0, real_t deviation = 1.0) { return randbase.randfn(mean, deviation); }
_FORCE_INLINE_ int randi_range(int from, int to) {
- unsigned int ret = randbase.rand();
- if (to < from) {
- return ret % (from - to + 1) + to;
- } else {
- return ret % (to - from + 1) + from;
+ int range;
+ int min;
+ if (to > from) {
+ range = to - from + 1;
+ min = from;
+ } else if (to < from) {
+ range = from - to + 1;
+ min = to;
+ } else { // from == to
+ return from;
}
+ return randbase.rand(range) + min;
}
RandomNumberGenerator() {}
diff --git a/core/math/random_pcg.h b/core/math/random_pcg.h
index 09b13ab74d..dfdae53eed 100644
--- a/core/math/random_pcg.h
+++ b/core/math/random_pcg.h
@@ -81,6 +81,10 @@ public:
current_seed = pcg.state;
return pcg32_random_r(&pcg);
}
+ _FORCE_INLINE_ uint32_t rand(uint32_t bounds) {
+ current_seed = pcg.state;
+ return pcg32_boundedrand_r(&pcg, bounds);
+ }
// Obtaining floating point numbers in [0, 1] range with "good enough" uniformity.
// These functions sample the output of rand() as the fraction part of an infinite binary number,
diff --git a/core/math/rect2.h b/core/math/rect2.h
index 14393325ec..7660db71eb 100644
--- a/core/math/rect2.h
+++ b/core/math/rect2.h
@@ -197,6 +197,10 @@ struct Rect2 {
return g;
}
+ inline Rect2 grow_margin_bind(uint32_t p_margin, real_t p_amount) const {
+ return grow_margin(Margin(p_margin), 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;
@@ -363,6 +367,10 @@ struct Rect2i {
return g;
}
+ inline Rect2i grow_margin_bind(uint32_t p_margin, int p_amount) const {
+ return grow_margin(Margin(p_margin), 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;
diff --git a/core/math/vector2.h b/core/math/vector2.h
index f41bcc15bc..c2a2656e72 100644
--- a/core/math/vector2.h
+++ b/core/math/vector2.h
@@ -65,6 +65,14 @@ struct Vector2 {
real_t length() const;
real_t length_squared() const;
+ Vector2 min(const Vector2 &p_vector2) const {
+ return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
+ }
+
+ Vector2 max(const Vector2 &p_vector2) const {
+ return Vector2(MAX(x, p_vector2.x), 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;