Skip to content

Commit

Permalink
hold reference to thw world data instead of a copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois Chataigner committed Jun 19, 2019
1 parent 396a979 commit 3154725
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
31 changes: 11 additions & 20 deletions src/AStar2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,18 @@ PathFinder::~PathFinder()

void PathFinder::setWorldData(unsigned width, unsigned height, const uint8_t *data, size_t bytes_per_line, uint8_t color_threshold)
{
_obstacle_threshold = color_threshold;
if( width >= std::numeric_limits<int16_t>::max() ||
height >= std::numeric_limits<int16_t>::max() )
{
throw std::invalid_argument("Either width or height exceed the maximum size allowed (32768) ");
}

if (bytes_per_line==0) bytes_per_line=width;

_world_width = width;
_world_height = height;
_gridmap.resize(width*height);

for (size_t row=0; row<height; row++ )
{
size_t data_idx = row*bytes_per_line, grid_idx = row*width;

for (size_t col=0; col<width; col++, data_idx++, grid_idx++ )
{
_gridmap[grid_idx].world = data[data_idx];
}
}
_world_data = data;
_bytes_per_line = bytes_per_line;
if (_bytes_per_line==0) _bytes_per_line=width;
_obstacle_threshold = color_threshold;
}

void PathFinder::setHeuristic(HeuristicFunction heuristic_)
Expand All @@ -100,11 +90,7 @@ void PathFinder::clean()
_open_set.pop();
}

for(Cell& cell: _gridmap)
{
cell.cost_G = std::numeric_limits< decltype(cell.cost_G)>::max();
cell.already_visited = false;
}
_gridmap.resize(_world_width*_world_height);

This comment has been minimized.

Copy link
@facontidavide

facontidavide Oct 7, 2019

Contributor

the clean up was important!!!!!

}


Expand Down Expand Up @@ -193,6 +179,9 @@ CoordinateList PathFinder::findPath(Coord2D startPos, Coord2D goalPos)

void PathFinder::exportPPM(const char *filename, CoordinateList* path)
{
if (_world_data=nullptr)
return;

std::ofstream outfile(filename, std::ios_base::out | std::ios_base::binary);

char header[100];
Expand All @@ -209,7 +198,9 @@ void PathFinder::exportPPM(const char *filename, CoordinateList* path)
{
for (uint32_t x=0; x<_world_width; x++)
{
if( cell( Coord2D(x,y) ).world == OBSTACLE )
uint8_t world_value = _world_data[y*_bytes_per_line+x];

if( world_value <= _obstacle_threshold )
{
uint8_t color[] = {0,0,0};
std::memcpy( &image[ toIndex(x,y) ], color, 3 );
Expand Down
47 changes: 23 additions & 24 deletions src/AStar2.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,42 +81,39 @@ class PathFinder
/// Export the resulting solution in a visual way. Useful for debugging.
void exportPPM(const char* filename, CoordinateList* path);

enum{
OBSTACLE = 0,
EMPTY = 255
};

uint8_t _obstacle_threshold;

struct Cell{
uint8_t world;
bool already_visited;
Coord2D path_parent;
float cost_G;

Cell(): already_visited(false), cost_G(std::numeric_limits< decltype(cost_G)>::max()) {}
};

const Cell& cell(Coord2D coordinates_) const
const Cell& cell(const Coord2D& coordinates_) const
{
return _gridmap[coordinates_.y*_world_width + coordinates_.x];
}

Cell& cell(Coord2D coordinates_)
Cell& cell(const Coord2D& coordinates_)
{
return _gridmap[coordinates_.y*_world_width + coordinates_.x];
}

private:

HeuristicFunction _heuristic;
uint32_t _world_width;
uint32_t _world_height;

uint8_t _obstacle_threshold=0;
uint32_t _world_width=0;
uint32_t _world_height=0;
const uint8_t* _world_data=nullptr;
size_t _bytes_per_line=0;

std::array<Coord2D,8> _directions;
std::array<uint32_t,8> _direction_cost;

std::priority_queue<ScoreCoordPair, std::vector<ScoreCoordPair>, CompareScore> _open_set;

bool detectCollision(Coord2D coordinates);
bool detectCollision(const Coord2D& coordinates);

std::vector<Cell> _gridmap;

Expand All @@ -126,34 +123,36 @@ class PathFinder
class Heuristic
{
public:
static uint32_t manhattan(Coord2D source_, Coord2D target_);
static uint32_t euclidean(Coord2D source_, Coord2D target_);
static uint32_t octagonal(Coord2D source_, Coord2D target_);
static uint32_t manhattan(const Coord2D& source_, const Coord2D& target_);
static uint32_t euclidean(const Coord2D& source_, const Coord2D& target_);
static uint32_t octagonal(const Coord2D& source_, const Coord2D& target_);
};



inline bool PathFinder::detectCollision(Coord2D coordinates)
inline bool PathFinder::detectCollision(const Coord2D& coordinates)
{
return (coordinates.x < 0 || coordinates.x >= _world_width ||
coordinates.y < 0 || coordinates.y >= _world_height ||
cell(coordinates).world <= _obstacle_threshold );
if (coordinates.x < 0 || coordinates.x >= _world_width ||
coordinates.y < 0 || coordinates.y >= _world_height ) return true;

uint8_t world_value = _world_data[coordinates.y*_bytes_per_line+coordinates.x];
return world_value <= _obstacle_threshold;
}


inline uint32_t Heuristic::manhattan(Coord2D source, Coord2D target)
inline uint32_t Heuristic::manhattan(const Coord2D& source, const Coord2D& target)
{
auto delta = Coord2D( (source.x - target.x), (source.y - target.y) );
return static_cast<uint32_t>(10 * ( abs(delta.x) + abs(delta.y)));
}

inline uint32_t Heuristic::euclidean(Coord2D source, Coord2D target)
inline uint32_t Heuristic::euclidean(const Coord2D& source, const Coord2D& target)
{
auto delta = Coord2D( (source.x - target.x), (source.y - target.y) );
return static_cast<uint32_t>(10 * sqrt(pow(delta.x, 2) + pow(delta.y, 2)));
}

inline uint32_t Heuristic::octagonal(Coord2D source, Coord2D target)
inline uint32_t Heuristic::octagonal(const Coord2D& source, const Coord2D& target)
{
auto delta = Coord2D( abs(source.x - target.x), abs(source.y - target.y) );
return 10 * (delta.x + delta.y) + (-6) * std::min(delta.x, delta.y);
Expand Down

0 comments on commit 3154725

Please sign in to comment.