2014-12-17 13:02:48 -05:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2015, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2015-01-20 10:24:49 -05:00
|
|
|
#include "coordinate_calculation.hpp"
|
|
|
|
|
2015-01-13 08:56:46 -05:00
|
|
|
#include "../Util/mercator.hpp"
|
2014-12-17 13:02:48 -05:00
|
|
|
#include "../Util/simple_logger.hpp"
|
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
#include <osrm/coordinate.hpp>
|
|
|
|
|
2015-01-20 10:24:49 -05:00
|
|
|
#include <cmath>
|
|
|
|
|
2014-12-17 13:02:48 -05:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include <bitset>
|
|
|
|
#endif
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
FixedPointCoordinate::FixedPointCoordinate()
|
|
|
|
: lat(std::numeric_limits<int>::min()), lon(std::numeric_limits<int>::min())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FixedPointCoordinate::FixedPointCoordinate(int lat, int lon) : lat(lat), lon(lon)
|
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (0 != (std::abs(lat) >> 30))
|
|
|
|
{
|
|
|
|
std::bitset<32> y_coordinate_vector(lat);
|
|
|
|
SimpleLogger().Write(logDEBUG) << "broken lat: " << lat
|
|
|
|
<< ", bits: " << y_coordinate_vector;
|
|
|
|
}
|
|
|
|
if (0 != (std::abs(lon) >> 30))
|
|
|
|
{
|
|
|
|
std::bitset<32> x_coordinate_vector(lon);
|
|
|
|
SimpleLogger().Write(logDEBUG) << "broken lon: " << lon
|
|
|
|
<< ", bits: " << x_coordinate_vector;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixedPointCoordinate::Reset()
|
|
|
|
{
|
|
|
|
lat = std::numeric_limits<int>::min();
|
|
|
|
lon = std::numeric_limits<int>::min();
|
|
|
|
}
|
|
|
|
bool FixedPointCoordinate::isSet() const
|
|
|
|
{
|
|
|
|
return (std::numeric_limits<int>::min() != lat) && (std::numeric_limits<int>::min() != lon);
|
|
|
|
}
|
|
|
|
bool FixedPointCoordinate::is_valid() const
|
|
|
|
{
|
|
|
|
if (lat > 90 * COORDINATE_PRECISION || lat < -90 * COORDINATE_PRECISION ||
|
|
|
|
lon > 180 * COORDINATE_PRECISION || lon < -180 * COORDINATE_PRECISION)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool FixedPointCoordinate::operator==(const FixedPointCoordinate &other) const
|
|
|
|
{
|
|
|
|
return lat == other.lat && lon == other.lon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixedPointCoordinate::Output(std::ostream &out) const
|
|
|
|
{
|
|
|
|
out << "(" << lat / COORDINATE_PRECISION << "," << lon / COORDINATE_PRECISION << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
float FixedPointCoordinate::GetBearing(const FixedPointCoordinate &other) const
|
|
|
|
{
|
|
|
|
const float lon_delta =
|
2015-01-20 10:24:49 -05:00
|
|
|
coordinate_calculation::deg_to_rad(lon / COORDINATE_PRECISION - other.lon / COORDINATE_PRECISION);
|
|
|
|
const float lat1 = coordinate_calculation::deg_to_rad(other.lat / COORDINATE_PRECISION);
|
|
|
|
const float lat2 = coordinate_calculation::deg_to_rad(lat / COORDINATE_PRECISION);
|
2014-12-17 13:02:48 -05:00
|
|
|
const float y_value = std::sin(lon_delta) * std::cos(lat2);
|
|
|
|
const float x_value =
|
|
|
|
std::cos(lat1) * std::sin(lat2) - std::sin(lat1) * std::cos(lat2) * std::cos(lon_delta);
|
2015-01-20 10:24:49 -05:00
|
|
|
float result = coordinate_calculation::rad_to_deg(std::atan2(y_value, x_value));
|
2014-12-17 13:02:48 -05:00
|
|
|
|
|
|
|
while (result < 0.f)
|
|
|
|
{
|
|
|
|
result += 360.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (result >= 360.f)
|
|
|
|
{
|
|
|
|
result -= 360.f;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|