|
| 1 | +/* |
| 2 | + * Copyright (c) 2008, Willow Garage, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * * Neither the name of the Willow Garage, Inc. nor the names of its |
| 14 | + * contributors may be used to endorse or promote products derived from |
| 15 | + * this software without specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | + * POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | + */ |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * \file |
| 34 | + * |
| 35 | + * General utilities for coordinate conversions |
| 36 | + * |
| 37 | + * \author Bhaskara Marthi |
| 38 | + */ |
| 39 | + |
| 40 | + |
| 41 | +#ifndef OCCUPANCY_GRID_UTILS_COORDINATE_CONVERSIONS_H |
| 42 | +#define OCCUPANCY_GRID_UTILS_COORDINATE_CONVERSIONS_H |
| 43 | + |
| 44 | +#include <occupancy_grid_utils/exceptions.h> |
| 45 | +#include <tf/transform_datatypes.h> |
| 46 | +#include <nav_msgs/OccupancyGrid.h> |
| 47 | +#include <geometry_msgs/Polygon.h> |
| 48 | +#include <ros/assert.h> |
| 49 | +#include <cstdlib> |
| 50 | +#include <ostream> |
| 51 | + |
| 52 | +namespace occupancy_grid_utils |
| 53 | +{ |
| 54 | + |
| 55 | +/************************************************************ |
| 56 | + * Public interface |
| 57 | + ************************************************************/ |
| 58 | + |
| 59 | +typedef uint32_t index_t; |
| 60 | +typedef int16_t coord_t; |
| 61 | + |
| 62 | +struct Cell |
| 63 | +{ |
| 64 | + Cell(const coord_t x=0, const coord_t y=0) : x(x), y(y) {} |
| 65 | + coord_t x; |
| 66 | + coord_t y; |
| 67 | + |
| 68 | + bool operator== (const Cell& c) const; |
| 69 | + bool operator< (const Cell& c) const; |
| 70 | +}; |
| 71 | + |
| 72 | + |
| 73 | +// These values have conventional meanings for occupancy grids |
| 74 | +const int8_t UNOCCUPIED=0; |
| 75 | +const int8_t OCCUPIED=100; |
| 76 | +const int8_t UNKNOWN=255; |
| 77 | + |
| 78 | + |
| 79 | +/// \brief Returns the index of a cell. |
| 80 | +/// |
| 81 | +/// \throws CellOutOfBoundsException if cell isn't within grid bounds |
| 82 | +index_t cellIndex (const nav_msgs::MapMetaData& info, const Cell& c); |
| 83 | + |
| 84 | +/// \brief Returns cell corresponding to index |
| 85 | +Cell indexCell (const nav_msgs::MapMetaData& info, index_t ind); |
| 86 | + |
| 87 | +/// \brief Returns cell corresponding to a point. |
| 88 | +/// |
| 89 | +/// The z coordinate of the point is ignored. |
| 90 | +Cell pointCell (const nav_msgs::MapMetaData& info, const geometry_msgs::Point& p); |
| 91 | + |
| 92 | +/// \brief Returns index of a point. |
| 93 | +/// \throws CellOutOfBoundsException if point isn't within grid bounds |
| 94 | +/// |
| 95 | +/// Ignores z coordinate of point |
| 96 | +index_t pointIndex (const nav_msgs::MapMetaData& info, const geometry_msgs::Point& p); |
| 97 | + |
| 98 | +/// \brief Return center of a cell |
| 99 | +geometry_msgs::Point cellCenter (const nav_msgs::MapMetaData& info, const Cell& c); |
| 100 | + |
| 101 | +/// \brief Return polygon corresponding to a cell |
| 102 | +geometry_msgs::Polygon cellPolygon (const nav_msgs::MapMetaData& info, const Cell& c); |
| 103 | + |
| 104 | +/// \brief Check if a point is on the grid |
| 105 | +bool withinBounds (const nav_msgs::MapMetaData& info, const geometry_msgs::Point& p); |
| 106 | + |
| 107 | +/// \brief Check if a cell is on the grid |
| 108 | +bool withinBounds (const nav_msgs::MapMetaData& info, const Cell& c); |
| 109 | + |
| 110 | +/// \brief Return polygon corresponding to grid bounds |
| 111 | +geometry_msgs::Polygon gridPolygon (const nav_msgs::MapMetaData& info); |
| 112 | + |
| 113 | +/// \brief Verify that data vector has the right size, throw |
| 114 | +/// DataSizeException otherwise |
| 115 | +void verifyDataSize (const nav_msgs::OccupancyGrid& g); |
| 116 | + |
| 117 | +/************************************************************ |
| 118 | + * Implementations of inline functions |
| 119 | + ************************************************************/ |
| 120 | + |
| 121 | +inline |
| 122 | +index_t cellIndex (const nav_msgs::MapMetaData& info, const Cell& c) |
| 123 | +{ |
| 124 | + if (!withinBounds(info, c)) |
| 125 | + throw CellOutOfBoundsException(c.x, c.y); |
| 126 | + return c.x + c.y*info.width; |
| 127 | +} |
| 128 | + |
| 129 | +inline |
| 130 | +Cell indexCell (const nav_msgs::MapMetaData& info, const index_t ind) |
| 131 | +{ |
| 132 | + const div_t result = div((int) ind, (int) info.width); |
| 133 | + return Cell(result.rem, result.quot); |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +inline |
| 138 | +tf::Transform mapToWorld (const nav_msgs::MapMetaData& info) |
| 139 | +{ |
| 140 | + tf::Transform world_to_map; |
| 141 | + tf::poseMsgToTF (info.origin, world_to_map); |
| 142 | + return world_to_map; |
| 143 | +} |
| 144 | + |
| 145 | +inline |
| 146 | +tf::Transform worldToMap (const nav_msgs::MapMetaData& info) |
| 147 | +{ |
| 148 | + return mapToWorld(info).inverse(); |
| 149 | +} |
| 150 | + |
| 151 | +inline |
| 152 | +Cell pointCell (const nav_msgs::MapMetaData& info, const geometry_msgs::Point& p) |
| 153 | +{ |
| 154 | + tf::Point pt; |
| 155 | + tf::pointMsgToTF(p, pt); |
| 156 | + tf::Point p2 = worldToMap(info)*pt; |
| 157 | + return Cell(floor(p2.x()/info.resolution), floor(p2.y()/info.resolution)); |
| 158 | +} |
| 159 | + |
| 160 | +inline |
| 161 | +index_t pointIndex (const nav_msgs::MapMetaData& info, const geometry_msgs::Point& p) |
| 162 | +{ |
| 163 | + return cellIndex(info, pointCell(info, p)); |
| 164 | +} |
| 165 | + |
| 166 | +inline |
| 167 | +geometry_msgs::Point cellCenter (const nav_msgs::MapMetaData& info, const Cell& c) |
| 168 | +{ |
| 169 | + tf::Point pt((c.x+0.5)*info.resolution, (c.y+0.5)*info.resolution, 0.0); |
| 170 | + geometry_msgs::Point p; |
| 171 | + tf::pointTFToMsg(mapToWorld(info)*pt, p); |
| 172 | + return p; |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +inline |
| 177 | +bool Cell::operator== (const Cell& c) const |
| 178 | +{ |
| 179 | + return ((this->x == c.x) && (this->y == c.y)); |
| 180 | +} |
| 181 | + |
| 182 | +inline |
| 183 | +bool Cell::operator< (const Cell& c) const |
| 184 | +{ |
| 185 | + return ((this->x < c.x) || ((this->x == c.x) && (this->y < c.y))); |
| 186 | +} |
| 187 | + |
| 188 | +inline |
| 189 | +std::ostream& operator<< (std::ostream& str, const Cell& c) |
| 190 | +{ |
| 191 | + str << "(" << c.x << ", " << c.y << ")"; |
| 192 | + return str; |
| 193 | +} |
| 194 | + |
| 195 | +inline |
| 196 | +bool withinBounds (const nav_msgs::MapMetaData& info, const geometry_msgs::Point& p) |
| 197 | +{ |
| 198 | + return withinBounds(info, pointCell(info, p)); |
| 199 | +} |
| 200 | + |
| 201 | +inline |
| 202 | +bool withinBounds (const nav_msgs::MapMetaData& info, const Cell& c) |
| 203 | +{ |
| 204 | + return (c.x >= 0) && (c.y >= 0) && (c.x < (coord_t) info.width) && (c.y < (coord_t) info.height); |
| 205 | +} |
| 206 | + |
| 207 | +} // namespace occupancy_grid_utils |
| 208 | + |
| 209 | +#endif // include guard |
0 commit comments