2012-08-27 11:40:59 -04:00
|
|
|
|
/*
|
2013-10-14 07:42:28 -04:00
|
|
|
|
|
|
|
|
|
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
*/
|
2012-08-27 11:40:59 -04:00
|
|
|
|
|
2013-08-14 07:12:28 -04:00
|
|
|
|
#ifndef FIXED_POINT_COORDINATE_H_
|
|
|
|
|
#define FIXED_POINT_COORDINATE_H_
|
2012-08-27 11:40:59 -04:00
|
|
|
|
|
2013-08-14 12:43:01 -04:00
|
|
|
|
#include "../DataStructures/MercatorUtil.h"
|
2013-06-27 09:45:28 -04:00
|
|
|
|
#include "../Util/StringUtil.h"
|
|
|
|
|
|
2013-06-24 14:12:16 -04:00
|
|
|
|
#include <cassert>
|
|
|
|
|
#include <cmath>
|
2012-08-27 11:40:59 -04:00
|
|
|
|
#include <climits>
|
2013-06-24 14:12:16 -04:00
|
|
|
|
|
2012-08-27 11:40:59 -04:00
|
|
|
|
#include <iostream>
|
|
|
|
|
|
2013-08-05 12:37:42 -04:00
|
|
|
|
static const double COORDINATE_PRECISION = 1000000.;
|
|
|
|
|
|
2013-08-14 07:12:28 -04:00
|
|
|
|
struct FixedPointCoordinate {
|
2012-08-27 11:40:59 -04:00
|
|
|
|
int lat;
|
|
|
|
|
int lon;
|
2013-08-14 07:12:28 -04:00
|
|
|
|
FixedPointCoordinate () : lat(INT_MIN), lon(INT_MIN) {}
|
2013-08-14 12:43:01 -04:00
|
|
|
|
explicit FixedPointCoordinate (int lat, int lon) : lat(lat) , lon(lon) {}
|
2013-06-27 13:21:33 -04:00
|
|
|
|
|
2012-08-27 11:40:59 -04:00
|
|
|
|
void Reset() {
|
|
|
|
|
lat = INT_MIN;
|
|
|
|
|
lon = INT_MIN;
|
|
|
|
|
}
|
|
|
|
|
bool isSet() const {
|
|
|
|
|
return (INT_MIN != lat) && (INT_MIN != lon);
|
|
|
|
|
}
|
|
|
|
|
inline bool isValid() const {
|
2013-08-05 12:37:42 -04:00
|
|
|
|
if(
|
|
|
|
|
lat > 90*COORDINATE_PRECISION ||
|
|
|
|
|
lat < -90*COORDINATE_PRECISION ||
|
|
|
|
|
lon > 180*COORDINATE_PRECISION ||
|
|
|
|
|
lon < -180*COORDINATE_PRECISION
|
|
|
|
|
) {
|
2012-08-27 11:40:59 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-08-14 07:12:28 -04:00
|
|
|
|
bool operator==(const FixedPointCoordinate & other) const {
|
2012-09-27 08:55:48 -04:00
|
|
|
|
return lat == other.lat && lon == other.lon;
|
|
|
|
|
}
|
2012-08-27 11:40:59 -04:00
|
|
|
|
};
|
|
|
|
|
|
2013-08-14 07:12:28 -04:00
|
|
|
|
inline std::ostream & operator<<(std::ostream & out, const FixedPointCoordinate & c){
|
2012-08-27 11:40:59 -04:00
|
|
|
|
out << "(" << c.lat << "," << c.lon << ")";
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline double ApproximateDistance( const int lat1, const int lon1, const int lat2, const int lon2 ) {
|
|
|
|
|
assert(lat1 != INT_MIN);
|
|
|
|
|
assert(lon1 != INT_MIN);
|
|
|
|
|
assert(lat2 != INT_MIN);
|
|
|
|
|
assert(lon2 != INT_MIN);
|
2012-10-04 11:27:17 -04:00
|
|
|
|
double RAD = 0.017453292519943295769236907684886;
|
2013-08-05 12:37:42 -04:00
|
|
|
|
double lt1 = lat1/COORDINATE_PRECISION;
|
|
|
|
|
double ln1 = lon1/COORDINATE_PRECISION;
|
|
|
|
|
double lt2 = lat2/COORDINATE_PRECISION;
|
|
|
|
|
double ln2 = lon2/COORDINATE_PRECISION;
|
2012-10-04 11:27:17 -04:00
|
|
|
|
double dlat1=lt1*(RAD);
|
|
|
|
|
|
|
|
|
|
double dlong1=ln1*(RAD);
|
|
|
|
|
double dlat2=lt2*(RAD);
|
|
|
|
|
double dlong2=ln2*(RAD);
|
|
|
|
|
|
|
|
|
|
double dLong=dlong1-dlong2;
|
|
|
|
|
double dLat=dlat1-dlat2;
|
|
|
|
|
|
|
|
|
|
double aHarv= pow(sin(dLat/2.0),2.0)+cos(dlat1)*cos(dlat2)*pow(sin(dLong/2.),2);
|
|
|
|
|
double cHarv=2.*atan2(sqrt(aHarv),sqrt(1.0-aHarv));
|
|
|
|
|
//earth's radius from wikipedia varies between 6,356.750 km — 6,378.135 km (˜3,949.901 — 3,963.189 miles)
|
|
|
|
|
//The IUGG value for the equatorial radius of the Earth is 6378.137 km (3963.19 mile)
|
|
|
|
|
const double earth=6372797.560856;//I am doing miles, just change this to radius in kilometers to get distances in km
|
|
|
|
|
double distance=earth*cHarv;
|
|
|
|
|
return distance;
|
2012-08-27 11:40:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 07:12:28 -04:00
|
|
|
|
inline double ApproximateDistance(const FixedPointCoordinate &c1, const FixedPointCoordinate &c2) {
|
2012-08-27 11:40:59 -04:00
|
|
|
|
return ApproximateDistance( c1.lat, c1.lon, c2.lat, c2.lon );
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 07:12:28 -04:00
|
|
|
|
inline double ApproximateEuclideanDistance(const FixedPointCoordinate &c1, const FixedPointCoordinate &c2) {
|
2012-12-17 05:22:44 -05:00
|
|
|
|
assert(c1.lat != INT_MIN);
|
|
|
|
|
assert(c1.lon != INT_MIN);
|
|
|
|
|
assert(c2.lat != INT_MIN);
|
|
|
|
|
assert(c2.lon != INT_MIN);
|
|
|
|
|
const double RAD = 0.017453292519943295769236907684886;
|
2013-08-05 12:37:42 -04:00
|
|
|
|
const double lat1 = (c1.lat/COORDINATE_PRECISION)*RAD;
|
|
|
|
|
const double lon1 = (c1.lon/COORDINATE_PRECISION)*RAD;
|
|
|
|
|
const double lat2 = (c2.lat/COORDINATE_PRECISION)*RAD;
|
|
|
|
|
const double lon2 = (c2.lon/COORDINATE_PRECISION)*RAD;
|
2012-12-17 05:22:44 -05:00
|
|
|
|
|
|
|
|
|
const double x = (lon2-lon1) * cos((lat1+lat2)/2.);
|
|
|
|
|
const double y = (lat2-lat1);
|
|
|
|
|
const double earthRadius = 6372797.560856;
|
|
|
|
|
const double d = sqrt(x*x + y*y) * earthRadius;
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 09:45:28 -04:00
|
|
|
|
static inline void convertInternalLatLonToString(const int value, std::string & output) {
|
|
|
|
|
char buffer[100];
|
2013-08-27 08:15:23 -04:00
|
|
|
|
buffer[11] = 0; // zero termination
|
|
|
|
|
char* string = printInt< 11, 6 >( buffer, value );
|
2013-06-27 09:45:28 -04:00
|
|
|
|
output = string;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 07:12:28 -04:00
|
|
|
|
static inline void convertInternalCoordinateToString(const FixedPointCoordinate & coord, std::string & output) {
|
2013-06-27 09:45:28 -04:00
|
|
|
|
std::string tmp;
|
|
|
|
|
convertInternalLatLonToString(coord.lon, tmp);
|
|
|
|
|
output = tmp;
|
|
|
|
|
output += ",";
|
|
|
|
|
convertInternalLatLonToString(coord.lat, tmp);
|
|
|
|
|
output += tmp;
|
|
|
|
|
output += " ";
|
|
|
|
|
}
|
2013-08-14 07:12:28 -04:00
|
|
|
|
static inline void convertInternalReversedCoordinateToString(const FixedPointCoordinate & coord, std::string & output) {
|
2013-06-27 09:45:28 -04:00
|
|
|
|
std::string tmp;
|
|
|
|
|
convertInternalLatLonToString(coord.lat, tmp);
|
|
|
|
|
output = tmp;
|
|
|
|
|
output += ",";
|
|
|
|
|
convertInternalLatLonToString(coord.lon, tmp);
|
|
|
|
|
output += tmp;
|
|
|
|
|
output += " ";
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 12:43:01 -04:00
|
|
|
|
|
|
|
|
|
/* Get angle of line segment (A,C)->(C,B), atan2 magic, formerly cosine theorem*/
|
|
|
|
|
template<class CoordinateT>
|
|
|
|
|
static inline double GetAngleBetweenThreeFixedPointCoordinates (
|
|
|
|
|
const CoordinateT & A,
|
|
|
|
|
const CoordinateT & C,
|
|
|
|
|
const CoordinateT & B
|
|
|
|
|
) {
|
|
|
|
|
const double v1x = (A.lon - C.lon)/COORDINATE_PRECISION;
|
|
|
|
|
const double v1y = lat2y(A.lat/COORDINATE_PRECISION) - lat2y(C.lat/COORDINATE_PRECISION);
|
|
|
|
|
const double v2x = (B.lon - C.lon)/COORDINATE_PRECISION;
|
|
|
|
|
const double v2y = lat2y(B.lat/COORDINATE_PRECISION) - lat2y(C.lat/COORDINATE_PRECISION);
|
|
|
|
|
|
|
|
|
|
double angle = (atan2(v2y,v2x) - atan2(v1y,v1x) )*180/M_PI;
|
|
|
|
|
while(angle < 0)
|
|
|
|
|
angle += 360;
|
|
|
|
|
return angle;
|
|
|
|
|
}
|
2013-08-14 07:12:28 -04:00
|
|
|
|
#endif /* FIXED_POINT_COORDINATE_H_ */
|