dijkstra.cpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include <igl/dijkstra.h>
  9. template <typename IndexType, typename DerivedD, typename DerivedP>
  10. IGL_INLINE int igl::dijkstra_compute_paths(const IndexType &source,
  11. const std::set<IndexType> &targets,
  12. const std::vector<std::vector<IndexType> >& VV,
  13. const std::vector<double>& weights,
  14. Eigen::PlainObjectBase<DerivedD> &min_distance,
  15. Eigen::PlainObjectBase<DerivedP> &previous)
  16. {
  17. int numV = VV.size();
  18. min_distance.setConstant(numV, 1, std::numeric_limits<typename DerivedD::Scalar>::infinity());
  19. min_distance[source] = 0;
  20. previous.setConstant(numV, 1, -1);
  21. std::set<std::pair<typename DerivedD::Scalar, IndexType> > vertex_queue;
  22. vertex_queue.insert(std::make_pair(min_distance[source], source));
  23. while (!vertex_queue.empty())
  24. {
  25. typename DerivedD::Scalar dist = vertex_queue.begin()->first;
  26. IndexType u = vertex_queue.begin()->second;
  27. vertex_queue.erase(vertex_queue.begin());
  28. if (targets.find(u)!= targets.end())
  29. return u;
  30. // Visit each edge exiting u
  31. const std::vector<int> &neighbors = VV[u];
  32. for (std::vector<int>::const_iterator neighbor_iter = neighbors.begin();
  33. neighbor_iter != neighbors.end();
  34. neighbor_iter++)
  35. {
  36. IndexType v = *neighbor_iter;
  37. typename DerivedD::Scalar distance_through_u = dist + weights[u];
  38. if (distance_through_u < min_distance[v]) {
  39. vertex_queue.erase(std::make_pair(min_distance[v], v));
  40. min_distance[v] = distance_through_u;
  41. previous[v] = u;
  42. vertex_queue.insert(std::make_pair(min_distance[v], v));
  43. }
  44. }
  45. }
  46. //we should never get here
  47. return -1;
  48. }
  49. template <typename IndexType, typename DerivedD, typename DerivedP>
  50. IGL_INLINE int igl::dijkstra_compute_paths(const IndexType &source,
  51. const std::set<IndexType> &targets,
  52. const std::vector<std::vector<IndexType> >& VV,
  53. Eigen::PlainObjectBase<DerivedD> &min_distance,
  54. Eigen::PlainObjectBase<DerivedP> &previous)
  55. {
  56. std::vector<double> weights(VV.size(), 1.0);
  57. return dijkstra_compute_paths(source, targets, VV, weights, min_distance, previous);
  58. }
  59. template <typename IndexType, typename DerivedP>
  60. IGL_INLINE void igl::dijkstra_get_shortest_path_to(const IndexType &vertex,
  61. const Eigen::PlainObjectBase<DerivedP> &previous,
  62. std::vector<IndexType> &path)
  63. {
  64. IndexType source = vertex;
  65. path.clear();
  66. for ( ; source != -1; source = previous[source])
  67. path.push_back(source);
  68. }
  69. #ifdef IGL_STATIC_LIBRARY
  70. // Explicit template instantiation
  71. template int igl::dijkstra_compute_paths<int, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(int const&, std::set<int, std::less<int>, std::allocator<int> > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  72. template void igl::dijkstra_get_shortest_path_to<int, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(int const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, std::vector<int, std::allocator<int> >&);
  73. #endif