DHART
Loading...
Searching...
No Matches
path_finder.h
Go to the documentation of this file.
1
8
9#include <memory>
10#include <vector>
11#include <string>
12#include <cassert>
13#include <iostream>
14
15namespace HF {
16 // Forward declares so we don't need to include these in the header.
17 namespace SpatialStructures {
18 class Graph;
19 class Path;
20 class PathMember;
21 }
22
34 namespace Pathfinding {
35 class BoostGraph; // Forward declared to prevent clients from importing boost.
36
54 public:
55
58
108 void operator()(BoostGraph * bg) const;
109 };
110
166 std::unique_ptr<BoostGraph,BoostGraphDeleter> CreateBoostGraph(
168 const std::string & cost_type = ""
169 );
170
176
212 HF::SpatialStructures::Path FindPath(BoostGraph * bg, int start_id, int end_id);
213
274 std::vector<HF::SpatialStructures::Path> FindPaths(
275 BoostGraph * bg,
276 const std::vector<int> & start_points,
277 const std::vector<int> & end_points
278 );
279
283
321 std::vector<HF::SpatialStructures::Path> FindAllPaths(BoostGraph * bg);
322
392 const BoostGraph* bg,
393 const std::vector<int>& start_points,
394 const std::vector<int>& end_points,
395 HF::SpatialStructures::Path** out_paths,
396 HF::SpatialStructures::PathMember** out_path_members,
397 int* out_sizes
398 );
399
410 std::vector<float>* dist;
411 std::vector<int>* pred;
412
413 int size = 0;
414
424
425 // Preallocate enough space in both arrays to hold n^2 values
426 int arr_count = pow(size, 2);
427 dist = new std::vector<float>(arr_count, -1);
428 pred = new std::vector<int>(arr_count, -1);
429
430 this->size = size;
431 }
432
439 inline float * GetRowOfDist(int i) {
440 // Check bounds in debug mode
441 assert(i >= 0 && i < size);
442
443 return dist->data() + (i * size);
444 }
445
446
453 inline int * GetRowOfPred(int i) {
454 // Check bounds in debug mode
455 assert(i >= 0 && i < size);
456
457 return pred->data() + (i * size);
458 }
459 };
460
480 DistanceAndPredecessor GenerateDistanceAndPred(const BoostGraph& bg);
481
616 void InsertAllToAllPathsIntoArray(BoostGraph* bg, HF::SpatialStructures::Path** out_paths, HF::SpatialStructures::PathMember** out_path_members, int* out_sizes);
617 }
618}
619
621inline std::ostream & operator<<(std::ostream& os, HF::Pathfinding::DistanceAndPredecessor & dist_pred) {
622 const int num_nodes = dist_pred.size;
623
624 // create strings to add to
625 std::string dist_string = "[";
626 std::string pred_string = "[";
627
628
629 // Iterate through every row
630 for (int row = 0; row < num_nodes; row++) {
631
632 // Get pointers to beginning and end of both matricies
633 const float* dist_row = dist_pred.GetRowOfDist(row);
634 const int* pred_row = dist_pred.GetRowOfPred(row);
635
636 // Iterate through every column in this row
637 for (int col = 0; col < num_nodes; col++) {
638
639 // Get values from matricies
640 const float dist_value = dist_row[col];
641 const int pred_value = pred_row[col];
642
643 // Convert values to strings
644 dist_string += std::to_string(dist_value);
645 pred_string += std::to_string(pred_value);
646
647 // Don't print a comma if this is the last element
648 if (!(row == num_nodes-1 && col == num_nodes-1)) {
649 dist_string += ", ";
650 pred_string += ", ";
651 }
652 }
653 }
654
655 // cap off both strings
656 dist_string += "]";
657 pred_string += "]";
658
659 // Print dist then predecessor
660 os << dist_string << std::endl << pred_string;
661 return os;
662}
std::ostream & operator<<(std::ostream &os, HF::Pathfinding::DistanceAndPredecessor &dist_pred)
An overload to print HF::Pathfinding::DistanceAndPredecessor when passed to cout .
Definition: path_finder.h:621
Perform human scale analysis on 3D environments.
vector< Path > FindPaths(BoostGraph *bg, const vector< int > &start_points, const vector< int > &end_points)
Find a path from every id in start_ids to the matching end node in end_ids.
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::vector< HF::SpatialStructures::Path > FindAllPaths(BoostGraph *bg)
Find a path from every node to every node (NOTE: Not implemented yet.)
std::unique_ptr< BoostGraph, BoostGraphDeleter > CreateBoostGraph(const Graph &g, const std::string &cost_type)
Create a new boost graph from a HF::SpatialStructures:Graph.
A graph usable with the BoostGraphLibrary.
Definition: boost_graph.h:166
Deleter for the BoostGraph.
Definition: path_finder.h:53
void operator()(BoostGraph *bg) const
Delete a boost graph.
Holds and maintains a distance and predecessor matrix.
Definition: path_finder.h:409
int * GetRowOfPred(int i)
Get a pointer to the beginning of the ith row of the predecessor array.
Definition: path_finder.h:453
float * GetRowOfDist(int i)
Get a pointer to the beginning of the ith row of the distance array.
Definition: path_finder.h:439
DistanceAndPredecessor(int size)
Create and allocate a pair of distance and predecessor arrays.
Definition: path_finder.h:423
int size
Number of rows and colums.
Definition: path_finder.h:413
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