-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdslab.cpp
More file actions
455 lines (351 loc) · 11.9 KB
/
dslab.cpp
File metadata and controls
455 lines (351 loc) · 11.9 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "dslab.h"
#include<stdlib.h>
#include<stdint.h>
#if defined(_WIN32)
#include <chrono>
int gettimeofday(struct timeval* tp, struct timezone* tzp) {
namespace sc = std::chrono;
sc::system_clock::duration d = sc::system_clock::now().time_since_epoch();
sc::seconds s = sc::duration_cast<sc::seconds>(d);
tp->tv_sec = s.count();
tp->tv_usec = sc::duration_cast<sc::microseconds>(d - s).count();
return 0;
}
#else
#include <sys/time.h>
#include<unistd.h>
#endif // _WIN32
BasicGLPane *glPane;
BasicGLPane *getView() {return glPane;};
DataEngine *pDataEngine;
void registerDataEngine(DataEngine *pData)
{
pDataEngine = pData;
}
DataEngine *getEngine() {return pDataEngine;};
class MyApp: public wxApp
{
virtual bool OnInit();
uint64_t lastthink;
bool render_loop_on;
public:
wxFrame *frame;
wxTextCtrl *textBox;
wxStreamToTextRedirector *redirect=NULL;
void activateRenderLoop(bool on)
{
if(on && !render_loop_on)
{
Connect( wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(MyApp::onIdle) );
render_loop_on = true;
}
else if(!on && render_loop_on)
{
Disconnect( wxEVT_IDLE, wxIdleEventHandler(MyApp::onIdle) );
render_loop_on = false;
}
}
uint64_t _ticks(void)
{
struct timeval tv;
gettimeofday(&tv, 0);
return uint64_t( tv.tv_sec ) * 1000 + tv.tv_usec / 1000;
}
void activateRedirection(bool which)
{
if (!which)
{
if(redirect != NULL)
delete redirect;
redirect = NULL;
}
}
void onIdle(wxIdleEvent& evt)
{
getEngine()->think(_ticks() - lastthink);
if(render_loop_on)
{
getView()->Refresh();
evt.RequestMore(); // render continuously, not only once on idle
}
lastthink = _ticks();
}
};
IMPLEMENT_APP(MyApp);
class myFrame :public wxFrame
{
public:
void menuHandler(wxCommandEvent& event )
{
switch(event.GetId())
{
case wxID_EXIT:
getEngine()->beforeQuit();
Close(true);
break;
case ID_ACTIVATE_LOOP:
wxGetApp().activateRenderLoop(true);
break;
case ID_DEACTIVATE_LOOP:
wxGetApp().activateRenderLoop(false);
break;
case ID_RENDER:
getView()->Refresh();
break;
case wxID_ABOUT:
wxMessageBox( wxT("This is a pervasive data science demonstration tool written by <martin.werner@ifi.lmu.de> to showcase research results.\n\nFind and download it at https://github.com/mwernerds/dslab"), wxT("About"), wxOK | wxICON_INFORMATION );
break;
default:
try{
getEngine()->handleMenu(event.GetId());
}catch(const std::exception& e)
{
std::string s = e.what();
wxString mystring(s.c_str(), wxConvUTF8);
wxMessageBox(mystring,_("Standard Exception"));
}
catch(...)
{
wxMessageBox(_("Unhandled and Unknown Exception (not std::exception derivative) in user code segment."),_("Fatal Error"));
}
}
}
myFrame(const wxString& title,
const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *) NULL, -1, title, pos, size)
{
Connect(wxID_EXIT, wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(myFrame::menuHandler));
Connect(wxID_ABOUT, wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(myFrame::menuHandler));
Connect(MIN_Menu, MAX_Menu, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(myFrame::menuHandler));
Connect(ID_CORE_MENU_MIN, ID_CORE_MENU_MAX, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(myFrame::menuHandler));
}
};
#define TEXT_ID wxID_HIGHEST + 1
bool MyApp::OnInit()
{
// first register the foreign class
registerDataEngine(getDataEngineImplementation());
#if defined(DSLAB_USE_WINDOWS_CONSOLE)
RedirectIOToConsole();
#endif
//wxMessageBox( wxT("This is a pervasive data science demonstration tool written by <martin.werner@ifi.lmu.de> to showcase research results.\n\n (https://github.com/mwernerds/dslab), wxT("About"), wxOK | wxICON_INFORMATION );
wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
wxString s(getDataEngineImplementation()->getTitle().c_str(),wxConvUTF8);
frame = new myFrame( s, wxPoint(50,50), wxSize(400,200));
int args[] = {WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, WX_GL_STENCIL_SIZE, 8, 0};
glPane = new BasicGLPane( (wxFrame*) frame, args);
sizer->Add(glPane, 5, wxEXPAND);
textBox = new wxTextCtrl( (wxFrame*) frame, TEXT_ID, _(""), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2 , wxDefaultValidator, wxTextCtrlNameStr);
sizer->Add(textBox,1, wxEXPAND);
/*The menu*/
wxMenu *menuFile = new wxMenu;
//menuFile->Append(500, wxT("&Hello...\tCtrl-H"), wxT("Help string shown in status bar for this menu item"));
// menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuRendering = new wxMenu();
menuRendering->Append(ID_ACTIVATE_LOOP,wxT("&Loop Renderer"),wxT("Activates Loop Rendering. This is the default"));
menuRendering->Append(ID_DEACTIVATE_LOOP,wxT("&Stop Renderer"),wxT("Deactivates Loop Rendering. This frees up resources, if rendering is exceptionally hard"));
menuRendering->Append(ID_RENDER,wxT("&Render View"),wxT("Render Single View"));
menuRendering->AppendSeparator();
getEngine()->extendViewMenu(menuRendering);
wxMenu *menuDataEngine = new wxMenu();
getEngine()->createMenu(menuDataEngine);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, wxT("&File" ));
menuBar->Append( menuRendering, wxT("&Rendering" ));
menuBar->Append( menuDataEngine, wxT("&Data Engine" ));
menuBar->Append( menuHelp, wxT("&Help" ));
frame->SetMenuBar( menuBar );
frame->CreateStatusBar();
frame->SetStatusText(wxT( "Welcome to DSLAB <see www.martinwerner.de for more information>!" ));
// Start off with the default toolbar. I expect this to be overwritten in many cases
frame->Maximize(true);
frame->SetSizer(sizer);
frame->SetAutoLayout(true);
frame->Show();
#ifndef DISABLE_GUI_CONSOLE
redirect = new wxStreamToTextRedirector(textBox);
#endif
/* if (wxGetApp().argc >1)
if(wxStrcmp(wxGetApp().argv[1],wxT("--console")) != 0)
console = true;
if (!console)
redirect = new wxStreamToTextRedirector(textBox); */
//RedirectIOToConsole();
// activateRenderLoop(true);
getEngine()->Init();
return true;
}
BEGIN_EVENT_TABLE(BasicGLPane, wxGLCanvas)
EVT_MOTION(BasicGLPane::mouseMoved)
EVT_LEFT_DOWN(BasicGLPane::mouseDown)
EVT_LEFT_UP(BasicGLPane::mouseReleased)
EVT_RIGHT_DOWN(BasicGLPane::rightClick)
EVT_LEAVE_WINDOW(BasicGLPane::mouseLeftWindow)
EVT_SIZE(BasicGLPane::resized)
EVT_CHAR_HOOK(BasicGLPane::keyPressed) // was keydown, now works
EVT_KEY_UP(BasicGLPane::keyReleased)
EVT_MOUSEWHEEL(BasicGLPane::mouseWheelMoved)
EVT_PAINT(BasicGLPane::render)
END_EVENT_TABLE()
// some useful events to use
void BasicGLPane::mouseMoved(wxMouseEvent& event)
{
if (event.Dragging())
{
dragging = true;
getEngine()->mouseDragging(last_known_mouse_x,last_known_mouse_y,event.GetX(), event.GetY());
}else{
getEngine()->mouseMoved(event.GetX(), event.GetY());
}
// BOOK-KEEPING
last_known_mouse_x=event.GetX();
last_known_mouse_y=event.GetY();
this->Refresh();
}
void BasicGLPane::mouseDown(wxMouseEvent& event) {
}
void BasicGLPane::mouseWheelMoved(wxMouseEvent& event) {
getEngine()->mouseWheel(event.GetX(),event.GetY(), event.GetWheelRotation() / event.GetWheelDelta());
}
void BasicGLPane::mouseReleased(wxMouseEvent& event) {
if (dragging)
{
dragging = false;
return;
}
getEngine()->mouseClick(event.GetX(), event.GetY());
}
void BasicGLPane::rightClick(wxMouseEvent& event) {
if (dragging)
{
dragging = false;
return;
}
getEngine()->rightClick(event.GetX(), event.GetY());
}
void BasicGLPane::mouseLeftWindow(wxMouseEvent& event) {}
void BasicGLPane::keyPressed(wxKeyEvent& event) {
getEngine()->key_down(event.GetKeyCode(),event.ControlDown(),event.ShiftDown(),event.AltDown());
}
void BasicGLPane::keyReleased(wxKeyEvent& event) {
getEngine()->key(event.GetKeyCode(),event.ControlDown(),event.ShiftDown(),event.AltDown());
}
// Vertices and faces of a simple cube to demonstrate 3D render
// source: http://www.opengl.org/resources/code/samples/glut_examples/examples/cube.c
GLfloat v[8][3];
GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube. */
{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
BasicGLPane::BasicGLPane(wxFrame* parent, int* args) :
wxGLCanvas(parent, wxID_ANY, args, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
{
m_context = new wxGLContext(this);
dragging = false;
SetBackgroundStyle(wxBG_STYLE_CUSTOM);
}
BasicGLPane::~BasicGLPane()
{
delete m_context;
}
void BasicGLPane::resized(wxSizeEvent& evt)
{
// wxGLCanvas::OnSize(evt);
wxSize size = evt.GetSize();
getEngine()->resize(size.GetWidth(), size.GetHeight());
Refresh();
}
/** Inits the OpenGL viewport for drawing in 3D. */
void BasicGLPane::prepare3DViewport(int topleft_x, int topleft_y, int bottomrigth_x, int bottomrigth_y)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_COLOR_MATERIAL);
glViewport(topleft_x, topleft_y, bottomrigth_x-topleft_x, bottomrigth_y-topleft_y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float ratio_w_h = (float)(bottomrigth_x-topleft_x)/(float)(bottomrigth_y-topleft_y);
gluPerspective(45 /*view angle*/, ratio_w_h, 0.1 /*clip close*/, 200 /*clip far*/);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/** Inits the OpenGL viewport for drawing in 2D. */
void BasicGLPane::prepare2DViewport(int topleft_x, int topleft_y, int bottomrigth_x, int bottomrigth_y)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
glEnable(GL_TEXTURE_2D); // textures
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glViewport(topleft_x, topleft_y, bottomrigth_x-topleft_x, bottomrigth_y-topleft_y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(topleft_x, bottomrigth_x, bottomrigth_y, topleft_y);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int BasicGLPane::getWidth()
{
return GetSize().x;
}
int BasicGLPane::getHeight()
{
return GetSize().y;
}
void BasicGLPane::render( wxPaintEvent& evt )
{
if(!IsShown()) return;
wxGLCanvas::SetCurrent(*m_context);
wxPaintDC(this); // only to be used in paint events. use wxClientDC to paint outside the paint event
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
getEngine()->render(getWidth(),getHeight());
glFlush();
SwapBuffers();
}
void BasicGLPane::writeScreenshot(std::string filename)
{
wxInitAllImageHandlers();
// wxPaintEvent evt;
glDrawBuffer(GL_BACK);
//render(evt);
Refresh(); // not sure it works.
glReadBuffer(GL_BACK);
GLvoid *imageData = malloc(getWidth()*getHeight()*3); // 3 byte for RGB
if (imageData == NULL)
{
fprintf(stderr,"Memory allocation failed for screenshot\n");
return;
}
glReadPixels(0,0,getWidth(),getHeight(),GL_RGB,GL_UNSIGNED_BYTE,imageData);
wxImage img(getWidth(),getHeight(), true);
img.SetData((unsigned char *) imageData); // This should make wxWidgets responsible for the allocation
wxString wxFilename(filename.c_str(), wxConvUTF8);;
img.SaveFile(wxFilename,wxBITMAP_TYPE_PNG );
}
void DS_stop_redirection()
{
wxGetApp().activateRedirection(false);
}
void DS_stop_rendering()
{
wxGetApp().activateRenderLoop(false);
}
void DS_start_rendering()
{
wxGetApp().activateRenderLoop(true);
}
void DS_render_once()
{
getView()->Refresh();
}
void DS_screenshot(std::string filename)
{
getView()->writeScreenshot(filename);
}
wxFrame *getFrame() {return wxGetApp().frame;};