Skip to content

Commit

Permalink
Merge pull request #107 from goostengine/fix-geometry-crashes
Browse files Browse the repository at this point in the history
Fix `GoostGeometry2D` and `PolyDecomp2D` crashes with invalid data
  • Loading branch information
Xrayez authored Jul 19, 2021
2 parents bf9c2a4 + 7cc56d0 commit 52ebb33
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
### Fixed
- Crashes with `LinkedList` when dealing with invalid data.
- `insert_before/after(null, value)` no longer pushes front/back an element.
- Out of memory error when calling `GoostGeometry2D.simplify_polyline()` with `epsilon = 0`.
- Crashes while decomposing empty polygons with `PolyDecomp2D` when using `polypartition` geometry backend.

## [1.0] - 2021-05-24

Expand Down
4 changes: 4 additions & 0 deletions core/math/geometry/2d/goost_geometry_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ Vector<Point2> GoostGeometry2D::simplify_polyline(const Vector<Point2> &p_polyli
if (p_polyline.size() <= 2) {
return p_polyline;
}
if (p_epsilon <= 0.0) {
// Retain all points.
return p_polyline;
}
Vector<bool> points_to_retain;
points_to_retain.resize(p_polyline.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ Vector<Vector<Point2>> partition(List<TriangulatorPoly> &p_out_poly) {

Vector<Vector<Point2>> PolyDecomp2DPolyPartition::triangulate_ec(const Vector<Vector<Point2>> &p_polygons) {
List<TriangulatorPoly> in_poly = configure(DECOMP_TRIANGLES_EC, p_polygons);

if (in_poly.empty()) {
return Vector<Vector<Point2>>();
}
TriangulatorPartition tpart;
List<TriangulatorPoly> out_poly;

Expand All @@ -74,7 +76,9 @@ Vector<Vector<Point2>> PolyDecomp2DPolyPartition::triangulate_ec(const Vector<Ve

Vector<Vector<Point2>> PolyDecomp2DPolyPartition::triangulate_opt(const Vector<Vector<Point2>> &p_polygons) {
List<TriangulatorPoly> in_poly = configure(DECOMP_TRIANGLES_OPT, p_polygons);

if (in_poly.empty()) {
return Vector<Vector<Point2>>();
}
TriangulatorPartition tpart;
List<TriangulatorPoly> out_poly;

Expand All @@ -87,7 +91,9 @@ Vector<Vector<Point2>> PolyDecomp2DPolyPartition::triangulate_opt(const Vector<V

Vector<Vector<Point2>> PolyDecomp2DPolyPartition::triangulate_mono(const Vector<Vector<Point2>> &p_polygons) {
List<TriangulatorPoly> in_poly = configure(DECOMP_TRIANGLES_MONO, p_polygons);

if (in_poly.empty()) {
return Vector<Vector<Point2>>();
}
TriangulatorPartition tpart;
List<TriangulatorPoly> out_poly;

Expand All @@ -101,7 +107,9 @@ Vector<Vector<Point2>> PolyDecomp2DPolyPartition::triangulate_mono(const Vector<

Vector<Vector<Point2>> PolyDecomp2DPolyPartition::decompose_convex_hm(const Vector<Vector<Point2>> &p_polygons) {
List<TriangulatorPoly> in_poly = configure(DECOMP_CONVEX_HM, p_polygons);

if (in_poly.empty()) {
return Vector<Vector<Point2>>();
}
TriangulatorPartition tpart;
List<TriangulatorPoly> out_poly;

Expand All @@ -114,7 +122,9 @@ Vector<Vector<Point2>> PolyDecomp2DPolyPartition::decompose_convex_hm(const Vect

Vector<Vector<Point2>> PolyDecomp2DPolyPartition::decompose_convex_opt(const Vector<Vector<Point2>> &p_polygons) {
List<TriangulatorPoly> in_poly = configure(DECOMP_CONVEX_OPT, p_polygons);

if (in_poly.empty()) {
return Vector<Vector<Point2>>();
}
TriangulatorPartition tpart;
List<TriangulatorPoly> out_poly;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ func test_decompose_polygons_convex_opt():
solution = PolyDecomp2D.decompose_polygons([poly_boundary], PolyDecomp2D.DECOMP_CONVEX_OPT)
assert_eq(solution.size(), 1)
assert_eq(solution[0].size(), 8)


# TODO: re-enable once error printing can be disabled.
# func test_decompose_polygon_empty():
# PolyDecomp2D.decompose_polygons(Array([]), PolyDecomp2D.DECOMP_TRIANGLES_OPT)
# PolyDecomp2D.decompose_polygons(Array([]), PolyDecomp2D.DECOMP_CONVEX_OPT)
10 changes: 10 additions & 0 deletions tests/project/goost/core/math/2d/geometry/test_geometry_2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ func test_simplify_polyline():
assert_eq(simplified[i], control[i])


func test_simplify_polyline_zero_epsilon():
var input = PoolVector2Array([
Vector2(-2.182464, -3.504493),
Vector2(-2.662979, -1.333622),
Vector2(2.71501, 1.567903),
])
var simplified = GoostGeometry2D.simplify_polyline(input, 0)
assert_eq(input.size(), simplified.size())


func test_smooth_polygon():
var input = [Vector2(26, 20), Vector2(73, 23), Vector2(72, 62), Vector2(29, 57)]
var control = [Vector2(26, 20), Vector2(49.311768, 15.934073), Vector2(73, 23),
Expand Down

0 comments on commit 52ebb33

Please sign in to comment.