Class SimpleMap

Class Documentation

class SimpleMap

Simple 2D uint8_t grid using basic C++ types, with full metric conversion support.

Supports arbitrary metric origins, allowing negative coordinates.

Public Functions

SimpleMap()

Default constructor.

Creates an empty (0x0) SimpleMap with 1.0 meter resolution and origin (0.0, 0.0).

void initialize(int width, int height, double resolution, double origin_x, double origin_y, bool initial_value = false)

Initialize the map to new dimensions, resolution, and origin.

Parameters:
  • width – Number of columns.

  • height – Number of rows.

  • resolution – Size of each cell in meters.

  • origin_x – Metric X coordinate corresponding to cell (0,0).

  • origin_y – Metric Y coordinate corresponding to cell (0,0).

  • initial_value – Value to initialize all cells.

inline size_t width() const

Returns the width (number of columns) of the map.

inline size_t height() const

Returns the height (number of rows) of the map.

inline double resolution() const

Returns the resolution (cell size in meters).

inline double origin_x() const

Returns the metric origin x coordinate.

inline double origin_y() const

Returns the metric origin y coordinate.

uint8_t at(int x, int y) const

Access a cell (const) at (x, y).

Throws:

std::out_of_range – if (x,y) is out of bounds.

uint8_t &at(int x, int y)

Access a cell (non-const) at (x, y).

Throws:

std::out_of_range – if (x,y) is out of bounds.

void fill(uint8_t value)

Set all cells to a given value.

Parameters:

value – Value to fill the map with.

void deep_copy(const SimpleMap &other)

Performs a deep copy from another SimpleMap.

Copies width, height, resolution, origin, and all grid data.

Parameters:

other – The SimpleMap to copy from.

bool check_bounds_metric(double mx, double my) const

Checks if given metric coordinates are within the map bounds.

Parameters:
  • mx – Metric x coordinate.

  • my – Metric y coordinate.

Returns:

true if inside the map bounds, false otherwise.

std::pair<double, double> cell_to_metric(int x, int y) const

Converts a cell index (x, y) to real-world metric coordinates (meters).

Parameters:
  • x – Cell column index.

  • y – Cell row index.

Returns:

Pair (meters_x, meters_y).

std::pair<int, int> metric_to_cell(double mx, double my) const

Converts real-world metric coordinates (meters) to a cell index (x, y).

Parameters:
  • mx – Metric x coordinate.

  • my – Metric y coordinate.

Throws:

std::out_of_range – if resulting indices are out of map bounds.

Returns:

Pair (x, y) cell indices.

void to_occupancy_grid(nav_msgs::msg::OccupancyGrid &grid_msg) const

Updates a nav_msgs::msg::OccupancyGrid message from the SimpleMap contents.

Cells are mapped as follows:

  • true -> 100 (occupied)

  • false -> 0 (free)

If the grid dimensions do not match, the message is rdata_esized automatically.

Parameters:

grid_msg – The occupancy grid message to fill or update.

void from_occupancy_grid(const nav_msgs::msg::OccupancyGrid &grid_msg)

Load map data and metadata from a nav_msgs::msg::OccupancyGrid message.

This function resizes the internal grid to match the occupancy grid dimensions, sets the resolution and origin, and copies the data.

Parameters:

grid_msg – The occupancy grid message to load from.

bool save_to_file(const std::string &path) const

Saves the map to a file, including metadata and cell data.

Parameters:

path – Path to the output file.

Returns:

true if the file was written successfully, false otherwise.

bool load_from_file(const std::string &path)

Loads the map from a file, reading metadata and cell data.

Parameters:

path – Path to the input file.

Returns:

true if the file was read successfully and is valid, false otherwise.

void print(bool view_data = false) const

Prints metadata and optionally all map cell values with coordinates.

Parameters:

view_data – If true, prints cell-by-cell content with coordinates. Default is false.

std::shared_ptr<SimpleMap> downsample_factor(int factor) const

Creates a downsampled version of the map by an integer factor.

Parameters:

factor – Integer factor (>1) to reduce resolution.

Returns:

A shared pointer to the new downsampled SimpleMap.

std::shared_ptr<SimpleMap> downsample(double new_resolution) const

Creates a downsampled version of the map to match a target resolution.

Parameters:

new_resolution – New desired resolution (must be a multiple of current).

Returns:

A shared pointer to the new downsampled SimpleMap.