Skip to content

Commit c739698

Browse files
committed
fix AStar and BinaryHeap
1 parent 4fc313d commit c739698

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

include/gf2/core/BinaryHeap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace gf {
7676

7777
if (has_available_index()) {
7878
index = available_index();
79-
ElementIndex& ei = element_index(index);
79+
ElementIndex& ei = m_elements[index];
8080
ei.element = value;
8181
ei.heap_index = m_size;
8282
} else {
@@ -97,7 +97,7 @@ namespace gf {
9797

9898
if (has_available_index()) {
9999
index = available_index();
100-
ElementIndex& ei = element_index(index);
100+
ElementIndex& ei = m_elements[index];
101101
ei.element = std::move(value);
102102
ei.heap_index = m_size;
103103
} else {

include/gf2/core/GridMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace gf {
2525
float blocked = 5.0f;
2626
};
2727

28-
using RouteCostFunction = std::function<float(Vec2I position, Vec2I neighbor, Flags<CellProperty> neighbor_properties, const AnyGrid& grid)>;
28+
using RouteCostFunction = std::function<float(Vec2I position, Vec2I neighbor)>;
2929

3030
class GF_CORE_API GridMap {
3131
public:

library/core/GridMap.cc

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ namespace gf {
338338
continue;
339339
}
340340

341-
const float updated_distance = m_data(heap_data.position).distance + cost_function(heap_data.position, position, cell, m_grid);
341+
const float updated_distance = m_data(heap_data.position).distance + cost_function(heap_data.position, position);
342342

343343
if (updated_distance < m_data(position).distance) {
344344
auto& data = m_data(position);
@@ -443,19 +443,7 @@ namespace gf {
443443
m_data(origin).distance = 0.0f;
444444
m_data(origin).state = AStarState::Open;
445445

446-
// for (auto position : m_cells.position_range()) {
447-
// const auto cell = m_cells(position);
448-
//
449-
// if (!cell.test(CellProperty::Walkable)) {
450-
// continue;
451-
// }
452-
//
453-
// AStarHeapData data = {};
454-
// data.position = position;
455-
// data.distance = m_data(position).distance;
456-
//
457-
// m_data(position).handle = m_heap.push(data);
458-
// }
446+
m_heap.push({ origin, 0.0f });
459447
}
460448

461449
void compute_node(AStarHeapData heap_data, RouteCostFunction& cost_function, Flags<CellNeighborQuery> flags)
@@ -477,7 +465,7 @@ namespace gf {
477465
continue;
478466
}
479467

480-
const float updated_distance = m_data(heap_data.position).distance + cost_function(heap_data.position, position, cell, m_grid);
468+
const float updated_distance = m_data(heap_data.position).distance + cost_function(heap_data.position, position);
481469

482470
if (updated_distance < m_data(position).distance) {
483471
auto& data = m_data(position);
@@ -550,14 +538,14 @@ namespace gf {
550538
flags |= CellNeighborQuery::Diagonal;
551539
}
552540

553-
auto cost_function = [cost](Vec2I position, Vec2I neighbor, Flags<CellProperty> neighbor_properties, const AnyGrid& grid)
541+
auto cost_function = [cost,this](Vec2I position, Vec2I neighbor)
554542
{
555-
const bool is_diagonal = grid.are_diagonal_neighbors(position, neighbor);
543+
const bool is_diagonal = m_grid.are_diagonal_neighbors(position, neighbor);
556544
assert(cost.diagonal > 0 || !is_diagonal);
557545

558546
float neighbor_cost = is_diagonal ? cost.diagonal : cost.cardinal;
559547

560-
if (neighbor_properties.test(CellProperty::Blocked)) {
548+
if (m_cells(neighbor).test(CellProperty::Blocked)) {
561549
neighbor_cost += cost.blocked;
562550
}
563551

tests/tests_core_Grid.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <gf2/core/GridMap.h>
2+
3+
#include "gtest/gtest.h"
4+
5+
TEST(GridTest, DefaultConstructor) {
6+
gf::GridMap map;
7+
8+
EXPECT_EQ(map.size(), gf::vec(0, 0));
9+
}
10+
11+
TEST(GridTest, Dijkstra) {
12+
constexpr int Size = 10;
13+
gf::GridMap map = gf::GridMap::make_orthogonal({ Size, Size });
14+
15+
gf::RouteCost cost;
16+
cost.cardinal = 1.0f;
17+
cost.diagonal = 0.0f;
18+
cost.blocked = 1.0f;
19+
20+
auto path = map.compute_route({ 0, 0 }, { Size - 1, Size - 1}, cost, gf::Route::Dijkstra);
21+
22+
EXPECT_EQ(path.size(), 2 * Size - 1);
23+
}
24+
25+
TEST(GridTest, AStar) {
26+
constexpr int Size = 10;
27+
gf::GridMap map = gf::GridMap::make_orthogonal({ Size, Size });
28+
29+
gf::RouteCost cost;
30+
cost.cardinal = 1.0f;
31+
cost.diagonal = 0.0f;
32+
cost.blocked = 1.0f;
33+
34+
auto path = map.compute_route({ 0, 0 }, { Size - 1, Size - 1}, cost, gf::Route::AStar);
35+
36+
EXPECT_EQ(path.size(), 2 * Size - 1);
37+
}

0 commit comments

Comments
 (0)