MathModule.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Copyright (c) 2006-2013 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/simplexnoise1234.h"
  30. // STL
  31. #include <vector>
  32. namespace love
  33. {
  34. namespace math
  35. {
  36. class BezierCurve;
  37. class Math : public Module
  38. {
  39. private:
  40. RandomGenerator rng;
  41. public:
  42. virtual ~Math()
  43. {}
  44. /**
  45. * @copydoc RandomGenerator::randomseed()
  46. **/
  47. inline void randomseed(uint64 seed)
  48. {
  49. rng.randomseed(seed);
  50. }
  51. /**
  52. * @copydoc RandomGenerator::random()
  53. **/
  54. inline double random()
  55. {
  56. return rng.random();
  57. }
  58. /**
  59. * @copydoc RandomGenerator::random(double)
  60. **/
  61. inline double random(double max)
  62. {
  63. return rng.random(max);
  64. }
  65. /**
  66. * @copydoc RandomGenerator::random(double,double)
  67. **/
  68. inline double random(double min, double max)
  69. {
  70. return rng.random(min, max);
  71. }
  72. /**
  73. * @copydoc RandomGenerator::randomnormal()
  74. **/
  75. inline double randomnormal(double stddev)
  76. {
  77. return rng.randomnormal(stddev);
  78. }
  79. /**
  80. * Create a new random number generator.
  81. **/
  82. RandomGenerator *newRandomGenerator();
  83. /**
  84. * Creates a new bezier curve.
  85. **/
  86. BezierCurve *newBezierCurve(const std::vector<Vector> &points);
  87. virtual const char *getName() const
  88. {
  89. return "love.math";
  90. }
  91. /**
  92. * Triangulate a simple polygon.
  93. *
  94. * @param polygon Polygon to triangulate. Must not intersect itself.
  95. * @return List of triangles the polygon is composed of.
  96. **/
  97. std::vector<Triangle> triangulate(const std::vector<vertex> &polygon);
  98. /**
  99. * Checks whether a polygon is convex.
  100. *
  101. * @param polygon Polygon to test.
  102. * @return True if the polygon is convex, false otherwise.
  103. **/
  104. bool isConvex(const std::vector<vertex> &polygon);
  105. /**
  106. * Calculate Simplex noise for the specified coordinate(s).
  107. *
  108. * @return Noise value in the range of [-1,1].
  109. **/
  110. float noise(float x) const;
  111. float noise(float x, float y) const;
  112. float noise(float x, float y, float z) const;
  113. float noise(float x, float y, float z, float w) const;
  114. static Math instance;
  115. private:
  116. Math();
  117. }; // Math
  118. inline float Math::noise(float x) const
  119. {
  120. return SimplexNoise1234::noise(x);
  121. }
  122. inline float Math::noise(float x, float y) const
  123. {
  124. return SimplexNoise1234::noise(x, y);
  125. }
  126. inline float Math::noise(float x, float y, float z) const
  127. {
  128. return SimplexNoise1234::noise(x, y, z);
  129. }
  130. inline float Math::noise(float x, float y, float z, float w) const
  131. {
  132. return SimplexNoise1234::noise(x, y, z, w);
  133. }
  134. } // math
  135. } // love
  136. #endif