EasyNav Simple Stack
Loading...
Searching...
No Matches
SimpleMap.hpp
Go to the documentation of this file.
1// Copyright 2025 Intelligent Robotics Lab
2//
3// This file is part of the project Easy Navigation (EasyNav in short)
4// licensed under the GNU General Public License v3.0.
5// See <http://www.gnu.org/licenses/> for details.
6//
7// Easy Navigation program is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
22
23#ifndef EASYNAV_PLANNER__SIMPLEMAP_HPP_
24#define EASYNAV_PLANNER__SIMPLEMAP_HPP_
25
26#include <vector>
27#include <stdexcept>
28#include <algorithm>
29#include <utility>
30#include <fstream>
31#include <sstream>
32
33#include "nav_msgs/msg/occupancy_grid.hpp"
34
35
36namespace easynav
37{
38
46{
47public:
53 SimpleMap();
54
65 void initialize(
66 int width, int height, double resolution, double origin_x,
67 double origin_y, bool initial_value = false);
68
72 size_t width() const {return width_;}
73
77 size_t height() const {return height_;}
78
82 double resolution() const {return resolution_;}
83
87 double origin_x() const {return origin_x_;}
88
92 double origin_y() const {return origin_y_;}
93
98 uint8_t at(int x, int y) const;
99
104 uint8_t & at(int x, int y);
105
111 void fill(uint8_t value);
112
120 void deep_copy(const SimpleMap & other);
121
129 bool check_bounds_metric(double mx, double my) const;
130
138 std::pair<double, double> cell_to_metric(int x, int y) const;
139
148 std::pair<int, int> metric_to_cell(double mx, double my) const;
149
161 void to_occupancy_grid(nav_msgs::msg::OccupancyGrid & grid_msg) const;
162
171 void from_occupancy_grid(const nav_msgs::msg::OccupancyGrid & grid_msg);
172
178 bool save_to_file(const std::string & path) const;
179
185 bool load_from_file(const std::string & path);
186
192 void print(bool view_data = false) const;
193
200 std::shared_ptr<SimpleMap> downsample_factor(int factor) const;
201
208 std::shared_ptr<SimpleMap> downsample(double new_resolution) const;
209
210private:
211 size_t width_;
212 size_t height_;
213 double resolution_;
214 double origin_x_;
215 double origin_y_;
216 std::vector<uint8_t> data_;
217
218 int index(int x, int y) const;
219 void check_bounds(size_t x, size_t y) const;
220};
221
222} // namespace easynav
223
224#endif // EASYNAV_PLANNER__SIMPLEMAP_HPP_
Simple 2D uint8_t grid using basic C++ types, with full metric conversion support.
Definition SimpleMap.hpp:46
double origin_y() const
Returns the metric origin y coordinate.
Definition SimpleMap.hpp:92
SimpleMap()
Default constructor.
Definition SimpleMap.cpp:37
void from_occupancy_grid(const nav_msgs::msg::OccupancyGrid &grid_msg)
Load map data and metadata from a nav_msgs::msg::OccupancyGrid message.
Definition SimpleMap.cpp:148
std::shared_ptr< SimpleMap > downsample_factor(int factor) const
Creates a downsampled version of the map by an integer factor.
Definition SimpleMap.cpp:273
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.
Definition SimpleMap.cpp:42
std::pair< int, int > metric_to_cell(double mx, double my) const
Converts real-world metric coordinates (meters) to a cell index (x, y).
Definition SimpleMap.cpp:111
bool check_bounds_metric(double mx, double my) const
Checks if given metric coordinates are within the map bounds.
Definition SimpleMap.cpp:87
void print(bool view_data=false) const
Prints metadata and optionally all map cell values with coordinates.
Definition SimpleMap.cpp:252
std::pair< double, double > cell_to_metric(int x, int y) const
Converts a cell index (x, y) to real-world metric coordinates (meters).
Definition SimpleMap.cpp:103
std::shared_ptr< SimpleMap > downsample(double new_resolution) const
Creates a downsampled version of the map to match a target resolution.
Definition SimpleMap.cpp:307
double origin_x() const
Returns the metric origin x coordinate.
Definition SimpleMap.hpp:87
size_t width() const
Returns the width (number of columns) of the map.
Definition SimpleMap.hpp:72
void fill(uint8_t value)
Set all cells to a given value.
Definition SimpleMap.cpp:70
void to_occupancy_grid(nav_msgs::msg::OccupancyGrid &grid_msg) const
Updates a nav_msgs::msg::OccupancyGrid message from the SimpleMap contents.
Definition SimpleMap.cpp:123
bool load_from_file(const std::string &path)
Loads the map from a file, reading metadata and cell data.
Definition SimpleMap.cpp:192
void deep_copy(const SimpleMap &other)
Performs a deep copy from another SimpleMap.
Definition SimpleMap.cpp:76
uint8_t at(int x, int y) const
Access a cell (const) at (x, y).
Definition SimpleMap.cpp:55
bool save_to_file(const std::string &path) const
Saves the map to a file, including metadata and cell data.
Definition SimpleMap.cpp:168
size_t height() const
Returns the height (number of rows) of the map.
Definition SimpleMap.hpp:77
double resolution() const
Returns the resolution (cell size in meters).
Definition SimpleMap.hpp:82
Definition SimpleMap.hpp:37