Files
voronoi_route/include/voronoi_route/map_convert.h
2026-09-22 15:44:53 +07:00

128 lines
6.3 KiB
C++

#ifndef VORONOI_ROUTE_MAP_CONVERT_H_
#define VORONOI_ROUTE_MAP_CONVERT_H_
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
namespace voronoi_route {
/**
* The costmap's own reading of an OccupancyGrid value — a port of
* `robot_costmap_2d::StaticLayer::interpretValue()` with the same parameter names, so a Voronoi
* built here calls the same cells "obstacle" as the static layer the planner runs on. Values are
* the ones `static_layer_params.yaml` (block `static_layer:`) carries; overrides per layer come
* from the costmap YAML (`global_costmap/<layer>/...`).
*/
struct StaticLayerRule {
bool track_unknown_space = true; ///< unknown -> NO_INFORMATION (true) or FREE_SPACE (false)
int lethal_cost_threshold = 100; ///< value >= this -> LETHAL_OBSTACLE
int unknown_cost_value = -1; ///< the grid value that means "unknown"
bool trinary_costmap = true; ///< true: everything below lethal is FREE; false: scaled cost
};
/** What the planner does with NO_INFORMATION cells: SBPL `allow_unknown`. */
enum class UnknownPolicy : uint8_t {
kTraversable, ///< allow_unknown = true (SBPL default): unknown is not an obstacle
kObstacle ///< allow_unknown = false: unknown blocks the robot
};
/** Full obstacle rule: how a grid value becomes a cost class, and which classes block. */
struct ObstacleRule {
StaticLayerRule layer;
UnknownPolicy unknown = UnknownPolicy::kTraversable;
/**
* Region of interest: the bounding box of obstacle cells grown by this margin; everything
* outside counts as obstacle. Kills the Voronoi rays that shoot from every wall gap to the image
* edge when the outside is free/unknown-traversable, and skips the distance transform over
* millions of empty cells. < 0 = off. The consumer must apply the same margin (it is part of the
* rule string) — the box is derived from the map itself, so both sides get the same box.
*/
int roi_margin_cells = -1;
};
/** Cost classes the static layer can produce. `kScaled` only when !trinary_costmap. */
enum class CostClass : uint8_t { kFree, kLethal, kNoInformation, kScaled };
/** Costmap cost values as `robot_costmap_2d/cost_values.h` defines them (copied, no dependency). */
constexpr unsigned char kCostmapLethalObstacle = 254;
constexpr unsigned char kCostmapNoInformation = 255;
/** map_server YAML fields that decide what an image pixel means. */
struct MapServerThresholds {
bool negate = false;
double occupied_thresh = 0.65;
double free_thresh = 0.196;
};
struct MapConvertStats {
size_t occupied = 0; ///< cells classified kLethal
size_t unknown = 0; ///< cells classified kNoInformation
size_t free = 0; ///< everything else
};
/** StaticLayer::interpretValue(), classification only. */
CostClass classifyGridValue(int value, const StaticLayerRule& rule);
/** True when a cell of this class is an obstacle for the Voronoi diagram under `rule`. */
bool isObstacleClass(CostClass c, const ObstacleRule& rule);
/**
* @brief Map image (any channel count) -> obstacle mask, by the ROS map_server rule in TRINARY
* mode, which is what the robot's static layer actually receives.
*
* map_server (`image_loader.cpp`) averages ALL channels of a pixel — alpha included in trinary
* mode — then occ = (255 - avg) / 255 (avg / 255 when negate); occ > occupied_thresh -> 100,
* occ < free_thresh -> 0, otherwise -1. On an RGBA map a gray pixel (79,79,79,255) therefore
* averages to 123 and is UNKNOWN, not occupied, and (205,205,205,255) averages to 217 and is
* FREE. Reading the same file as luminance gives a different lethal set and a different hash, so
* the producer must apply this rule, not a decoder's grayscale conversion.
*
* Then value -> class by `rule.layer`, class -> obstacle by `rule.unknown`. `pixels` is row-major,
* row 0 = top, `channels` bytes per pixel; the mask keeps that layout.
*/
std::vector<uint8_t> occupancyFromImage(const uint8_t* pixels, int width, int height, int channels,
const MapServerThresholds& thresholds,
const ObstacleRule& rule, MapConvertStats* stats = nullptr);
/** Single-channel convenience: occupancyFromImage(gray, w, h, 1, ...). */
std::vector<uint8_t> occupancyFromGray(const uint8_t* gray, int width, int height,
const MapServerThresholds& thresholds,
const ObstacleRule& rule, MapConvertStats* stats = nullptr);
/**
* @brief OccupancyGrid int8 array -> obstacle mask, by `rule`. Input is ROS layout (row 0 =
* bottom); output is flipped to row 0 = top to match the image layout used everywhere else.
*/
std::vector<uint8_t> occupancyFromGrid(const int8_t* data, int width, int height,
const ObstacleRule& rule, MapConvertStats* stats = nullptr);
/**
* @brief Live costmap char map (values 0..255 as the static layer wrote them) -> obstacle mask.
* This is what a runtime consumer hashes to decide whether a bundle matches:
* 254 -> obstacle; 255 -> obstacle only under UnknownPolicy::kObstacle; else free.
* @param rows_bottom_up true for a costmap (row 0 = bottom): the mask is flipped to row 0 = top.
*/
std::vector<uint8_t> occupancyFromCostmap(const unsigned char* charmap, int width, int height,
UnknownPolicy unknown, bool rows_bottom_up);
/**
* @brief Apply ObstacleRule::roi_margin_cells to a mask in place: cells outside the grown bounding
* box of the mask's obstacle cells become obstacles. No-op when margin < 0 or when the mask
* has fewer than `min_obstacle_cells` obstacles (a near-empty map has no meaningful box).
* @return number of cells turned into obstacles.
*/
size_t applyObstacleRoi(std::vector<uint8_t>& mask, int width, int height, int margin_cells,
size_t min_obstacle_cells = 16);
/** Compact, stable description for the bundle's `source` field, e.g. "lethal>=100;...;unknown=traversable;roi=40". */
std::string obstacleRuleString(const ObstacleRule& rule);
/** Inverse of obstacleRuleString(); false if the string is not one it produced. */
bool parseObstacleRuleString(const std::string& s, ObstacleRule* rule);
} // namespace voronoi_route
#endif // VORONOI_ROUTE_MAP_CONVERT_H_