| 1 | /** |
| 2 | * Copyright (c) 2006-2023 LOVE Development Team |
| 3 | * |
| 4 | * This software is provided 'as-is', without any express or implied |
| 5 | * warranty. In no event will the authors be held liable for any damages |
| 6 | * arising from the use of this software. |
| 7 | * |
| 8 | * Permission is granted to anyone to use this software for any purpose, |
| 9 | * including commercial applications, and to alter it and redistribute it |
| 10 | * freely, subject to the following restrictions: |
| 11 | * |
| 12 | * 1. The origin of this software must not be misrepresented; you must not |
| 13 | * claim that you wrote the original software. If you use this software |
| 14 | * in a product, an acknowledgment in the product documentation would be |
| 15 | * appreciated but is not required. |
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 17 | * misrepresented as being the original software. |
| 18 | * 3. This notice may not be removed or altered from any source distribution. |
| 19 | **/ |
| 20 | |
| 21 | #ifndef LOVE_MATH_MODMATH_H |
| 22 | #define LOVE_MATH_MODMATH_H |
| 23 | |
| 24 | #include "RandomGenerator.h" |
| 25 | |
| 26 | // LOVE |
| 27 | #include "common/Module.h" |
| 28 | #include "common/math.h" |
| 29 | #include "common/Vector.h" |
| 30 | #include "common/int.h" |
| 31 | |
| 32 | // Noise |
| 33 | #include "libraries/noise1234/noise1234.h" |
| 34 | #include "libraries/noise1234/simplexnoise1234.h" |
| 35 | |
| 36 | // STL |
| 37 | #include <vector> |
| 38 | |
| 39 | namespace love |
| 40 | { |
| 41 | namespace math |
| 42 | { |
| 43 | |
| 44 | class BezierCurve; |
| 45 | class Transform; |
| 46 | |
| 47 | struct Triangle |
| 48 | { |
| 49 | Triangle(const Vector2 &x, const Vector2 &y, const Vector2 &z) |
| 50 | : a(x), b(y), c(z) |
| 51 | {} |
| 52 | Vector2 a, b, c; |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * Triangulate a simple polygon. |
| 57 | * |
| 58 | * @param polygon Polygon to triangulate. Must not intersect itself. |
| 59 | * @return List of triangles the polygon is composed of. |
| 60 | **/ |
| 61 | std::vector<Triangle> triangulate(const std::vector<love::Vector2> &polygon); |
| 62 | |
| 63 | /** |
| 64 | * Checks whether a polygon is convex. |
| 65 | * |
| 66 | * @param polygon Polygon to test. |
| 67 | * @return True if the polygon is convex, false otherwise. |
| 68 | **/ |
| 69 | bool isConvex(const std::vector<love::Vector2> &polygon); |
| 70 | |
| 71 | /** |
| 72 | * Converts a value from the sRGB (gamma) colorspace to linear RGB. |
| 73 | **/ |
| 74 | float gammaToLinear(float c); |
| 75 | |
| 76 | /** |
| 77 | * Converts a value from linear RGB to the sRGB (gamma) colorspace. |
| 78 | **/ |
| 79 | float linearToGamma(float c); |
| 80 | |
| 81 | /** |
| 82 | * Calculate noise for the specified coordinate(s). |
| 83 | * |
| 84 | * @return Noise value in the range of [0, 1]. |
| 85 | **/ |
| 86 | static float noise1(float x); |
| 87 | static float noise2(float x, float y); |
| 88 | static float noise3(float x, float y, float z); |
| 89 | static float noise4(float x, float y, float z, float w); |
| 90 | |
| 91 | |
| 92 | class Math : public Module |
| 93 | { |
| 94 | public: |
| 95 | |
| 96 | Math(); |
| 97 | virtual ~Math(); |
| 98 | |
| 99 | RandomGenerator *getRandomGenerator() |
| 100 | { |
| 101 | return &rng; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Create a new random number generator. |
| 106 | **/ |
| 107 | RandomGenerator *newRandomGenerator(); |
| 108 | |
| 109 | /** |
| 110 | * Creates a new bezier curve. |
| 111 | **/ |
| 112 | BezierCurve *newBezierCurve(const std::vector<Vector2> &points); |
| 113 | |
| 114 | Transform *newTransform(); |
| 115 | Transform *newTransform(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky); |
| 116 | |
| 117 | // Implements Module. |
| 118 | virtual ModuleType getModuleType() const |
| 119 | { |
| 120 | return M_MATH; |
| 121 | } |
| 122 | |
| 123 | virtual const char *getName() const |
| 124 | { |
| 125 | return "love.math" ; |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | |
| 130 | RandomGenerator rng; |
| 131 | |
| 132 | }; // Math |
| 133 | |
| 134 | |
| 135 | static inline float noise1(float x) |
| 136 | { |
| 137 | return SimplexNoise1234::noise(x) * 0.5f + 0.5f; |
| 138 | } |
| 139 | |
| 140 | static inline float noise2(float x, float y) |
| 141 | { |
| 142 | return SimplexNoise1234::noise(x, y) * 0.5f + 0.5f; |
| 143 | } |
| 144 | |
| 145 | // Perlin noise is used instead of Simplex noise in the 3D and 4D cases to avoid |
| 146 | // patent issues. |
| 147 | |
| 148 | static inline float noise3(float x, float y, float z) |
| 149 | { |
| 150 | return Noise1234::noise(x, y, z) * 0.5f + 0.5f; |
| 151 | } |
| 152 | |
| 153 | static inline float noise4(float x, float y, float z, float w) |
| 154 | { |
| 155 | return Noise1234::noise(x, y, z, w) * 0.5f + 0.5f; |
| 156 | } |
| 157 | |
| 158 | } // math |
| 159 | } // love |
| 160 | |
| 161 | #endif |
| 162 | |