MathModule.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * Copyright (c) 2006-2017 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 noise1(float x);
  74. static float noise2(float x, float y);
  75. static float noise3(float x, float y, float z);
  76. static float noise4(float x, float y, float z, float w);
  77. class Math : public Module
  78. {
  79. private:
  80. RandomGenerator rng;
  81. public:
  82. virtual ~Math();
  83. RandomGenerator *getRandomGenerator()
  84. {
  85. return &rng;
  86. }
  87. /**
  88. * Create a new random number generator.
  89. **/
  90. RandomGenerator *newRandomGenerator();
  91. /**
  92. * Creates a new bezier curve.
  93. **/
  94. BezierCurve *newBezierCurve(const std::vector<Vector2> &points);
  95. Transform *newTransform();
  96. Transform *newTransform(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky);
  97. // Implements Module.
  98. virtual ModuleType getModuleType() const
  99. {
  100. return M_MATH;
  101. }
  102. virtual const char *getName() const
  103. {
  104. return "love.math";
  105. }
  106. static Math instance;
  107. private:
  108. Math();
  109. }; // Math
  110. static inline float noise1(float x)
  111. {
  112. return SimplexNoise1234::noise(x) * 0.5f + 0.5f;
  113. }
  114. static inline float noise2(float x, float y)
  115. {
  116. return SimplexNoise1234::noise(x, y) * 0.5f + 0.5f;
  117. }
  118. // Perlin noise is used instead of Simplex noise in the 3D and 4D cases to avoid
  119. // patent issues.
  120. static inline float noise3(float x, float y, float z)
  121. {
  122. return Noise1234::noise(x, y, z) * 0.5f + 0.5f;
  123. }
  124. static inline float noise4(float x, float y, float z, float w)
  125. {
  126. return Noise1234::noise(x, y, z, w) * 0.5f + 0.5f;
  127. }
  128. } // math
  129. } // love
  130. #endif