Skip to content

Commit

Permalink
Improve bounding boxes used by railviz (#603)
Browse files Browse the repository at this point in the history
* Extend bounding boxes to contain trip shpaes

* WIP: Use precalculated bounding boxes per segment

* WIP: Fix bounding boxes

* WIP: Store bounding boxes to disk

* WIP: Remove not needed pointer

* Replace not merged function

* Reduce storage usage for trivial bounding boxes

* Update dependencies

* Cleanup code

* Add comments for internal structure

* Remove fixed 'geo' dependency

* Move implementation into 'nigiri'

* Use optionals to remove duplicated code

* Remove unused code

* Update nigiri dependency

* Fix dangeling pointer

* Update nigiri dependency

* Remove unused includes

* Simplify fallback for loading bounding boxes

* Fix missing const

* Update src/railviz.cc

---------

Co-authored-by: Felix Gündling <[email protected]>
  • Loading branch information
MichaelKutzner and felixguendling authored Nov 12, 2024
1 parent 995f056 commit 24e51f6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .pkg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[nigiri]
[email protected]:motis-project/nigiri.git
branch=master
commit=fe8663ead336cb0bb98b0d6a9a9ff7032985781b
commit=11b8d96f7c62b7f58c45b343ba8436e332d21a72
[cista]
[email protected]:felixguendling/cista.git
branch=master
Expand Down
4 changes: 2 additions & 2 deletions .pkg.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
4369112269552140948
7151612309715904295
cista 950f96f4ded53a6b5753824b280550b722933e55
zlib-ng 68ab3e2d80253ec5dc3c83691d9ff70477b32cd3
boost 930f38eb0365ceb7853273e03da4d9e7787abfb9
Expand All @@ -24,7 +24,7 @@ opentelemetry-cpp 60770dc9dc63e3543fc87d605b2e88fd53d7a414
pugixml 60175e80e2f5e97e027ac78f7e14c5acc009ce50
unordered_dense b33b037377ca966bbdd9cccc3417e46e88f83bfb
wyhash 1e012b57fc2227a9e583a57e2eacb3da99816d99
nigiri fe8663ead336cb0bb98b0d6a9a9ff7032985781b
nigiri 11b8d96f7c62b7f58c45b343ba8436e332d21a72
conf f9bf4bd83bf55a2170725707e526cbacc45dcc66
expat 636c9861e8e7c119f3626d1e6c260603ab624516
libosmium 6e6d6b3081cc8bdf25dda89730e25c36eb995516
Expand Down
2 changes: 1 addition & 1 deletion include/motis/railviz.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace motis {

struct railviz_static_index {
railviz_static_index(nigiri::timetable const&);
railviz_static_index(nigiri::timetable const&, nigiri::shapes_storage const*);
~railviz_static_index();

struct impl;
Expand Down
2 changes: 1 addition & 1 deletion src/data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void data::load_shapes() {
}

void data::load_railviz() {
railviz_static_ = std::make_unique<railviz_static_index>(*tt_);
railviz_static_ = std::make_unique<railviz_static_index>(*tt_, shapes_.get());
rt_->railviz_rt_ = std::make_unique<railviz_rt_index>(*tt_, *rt_->rtt_);
}

Expand Down
47 changes: 35 additions & 12 deletions src/railviz.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

#include "utl/enumerate.h"
#include "utl/get_or_create.h"
#include "utl/pairwise.h"
#include "utl/to_vec.h"
#include "utl/verify.h"

#include "geo/box.h"
#include "geo/detail/register_box.h"
#include "geo/latlng.h"
#include "geo/polyline_format.h"
Expand All @@ -18,6 +21,7 @@
#include "nigiri/rt/frun.h"
#include "nigiri/rt/rt_timetable.h"
#include "nigiri/rt/run.h"
#include "nigiri/shapes_storage.h"
#include "nigiri/timetable.h"
#include "nigiri/types.h"

Expand Down Expand Up @@ -93,19 +97,26 @@ struct route_geo_index {
route_geo_index() = default;

route_geo_index(n::timetable const& tt,
n::shapes_storage const* shapes_data,
n::clasz const clasz,
n::vector_map<n::route_idx_t, float>& distances) {
// Fallback, if no route bounding box can be loaded
auto const get_box = [&](n::route_idx_t const route_idx) {
auto bounding_box = geo::box{};
for (auto const l : tt.route_location_seq_[route_idx]) {
bounding_box.extend(
tt.locations_.coordinates_.at(n::stop{l}.location_idx()));
}
return bounding_box;
};
for (auto const [i, claszes] : utl::enumerate(tt.route_section_clasz_)) {
auto const r = n::route_idx_t{i};
if (claszes.at(0) != clasz) {
continue;
}

auto bounding_box = geo::box{};
for (auto const l : tt.route_location_seq_[r]) {
bounding_box.extend(
tt.locations_.coordinates_.at(n::stop{l}.location_idx()));
}
auto const bounding_box = (shapes_data == nullptr)
? get_box(r)
: shapes_data->get_bounding_box(r);

rtree_.insert(bounding_box.min_.lnglat_float(),
bounding_box.max_.lnglat_float(), r);
Expand Down Expand Up @@ -176,12 +187,13 @@ struct railviz_static_index::impl {
n::vector_map<n::route_idx_t, float> static_distances_{};
};

railviz_static_index::railviz_static_index(n::timetable const& tt)
railviz_static_index::railviz_static_index(n::timetable const& tt,
n::shapes_storage const* shapes_data)
: impl_{std::make_unique<impl>()} {
impl_->static_distances_.resize(tt.route_location_seq_.size());
for (auto c = int_clasz{0U}; c != n::kNumClasses; ++c) {
impl_->static_geo_indices_[c] =
route_geo_index{tt, n::clasz{c}, impl_->static_distances_};
route_geo_index{tt, shapes_data, n::clasz{c}, impl_->static_distances_};
}
}

Expand Down Expand Up @@ -234,6 +246,7 @@ void add_static_transports(n::timetable const& tt,
n::route_idx_t const r,
n::interval<n::unixtime_t> const time_interval,
geo::box const& area,
n::shapes_storage const* shapes_data,
std::vector<stop_pair>& runs) {
auto const is_active = [&](n::transport const t) -> bool {
return (rtt == nullptr
Expand All @@ -247,10 +260,20 @@ void add_static_transports(n::timetable const& tt,
n::interval{n::stop_idx_t{0U}, static_cast<n::stop_idx_t>(seq.size())};
auto const [start_day, _] = tt.day_idx_mam(time_interval.from_);
auto const [end_day, _1] = tt.day_idx_mam(time_interval.to_);
auto const get_box = [&](std::size_t segment) {
if (shapes_data != nullptr) {
auto const box = shapes_data->get_bounding_box(r, segment);
if (box.has_value()) {
return *box;
}
}
// Fallback, if no segment bounding box can be loaded
return geo::make_box(
{tt.locations_.coordinates_[n::stop{seq[segment]}.location_idx()],
tt.locations_.coordinates_[n::stop{seq[segment + 1]}.location_idx()]});
};
for (auto const [from, to] : utl::pairwise(stop_indices)) {
auto const box = geo::make_box(
{tt.locations_.coordinates_[n::stop{seq[from]}.location_idx()],
tt.locations_.coordinates_[n::stop{seq[to]}.location_idx()]});
auto const box = get_box(from);
if (!box.overlaps(area)) {
continue;
}
Expand Down Expand Up @@ -323,7 +346,7 @@ api::trips_response get_trains(tag_lookup const& tags,

for (auto const& r : static_index.static_geo_indices_[c].get_routes(area)) {
if (should_display(cl, zoom_level, static_index.static_distances_[r])) {
add_static_transports(tt, rtt, r, time_interval, area, runs);
add_static_transports(tt, rtt, r, time_interval, area, shapes, runs);
}
}
}
Expand Down

0 comments on commit 24e51f6

Please sign in to comment.