TrajectoryChecker2D.cpp
Go to the documentation of this file.
1/**
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "TrajectoryChecker2D.h"
18
19#include <cmath>
20
22
24{
26 costmap_(costmap),
27 clearance_(clearance)
28 {
29 }
30
31 bool TrajectoryChecker2D::isInCollision(float x, float y) const
32 {
33 const auto distOpt = costmap_.value(Eigen::Vector2f{x, y});
34 if (!distOpt.has_value())
35 {
36 // Unknown/masked region -> treat as collision to be safe.
37 return true;
38 }
39 return distOpt.value() < static_cast<float>(clearance_);
40 }
41
43 {
44 bool collisionFree = true;
45
46 // Check waypoints.
47 for (std::size_t i = 0; i < trajectory.points().size(); ++i)
48 {
49 const auto& p = trajectory.points()[i].waypoint.pose.translation();
50 if (isInCollision(p.x(), p.y()))
51 {
52 collisionFree = false;
53 if (logDetails)
54 {
55 ARMARX_INFO << "[TrajectoryChecker2D] Collision at waypoint " << i << " : ("
56 << p.x() << ", " << p.y() << ")";
57 }
58 }
59 }
60
61 // Check segments.
62 constexpr float segmentResolution = 50.0f;
63 for (std::size_t i = 0; i + 1 < trajectory.points().size(); ++i)
64 {
65 const auto& p1 = trajectory.points()[i].waypoint.pose.translation();
66 const auto& p2 = trajectory.points()[i + 1].waypoint.pose.translation();
67
68 const float dx = p2.x() - p1.x();
69 const float dy = p2.y() - p1.y();
70 const float dist = std::hypot(dx, dy);
71 int numSamples = static_cast<int>(std::ceil(dist / segmentResolution));
72 if (numSamples < 2)
73 numSamples = 2;
74
75 for (int s = 1; s < numSamples; ++s)
76 {
77 const float t = static_cast<float>(s) / numSamples;
78 const float x = p1.x() + t * dx;
79 const float y = p1.y() + t * dy;
80 if (isInCollision(x, y))
81 {
82 collisionFree = false;
83 if (logDetails)
84 {
85 ARMARX_INFO << "[TrajectoryChecker2D] Collision on segment " << i << "-"
86 << (i + 1) << " at t=" << t;
87 }
88 }
89 }
90 }
91
92 return collisionFree;
93 }
94
95} // namespace armarx::navigation::algorithms::spfa::smoothing
std::optional< float > value(const Index &index) const
Definition Costmap.cpp:541
TrajectoryChecker2D(const algorithms::Costmap &costmap, double clearance)
bool check(const core::GlobalTrajectory &trajectory, bool logDetails=false) const
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
This file offers overloads of toIce() and fromIce() functions for STL container types.