remove redundant code and streamline coordinate interface
This commit is contained in:
parent
0ea7fd9c3d
commit
2e5cc1e6ae
@ -45,25 +45,28 @@ struct FixedPointCoordinate
|
|||||||
FixedPointCoordinate();
|
FixedPointCoordinate();
|
||||||
FixedPointCoordinate(int lat, int lon);
|
FixedPointCoordinate(int lat, int lon);
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
FixedPointCoordinate(const T &coordinate) : lat(coordinate.lat), lon(coordinate.lon)
|
FixedPointCoordinate(const T &coordinate)
|
||||||
|
: lat(coordinate.lat), lon(coordinate.lon)
|
||||||
{
|
{
|
||||||
static_assert(std::is_same<decltype(lat), decltype(coordinate.lat)>::value, "coordinate types incompatible");
|
static_assert(std::is_same<decltype(lat), decltype(coordinate.lat)>::value,
|
||||||
static_assert(std::is_same<decltype(lon), decltype(coordinate.lon)>::value, "coordinate types incompatible");
|
"coordinate types incompatible");
|
||||||
|
static_assert(std::is_same<decltype(lon), decltype(coordinate.lon)>::value,
|
||||||
|
"coordinate types incompatible");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Reset();
|
void reset();
|
||||||
bool isSet() const;
|
bool is_set() const;
|
||||||
bool is_valid() const;
|
bool is_valid() const;
|
||||||
bool operator==(const FixedPointCoordinate &other) const;
|
bool operator==(const FixedPointCoordinate &other) const;
|
||||||
|
|
||||||
float GetBearing(const FixedPointCoordinate &other) const;
|
float bearing(const FixedPointCoordinate &other) const;
|
||||||
void Output(std::ostream &out) const;
|
void output(std::ostream &out) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline std::ostream &operator<<(std::ostream &out_stream, FixedPointCoordinate const &coordinate)
|
inline std::ostream &operator<<(std::ostream &out_stream, FixedPointCoordinate const &coordinate)
|
||||||
{
|
{
|
||||||
coordinate.Output(out_stream);
|
coordinate.output(out_stream);
|
||||||
return out_stream;
|
return out_stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,12 +63,12 @@ FixedPointCoordinate::FixedPointCoordinate(int lat, int lon) : lat(lat), lon(lon
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void FixedPointCoordinate::Reset()
|
void FixedPointCoordinate::reset()
|
||||||
{
|
{
|
||||||
lat = std::numeric_limits<int>::min();
|
lat = std::numeric_limits<int>::min();
|
||||||
lon = std::numeric_limits<int>::min();
|
lon = std::numeric_limits<int>::min();
|
||||||
}
|
}
|
||||||
bool FixedPointCoordinate::isSet() const
|
bool FixedPointCoordinate::is_set() const
|
||||||
{
|
{
|
||||||
return (std::numeric_limits<int>::min() != lat) && (std::numeric_limits<int>::min() != lon);
|
return (std::numeric_limits<int>::min() != lat) && (std::numeric_limits<int>::min() != lon);
|
||||||
}
|
}
|
||||||
@ -86,30 +86,12 @@ bool FixedPointCoordinate::operator==(const FixedPointCoordinate &other) const
|
|||||||
return lat == other.lat && lon == other.lon;
|
return lat == other.lat && lon == other.lon;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FixedPointCoordinate::Output(std::ostream &out) const
|
void FixedPointCoordinate::output(std::ostream &out) const
|
||||||
{
|
{
|
||||||
out << "(" << lat / COORDINATE_PRECISION << "," << lon / COORDINATE_PRECISION << ")";
|
out << "(" << lat / COORDINATE_PRECISION << "," << lon / COORDINATE_PRECISION << ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
float FixedPointCoordinate::GetBearing(const FixedPointCoordinate &other) const
|
float FixedPointCoordinate::bearing(const FixedPointCoordinate &other) const
|
||||||
{
|
{
|
||||||
const float lon_delta = coordinate_calculation::deg_to_rad(lon / COORDINATE_PRECISION -
|
return coordinate_calculation::bearing(*this, other);
|
||||||
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);
|
|
||||||
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);
|
|
||||||
float result = coordinate_calculation::rad_to_deg(std::atan2(y_value, x_value));
|
|
||||||
|
|
||||||
while (result < 0.f)
|
|
||||||
{
|
|
||||||
result += 360.f;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (result >= 360.f)
|
|
||||||
{
|
|
||||||
result -= 360.f;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
@ -239,3 +239,26 @@ float coordinate_calculation::rad_to_deg(const float radian)
|
|||||||
{
|
{
|
||||||
return radian * (180.f * static_cast<float>(M_1_PI));
|
return radian * (180.f * static_cast<float>(M_1_PI));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float coordinate_calculation::bearing(const FixedPointCoordinate &first_coordinate,
|
||||||
|
const FixedPointCoordinate &second_coordinate)
|
||||||
|
{
|
||||||
|
const float lon_diff =
|
||||||
|
second_coordinate.lon / COORDINATE_PRECISION - first_coordinate.lon / COORDINATE_PRECISION;
|
||||||
|
const float lon_delta = deg_to_rad(lon_diff);
|
||||||
|
const float lat1 = deg_to_rad(first_coordinate.lat / COORDINATE_PRECISION);
|
||||||
|
const float lat2 = deg_to_rad(second_coordinate.lat / COORDINATE_PRECISION);
|
||||||
|
const float y = sin(lon_delta) * cos(lat2);
|
||||||
|
const float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(lon_delta);
|
||||||
|
float result = rad_to_deg(std::atan2(y, x));
|
||||||
|
while (result < 0.f)
|
||||||
|
{
|
||||||
|
result += 360.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (result >= 360.f)
|
||||||
|
{
|
||||||
|
result -= 360.f;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
@ -75,6 +75,9 @@ struct coordinate_calculation
|
|||||||
|
|
||||||
static float deg_to_rad(const float degree);
|
static float deg_to_rad(const float degree);
|
||||||
static float rad_to_deg(const float radian);
|
static float rad_to_deg(const float radian);
|
||||||
|
|
||||||
|
static float bearing(const FixedPointCoordinate &first_coordinate,
|
||||||
|
const FixedPointCoordinate &second_coordinate);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // COORDINATE_CALCULATION
|
#endif // COORDINATE_CALCULATION
|
||||||
|
@ -237,7 +237,7 @@ void DescriptionFactory::Run(const unsigned zoom_level)
|
|||||||
via_indices.push_back(necessary_pieces);
|
via_indices.push_back(necessary_pieces);
|
||||||
}
|
}
|
||||||
const double angle =
|
const double angle =
|
||||||
path_description[i + 1].location.GetBearing(path_description[i].location);
|
path_description[i + 1].location.bearing(path_description[i].location);
|
||||||
path_description[i].bearing = static_cast<unsigned>(angle * 10);
|
path_description[i].bearing = static_cast<unsigned>(angle * 10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user