-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenuwindow.cpp
More file actions
432 lines (361 loc) · 11 KB
/
Copy pathmenuwindow.cpp
File metadata and controls
432 lines (361 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "menuwindow.h"
MapDrawArea::~MapDrawArea()
{
}
MapDrawArea::MapDrawArea()
{
guiMapData.MapState = UNINITIALIZED;
}
MapWindow::MapWindow()
: m_draw(),
m_VertParentBox(Gtk::ORIENTATION_VERTICAL),
m_HorzChildBox(Gtk::ORIENTATION_HORIZONTAL),
m_Button_File(),
m_Button_Solve(),
m_WorkerThread(NULL),
DialogWindow()
{
// Add the text for the file button and picture
m_Button_File.add_pixlabel("open.gif","Choose file");
m_Button_Solve.add_pixlabel("worker.gif","Solve path");
// Add a buffer border to the window
set_border_width(10);
// Set title, maximize window
set_title("Astar");
maximize();
// Set the minimum window size
set_size_request(400,300);
// Connect button handlers
m_Button_File.signal_clicked().connect(sigc::mem_fun(*this,
&MapWindow::on_button_file_clicked) );
m_Button_Solve.signal_clicked().connect(sigc::mem_fun(*this,
&MapWindow::on_button_solve_clicked) );
// Connect dispatcher for worker thread
m_Dispatcher.connect(sigc::mem_fun(*this,
&MapWindow::on_notification_from_worker_thread));
// Disable the Solve button
m_Button_Solve.set_sensitive(false);
// Add a vertical box
add(m_VertParentBox);
m_VertParentBox.show();
// Horizontal box contains the buttons
m_HorzChildBox.pack_end(m_Button_File,Gtk::PACK_EXPAND_PADDING,0);
m_HorzChildBox.pack_end(m_Button_Solve,Gtk::PACK_EXPAND_PADDING,0);
// Vertical box contains the horizontal box and the drawing area
m_VertParentBox.pack_start(m_HorzChildBox,false,false,10);
m_VertParentBox.pack_start(m_draw);
// Show
show_all_children();
}
bool MapWindow::SolveOptimalPath()
{
bool result = m_draw.guiMapData.SolveOptimalPath( inputFilename.c_str(), outputFilename.c_str() );
return result;
}
void MapDrawArea::DrawObstacles(const Cairo::RefPtr<Cairo::Context>& cr)
{
// Get size characteristics of the window
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
const int lesser = MIN(width, height);
// We should be able to just store the obstacles and path once
// Do need to update based on the window size
std::vector<Coord> vObstacles = guiMapData.copyObstacles();
Coord maxXY = guiMapData.copyMaxCoord();
Coord originCoord = guiMapData.copyStartCoord();
Coord goalCoord = guiMapData.copyEndCoord();
// These have to be updated each iteration
originCoord.x = int( float(width)*float(originCoord.x)/float(maxXY.x) );
originCoord.y = int( float(height)*float(originCoord.y)/float(maxXY.y) );
goalCoord.x = int( float(width)*float(goalCoord.x)/float(maxXY.x) );
goalCoord.y = int( float(height)*float(goalCoord.y)/float(maxXY.y) );
// Draw obstacles
std::vector<Coord> scaledObstacleCoord;
std::vector<Coord> rawObstacleCoord = guiMapData.copyObstacles();
Coord stdCoord;
// Adjust obstacle values based on window size
for(std::vector<Coord>::const_iterator itr=rawObstacleCoord.begin();itr!=rawObstacleCoord.end();++itr)
{
stdCoord.x = int( float(width)*float(itr->x)/float(maxXY.x) );
stdCoord.y = int( height*float(itr->y)/float(maxXY.y) );
scaledObstacleCoord.push_back(stdCoord);
}
cr->save();
cr->set_source_rgb(0.0, 0.0, 0.0); // black for obstacles
cr->set_line_width(lesser * 0.005);
cr->set_line_cap(Cairo::LINE_CAP_ROUND);
// Plot obstacles
for(std::vector<Coord>::iterator itr=scaledObstacleCoord.begin();itr != scaledObstacleCoord.end();++itr)
{
cr->move_to( itr->x,itr->y );
cr->line_to( itr->x,itr->y );
cr->stroke();
}
// Plot start/end coord
cr->save();
cr->set_line_width(lesser * 0.015);
cr->set_source_rgb(1.0, 0.0, 0.0); // red for start point
cr->move_to( originCoord.x,originCoord.y );
cr->line_to( originCoord.x,originCoord.y );
cr->stroke();
cr->save();
cr->set_source_rgb(0.0, 1.0, 0.0); // green for end point
cr->move_to( goalCoord.x,goalCoord.y );
cr->line_to( goalCoord.x,goalCoord.y );
cr->stroke();
}
void MapDrawArea::DrawOptimalPath(const Cairo::RefPtr<Cairo::Context>& cr)
{
// This is where we draw on the window
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
const int lesser = MIN(width, height);
const Coord maxXY = guiMapData.copyMaxCoord();
// Copy the optimal path to the draw area
std::vector<Coord> optimalPath = guiMapData.copyOptPath();
// Plot the path
cr->save();
cr->set_source_rgb(1.0, 0.08, 0.58); // pink for path
cr->set_line_width(lesser * 0.005);
cr->set_line_cap(Cairo::LINE_CAP_ROUND);
for(std::vector<Coord>::iterator itr=optimalPath.begin();itr != optimalPath.end();++itr)
{
cr->move_to( int( float(width)*float(itr->x)/float(maxXY.x) ),int( height*float(itr->y)/float(maxXY.y)));
cr->line_to( int( float(width)*float(itr->x)/float(maxXY.x) ),int( height*float(itr->y)/float(maxXY.y)));
cr->stroke();
}
}
void MapDrawArea::ClearDrawingArea(const Cairo::RefPtr<Cairo::Context>& cr)
{
cr->set_source_rgb(255,255,255);
cr->paint();
}
MapWindow::~MapWindow()
{
}
// Handler for popup dialog
void MapWindow::on_button_file_clicked()
{
Gtk::FileChooserDialog dialog("Please choose a file",
Gtk::FILE_CHOOSER_ACTION_OPEN);
dialog.set_transient_for(*this);
//Add response buttons the the dialog:
dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
dialog.add_button("_Open", Gtk::RESPONSE_OK);
//Add filters, so that only certain file types can be selected:
Glib::RefPtr<Gtk::FileFilter> filter_text = Gtk::FileFilter::create();
filter_text->set_name("Text/Data files");
filter_text->add_mime_type("text/plain");
dialog.add_filter(filter_text);
Glib::RefPtr<Gtk::FileFilter> filter_any = Gtk::FileFilter::create();
filter_any->set_name("Any files");
filter_any->add_pattern("*");
dialog.add_filter(filter_any);
//Show the dialog and wait for a user response:
int result = dialog.run();
m_draw.guiMapData.MapState = UNINITIALIZED;
//Handle the response:
switch(result)
{
case(Gtk::RESPONSE_OK):
{
// Clear out the obstacles and optimal path
m_draw.guiMapData.ClearMap();
// Set the file names
inputFilename = dialog.get_filename().c_str();
outputFilename = inputFilename + ".out";
outputFilename = outputFilename.c_str();
// Initialize the obstacles and start/goal coordinates
m_draw.guiMapData.InitMap(inputFilename);
if(m_draw.guiMapData.MapInitialized)
{
m_draw.guiMapData.MapState = OBSTACLES_ONLY;
// Enable the Solve button
m_Button_Solve.set_sensitive(true);
}
else
m_draw.guiMapData.MapState = UNINITIALIZED;
break;
}
default:
break;
}
}
// Button handler for Solve button
void MapWindow::on_button_solve_clicked()
{
// Find the middle of the window and show the SpinnerWindow
DialogWindow.set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
DialogWindow.get_parent_window();
DialogWindow.show();
// Spawn a new worker thread
if( !m_WorkerThread )
{
m_WorkerThread = Glib::Threads::Thread::create(sigc::bind(sigc::mem_fun(worker, &Worker::solve_path), this));
DialogWindow.spawn_timer_thread();
}
}
// Draw the obstacles, start point, end point, final path
bool MapDrawArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
switch(guiMapData.MapState)
{
case UNINITIALIZED:
ClearDrawingArea(cr);
break;
case OBSTACLES_ONLY:
DrawObstacles(cr);
break;
case FULL_MAP:
DrawObstacles(cr);
DrawOptimalPath(cr);
break;
}
return true;
}
// Function executed after the map has been solved
void MapWindow::on_notification_from_worker_thread()
{
if (m_WorkerThread && worker.has_stopped())
{
// Show a window dialog if there is no solution
if( !worker.get_solution_exists() )
{
DialogWindow.notification();
Gtk::MessageDialog *dlg = new Gtk::MessageDialog("No solution exists.");
dlg->run();
delete dlg;
}
// Work is done.
m_WorkerThread->join();
m_WorkerThread = NULL;
// Update buttons
m_Button_Solve.set_sensitive(false);
m_Button_File.set_sensitive(true);
// Force the on_draw function
m_draw.queue_draw();
// Hide the spinner window
DialogWindow.notification();
}
}
void MapWindow::notify()
{
m_Dispatcher.emit();
}
void MapWindow::update_timer()
{
}
Worker::Worker() { };
void Worker::solve_path(MapWindow *window)
{
Glib::Threads::Mutex::Lock lock(m_Mutex);
m_has_stopped = false;
lock.release();
bool result = false;
result = window->SolveOptimalPath();
lock.acquire();
m_has_stopped = true;
m_solution_exists = result;
lock.release();
window->notify();
}
bool Worker::has_stopped()
{
Glib::Threads::Mutex::Lock lock(m_Mutex);
return m_has_stopped;
}
SpinnerWindow::SpinnerWindow()
: SpinnerBox(Gtk::ORIENTATION_VERTICAL),
spinner(),
WaitLabel("Please wait..."),
TimeElapsed("0.00"),
m_TimerWorkerThread(NULL)
{
m_Dispatcher.connect(sigc::mem_fun(*this,
&SpinnerWindow::notification));
SpinnerBox.pack_start(spinner,Gtk::PACK_EXPAND_PADDING,0);
SpinnerBox.pack_end(WaitLabel,Gtk::PACK_EXPAND_PADDING,0);
SpinnerBox.pack_end(TimeElapsed,Gtk::PACK_EXPAND_PADDING,0);
add(SpinnerBox);
show_all_children();
set_size_request(150,25);
set_title("Solving");
hide();
}
// Launch a new thread to keep track of the time
void SpinnerWindow::spawn_timer_thread()
{
if( !m_TimerWorkerThread )
m_TimerWorkerThread = Glib::Threads::Thread::create(sigc::bind(sigc::mem_fun(timer_thread, &TimerWorker::keep_time), this));
}
// Update the GUI with the current time elapsed
void SpinnerWindow::update_timer()
{
double time_elapsed;
timer_thread.get_timer(&time_elapsed);
Glib::ustring time_string = " ";
time_string = Glib::ustring::compose("%1\n",time_elapsed);
// Set the text in the spinner window
TimeElapsed.set_label(time_string);
}
TimerWorker::TimerWorker() { };
bool TimerWorker::has_stopped()
{
Glib::Threads::Mutex::Lock lock(m_Mutex);
return m_has_stopped;
}
// Establish the start time to measure against
void TimerWorker::start_timer()
{
gettimeofday(&BeginTimer,NULL);
}
// Measure the time since the BeginTimer was started
void TimerWorker::get_timer(double *timer_var)
{
gettimeofday(&EndTimer,NULL);
*timer_var = (double)EndTimer.tv_sec-(double)BeginTimer.tv_sec+((double)EndTimer.tv_usec)/1e6-((double)BeginTimer.tv_usec/1e6);
}
// Keep track of the time it takes to solve the map
void TimerWorker::keep_time(SpinnerWindow *m_window)
{
// Lock the data
Glib::Threads::Mutex::Lock lock(m_Mutex);
m_has_stopped = false;
start_timer();
m_window->StartSpinner();
m_window->show();
lock.release();
// Execute until map solved
while( !m_has_stopped )
{
lock.acquire();
// Update the time
m_window->update_timer();
lock.release();
// Sleep thread every 1 ms
usleep(1000);
}
lock.release();
}
// Notification from timer thread when finished
void SpinnerWindow::notification()
{
timer_thread.has_stopped(true);
spinner.stop();
// Work done, join threads
if( m_TimerWorkerThread && timer_thread.has_stopped() )
{
m_TimerWorkerThread->join();
m_TimerWorkerThread = NULL;
}
// Hide the window when done
hide();
}
// Read if the timer thread has stopped
void TimerWorker::has_stopped(bool stopped)
{
Glib::Threads::Mutex::Lock lock(m_Mutex);
m_has_stopped = stopped;
}