dijkstra.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <[email protected]>
  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 "dijkstra.h"
  9. template <typename IndexType, typename DerivedD, typename DerivedP>
  10. IGL_INLINE int igl::dijkstra(
  11. const IndexType &source,
  12. const std::set<IndexType> &targets,
  13. const std::vector<std::vector<IndexType> >& VV,
  14. const std::vector<double>& weights,
  15. Eigen::PlainObjectBase<DerivedD> &min_distance,
  16. Eigen::PlainObjectBase<DerivedP> &previous)
  17. {
  18. int numV = VV.size();
  19. min_distance.setConstant(numV, 1, std::numeric_limits<typename DerivedD::Scalar>::max());
  20. min_distance[source] = 0;
  21. previous.setConstant(numV, 1, -1);
  22. std::set<std::pair<typename DerivedD::Scalar, IndexType> > vertex_queue;
  23. vertex_queue.insert(std::make_pair(min_distance[source], source));
  24. while (!vertex_queue.empty())
  25. {
  26. typename DerivedD::Scalar dist = vertex_queue.begin()->first;
  27. IndexType u = vertex_queue.begin()->second;
  28. vertex_queue.erase(vertex_queue.begin());
  29. if (targets.find(u)!= targets.end())
  30. return u;
  31. // Visit each edge exiting u
  32. const std::vector<IndexType> &neighbors = VV[u];
  33. for (typename std::vector<IndexType>::const_iterator neighbor_iter = neighbors.begin();
  34. neighbor_iter != neighbors.end();
  35. neighbor_iter++)
  36. {
  37. IndexType v = *neighbor_iter;
  38. typename DerivedD::Scalar distance_through_u = dist + weights[u];
  39. if (distance_through_u < min_distance[v]) {
  40. vertex_queue.erase(std::make_pair(min_distance[v], v));
  41. min_distance[v] = distance_through_u;
  42. previous[v] = u;
  43. vertex_queue.insert(std::make_pair(min_distance[v], v));
  44. }
  45. }
  46. }
  47. //we should never get here
  48. return -1;
  49. }
  50. template <typename IndexType, typename DerivedD, typename DerivedP>
  51. IGL_INLINE int igl::dijkstra(
  52. const IndexType &source,
  53. const std::set<IndexType> &targets,
  54. const std::vector<std::vector<IndexType> >& VV,
  55. Eigen::PlainObjectBase<DerivedD> &min_distance,
  56. Eigen::PlainObjectBase<DerivedP> &previous)
  57. {
  58. std::vector<double> weights(VV.size(), 1.0);
  59. return dijkstra(source, targets, VV, weights, min_distance, previous);
  60. }
  61. template <typename IndexType, typename DerivedP>
  62. IGL_INLINE void igl::dijkstra(
  63. const IndexType &vertex,
  64. const Eigen::MatrixBase<DerivedP> &previous,
  65. std::vector<IndexType> &path)
  66. {
  67. IndexType source = vertex;
  68. path.clear();
  69. for ( ; source != -1; source = previous[source])
  70. path.push_back(source);
  71. }
  72. template <typename IndexType, typename DerivedV,
  73. typename DerivedD, typename DerivedP>
  74. IGL_INLINE int igl::dijkstra(
  75. const Eigen::MatrixBase<DerivedV> &V,
  76. const std::vector<std::vector<IndexType> >& VV,
  77. const IndexType &source,
  78. const std::set<IndexType> &targets,
  79. Eigen::PlainObjectBase<DerivedD> &min_distance,
  80. Eigen::PlainObjectBase<DerivedP> &previous)
  81. {
  82. int numV = VV.size();
  83. min_distance.setConstant(numV, 1, std::numeric_limits<typename DerivedD::Scalar>::infinity());
  84. min_distance[source] = 0;
  85. previous.setConstant(numV, 1, -1);
  86. std::set<std::pair<typename DerivedD::Scalar, IndexType> > vertex_queue;
  87. vertex_queue.insert(std::make_pair(min_distance[source], source));
  88. while (!vertex_queue.empty())
  89. {
  90. typename DerivedD::Scalar dist = vertex_queue.begin()->first;
  91. IndexType u = vertex_queue.begin()->second;
  92. vertex_queue.erase(vertex_queue.begin());
  93. if (targets.find(u)!= targets.end())
  94. return u;
  95. // Visit each edge exiting u
  96. const std::vector<IndexType> &neighbors = VV[u];
  97. for (typename std::vector<IndexType>::const_iterator neighbor_iter = neighbors.begin();
  98. neighbor_iter != neighbors.end();
  99. neighbor_iter++)
  100. {
  101. IndexType v = *neighbor_iter;
  102. typename DerivedD::Scalar distance_through_u = dist + (V.row(u) - V.row(v)).norm();
  103. if (distance_through_u < min_distance[v]) {
  104. vertex_queue.erase(std::make_pair(min_distance[v], v));
  105. min_distance[v] = distance_through_u;
  106. previous[v] = u;
  107. vertex_queue.insert(std::make_pair(min_distance[v], v));
  108. }
  109. }
  110. }
  111. return -1;
  112. }
  113. #ifdef IGL_STATIC_LIBRARY
  114. // Explicit template instantiation
  115. template int igl::dijkstra<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> >&);
  116. template int igl::dijkstra<int, Eigen::Matrix<int, -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<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  117. template void igl::dijkstra<int, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(int const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, std::vector<int, std::allocator<int> >&);
  118. template int igl::dijkstra<int, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, int const&, std::set<int, std::less<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> >&);
  119. #endif