diff options
author | Juan Linietsky <reduzio@gmail.com> | 2019-01-08 19:10:24 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2019-01-08 19:11:08 -0300 |
commit | e46f28e02dde08bb515fdd796ffcf114361e4877 (patch) | |
tree | 495178f917c2103c30440f7befef4a24681bd577 /core | |
parent | d8c40bccbbbf74001cbd085da5f71180e054e17f (diff) |
Removed splits in Polygon editor, replace by internal vertices and polygon support.
Diffstat (limited to 'core')
-rw-r--r-- | core/math/geometry.cpp | 2 | ||||
-rw-r--r-- | core/math/geometry.h | 32 |
2 files changed, 33 insertions, 1 deletions
diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp index c51ffd7b0a..12c88f43b3 100644 --- a/core/math/geometry.cpp +++ b/core/math/geometry.cpp @@ -32,6 +32,7 @@ #include "core/print_string.h" +/* this implementation is very inefficient, commenting unless bugs happen. See the other one. bool Geometry::is_point_in_polygon(const Vector2 &p_point, const Vector<Vector2> &p_polygon) { Vector<int> indices = Geometry::triangulate_polygon(p_polygon); @@ -42,6 +43,7 @@ bool Geometry::is_point_in_polygon(const Vector2 &p_point, const Vector<Vector2> } return false; } +*/ void Geometry::MeshData::optimize_vertices() { diff --git a/core/math/geometry.h b/core/math/geometry.h index 67805dd6c9..29493516b8 100644 --- a/core/math/geometry.h +++ b/core/math/geometry.h @@ -514,7 +514,7 @@ public: return (cn.cross(an) > 0) == orientation; } - static bool is_point_in_polygon(const Vector2 &p_point, const Vector<Vector2> &p_polygon); + //static bool is_point_in_polygon(const Vector2 &p_point, const Vector<Vector2> &p_polygon); static Vector2 get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 *p_segment) { @@ -815,6 +815,36 @@ public: return sum > 0.0f; } + /* alternate implementation that should be faster */ + static bool is_point_in_polygon(const Vector2 &p_point, const Vector<Vector2> &p_polygon) { + int c = p_polygon.size(); + if (c < 3) + return false; + const Vector2 *p = p_polygon.ptr(); + Vector2 further_away(-1e20, -1e20); + Vector2 further_away_opposite(1e20, 1e20); + + for (int i = 0; i < c; i++) { + further_away.x = MAX(p[i].x, further_away.x); + further_away.y = MAX(p[i].y, further_away.y); + further_away_opposite.x = MIN(p[i].x, further_away_opposite.x); + further_away_opposite.y = MIN(p[i].y, further_away_opposite.y); + } + + further_away += (further_away - further_away_opposite) * Vector2(1.221313, 1.512312); // make point outside that wont intersect with points in segment from p_point + + int intersections = 0; + for (int i = 0; i < c; i++) { + const Vector2 &v1 = p[i]; + const Vector2 &v2 = p[(i + 1) % c]; + if (segment_intersects_segment_2d(v1, v2, p_point, further_away, NULL)) { + intersections++; + } + } + + return (intersections & 1); + } + static PoolVector<PoolVector<Face3> > separate_objects(PoolVector<Face3> p_array); static PoolVector<Face3> wrap_geometry(PoolVector<Face3> p_array, real_t *p_error = NULL); ///< create a "wrap" that encloses the given geometry |