@@ -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
0 commit comments