dijkstra.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "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>::infinity());
  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<int> &neighbors = VV[u];
  33. for (std::vector<int>::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. #ifdef IGL_STATIC_LIBRARY
  73. // Explicit template instantiation
  74. 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> >&);
  75. 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> >&);
  76. #endif