coordinate_conversion.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_COORDINATE_CONVERSION_H
36 #define NAV_GRID_COORDINATE_CONVERSION_H
37 
38 #include <nav_grid/nav_grid_info.h>
39 #include <math.h>
40 
41 namespace nav_grid
42 {
53 inline void gridToWorld(const NavGridInfo& info, int mx, int my, double& wx, double& wy)
54 {
55  wx = info.origin_x + (mx + 0.5) * info.resolution;
56  wy = info.origin_y + (my + 0.5) * info.resolution;
57 }
58 
69 inline void worldToGrid(const NavGridInfo& info, double wx, double wy, double& mx, double& my)
70 {
71  mx = (wx - info.origin_x) / info.resolution;
72  my = (wy - info.origin_y) / info.resolution;
73 }
74 
83 inline void worldToGrid(const NavGridInfo& info, double wx, double wy, int& mx, int& my)
84 {
85  double dmx, dmy;
86  worldToGrid(info, wx, wy, dmx, dmy);
87  mx = static_cast<int>(floor(dmx));
88  my = static_cast<int>(floor(dmy));
89 }
90 
104 inline bool worldToGridBounded(const NavGridInfo& info, double wx, double wy, unsigned int& mx, unsigned int& my)
105 {
106  double dmx, dmy;
107  worldToGrid(info, wx, wy, dmx, dmy);
108 
109  bool valid = true;
110 
111  if (dmx < 0.0)
112  {
113  mx = 0;
114  valid = false;
115  }
116  else if (dmx >= info.width)
117  {
118  mx = info.width - 1;
119  valid = false;
120  }
121  else
122  {
123  mx = static_cast<unsigned int>(dmx);
124  }
125 
126  if (dmy < 0.0)
127  {
128  my = 0;
129  valid = false;
130  }
131  else if (dmy >= info.height)
132  {
133  my = info.height - 1;
134  valid = false;
135  }
136  else
137  {
138  my = static_cast<unsigned int>(dmy);
139  }
140 
141  return valid;
142 }
143 
154 inline bool isWithinGrid(const NavGridInfo& info, double wx, double wy)
155 {
156  wx -= info.origin_x;
157  wy -= info.origin_y;
158  return wx >= 0.0 &&
159  wy >= 0.0 &&
160  wx < info.width * info.resolution &&
161  wy < info.height * info.resolution;
162 }
163 
164 
165 
166 } // namespace nav_grid
167 
168 #endif // NAV_GRID_COORDINATE_CONVERSION_H
double origin_x
The origin defines the coordinates of minimum corner of cell (0,0) in the grid.
Definition: nav_grid_info.h:57
void gridToWorld(const NavGridInfo &info, int mx, int my, double &wx, double &wy)
Convert from grid coordinates to world coordinates of the center of the cell.
bool isWithinGrid(const NavGridInfo &info, double wx, double wy)
Check to see if the world coordinates are within the grid.
void worldToGrid(const NavGridInfo &info, double wx, double wy, double &mx, double &my)
Convert from world coordinates to the precise (double) grid coordinates.
bool worldToGridBounded(const NavGridInfo &info, double wx, double wy, unsigned int &mx, unsigned int &my)
Convert from world coordinates to grid coordinates.


nav_grid
Author(s):
autogenerated on Sun Jan 10 2021 04:08:26