00001
00011 #include "map.h"
00012 #include <sys/shm.h>
00013 #include <stdlib.h>
00014 #include <stdio.h>
00015 #include <sys/stat.h>
00016 #include <math.h>
00017
00018 #define GETMAPPOS(i,j) (map->cells[j][i].value)
00019 #define GETFLAG(i,j) (map->cells[j][i].flag)
00020 static Map * map = NULL;
00021 int shmap_id;
00039 int ShmapInit(int init_flag){
00040 const int shmap_size = sizeof(Map);
00041 if (map == NULL){
00042
00043 shmap_id = shmget (SHM_MAP_KEY , shmap_size, IPC_CREAT | S_IRUSR | S_IWUSR);
00044
00045 if (shmap_id == -1) {
00046 perror("shmget");
00047 exit(1);
00048 }
00049
00050
00051 map = (Map *) shmat (shmap_id, 0, 0);
00052 if ((int)map == -1) {
00053 perror("shmat");
00054 exit(1);
00055 }
00056
00057
00058 if(init_flag) ShmapAllFreeSpace();
00059
00060
00061 }
00062 return 1;
00063
00064 }
00065
00069 void ShmapFree(void){
00070
00071 shmdt (map);
00072
00073
00074 shmctl (shmap_id, IPC_RMID, 0);
00075 }
00076
00080 void ShmapDt(void){
00081
00082 shmdt (map);
00083
00084 }
00089 int ShmapIsMapInit(void){
00090 if (map == NULL) return 0;
00091 else return 1;
00092
00093 }
00105 void ShmapAllFreeSpace(void){
00106 int i,j;
00107 for (j=0;j<MAP_HEIGHT;j++){
00108 for(i=0;i<MAP_WIDTH;i++){
00109 ShmapSetCellValue(i,j, MAP_FREE);
00110 ShmapSetCellFlag(i,j, MAP_FLAG_NO_FLAG);
00111 }
00112 }
00113 }
00114
00119 void ShmapClearOldPath(void){
00120 int i,j;
00121 for (j=0;j<MAP_HEIGHT;j++){
00122 for(i=0;i<MAP_WIDTH;i++){
00123 if(ShmapIsFreeCell(i,j)) ShmapSetCellValue(i,j, MAP_FREE);
00124 }
00125 }
00126 }
00127
00128
00136 int ShmapIsCellInMap(int x, int y){
00137 if( ( x < MAP_WIDTH ) && (y < MAP_HEIGHT) && ( x >= 0 ) && ( y >= 0) ) return 1;
00138 else return 0;
00139 }
00146 int ShampIsPointInMap(double x_m, double y_m){
00147 return ShmapIsCellInMap(ShmapPoint2Cell_X(x_m), ShmapPoint2Cell_Y(y_m));
00148 }
00149
00150
00159 int ShmapSetCellValue(int x, int y, MapCellValue value){
00160 if(ShmapIsCellInMap(x,y)){
00161 GETMAPPOS(x,y) = value;
00162 return 1;
00163 } else return 0;
00164
00165 }
00172 int ShmapSetPointValue(double x_m, double y_m, MapCellValue value){
00173 return ShmapSetCellValue(ShmapPoint2Cell_X(x_m), ShmapPoint2Cell_Y(y_m), value);
00174 }
00175
00182 void ShmapUpdateTmpObstacles(MapCellValue val){
00183 int i,j;
00184 MapCellValue act;
00185 if (ShmapIsMapInit()){
00187 for (j=0;j<MAP_HEIGHT;j++){
00188 for(i=0;i<MAP_WIDTH;i++){
00190 act = ShmapGetCellValue(i,j);
00192 if ( (act != (MapCellValue) MAP_NOT_IN_MAP) && (act <= (MapCellValue) MAP_NEW_OBSTACLE) && (act > (MapCellValue) MAP_FREE)){
00194 if (val<act) {
00195 act = act - val;
00196 ShmapSetCellValue(i,j, act);
00197 } else ShmapSetCellValue(i,j, MAP_FREE);
00198 }
00199 }
00200 }
00201 }
00202 }
00203
00210 MapCellValue ShmapGetCellValue(int x, int y){
00211 if (ShmapIsMapInit() && ShmapIsCellInMap(x,y)) return GETMAPPOS(x,y);
00212 else return MAP_NOT_IN_MAP;
00213
00214 }
00215
00222 MapCellValue ShmapGetPointValue(double x_m, double y_m){
00223 return ShmapGetCellValue(ShmapPoint2Cell_X(x_m), ShmapPoint2Cell_Y(y_m));
00224 }
00225
00233 int ShmapIsFreeCell(int x, int y){
00234 if(ShmapIsCellInMap(x,y)) {
00235 switch (ShmapGetCellValue(x,y)){
00236 case MAP_START:
00237 case MAP_GOAL:
00238 case MAP_PATH:
00239 case MAP_FREE:
00240 return 1;
00241 break;
00242 default:
00243 return 0;
00244 break;
00245 }
00246 } else return -1;
00247 }
00255 int ShmapIsFreePoint(double x_m, double y_m){
00256 return ShmapIsFreeCell(ShmapPoint2Cell_X(x_m), ShmapPoint2Cell_Y(y_m));
00257 }
00258
00268 int ShmapSetRectangleType(double x1, double y1, double x2, double y2, MapCellValue cell){
00269 int i,j, init_i, limit_i, init_j, limit_j;
00270 if (x1 < x2){
00271 init_i=ShmapPoint2Cell_X(x1); limit_i = ShmapPoint2Cell_X(x2);
00272 } else {
00273 init_i=ShmapPoint2Cell_X(x2); limit_i = ShmapPoint2Cell_X(x1);
00274 }
00275 if (y1 > y2){
00276 init_j=ShmapPoint2Cell_Y(y1); limit_j = ShmapPoint2Cell_Y(y2);
00277 } else {
00278 init_j=ShmapPoint2Cell_Y(y2); limit_j = ShmapPoint2Cell_Y(y1);
00279 }
00280
00281
00282
00283 for(i=init_i;i<=limit_i;i++){
00284 for(j=init_j;j<=limit_j;j++){
00285
00286 ShmapSetCellValue(i,j,cell);
00287 }
00288 }
00289 return 1;
00290 }
00291
00305 int ShmapPoint2Cell_X(double x){
00306 if ( (x>=0.0) && (x<=(MAP_PLAYGROUND_WIDTH_MM/1000.0))) {
00307 if (x<0.1) return 0;
00308 else return (( int ) ( floor( x*10 ) -1 ));
00309 }
00310 else return -1;
00311 }
00312
00319 int ShmapPoint2Cell_Y(double y){
00320 if ( (y>=0.0) && (y<=(MAP_PLAYGROUND_HEIGHT_MM/1000.0))){
00321 if (y<0.1) return (MAP_HEIGHT-1);
00322 else return (( int ) (MAP_HEIGHT-floor( y*10 ) ));
00323 }
00324 else return -1;
00325 }
00326
00332 double ShmapCell2Point_X(int x){
00333 if ( (x>=0) && (x<=(MAP_WIDTH-1))) return ((x+0.5)/10.0);
00334 else return -1;
00335
00336 }
00337
00343 double ShmapCell2Point_Y(int y){
00344 if ( (y>=0) && (y<=(MAP_HEIGHT-1))) return (((MAP_HEIGHT-y)-0.5)/10.0);
00345 else return -1;
00346 }
00347
00361 int ShmapSetCellFlag(int x, int y, MapCellFlag flag){
00362 if(ShmapIsCellInMap(x,y)){
00363 GETFLAG(x,y) = flag;
00364 return 1;
00365 } else return 0;
00366 }
00367
00375 int ShmapSetPointFlag(double x_m, double y_m, MapCellFlag flag){
00376 return ShmapSetCellFlag(ShmapPoint2Cell_X(x_m), ShmapPoint2Cell_Y(y_m), flag);
00377 }
00378
00385 MapCellFlag ShmapGetCellFlag(int x, int y){
00386 if(ShmapIsCellInMap(x,y)) return GETFLAG(x,y);
00387 else return MAP_FLAG_ERROR;
00388 }
00395 MapCellFlag ShmapGetPointFlag(double x_m, double y_m){
00396 return ShmapGetCellFlag(ShmapPoint2Cell_X(x_m), ShmapPoint2Cell_Y(y_m));
00397 }
00398