Bladeren bron

Added a weights option to dijkstras

Former-commit-id: 34be9cb7a86440396be063f2919ca92dd6b85899
zero-impact 6 jaren geleden
bovenliggende
commit
cec9df42aa
2 gewijzigde bestanden met toevoegingen van 37 en 3 verwijderingen
  1. 13 1
      include/igl/dijkstra.cpp
  2. 24 2
      include/igl/dijkstra.h

+ 13 - 1
include/igl/dijkstra.cpp

@@ -11,6 +11,7 @@ template <typename IndexType, typename DerivedD, typename DerivedP>
 IGL_INLINE int igl::dijkstra_compute_paths(const IndexType &source,
                                            const std::set<IndexType> &targets,
                                            const std::vector<std::vector<IndexType> >& VV,
+                                           const std::vector<double>& weights,
                                            Eigen::PlainObjectBase<DerivedD> &min_distance,
                                            Eigen::PlainObjectBase<DerivedP> &previous)
 {
@@ -37,7 +38,7 @@ IGL_INLINE int igl::dijkstra_compute_paths(const IndexType &source,
          neighbor_iter++)
     {
       IndexType v = *neighbor_iter;
-      typename DerivedD::Scalar distance_through_u = dist + 1.;
+      typename DerivedD::Scalar distance_through_u = dist + weights[u];
       if (distance_through_u < min_distance[v]) {
         vertex_queue.erase(std::make_pair(min_distance[v], v));
 
@@ -53,6 +54,17 @@ IGL_INLINE int igl::dijkstra_compute_paths(const IndexType &source,
   return -1;
 }
 
+template <typename IndexType, typename DerivedD, typename DerivedP>
+IGL_INLINE int igl::dijkstra_compute_paths(const IndexType &source,
+                                           const std::set<IndexType> &targets,
+                                           const std::vector<std::vector<IndexType> >& VV,
+                                           Eigen::PlainObjectBase<DerivedD> &min_distance,
+                                           Eigen::PlainObjectBase<DerivedP> &previous)
+{
+  std::vector<double> weights(VV.size(), 1.0);
+  return dijkstra_compute_paths(source, targets, VV, weights, min_distance, previous);
+}
+
 template <typename IndexType, typename DerivedP>
 IGL_INLINE void igl::dijkstra_get_shortest_path_to(const IndexType &vertex,
                                                    const Eigen::PlainObjectBase<DerivedP> &previous,

+ 24 - 2
include/igl/dijkstra.h

@@ -16,7 +16,7 @@
 
 namespace igl {
 
-  // Dijstra's algorithm for shortest paths, with multiple targets.
+  // Dijkstra's algorithm for vertex-weighted shortest paths, with multiple targets.
   // Adapted from http://rosettacode.org/wiki/Dijkstra%27s_algorithm .
   //
   // Inputs:
@@ -24,6 +24,7 @@ namespace igl {
   //   targets          target vector set
   //   VV               #V list of lists of incident vertices (adjacency list), e.g.
   //                    as returned by igl::adjacency_list
+  //   weights          #V list of scalar vertex weights
   //
   // Output:
   //   min_distance     #V by 1 list of the minimum distances from source to all vertices
@@ -33,10 +34,31 @@ namespace igl {
   IGL_INLINE int dijkstra_compute_paths(const IndexType &source,
                                         const std::set<IndexType> &targets,
                                         const std::vector<std::vector<IndexType> >& VV,
+                                        const std::vector<double>& weights,
                                         Eigen::PlainObjectBase<DerivedD> &min_distance,
                                         Eigen::PlainObjectBase<DerivedP> &previous);
 
-  // Backtracking after Dijstra's algorithm, to find shortest path.
+  // Dijkstra's algorithm for shortest paths, with multiple targets.
+  // Adapted from http://rosettacode.org/wiki/Dijkstra%27s_algorithm .
+  //
+  // Inputs:
+  //   source           index of source vertex
+  //   targets          target vector set
+  //   VV               #V list of lists of incident vertices (adjacency list), e.g.
+  //                    as returned by igl::adjacency_list
+  //
+  // Output:
+  //   min_distance     #V by 1 list of the minimum distances from source to all vertices
+  //   previous         #V by 1 list of the previous visited vertices (for each vertex) - used for backtracking
+  //
+  template <typename IndexType, typename DerivedD, typename DerivedP>
+  IGL_INLINE int dijkstra_compute_paths(const IndexType &source,
+                                        const std::set<IndexType> &targets,
+                                        const std::vector<std::vector<IndexType> >& VV,
+                                        Eigen::PlainObjectBase<DerivedD> &min_distance,
+                                        Eigen::PlainObjectBase<DerivedP> &previous);
+
+  // Backtracking after Dijkstra's algorithm, to find shortest path.
   //
   // Inputs:
   //   vertex           vertex to which we want the shortest path (from same source as above)