diff --git a/include/osr/elevation_storage.h b/include/osr/elevation_storage.h index 2a9797b..fa36197 100644 --- a/include/osr/elevation_storage.h +++ b/include/osr/elevation_storage.h @@ -19,6 +19,7 @@ struct provider; struct elevation_storage { struct elevation { + elevation& operator+=(elevation const&); elevation swap() const; elevation_t up_; elevation_t down_; diff --git a/include/osr/routing/route.h b/include/osr/routing/route.h index 67597d8..670b103 100644 --- a/include/osr/routing/route.h +++ b/include/osr/routing/route.h @@ -31,15 +31,13 @@ struct path { way_idx_t way_{way_idx_t::invalid()}; cost_t cost_{kInfeasible}; distance_t dist_{0}; - elevation_t elevation_up_{0}; - elevation_t elevation_down_{0}; + elevation_storage::elevation elevation_{}; mode mode_{mode::kFoot}; }; cost_t cost_{kInfeasible}; double dist_{0.0}; - elevation_t elevation_up_{0}; - elevation_t elevation_down_{0}; + elevation_storage::elevation elevation_{}; std::vector segments_{}; bool uses_elevator_{false}; }; diff --git a/src/elevation_storage.cc b/src/elevation_storage.cc index da49514..632b020 100644 --- a/src/elevation_storage.cc +++ b/src/elevation_storage.cc @@ -52,8 +52,7 @@ elevation_storage::elevation get_way_elevation( point const& from, point const& to, preprocessing::elevation::step_size const& max_step_size) { - auto elevation_up = elevation_t{0}; - auto elevation_down = elevation_t{0}; + auto elevation = elevation_storage::elevation{}; auto a = dem.get(from); auto const b = dem.get(to); if (a != NO_ELEVATION_DATA && b != NO_ELEVATION_DATA) { @@ -73,21 +72,21 @@ elevation_storage::elevation get_way_elevation( from_lng + s * step_size.y_)); if (m != NO_ELEVATION_DATA) { if (a < m) { - elevation_up += m - a; + elevation.up_ += m - a; } else { - elevation_down += a - m; + elevation.down_ += a - m; } a = m; } } } if (a < b) { - elevation_up += b - a; + elevation.up_ += b - a; } else { - elevation_down += a - b; + elevation.down_ += a - b; } } - return {elevation_up, elevation_down}; + return elevation; } void elevation_storage::set_elevations( @@ -139,6 +138,13 @@ elevation_storage::elevation get_elevations(elevation_storage const* elevations, : elevations->get_elevations(way, from, to); } +elevation_storage::elevation& elevation_storage::elevation::operator+=( + elevation const& other) { + up_ += other.up_; + down_ += other.down_; + return *this; +} + elevation_storage::elevation elevation_storage::elevation::swap() const { return { .up_ = down_, diff --git a/src/route.cc b/src/route.cc index c051b16..255c8cc 100644 --- a/src/route.cc +++ b/src/route.cc @@ -30,8 +30,7 @@ struct connecting_way { std::uint16_t from_{}, to_{}; bool is_loop_{}; std::uint16_t distance_{}; - elevation_t elevation_up_{}; - elevation_t elevation_down_{}; + elevation_storage::elevation elevation_; }; template @@ -53,9 +52,9 @@ connecting_way find_connecting_way(ways const& w, auto const is_loop = way != way_idx_t::invalid() && r.is_loop(way) && static_cast(std::abs(a_idx - b_idx)) == r.way_nodes_[way].size() - 2U; - auto const elevation = get_elevations(elevations, way, a_idx, b_idx); - conn = {way, a_idx, b_idx, is_loop, - dist, elevation.up_, elevation.down_}; + conn = {way, a_idx, + b_idx, is_loop, + dist, get_elevations(elevations, way, a_idx, b_idx)}; } }); utl::verify( @@ -106,8 +105,7 @@ double add_path(ways const& w, cost_t const expected_cost, std::vector& path, direction const dir) { - auto const& [way, from_idx, to_idx, is_loop, distance, elevation_up, - elevation_down] = + auto const& [way, from_idx, to_idx, is_loop, distance, elevation] = find_connecting_way(w, blocked, sharing, elevations, from, to, expected_cost, dir); auto j = 0U; @@ -116,8 +114,7 @@ double add_path(ways const& w, segment.way_ = way; segment.dist_ = distance; segment.cost_ = expected_cost; - segment.elevation_up_ = elevation_up; - segment.elevation_down_ = elevation_down; + segment.elevation_ = elevation; segment.mode_ = to.get_mode(); if (way != way_idx_t::invalid()) { @@ -212,13 +209,11 @@ path reconstruct(ways const& w, std::reverse(begin(segments), end(segments)); auto path_elevation = elevation_storage::elevation{}; for (auto const& segment : segments) { - path_elevation.up_ += segment.elevation_up_; - path_elevation.down_ += segment.elevation_down_; + path_elevation += segment.elevation_; } auto p = path{.cost_ = cost, .dist_ = start_node.dist_to_node_ + dist + dest.dist_to_node_, - .elevation_up_ = path_elevation.up_, - .elevation_down_ = path_elevation.down_, + .elevation_ = path_elevation, .segments_ = segments}; d.cost_.at(dest_node.get_key()).write(dest_node, p); return p; diff --git a/test/restriction_test.cc b/test/restriction_test.cc index b6a1156..412efc1 100644 --- a/test/restriction_test.cc +++ b/test/restriction_test.cc @@ -65,12 +65,12 @@ TEST(extract, wa) { ASSERT_TRUE(p2.has_value()); EXPECT_TRUE(std::abs(p2->dist_ - kShortestDistance) < 2.0); // Upper bounds for elevations on each segment - EXPECT_DOUBLE_EQ(4 + 1, p2->elevation_up_); - EXPECT_DOUBLE_EQ(0 + 6, p2->elevation_down_); + EXPECT_DOUBLE_EQ(4 + 1, p2->elevation_.up_); + EXPECT_DOUBLE_EQ(0 + 6, p2->elevation_.down_); ASSERT_TRUE(p3.has_value()); EXPECT_TRUE(p3->dist_ - kShortestDistance > 2.0); // Upper bounds for elevations on each segment - EXPECT_DOUBLE_EQ(1 + 0, p3->elevation_up_); - EXPECT_DOUBLE_EQ(4 + 0, p3->elevation_down_); + EXPECT_DOUBLE_EQ(1 + 0, p3->elevation_.up_); + EXPECT_DOUBLE_EQ(4 + 0, p3->elevation_.down_); }