Add max_deviation_deg (default 90, valid 0 < x < 180). The field carries the value so checkSegment and every planner use the same rule. 90 keeps the previous masks bit for bit; out-of-range values stop the layer from initialising.
226 lines
9.2 KiB
C++
226 lines
9.2 KiB
C++
#ifndef ROBOT_COSTMAP_2D_DIRECTION_FIELD_H_
|
|
#define ROBOT_COSTMAP_2D_DIRECTION_FIELD_H_
|
|
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <robot_nav_msgs/OccupancyGrid.h>
|
|
|
|
namespace robot_costmap_2d
|
|
{
|
|
|
|
/** @brief Geometry of an unrotated grid in its global frame. */
|
|
struct DirectionGeometry
|
|
{
|
|
uint32_t width, height; ///< Grid dimensions [cells].
|
|
double resolution; ///< Cell size [m/cell].
|
|
double origin_x, origin_y; ///< Lower-left grid origin [m] in frame_id.
|
|
std::string frame_id; ///< Global coordinate frame.
|
|
};
|
|
|
|
/** @brief Reason a complete direction grid failed validation. */
|
|
enum class DirectionValidationError
|
|
{
|
|
kNone, kOverflow, kGeometry, kFrame, kOrientation, kSize, kCode
|
|
};
|
|
|
|
/** @brief Typed validation result with a human-readable offending value. */
|
|
struct DirectionValidationResult
|
|
{
|
|
DirectionValidationError error;
|
|
std::string message;
|
|
explicit operator bool() const { return error == DirectionValidationError::kNone; }
|
|
};
|
|
|
|
/** @brief Validate the entire message against the master geometry.
|
|
* @param grid Grid in the expected global frame. Codes are 0 for unconstrained
|
|
* and 1..8 for direction indices 0..7 in CCW order from +X:
|
|
* (+1,0), (+1,+1), (0,+1), (-1,+1), (-1,0), (-1,-1), (0,-1), (+1,-1).
|
|
* @param expected Required master dimensions, resolution [m/cell], origin [m], and frame.
|
|
* @return Typed error and description; empty data is valid after geometry checks.
|
|
* An identity quaternion or the default all-zero quaternion means no rotation.
|
|
*/
|
|
DirectionValidationResult validateDirectionGrid(const robot_nav_msgs::OccupancyGrid& grid,
|
|
const DirectionGeometry& expected);
|
|
|
|
/** @brief Traverse a segment in grid coordinates with allocation-free 2D DDA.
|
|
* @param x0 Start x [cells] relative to the grid origin.
|
|
* @param y0 Start y [cells] relative to the grid origin.
|
|
* @param x1 End x [cells] relative to the grid origin.
|
|
* @param y1 End y [cells] relative to the grid origin.
|
|
* @param visitor Called in order with signed integer cell indices (x, y); return
|
|
* false to stop. The visitor must perform its own grid bounds checks.
|
|
* @return False for invalid coordinates or a visitor-requested stop.
|
|
* Cells own [x,x+1) x [y,y+1). Only positive-length overlaps are visited:
|
|
* a boundary-aligned segment belongs to the higher-index cell, a corner touch
|
|
* does not visit the adjacent cells, and an endpoint on a boundary does not
|
|
* visit the cell beyond it. A zero-length segment visits its point's cell.
|
|
* Coordinates must fit within the documented int64_t safety range.
|
|
*/
|
|
template <typename Visitor>
|
|
bool traverseGridSegment(double x0, double y0, double x1, double y1, Visitor&& visitor)
|
|
{
|
|
const double limit = static_cast<double>(std::numeric_limits<int64_t>::max()) / 2.0;
|
|
if (!std::isfinite(x0) || !std::isfinite(y0) || !std::isfinite(x1) || !std::isfinite(y1) ||
|
|
std::abs(x0) >= limit || std::abs(y0) >= limit || std::abs(x1) >= limit || std::abs(y1) >= limit)
|
|
return false;
|
|
|
|
const long double dx = static_cast<long double>(x1) - x0;
|
|
const long double dy = static_cast<long double>(y1) - y0;
|
|
int64_t x = static_cast<int64_t>(std::floor(x0));
|
|
int64_t y = static_cast<int64_t>(std::floor(y0));
|
|
if (dx < 0 && x0 == std::floor(x0))
|
|
--x;
|
|
if (dy < 0 && y0 == std::floor(y0))
|
|
--y;
|
|
if (dx == 0 && dy == 0)
|
|
return visitor(x, y);
|
|
|
|
const int sx = (dx > 0) - (dx < 0);
|
|
const int sy = (dy > 0) - (dy < 0);
|
|
const long double adx = std::abs(dx);
|
|
const long double ady = std::abs(dy);
|
|
while (true)
|
|
{
|
|
if (!visitor(x, y))
|
|
return false;
|
|
// Recompute crossings from the original point to avoid accumulated error at corners.
|
|
const long double distance_x = sx ? std::abs((sx > 0 ? x + 1 : x) - static_cast<long double>(x0)) : 0;
|
|
const long double distance_y = sy ? std::abs((sy > 0 ? y + 1 : y) - static_cast<long double>(y0)) : 0;
|
|
const bool cross_x = sx && distance_x < adx;
|
|
const bool cross_y = sy && distance_y < ady;
|
|
if (!cross_x && !cross_y)
|
|
break;
|
|
if (!cross_y)
|
|
{
|
|
x += sx;
|
|
continue;
|
|
}
|
|
if (!cross_x)
|
|
{
|
|
y += sy;
|
|
continue;
|
|
}
|
|
const long double left = distance_x * ady;
|
|
const long double right = distance_y * adx;
|
|
if (left <= right)
|
|
x += sx;
|
|
if (right <= left)
|
|
y += sy;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/** @brief Return allowed direction indices for a displacement.
|
|
* @param dx Displacement along +X [m or cells] in the global/grid frame.
|
|
* @param dy Displacement along +Y [same units as dx] in the global/grid frame.
|
|
* @param max_deviation_rad Maximum inclusive angular deviation [rad], in (0, pi).
|
|
* @return Bit k is set when direction k accepts the displacement within relative tolerance.
|
|
*/
|
|
uint8_t allowedDirectionMask(double dx, double dy, double max_deviation_rad);
|
|
|
|
/** @brief Outcome of a world-space segment query. */
|
|
enum class SegmentResult
|
|
{
|
|
kAllowed, ///< All visited constrained cells permit the displacement.
|
|
kViolation, ///< At least one visited cell forbids the displacement.
|
|
kOutOfGrid ///< Any point is outside the grid or coordinates are invalid.
|
|
};
|
|
|
|
/** @brief First prohibited cell and world-frame segment displacement. */
|
|
struct SegmentViolation
|
|
{
|
|
uint32_t x, y; ///< Cell indices [cells] relative to the grid origin.
|
|
uint8_t code; ///< Direction code 1..8.
|
|
double dx, dy; ///< Segment displacement [m] in the global frame.
|
|
};
|
|
|
|
/** @brief Immutable validated grid, safe to share and hold for a whole search. */
|
|
class DirectionField
|
|
{
|
|
public:
|
|
DirectionField(const DirectionField&) = delete;
|
|
DirectionField& operator=(const DirectionField&) = delete;
|
|
|
|
/** @brief Validate and construct a field; invalid input returns null.
|
|
* @param grid Complete direction message in the global frame.
|
|
* @param expected Master grid geometry in the global frame.
|
|
* @param max_deviation_rad Maximum inclusive deviation [rad], in (0, pi).
|
|
* @param result Optional output explaining validation success or failure.
|
|
*/
|
|
static std::shared_ptr<const DirectionField> create(const robot_nav_msgs::OccupancyGrid& grid,
|
|
const DirectionGeometry& expected,
|
|
double max_deviation_rad,
|
|
DirectionValidationResult* result = nullptr);
|
|
|
|
/** @brief Return cell code: 0 unconstrained, 1..8 direction_index + 1.
|
|
* @param x Column [cells] from the grid origin; must be less than width.
|
|
* @param y Row [cells] from the grid origin; must be less than height.
|
|
* @pre Both indices are in bounds. Use codeAt() for untrusted indices.
|
|
*/
|
|
uint8_t code(uint32_t x, uint32_t y) const;
|
|
|
|
/** @brief Read a cell only when both signed indices are in bounds.
|
|
* @param x Column [cells] from the grid origin.
|
|
* @param y Row [cells] from the grid origin.
|
|
* @param code_out Output code; must not be null. Unchanged on failure.
|
|
* @return False for an out-of-grid index or null output pointer.
|
|
*/
|
|
bool codeAt(int64_t x, int64_t y, uint8_t* code_out) const;
|
|
|
|
/** @brief Return the immutable master geometry and global frame. */
|
|
const DirectionGeometry& geometry() const { return geometry_; }
|
|
/** @brief Return the immutable maximum angular deviation [rad]. */
|
|
double maxDeviationRad() const { return max_deviation_rad_; }
|
|
/** @brief True if at least one cell has a nonzero direction code. */
|
|
bool hasConstraints() const { return has_constraints_; }
|
|
|
|
/** @brief Check all owned cells traversed by a world-space segment.
|
|
* @param wx0 Start x [m] in the global frame.
|
|
* @param wy0 Start y [m] in the global frame.
|
|
* @param wx1 End x [m] in the global frame.
|
|
* @param wy1 End y [m] in the global frame.
|
|
* @param first Optional output populated on kViolation.
|
|
* @return kAllowed, kViolation, or kOutOfGrid; see SegmentResult.
|
|
*/
|
|
SegmentResult checkSegment(double wx0, double wy0, double wx1, double wy1,
|
|
SegmentViolation* first = nullptr) const;
|
|
|
|
private:
|
|
DirectionField(const robot_nav_msgs::OccupancyGrid& grid, const DirectionGeometry& geometry,
|
|
double max_deviation_rad);
|
|
DirectionGeometry geometry_;
|
|
std::vector<uint8_t> codes_;
|
|
double max_deviation_rad_;
|
|
bool has_constraints_;
|
|
};
|
|
|
|
/** @brief Disabled, waiting for a current grid, or ready for queries. */
|
|
enum class DirectionFieldState { kDisabled, kPending, kReady };
|
|
|
|
/** @brief Consistent state and generation with an immutable, shared field.
|
|
* The field is non-null only in kReady. Readers may hold it through a whole
|
|
* search while the layer publishes a newer generation on another thread.
|
|
*/
|
|
struct DirectionFieldSnapshot
|
|
{
|
|
DirectionFieldState state = DirectionFieldState::kDisabled;
|
|
std::shared_ptr<const DirectionField> field;
|
|
uint64_t generation = 0;
|
|
};
|
|
|
|
/** @brief Core-side snapshot contract; avoids linking core against libplugins. */
|
|
class DirectionFieldProvider
|
|
{
|
|
public:
|
|
virtual ~DirectionFieldProvider() = default;
|
|
/** @brief Return a consistent snapshot safe to retain on a planner thread. */
|
|
virtual DirectionFieldSnapshot directionSnapshot() const = 0;
|
|
};
|
|
} // namespace robot_costmap_2d
|
|
#endif // ROBOT_COSTMAP_2D_DIRECTION_FIELD_H_
|