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


gmcl
Author(s): Mhd Ali Alshikh Khalil, adler1994@gmail.com
autogenerated on Wed Mar 2 2022 00:20:14