From df68c3ad48b58fd78a4359cc6d480effd722b1b1 Mon Sep 17 00:00:00 2001 From: DennisOSRM Date: Mon, 11 Jun 2012 16:36:33 +0200 Subject: [PATCH] Bearing is reported as integer only, fixes cucumber tests for issue #292 --- Descriptors/DescriptionFactory.cpp | 34 +++++++++++++++++++++--------- Descriptors/DescriptionFactory.h | 5 ++++- Descriptors/JSONDescriptor.h | 2 +- Util/Azimuth.h | 12 +++++------ features/bearing.feature | 22 +++++++++---------- 5 files changed, 46 insertions(+), 29 deletions(-) diff --git a/Descriptors/DescriptionFactory.cpp b/Descriptors/DescriptionFactory.cpp index b99d12bef..a51aa14f3 100644 --- a/Descriptors/DescriptionFactory.cpp +++ b/Descriptors/DescriptionFactory.cpp @@ -24,15 +24,29 @@ DescriptionFactory::DescriptionFactory() : entireLength(0) { } DescriptionFactory::~DescriptionFactory() { } -double DescriptionFactory::GetAzimuth(const _Coordinate& A, const _Coordinate& B) const { - double lonDiff = (A.lon-B.lon)/100000.; - double angle = atan2(sin(lonDiff)*cos(B.lat/100000.), - cos(A.lat/100000.)*sin(B.lat/100000.)-sin(A.lat/100000.)*cos(B.lat/100000.)*cos(lonDiff)); - angle*=180/M_PI; - while(angle < 0) - angle += 360; +inline double DescriptionFactory::DegreeToRadian(const double degree) const { + return degree * (M_PI/180); +} - return angle; +inline double DescriptionFactory::RadianToDegree(const double radian) const { + return radian * (180/M_PI); +} + +double DescriptionFactory::GetBearing(const _Coordinate& A, const _Coordinate& B) const { + double deltaLong = DegreeToRadian(B.lon/100000. - A.lon/100000.); + + double lat1 = DegreeToRadian(A.lat/100000.); + double lat2 = DegreeToRadian(B.lat/100000.); + + double y = sin(deltaLong) * cos(lat2); + double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(deltaLong); + double result = RadianToDegree(atan2(y, x)); + while(result <= 0.) + result += 360.; + while(result >= 360.) + result -= 360.; + + return result; } void DescriptionFactory::SetStartSegment(const PhantomNode & _startPhantom) { @@ -170,8 +184,8 @@ void DescriptionFactory::Run(const SearchEngineT &sEngine, const unsigned zoomLe //fix what needs to be fixed else for(unsigned i = 0; i < pathDescription.size()-1 && pathDescription.size() >= 2; ++i){ if(pathDescription[i].necessary) { - int angle = 100*GetAzimuth(pathDescription[i].location, pathDescription[i+1].location); - pathDescription[i].bearing = angle/100.; + double angle = GetBearing(pathDescription[i].location, pathDescription[i+1].location); + pathDescription[i].bearing = angle; } } diff --git a/Descriptors/DescriptionFactory.h b/Descriptors/DescriptionFactory.h index f5ce2f2d2..e16ce6b0e 100644 --- a/Descriptors/DescriptionFactory.h +++ b/Descriptors/DescriptionFactory.h @@ -41,6 +41,9 @@ class DescriptionFactory { PhantomNode startPhantom, targetPhantom; typedef SearchEngine > SearchEngineT; + + double DegreeToRadian(const double degree) const; + double RadianToDegree(const double degree) const; public: struct _RouteSummary { std::string lengthString; @@ -66,7 +69,7 @@ public: std::vector pathDescription; DescriptionFactory(); virtual ~DescriptionFactory(); - double GetAzimuth(const _Coordinate& C, const _Coordinate& B) const; + double GetBearing(const _Coordinate& C, const _Coordinate& B) const; void AppendEncodedPolylineString(std::string &output); void AppendUnencodedPolylineString(std::string &output); void AppendSegment(const _Coordinate & coordinate, const _PathData & data); diff --git a/Descriptors/JSONDescriptor.h b/Descriptors/JSONDescriptor.h index ffed7ac53..873c1facf 100644 --- a/Descriptors/JSONDescriptor.h +++ b/Descriptors/JSONDescriptor.h @@ -127,7 +127,7 @@ public: reply.content += "m\",\""; reply.content += Azimuth::Get(segment.bearing); reply.content += "\","; - doubleToStringWithTwoDigitsBehindComma(segment.bearing, tmpBearing); + intToString(round(segment.bearing), tmpBearing); reply.content += tmpBearing; reply.content += "]"; } diff --git a/Util/Azimuth.h b/Util/Azimuth.h index 48246c78e..166f55fdf 100644 --- a/Util/Azimuth.h +++ b/Util/Azimuth.h @@ -31,19 +31,19 @@ struct Azimuth { if(heading >= 0 && heading <= 22.5) return "N"; if(heading > 22.5 && heading <= 67.5) - return "NW"; + return "NE"; if(heading > 67.5 && heading <= 112.5) - return "W"; + return "E"; if(heading > 112.5 && heading <= 157.5) - return "SW"; + return "SE"; return "S"; } if(heading > 202.5 && heading <= 247.5) - return "SE"; + return "SW"; if(heading > 247.5 && heading <= 292.5) - return "E"; + return "W"; if(heading > 292.5 && heading <= 337.5) - return "NE"; + return "NW"; return "N"; } }; diff --git a/features/bearing.feature b/features/bearing.feature index 9774737bb..5e04666b7 100644 --- a/features/bearing.feature +++ b/features/bearing.feature @@ -13,7 +13,7 @@ Feature: Compass bearing When I route I should get | from | to | route | compass | bearing | - | a | b | ab | NW | 45 | + | a | b | ab | NW | 315 | @west Scenario: Bearing when going west @@ -26,7 +26,7 @@ Feature: Compass bearing When I route I should get | from | to | route | compass | bearing | - | a | b | ab | W | 90 | + | a | b | ab | W | 270 | Scenario: Bearing af 45 degree intervals Given the node map @@ -48,13 +48,13 @@ Feature: Compass bearing When I route I should get | from | to | route | compass | bearing | | x | a | xa | N | 0 | - | x | b | xb | NW | 45 | - | x | c | xc | W | 90 | - | x | d | xd | SW | 135 | + | x | b | xb | NW | 315 | + | x | c | xc | W | 270 | + | x | d | xd | SW | 225 | | x | e | xe | S | 180 | - | x | f | xf | SE | 225 | - | x | g | xg | E | 270 | - | x | h | xh | NE | 315 | + | x | f | xf | SE | 135 | + | x | g | xg | E | 90 | + | x | h | xh | NE | 45 | Scenario: Bearing in a roundabout Given the node map @@ -76,8 +76,8 @@ Feature: Compass bearing When I route I should get | from | to | route | compass | bearing | - | c | b | cd,de,ef,fg,gh,ha,ab | W,SW,S,SE,E,NE,N | 90,135,180,225,270,0,45 | - | g | f | gh,ha,ab,bc,cd,de,ef | E,NE,N,NW,W,SW,S | 270,315,0,45,90,180,225 | + | c | b | cd,de,ef,fg,gh,ha,ab | W,SW,S,SE,E,NE,N | 270,225,180,135,90,45,0 | + | g | f | gh,ha,ab,bc,cd,de,ef | E,NE,N,NW,W,SW,S | 90,45,0,315,270,225,180 | Scenario: Bearing should stay constant when zig-zagging Given the node map @@ -96,4 +96,4 @@ Feature: Compass bearing When I route I should get | from | to | route | compass | bearing | - | a | h | ab,bc,cd,de,ef,fg,gh | N,SE,N,SE,N,SE,N | 0,225,0,225,0,225,0 | + | a | h | ab,bc,cd,de,ef,fg,gh | N,SE,N,SE,N,SE,N | 0,135,0,135,0,135,0 |