Skip to content

Commit d047cdf

Browse files
committed
Add eight connectivity grids
1 parent fe69783 commit d047cdf

File tree

5 files changed

+125
-19
lines changed

5 files changed

+125
-19
lines changed

include/States/Algorithms/GraphBased/GraphBased.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "MessageQueue.h"
1414
#include "State.h"
1515
#include "States/Algorithms/GraphBased/Node.h"
16+
#include "States/Algorithms/GraphBased/Utils.h"
1617

1718
namespace path_finding_visualizer {
1819
namespace graph_based {
@@ -56,6 +57,8 @@ class GraphBased : public State {
5657
// Map Variables
5758
int gridSize_;
5859
int slider_grid_size_;
60+
// 0 = 4 connected grid, 1 = 8 connected grid
61+
int grid_connectivity_;
5962
unsigned int mapWidth_;
6063
unsigned int mapHeight_;
6164

@@ -83,7 +86,7 @@ class GraphBased : public State {
8386
// initialization Functions
8487
void initColors();
8588
void initVariables();
86-
void initNodes(bool reset = true);
89+
void initNodes(bool reset = true, bool reset_neighbours_only = false);
8790

8891
void updateKeyTime(const float& dt);
8992
const bool getKeyTime();

include/States/Algorithms/GraphBased/Node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Node {
4949
void setPath(bool b);
5050
void setPosition(sf::Vector2i pos);
5151
void setNeighbours(std::shared_ptr<Node> node);
52+
void clearNeighbours();
5253
void setParentNode(std::shared_ptr<Node> node);
5354
void setGDistance(double dist);
5455
void setFDistance(double dist);

include/States/Algorithms/GraphBased/Utils.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,95 @@ inline double L1_Distance(const std::shared_ptr<Node> &n1,
1212
fabs(n1->getPos().y - n2->getPos().y);
1313
}
1414

15+
inline void addNeighbours(std::vector<std::shared_ptr<Node>> &nodes,
16+
const unsigned int node_idx, const unsigned int width,
17+
const unsigned int height,
18+
bool use_eight_connectivity = false) {
19+
// TODO: Add error handling here
20+
21+
// setup
22+
const int size = width * height;
23+
const int cell_minus_width = node_idx - width;
24+
const int cell_plus_width = node_idx + width;
25+
const int cell_mod_width = node_idx % width;
26+
const int top_right = width - 1;
27+
28+
unsigned int top_idx, bottom_idx;
29+
30+
// if not at top-edge and bottom-edge, we add both top and bottom neighbours
31+
if (cell_minus_width >= 0 && cell_plus_width < size) {
32+
top_idx = cell_minus_width;
33+
bottom_idx = cell_plus_width;
34+
nodes[node_idx]->setNeighbours(nodes[top_idx]);
35+
nodes[node_idx]->setNeighbours(nodes[bottom_idx]);
36+
}
37+
// otherwise, it can be at either top-edge or bottom-edge
38+
else {
39+
// if at top-edge, only add bottom neighbour
40+
if (cell_minus_width < 0) {
41+
bottom_idx = cell_plus_width;
42+
nodes[node_idx]->setNeighbours(nodes[bottom_idx]);
43+
}
44+
// otherwise, add top neighbour
45+
else {
46+
top_idx = cell_minus_width;
47+
nodes[node_idx]->setNeighbours(nodes[top_idx]);
48+
}
49+
}
50+
51+
// if the cell is not at the right-edge of 2D grid,
52+
// we find all the right, top-right and bottom-right neighbours
53+
if (cell_mod_width != top_right) {
54+
nodes[node_idx]->setNeighbours(nodes[node_idx + 1]); // right neighbour
55+
56+
if (use_eight_connectivity) {
57+
// now find if node is at top edge or bottom edge or otherwise
58+
if (cell_minus_width < 0) { // if at top-edge
59+
nodes[node_idx]->setNeighbours(
60+
nodes[bottom_idx + 1]); // bottom-right neighbour
61+
}
62+
// if at bottom-edge
63+
else if (cell_plus_width >= size) {
64+
nodes[node_idx]->setNeighbours(
65+
nodes[top_idx + 1]); // top-right neighbour
66+
}
67+
// otherwise, we add both
68+
else {
69+
nodes[node_idx]->setNeighbours(
70+
nodes[top_idx + 1]); // top-right neighbour
71+
nodes[node_idx]->setNeighbours(
72+
nodes[bottom_idx + 1]); // bottom-right neighbour
73+
}
74+
}
75+
}
76+
77+
// if the cell is not at the left-edge of 2D grid,
78+
// we find all the left, top-left and bottom-left neighbours
79+
if (cell_mod_width != 0) {
80+
nodes[node_idx]->setNeighbours(nodes[node_idx - 1]); // left neighbour
81+
82+
if (use_eight_connectivity) {
83+
// now find if node is at top edge or bottom edge or otherwise
84+
if (cell_minus_width < 0) { // if at top-edge
85+
nodes[node_idx]->setNeighbours(
86+
nodes[bottom_idx - 1]); // bottom-left neighbour
87+
}
88+
// if at bottom-edge
89+
else if (cell_plus_width >= size) {
90+
nodes[node_idx]->setNeighbours(
91+
nodes[top_idx - 1]); // top-left neighbour
92+
}
93+
// otherwise, we add both
94+
else {
95+
nodes[node_idx]->setNeighbours(
96+
nodes[top_idx - 1]); // top-right neighbour
97+
nodes[node_idx]->setNeighbours(
98+
nodes[bottom_idx - 1]); // bottom-right neighbour
99+
}
100+
}
101+
}
102+
}
103+
15104
} // namespace utils
16105
} // namespace graph_based
17106
} // namespace path_finding_visualizer

src/States/Algorithms/GraphBased/GraphBased.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ void GraphBased::initVariables() {
2424
// for now, just use these and can improve it later
2525
slider_grid_size_ = 20;
2626
gridSize_ = slider_grid_size_;
27+
grid_connectivity_ = 0;
2728
mapWidth_ = 700;
2829
mapHeight_ = 700;
2930

@@ -52,7 +53,7 @@ void GraphBased::initColors() {
5253
PATH_COL = sf::Color(190, 242, 227, 255);
5354
}
5455

55-
void GraphBased::initNodes(bool reset) {
56+
void GraphBased::initNodes(bool reset, bool reset_neighbours_only) {
5657
if (reset) {
5758
nodes_.clear();
5859

@@ -79,26 +80,23 @@ void GraphBased::initNodes(bool reset) {
7980
}
8081
}
8182

82-
// add all neighbours to respective nodes
83-
if (reset) {
83+
// add neighbours based on 4 or 8 connectivity grid
84+
if (reset || reset_neighbours_only) {
8485
for (int x = 0; x < mapHeight_ / gridSize_; x++) {
8586
for (int y = 0; y < mapWidth_ / gridSize_; y++) {
8687
int nodeIndex = (mapWidth_ / gridSize_) * x + y;
87-
if (y > 0)
88-
nodes_[nodeIndex]->setNeighbours(
89-
nodes_[x * (mapWidth_ / gridSize_) + (y - 1)]);
90-
if (y < ((mapWidth_ / gridSize_) - 1))
91-
nodes_[nodeIndex]->setNeighbours(
92-
nodes_[x * (mapWidth_ / gridSize_) + (y + 1)]);
93-
if (x > 0)
94-
nodes_[nodeIndex]->setNeighbours(
95-
nodes_[(x - 1) * (mapWidth_ / gridSize_) + y]);
96-
if (x < ((mapHeight_ / gridSize_) - 1))
97-
nodes_[nodeIndex]->setNeighbours(
98-
nodes_[(x + 1) * (mapWidth_ / gridSize_) + y]);
88+
nodes_[nodeIndex]->clearNeighbours();
89+
utils::addNeighbours(nodes_, nodeIndex,
90+
static_cast<unsigned>(mapWidth_ / gridSize_),
91+
static_cast<unsigned>(mapHeight_ / gridSize_),
92+
[](int connectivity) {
93+
return (connectivity == 1) ? true : false;
94+
}(grid_connectivity_));
9995
}
10096
}
97+
}
10198

99+
if (reset) {
102100
// initialize Start and End nodes ptrs (upper left and lower right corners)
103101
nodeStart_ = nodes_[(mapWidth_ / gridSize_) * 0 + 0];
104102
nodeStart_->setParentNode(nullptr);
@@ -145,7 +143,7 @@ void GraphBased::update(const float& dt) {
145143
// updateButtons();
146144

147145
if (is_reset_) {
148-
initNodes(false);
146+
initNodes(false, false);
149147
is_running_ = false;
150148
is_initialized_ = false;
151149
is_reset_ = false;
@@ -246,10 +244,23 @@ void GraphBased::renderGui() {
246244
// grid size slider
247245
if (ImGui::SliderInt("Grid Size", &slider_grid_size_, 10, 100)) {
248246
gridSize_ = slider_grid_size_;
249-
initNodes(true);
247+
initNodes(true, false);
250248
}
251249

252-
// ImGui::Spacing();
250+
ImGui::Spacing();
251+
252+
// radio buttons for choosing 4 or 8 connected grids
253+
{
254+
bool a, b;
255+
a = ImGui::RadioButton("4-connected", &grid_connectivity_, 0);
256+
ImGui::SameLine();
257+
b = ImGui::RadioButton("8-connected", &grid_connectivity_, 1);
258+
if (a || b) {
259+
initNodes(false, true);
260+
}
261+
}
262+
263+
ImGui::Spacing();
253264
// virtual function renderParametersGui()
254265
// need to be implemented by derived class
255266
renderParametersGui();

src/States/Algorithms/GraphBased/Node.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const std::vector<std::shared_ptr<Node>>* Node::getNeighbours() const {
3434
return &vecNeighbours_;
3535
}
3636

37+
void Node::clearNeighbours() { vecNeighbours_.clear(); }
38+
3739
const double Node::getGDistance() const { return gDist_; }
3840

3941
const double Node::getFDistance() const { return fDist_; }

0 commit comments

Comments
 (0)