Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
NavHeuDistances.hpp
Go to the documentation of this file.
00001 
00012 #ifndef LGL_NAVIGATION_DISTANCES_HEURISTICS_HPP
00013 #define LGL_NAVIGATION_DISTANCES_HEURISTICS_HPP
00014 
00015 #include <math.h>
00016 
00017 namespace jafar { 
00018   namespace lgl { 
00019 
00024     // Heuristics for grid maps
00025     // 
00026     // On a grid, there are well-known heuristic functions to use.
00027     // 
00028     // 1 - Manhattan distance
00029     // 
00030     // h(n) = (abs(n.x-goal.x) + abs(n.y-goal.y))
00031     // 
00032     // 2 - Diagonal distance
00033     // 
00034     // h(n) = max(abs(n.x-goal.x), abs(n.y-goal.y))
00035     // 
00036     // 3 - Euclidean Distance 
00037     // 
00038     // h(n) = sqrt((n.x-goal.x)^2 + (n.y-goal.y)^2)
00039     // 
00040 
00041     // Amit P Stanford Distance : http://theory.stanford.edu/~amitp/GameProgramming/
00042     template <class Point, class DistType> 
00043     DistType mahattanDist(const Point& v1, const Point& v2, const int& d1, const int& d2) {
00044 
00045       DistType a1 = d1*v1.x();
00046       DistType a2 =  d1*v1.y() + v1.x()%d1 - v1.x();
00047       DistType a3 = -d1*v1.y() - v1.x()%d1 - v1.x(); // == -a1-a2
00048 
00049       DistType b1 = d1*v2.x();
00050       DistType b2 =  d1*v2.y() + v2.x()%d1 - v2.x();
00051       DistType b3 = -d1*v2.y() - v2.x()%d1 - v2.x(); // == -b1-b2
00052 
00053       /* One step on the map is 10 in this function */
00054       return d2*max(abs(a1-b1), max(abs(a2-b2), abs(a3-b3)));   
00055     }
00056 
00058     template <class Point, class DistType> 
00059     DistType Mahattan2dDist(const Point& v1, const Point& v2) {
00060       return (fabs(v1.x()-v2.x()) + fabs(v1.y()-v2.y()));   
00061     }
00062 
00064     template <class Point, class DistType> 
00065     DistType Mahattan3dDist(const Point& v1, const Point& v2) {
00066       return fabs(v1.x()-v2.x()) + fabs(v1.y()-v2.y()) + fabs(v1.z()-v2.z());   
00067     }
00068 
00070     template <class Point, class DistType> 
00071     DistType Euclidean2DDist(const Point& v1, const Point& v2) {
00072       DistType dx = v2.x() - v1.x();
00073       DistType dy = v2.y() - v1.y();
00074       return sqrt(dx * dx + dy * dy);
00075     }
00076   }
00077 }
00078 
00079 #endif /* LGL_NAVIGATION_DISTANCES_HEURISTICS_HPP */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:24 for Jafar by doxygen 1.7.6.1
LAAS-CNRS