Skip to content

Commit 3c5d99b

Browse files
Improve performance of map matching via getPathDistance optimization (#6378)
1 parent ef8f3d7 commit 3c5d99b

File tree

3 files changed

+20
-37
lines changed

3 files changed

+20
-37
lines changed

.github/workflows/osrm-backend.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ jobs:
211211
BUILD_TYPE: Release
212212
CCOMPILER: gcc-11
213213
CXXCOMPILER: g++-11
214+
ENABLE_BENCHMARKS: ON
214215

215216
- name: gcc-10-release
216217
continue-on-error: false
@@ -692,7 +693,6 @@ jobs:
692693
cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE}
693694
make --jobs=${JOBS}
694695
popd
695-
696696
- name: Run all tests
697697
if: ${{ matrix.NODE_PACKAGE_TESTS_ONLY != 'ON' && matrix.ENABLE_APPLE_SILICON != 'ON' }}
698698
run: |
@@ -710,6 +710,16 @@ jobs:
710710
fi
711711
popd
712712
npm test
713+
- name: Run benchmarks
714+
if: ${{ matrix.ENABLE_BENCHMARKS == 'ON' }}
715+
run: |
716+
pushd ${OSRM_BUILD_DIR}
717+
make --jobs=${JOBS} benchmarks
718+
./src/benchmarks/alias-bench
719+
./src/benchmarks/match-bench ../test/data/ch/monaco.osrm
720+
./src/benchmarks/packedvector-bench
721+
./src/benchmarks/rtree-bench ../test/data/monaco.osrm.ramIndex ../test/data/monaco.osrm.fileIndex ../test/data/monaco.osrm.nbg_nodes
722+
popd
713723
- name: Run Node package tests only
714724
if: ${{ matrix.NODE_PACKAGE_TESTS_ONLY == 'ON' && matrix.ENABLE_APPLE_SILICON != 'ON' }}
715725
run: |

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- NodeJS:
1010
- FIXED: Support `skip_waypoints` in Node bindings [#6060](https://github.com/Project-OSRM/osrm-backend/pull/6060)
1111
- Misc:
12+
- CHANGED: Improve performance of map matching via getPathDistance optimization. [#6378](https://github.com/Project-OSRM/osrm-backend/pull/6378)
1213
- CHANGED: Optimize RestrictionParser performance. [#6344](https://github.com/Project-OSRM/osrm-backend/pull/6344)
1314
- ADDED: Support floats for speed value in traffic updates CSV. [#6327](https://github.com/Project-OSRM/osrm-backend/pull/6327)
1415
- CHANGED: Use Lua 5.4 in Docker image. [#6346](https://github.com/Project-OSRM/osrm-backend/pull/6346)

include/engine/routing_algorithms/routing_base.hpp

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -353,49 +353,21 @@ double getPathDistance(const DataFacade<Algorithm> &facade,
353353
const PhantomNode &source_phantom,
354354
const PhantomNode &target_phantom)
355355
{
356-
using util::coordinate_calculation::detail::DEGREE_TO_RAD;
357-
using util::coordinate_calculation::detail::EARTH_RADIUS;
358-
359-
double distance = 0;
360-
double prev_lat =
361-
static_cast<double>(util::toFloating(source_phantom.location.lat)) * DEGREE_TO_RAD;
362-
double prev_lon =
363-
static_cast<double>(util::toFloating(source_phantom.location.lon)) * DEGREE_TO_RAD;
364-
double prev_cos = std::cos(prev_lat);
356+
double distance = 0.0;
357+
auto prev_coordinate = source_phantom.location;
358+
365359
for (const auto &p : unpacked_path)
366360
{
367361
const auto current_coordinate = facade.GetCoordinateOfNode(p.turn_via_node);
368362

369-
const double current_lat =
370-
static_cast<double>(util::toFloating(current_coordinate.lat)) * DEGREE_TO_RAD;
371-
const double current_lon =
372-
static_cast<double>(util::toFloating(current_coordinate.lon)) * DEGREE_TO_RAD;
373-
const double current_cos = std::cos(current_lat);
374-
375-
const double sin_dlon = std::sin((prev_lon - current_lon) / 2.0);
376-
const double sin_dlat = std::sin((prev_lat - current_lat) / 2.0);
377-
378-
const double aharv = sin_dlat * sin_dlat + prev_cos * current_cos * sin_dlon * sin_dlon;
379-
const double charv = 2. * std::atan2(std::sqrt(aharv), std::sqrt(1.0 - aharv));
380-
distance += EARTH_RADIUS * charv;
363+
distance +=
364+
util::coordinate_calculation::greatCircleDistance(prev_coordinate, current_coordinate);
381365

382-
prev_lat = current_lat;
383-
prev_lon = current_lon;
384-
prev_cos = current_cos;
366+
prev_coordinate = current_coordinate;
385367
}
386368

387-
const double current_lat =
388-
static_cast<double>(util::toFloating(target_phantom.location.lat)) * DEGREE_TO_RAD;
389-
const double current_lon =
390-
static_cast<double>(util::toFloating(target_phantom.location.lon)) * DEGREE_TO_RAD;
391-
const double current_cos = std::cos(current_lat);
392-
393-
const double sin_dlon = std::sin((prev_lon - current_lon) / 2.0);
394-
const double sin_dlat = std::sin((prev_lat - current_lat) / 2.0);
395-
396-
const double aharv = sin_dlat * sin_dlat + prev_cos * current_cos * sin_dlon * sin_dlon;
397-
const double charv = 2. * std::atan2(std::sqrt(aharv), std::sqrt(1.0 - aharv));
398-
distance += EARTH_RADIUS * charv;
369+
distance +=
370+
util::coordinate_calculation::greatCircleDistance(prev_coordinate, target_phantom.location);
399371

400372
return distance;
401373
}

0 commit comments

Comments
 (0)