MathModule.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Copyright (c) 2006-2022 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. #ifndef LOVE_MATH_MODMATH_H
  21. #define LOVE_MATH_MODMATH_H
  22. #include "RandomGenerator.h"
  23. // LOVE
  24. #include "common/Module.h"
  25. #include "common/math.h"
  26. #include "common/Vector.h"
  27. #include "common/int.h"
  28. // Noise
  29. #include "libraries/noise1234/noise1234.h"
  30. #include "libraries/noise1234/simplexnoise1234.h"
  31. // STL
  32. #include <vector>
  33. namespace love
  34. {
  35. namespace math
  36. {
  37. class BezierCurve;
  38. class Transform;
  39. struct Triangle
  40. {
  41. Triangle(const Vector2 &x, const Vector2 &y, const Vector2 &z)
  42. : a(x), b(y), c(z)
  43. {}
  44. Vector2 a, b, c;
  45. };
  46. /**
  47. * Triangulate a simple polygon.
  48. *
  49. * @param polygon Polygon to triangulate. Must not intersect itself.
  50. * @return List of triangles the polygon is composed of.
  51. **/
  52. std::vector<Triangle> triangulate(const std::vector<love::Vector2> &polygon);
  53. /**
  54. * Checks whether a polygon is convex.
  55. *
  56. * @param polygon Polygon to test.
  57. * @return True if the polygon is convex, false otherwise.
  58. **/
  59. bool isConvex(const std::vector<love::Vector2> &polygon);
  60. /**
  61. * Converts a value from the sRGB (gamma) colorspace to linear RGB.
  62. **/
  63. float gammaToLinear(float c);
  64. /**
  65. * Converts a value from linear RGB to the sRGB (gamma) colorspace.
  66. **/
  67. float linearToGamma(float c);
  68. /**
  69. * Calculate noise for the specified coordinate(s).
  70. *
  71. * @return Noise value in the range of [0, 1].
  72. **/
  73. static float simplexNoise1(double x);
  74. static float simplexNoise2(double x, double y);
  75. static float simplexNoise3(double x, double y, double z);
  76. static float simplexNoise4(double x, double y, double z, double w);
  77. static float perlinNoise1(double x);
  78. static float perlinNoise2(double x, double y);
  79. static float perlinNoise3(double x, double y, double z);
  80. static float perlinNoise4(double x, double y, double z, double w);
  81. class Math : public Module
  82. {
  83. public:
  84. Math();
  85. virtual ~Math();
  86. RandomGenerator *getRandomGenerator()
  87. {
  88. return &rng;
  89. }
  90. /**
  91. * Create a new random number generator.
  92. **/
  93. RandomGenerator *newRandomGenerator();
  94. /**
  95. * Creates a new bezier curve.
  96. **/
  97. BezierCurve *newBezierCurve(const std::vector<Vector2> &points);
  98. Transform *newTransform();
  99. Transform *newTransform(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky);
  100. // Implements Module.
  101. virtual ModuleType getModuleType() const
  102. {
  103. return M_MATH;
  104. }
  105. virtual const char *getName() const
  106. {
  107. return "love.math";
  108. }
  109. private:
  110. RandomGenerator rng;
  111. }; // Math
  112. static inline float simplexNoise1(double x)
  113. {
  114. return SimplexNoise1234::noise(x) * 0.5f + 0.5f;
  115. }
  116. static inline float simplexNoise2(double x, double y)
  117. {
  118. return SimplexNoise1234::noise(x, y) * 0.5f + 0.5f;
  119. }
  120. static inline float simplexNoise3(double x, double y, double z)
  121. {
  122. return SimplexNoise1234::noise(x, y, z) * 0.5f + 0.5f;
  123. }
  124. static inline float simplexNoise4(double x, double y, double z, double w)
  125. {
  126. return SimplexNoise1234::noise(x, y, z, w) * 0.5f + 0.5f;
  127. }
  128. static inline float perlinNoise1(double x)
  129. {
  130. return Noise1234::noise(x) * 0.5f + 0.5f;
  131. }
  132. static inline float perlinNoise2(double x, double y)
  133. {
  134. return Noise1234::noise(x, y) * 0.5f + 0.5f;
  135. }
  136. static inline float perlinNoise3(double x, double y, double z)
  137. {
  138. return Noise1234::noise(x, y, z) * 0.5f + 0.5f;
  139. }
  140. static inline float perlinNoise4(double x, double y, double z, double w)
  141. {
  142. return Noise1234::noise(x, y, z, w) * 0.5f + 0.5f;
  143. }
  144. } // math
  145. } // love
  146. #endif