00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef _EUCLIDEAN_DISTANCE_H_
00013 #define _EUCLIDEAN_DISTANCE_H_
00014
00020 #include <art/epsilon.h>
00021 #include <art/infinity.h>
00022 #include <art_map/types.h>
00023
00024 namespace Euclidean
00025 {
00026
00030 void inline DistanceFromLine(float cx, float cy,
00031 float ax, float ay ,
00032 float bx, float by,
00033 float &distanceSegment,
00034 float &distanceLine)
00035 {
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 float r_numerator = (cx-ax)*(bx-ax) + (cy-ay)*(by-ay);
00046 float r_denomenator = (bx-ax)*(bx-ax) + (by-ay)*(by-ay);
00047 float r = r_numerator / r_denomenator;
00048
00049 float px = ax + r*(bx-ax);
00050 float py = ay + r*(by-ay);
00051
00052 float s = ((ay-cy)*(bx-ax)-(ax-cx)*(by-ay) ) / r_denomenator;
00053
00054 distanceLine = fabs(s)*sqrtf(r_denomenator);
00055
00056
00057
00058
00059 float xx = px;
00060 float yy = py;
00061
00062 if ( (r >= 0) && (r <= 1) )
00063 {
00064 distanceSegment = distanceLine;
00065 }
00066 else
00067 {
00068
00069 float dist1 = (cx-ax)*(cx-ax) + (cy-ay)*(cy-ay);
00070 float dist2 = (cx-bx)*(cx-bx) + (cy-by)*(cy-by);
00071 if (dist1 < dist2)
00072 {
00073 xx = ax;
00074 yy = ay;
00075 distanceSegment = sqrtf(dist1);
00076 }
00077 else
00078 {
00079 xx = bx;
00080 yy = by;
00081 distanceSegment = sqrtf(dist2);
00082 }
00083 }
00084 }
00085
00086 void inline DistanceFromLine(const MapXY& c,
00087 const MapXY& a,
00088 const MapXY& b,
00089 float &distanceSegment,
00090 float &distanceLine)
00091 {
00092 return DistanceFromLine(c.x,c.y,a.x,a.y,b.x,b.y,distanceSegment,
00093 distanceLine);
00094 }
00095
00097 float inline DistanceTo(MapPose p1, MapPose p2)
00098 {
00099 float x_dist = p1.map.x - p2.map.x;
00100 float y_dist = p1.map.y - p2.map.y;
00101 return sqrtf(x_dist*x_dist + y_dist*y_dist);
00102 }
00103
00105 float inline DistanceTo(float p1x, float p1y, float p2x, float p2y)
00106 {
00107 float x_dist = p1x - p2x;
00108 float y_dist = p1y - p2y;
00109 return sqrtf(x_dist*x_dist + y_dist*y_dist);
00110 }
00111
00113 float inline DistanceTo(MapXY p1, MapXY p2)
00114 {
00115 MapXY dist = p1 - p2;
00116 return sqrtf(dist.x*dist.x + dist.y*dist.y);
00117 }
00118
00122 float inline DistanceToTime(float distance, float speed)
00123 {
00124 float abs_speed = fabs(speed);
00125 if (abs_speed < Epsilon::speed)
00126 return (Infinite::time);
00127 else
00128 return (distance / abs_speed);
00129 }
00130
00132 float inline DistanceToWaypt(const MapPose &pose,
00133 const WayPointNode &waypt)
00134 {
00135 return DistanceTo(pose.map, waypt.map);
00136 }
00137
00139 float inline DistanceToWaypt(MapXY point, const WayPointNode &waypt)
00140 {
00141 return DistanceTo(point, waypt.map);
00142 }
00143
00146 float inline DistanceToWaypt(Polar polar,
00147 const MapPose &origin,
00148 const WayPointNode &waypt)
00149 {
00150 return DistanceTo(Coordinates::Polar_to_MapXY(polar, origin),
00151 waypt.map);
00152 }
00153
00155 bool inline point_in_line_segment(MapXY point, MapXY lp1, MapXY lp2)
00156 {
00157 return (fabs(DistanceTo(lp1, point) + DistanceTo(point, lp2)
00158 - DistanceTo(lp1, lp2))
00159 < Epsilon::distance);
00160 }
00161
00162 }
00163
00164 #endif // _EUCLIDEAN_DISTANCE_H_ //
00165