summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
Diffstat (limited to 'core/math')
-rw-r--r--core/math/basis.cpp26
-rw-r--r--core/math/basis.h3
-rw-r--r--core/math/expression.cpp12
-rw-r--r--core/math/math_funcs.h4
-rw-r--r--core/math/random_pcg.cpp3
-rw-r--r--core/math/random_pcg.h6
6 files changed, 35 insertions, 19 deletions
diff --git a/core/math/basis.cpp b/core/math/basis.cpp
index 8816e3639a..82b2f7006d 100644
--- a/core/math/basis.cpp
+++ b/core/math/basis.cpp
@@ -557,11 +557,23 @@ void Basis::set_euler_yxz(const Vector3 &p_euler) {
*this = ymat * xmat * zmat;
}
-bool Basis::is_equal_approx(const Basis &a, const Basis &b) const {
+bool Basis::is_equal_approx(const Basis &a, const Basis &b,real_t p_epsilon) const {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
- if (!Math::is_equal_approx_ratio(a.elements[i][j], b.elements[i][j], UNIT_EPSILON))
+ if (!Math::is_equal_approx(a.elements[i][j], b.elements[i][j], p_epsilon))
+ return false;
+ }
+ }
+
+ return true;
+}
+
+bool Basis::is_equal_approx_ratio(const Basis &a, const Basis &b,real_t p_epsilon) const {
+
+ for (int i = 0; i < 3; i++) {
+ for (int j = 0; j < 3; j++) {
+ if (!Math::is_equal_approx_ratio(a.elements[i][j], b.elements[i][j], p_epsilon))
return false;
}
}
@@ -605,12 +617,14 @@ Basis::operator String() const {
Quat Basis::get_quat() const {
+#ifdef MATH_CHECKS
+ if (!is_rotation()) {
+ ERR_EXPLAIN("Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quat() or call orthonormalized() instead.");
+ ERR_FAIL_V(Quat());
+ }
+#endif
/* Allow getting a quaternion from an unnormalized transform */
Basis m = *this;
- m.elements[0].normalize();
- m.elements[1].normalize();
- m.elements[2].normalize();
-
real_t trace = m.elements[0][0] + m.elements[1][1] + m.elements[2][2];
real_t temp[4];
diff --git a/core/math/basis.h b/core/math/basis.h
index 128e56b494..aa0ddb280f 100644
--- a/core/math/basis.h
+++ b/core/math/basis.h
@@ -133,7 +133,8 @@ public:
return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2];
}
- bool is_equal_approx(const Basis &a, const Basis &b) const;
+ bool is_equal_approx(const Basis &a, const Basis &b, real_t p_epsilon=CMP_EPSILON) const;
+ bool is_equal_approx_ratio(const Basis &a, const Basis &b, real_t p_epsilon=UNIT_EPSILON) const;
bool operator==(const Basis &p_matrix) const;
bool operator!=(const Basis &p_matrix) const;
diff --git a/core/math/expression.cpp b/core/math/expression.cpp
index 99251d80e3..708054e4ab 100644
--- a/core/math/expression.cpp
+++ b/core/math/expression.cpp
@@ -164,10 +164,10 @@ int Expression::get_func_argument_count(BuiltinFunc p_func) {
case TEXT_PRINTRAW:
case VAR_TO_STR:
case STR_TO_VAR:
- case VAR_TO_BYTES:
- case BYTES_TO_VAR:
case TYPE_EXISTS:
return 1;
+ case VAR_TO_BYTES:
+ case BYTES_TO_VAR:
case MATH_ATAN2:
case MATH_FMOD:
case MATH_FPOSMOD:
@@ -696,8 +696,9 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant
case VAR_TO_BYTES: {
PoolByteArray barr;
+ bool full_objects = *p_inputs[1];
int len;
- Error err = encode_variant(*p_inputs[0], NULL, len);
+ Error err = encode_variant(*p_inputs[0], NULL, len, full_objects);
if (err) {
r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
@@ -709,7 +710,7 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant
barr.resize(len);
{
PoolByteArray::Write w = barr.write();
- encode_variant(*p_inputs[0], w.ptr(), len);
+ encode_variant(*p_inputs[0], w.ptr(), len, full_objects);
}
*r_return = barr;
} break;
@@ -724,10 +725,11 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant
}
PoolByteArray varr = *p_inputs[0];
+ bool allow_objects = *p_inputs[1];
Variant ret;
{
PoolByteArray::Read r = varr.read();
- Error err = decode_variant(ret, r.ptr(), varr.size(), NULL);
+ Error err = decode_variant(ret, r.ptr(), varr.size(), NULL, allow_objects);
if (err != OK) {
r_error_str = RTR("Not enough bytes for decoding bytes, or invalid format.");
r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h
index 17112d8940..6ac6839827 100644
--- a/core/math/math_funcs.h
+++ b/core/math/math_funcs.h
@@ -249,11 +249,11 @@ public:
static float random(float from, float to);
static real_t random(int from, int to) { return (real_t)random((real_t)from, (real_t)to); }
- static _ALWAYS_INLINE_ bool is_equal_approx_ratio(real_t a, real_t b, real_t epsilon = CMP_EPSILON) {
+ static _ALWAYS_INLINE_ bool is_equal_approx_ratio(real_t a, real_t b, real_t epsilon = CMP_EPSILON, real_t min_epsilon = CMP_EPSILON) {
// this is an approximate way to check that numbers are close, as a ratio of their average size
// helps compare approximate numbers that may be very big or very small
real_t diff = abs(a - b);
- if (diff == 0.0) {
+ if (diff == 0.0 || diff < min_epsilon) {
return true;
}
real_t avg_size = (abs(a) + abs(b)) / 2.0;
diff --git a/core/math/random_pcg.cpp b/core/math/random_pcg.cpp
index 45467b32b2..8351bd138e 100644
--- a/core/math/random_pcg.cpp
+++ b/core/math/random_pcg.cpp
@@ -34,8 +34,7 @@
RandomPCG::RandomPCG(uint64_t p_seed, uint64_t p_inc) :
pcg(),
- current_seed(DEFAULT_SEED) {
- pcg.inc = p_inc;
+ current_inc(p_inc) {
seed(p_seed);
}
diff --git a/core/math/random_pcg.h b/core/math/random_pcg.h
index 230eb9a11b..cd721ef4d1 100644
--- a/core/math/random_pcg.h
+++ b/core/math/random_pcg.h
@@ -38,18 +38,18 @@
class RandomPCG {
pcg32_random_t pcg;
uint64_t current_seed; // seed with this to get the same state
+ uint64_t current_inc;
public:
static const uint64_t DEFAULT_SEED = 12047754176567800795U;
static const uint64_t DEFAULT_INC = PCG_DEFAULT_INC_64;
static const uint64_t RANDOM_MAX = 0xFFFFFFFF;
- RandomPCG(uint64_t p_seed = DEFAULT_SEED, uint64_t p_inc = PCG_DEFAULT_INC_64);
+ RandomPCG(uint64_t p_seed = DEFAULT_SEED, uint64_t p_inc = DEFAULT_INC);
_FORCE_INLINE_ void seed(uint64_t p_seed) {
current_seed = p_seed;
- pcg.state = p_seed;
- pcg32_random_r(&pcg); // Force changing internal state to avoid initial 0
+ pcg32_srandom_r(&pcg, current_seed, current_inc);
}
_FORCE_INLINE_ uint64_t get_seed() { return current_seed; }