map_store.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: Global map storage functions
31  * Author: Andrew Howard
32  * Date: 6 Feb 2003
33  * CVS: $Id: map_store.c 2951 2005-08-19 00:48:20Z gerkey $
34 **************************************************************************/
35 
36 #include <errno.h>
37 #include <math.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "gmcl/map/map.h"
43 
44 
46 // Load an occupancy grid
47 int map_load_occ(map_t *map, const char *filename, double scale, int negate)
48 {
49  FILE *file;
50  char magic[3];
51  int i, j;
52  int ch, occ;
53  int width, height, depth;
54  map_cell_t *cell;
55 
56  // Open file
57  file = fopen(filename, "r");
58  if (file == NULL)
59  {
60  fprintf(stderr, "%s: %s\n", strerror(errno), filename);
61  return -1;
62  }
63 
64  // Read ppm header
65 
66  if ((fscanf(file, "%2s \n", magic) != 1) || (strcmp(magic, "P5") != 0))
67  {
68  fprintf(stderr, "incorrect image format; must be PGM/binary");
69  fclose(file);
70  return -1;
71  }
72 
73  // Ignore comments
74  while ((ch = fgetc(file)) == '#')
75  while (fgetc(file) != '\n');
76  ungetc(ch, file);
77 
78  // Read image dimensions
79  if(fscanf(file, " %d %d \n %d \n", &width, &height, &depth) != 3)
80  {
81  fprintf(stderr, "Failed ot read image dimensions");
82  return -1;
83  }
84 
85  // Allocate space in the map
86  if (map->cells == NULL)
87  {
88  map->scale = scale;
89  map->size_x = width;
90  map->size_y = height;
91  map->cells = calloc(width * height, sizeof(map->cells[0]));
92  }
93  else
94  {
95  if (width != map->size_x || height != map->size_y)
96  {
97  //PLAYER_ERROR("map dimensions are inconsistent with prior map dimensions");
98  return -1;
99  }
100  }
101 
102  // Read in the image
103  for (j = height - 1; j >= 0; j--)
104  {
105  for (i = 0; i < width; i++)
106  {
107  ch = fgetc(file);
108 
109  // Black-on-white images
110  if (!negate)
111  {
112  if (ch < depth / 4)
113  occ = +1;
114  else if (ch > 3 * depth / 4)
115  occ = -1;
116  else
117  occ = 0;
118  }
119 
120  // White-on-black images
121  else
122  {
123  if (ch < depth / 4)
124  occ = -1;
125  else if (ch > 3 * depth / 4)
126  occ = +1;
127  else
128  occ = 0;
129  }
130 
131  if (!MAP_VALID(map, i, j))
132  continue;
133  cell = map->cells + MAP_INDEX(map, i, j);
134  cell->occ_state = occ;
135  }
136  }
137 
138  fclose(file);
139 
140  return 0;
141 }
142 
143 
145 // Load a wifi signal strength map
146 /*
147 int map_load_wifi(map_t *map, const char *filename, int index)
148 {
149  FILE *file;
150  char magic[3];
151  int i, j;
152  int ch, level;
153  int width, height, depth;
154  map_cell_t *cell;
155 
156  // Open file
157  file = fopen(filename, "r");
158  if (file == NULL)
159  {
160  fprintf(stderr, "%s: %s\n", strerror(errno), filename);
161  return -1;
162  }
163 
164  // Read ppm header
165  fscanf(file, "%10s \n", magic);
166  if (strcmp(magic, "P5") != 0)
167  {
168  fprintf(stderr, "incorrect image format; must be PGM/binary");
169  return -1;
170  }
171 
172  // Ignore comments
173  while ((ch = fgetc(file)) == '#')
174  while (fgetc(file) != '\n');
175  ungetc(ch, file);
176 
177  // Read image dimensions
178  fscanf(file, " %d %d \n %d \n", &width, &height, &depth);
179 
180  // Allocate space in the map
181  if (map->cells == NULL)
182  {
183  map->size_x = width;
184  map->size_y = height;
185  map->cells = calloc(width * height, sizeof(map->cells[0]));
186  }
187  else
188  {
189  if (width != map->size_x || height != map->size_y)
190  {
191  //PLAYER_ERROR("map dimensions are inconsistent with prior map dimensions");
192  return -1;
193  }
194  }
195 
196  // Read in the image
197  for (j = height - 1; j >= 0; j--)
198  {
199  for (i = 0; i < width; i++)
200  {
201  ch = fgetc(file);
202 
203  if (!MAP_VALID(map, i, j))
204  continue;
205 
206  if (ch == 0)
207  level = 0;
208  else
209  level = ch * 100 / 255 - 100;
210 
211  cell = map->cells + MAP_INDEX(map, i, j);
212  cell->wifi_levels[index] = level;
213  }
214  }
215 
216  fclose(file);
217 
218  return 0;
219 }
220 */
221 
222 
223 
map_load_occ
int map_load_occ(map_t *map, const char *filename, double scale, int negate)
Definition: map_store.c:47
map_t::cells
map_cell_t * cells
Definition: map.h:79
map_cell_t::occ_state
int occ_state
Definition: map.h:55
MAP_INDEX
#define MAP_INDEX(map, i, j)
Definition: map.h:207
map.h
map_t::scale
double scale
Definition: map.h:73
map_t::size_x
int size_x
Definition: map.h:76
map_t
Definition: map.h:67
map_t::size_y
int size_y
Definition: map.h:76
map_cell_t
Definition: map.h:52
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