Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
map_range.c
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2000 Brian Gerkey & Kasper Stoy
4  * gerkey@usc.edu kaspers@robotics.usc.edu
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 /**************************************************************************
22  * Desc: Range routines
23  * Author: Andrew Howard
24  * Date: 18 Jan 2003
25  * CVS: $Id: map_range.c 1347 2003-05-05 06:24:33Z inspectorg $
26 **************************************************************************/
27 
28 #include <math.h>
29 #include <string.h>
30 #include <stdlib.h>
31 
32 #include "nav2_amcl/map/map.hpp"
33 
34 // Extract a single range reading from the map. Unknown cells and/or
35 // out-of-bound cells are treated as occupied, which makes it easy to
36 // use Stage bitmap files.
37 double map_calc_range(map_t * map, double ox, double oy, double oa, double max_range)
38 {
39  // Bresenham raytracing
40  int x0, x1, y0, y1;
41  int x, y;
42  int xstep, ystep;
43  char steep;
44  int tmp;
45  int deltax, deltay, error, deltaerr;
46 
47  x0 = MAP_GXWX(map, ox);
48  y0 = MAP_GYWY(map, oy);
49 
50  x1 = MAP_GXWX(map, ox + max_range * cos(oa));
51  y1 = MAP_GYWY(map, oy + max_range * sin(oa));
52 
53  if (abs(y1 - y0) > abs(x1 - x0)) {
54  steep = 1;
55  } else {
56  steep = 0;
57  }
58 
59  if (steep) {
60  tmp = x0;
61  x0 = y0;
62  y0 = tmp;
63 
64  tmp = x1;
65  x1 = y1;
66  y1 = tmp;
67  }
68 
69  deltax = abs(x1 - x0);
70  deltay = abs(y1 - y0);
71  error = 0;
72  deltaerr = deltay;
73 
74  x = x0;
75  y = y0;
76 
77  if (x0 < x1) {
78  xstep = 1;
79  } else {
80  xstep = -1;
81  }
82  if (y0 < y1) {
83  ystep = 1;
84  } else {
85  ystep = -1;
86  }
87 
88  if (steep) {
89  if (!MAP_VALID(map, y, x) || map->cells[MAP_INDEX(map, y, x)].occ_state > -1) {
90  return sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0)) * map->scale;
91  }
92  } else {
93  if (!MAP_VALID(map, x, y) || map->cells[MAP_INDEX(map, x, y)].occ_state > -1) {
94  return sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0)) * map->scale;
95  }
96  }
97 
98  while (x != (x1 + xstep * 1)) {
99  x += xstep;
100  error += deltaerr;
101  if (2 * error >= deltax) {
102  y += ystep;
103  error -= deltax;
104  }
105 
106  if (steep) {
107  if (!MAP_VALID(map, y, x) || map->cells[MAP_INDEX(map, y, x)].occ_state > -1) {
108  return sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0)) * map->scale;
109  }
110  } else {
111  if (!MAP_VALID(map, x, y) || map->cells[MAP_INDEX(map, x, y)].occ_state > -1) {
112  return sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0)) * map->scale;
113  }
114  }
115  }
116  return max_range;
117 }
Definition: map.hpp:62