MathModule.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * Copyright (c) 2006-2024 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. // LOVE
  21. #include "MathModule.h"
  22. #include "common/Vector.h"
  23. #include "common/int.h"
  24. #include "common/StringMap.h"
  25. #include "BezierCurve.h"
  26. #include "Transform.h"
  27. // STL
  28. #include <cmath>
  29. #include <list>
  30. #include <iostream>
  31. // C
  32. #include <time.h>
  33. using std::list;
  34. using love::Vector2;
  35. namespace
  36. {
  37. // check if an angle is oriented counter clockwise
  38. inline bool is_oriented_ccw(const Vector2 &a, const Vector2 &b, const Vector2 &c)
  39. {
  40. // return det(b-a, c-a) >= 0
  41. return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)) >= 0;
  42. }
  43. // check if a and b are on the same side of the line c->d
  44. bool on_same_side(const Vector2 &a, const Vector2 &b, const Vector2 &c, const Vector2 &d)
  45. {
  46. float px = d.x - c.x, py = d.y - c.y;
  47. // return det(p, a-c) * det(p, b-c) >= 0
  48. float l = px * (a.y - c.y) - py * (a.x - c.x);
  49. float m = px * (b.y - c.y) - py * (b.x - c.x);
  50. return l * m >= 0;
  51. }
  52. // checks is p is contained in the triangle abc
  53. inline bool point_in_triangle(const Vector2 &p, const Vector2 &a, const Vector2 &b, const Vector2 &c)
  54. {
  55. return on_same_side(p,a, b,c) && on_same_side(p,b, a,c) && on_same_side(p,c, a,b);
  56. }
  57. // checks if any vertex in `vertices' is in the triangle abc.
  58. bool any_point_in_triangle(const std::list<const Vector2 *> &vertices, const Vector2 &a, const Vector2 &b, const Vector2 &c)
  59. {
  60. for (const Vector2 *p : vertices)
  61. {
  62. if ((p != &a) && (p != &b) && (p != &c) && point_in_triangle(*p, a,b,c)) // oh god...
  63. return true;
  64. }
  65. return false;
  66. }
  67. inline bool is_ear(const Vector2 &a, const Vector2 &b, const Vector2 &c, const std::list<const Vector2 *> &vertices)
  68. {
  69. return is_oriented_ccw(a,b,c) && !any_point_in_triangle(vertices, a,b,c);
  70. }
  71. } // anonymous namespace
  72. namespace love
  73. {
  74. namespace math
  75. {
  76. std::vector<Triangle> triangulate(const std::vector<love::Vector2> &polygon)
  77. {
  78. if (polygon.size() < 3)
  79. throw love::Exception("Not a polygon");
  80. else if (polygon.size() == 3)
  81. return std::vector<Triangle>(1, Triangle(polygon[0], polygon[1], polygon[2]));
  82. // collect list of connections and record leftmost item to check if the polygon
  83. // has the expected winding
  84. std::vector<size_t> next_idx(polygon.size()), prev_idx(polygon.size());
  85. size_t idx_lm = 0;
  86. for (size_t i = 0; i < polygon.size(); ++i)
  87. {
  88. const love::Vector2 &lm = polygon[idx_lm], &p = polygon[i];
  89. if (p.x < lm.x || (p.x == lm.x && p.y < lm.y))
  90. idx_lm = i;
  91. next_idx[i] = i+1;
  92. prev_idx[i] = i-1;
  93. }
  94. next_idx[next_idx.size()-1] = 0;
  95. prev_idx[0] = prev_idx.size()-1;
  96. // check if the polygon has the expected winding and reverse polygon if needed
  97. if (!is_oriented_ccw(polygon[prev_idx[idx_lm]], polygon[idx_lm], polygon[next_idx[idx_lm]]))
  98. next_idx.swap(prev_idx);
  99. // collect list of concave polygons
  100. std::list<const love::Vector2 *> concave_vertices;
  101. for (size_t i = 0; i < polygon.size(); ++i)
  102. {
  103. if (!is_oriented_ccw(polygon[prev_idx[i]], polygon[i], polygon[next_idx[i]]))
  104. concave_vertices.push_back(&polygon[i]);
  105. }
  106. // triangulation according to kong
  107. std::vector<Triangle> triangles;
  108. size_t n_vertices = polygon.size();
  109. size_t current = 1, skipped = 0, next, prev;
  110. while (n_vertices > 3)
  111. {
  112. next = next_idx[current];
  113. prev = prev_idx[current];
  114. const Vector2 &a = polygon[prev], &b = polygon[current], &c = polygon[next];
  115. if (is_ear(a,b,c, concave_vertices))
  116. {
  117. triangles.push_back(Triangle(a,b,c));
  118. next_idx[prev] = next;
  119. prev_idx[next] = prev;
  120. concave_vertices.remove(&b);
  121. --n_vertices;
  122. skipped = 0;
  123. }
  124. else if (++skipped > n_vertices)
  125. {
  126. throw love::Exception("Cannot triangulate polygon.");
  127. }
  128. current = next;
  129. }
  130. next = next_idx[current];
  131. prev = prev_idx[current];
  132. triangles.push_back(Triangle(polygon[prev], polygon[current], polygon[next]));
  133. return triangles;
  134. }
  135. bool isConvex(const std::vector<love::Vector2> &polygon)
  136. {
  137. if (polygon.size() < 3)
  138. return false;
  139. // a polygon is convex if all corners turn in the same direction
  140. // turning direction can be determined using the cross-product of
  141. // the forward difference vectors
  142. size_t i = polygon.size() - 2, j = polygon.size() - 1, k = 0;
  143. Vector2 p(polygon[j] - polygon[i]);
  144. Vector2 q(polygon[k] - polygon[j]);
  145. float winding = Vector2::cross(p, q);
  146. while (k+1 < polygon.size())
  147. {
  148. i = j; j = k; k++;
  149. p = polygon[j] - polygon[i];
  150. q = polygon[k] - polygon[j];
  151. if (Vector2::cross(p, q) * winding < 0)
  152. return false;
  153. }
  154. return true;
  155. }
  156. /**
  157. * http://en.wikipedia.org/wiki/SRGB#The_reverse_transformation
  158. **/
  159. float gammaToLinear(float c)
  160. {
  161. if (c <= 0.04045f)
  162. return c / 12.92f;
  163. else
  164. return powf((c + 0.055f) / 1.055f, 2.4f);
  165. }
  166. /**
  167. * http://en.wikipedia.org/wiki/SRGB#The_forward_transformation_.28CIE_xyY_or_CIE_XYZ_to_sRGB.29
  168. **/
  169. float linearToGamma(float c)
  170. {
  171. if (c <= 0.0031308f)
  172. return c * 12.92f;
  173. else
  174. return 1.055f * powf(c, 1.0f / 2.4f) - 0.055f;
  175. }
  176. Math::Math()
  177. : Module(M_MATH, "love.math")
  178. , rng()
  179. {
  180. RandomGenerator::Seed seed;
  181. seed.b64 = (uint64) time(nullptr);
  182. rng.setSeed(seed);
  183. }
  184. Math::~Math()
  185. {
  186. }
  187. RandomGenerator *Math::newRandomGenerator()
  188. {
  189. return new RandomGenerator();
  190. }
  191. BezierCurve *Math::newBezierCurve(const std::vector<Vector2> &points)
  192. {
  193. return new BezierCurve(points);
  194. }
  195. Transform *Math::newTransform()
  196. {
  197. return new Transform();
  198. }
  199. Transform *Math::newTransform(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky)
  200. {
  201. return new Transform(x, y, a, sx, sy, ox, oy, kx, ky);
  202. }
  203. } // math
  204. } // love