nav_grid_message_utils.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef NAV_GRID_PUB_SUB_NAV_GRID_MESSAGE_UTILS_H
36 #define NAV_GRID_PUB_SUB_NAV_GRID_MESSAGE_UTILS_H
37 
39 #include <nav_2d_msgs/NavGridOfChars.h>
40 #include <nav_2d_msgs/NavGridOfCharsUpdate.h>
41 #include <nav_2d_msgs/NavGridOfDoubles.h>
42 #include <nav_2d_msgs/NavGridOfDoublesUpdate.h>
45 #include <nav_core2/bounds.h>
46 
47 namespace nav_grid_pub_sub
48 {
58 template<typename ROSMsgType, typename NumericType>
59 ROSMsgType toMsg(const nav_grid::NavGrid<NumericType>& grid, const ros::Time& stamp)
60 {
61  const nav_grid::NavGridInfo& info = grid.getInfo();
62 
63  ROSMsgType msg;
64  msg.stamp = stamp;
65  msg.info = nav_2d_utils::toMsg(info);
66  msg.data.resize(info.width * info.height);
67 
68  unsigned int data_index = 0;
69  for (const nav_grid::Index& index : nav_grid_iterators::WholeGrid(info))
70  {
71  msg.data[data_index++] = grid(index);
72  }
73  return msg;
74 }
75 
81 template<typename ROSMsgType, typename NumericType>
82 ROSMsgType toUpdate(const nav_grid::NavGrid<NumericType>& grid, const nav_core2::UIntBounds& bounds,
83  const ros::Time& stamp)
84 {
85  ROSMsgType update;
86  update.stamp = stamp;
87  update.bounds = nav_2d_utils::toMsg(bounds);
88  update.data.resize(bounds.getWidth() * bounds.getHeight());
89 
90  unsigned int data_index = 0;
91  const nav_grid::NavGridInfo& info = grid.getInfo();
92  for (const nav_grid::Index& index : nav_grid_iterators::SubGrid(&info, bounds))
93  {
94  update.data[data_index++] = grid(index);
95  }
96  return update;
97 }
98 
102 template<typename ROSMsgType, typename NumericType>
103 void fromMsg(const ROSMsgType& msg, nav_grid::NavGrid<NumericType>& grid)
104 {
106  const nav_grid::NavGridInfo current_info = grid.getInfo();
107  if (info != current_info)
108  {
109  grid.setInfo(info);
110  }
111 
112  unsigned int data_index = 0;
113  for (const nav_grid::Index& index : nav_grid_iterators::WholeGrid(info))
114  {
115  grid.setValue(index, msg.data[data_index++]);
116  }
117 }
118 
122 template<typename ROSMsgType, typename NumericType>
124 {
125  const nav_grid::NavGridInfo& info = grid.getInfo();
126  nav_core2::UIntBounds bounds = nav_2d_utils::fromMsg(update.bounds);
127 
128  unsigned int data_index = 0;
129  for (const nav_grid::Index& index : nav_grid_iterators::SubGrid(&info, bounds))
130  {
131  grid.setValue(index, update.data[data_index++]);
132  }
133  return bounds;
134 }
135 
139 inline nav_2d_msgs::NavGridOfChars toMsg(const nav_grid::NavGrid<unsigned char>& grid,
140  const ros::Time& stamp = ros::Time(0))
141 {
142  return toMsg<nav_2d_msgs::NavGridOfChars>(grid, stamp);
143 }
144 
148 inline nav_2d_msgs::NavGridOfDoubles toMsg(const nav_grid::NavGrid<double>& grid, const ros::Time& stamp = ros::Time(0))
149 {
150  return toMsg<nav_2d_msgs::NavGridOfDoubles>(grid, stamp);
151 }
152 
156 inline nav_2d_msgs::NavGridOfDoubles toMsg(const nav_grid::NavGrid<float>& grid, const ros::Time& stamp = ros::Time(0))
157 {
158  return toMsg<nav_2d_msgs::NavGridOfDoubles>(grid, stamp);
159 }
160 
164 inline nav_2d_msgs::NavGridOfCharsUpdate toUpdate(const nav_grid::NavGrid<unsigned char>& grid,
165  const nav_core2::UIntBounds& bounds, const ros::Time& stamp = ros::Time(0))
166 {
167  return toUpdate<nav_2d_msgs::NavGridOfCharsUpdate>(grid, bounds, stamp);
168 }
169 
173 inline nav_2d_msgs::NavGridOfDoublesUpdate toUpdate(const nav_grid::NavGrid<double>& grid,
174  const nav_core2::UIntBounds& bounds, const ros::Time& stamp = ros::Time(0))
175 {
176  return toUpdate<nav_2d_msgs::NavGridOfDoublesUpdate>(grid, bounds, stamp);
177 }
178 
182 inline nav_2d_msgs::NavGridOfDoublesUpdate toUpdate(const nav_grid::NavGrid<float>& grid,
183  const nav_core2::UIntBounds& bounds, const ros::Time& stamp = ros::Time(0))
184 {
185  return toUpdate<nav_2d_msgs::NavGridOfDoublesUpdate>(grid, bounds, stamp);
186 }
187 
188 
189 } // namespace nav_grid_pub_sub
190 
191 #endif // NAV_GRID_PUB_SUB_NAV_GRID_MESSAGE_UTILS_H
ROSMsgType toUpdate(const nav_grid::NavGrid< NumericType > &grid, const nav_core2::UIntBounds &bounds, const ros::Time &stamp)
Generic conversion from a portion of a grid of arbitrary type to an update message of arbitrary type...
virtual void setValue(const unsigned int x, const unsigned int y, const T &value)=0
nav_2d_msgs::NavGridInfo toMsg(const nav_grid::NavGridInfo &info)
nav_grid::NavGridInfo fromMsg(const nav_2d_msgs::NavGridInfo &msg)
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
virtual void setInfo(const NavGridInfo &new_info)=0
NavGridInfo getInfo() const
unsigned int getHeight() const
void fromMsg(const ROSMsgType &msg, nav_grid::NavGrid< NumericType > &grid)
Generic conversion from message of arbitrary type to grid of arbitrary type.
unsigned int getWidth() const
ROSMsgType toMsg(const nav_grid::NavGrid< NumericType > &grid, const ros::Time &stamp)
Utilities for converting NavGrid objects to NavGridOfX messages and updates.
nav_core2::UIntBounds fromUpdate(const ROSMsgType &update, nav_grid::NavGrid< NumericType > &grid)
Generic conversion from an update message to a portion of a grid of arbitrary type.


nav_grid_pub_sub
Author(s):
autogenerated on Sun Jan 10 2021 04:08:50