coordinate_conversion.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2018, Locus Robotics
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the copyright holder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
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 }  // namespace nav_grid
00167 
00168 #endif  // NAV_GRID_COORDINATE_CONVERSION_H


nav_grid
Author(s):
autogenerated on Wed Jun 26 2019 20:09:30