Skip to content

Commit 99061fe

Browse files
committed
Make buttons disabled and fix some bugs
1 parent 1253b82 commit 99061fe

File tree

9 files changed

+143
-65
lines changed

9 files changed

+143
-65
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.10)
1+
cmake_minimum_required(VERSION 3.11) # minimum version needed for FetchContent
22
project(path_finding_visualizer)
33

44
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread")

dependencies/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ add_subdirectory(sfml)
1111
FetchContent_Declare(
1212
imgui
1313
GIT_REPOSITORY https://github.com/ocornut/imgui
14-
GIT_TAG 35b1148efb839381b84de9290d9caf0b66ad7d03
14+
GIT_TAG 55d35d8387c15bf0cfd71861df67af8cfbda7456
1515
)
1616

1717
FetchContent_Declare(

include/States/Algorithms/GraphBased/GraphBased.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ class GraphBased : public State {
6969
std::shared_ptr<MessageQueue<bool>> message_queue_;
7070

7171
// logic flags
72-
bool Algorithm_running_;
73-
bool Algorithm_initialized_;
74-
bool Algorithm_reset_;
75-
bool Algorithm_solved_;
72+
bool is_running_;
73+
bool is_initialized_;
74+
bool is_reset_;
75+
bool is_solved_;
76+
bool disable_run_;
77+
bool disable_gui_parameters_;
7678

7779
// threads
7880
std::thread t_;
@@ -81,7 +83,7 @@ class GraphBased : public State {
8183
// initialization Functions
8284
void initColors();
8385
void initVariables();
84-
void initNodes();
86+
void initNodes(bool reset = true);
8587

8688
void updateKeyTime(const float& dt);
8789
const bool getKeyTime();

include/States/Algorithms/SamplingBased/SamplingBased.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class SamplingBased : public State {
116116
bool is_reset_;
117117
bool is_solved_;
118118
bool is_stopped_;
119+
bool disable_run_;
120+
bool disable_gui_parameters_;
119121

120122
// threads & mutex
121123
std::thread t_;

src/States/Algorithms/GraphBased/BFS/BFS.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void BFS::updateNodes() {
3838
isObstacle = true;
3939
}
4040

41-
if (!Algorithm_solved_) {
41+
if (!is_solved_) {
4242
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) {
4343
if (!isObstacle) {
4444
if (selectedNode != nodeEnd_) nodeStart_ = selectedNode;

src/States/Algorithms/GraphBased/GraphBased.cpp

Lines changed: 88 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ void GraphBased::initVariables() {
2929

3030
message_queue_ = std::make_shared<MessageQueue<bool>>();
3131

32-
Algorithm_running_ = false;
33-
Algorithm_initialized_ = false;
34-
Algorithm_reset_ = false;
35-
Algorithm_solved_ = false;
32+
is_running_ = false;
33+
is_initialized_ = false;
34+
is_reset_ = false;
35+
is_solved_ = false;
3636
thread_joined_ = true;
37+
disable_run_ = false;
38+
disable_gui_parameters_ = false;
3739
}
3840

3941
void GraphBased::initColors() {
@@ -50,19 +52,24 @@ void GraphBased::initColors() {
5052
PATH_COL = sf::Color(190, 242, 227, 255);
5153
}
5254

53-
void GraphBased::initNodes() {
54-
nodes_.clear();
55+
void GraphBased::initNodes(bool reset) {
56+
if (reset) {
57+
nodes_.clear();
5558

56-
for (int i = 0; i < (mapWidth_ / gridSize_) * (mapHeight_ / gridSize_); i++) {
57-
nodes_.emplace_back(std::make_shared<Node>());
59+
for (int i = 0; i < (mapWidth_ / gridSize_) * (mapHeight_ / gridSize_);
60+
i++) {
61+
nodes_.emplace_back(std::make_shared<Node>());
62+
}
5863
}
5964

6065
// set all nodes to free obsts and respective positions
6166
for (int x = 0; x < mapHeight_ / gridSize_; x++) {
6267
for (int y = 0; y < mapWidth_ / gridSize_; y++) {
6368
int nodeIndex = (mapWidth_ / gridSize_) * x + y;
64-
nodes_[nodeIndex]->setPosition(sf::Vector2i(x, y));
65-
nodes_[nodeIndex]->setObstacle(false);
69+
if (reset) {
70+
nodes_[nodeIndex]->setPosition(sf::Vector2i(x, y));
71+
nodes_[nodeIndex]->setObstacle(false);
72+
}
6673
nodes_[nodeIndex]->setVisited(false);
6774
nodes_[nodeIndex]->setFrontier(false);
6875
nodes_[nodeIndex]->setPath(false);
@@ -73,29 +80,31 @@ void GraphBased::initNodes() {
7380
}
7481

7582
// add all neighbours to respective nodes
76-
for (int x = 0; x < mapHeight_ / gridSize_; x++) {
77-
for (int y = 0; y < mapWidth_ / gridSize_; y++) {
78-
int nodeIndex = (mapWidth_ / gridSize_) * x + y;
79-
if (y > 0)
80-
nodes_[nodeIndex]->setNeighbours(
81-
nodes_[x * (mapWidth_ / gridSize_) + (y - 1)]);
82-
if (y < ((mapWidth_ / gridSize_) - 1))
83-
nodes_[nodeIndex]->setNeighbours(
84-
nodes_[x * (mapWidth_ / gridSize_) + (y + 1)]);
85-
if (x > 0)
86-
nodes_[nodeIndex]->setNeighbours(
87-
nodes_[(x - 1) * (mapWidth_ / gridSize_) + y]);
88-
if (x < ((mapHeight_ / gridSize_) - 1))
89-
nodes_[nodeIndex]->setNeighbours(
90-
nodes_[(x + 1) * (mapWidth_ / gridSize_) + y]);
83+
if (reset) {
84+
for (int x = 0; x < mapHeight_ / gridSize_; x++) {
85+
for (int y = 0; y < mapWidth_ / gridSize_; y++) {
86+
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]);
99+
}
91100
}
92-
}
93101

94-
// initialize Start and End nodes ptrs (upper left and lower right corners)
95-
nodeStart_ = nodes_[(mapWidth_ / gridSize_) * 0 + 0];
96-
nodeStart_->setParentNode(nullptr);
97-
nodeEnd_ = nodes_[(mapWidth_ / gridSize_) * (mapHeight_ / gridSize_ - 1) +
98-
(mapWidth_ / gridSize_ - 1)];
102+
// initialize Start and End nodes ptrs (upper left and lower right corners)
103+
nodeStart_ = nodes_[(mapWidth_ / gridSize_) * 0 + 0];
104+
nodeStart_->setParentNode(nullptr);
105+
nodeEnd_ = nodes_[(mapWidth_ / gridSize_) * (mapHeight_ / gridSize_ - 1) +
106+
(mapWidth_ / gridSize_ - 1)];
107+
}
99108
}
100109

101110
void GraphBased::endState() {}
@@ -135,21 +144,21 @@ void GraphBased::update(const float& dt) {
135144
updateKeybinds();
136145
// updateButtons();
137146

138-
if (Algorithm_reset_) {
139-
initNodes();
140-
Algorithm_running_ = false;
141-
Algorithm_initialized_ = false;
142-
Algorithm_reset_ = false;
143-
Algorithm_solved_ = false;
147+
if (is_reset_) {
148+
initNodes(false);
149+
is_running_ = false;
150+
is_initialized_ = false;
151+
is_reset_ = false;
152+
is_solved_ = false;
144153

145154
message_queue_ = std::make_shared<MessageQueue<bool>>();
146155
}
147156

148-
if (Algorithm_running_) {
149-
Algorithm_reset_ = false;
157+
if (is_running_) {
158+
is_reset_ = false;
150159

151160
// initialize Algorithm
152-
if (!Algorithm_initialized_) {
161+
if (!is_initialized_) {
153162
initAlgorithm();
154163

155164
// create thread
@@ -158,7 +167,7 @@ void GraphBased::update(const float& dt) {
158167
nodeEnd_, message_queue_);
159168

160169
thread_joined_ = false;
161-
Algorithm_initialized_ = true;
170+
is_initialized_ = true;
162171
}
163172

164173
// check the algorithm is solved or not
@@ -167,8 +176,8 @@ void GraphBased::update(const float& dt) {
167176
if (msg) {
168177
t_.join();
169178
thread_joined_ = true;
170-
Algorithm_running_ = false;
171-
Algorithm_solved_ = true;
179+
is_running_ = false;
180+
is_solved_ = true;
172181
}
173182
} else {
174183
// only allow mouse and key inputs
@@ -191,28 +200,53 @@ void GraphBased::clearObstacles() {
191200
void GraphBased::renderGui() {
192201
// buttons
193202
{
194-
if (ImGui::Button("RESET", ImVec2(100.f, 40.f)) && !Algorithm_running_) {
195-
Algorithm_reset_ = true;
196-
std::cout << "RESET" << std::endl;
203+
// RESET button
204+
{
205+
if (!disable_run_ || is_running_) ImGui::BeginDisabled();
206+
bool clicked = ImGui::Button("RESET", ImVec2(100.f, 40.f));
207+
if (!disable_run_ || is_running_) ImGui::EndDisabled();
208+
if (clicked && !is_running_) {
209+
is_reset_ = true;
210+
disable_gui_parameters_ = false;
211+
disable_run_ = false;
212+
}
197213
}
214+
198215
ImGui::SameLine();
199-
if (ImGui::Button("PAUSE", ImVec2(100.f, 40.f)))
200-
std::cout << "PAUSE" << std::endl;
216+
217+
// TODO: PAUSE button
218+
// always disabled (not implemented yet)
219+
{
220+
if (true) ImGui::BeginDisabled();
221+
bool clicked = ImGui::Button("PAUSE", ImVec2(100.f, 40.f));
222+
if (true) ImGui::EndDisabled();
223+
}
224+
201225
ImGui::SameLine();
202-
if (ImGui::Button("RUN", ImVec2(100.f, 40.f)) && !Algorithm_solved_) {
203-
std::cout << "RUN" << std::endl;
204-
Algorithm_running_ = true;
226+
227+
// RUN button
228+
{
229+
if (disable_run_) ImGui::BeginDisabled();
230+
bool clicked = ImGui::Button("RUN", ImVec2(100.f, 40.f));
231+
if (disable_run_) ImGui::EndDisabled();
232+
if (clicked && !is_solved_) {
233+
is_running_ = true;
234+
disable_gui_parameters_ = true;
235+
disable_run_ = true;
236+
}
205237
}
206238
}
207239

208240
ImGui::Spacing();
209241
ImGui::Separator();
210242
ImGui::Spacing();
211243

244+
if (disable_gui_parameters_) ImGui::BeginDisabled();
245+
212246
// grid size slider
213247
if (ImGui::SliderInt("Grid Size", &slider_grid_size_, 10, 100)) {
214-
Algorithm_reset_ = true;
215248
gridSize_ = slider_grid_size_;
249+
initNodes(true);
216250
}
217251

218252
// ImGui::Spacing();
@@ -230,6 +264,8 @@ void GraphBased::renderGui() {
230264
if (ImGui::Button("RESET PARAMETERS", ImVec2(154.f, 40.f))) {
231265
}
232266
}
267+
268+
if (disable_gui_parameters_) ImGui::EndDisabled();
233269
}
234270

235271
void GraphBased::render() {

src/States/Algorithms/SamplingBased/RRT/RRT.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ RRT::~RRT() {}
1515

1616
void RRT::initParameters() {
1717
// initialize default planner related params
18+
// TODO: default values should be read from file
1819
max_iterations_ = 1000;
1920
interpolation_dist_ = 0.005;
2021
range_ = 0.05;
@@ -96,6 +97,8 @@ void RRT::renderPlannerData() {
9697
}
9798

9899
void RRT::renderParametersGui() {
100+
// TODO: instead of manually putting like this, its good to have custom
101+
// declare functions for these parameters with different types
99102
if (ImGui::InputDouble("range", &range_, 0.01, 1.0, "%.3f")) {
100103
if (range_ < 0) range_ = 0.01;
101104
}
@@ -105,6 +108,8 @@ void RRT::renderParametersGui() {
105108
}
106109
}
107110

111+
// TODO: Since RRT*-like planners share the same iteration concept,
112+
// solveConcurrently() function can be only defined one time
108113
void RRT::solveConcurrently(std::shared_ptr<Vertex> start_point,
109114
std::shared_ptr<Vertex> goal_point,
110115
std::shared_ptr<MessageQueue<bool>> message_queue) {

src/States/Algorithms/SamplingBased/RRT_STAR/RRT_STAR.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ RRT_STAR::~RRT_STAR() {}
1616

1717
void RRT_STAR::initParameters() {
1818
// initialize default planner related params
19+
// TODO: default values should be read from file
1920
max_iterations_ = 2500;
2021
interpolation_dist_ = 0.005;
2122
range_ = 0.05;

src/States/Algorithms/SamplingBased/SamplingBased.cpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void SamplingBased::initVariables() {
4141
is_solved_ = false;
4242
is_stopped_ = false;
4343
thread_joined_ = true;
44+
disable_gui_parameters_ = false;
45+
disable_run_ = false;
4446
}
4547

4648
void SamplingBased::endState() {}
@@ -203,20 +205,48 @@ void SamplingBased::clearObstacles() { obstacles_.clear(); }
203205
void SamplingBased::renderGui() {
204206
// buttons
205207
{
206-
if (ImGui::Button("RESET", ImVec2(100.f, 40.f)) && !is_running_) {
207-
is_reset_ = true;
208+
// RESET button
209+
{
210+
if (!disable_run_ || is_running_) ImGui::BeginDisabled();
211+
bool clicked = ImGui::Button("RESET", ImVec2(100.f, 40.f));
212+
if (!disable_run_ || is_running_) ImGui::EndDisabled();
213+
if (clicked && !is_running_) {
214+
is_reset_ = true;
215+
disable_gui_parameters_ = false;
216+
disable_run_ = false;
217+
}
208218
}
219+
209220
ImGui::SameLine();
210-
if (ImGui::Button("PAUSE", ImVec2(100.f, 40.f))) {
221+
222+
// TODO: PAUSE button
223+
// always disabled (not implemented yet)
224+
{
225+
if (true) ImGui::BeginDisabled();
226+
bool clicked = ImGui::Button("PAUSE", ImVec2(100.f, 40.f));
227+
if (true) ImGui::EndDisabled();
211228
}
229+
212230
ImGui::SameLine();
213-
if (ImGui::Button("RUN", ImVec2(100.f, 40.f)) && !is_solved_) {
214-
is_running_ = true;
231+
232+
// RUN button
233+
{
234+
if (disable_run_) ImGui::BeginDisabled();
235+
bool clicked = ImGui::Button("RUN", ImVec2(100.f, 40.f));
236+
if (disable_run_) ImGui::EndDisabled();
237+
if (clicked && !is_solved_) {
238+
is_running_ = true;
239+
disable_gui_parameters_ = true;
240+
disable_run_ = true;
241+
}
215242
}
216243
}
217244
ImGui::Spacing();
218245
ImGui::Separator();
219246
ImGui::Spacing();
247+
248+
if (disable_gui_parameters_) ImGui::BeginDisabled();
249+
220250
if (ImGui::InputInt("max_iterations", &max_iterations_, 1, 1000)) {
221251
if (max_iterations_ < 1) max_iterations_ = 1;
222252
}
@@ -236,6 +266,8 @@ void SamplingBased::renderGui() {
236266
initParameters();
237267
}
238268
}
269+
270+
if (disable_gui_parameters_) ImGui::EndDisabled();
239271
}
240272

241273
void SamplingBased::render() {

0 commit comments

Comments
 (0)