dijkstra.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. Eigen::PlainObjectBase<DerivedD> &min_distance,
  14. Eigen::PlainObjectBase<DerivedP> &previous)
  15. {
  16. int numV = VV.size();
  17. min_distance.setConstant(numV, 1, std::numeric_limits<typename DerivedD::Scalar>::infinity());
  18. min_distance[source] = 0;
  19. previous.setConstant(numV, 1, -1);
  20. std::set<std::pair<typename DerivedD::Scalar, IndexType> > vertex_queue;
  21. vertex_queue.insert(std::make_pair(min_distance[source], source));
  22. while (!vertex_queue.empty())
  23. {
  24. typename DerivedD::Scalar dist = vertex_queue.begin()->first;
  25. IndexType u = vertex_queue.begin()->second;
  26. vertex_queue.erase(vertex_queue.begin());
  27. if (targets.find(u)!= targets.end())
  28. return u;
  29. // Visit each edge exiting u
  30. const std::vector<int> &neighbors = VV[u];
  31. for (std::vector<int>::const_iterator neighbor_iter = neighbors.begin();
  32. neighbor_iter != neighbors.end();
  33. neighbor_iter++)
  34. {
  35. IndexType v = *neighbor_iter;
  36. typename DerivedD::Scalar distance_through_u = dist + 1.;
  37. if (distance_through_u < min_distance[v]) {
  38. vertex_queue.erase(std::make_pair(min_distance[v], v));
  39. min_distance[v] = distance_through_u;
  40. previous[v] = u;
  41. vertex_queue.insert(std::make_pair(min_distance[v], v));
  42. }
  43. }
  44. }
  45. //we should never get here
  46. return -1;
  47. }
  48. template <typename IndexType, typename DerivedP>
  49. IGL_INLINE void igl::dijkstra_get_shortest_path_to(const IndexType &vertex,
  50. const Eigen::PlainObjectBase<DerivedP> &previous,
  51. std::vector<IndexType> &path)
  52. {
  53. IndexType source = vertex;
  54. path.clear();
  55. for ( ; source != -1; source = previous[source])
  56. path.push_back(source);
  57. }
  58. #ifdef IGL_STATIC_LIBRARY
  59. // Explicit template specialization
  60. 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> >&);
  61. 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> >&);
  62. #endif