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

Update padFootprint function #108

Merged
merged 8 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions costmap_2d/include/costmap_2d/footprint.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ void transformFootprint(double x, double y, double theta, const std::vector<geom
void transformFootprint(double x, double y, double theta, const std::vector<geometry_msgs::Point>& footprint_spec,
geometry_msgs::PolygonStamped & oriented_footprint);

/**
* @brief Calculate the centroid of a vector of 2D points.
* @param footprint A vector of points (type geometry_msgs::Point) representing a 2D polygon (footprint).
* @return The centroid of the footprint as a geometry_msgs::Point.
*/
geometry_msgs::Point calculateCentroid(const std::vector<geometry_msgs::Point>& footprint);

/**
* @brief Adds the specified amount of padding to the footprint (in place)
*/
Expand Down
36 changes: 31 additions & 5 deletions costmap_2d/src/footprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,40 @@ void transformFootprint(double x, double y, double theta, const std::vector<geom
}
}

geometry_msgs::Point calculateCentroid(const std::vector<geometry_msgs::Point>& footprint)
{
geometry_msgs::Point center;
double sum_x = 0.0, sum_y = 0.0;

for (const auto& point : footprint)
{
sum_x += point.x;
sum_y += point.y;
}

if (!footprint.empty()) {
center.x = sum_x / footprint.size();
center.y = sum_y / footprint.size();
}

return center;
}

void padFootprint(std::vector<geometry_msgs::Point>& footprint, double padding)
{
// pad footprint in place
for (unsigned int i = 0; i < footprint.size(); i++)
geometry_msgs::Point footprint_center = calculateCentroid(footprint);

for (auto& point : footprint)
{
geometry_msgs::Point& pt = footprint[ i ];
pt.x += sign0(pt.x) * padding;
pt.y += sign0(pt.y) * padding;
double dx = point.x - footprint_center.x;
double dy = point.y - footprint_center.y;
double distance = std::hypot(dx, dy);

if (distance > 0.0)
{
point.x += padding * (dx / distance);
point.y += padding * (dy / distance);
}
}
}

Expand Down
20 changes: 10 additions & 10 deletions costmap_2d/test/footprint_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ TEST( Costmap2DROS, padded_footprint_from_string_param )
std::vector<geometry_msgs::Point> footprint = cm.getRobotFootprint();
EXPECT_EQ( 3, footprint.size() );

EXPECT_EQ( 1.5f, footprint[ 0 ].x );
EXPECT_EQ( 1.5f, footprint[ 0 ].y );
EXPECT_EQ( 0.0f, footprint[ 0 ].z );
EXPECT_NEAR( 1.44721f, footprint[ 0 ].x, 0.0001f);
EXPECT_NEAR( 1.22361f, footprint[ 0 ].y, 0.0001f);
EXPECT_EQ( 0.0f, footprint[ 0 ].z);

EXPECT_EQ( -1.5f, footprint[ 1 ].x );
EXPECT_EQ( 1.5f, footprint[ 1 ].y );
EXPECT_EQ( 0.0f, footprint[ 1 ].z );
EXPECT_NEAR( -1.35355f, footprint[ 1 ].x, 0.0001f);
EXPECT_NEAR( 1.35355f, footprint[ 1 ].y, 0.0001f);
EXPECT_EQ( 0.0f, footprint[ 1 ].z);

EXPECT_EQ( -1.5f, footprint[ 2 ].x );
EXPECT_EQ( -1.5f, footprint[ 2 ].y );
EXPECT_EQ( 0.0f, footprint[ 2 ].z );
EXPECT_NEAR( -1.22361f, footprint[ 2 ].x, 0.0001f);
EXPECT_NEAR( -1.44721f, footprint[ 2 ].y, 0.0001f);
EXPECT_EQ( 0.0f, footprint[ 2 ].z);
}

TEST( Costmap2DROS, radius_param )
Expand Down Expand Up @@ -155,7 +155,7 @@ TEST( Costmap2DROS, footprint_empty )
// With no specification of footprint or radius, defaults to 0.46 meter radius plus 0.01 meter padding.
EXPECT_EQ( 16, footprint.size() );

EXPECT_NEAR( 0.47f, footprint[ 0 ].x, 0.0001 );
EXPECT_NEAR( 0.47f, footprint[ 0 ].x, 0.001 );
EXPECT_NEAR( 0.0f, footprint[ 0 ].y, 0.0001 );
EXPECT_EQ( 0.0f, footprint[ 0 ].z );
}
Expand Down
Loading