Skip to content

Commit

Permalink
Merge up and down elevations into one variable
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKutzner committed Dec 18, 2024
1 parent b6fcca2 commit 016e419
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
1 change: 1 addition & 0 deletions include/osr/elevation_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct provider;

struct elevation_storage {
struct elevation {
elevation& operator+=(elevation const&);
elevation swap() const;
elevation_t up_;
elevation_t down_;
Expand Down
6 changes: 2 additions & 4 deletions include/osr/routing/route.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<segment> segments_{};
bool uses_elevator_{false};
};
Expand Down
20 changes: 13 additions & 7 deletions src/elevation_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(
Expand Down Expand Up @@ -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_,
Expand Down
21 changes: 8 additions & 13 deletions src/route.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <direction SearchDir, bool WithBlocked, typename Profile>
Expand All @@ -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<unsigned>(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(
Expand Down Expand Up @@ -106,8 +105,7 @@ double add_path(ways const& w,
cost_t const expected_cost,
std::vector<path::segment>& 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<Profile>(w, blocked, sharing, elevations, from, to,
expected_cost, dir);
auto j = 0U;
Expand All @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions test/restriction_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}

0 comments on commit 016e419

Please sign in to comment.