Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef NAV_GRID_COORDINATE_CONVERSION_H
00036 #define NAV_GRID_COORDINATE_CONVERSION_H
00037
00038 #include <nav_grid/nav_grid_info.h>
00039 #include <math.h>
00040
00041 namespace nav_grid
00042 {
00053 inline void gridToWorld(const NavGridInfo& info, int mx, int my, double& wx, double& wy)
00054 {
00055 wx = info.origin_x + (mx + 0.5) * info.resolution;
00056 wy = info.origin_y + (my + 0.5) * info.resolution;
00057 }
00058
00069 inline void worldToGrid(const NavGridInfo& info, double wx, double wy, double& mx, double& my)
00070 {
00071 mx = (wx - info.origin_x) / info.resolution;
00072 my = (wy - info.origin_y) / info.resolution;
00073 }
00074
00083 inline void worldToGrid(const NavGridInfo& info, double wx, double wy, int& mx, int& my)
00084 {
00085 double dmx, dmy;
00086 worldToGrid(info, wx, wy, dmx, dmy);
00087 mx = static_cast<int>(floor(dmx));
00088 my = static_cast<int>(floor(dmy));
00089 }
00090
00104 inline bool worldToGridBounded(const NavGridInfo& info, double wx, double wy, unsigned int& mx, unsigned int& my)
00105 {
00106 double dmx, dmy;
00107 worldToGrid(info, wx, wy, dmx, dmy);
00108
00109 bool valid = true;
00110
00111 if (dmx < 0.0)
00112 {
00113 mx = 0;
00114 valid = false;
00115 }
00116 else if (dmx >= info.width)
00117 {
00118 mx = info.width - 1;
00119 valid = false;
00120 }
00121 else
00122 {
00123 mx = static_cast<unsigned int>(dmx);
00124 }
00125
00126 if (dmy < 0.0)
00127 {
00128 my = 0;
00129 valid = false;
00130 }
00131 else if (dmy >= info.height)
00132 {
00133 my = info.height - 1;
00134 valid = false;
00135 }
00136 else
00137 {
00138 my = static_cast<unsigned int>(dmy);
00139 }
00140
00141 return valid;
00142 }
00143
00154 inline bool isWithinGrid(const NavGridInfo& info, double wx, double wy)
00155 {
00156 wx -= info.origin_x;
00157 wy -= info.origin_y;
00158 return wx >= 0.0 &&
00159 wy >= 0.0 &&
00160 wx < info.width * info.resolution &&
00161 wy < info.height * info.resolution;
00162 }
00163
00164
00165
00166 }
00167
00168 #endif // NAV_GRID_COORDINATE_CONVERSION_H