Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autoware_utils): address self-intersecting polygons in random_concave_generator and handle empty inners() during triangulation #8995

Merged
merged 9 commits into from
Nov 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@

#include <autoware/universe_utils/geometry/geometry.hpp>

#include <optional>
#include <vector>

namespace autoware::universe_utils
{
/// @brief generate a random non-convex polygon
/// @param vertices number of vertices for the desired polygon
/// @param max points will be generated in the range [-max, max]
/// @details algorithm from
/// https://digitalscholarship.unlv.edu/cgi/viewcontent.cgi?article=3183&context=thesesdissertations
Polygon2d random_concave_polygon(const size_t vertices, const double max);
std::optional<Polygon2d> random_concave_polygon(const size_t vertices, const double max);

/// @brief checks for collisions between two vectors of convex polygons using a specified collision
/// detection algorithm
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 TIER IV, Inc.

Check notice on line 1 in common/autoware_universe_utils/src/geometry/alt_geometry.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 4.50 to 4.57, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -68,6 +68,9 @@
std::vector<PointList2d> inners;
for (const auto & inner : polygon.inners()) {
PointList2d _inner;
if (inner.empty()) {
continue;
}
for (const auto & point : inner) {
_inner.push_back(Point2d(point));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 TIER IV, Inc.

Check notice on line 1 in common/autoware_universe_utils/src/geometry/ear_clipping.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 5.59 to 5.63, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -420,6 +420,9 @@
std::vector<std::size_t> queue;

for (const auto & ring : inners) {
if (ring.empty()) {
continue;

Check warning on line 424 in common/autoware_universe_utils/src/geometry/ear_clipping.cpp

View check run for this annotation

Codecov / codecov/patch

common/autoware_universe_utils/src/geometry/ear_clipping.cpp#L424

Added line #L424 was not covered by tests
}
auto inner_index = linked_list(ring, false, vertices, points);

if (points[inner_index].next_index.value() == inner_index) {
Expand Down Expand Up @@ -617,10 +620,6 @@
std::vector<Polygon2d> triangulate(const Polygon2d & poly)
{
const auto alt_poly = alt::Polygon2d::create(poly);
if (!alt_poly.has_value()) {
return {};
}

const auto alt_triangles = triangulate(alt_poly.value());
std::vector<Polygon2d> triangles;
for (const auto & alt_triangle : alt_triangles) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 TIER IV, Inc.

Check notice on line 1 in common/autoware_universe_utils/src/geometry/random_concave_polygon.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 4.73 to 4.80, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
#include <boost/geometry/algorithms/convex_hull.hpp>
#include <boost/geometry/algorithms/correct.hpp>
#include <boost/geometry/algorithms/intersects.hpp>
#include <boost/geometry/algorithms/is_valid.hpp>
#include <boost/geometry/strategies/agnostic/hull_graham_andrew.hpp>

#include <random>
Expand Down Expand Up @@ -138,30 +139,23 @@
}

/// @brief checks if an edge is valid for a given polygon and set of points
bool is_valid(const Edge & e, const Polygon2d & P, const std::vector<Point2d> & Q)
bool is_valid(const Edge & e, const Polygon2d & P, const std::list<Point2d> & Q)
{
bool valid = false;
size_t i = 0;

while (!valid && i < Q.size()) {
const Point2d & q = Q[i];
for (const Point2d & q : Q) {
Edge e1 = {e.first, q};
Edge e2 = {q, e.second};
bool intersects_e1 = intersecting(e1, P);
bool intersects_e2 = intersecting(e2, P);

if (!intersects_e1 && !intersects_e2) {
valid = true;
if (intersects_e1 || intersects_e2) {
return false;
}

++i;
}

return valid;
return true;
}

/// @brief finds the nearest node from a set of points to an edge
Point2d get_nearest_node(const std::vector<Point2d> & Q, const Edge & e)
Point2d get_nearest_node(const std::list<Point2d> & Q, const Edge & e)
{
double min_distance = std::numeric_limits<double>::max();
Point2d nearest_node(0, 0);
Expand All @@ -178,7 +172,7 @@
}

/// @brief finds the edge that is closest to the given set of points
Edge get_breaking_edge(const PolygonWithEdges & polygon_with_edges, const std::vector<Point2d> & Q)
Edge get_breaking_edge(const PolygonWithEdges & polygon_with_edges, const std::list<Point2d> & Q)
{
double min_distance = std::numeric_limits<double>::max();
Edge e_breaking;
Expand Down Expand Up @@ -229,7 +223,7 @@
}

/// @brief removes a node from a set of points
void remove_node(std::vector<Point2d> & Q, const Point2d & w)
void remove_node(std::list<Point2d> & Q, const Point2d & w)
{
const double epsilon = 1e-9;

Expand All @@ -243,7 +237,7 @@
}

/// @brief marks edges as valid if they are valid according to the polygon and points
void mark_valid_edges(PolygonWithEdges & polygon_with_edges, const std::vector<Point2d> & Q)
void mark_valid_edges(PolygonWithEdges & polygon_with_edges, const std::list<Point2d> & Q)
{
for (auto & edge : polygon_with_edges.edges) {
if (is_valid(edge, polygon_with_edges.polygon, Q)) {
Expand All @@ -256,8 +250,7 @@
Polygon2d inward_denting(LinearRing2d & ring)
{
LinearRing2d convex_ring;
std::vector<Point2d> q;
q.reserve(ring.size());
std::list<Point2d> q;
boost::geometry::strategy::convex_hull::graham_andrew<LinearRing2d, Point2d> strategy;
boost::geometry::convex_hull(ring, convex_ring, strategy);
PolygonWithEdges polygon_with_edges;
Expand Down Expand Up @@ -342,41 +335,41 @@
return false;
}

Polygon2d random_concave_polygon(const size_t vertices, const double max)
std::optional<Polygon2d> random_concave_polygon(const size_t vertices, const double max)
{
if (vertices < 4) {
return Polygon2d();
}

std::random_device r;
std::default_random_engine random_engine(r());
std::uniform_real_distribution<double> uniform_dist(-max, max);
std::uniform_int_distribution<int> random_bool(0, 1);

Polygon2d poly;
bool is_non_convex = false;
int max_attempts = 100;
int attempt = 0;

while (!is_non_convex && attempt < max_attempts) {
auto xs = prepare_coordinate_vectors(vertices, uniform_dist, random_bool, random_engine);
auto ys = prepare_coordinate_vectors(vertices, uniform_dist, random_bool, random_engine);

std::shuffle(ys.begin(), ys.end(), random_engine);

LinearRing2d vectors;
for (size_t i = 0; i < xs.size(); ++i) {
vectors.emplace_back(xs[i], ys[i]);
}

LinearRing2d points;
for (const auto & p : vectors) {
points.emplace_back(p.x(), p.y());
}
// apply inward denting algorithm
poly = inward_denting(points);
// check for convexity
if (!is_convex(poly)) {
if (!is_convex(poly) && boost::geometry::is_valid(poly) && poly.outer().size() != vertices) {

Check notice on line 372 in common/autoware_universe_utils/src/geometry/random_concave_polygon.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Complex Method

random_concave_polygon increases in cyclomatic complexity from 11 to 13, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check warning on line 372 in common/autoware_universe_utils/src/geometry/random_concave_polygon.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Conditional

random_concave_polygon has 1 complex conditionals with 2 branches, threshold = 2. A complex conditional is an expression inside a branch (e.g. if, for, while) which consists of multiple, logical operators such as AND/OR. The more logical operators in an expression, the more severe the code smell.
is_non_convex = true;
}
LinearRing2d poly_outer = poly.outer();
Expand Down
104 changes: 103 additions & 1 deletion common/autoware_universe_utils/test/src/geometry/test_geometry.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2024 Tier IV, Inc.

Check notice on line 1 in common/autoware_universe_utils/test/src/geometry/test_geometry.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 1898 to 1979, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -2021,6 +2021,105 @@
}
}

double calculate_total_polygon_area(
const std::vector<autoware::universe_utils::Polygon2d> & polygons)
{
double totalArea = 0.0;
for (const auto & polygon : polygons) {
totalArea += boost::geometry::area(polygon);
}
return totalArea;
}

TEST(geometry, PolygonTriangulation)
{
using autoware::universe_utils::Polygon2d;
using autoware::universe_utils::triangulate;

{ // concave polygon
Polygon2d poly;

poly.outer().emplace_back(0.0, 0.0);
poly.outer().emplace_back(4.0, 0.0);
poly.outer().emplace_back(4.0, 4.0);
poly.outer().emplace_back(2.0, 2.0);
poly.outer().emplace_back(0.0, 4.0);
boost::geometry::correct(poly);

const auto triangles = triangulate(poly);

const auto triangle_area = calculate_total_polygon_area(triangles);
const auto poly_area = boost::geometry::area(poly);
EXPECT_NEAR(triangle_area, poly_area, epsilon);
}

{ // concave polygon with empty inners
Polygon2d poly;

poly.outer().emplace_back(0.0, 0.0);
poly.outer().emplace_back(4.0, 0.0);
poly.outer().emplace_back(4.0, 4.0);
poly.outer().emplace_back(2.0, 2.0);
poly.outer().emplace_back(0.0, 4.0);
boost::geometry::correct(poly);

poly.inners().emplace_back();

const auto triangles = triangulate(poly);

const auto triangle_area = calculate_total_polygon_area(triangles);
const auto poly_area = boost::geometry::area(poly);
EXPECT_NEAR(triangle_area, poly_area, epsilon);
}

{ // concave polygon with hole
Polygon2d poly;

poly.outer().emplace_back(0.0, 0.0);
poly.outer().emplace_back(4.0, 0.0);
poly.outer().emplace_back(4.0, 4.0);
poly.outer().emplace_back(2.0, 2.0);
poly.outer().emplace_back(0.0, 4.0);

poly.inners().emplace_back();
poly.inners().back().emplace_back(1.0, 1.0);
poly.inners().back().emplace_back(1.5, 1.0);
poly.inners().back().emplace_back(1.5, 1.5);
poly.inners().back().emplace_back(1.0, 1.5);
boost::geometry::correct(poly);

const auto triangles = triangulate(poly);

const auto triangle_area = calculate_total_polygon_area(triangles);
const auto poly_area = boost::geometry::area(poly);
EXPECT_NEAR(triangle_area, poly_area, epsilon);
}

{ // concave polygon with one empty inner followed by one hole
Polygon2d poly;

poly.outer().emplace_back(0.0, 0.0);
poly.outer().emplace_back(4.0, 0.0);
poly.outer().emplace_back(4.0, 4.0);
poly.outer().emplace_back(2.0, 2.0);
poly.outer().emplace_back(0.0, 4.0);

poly.inners().emplace_back();
poly.inners().emplace_back();
poly.inners().back().emplace_back(1.0, 1.0);
poly.inners().back().emplace_back(1.5, 1.0);
poly.inners().back().emplace_back(1.5, 1.5);
poly.inners().back().emplace_back(1.0, 1.5);
boost::geometry::correct(poly);

const auto triangles = triangulate(poly);

const auto triangle_area = calculate_total_polygon_area(triangles);
const auto poly_area = boost::geometry::area(poly);
EXPECT_NEAR(triangle_area, poly_area, epsilon);
}
}

TEST(geometry, intersectPolygonWithHoles)
{
using autoware::universe_utils::Polygon2d;
Expand Down Expand Up @@ -2246,7 +2345,10 @@
triangulations.clear();

for (auto i = 0; i < polygons_nb; ++i) {
polygons.push_back(autoware::universe_utils::random_concave_polygon(vertices, max_values));
auto polygon_opt = autoware::universe_utils::random_concave_polygon(vertices, max_values);
if (polygon_opt.has_value()) {
polygons.push_back(polygon_opt.value());
}

Check warning on line 2351 in common/autoware_universe_utils/test/src/geometry/test_geometry.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

TEST:geometry:intersectConcavePolygonRand increases in cyclomatic complexity from 11 to 12, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
}

for (const auto & polygon : polygons) {
Expand Down
Loading