5#include <unordered_map>
8#include "TinyRobotics/coordinates/Coordinate.h"
9#include "TinyRobotics/planning/Path.h"
10#include "TinyRobotics/utils/Common.h"
12namespace tinyrobotics {
15
16
17
18
19
20
21
22
23template <
typename T = DistanceM>
26 using CostCallback = std::function<
float(
const Coordinate<T>&,
28 using ValidCallback = std::function<
bool(
const Coordinate<T>&,
void* ref)>;
42
43
44
45
46
50 std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> open;
51 std::unordered_map<Coordinate<T>,
float> cost_so_far;
52 std::unordered_map<Coordinate<T>, Coordinate<T>> came_from;
54 open.emplace(0.0f, start);
55 cost_so_far[start] = 0.0f;
56 came_from[start] = start;
58 while (!open.empty()) {
59 Coordinate<T> current = open.top().second;
62 if (current == goal)
break;
64 for (
const Coordinate<T>& next : map.getNeighbors(current)) {
65 if (!valid_cb(next, ref))
continue;
66 float new_cost = cost_so_far[current] + cost_cb(current, next, ref);
67 if (!cost_so_far.count(next) || new_cost < cost_so_far[next]) {
68 cost_so_far[next] = new_cost;
69 came_from[next] = current;
70 open.emplace(new_cost, next);
76 Path<Coordinate<T>> path;
77 if (!came_from.count(goal))
return path;
78 for (
Coordinate<T> at = goal; at != start; at = came_from[at]) {
81 path.addWaypoint(start);
87 CostCallback cost_cb = defaultCost;
88 ValidCallback valid_cb = defaultValid;
93 return from.distance(to);
96 static bool defaultValid(
const Coordinate<T>& node,
void* ref) {
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
Generic Dijkstra shortest path algorithm for graphs/maps with callback-based cost and validity.
Definition: Dijkstra.h:24
void setValidCallback(ValidCallback cb)
provide a callback to determine if a node is valid (e.g., not an obstacle)
Definition: Dijkstra.h:36
Path< Coordinate< T > > findPath(const IMapNeighbors< T > &map, const Coordinate< T > &start, const Coordinate< T > &goal)
Finds the shortest path from start to goal.
Definition: Dijkstra.h:47
void setReference(void *reference)
Definition: Dijkstra.h:39
void setCostCallback(CostCallback cb)
Definition: Dijkstra.h:34