MathModule.h 3.1 KB

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