summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
Diffstat (limited to 'core/math')
-rw-r--r--core/math/geometry.h15
-rw-r--r--core/math/math_defs.h4
-rw-r--r--core/math/math_funcs.cpp25
-rw-r--r--core/math/math_funcs.h3
-rw-r--r--core/math/random_number_generator.cpp45
-rw-r--r--core/math/random_number_generator.h61
-rw-r--r--core/math/random_pcg.cpp55
-rw-r--r--core/math/random_pcg.h61
8 files changed, 250 insertions, 19 deletions
diff --git a/core/math/geometry.h b/core/math/geometry.h
index a813a90774..df63f0dabe 100644
--- a/core/math/geometry.h
+++ b/core/math/geometry.h
@@ -800,6 +800,21 @@ public:
return Vector<Vector<Vector2> >();
}
+ static bool is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
+ int c = p_polygon.size();
+ if (c < 3)
+ return false;
+ const Vector2 *p = p_polygon.ptr();
+ real_t sum = 0;
+ for (int i = 0; i < c; i++) {
+ const Vector2 &v1 = p[i];
+ const Vector2 &v2 = p[(i + 1) % c];
+ sum += (v2.x - v1.x) * (v2.y + v1.y);
+ }
+
+ return sum > 0.0f;
+ }
+
static PoolVector<PoolVector<Face3> > separate_objects(PoolVector<Face3> p_array);
static PoolVector<Face3> wrap_geometry(PoolVector<Face3> p_array, real_t *p_error = NULL); ///< create a "wrap" that encloses the given geometry
diff --git a/core/math/math_defs.h b/core/math/math_defs.h
index a5feee6eb5..db9055cee2 100644
--- a/core/math/math_defs.h
+++ b/core/math/math_defs.h
@@ -93,9 +93,9 @@ enum Corner {
};
/**
- * The "Real" type is an abstract type used for real numbers, such as 1.5,
+ * The "Real" type is an abstract type used for real numbers, such as 1.5,
* in contrast to integer numbers. Precision can be controlled with the
- * presence or absence of the REAL_T_IS_DOUBLE define.
+ * presence or absence of the REAL_T_IS_DOUBLE define.
*/
#ifdef REAL_T_IS_DOUBLE
typedef double real_t;
diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp
index 0c06d2a2b5..06355d15ed 100644
--- a/core/math/math_funcs.cpp
+++ b/core/math/math_funcs.cpp
@@ -30,30 +30,27 @@
#include "math_funcs.h"
-#include "core/os/os.h"
-
-pcg32_random_t Math::default_pcg = { 12047754176567800795ULL, PCG_DEFAULT_INC_64 };
+RandomPCG Math::default_rand(RandomPCG::DEFAULT_SEED, RandomPCG::DEFAULT_INC);
#define PHI 0x9e3779b9
-// TODO: we should eventually expose pcg.inc too
uint32_t Math::rand_from_seed(uint64_t *seed) {
- pcg32_random_t pcg = { *seed, PCG_DEFAULT_INC_64 };
- uint32_t r = pcg32_random_r(&pcg);
- *seed = pcg.state;
+ RandomPCG rng = RandomPCG(*seed, RandomPCG::DEFAULT_INC);
+ uint32_t r = rng.rand();
+ *seed = rng.get_seed();
return r;
}
void Math::seed(uint64_t x) {
- default_pcg.state = x;
+ default_rand.seed(x);
}
void Math::randomize() {
- seed(OS::get_singleton()->get_ticks_usec() * default_pcg.state + PCG_DEFAULT_INC_64);
+ default_rand.randomize();
}
uint32_t Math::rand() {
- return pcg32_random_r(&default_pcg);
+ return default_rand.rand();
}
int Math::step_decimals(double p_step) {
@@ -169,13 +166,9 @@ uint32_t Math::larger_prime(uint32_t p_val) {
}
double Math::random(double from, double to) {
- unsigned int r = Math::rand();
- double ret = (double)r / (double)RANDOM_MAX;
- return (ret) * (to - from) + from;
+ return default_rand.random(from, to);
}
float Math::random(float from, float to) {
- unsigned int r = Math::rand();
- float ret = (float)r / (float)RANDOM_MAX;
- return (ret) * (to - from) + from;
+ return default_rand.random(from, to);
}
diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h
index 65c318448c..f9d89d5d5a 100644
--- a/core/math/math_funcs.h
+++ b/core/math/math_funcs.h
@@ -32,6 +32,7 @@
#define MATH_FUNCS_H
#include "core/math/math_defs.h"
+#include "core/math/random_pcg.h"
#include "core/typedefs.h"
#include "thirdparty/misc/pcg.h"
@@ -41,7 +42,7 @@
class Math {
- static pcg32_random_t default_pcg;
+ static RandomPCG default_rand;
public:
Math() {} // useless to instance
diff --git a/core/math/random_number_generator.cpp b/core/math/random_number_generator.cpp
new file mode 100644
index 0000000000..e4ec0dac99
--- /dev/null
+++ b/core/math/random_number_generator.cpp
@@ -0,0 +1,45 @@
+/*************************************************************************/
+/* random_number_generator.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#include "random_number_generator.h"
+
+RandomNumberGenerator::RandomNumberGenerator() :
+ randbase() {}
+
+void RandomNumberGenerator::_bind_methods() {
+ ClassDB::bind_method(D_METHOD("set_seed", "seed"), &RandomNumberGenerator::set_seed);
+ ClassDB::bind_method(D_METHOD("get_seed"), &RandomNumberGenerator::get_seed);
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "seed"), "set_seed", "get_seed");
+
+ ClassDB::bind_method(D_METHOD("randi"), &RandomNumberGenerator::randi);
+ ClassDB::bind_method(D_METHOD("randf"), &RandomNumberGenerator::randf);
+ ClassDB::bind_method(D_METHOD("rand_range", "from", "to"), &RandomNumberGenerator::rand_range);
+ ClassDB::bind_method(D_METHOD("randomize"), &RandomNumberGenerator::randomize);
+}
diff --git a/core/math/random_number_generator.h b/core/math/random_number_generator.h
new file mode 100644
index 0000000000..557863fdbd
--- /dev/null
+++ b/core/math/random_number_generator.h
@@ -0,0 +1,61 @@
+/*************************************************************************/
+/* random_number_generator.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#ifndef RANDOM_NUMBER_GENERATOR_H
+#define RANDOM_NUMBER_GENERATOR_H
+
+#include "core/math/random_pcg.h"
+#include "core/reference.h"
+
+class RandomNumberGenerator : public Reference {
+ GDCLASS(RandomNumberGenerator, Reference);
+
+ RandomPCG randbase;
+
+protected:
+ static void _bind_methods();
+
+public:
+ _FORCE_INLINE_ void set_seed(uint64_t seed) { randbase.seed(seed); }
+
+ _FORCE_INLINE_ uint64_t get_seed() { return randbase.get_seed(); }
+
+ _FORCE_INLINE_ void randomize() { return randbase.randomize(); }
+
+ _FORCE_INLINE_ uint32_t randi() { return randbase.rand(); }
+
+ _FORCE_INLINE_ real_t randf() { return randbase.randf(); }
+
+ _FORCE_INLINE_ real_t rand_range(real_t from, real_t to) { return randbase.random(from, to); }
+
+ RandomNumberGenerator();
+};
+
+#endif // RANDOM_NUMBER_GENERATOR_H
diff --git a/core/math/random_pcg.cpp b/core/math/random_pcg.cpp
new file mode 100644
index 0000000000..16899f79da
--- /dev/null
+++ b/core/math/random_pcg.cpp
@@ -0,0 +1,55 @@
+/*************************************************************************/
+/* random_pcg.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#include "random_pcg.h"
+
+#include "core/os/os.h"
+
+RandomPCG::RandomPCG(uint64_t seed, uint64_t inc) :
+ pcg() {
+ pcg.state = seed;
+ pcg.inc = inc;
+}
+
+void RandomPCG::randomize() {
+ seed(OS::get_singleton()->get_ticks_usec() * pcg.state + PCG_DEFAULT_INC_64);
+}
+
+double RandomPCG::random(double from, double to) {
+ unsigned int r = rand();
+ double ret = (double)r / (double)RANDOM_MAX;
+ return (ret) * (to - from) + from;
+}
+
+float RandomPCG::random(float from, float to) {
+ unsigned int r = rand();
+ float ret = (float)r / (float)RANDOM_MAX;
+ return (ret) * (to - from) + from;
+}
diff --git a/core/math/random_pcg.h b/core/math/random_pcg.h
new file mode 100644
index 0000000000..4a43c36ede
--- /dev/null
+++ b/core/math/random_pcg.h
@@ -0,0 +1,61 @@
+/*************************************************************************/
+/* random_pcg.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#ifndef RANDOM_PCG_H
+#define RANDOM_PCG_H
+
+#include "core/math/math_defs.h"
+
+#include "thirdparty/misc/pcg.h"
+
+class RandomPCG {
+ pcg32_random_t pcg;
+
+public:
+ static const uint64_t DEFAULT_SEED = 12047754176567800795ULL;
+ static const uint64_t DEFAULT_INC = PCG_DEFAULT_INC_64;
+ static const uint64_t RANDOM_MAX = 4294967295;
+
+ RandomPCG(uint64_t seed = DEFAULT_SEED, uint64_t inc = PCG_DEFAULT_INC_64);
+
+ _FORCE_INLINE_ void seed(uint64_t seed) { pcg.state = seed; }
+ _FORCE_INLINE_ uint64_t get_seed() { return pcg.state; }
+
+ void randomize();
+ _FORCE_INLINE_ uint32_t rand() { return pcg32_random_r(&pcg); }
+ _FORCE_INLINE_ double randf() { return (double)rand() / (double)RANDOM_MAX; }
+ _FORCE_INLINE_ float randd() { return (float)rand() / (float)RANDOM_MAX; }
+
+ double random(double from, double to);
+ float random(float from, float to);
+ real_t random(int from, int to) { return (real_t)random((real_t)from, (real_t)to); }
+};
+
+#endif // RANDOM_PCG_H