From 677a8d60b8db6e408c9d8bf47a7bff8110ed5f91 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Wed, 24 Aug 2016 18:02:27 -0300 Subject: [PATCH] Use love::Vector (8 bytes) instead of love::Vertex (20 bytes) for love.math.triangulate and love.math.isConvex. --- src/common/math.h | 8 -------- src/modules/math/MathModule.cpp | 35 +++++++++++++++------------------ src/modules/math/MathModule.h | 12 +++++++++-- src/modules/math/wrap_Math.cpp | 12 +++++------ 4 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/common/math.h b/src/common/math.h index a151ce628..0a77ee3b4 100644 --- a/src/common/math.h +++ b/src/common/math.h @@ -68,14 +68,6 @@ struct Vertex unsigned char r, g, b, a; }; -struct Triangle -{ - Triangle(const Vertex &x, const Vertex &y, const Vertex &z) - : a(x), b(y), c(z) - {} - Vertex a, b, c; -}; - inline int nextP2(int x) { x += (x == 0); diff --git a/src/modules/math/MathModule.cpp b/src/modules/math/MathModule.cpp index 2493630de..0432edd79 100644 --- a/src/modules/math/MathModule.cpp +++ b/src/modules/math/MathModule.cpp @@ -29,20 +29,19 @@ #include using std::list; -using std::vector; -using love::Vertex; +using love::Vector; namespace { // check if an angle is oriented counter clockwise - inline bool is_oriented_ccw(const Vertex &a, const Vertex &b, const Vertex &c) + inline bool is_oriented_ccw(const Vector &a, const Vector &b, const Vector &c) { // return det(b-a, c-a) >= 0 return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)) >= 0; } // check if a and b are on the same side of the line c->d - bool on_same_side(const Vertex &a, const Vertex &b, const Vertex &c, const Vertex &d) + bool on_same_side(const Vector &a, const Vector &b, const Vector &c, const Vector &d) { float px = d.x - c.x, py = d.y - c.y; // return det(p, a-c) * det(p, b-c) >= 0 @@ -52,18 +51,16 @@ namespace } // checks is p is contained in the triangle abc - inline bool point_in_triangle(const Vertex &p, const Vertex &a, const Vertex &b, const Vertex &c) + inline bool point_in_triangle(const Vector &p, const Vector &a, const Vector &b, const Vector &c) { return on_same_side(p,a, b,c) && on_same_side(p,b, a,c) && on_same_side(p,c, a,b); } // checks if any vertex in `vertices' is in the triangle abc. - bool any_point_in_triangle(const list &vertices, const Vertex &a, const Vertex &b, const Vertex &c) + bool any_point_in_triangle(const list &vertices, const Vector &a, const Vector &b, const Vector &c) { - list::const_iterator it, end = vertices.end(); - for (it = vertices.begin(); it != end; ++it) + for (const Vector *p : vertices) { - const Vertex *p = *it; if ((p != &a) && (p != &b) && (p != &c) && point_in_triangle(*p, a,b,c)) // oh god... return true; } @@ -71,7 +68,7 @@ namespace return false; } - inline bool is_ear(const Vertex &a, const Vertex &b, const Vertex &c, const list &vertices) + inline bool is_ear(const Vector &a, const Vector &b, const Vector &c, const list &vertices) { return is_oriented_ccw(a,b,c) && !any_point_in_triangle(vertices, a,b,c); } @@ -100,25 +97,25 @@ RandomGenerator *Math::newRandomGenerator() return new RandomGenerator(); } -BezierCurve *Math::newBezierCurve(const vector &points) +BezierCurve *Math::newBezierCurve(const std::vector &points) { return new BezierCurve(points); } -vector Math::triangulate(const vector &polygon) +std::vector Math::triangulate(const std::vector &polygon) { if (polygon.size() < 3) throw love::Exception("Not a polygon"); else if (polygon.size() == 3) - return vector(1, Triangle(polygon[0], polygon[1], polygon[2])); + return std::vector(1, Triangle(polygon[0], polygon[1], polygon[2])); // collect list of connections and record leftmost item to check if the polygon // has the expected winding - vector next_idx(polygon.size()), prev_idx(polygon.size()); + std::vector next_idx(polygon.size()), prev_idx(polygon.size()); size_t idx_lm = 0; for (size_t i = 0; i < polygon.size(); ++i) { - const Vertex &lm = polygon[idx_lm], &p = polygon[i]; + const love::Vector &lm = polygon[idx_lm], &p = polygon[i]; if (p.x < lm.x || (p.x == lm.x && p.y < lm.y)) idx_lm = i; next_idx[i] = i+1; @@ -132,7 +129,7 @@ vector Math::triangulate(const vector &polygon) next_idx.swap(prev_idx); // collect list of concave polygons - list concave_vertices; + list concave_vertices; for (size_t i = 0; i < polygon.size(); ++i) { if (!is_oriented_ccw(polygon[prev_idx[i]], polygon[i], polygon[next_idx[i]])) @@ -140,14 +137,14 @@ vector Math::triangulate(const vector &polygon) } // triangulation according to kong - vector triangles; + std::vector triangles; size_t n_vertices = polygon.size(); size_t current = 1, skipped = 0, next, prev; while (n_vertices > 3) { next = next_idx[current]; prev = prev_idx[current]; - const Vertex &a = polygon[prev], &b = polygon[current], &c = polygon[next]; + const Vector &a = polygon[prev], &b = polygon[current], &c = polygon[next]; if (is_ear(a,b,c, concave_vertices)) { triangles.push_back(Triangle(a,b,c)); @@ -170,7 +167,7 @@ vector Math::triangulate(const vector &polygon) return triangles; } -bool Math::isConvex(const std::vector &polygon) +bool Math::isConvex(const std::vector &polygon) { if (polygon.size() < 3) return false; diff --git a/src/modules/math/MathModule.h b/src/modules/math/MathModule.h index 66b4100ba..ee4ffe6fa 100644 --- a/src/modules/math/MathModule.h +++ b/src/modules/math/MathModule.h @@ -43,6 +43,14 @@ namespace love namespace math { +struct Triangle +{ + Triangle(const Vector &x, const Vector &y, const Vector &z) + : a(x), b(y), c(z) + {} + Vector a, b, c; +}; + class BezierCurve; class Math : public Module @@ -87,7 +95,7 @@ public: * @param polygon Polygon to triangulate. Must not intersect itself. * @return List of triangles the polygon is composed of. **/ - std::vector triangulate(const std::vector &polygon); + std::vector triangulate(const std::vector &polygon); /** * Checks whether a polygon is convex. @@ -95,7 +103,7 @@ public: * @param polygon Polygon to test. * @return True if the polygon is convex, false otherwise. **/ - bool isConvex(const std::vector &polygon); + bool isConvex(const std::vector &polygon); /** * Converts a value from the sRGB (gamma) colorspace to linear RGB. diff --git a/src/modules/math/wrap_Math.cpp b/src/modules/math/wrap_Math.cpp index 74c8a4bd3..0453a153c 100644 --- a/src/modules/math/wrap_Math.cpp +++ b/src/modules/math/wrap_Math.cpp @@ -119,7 +119,7 @@ int w_newBezierCurve(lua_State *L) int w_triangulate(lua_State *L) { - std::vector vertices; + std::vector vertices; if (lua_istable(L, 1)) { int top = (int) luax_objlen(L, 1); @@ -129,7 +129,7 @@ int w_triangulate(lua_State *L) lua_rawgeti(L, 1, i); lua_rawgeti(L, 1, i+1); - Vertex v; + Vector v; v.x = (float) luaL_checknumber(L, -2); v.y = (float) luaL_checknumber(L, -1); vertices.push_back(v); @@ -143,7 +143,7 @@ int w_triangulate(lua_State *L) vertices.reserve(top / 2); for (int i = 1; i <= top; i += 2) { - Vertex v; + Vector v; v.x = (float) luaL_checknumber(L, i); v.y = (float) luaL_checknumber(L, i+1); vertices.push_back(v); @@ -189,7 +189,7 @@ int w_triangulate(lua_State *L) int w_isConvex(lua_State *L) { - std::vector vertices; + std::vector vertices; if (lua_istable(L, 1)) { int top = (int) luax_objlen(L, 1); @@ -199,7 +199,7 @@ int w_isConvex(lua_State *L) lua_rawgeti(L, 1, i); lua_rawgeti(L, 1, i+1); - Vertex v; + love::Vector v; v.x = (float) luaL_checknumber(L, -2); v.y = (float) luaL_checknumber(L, -1); vertices.push_back(v); @@ -213,7 +213,7 @@ int w_isConvex(lua_State *L) vertices.reserve(top / 2); for (int i = 1; i <= top; i += 2) { - Vertex v; + love::Vector v; v.x = (float) luaL_checknumber(L, i); v.y = (float) luaL_checknumber(L, i+1); vertices.push_back(v);