|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Open Source Robotics Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | +*/ |
| 17 | +#ifndef GZ_MATH_OCCUPANCY_GRID_HH_ |
| 18 | +#define GZ_MATH_OCCUPANCY_GRID_HH_ |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include <gz/math/Helpers.hh> |
| 24 | +#include <gz/utils/ImplPtr.hh> |
| 25 | + |
| 26 | +namespace gz::math |
| 27 | +{ |
| 28 | +inline namespace GZ_MATH_VERSION_NAMESPACE { |
| 29 | + |
| 30 | +/// \enum OccupancyCellState |
| 31 | +/// \brief Defines the state of a cell in the occupancy grid. |
| 32 | +enum class OccupancyCellState |
| 33 | +{ |
| 34 | + /// \brief The cell is considered free space. |
| 35 | + Free, |
| 36 | + /// \brief The cell is considered occupied. |
| 37 | + Occupied, |
| 38 | + /// \brief The state of the cell is unknown. |
| 39 | + Unknown |
| 40 | +}; |
| 41 | + |
| 42 | + /// \class OccupancyGrid OccupancyGrid.hh gz/math/OccupancyGrid.hh |
| 43 | + /// \brief Class representing an occupancy grid. |
| 44 | +class GZ_MATH_VISIBLE OccupancyGrid |
| 45 | +{ |
| 46 | + /// \brief Constructor. |
| 47 | + /// \param[in] _resolutionMeters Resolution of the grid in meters per cell. |
| 48 | + /// \param[in] _widthCells Width of the grid in cells. |
| 49 | + /// \param[in] _heightCells Height of the grid in cells. |
| 50 | + /// \param[in] _originX X-coordinate of the grid's origin in world |
| 51 | + /// coordinates. |
| 52 | + /// \param[in] _originY Y-coordinate of the grid's origin in world |
| 53 | + /// coordinates. |
| 54 | + public: OccupancyGrid(double _resolutionMeters, int _widthCells, |
| 55 | + int _heightCells, double _originX = 0.0, double _originY = 0.0); |
| 56 | + |
| 57 | + /// \brief Convert world coordinates (meters) to grid coordinates (cells). |
| 58 | + /// \param[in] _worldX World X coordinate in meters. |
| 59 | + /// \param[in] _worldY World Y coordinate in meters. |
| 60 | + /// \param[out] _gridX Grid X coordinate in cells. |
| 61 | + /// \param[out] _gridY Grid Y coordinate in cells. |
| 62 | + /// \return True if coordinates are within bounds, false otherwise. May also |
| 63 | + // be false if resolution is set to zero. |
| 64 | + public: bool WorldToGrid(double _worldX, double _worldY, int &_gridX, |
| 65 | + int &_gridY) const; |
| 66 | + |
| 67 | + /// \brief Convert grid coordinates (cells) to world coordinates (meters - |
| 68 | + /// center of cell). |
| 69 | + /// \param[in] _gridX Grid X coordinate in cells. |
| 70 | + /// \param[in] _gridY Grid Y coordinate in cells. |
| 71 | + /// \param[out] _worldX World X coordinate in meters. |
| 72 | + /// \param[out] _worldY World Y coordinate in meters. |
| 73 | + public: void GridToWorld(int _gridX, int _gridY, double &_worldX, |
| 74 | + double &_worldY) const; |
| 75 | + |
| 76 | + /// \brief Check if grid coordinates are within bounds. |
| 77 | + /// \param[in] _gridX Grid X coordinate in cells. |
| 78 | + /// \param[in] _gridY Grid Y coordinate in cells. |
| 79 | + /// \return True if the coordinates are valid, false otherwise. |
| 80 | + public: bool IsValidGridCoordinate(int _gridX, int _gridY) const; |
| 81 | + |
| 82 | + /// \brief Get the state of a cell. |
| 83 | + /// \param[in] _gridX Grid X coordinate in cells. |
| 84 | + /// \param[in] _gridY Grid Y coordinate in cells. |
| 85 | + /// \return The state of the cell. |
| 86 | + public: OccupancyCellState CellState(int _gridX, int _gridY) const; |
| 87 | + |
| 88 | + /// \brief Set the state of a cell. |
| 89 | + /// \param[in] _gridX Grid X coordinate in cells. |
| 90 | + /// \param[in] _gridY Grid Y coordinate in cells. |
| 91 | + /// \param[in] _state The new state of the cell. |
| 92 | + public: void CellState(int _gridX, int _gridY, OccupancyCellState _state); |
| 93 | + |
| 94 | + /// \brief Calculate information gain along a line. This is useful when |
| 95 | + /// implementing safe frontier exploration algorithms. |
| 96 | + /// \param[in] _x0 X coordinate of the start point in cells. |
| 97 | + /// \param[in] _y0 Y coordinate of the start point in cells. |
| 98 | + /// \param[in] _x1 X coordinate of the end point in cells. |
| 99 | + /// \param[in] _y1 Y coordinate of the end point in cells. |
| 100 | + /// \return The information gain. |
| 101 | + public: int CalculateIGain(int _x0, int _y0, int _x1, int _y1); |
| 102 | + |
| 103 | + /// \brief Use Bresenham's Line Algorithm to mark cells along a line. This |
| 104 | + /// function will not modify cells that are already marked as Occupied. |
| 105 | + /// It marks cells from (_x0, _y0) to (_x1, _y1) with the specified state. |
| 106 | + /// \param[in] _x0 X coordinate of the start point in cells. |
| 107 | + /// \param[in] _y0 Y coordinate of the start point in cells. |
| 108 | + /// \param[in] _x1 X coordinate of the end point in cells. |
| 109 | + /// \param[in] _y1 Y coordinate of the end point in cells. |
| 110 | + /// \param[in] _state The state to mark the cells with. |
| 111 | + public: void MarkLine(int _x0, int _y0, int _x1, int _y1, |
| 112 | + OccupancyCellState _state); |
| 113 | + |
| 114 | + /// \brief Mark a single point as occupied (e.g., an obstacle |
| 115 | + /// detection). |
| 116 | + /// \param[in] _worldX World X coordinate in meters. |
| 117 | + /// \param[in] _worldY World Y coordinate in meters. |
| 118 | + /// \returns true if the grid was inside the coordinates, false otherwise |
| 119 | + public: bool MarkOccupied(double _worldX, double _worldY); |
| 120 | + |
| 121 | + /// \brief Mark a path as free (e.g., a clear line of sight). |
| 122 | + /// \param[in] _worldX0 World X coordinate of the start point in meters. |
| 123 | + /// \param[in] _worldY0 World Y coordinate of the start point in meters. |
| 124 | + /// \param[in] _worldX1 World X coordinate of the end point in meters. |
| 125 | + /// \param[in] _worldY1 World Y coordinate of the end point in meters. |
| 126 | + /// \returns true if the grid was inside the coordinates, false otherwise |
| 127 | + public: bool MarkFree(double _worldX0, double _worldY0, double _worldX1, |
| 128 | + double _worldY1); |
| 129 | + |
| 130 | + /// \brief Export the occupancy grid to a RGB image buffer. |
| 131 | + /// \param[out] _pixels The output buffer to store the RGB image data. |
| 132 | + public: void ExportToRGBImage(std::vector<uint8_t> &_pixels) const; |
| 133 | + |
| 134 | + /// \brief Export the occupancy grid to a raw buffer. |
| 135 | + /// \param[out] _data The output buffer to store the raw occupancy data. |
| 136 | + public: void RawOccupancy(std::vector<int8_t> &_data) const; |
| 137 | + |
| 138 | + /// \brief Get the resolution of the occupancy grid. |
| 139 | + /// \return The resolution in meters per cell. |
| 140 | + public: double Resolution() const; |
| 141 | + |
| 142 | + /// \brief Get the number of cells in width. |
| 143 | + /// \return The width of the grid in cells. |
| 144 | + public: int Width() const; |
| 145 | + |
| 146 | + /// \brief Get the number of cells in height. |
| 147 | + /// \return The height of the grid in cells. |
| 148 | + public: int Height() const; |
| 149 | + |
| 150 | + /// \brief Get the origin X position. |
| 151 | + /// \return The X-coordinate of the grid's origin in world coordinates. |
| 152 | + public: double OriginX() const; |
| 153 | + |
| 154 | + /// \brief Get the origin Y position. |
| 155 | + /// \return The Y-coordinate of the grid's origin in world coordinates. |
| 156 | + public: double OriginY() const; |
| 157 | + |
| 158 | + /// \brief Private data pointer. |
| 159 | + GZ_UTILS_IMPL_PTR(dataPtr) |
| 160 | +}; |
| 161 | +} |
| 162 | +} // namespace gz::math |
| 163 | + |
| 164 | +#endif |
0 commit comments