DHART
Loading...
Searching...
No Matches
pathfinder_C.cpp
Go to the documentation of this file.
1#include <pathfinder_C.h>
2
3#include <memory>
4#include <vector>
5
6#include <HFExceptions.h>
7#include <graph.h>
8#include <path_finder.h>
9#include <path.h>
10
11using std::unique_ptr;
12using std::make_unique;
13using std::vector;
14using std::string;
15
20using namespace HF::Pathfinding;
21
22
25 int start,
26 int end,
27 const char * cost_type,
28 int* out_size,
31) {
32 // Get a boost graph from *g
33 std::unique_ptr<BoostGraph, BoostGraphDeleter> bg;
34
35 try {
36 // If cost_name is not a valid cost type in *g,
37 // HF::Exceptions::NoCost is thrown by Graph::GetEdges
38 bg = CreateBoostGraph(*g, std::string(cost_type));
39 }
42 }
43 catch (...) { // Ensure that we catch other potential issues
45 }
46
47
48 // Allocate a new empty path
49 Path* P = new Path();
50
51 // Generate a path using the boost graph we just created.
52 *P = FindPath(bg.get(), start, end);
53
54 // If P isn't empty, set our output pointer to it
55 if (!P->empty()) {
56 *out_path = P;
57 *out_data = P->GetPMPointer();
58 *out_size = P->size();
60 }
61
62 // Otherwise, free the memory for it and signal that no path
63 // could be found
64 else {
65 delete P;
67 }
68}
69
72 const int* start,
73 const int* end,
74 const char* cost_type,
75 HF::SpatialStructures::Path** out_path_ptr_holder,
76 HF::SpatialStructures::PathMember** out_path_member_ptr_holder,
77 int* out_sizes,
78 int num_paths
79) {
80
81 vector<int> starts(start, start + num_paths);
82 vector<int> ends(end, end + num_paths);
83
84 // Get a boost graph from *g
85 std::unique_ptr<BoostGraph, BoostGraphDeleter> bg;
86
87 try {
88 // If cost_name is not a valid cost type in *g,
89 // std::out_of_range is thrown by Graph::GetEdges
90 bg = CreateBoostGraph(*g, std::string(cost_type));
91 }
94 }
95 catch (...) {
97 }
98
99 // Find all the asked for paths
101 bg.get(),
102 starts,
103 ends,
104 out_path_ptr_holder,
105 out_path_member_ptr_holder,
106 out_sizes
107 );
108
110}
111
117 PathMember** out_member_ptr,
118 int* out_size
119) {
120 if (p) {
121 *out_size = p->size();
122 *out_member_ptr = p->GetPMPointer();
124 }
125 else {
126 *out_size = -1;
127 *out_member_ptr = NULL;
129 }
130}
131
132
133C_INTERFACE DestroyPath(Path* path_to_destroy) {
134 DeleteRawPtr(path_to_destroy);
135 return HF::Exceptions::OK;
136}
137
140 const char* cost_type,
141 HF::SpatialStructures::Path** out_path_ptr_holder,
142 HF::SpatialStructures::PathMember** out_path_member_ptr_holder,
143 int* out_sizes,
144 int num_paths
145) {
146
147 // Create a string from cost_type
148 std::string cost(cost_type);
149
150 try {
151 // Create a boost graph with the cost type
152 auto bg = CreateBoostGraph(*g, cost_type);
153
154 // Generate paths
155 InsertAllToAllPathsIntoArray(bg.get(), out_path_ptr_holder, out_path_member_ptr_holder, out_sizes);
156
157 // Return OK
158 return HF_STATUS::OK;
159 }
160
161 // Handle the case that the cost type doesn't exist
163 return HF_STATUS::NO_COST;
164 }
165
166 // IF we get here something went wrong.
167 return HF_STATUS::GENERIC_ERROR;
168
169}
170
172 const Graph* g,
173 const char* cost_name,
174 vector<float>** out_dist_vector,
175 float** out_dist_data,
176 vector<int>** out_pred_vector,
177 int** out_pred_data
178) {
179
180 try {
181 //Try to create a boost graph from G and cost_type
182 auto bg = CreateBoostGraph(*g, string(cost_name));
183
184 // Calculate Distance and predecessor matricies
185 DistanceAndPredecessor matricies = GenerateDistanceAndPred(*bg.get());
186
187 // Update Output
188 *out_dist_vector = matricies.dist;
189 *out_dist_data = matricies.dist->data();
190 *out_pred_vector = matricies.pred;
191 *out_pred_data = matricies.pred->data();
192 }
194 return HF_STATUS::NO_COST;
195 }
196
197 return HF_STATUS::OK;
198}
Contains definitions for the Exceptions namespace.
Contains definition for the BoostGraphDeleter structure.
Contains definitions for the Graph class.
Contains definitions for the Path structure.
#define C_INTERFACE
Definition: analysis_C.h:16
void DeleteRawPtr(T *ptr)
Delete some object pointed to by ptr
Header file for C Interface pathfinding functionality.
C_INTERFACE CalculateDistanceAndPredecessor(const Graph *g, const char *cost_name, vector< float > **out_dist_vector, float **out_dist_data, vector< int > **out_pred_vector, int **out_pred_data)
Calculate the distance and predecessor matricies for a graph.
C_INTERFACE CreatePath(const HF::SpatialStructures::Graph *g, int start, int end, const char *cost_type, int *out_size, HF::SpatialStructures::Path **out_path, HF::SpatialStructures::PathMember **out_data)
Find the shortest path from start to end.
C_INTERFACE DestroyPath(Path *path_to_destroy)
Delete a path.
C_INTERFACE GetPathInfo(HF::SpatialStructures::Path *p, PathMember **out_member_ptr, int *out_size)
Get the size of a path
C_INTERFACE CreateAllToAllPaths(const HF::SpatialStructures::Graph *g, const char *cost_type, HF::SpatialStructures::Path **out_path_ptr_holder, HF::SpatialStructures::PathMember **out_path_member_ptr_holder, int *out_sizes, int num_paths)
Find a path from every node in a graph to every other node.
C_INTERFACE CreatePaths(const HF::SpatialStructures::Graph *g, const int *start, const int *end, const char *cost_type, HF::SpatialStructures::Path **out_path_ptr_holder, HF::SpatialStructures::PathMember **out_path_member_ptr_holder, int *out_sizes, int num_paths)
Find multiple shortest paths in paralllel.
HF_STATUS
A set of error codes standard throughout every DHARTAPI codebase.
Definition: HFExceptions.h:31
@ OK
Operation was successful.
Definition: HFExceptions.h:32
@ GENERIC_ERROR
Not sure what happened here (If this gets thrown, either fix it or give it a status code!...
Definition: HFExceptions.h:38
@ NO_COST
There is no cost with the given name in the given graph.
Definition: HFExceptions.h:50
@ NO_PATH
There is no path between the start and end points.
Definition: HFExceptions.h:49
Algorithms to find the shortest path between nodes in a HF::SpatialStructures::Graph.
Definition: boost_graph.cpp:20
void InsertPathsIntoArray(const BoostGraph *bg, const std::vector< int > &start_points, const std::vector< int > &end_points, HF::SpatialStructures::Path **out_paths, HF::SpatialStructures::PathMember **out_path_members, int *out_sizes)
A special version of FindPaths optimized for the C_Interface.
Path FindPath(BoostGraph *bg, int start_id, int end_id)
Find a path between points A and B using Dijkstra's Shortest Path algorithm.
void InsertAllToAllPathsIntoArray(BoostGraph *bg, Path **out_paths, PathMember **out_path_members, int *out_sizes)
A special version of FindPaths optimized for the C_Interface, such that all paths possible from each ...
DistanceAndPredecessor GenerateDistanceAndPred(const BoostGraph &bg)
Generate the distance and predecessor matricies for a specific boost graph.
std::unique_ptr< BoostGraph, BoostGraphDeleter > CreateBoostGraph(const Graph &g, const std::string &cost_type)
Create a new boost graph from a HF::SpatialStructures:Graph.
Thrown when a dependency is missing such as Embree.
Definition: HFExceptions.h:90
Holds and maintains a distance and predecessor matrix.
Definition: path_finder.h:409
A Graph of nodes connected by edges that supports both integers and HF::SpatialStructures::Node.
Definition: graph.h:486
The ID of a node, and the cost cost to the node after it.
Definition: path.h:19
A collection of nodes that form a path.
Definition: path.h:76
int size() const
Determine how many nodes are in this path.
Definition: path.cpp:20
bool empty() const
Determine if this path has any nodes in it.
Definition: path.cpp:16
PathMember * GetPMPointer()
Get a pointer to the path's underlying path members vector.
Definition: path.cpp:56