From 6c00bda1b58ce329e38c6692ba4fa71f002c68ed Mon Sep 17 00:00:00 2001 From: Max Yang Date: Tue, 19 Jan 2021 12:18:26 -0800 Subject: [PATCH 01/39] Move background setting to MyExpose --- lib/aux_vis.cpp | 4 ++++ lib/openglvis.cpp | 3 --- lib/openglvis.hpp | 11 +++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 1ec14c18..bdf2cbfd 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -381,6 +381,10 @@ void MyExpose(GLsizei w, GLsizei h) GLuint alpha_tex = locscene->palette.GetAlphaTexture(); wnd->getRenderer().setColorTexture(color_tex); wnd->getRenderer().setAlphaTexture(alpha_tex); + + std::array bgcol = locscene->GetBackgroundColor(); + wnd->getRenderer().setClearColor(bgcol[0], bgcol[1], bgcol[2], bgcol[3]); + gl3::SceneInfo frame = locscene->GetSceneObjs(); for (auto drawable_ptr : frame.needs_buffering) { diff --git a/lib/openglvis.cpp b/lib/openglvis.cpp index 069a94d8..b5744b46 100644 --- a/lib/openglvis.cpp +++ b/lib/openglvis.cpp @@ -123,7 +123,6 @@ VisualizationScene::VisualizationScene() ViewCenterY = 0.0; background = BG_WHITE; - GetAppWindow()->getRenderer().setClearColor(1.f, 1.f, 1.f, 1.f); _use_cust_l0_pos = false; light_mat_idx = 3; use_light = true; @@ -357,12 +356,10 @@ void VisualizationScene::ToggleBackground() if (background == BG_BLK) { background = BG_WHITE; - GetAppWindow()->getRenderer().setClearColor(1.f, 1.f, 1.f, 1.f); } else { background = BG_BLK; - GetAppWindow()->getRenderer().setClearColor(0.f, 0.f, 0.f, 1.f); } } diff --git a/lib/openglvis.hpp b/lib/openglvis.hpp index a23bf93a..3949a88d 100644 --- a/lib/openglvis.hpp +++ b/lib/openglvis.hpp @@ -172,6 +172,17 @@ class VisualizationScene void SetLight0CustomPos(std::array pos); void ToggleBackground(); + std::array GetBackgroundColor() + { + if (background == BG_BLK) + { + return { 0.f, 0.f, 0.f, 1.f }; + } + else + { + return { 1.f, 1.f, 1.f, 1.f }; + } + } void GenerateAlphaTexture() { palette.GenerateAlphaTexture(matAlpha, matAlphaCenter); } From a1c18c4cd1de8c4c025b0ab8621e5c6fc2a26fef Mon Sep 17 00:00:00 2001 From: Max Yang Date: Tue, 19 Jan 2021 16:25:20 -0800 Subject: [PATCH 02/39] Move antialiasing to aux_vis --- lib/aux_vis.cpp | 13 +++++++++++++ lib/aux_vis.hpp | 1 + lib/vsdata.cpp | 13 ------------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index bdf2cbfd..c3955766 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -108,6 +108,7 @@ int InitVisualization (const char name[], int x, int y, int w, int h) wnd->setOnMouseMove(SDL_BUTTON_RIGHT, RightButtonLoc); wnd->setTouchPinchCallback(TouchPinch); + wnd->setOnKeyDown('A', ToggleAntialiasing); // auxKeyFunc (AUX_p, KeyCtrlP); // handled in vsdata.cpp wnd->setOnKeyDown (SDLK_s, KeyS); @@ -739,6 +740,18 @@ void RightButtonLoc (EventInfo *event) void RightButtonUp (EventInfo*) {} +void ToggleAntialiasing() +{ + bool curr_aa = wnd->getRenderer().getAntialiasing(); + wnd->getRenderer().setAntialiasing(!curr_aa); + const std::string strings_off_on[2] = { "off", "on" }; + + cout << "Multisampling/Antialiasing: " + << strings_off_on[!curr_aa ? 1 : 0] << endl; + + SendExposeEvent(); +} + void TouchPinch(SDL_MultiGestureEvent & e) { // Scale or Zoom? diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 93542868..c8feddb7 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -55,6 +55,7 @@ void RightButtonUp (EventInfo *event); void TouchPinch(SDL_MultiGestureEvent & e); +void ToggleAntialiasing(); void KeyCtrlP(); void KeyS(); void KeyQPressed(); diff --git a/lib/vsdata.cpp b/lib/vsdata.cpp index aa5c5fc9..d1680d7f 100644 --- a/lib/vsdata.cpp +++ b/lib/vsdata.cpp @@ -740,18 +740,6 @@ void KeyKPressed() SendExposeEvent(); } -void KeyAPressed() -{ - bool curr_aa = GetAppWindow()->getRenderer().getAntialiasing(); - GetAppWindow()->getRenderer().setAntialiasing(!curr_aa); - - cout << "Multisampling/Antialiasing: " - << strings_off_on[!curr_aa ? 1 : 0] << endl; - - // vsdata -> EventUpdateColors(); - SendExposeEvent(); -} - void KeyCommaPressed() { locscene->matAlphaCenter -= 0.25; @@ -1111,7 +1099,6 @@ void VisualizationSceneScalarData::Init() // wnd->setOnKeyDown('a', KeyaPressed); wnd->setOnKeyDown('a', Key_Mod_a_Pressed); - wnd->setOnKeyDown('A', KeyAPressed); wnd->setOnKeyDown('r', KeyrPressed); wnd->setOnKeyDown('R', KeyRPressed); From dd9b278d95a6fcadcfd25d9aad5fc5b665284aed Mon Sep 17 00:00:00 2001 From: Max Yang Date: Tue, 19 Jan 2021 16:28:40 -0800 Subject: [PATCH 03/39] Create GLVisWindow class, move font handling stuff into it --- glvis.cpp | 41 ++++++++++++-------- lib/aux_vis.cpp | 82 ++++++++++++++++------------------------ lib/aux_vis.hpp | 37 ++++++++++++------ lib/font.cpp | 4 +- lib/font.hpp | 14 ++++++- lib/gl/renderer.cpp | 4 +- lib/gl/renderer.hpp | 11 ++++-- lib/gl/renderer_core.cpp | 12 +++--- lib/gl/renderer_core.hpp | 4 +- lib/gl/renderer_ff.cpp | 14 +++---- lib/gl/renderer_ff.hpp | 4 +- lib/openglvis.hpp | 4 ++ lib/sdl.cpp | 2 +- lib/sdl.hpp | 2 +- lib/vsdata.cpp | 6 +-- 15 files changed, 133 insertions(+), 108 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 90f6f542..f11c580c 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -48,14 +48,17 @@ int window_x = 0; // not a command line option int window_y = 0; // not a command line option int window_w = 400; int window_h = 350; +bool legacy_gl_ctx = false; const char *window_title = string_default; const char *c_plot_caption = string_none; +const char *font_name = string_default; string plot_caption; string extra_caption; // Global variables int input = 1; StreamState stream_state; +GLVisWindow * mainWindow = nullptr; VisualizationSceneScalarData *vs = NULL; communication_thread *comm_thread = NULL; @@ -105,15 +108,31 @@ bool GLVisInitVis(int field_type) const char *win_title = (window_title == string_default) ? window_titles[field_type] : window_title; - if (InitVisualization(win_title, window_x, window_y, window_w, window_h)) + try { - cerr << "Initializing the visualization failed." << endl; + mainWindow = new GLVisWindow(win_title, window_x, window_y, + window_w, window_h, legacy_gl_ctx); + } + catch (std::runtime_error& ex) + { + cerr << "Initializing the visualization failed: " << endl + << ex.what() << endl; return false; } + catch(...) + { + cerr << "Initializing the visualization failed - unknown error." + << endl; + return false; + } + if (font_name != string_default) + { + mainWindow->SetFont(font_name); + } if (input_streams.Size() > 0) { - GetAppWindow()->setOnKeyDown(SDLK_SPACE, ThreadsPauseFunc); + mainWindow->getSdl()->setOnKeyDown(SDLK_SPACE, ThreadsPauseFunc); glvis_command = new GLVisCommand(&vs, stream_state, &keep_attr); comm_thread = new communication_thread(input_streams); } @@ -156,6 +175,8 @@ bool GLVisInitVis(int field_type) VisualizationSceneSolution3d * vss; vs = vss = new VisualizationSceneSolution3d(*stream_state.mesh, stream_state.sol); + // HACK: below needs to be called before ToggleAxes + vs->SetFont(mainWindow->getFont()); if (stream_state.grid_f) { vss->SetGridFunction(stream_state.grid_f.get()); @@ -235,11 +256,11 @@ bool GLVisInitVis(int field_type) } if (stream_state.mesh->SpaceDimension() == 2 && field_type == 2) { - SetVisualizationScene(vs, 2, stream_state.keys.c_str()); + mainWindow->SetVisualizationScene(vs, 2, stream_state.keys.c_str()); } else { - SetVisualizationScene(vs, 3, stream_state.keys.c_str()); + mainWindow->SetVisualizationScene(vs, 3, stream_state.keys.c_str()); } } return true; @@ -863,14 +884,12 @@ int main (int argc, char *argv[]) bool mac = false; const char *stream_file = string_none; const char *script_file = string_none; - const char *font_name = string_default; int portnum = 19916; bool secure = socketstream::secure_default; int multisample = GetMultisample(); double line_width = 1.0; double ms_line_width = gl3::LINE_WIDTH_AA; int geom_ref_type = Quadrature1D::ClosedUniform; - bool legacy_gl_ctx = false; OptionsParser args(argc, argv); @@ -986,10 +1005,6 @@ int main (int argc, char *argv[]) { stream_state.keys = arg_keys; } - if (font_name != string_default) - { - SetFont(font_name); - } if (multisample != GetMultisample()) { SetMultisample(multisample); @@ -1006,10 +1021,6 @@ int main (int argc, char *argv[]) { plot_caption = c_plot_caption; } - if (legacy_gl_ctx == true) - { - SetLegacyGLOnly(legacy_gl_ctx); - } GLVisGeometryRefiner.SetType(geom_ref_type); diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index c3955766..8dc45964 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include "mfem.hpp" using namespace mfem; @@ -45,12 +46,11 @@ static int glvis_multisample = -1; float line_w = 1.f; float line_w_aa = gl3::LINE_WIDTH_AA; -SdlWindow * wnd = nullptr; -bool wndLegacyGl = false; +[[deprecated]] SdlWindow* wnd; SdlWindow * GetAppWindow() { - return wnd; + return wnd; } VisualizationScene * GetVisualizationScene() @@ -58,37 +58,27 @@ VisualizationScene * GetVisualizationScene() return locscene; } -void SetLegacyGLOnly(bool status) -{ - wndLegacyGl = true; -} - void MyExpose(GLsizei w, GLsizei h); void MyExpose(); -int InitVisualization (const char name[], int x, int y, int w, int h) +GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool legacyGlOnly) + : locscene(nullptr) { #ifdef GLVIS_DEBUG cout << "OpenGL Visualization" << endl; #endif - if (!wnd) - { - wnd = new SdlWindow(); - if (!wnd->createWindow(name, x, y, w, h, wndLegacyGl)) - { - return 1; - } - } - else + wnd.reset(new SdlWindow()); + if (!wnd->createWindow(name, x, y, w, h, legacyGlOnly)) { - wnd->clearEvents(); + throw std::runtime_error("Could not create an SDL window."); } - + ::wnd = wnd.get(); #ifdef GLVIS_DEBUG cout << "Window should be up" << endl; #endif InitFont(); + wnd->getRenderer().setFont(&font); wnd->getRenderer().setLineWidth(line_w); wnd->getRenderer().setLineWidthMS(line_w_aa); @@ -173,15 +163,7 @@ int InitVisualization (const char name[], int x, int y, int w, int h) #ifndef __EMSCRIPTEN__ wnd->setOnKeyDown(SDLK_LEFTPAREN, ShrinkWindow); wnd->setOnKeyDown(SDLK_RIGHTPAREN, EnlargeWindow); - - if (locscene) - { - delete locscene; - } #endif - locscene = nullptr; - - return 0; } void SendKeySequence(const char *seq) @@ -293,10 +275,11 @@ void CallKeySequence(const char *seq) void InitIdleFuncs(); -void SetVisualizationScene(VisualizationScene * scene, int view, - const char *keys) +void GLVisWindow::SetVisualizationScene(VisualizationScene * scene, int view, + const char *keys) { - locscene = scene; + ::locscene = locscene = scene; + locscene->SetFont(&font); locscene -> view = view; if (view == 2) { @@ -328,7 +311,7 @@ void RunVisualization() #endif InitIdleFuncs(); delete locscene; - delete wnd; + //delete wnd; wnd = nullptr; } @@ -1562,23 +1545,27 @@ vector fc_font_patterns = "Helvetica:style=Regular", }; -constexpr int default_font_size = 12; -int font_size = default_font_size; +//constexpr int default_font_size = 12; +//int font_size = default_font_size; -GlVisFont glvis_font; -std::string priority_font; +//GlVisFont glvis_font; +//std::string priority_font; -void InitFont() +void GLVisWindow::InitFont() { // This function is called after the window is created. GLenum alphaChannel = gl3::GLDevice::useLegacyTextureFmts() ? GL_ALPHA : GL_RED; - glvis_font.setAlphaChannel(alphaChannel); + int ppi_w, ppi_h; + wnd->getDpi(ppi_w, ppi_h); + bool is_hidpi = wnd->isHighDpi(); + font.SetDPIParams(is_hidpi, ppi_w, ppi_h); + font.setAlphaChannel(alphaChannel); bool try_fc_patterns = true; if (!priority_font.empty()) { if (SetFont({priority_font}, font_size) || - glvis_font.LoadFont(priority_font, 0, font_size)) + font.LoadFont(priority_font, 0, font_size)) { try_fc_patterns = false; } @@ -1597,18 +1584,13 @@ void InitFont() << endl; } } - wnd->getRenderer().setFontTexture(glvis_font.getFontTex()); -} - -GlVisFont * GetFont() -{ - return &glvis_font; + wnd->getRenderer().setFontTexture(font.getFontTex()); } -bool SetFont(const vector& font_patterns, int height) +bool GLVisWindow::SetFont(const vector& font_patterns, int height) { #ifdef __EMSCRIPTEN__ - return glvis_font.LoadFont("OpenSans.ttf", 0, height); + return font.LoadFont("OpenSans.ttf", 0, height); #else if (!FcInit()) { @@ -1671,7 +1653,7 @@ bool SetFont(const vector& font_patterns, int height) FcFontSetDestroy(fs); if (font_file != std::string("")) { - if (glvis_font.LoadFont(font_file, font_index, height)) + if (font.LoadFont(font_file, font_index, height)) { break; } @@ -1685,11 +1667,11 @@ bool SetFont(const vector& font_patterns, int height) FcFini(); - return glvis_font.isFontLoaded(); + return font.isFontLoaded(); #endif } -void SetFont(const std::string& fn) +void GLVisWindow::SetFont(const std::string& fn) { priority_font = fn; size_t pos = priority_font.rfind('-'); diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index c8feddb7..4a258ee2 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -19,11 +19,31 @@ #include "font.hpp" #include "openglvis.hpp" -/// Initializes the visualization and some keys. -int InitVisualization(const char name[], int x, int y, int w, int h); +class GLVisWindow +{ +public: + /// Initializes the visualization and some keys. + GLVisWindow(std::string name, int x, int y, int w, int h, bool legacyGlOnly); -void SetVisualizationScene(VisualizationScene * scene, - int view = 3, const char *keys = NULL); + void SetVisualizationScene(VisualizationScene * scene, + int view = 3, const char *keys = NULL); + void SetFont(const std::string& fn); + + GlVisFont* getFont() { return &font; } + + SdlWindow* getSdl() { return wnd.get(); } + +private: + void InitFont(); + bool SetFont(const vector& patterns, int height); + std::unique_ptr wnd; + VisualizationScene* locscene; + + int visualize; + GlVisFont font; + std::string priority_font; + int font_size = 12; +}; /// Start the infinite visualization loop. void RunVisualization(); @@ -35,11 +55,9 @@ void MyExpose(); void MainLoop(); -SdlWindow * GetAppWindow(); +[[deprecated]] SdlWindow * GetAppWindow(); VisualizationScene * GetVisualizationScene(); -void SetLegacyGLOnly(bool status); - void AddIdleFunc(void (*Func)(void)); void RemoveIdleFunc(void (*Func)(void)); @@ -122,9 +140,4 @@ float GetLineWidth(); void SetLineWidthMS(float width_ms); float GetLineWidthMS(); -void InitFont(); -GlVisFont * GetFont(); -bool SetFont(const vector& patterns, int height); -void SetFont(const std::string& fn); - #endif diff --git a/lib/font.cpp b/lib/font.cpp index ddcf4bdc..da694fbf 100644 --- a/lib/font.cpp +++ b/lib/font.cpp @@ -43,13 +43,11 @@ bool GlVisFont::LoadFont(const std::string& path, int font_index, int font_size) return false; } face_has_kerning = FT_HAS_KERNING(face); - int ppi_w, ppi_h; - GetAppWindow()->getDpi(ppi_w, ppi_h); const bool use_fixed_ppi_h = true; if (use_fixed_ppi_h) { double ratio = double(ppi_w)/ppi_h; - ppi_h = GetAppWindow()->isHighDpi() ? 192 : 96; + ppi_h = is_hidpi ? 192 : 96; ppi_w = ratio*ppi_h + 0.5; #ifdef GLVIS_DEBUG cout << "Fonts use fixed ppi: " << ppi_w << " x " << ppi_h << endl; diff --git a/lib/font.hpp b/lib/font.hpp index a1f3137e..ddd8b29f 100644 --- a/lib/font.hpp +++ b/lib/font.hpp @@ -47,8 +47,18 @@ class GlVisFont FT_Library library; FT_Face face; bool face_has_kerning; + + bool is_hidpi; + int ppi_w, ppi_h; public: + void SetDPIParams(bool is_hidpi, int ppi_w, int ppi_h) + { + this->is_hidpi = is_hidpi; + this->ppi_w = ppi_w; + this->ppi_h = ppi_h; + } + bool LoadFont(const std::string& path, int font_index, int font_size); const glyph &GetTexChar(char c) const @@ -62,7 +72,9 @@ class GlVisFont GlVisFont() : init(false), font_init(false), - face_has_kerning(false) + face_has_kerning(false), + is_hidpi(false), + ppi_w(96), ppi_h(96) { if (FT_Init_FreeType(&library)) { diff --git a/lib/gl/renderer.cpp b/lib/gl/renderer.cpp index 7d6442b4..ea1d269c 100644 --- a/lib/gl/renderer.cpp +++ b/lib/gl/renderer.cpp @@ -238,7 +238,7 @@ void MeshRenderer::render(const RenderQueue& queue) device->setNumLights(0); for (TextBuffer* buf : text_bufs) { - device->drawDeviceBuffer(*buf); + device->drawDeviceBuffer(*font, *buf); } device->enableDepthWrite(); if (feat_use_fbo_antialias || !msaa_enable) { device->disableBlend(); } @@ -391,7 +391,7 @@ void MeshRenderer::buffer(GlDrawable* buf) } } } - device->bufferToDevice(buf->text_buffer); + device->bufferToDevice(*font, buf->text_buffer); } void GLDevice::init() diff --git a/lib/gl/renderer.hpp b/lib/gl/renderer.hpp index 6144faa0..61483a37 100644 --- a/lib/gl/renderer.hpp +++ b/lib/gl/renderer.hpp @@ -20,6 +20,7 @@ #include "platform_gl.hpp" #include "types.hpp" #include "../material.hpp" +#include "../font.hpp" #include "../palettes.hpp" namespace gl3 @@ -173,10 +174,10 @@ class GLDevice // Load a client-side vertex buffer into a device buffer. virtual void bufferToDevice(array_layout layout, IVertexBuffer& buf) = 0; virtual void bufferToDevice(array_layout layout, IIndexedBuffer& buf) = 0; - virtual void bufferToDevice(TextBuffer& t_buf) = 0; + virtual void bufferToDevice(GlVisFont& font, TextBuffer& t_buf) = 0; // Draw the data loaded in a device buffer. virtual void drawDeviceBuffer(int hnd) = 0; - virtual void drawDeviceBuffer(const TextBuffer& t_buf) = 0; + virtual void drawDeviceBuffer(GlVisFont& font, const TextBuffer& t_buf) = 0; // === Transform feedback functions === @@ -201,6 +202,7 @@ class MeshRenderer GLuint color_tex, alpha_tex, font_tex; float line_w, line_w_aa; PaletteState* pal; + GlVisFont* font; bool feat_use_fbo_antialias; void init(); @@ -209,7 +211,8 @@ class MeshRenderer : msaa_enable(false) , msaa_samples(0) , line_w(1.f) - , line_w_aa(LINE_WIDTH_AA) { init(); } + , line_w_aa(LINE_WIDTH_AA) + , font(nullptr) { init(); } template void setDevice() @@ -227,6 +230,8 @@ class MeshRenderer } void setPalette(PaletteState* pal) { this->pal = pal; } + void setFont(GlVisFont* fnt) { this->font = fnt; } + // Sets the texture handle of the color palette. void setColorTexture(GLuint tex_h) { color_tex = tex_h; } // Sets the texture handle of the alpha texture. diff --git a/lib/gl/renderer_core.cpp b/lib/gl/renderer_core.cpp index dc761643..50a28be1 100644 --- a/lib/gl/renderer_core.cpp +++ b/lib/gl/renderer_core.cpp @@ -290,19 +290,19 @@ void CoreGLDevice::bufferToDevice(array_layout layout, IIndexedBuffer& buf) buf.getIndices().data(), GL_STATIC_DRAW); } -void CoreGLDevice::bufferToDevice(TextBuffer &t_buf) +void CoreGLDevice::bufferToDevice(GlVisFont& font, TextBuffer &t_buf) { std::vector buf_data; - float tex_w = GetFont()->getAtlasWidth(); - float tex_h = GetFont()->getAtlasHeight(); + float tex_w = font.getAtlasWidth(); + float tex_h = font.getAtlasHeight(); for (auto &e : t_buf) { float pen_x = e.ox, pen_y = e.oy; char prev_c = '\0'; for (char c : e.text) { - const GlVisFont::glyph &g = GetFont()->GetTexChar(c); - pen_x += GetFont()->GetKerning(prev_c, c); + const GlVisFont::glyph &g = font.GetTexChar(c); + pen_x += font.GetKerning(prev_c, c); // note: subtract 1 to account for the padding in the texture glyphs float cur_x = pen_x + g.bear_x - 1; float cur_y = -pen_y - g.bear_y - 1; @@ -402,7 +402,7 @@ void CoreGLDevice::drawDeviceBuffer(int hnd) cerr << "WARNING: Unhandled vertex layout " << vbos[hnd].layout << endl; } } -void CoreGLDevice::drawDeviceBuffer(const TextBuffer& t_buf) +void CoreGLDevice::drawDeviceBuffer(GlVisFont&, const TextBuffer& t_buf) { if (t_buf.getHandle() == 0) { return; } if (t_buf.count() == 0) { return; } diff --git a/lib/gl/renderer_core.hpp b/lib/gl/renderer_core.hpp index 5804c1a6..eaf3a0aa 100644 --- a/lib/gl/renderer_core.hpp +++ b/lib/gl/renderer_core.hpp @@ -92,9 +92,9 @@ class CoreGLDevice : public GLDevice void bufferToDevice(array_layout layout, IVertexBuffer& buf) override; void bufferToDevice(array_layout layout, IIndexedBuffer& buf) override; - void bufferToDevice(TextBuffer& t_buf) override; + void bufferToDevice(GlVisFont& font, TextBuffer& t_buf) override; void drawDeviceBuffer(int hnd) override; - void drawDeviceBuffer(const TextBuffer& t_buf) override; + void drawDeviceBuffer(GlVisFont& font, const TextBuffer& t_buf) override; void initXfbMode() override { diff --git a/lib/gl/renderer_ff.cpp b/lib/gl/renderer_ff.cpp index 0d641335..745b4a0a 100644 --- a/lib/gl/renderer_ff.cpp +++ b/lib/gl/renderer_ff.cpp @@ -236,9 +236,9 @@ void FFGLDevice::bufferToDevice(array_layout layout, IIndexedBuffer& buf) } } -void FFGLDevice::bufferToDevice(TextBuffer&) +void FFGLDevice::bufferToDevice(GlVisFont&, TextBuffer&) { - // we can't really do anything here can only compute offset matrix at draw + // we can't really do anything here - can only compute offset matrix at draw } void FFGLDevice::drawDeviceBuffer(int hnd) @@ -266,13 +266,13 @@ void FFGLDevice::drawDeviceBuffer(int hnd) // glMultiTexCoord2f(GL_TEXTURE1, 0.f, 0.f); } -void FFGLDevice::drawDeviceBuffer(const TextBuffer& buf) +void FFGLDevice::drawDeviceBuffer(GlVisFont& font, const TextBuffer& buf) { glColor4fv(static_color.data()); glNormal3f(0.f, 0.f, 1.f); glMultiTexCoord2f(GL_TEXTURE0, 0.f, 0.f); - float tex_w = GetFont()->getAtlasWidth(); - float tex_h = GetFont()->getAtlasHeight(); + float tex_w = font.getAtlasWidth(); + float tex_h = font.getAtlasHeight(); // Model-view transform: // - scale bounding boxes to relative clip-space/NDC coords // - add z-offset of -0.005 to reduce text hiding by mesh @@ -298,8 +298,8 @@ void FFGLDevice::drawDeviceBuffer(const TextBuffer& buf) char prev_c = '\0'; for (char c : e.text) { - const GlVisFont::glyph &g = GetFont()->GetTexChar(c); - pen_x += GetFont()->GetKerning(prev_c, c); + const GlVisFont::glyph &g = font.GetTexChar(c); + pen_x += font.GetKerning(prev_c, c); // note: subtract 1 to account for the padding in the texture glyphs float cur_x = pen_x + g.bear_x - 1; float cur_y = -pen_y - g.bear_y - 1; diff --git a/lib/gl/renderer_ff.hpp b/lib/gl/renderer_ff.hpp index 565ba517..74d6f786 100644 --- a/lib/gl/renderer_ff.hpp +++ b/lib/gl/renderer_ff.hpp @@ -54,9 +54,9 @@ class FFGLDevice : public GLDevice void bufferToDevice(array_layout layout, IVertexBuffer& buf) override; void bufferToDevice(array_layout layout, IIndexedBuffer& buf) override; - void bufferToDevice(TextBuffer& t_buf) override; + void bufferToDevice(GlVisFont& font, TextBuffer& t_buf) override; void drawDeviceBuffer(int hnd) override; - void drawDeviceBuffer(const TextBuffer& t_buf) override; + void drawDeviceBuffer(GlVisFont& font, const TextBuffer& t_buf) override; void captureXfbBuffer(PaletteState& pal, CaptureBuffer& cbuf, int hnd) override; }; diff --git a/lib/openglvis.hpp b/lib/openglvis.hpp index 3949a88d..82754967 100644 --- a/lib/openglvis.hpp +++ b/lib/openglvis.hpp @@ -14,6 +14,7 @@ #include #include "sdl.hpp" +#include "font.hpp" #include "gl/types.hpp" #include "material.hpp" #include "palettes.hpp" @@ -87,6 +88,7 @@ class VisualizationScene int light_mat_idx; bool use_light; + GlVisFont* font = nullptr; gl3::RenderParams GetMeshDrawParams(); glm::mat4 GetModelViewMtx(); @@ -184,6 +186,8 @@ class VisualizationScene } } + void SetFont(GlVisFont* fnt) { this->font = fnt; }; + void GenerateAlphaTexture() { palette.GenerateAlphaTexture(matAlpha, matAlphaCenter); } diff --git a/lib/sdl.cpp b/lib/sdl.cpp index 63b31533..1dbf5bfe 100644 --- a/lib/sdl.cpp +++ b/lib/sdl.cpp @@ -205,7 +205,7 @@ void SdlWindow::probeGLContextSupport(bool legacyGlOnly) #endif } -bool SdlWindow::createWindow(const char * title, int x, int y, int w, int h, +bool SdlWindow::createWindow(const std::string& title, int x, int y, int w, int h, bool legacyGlOnly) { if (!SDL_WasInit(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) diff --git a/lib/sdl.hpp b/lib/sdl.hpp index 88e16314..3c42cae1 100644 --- a/lib/sdl.hpp +++ b/lib/sdl.hpp @@ -109,7 +109,7 @@ class SdlWindow /// Creates a new OpenGL window. Returns false if SDL or OpenGL initialization /// fails. - bool createWindow(const char * title, int x, int y, int w, int h, + bool createWindow(const std::string& title, int x, int y, int w, int h, bool legacyGlOnly); /// Runs the window loop. void mainLoop(); diff --git a/lib/vsdata.cpp b/lib/vsdata.cpp index d1680d7f..e2269621 100644 --- a/lib/vsdata.cpp +++ b/lib/vsdata.cpp @@ -460,7 +460,7 @@ void VisualizationSceneScalarData::PrepareCaption() caption_buf.clear(); caption_buf.addText(0, 0, 0, caption); updated_bufs.emplace_back(&caption_buf); - GetFont()->getObjectSize(caption, caption_w, caption_h); + font->getObjectSize(caption, caption_w, caption_h); } VisualizationSceneScalarData * vsdata; @@ -996,7 +996,7 @@ gl3::SceneInfo VisualizationSceneScalarData::GetSceneObjs() wnd->getGLDrawSize(gl_w, gl_h); // add caption to draw list double v_pos = 2.; - double line_h = GetFont()->getFontLineSpacing(); + double line_h = font->getFontLineSpacing(); params.model_view.translate(-(double)caption_w / gl_w, 1.0 - 2 * v_pos * line_h / gl_h, 0.0); scene.queue.emplace_back(params, &caption_buf); @@ -1313,7 +1313,7 @@ void VisualizationSceneScalarData::PrepareAxes() if (drawaxes == 1) { - int desc = GetFont()->getFontDescender(); + int desc = font->getFontDescender(); int ox = -desc/2; int oy = -3*desc/2; ostringstream buf; From 403282fbf56bb0f63d73f3aa3b585055a265bb44 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Wed, 27 Jan 2021 15:30:44 -0800 Subject: [PATCH 04/39] Moving more methods to GLVisWindow class --- glvis.cpp | 2 +- lib/aux_vis.cpp | 42 +++++++++++++++++++++++++++++++++--------- lib/aux_vis.hpp | 29 ++++++++++++++++++++++++++--- lib/sdl.hpp | 2 +- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index f11c580c..161ffe83 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -268,7 +268,7 @@ bool GLVisInitVis(int field_type) void GLVisStartVis() { - RunVisualization(); // deletes vs + mainWindow->RunVisualization(); // deletes vs vs = NULL; if (input_streams.Size() > 0) { diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 8dc45964..40203f76 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -47,12 +47,18 @@ float line_w = 1.f; float line_w_aa = gl3::LINE_WIDTH_AA; [[deprecated]] SdlWindow* wnd; +[[deprecated]] GLVisWindow * glvis_wnd = nullptr; SdlWindow * GetAppWindow() { return wnd; } +GLVisWindow * GetGlvisWindow() +{ + return glvis_wnd; +} + VisualizationScene * GetVisualizationScene() { return locscene; @@ -68,6 +74,7 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega #ifdef GLVIS_DEBUG cout << "OpenGL Visualization" << endl; #endif + ::glvis_wnd = this; wnd.reset(new SdlWindow()); if (!wnd->createWindow(name, x, y, w, h, legacyGlOnly)) { @@ -84,8 +91,7 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega // auxReshapeFunc (MyReshape); // not needed, MyExpose calls it // auxReshapeFunc (NULL); - void (*exposeFunc)(void) = MyExpose; - wnd->setOnExpose(exposeFunc); + wnd->setOnExpose([this]() { MyExpose(); }); wnd->setOnMouseDown(SDL_BUTTON_LEFT, LeftButtonDown); wnd->setOnMouseUp(SDL_BUTTON_LEFT, LeftButtonUp); @@ -167,6 +173,16 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega } void SendKeySequence(const char *seq) +{ + glvis_wnd->SendKeySequence(seq); +} + +void CallKeySequence(const char *seq) +{ + glvis_wnd->CallKeySequence(seq); +} + +void GLVisWindow::SendKeySequence(const char *seq) { for (const char* key = seq; *key != '\0'; key++) { @@ -219,9 +235,7 @@ void SendKeySequence(const char *seq) } -static bool disableSendExposeEvent = false; - -void CallKeySequence(const char *seq) +void GLVisWindow::CallKeySequence(const char *seq) { const char *key = seq; @@ -304,7 +318,7 @@ void GLVisWindow::SetVisualizationScene(VisualizationScene * scene, int view, wnd->getRenderer().setPalette(&locscene->palette); } -void RunVisualization() +void GLVisWindow::RunVisualization() { #ifndef __EMSCRIPTEN__ wnd->mainLoop(); @@ -316,12 +330,22 @@ void RunVisualization() } void SendExposeEvent() +{ + glvis_wnd->SendExposeEvent(); +} + +void MyExpose() +{ + glvis_wnd->MyExpose(); +} + +void GLVisWindow::SendExposeEvent() { if (disableSendExposeEvent) { return; } wnd->signalExpose(); } -void MyReshape(GLsizei w, GLsizei h) +void GLVisWindow::MyReshape(GLsizei w, GLsizei h) { wnd->getRenderer().setViewport(w, h); @@ -358,7 +382,7 @@ void MyReshape(GLsizei w, GLsizei h) locscene->SetProjectionMtx(projmtx.mtx); } -void MyExpose(GLsizei w, GLsizei h) +void GLVisWindow::MyExpose(GLsizei w, GLsizei h) { MyReshape (w, h); GLuint color_tex = locscene->palette.GetColorTexture(); @@ -377,7 +401,7 @@ void MyExpose(GLsizei w, GLsizei h) wnd->getRenderer().render(frame.queue); } -void MyExpose() +void GLVisWindow::MyExpose() { int w, h; wnd->getGLDrawSize(w, h); diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 4a258ee2..680b1b04 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -33,21 +33,43 @@ class GLVisWindow SdlWindow* getSdl() { return wnd.get(); } + /// Start the infinite visualization loop. + void RunVisualization(); + + /// Send expose event. In our case MyReshape is executed and Draw after it. + void SendExposeEvent(); + + void MyExpose(); + + /// Send a sequence of keystrokes to the visualization window + void SendKeySequence(const char *seq); + + // Directly call the functions assigned to the given keys. Unlike the above + // function, SendKeySequence(), this function does not send X events and + // actually disables the function SendExposeEvent() used by many of the + // functions assigned to keys. Call MyExpose() after calling this function to + // update the visualization window. + void CallKeySequence(const char *seq); private: void InitFont(); bool SetFont(const vector& patterns, int height); + + void MyReshape(GLsizei w, GLsizei h); + void MyExpose(GLsizei w, GLsizei h); std::unique_ptr wnd; VisualizationScene* locscene; + std::vector> idle_funcs; + int visualize; + + bool disableSendExposeEvent = false; + GlVisFont font; std::string priority_font; int font_size = 12; }; -/// Start the infinite visualization loop. -void RunVisualization(); - /// Send expose event. In our case MyReshape is executed and Draw after it. void SendExposeEvent(); @@ -56,6 +78,7 @@ void MyExpose(); void MainLoop(); [[deprecated]] SdlWindow * GetAppWindow(); +[[deprecated]] GLVisWindow * GetGLVisWindow(); VisualizationScene * GetVisualizationScene(); void AddIdleFunc(void (*Func)(void)); diff --git a/lib/sdl.hpp b/lib/sdl.hpp index 3c42cae1..59981b90 100644 --- a/lib/sdl.hpp +++ b/lib/sdl.hpp @@ -31,7 +31,7 @@ typedef void (*TouchDelegate)(SDL_MultiGestureEvent&); typedef void (*MouseDelegate)(EventInfo*); typedef std::function KeyDelegate; typedef void (*WindowDelegate)(int, int); -typedef void (*Delegate)(); +typedef std::function Delegate; class SdlWindow { From a392026822f812248c28d16296c24525f5a3da90 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Wed, 27 Jan 2021 15:58:45 -0800 Subject: [PATCH 05/39] Move idle func handling into GLVisWindow --- glvis.cpp | 6 ++--- lib/aux_vis.cpp | 57 ++++++++++++++++++---------------------------- lib/aux_vis.hpp | 19 ++++++++++++---- lib/vsdata.cpp | 4 ++-- lib/vsvector3d.cpp | 4 ++-- 5 files changed, 43 insertions(+), 47 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 161ffe83..a0932bf8 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -772,7 +772,7 @@ void ExecuteScriptCommand() void ScriptControl(); -void ScriptIdleFunc() +void ScriptIdleFunc(GLVisWindow* wnd) { ExecuteScriptCommand(); if (scr_level == 0) @@ -786,12 +786,12 @@ void ScriptControl() if (scr_running) { scr_running = 0; - RemoveIdleFunc(ScriptIdleFunc); + mainWindow->RemoveIdleFunc(ScriptIdleFunc); } else { scr_running = 1; - AddIdleFunc(ScriptIdleFunc); + mainWindow->AddIdleFunc(ScriptIdleFunc); } } diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 40203f76..d3972b4a 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -54,7 +54,7 @@ SdlWindow * GetAppWindow() return wnd; } -GLVisWindow * GetGlvisWindow() +GLVisWindow * GetGLVisWindow() { return glvis_wnd; } @@ -69,6 +69,7 @@ void MyExpose(); GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool legacyGlOnly) : locscene(nullptr) + , idle_funcs(0) { #ifdef GLVIS_DEBUG @@ -287,8 +288,6 @@ void GLVisWindow::CallKeySequence(const char *seq) disableSendExposeEvent = false; } -void InitIdleFuncs(); - void GLVisWindow::SetVisualizationScene(VisualizationScene * scene, int view, const char *keys) { @@ -304,7 +303,6 @@ void GLVisWindow::SetVisualizationScene(VisualizationScene * scene, int view, scene -> CenterObject(); } - InitIdleFuncs(); if (scene -> spinning) { AddIdleFunc(MainLoop); @@ -323,7 +321,6 @@ void GLVisWindow::RunVisualization() #ifndef __EMSCRIPTEN__ wnd->mainLoop(); #endif - InitIdleFuncs(); delete locscene; //delete wnd; wnd = nullptr; @@ -409,38 +406,27 @@ void GLVisWindow::MyExpose() wnd->signalSwap(); } - -Array IdleFuncs; -int LastIdleFunc; - -void InitIdleFuncs() -{ - IdleFuncs.SetSize(0); - LastIdleFunc = 0; - wnd->setOnIdle(NULL); -} - -void MainIdleFunc() +void GLVisWindow::MainIdleFunc() { - LastIdleFunc = (LastIdleFunc + 1) % IdleFuncs.Size(); - if (IdleFuncs[LastIdleFunc]) + last_idle_func = (last_idle_func + 1) % idle_funcs.Size(); + if (idle_funcs[last_idle_func]) { - (*IdleFuncs[LastIdleFunc])(); + (*idle_funcs[last_idle_func])(this); } } -void AddIdleFunc(void (*Func)(void)) +void GLVisWindow::AddIdleFunc(GLVisWindow::IdleFPtr Func) { - IdleFuncs.Union(Func); - wnd->setOnIdle(MainIdleFunc); + idle_funcs.Union(Func); + wnd->setOnIdle([this](){MainIdleFunc();}); } -void RemoveIdleFunc(void (*Func)(void)) +void GLVisWindow::RemoveIdleFunc(GLVisWindow::IdleFPtr Func) { - IdleFuncs.DeleteFirst(Func); - if (IdleFuncs.Size() == 0) + idle_funcs.DeleteFirst(Func); + if (idle_funcs.Size() == 0) { - wnd->setOnIdle(NULL); + wnd->setOnIdle(nullptr); } } @@ -453,8 +439,9 @@ static GLint oldx, oldy, startx, starty; int constrained_spinning = 0; -void MainLoop() +void MainLoop(GLVisWindow* wnd) { + VisualizationScene* locscene = wnd->getScene(); static int p = 1; struct timespec req; if (locscene->spinning) @@ -477,7 +464,7 @@ void MainLoop() { char fname[20]; snprintf(fname, 20, "GLVis_m%04d", p++); - wnd->screenshot(fname); + wnd->getSdl()->screenshot(fname); } } @@ -514,7 +501,7 @@ inline void ComputeSphereAngles(int &newx, int &newy, void LeftButtonDown (EventInfo *event) { locscene -> spinning = 0; - RemoveIdleFunc(MainLoop); + glvis_wnd->RemoveIdleFunc(MainLoop); oldx = event->mouse_x; oldy = event->mouse_y; @@ -598,7 +585,7 @@ void LeftButtonUp (EventInfo *event) if ( (event->keymod & KMOD_SHIFT) && (xang != 0.0 || yang != 0.0) ) { locscene -> spinning = 1; - AddIdleFunc(MainLoop); + glvis_wnd->AddIdleFunc(MainLoop); if (xang > 20) { xang = 20; } if (xang < -20) { xang = -20; } if (yang > 20) { yang = 20; } if (yang < -20) { yang = -20; } @@ -1214,12 +1201,12 @@ void CheckSpin() if (xang != 0. || yang != 0.) { locscene->spinning = 1; - AddIdleFunc(MainLoop); + glvis_wnd->AddIdleFunc(MainLoop); } else { locscene->spinning = 0; - RemoveIdleFunc(MainLoop); + glvis_wnd->RemoveIdleFunc(MainLoop); } cout << "Spin angle: " << xang << " degrees / frame" << endl; } @@ -1242,14 +1229,14 @@ void KeyDeletePressed() { xang = yang = 0.; locscene -> spinning = 0; - RemoveIdleFunc(MainLoop); + glvis_wnd->RemoveIdleFunc(MainLoop); constrained_spinning = 1; } else { xang = xang_step; locscene -> spinning = 1; - AddIdleFunc(MainLoop); + glvis_wnd->AddIdleFunc(MainLoop); constrained_spinning = 1; } } diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 680b1b04..472c4936 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -12,6 +12,8 @@ #ifndef GLVIS_AUX_VIS_HPP #define GLVIS_AUX_VIS_HPP +#include "mfem/mfem.hpp" + #include "gl/platform_gl.hpp" #include "gl/types.hpp" @@ -22,6 +24,8 @@ class GLVisWindow { public: + using IdleFPtr = void(*)(GLVisWindow* wnd); + /// Initializes the visualization and some keys. GLVisWindow(std::string name, int x, int y, int w, int h, bool legacyGlOnly); @@ -29,6 +33,8 @@ class GLVisWindow int view = 3, const char *keys = NULL); void SetFont(const std::string& fn); + VisualizationScene* getScene() { return locscene; } + GlVisFont* getFont() { return &font; } SdlWindow* getSdl() { return wnd.get(); } @@ -50,16 +56,22 @@ class GLVisWindow // functions assigned to keys. Call MyExpose() after calling this function to // update the visualization window. void CallKeySequence(const char *seq); + + void AddIdleFunc(IdleFPtr func); + void RemoveIdleFunc(IdleFPtr func); private: void InitFont(); bool SetFont(const vector& patterns, int height); + void MainIdleFunc(); + void MyReshape(GLsizei w, GLsizei h); void MyExpose(GLsizei w, GLsizei h); std::unique_ptr wnd; VisualizationScene* locscene; - std::vector> idle_funcs; + mfem::Array idle_funcs{}; + int last_idle_func = 0; int visualize; @@ -75,15 +87,12 @@ void SendExposeEvent(); void MyExpose(); -void MainLoop(); +void MainLoop(GLVisWindow* wnd); [[deprecated]] SdlWindow * GetAppWindow(); [[deprecated]] GLVisWindow * GetGLVisWindow(); VisualizationScene * GetVisualizationScene(); -void AddIdleFunc(void (*Func)(void)); -void RemoveIdleFunc(void (*Func)(void)); - void LeftButtonDown (EventInfo *event); void LeftButtonLoc (EventInfo *event); void LeftButtonUp (EventInfo *event); diff --git a/lib/vsdata.cpp b/lib/vsdata.cpp index e2269621..5affe56e 100644 --- a/lib/vsdata.cpp +++ b/lib/vsdata.cpp @@ -531,7 +531,7 @@ void KeyLPressed() void KeyrPressed() { locscene -> spinning = 0; - RemoveIdleFunc(MainLoop); + GetGLVisWindow()->RemoveIdleFunc(MainLoop); vsdata -> CenterObject(); locscene -> ViewAngle = 45.0; @@ -546,7 +546,7 @@ void KeyrPressed() void KeyRPressed() { locscene->spinning = 0; - RemoveIdleFunc(MainLoop); + GetGLVisWindow()->RemoveIdleFunc(MainLoop); vsdata->Toggle2DView(); SendExposeEvent(); } diff --git a/lib/vsvector3d.cpp b/lib/vsvector3d.cpp index 0820537a..d3b223b7 100644 --- a/lib/vsvector3d.cpp +++ b/lib/vsvector3d.cpp @@ -137,7 +137,7 @@ static void KeyBPressed() static void KeyrPressed() { locscene -> spinning = 0; - RemoveIdleFunc(MainLoop); + GetGLVisWindow()->RemoveIdleFunc(MainLoop); vsvector3d -> CenterObject(); locscene -> ViewAngle = 45.0; locscene -> ViewScale = 1.0; @@ -154,7 +154,7 @@ static void KeyrPressed() static void KeyRPressed() { locscene->spinning = 0; - RemoveIdleFunc(MainLoop); + GetGLVisWindow()->RemoveIdleFunc(MainLoop); vsvector3d -> ianim = vsvector3d -> ianimd = 0; vsvector3d -> Prepare(); vsvector3d -> PrepareLines(); From d40b414f1dc8726c59f7757b3afd642ef1302427 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 28 Jan 2021 11:39:33 -0800 Subject: [PATCH 06/39] Add mouse events, model spinning stuff to GLVisWindow --- lib/aux_vis.cpp | 212 ++++++++++++++++++++++++++++++------------------ lib/aux_vis.hpp | 21 ++--- lib/sdl.hpp | 4 +- 3 files changed, 141 insertions(+), 96 deletions(-) diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index d3972b4a..d4987c1d 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -67,14 +67,54 @@ VisualizationScene * GetVisualizationScene() void MyExpose(GLsizei w, GLsizei h); void MyExpose(); +struct GLVisWindow::RotationControl +{ + GLVisWindow* wnd; + double xang = 0., yang = 0.; + gl3::GlMatrix srot; + double sph_t, sph_u; + GLint oldx, oldy, startx, starty; + + bool constrained_spinning = 0; + + void LeftButtonDown (EventInfo *event); + void LeftButtonLoc (EventInfo *event); + void LeftButtonUp (EventInfo *event); + void MiddleButtonDown(EventInfo *event); + void MiddleButtonLoc (EventInfo *event); + void MiddleButtonUp (EventInfo *event); + void RightButtonDown (EventInfo *event); + void RightButtonLoc (EventInfo *event); + void RightButtonUp (EventInfo *event); + + void CheckSpin(); + void Key0Pressed(); + void KeyDeletePressed(); + void KeyEnterPressed(); + MouseDelegate CreateMouseEvent(void (GLVisWindow::RotationControl::*func)(EventInfo*)) + { + return [this, func](EventInfo* ei) { (this->*func)(ei); }; + } + +}; + +template +KeyDelegate CreateKeyEvent(T* inst, void (T::*func)()) +{ + return [inst, func](GLenum) { (inst->*func)(); }; +} + + GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool legacyGlOnly) : locscene(nullptr) , idle_funcs(0) + , rot_data(new RotationControl) { #ifdef GLVIS_DEBUG cout << "OpenGL Visualization" << endl; #endif + rot_data->wnd = this; ::glvis_wnd = this; wnd.reset(new SdlWindow()); if (!wnd->createWindow(name, x, y, w, h, legacyGlOnly)) @@ -93,6 +133,19 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega // auxReshapeFunc (MyReshape); // not needed, MyExpose calls it // auxReshapeFunc (NULL); wnd->setOnExpose([this]() { MyExpose(); }); + auto LeftButtonDown = rot_data->CreateMouseEvent(&RotationControl::LeftButtonDown); + auto LeftButtonUp = rot_data->CreateMouseEvent(&RotationControl::LeftButtonUp); + auto LeftButtonLoc = rot_data->CreateMouseEvent(&RotationControl::LeftButtonLoc); + auto MiddleButtonDown = rot_data->CreateMouseEvent(&RotationControl::MiddleButtonDown); + auto MiddleButtonUp = rot_data->CreateMouseEvent(&RotationControl::MiddleButtonUp); + auto MiddleButtonLoc = rot_data->CreateMouseEvent(&RotationControl::MiddleButtonLoc); + auto RightButtonDown = rot_data->CreateMouseEvent(&RotationControl::RightButtonDown); + auto RightButtonUp = rot_data->CreateMouseEvent(&RotationControl::RightButtonUp); + auto RightButtonLoc = rot_data->CreateMouseEvent(&RotationControl::RightButtonLoc); + + auto Key0Pressed = CreateKeyEvent(rot_data.get(), &RotationControl::Key0Pressed); + auto KeyEnterPressed = CreateKeyEvent(rot_data.get(), &RotationControl::KeyEnterPressed); + auto KeyDeletePressed = CreateKeyEvent(rot_data.get(), &RotationControl::KeyDeletePressed); wnd->setOnMouseDown(SDL_BUTTON_LEFT, LeftButtonDown); wnd->setOnMouseUp(SDL_BUTTON_LEFT, LeftButtonUp); @@ -305,7 +358,7 @@ void GLVisWindow::SetVisualizationScene(VisualizationScene * scene, int view, if (scene -> spinning) { - AddIdleFunc(MainLoop); + AddIdleFunc(::MainLoop); } if (keys) @@ -430,23 +483,20 @@ void GLVisWindow::RemoveIdleFunc(GLVisWindow::IdleFPtr Func) } } - -double xang = 0., yang = 0.; -gl3::GlMatrix srot; -double sph_t, sph_u; -static GLint oldx, oldy, startx, starty; - -int constrained_spinning = 0; - - void MainLoop(GLVisWindow* wnd) { - VisualizationScene* locscene = wnd->getScene(); + wnd->MainLoop(); +} + +void GLVisWindow::MainLoop() +{ static int p = 1; struct timespec req; + double xang = rot_data->xang; + double yang = rot_data->yang; if (locscene->spinning) { - if (!constrained_spinning) + if (!rot_data->constrained_spinning) { locscene->Rotate(xang, yang); SendExposeEvent(); @@ -464,7 +514,7 @@ void MainLoop(GLVisWindow* wnd) { char fname[20]; snprintf(fname, 20, "GLVis_m%04d", p++); - wnd->getSdl()->screenshot(fname); + wnd->screenshot(fname); } } @@ -475,14 +525,14 @@ inline double sqr(double t) return t*t; } -inline void ComputeSphereAngles(int &newx, int &newy, +inline void ComputeSphereAngles(int viewport_w, int viewport_h, + int &newx, int &newy, double &new_sph_u, double &new_sph_t) { - GLint viewport[4] = { 0, 0, 0, 0 }; + GLint viewport[4] = { 0, 0, viewport_w, viewport_h }; double r, x, y, rr; const double maxr = 0.996194698091745532295; - wnd->getGLDrawSize(viewport[2], viewport[3]); r = sqrt(sqr(viewport[2])+sqr(viewport[3]))*M_SQRT1_2; x = double(newx-viewport[0]-viewport[2]/2) / r; @@ -498,41 +548,46 @@ inline void ComputeSphereAngles(int &newx, int &newy, new_sph_t = atan2(y, x); } -void LeftButtonDown (EventInfo *event) +void GLVisWindow::RotationControl::LeftButtonDown (EventInfo *event) { - locscene -> spinning = 0; - glvis_wnd->RemoveIdleFunc(MainLoop); + wnd->locscene -> spinning = 0; + wnd->RemoveIdleFunc(::MainLoop); oldx = event->mouse_x; oldy = event->mouse_y; - ComputeSphereAngles(oldx, oldy, sph_u, sph_t); + int vp_w, vp_h; + wnd->wnd->getGLDrawSize(vp_w, vp_h); + + ComputeSphereAngles(vp_w, vp_h, oldx, oldy, sph_u, sph_t); srot.identity(); - srot.mult(locscene->cam.RotMatrix()); - srot.mult(locscene->rotmat); + srot.mult(wnd->locscene->cam.RotMatrix()); + srot.mult(wnd->locscene->rotmat); startx = oldx; starty = oldy; } -void LeftButtonLoc (EventInfo *event) +void GLVisWindow::RotationControl::LeftButtonLoc (EventInfo *event) { GLint newx = event->mouse_x; GLint newy = event->mouse_y; - int sendexpose = 1; if (event->keymod & KMOD_CTRL) { if (event->keymod & KMOD_SHIFT) { - locscene->PreRotate(double(newx-oldx)/2, 0.0, 0.0, 1.0); + wnd->locscene->PreRotate(double(newx-oldx)/2, 0.0, 0.0, 1.0); } else { double new_sph_u, new_sph_t; - ComputeSphereAngles(newx, newy, new_sph_u, new_sph_t); + int vp_w, vp_h; + wnd->wnd->getGLDrawSize(vp_w, vp_h); + + ComputeSphereAngles(vp_w, vp_h, newx, newy, new_sph_u, new_sph_t); gl3::GlMatrix newrot; newrot.identity(); @@ -545,36 +600,33 @@ void LeftButtonLoc (EventInfo *event) inprod = InnerProd(scoord, ncoord); CrossProd(scoord, ncoord, cross); - newrot.mult(locscene->cam.TransposeRotMatrix()); + newrot.mult(wnd->locscene->cam.TransposeRotMatrix()); newrot.rotate(acos(inprod)*(180.0/M_PI), cross[0], cross[1], cross[2]); newrot.mult(srot.mtx); - locscene->rotmat = newrot.mtx; + wnd->locscene->rotmat = newrot.mtx; } } else if (event->keymod & KMOD_ALT) { - locscene->Rotate(double(newx-oldx)/2, 0.0, 0.0, 1.0); + wnd->locscene->Rotate(double(newx-oldx)/2, 0.0, 0.0, 1.0); } else if (event->keymod & KMOD_SHIFT) { - locscene->Rotate(double(newx-oldx)/2, double(newy-oldy)/2); + wnd->locscene->Rotate(double(newx-oldx)/2, double(newy-oldy)/2); } else { - locscene->Rotate(double(newy-oldy)/2, 1.0, 0.0, 0.0); - locscene->PreRotate(double(newx-oldx)/2, 0.0, 0.0, 1.0); + wnd->locscene->Rotate(double(newy-oldy)/2, 1.0, 0.0, 0.0); + wnd->locscene->PreRotate(double(newx-oldx)/2, 0.0, 0.0, 1.0); } oldx = newx; oldy = newy; - if (sendexpose) - { - SendExposeEvent(); - } + wnd->SendExposeEvent(); } -void LeftButtonUp (EventInfo *event) +void GLVisWindow::RotationControl::LeftButtonUp (EventInfo *event) { GLint newx = event->mouse_x; GLint newy = event->mouse_y; @@ -584,8 +636,8 @@ void LeftButtonUp (EventInfo *event) if ( (event->keymod & KMOD_SHIFT) && (xang != 0.0 || yang != 0.0) ) { - locscene -> spinning = 1; - glvis_wnd->AddIdleFunc(MainLoop); + wnd->locscene -> spinning = 1; + wnd->AddIdleFunc(::MainLoop); if (xang > 20) { xang = 20; } if (xang < -20) { xang = -20; } if (yang > 20) { yang = 20; } if (yang < -20) { yang = -20; } @@ -600,13 +652,13 @@ void LeftButtonUp (EventInfo *event) } } -void MiddleButtonDown (EventInfo *event) +void GLVisWindow::RotationControl::MiddleButtonDown (EventInfo *event) { startx = oldx = event->mouse_x; starty = oldy = event->mouse_y; } -void MiddleButtonLoc (EventInfo *event) +void GLVisWindow::RotationControl::MiddleButtonLoc (EventInfo *event) { GLint newx = event->mouse_x; GLint newy = event->mouse_y; @@ -616,15 +668,15 @@ void MiddleButtonLoc (EventInfo *event) int w, h; double TrX, TrY, scale; - if (locscene->OrthogonalProjection) + if (wnd->locscene->OrthogonalProjection) { - scale = locscene->ViewScale; + scale = wnd->locscene->ViewScale; } else { - scale = 0.4142135623730950488/tan(locscene->ViewAngle*(M_PI/360)); + scale = 0.4142135623730950488/tan(wnd->locscene->ViewAngle*(M_PI/360)); } - wnd->getGLDrawSize(w, h); + wnd->wnd->getGLDrawSize(w, h); if (w < h) { scale *= w; @@ -635,12 +687,12 @@ void MiddleButtonLoc (EventInfo *event) } TrX = 2.0*double(oldx-newx)/scale; TrY = 2.0*double(newy-oldy)/scale; - locscene->ViewCenterX += TrX; - locscene->ViewCenterY += TrY; + wnd->locscene->ViewCenterX += TrX; + wnd->locscene->ViewCenterY += TrY; } else { - // locscene->Translate((double)(newx-oldx)/200,(double)(newy-oldy)/200); + // wnd->locscene->Translate((double)(newx-oldx)/200,(double)(newy-oldy)/200); double dx = double(newx-oldx)/400; double dy = double(oldy-newy)/400; @@ -650,40 +702,40 @@ void MiddleButtonLoc (EventInfo *event) double sx = double(newx-startx)/400; double sy = double(starty-newy)/400; - locscene->cam.TurnLeftRight(dx-sx); - locscene->cam.TurnUpDown(sy-dy); + wnd->locscene->cam.TurnLeftRight(dx-sx); + wnd->locscene->cam.TurnUpDown(sy-dy); - locscene->cam.TurnUpDown(-sy); - locscene->cam.TurnLeftRight(sx); + wnd->locscene->cam.TurnUpDown(-sy); + wnd->locscene->cam.TurnLeftRight(sx); } else if (event->keymod & KMOD_ALT) // ctrl + alt { - locscene->cam.MoveForwardBackward(dy); - locscene->cam.TiltLeftRight(-dx); + wnd->locscene->cam.MoveForwardBackward(dy); + wnd->locscene->cam.TiltLeftRight(-dx); } else // ctrl { - locscene->cam.MoveLeftRight(dx); - locscene->cam.MoveUpDown(-dy); + wnd->locscene->cam.MoveLeftRight(dx); + wnd->locscene->cam.MoveUpDown(-dy); } } - SendExposeEvent(); + wnd->SendExposeEvent(); oldx = newx; oldy = newy; } -void MiddleButtonUp (EventInfo*) +void GLVisWindow::RotationControl::MiddleButtonUp (EventInfo*) {} -void RightButtonDown (EventInfo *event) +void GLVisWindow::RotationControl::RightButtonDown (EventInfo *event) { startx = oldx = event->mouse_x; starty = oldy = event->mouse_y; } -void RightButtonLoc (EventInfo *event) +void GLVisWindow::RotationControl::RightButtonLoc (EventInfo *event) { GLint newx = event->mouse_x; GLint newy = event->mouse_y; @@ -714,24 +766,24 @@ void RightButtonLoc (EventInfo *event) x = 0.; y = 0.; z = -1.; } cout << "(x,y,z) = (" << x << ',' << y << ',' << z << ')' << endl; - locscene->SetLight0CustomPos({(float)x, (float)y, (float)z, 0.f}); + wnd->locscene->SetLight0CustomPos({(float)x, (float)y, (float)z, 0.f}); } else if ( !( event->keymod & KMOD_CTRL ) ) { - locscene -> Zoom (exp ( double (oldy-newy) / 100 )); + wnd->locscene -> Zoom (exp ( double (oldy-newy) / 100 )); } else { - locscene -> Scale ( exp ( double (oldy-newy) / 50 ) ); + wnd->locscene -> Scale ( exp ( double (oldy-newy) / 50 ) ); } - SendExposeEvent(); + wnd->SendExposeEvent(); oldx = newx; oldy = newy; } -void RightButtonUp (EventInfo*) +void GLVisWindow::RotationControl::RightButtonUp (EventInfo*) {} void ToggleAntialiasing() @@ -1192,7 +1244,7 @@ void ThreadsRun() } } -void CheckSpin() +void GLVisWindow::RotationControl::CheckSpin() { if (fabs(xang) < 1.e-2) { @@ -1200,22 +1252,22 @@ void CheckSpin() } if (xang != 0. || yang != 0.) { - locscene->spinning = 1; - glvis_wnd->AddIdleFunc(MainLoop); + wnd->locscene->spinning = 1; + wnd->AddIdleFunc(::MainLoop); } else { - locscene->spinning = 0; - glvis_wnd->RemoveIdleFunc(MainLoop); + wnd->locscene->spinning = 0; + wnd->RemoveIdleFunc(::MainLoop); } cout << "Spin angle: " << xang << " degrees / frame" << endl; } const double xang_step = 0.2; // angle in degrees -void Key0Pressed() +void GLVisWindow::RotationControl::Key0Pressed() { - if (!locscene -> spinning) + if (!wnd->locscene -> spinning) { xang = 0; } @@ -1223,27 +1275,27 @@ void Key0Pressed() CheckSpin(); } -void KeyDeletePressed() +void GLVisWindow::RotationControl::KeyDeletePressed() { - if (locscene -> spinning) + if (wnd->locscene -> spinning) { xang = yang = 0.; - locscene -> spinning = 0; - glvis_wnd->RemoveIdleFunc(MainLoop); + wnd->locscene -> spinning = 0; + wnd->RemoveIdleFunc(::MainLoop); constrained_spinning = 1; } else { xang = xang_step; - locscene -> spinning = 1; - glvis_wnd->AddIdleFunc(MainLoop); + wnd->locscene -> spinning = 1; + wnd->AddIdleFunc(::MainLoop); constrained_spinning = 1; } } -void KeyEnterPressed() +void GLVisWindow::RotationControl::KeyEnterPressed() { - if (!locscene -> spinning) + if (!wnd->locscene -> spinning) { xang = 0; } diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 472c4936..0ce22de9 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -25,6 +25,7 @@ class GLVisWindow { public: using IdleFPtr = void(*)(GLVisWindow* wnd); + using KeyEvent = void(*)(GLVisWindow* wnd, int keystate); /// Initializes the visualization and some keys. GLVisWindow(std::string name, int x, int y, int w, int h, bool legacyGlOnly); @@ -59,6 +60,8 @@ class GLVisWindow void AddIdleFunc(IdleFPtr func); void RemoveIdleFunc(IdleFPtr func); + + void MainLoop(); private: void InitFont(); bool SetFont(const vector& patterns, int height); @@ -80,6 +83,10 @@ class GLVisWindow GlVisFont font; std::string priority_font; int font_size = 12; + + struct RotationControl; + std::unique_ptr rot_data; + }; /// Send expose event. In our case MyReshape is executed and Draw after it. @@ -93,16 +100,6 @@ void MainLoop(GLVisWindow* wnd); [[deprecated]] GLVisWindow * GetGLVisWindow(); VisualizationScene * GetVisualizationScene(); -void LeftButtonDown (EventInfo *event); -void LeftButtonLoc (EventInfo *event); -void LeftButtonUp (EventInfo *event); -void MiddleButtonDown(EventInfo *event); -void MiddleButtonLoc (EventInfo *event); -void MiddleButtonUp (EventInfo *event); -void RightButtonDown (EventInfo *event); -void RightButtonLoc (EventInfo *event); -void RightButtonUp (EventInfo *event); - void TouchPinch(SDL_MultiGestureEvent & e); void ToggleAntialiasing(); @@ -124,10 +121,6 @@ void Key7Pressed(); void Key8Pressed(); void Key9Pressed(); -void Key0Pressed(); -void KeyDeletePressed(); -void KeyEnterPressed(); - void KeyLeftPressed(GLenum); void KeyRightPressed(GLenum); void KeyUpPressed(GLenum); diff --git a/lib/sdl.hpp b/lib/sdl.hpp index 59981b90..d2a3b8b6 100644 --- a/lib/sdl.hpp +++ b/lib/sdl.hpp @@ -28,9 +28,9 @@ struct EventInfo }; typedef void (*TouchDelegate)(SDL_MultiGestureEvent&); -typedef void (*MouseDelegate)(EventInfo*); +typedef std::function MouseDelegate; typedef std::function KeyDelegate; -typedef void (*WindowDelegate)(int, int); +typedef std::function WindowDelegate; typedef std::function Delegate; class SdlWindow From 9ff3eeb8dea4a4c8f99bdd2f28d546e7810a455b Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 28 Jan 2021 12:19:55 -0800 Subject: [PATCH 07/39] Move MSAA, screenshot controls into GLVisWindow, add wrapper for member function events --- lib/aux_vis.cpp | 18 ++++++++++++------ lib/aux_vis.hpp | 9 ++++++--- lib/vsdata.cpp | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index d4987c1d..c3f1117f 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -158,11 +158,11 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega wnd->setOnMouseMove(SDL_BUTTON_RIGHT, RightButtonLoc); wnd->setTouchPinchCallback(TouchPinch); - wnd->setOnKeyDown('A', ToggleAntialiasing); + SetKeyEventHandler('A', &GLVisWindow::ToggleAntialiasing); // auxKeyFunc (AUX_p, KeyCtrlP); // handled in vsdata.cpp - wnd->setOnKeyDown (SDLK_s, KeyS); - wnd->setOnKeyDown ('S', KeyS); + SetKeyEventHandler (SDLK_s, &GLVisWindow::Screenshot); + SetKeyEventHandler ('S', &GLVisWindow::Screenshot); wnd->setOnKeyDown (SDLK_q, KeyQPressed); // wnd->setOnKeyDown (SDLK_Q, KeyQPressed); @@ -226,6 +226,12 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega #endif } +void GLVisWindow::SetKeyEventHandler(int key, void (GLVisWindow::*handler)()) +{ + auto handlerWrapper = [this, handler]() { (this->*handler)(); }; + wnd->setOnKeyDown(key, handlerWrapper); +} + void SendKeySequence(const char *seq) { glvis_wnd->SendKeySequence(seq); @@ -786,7 +792,7 @@ void GLVisWindow::RotationControl::RightButtonLoc (EventInfo *event) void GLVisWindow::RotationControl::RightButtonUp (EventInfo*) {} -void ToggleAntialiasing() +void GLVisWindow::ToggleAntialiasing() { bool curr_aa = wnd->getRenderer().getAntialiasing(); wnd->getRenderer().setAntialiasing(!curr_aa); @@ -1089,7 +1095,7 @@ int Screenshot(const char *fname, bool convert) #endif } -void KeyS() +void GLVisWindow::Screenshot() { static int p = 1; @@ -1159,7 +1165,7 @@ void PrintCaptureBuffer(gl3::CaptureBuffer& cbuf) } } -void KeyCtrlP() +void GLVisWindow::PrintToPDF() { #ifdef __EMSCRIPTEN__ cerr << "Printing in WebGL is not supported at this time." << endl; diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 0ce22de9..61c55869 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -62,10 +62,16 @@ class GLVisWindow void RemoveIdleFunc(IdleFPtr func); void MainLoop(); + + void ToggleAntialiasing(); + void Screenshot(); + void PrintToPDF(); private: void InitFont(); bool SetFont(const vector& patterns, int height); + void SetKeyEventHandler(int key, void (GLVisWindow::*handler)()); + void MainIdleFunc(); void MyReshape(GLsizei w, GLsizei h); @@ -102,9 +108,6 @@ VisualizationScene * GetVisualizationScene(); void TouchPinch(SDL_MultiGestureEvent & e); -void ToggleAntialiasing(); -void KeyCtrlP(); -void KeyS(); void KeyQPressed(); void ToggleThreads(); void ThreadsPauseFunc(GLenum); diff --git a/lib/vsdata.cpp b/lib/vsdata.cpp index 5affe56e..8b5f31c0 100644 --- a/lib/vsdata.cpp +++ b/lib/vsdata.cpp @@ -555,7 +555,7 @@ void KeypPressed(GLenum state) { if (state & KMOD_CTRL) { - KeyCtrlP(); + GetGLVisWindow()->PrintToPDF(); } else { From 1dfc47ab8455153715bc393e8fe0ec8064ce43f6 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 28 Jan 2021 13:40:33 -0800 Subject: [PATCH 08/39] Move screenshot implementation to new file --- glvis.cpp | 13 +- lib/CMakeLists.txt | 1 + lib/aux_vis.cpp | 299 ++-------------------------------------- lib/aux_vis.hpp | 5 +- lib/sdl.cpp | 2 +- lib/sdl.hpp | 2 + lib/sdl_screenshot.cpp | 305 +++++++++++++++++++++++++++++++++++++++++ lib/threads.cpp | 10 +- makefile | 5 +- 9 files changed, 333 insertions(+), 309 deletions(-) create mode 100644 lib/sdl_screenshot.cpp diff --git a/glvis.cpp b/glvis.cpp index a0932bf8..225dbe22 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -416,7 +416,7 @@ int ScriptReadDisplMesh(istream &scr, StreamState& state) return 0; } -void ExecuteScriptCommand() +void ExecuteScriptCommand(GLVisWindow* wnd) { if (!script) { @@ -504,12 +504,9 @@ void ExecuteScriptCommand() cout << "Script: screenshot: " << flush; - if (Screenshot(word.c_str(), true)) - { - cout << "Screenshot(" << word << ") failed." << endl; - done_one_command = 1; - continue; - } + + + wnd->Screenshot(word.c_str()); cout << "-> " << word << endl; if (scr_min_val > vs->GetMinV()) @@ -774,7 +771,7 @@ void ScriptControl(); void ScriptIdleFunc(GLVisWindow* wnd) { - ExecuteScriptCommand(); + ExecuteScriptCommand(wnd); if (scr_level == 0) { ScriptControl(); diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index a02c0b3e..461182b2 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -20,6 +20,7 @@ list(APPEND SOURCES openglvis.cpp palettes.cpp sdl.cpp + sdl_screenshot.cpp stream_reader.cpp vsdata.cpp vssolution.cpp diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index c3f1117f..c99e4137 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -811,291 +811,7 @@ void TouchPinch(SDL_MultiGestureEvent & e) SendExposeEvent(); } -#if defined(GLVIS_USE_LIBTIFF) -const char *glvis_screenshot_ext = ".tif"; -#elif defined(GLVIS_USE_LIBPNG) -const char *glvis_screenshot_ext = ".png"; -#else -const char *glvis_screenshot_ext = ".bmp"; -#endif - -// https://wiki.libsdl.org/SDL_CreateRGBSurfaceFrom -#if SDL_BYTEORDER == SDL_BIG_ENDIAN -Uint32 rmask = 0xff000000; -Uint32 gmask = 0x00ff0000; -Uint32 bmask = 0x0000ff00: - Uint32 amask = 0x000000ff; -#else // little endian, like x86 -Uint32 rmask = 0x000000ff; -Uint32 gmask = 0x0000ff00; -Uint32 bmask = 0x00ff0000; -Uint32 amask = 0xff000000; -#endif - -// https://halfgeek.org/wiki/Vertically_invert_a_surface_in_SDL -#define SDL_LOCKIFMUST(s) (SDL_MUSTLOCK(s) ? SDL_LockSurface(s) : 0) -#define SDL_UNLOCKIFMUST(s) { if(SDL_MUSTLOCK(s)) SDL_UnlockSurface(s); } - -int InvertSurfaceVertical(SDL_Surface *surface) -{ - Uint8 *t, *a, *b, *last; - Uint16 pitch; - - if ( SDL_LOCKIFMUST(surface) < 0 ) - { - return -2; - } - - /* do nothing unless at least two lines */ - if (surface->h < 2) - { - SDL_UNLOCKIFMUST(surface); - return 0; - } - - /* get a place to store a line */ - pitch = surface->pitch; - t = (Uint8*)malloc(pitch); - - if (t == NULL) - { - SDL_UNLOCKIFMUST(surface); - return -2; - } - - /* get first line; it's about to be trampled */ - memcpy(t,surface->pixels,pitch); - - /* now, shuffle the rest so it's almost correct */ - a = (Uint8*)surface->pixels; - last = a + pitch * (surface->h - 1); - b = last; - - while (a < b) - { - memcpy(a,b,pitch); - a += pitch; - memcpy(b,a,pitch); - b -= pitch; - } - - /* in this shuffled state, the bottom slice is too far down */ - memmove( b, b+pitch, last-b ); - - /* now we can put back that first row--in the last place */ - memcpy(last,t,pitch); - - /* everything is in the right place; close up. */ - free(t); - SDL_UNLOCKIFMUST(surface); - - return 0; -} - -int Screenshot(const char *fname, bool convert) -{ -#ifdef GLVIS_DEBUG - cout << "Screenshot: glFinish() ... " << flush; -#endif - glFinish(); -#ifdef GLVIS_DEBUG - cout << "done." << endl; -#endif -#ifndef __EMSCRIPTEN__ - if (wnd->isExposePending()) - { - MFEM_WARNING("Expose pending, some events may not have been handled." << endl); - } - string filename = fname; - string convert_name = fname; - bool call_convert = false; - if (convert) - { - // check the extension of 'fname' to see if convert is needed - size_t ext_size = strlen(glvis_screenshot_ext); - if (filename.size() < ext_size || - filename.compare(filename.size() - ext_size, - ext_size, glvis_screenshot_ext) != 0) - { - call_convert = true; - filename += glvis_screenshot_ext; - } - } - else // do not call convert - { - filename += glvis_screenshot_ext; - } - - int w, h; - wnd->getGLDrawSize(w, h); - if (wnd->isSwapPending()) - { -#if GLVIS_DEBUG - cerr << "Screenshot: reading image data from back buffer..." << endl; -#endif - glReadBuffer(GL_BACK); - } - else - { -#if GLVIS_DEBUG - cerr << "Screenshot: reading image data from front buffer..." << endl; -#endif - glReadBuffer(GL_FRONT); - } -#if defined(GLVIS_USE_LIBTIFF) - // Save a TIFF image. This requires the libtiff library, see www.libtiff.org - TIFF* image; - - // MyExpose(w,h); - - unsigned char *pixels = new unsigned char[3*w]; - if (!pixels) - { - return 1; - } - - image = TIFFOpen(filename.c_str(), "w"); - if (!image) - { - delete [] pixels; - return 2; - } - - TIFFSetField(image, TIFFTAG_IMAGEWIDTH, w); - TIFFSetField(image, TIFFTAG_IMAGELENGTH, h); - TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, 8); - TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS); - TIFFSetField(image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); - TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, 3); - TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, 1); - TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); - TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); - for (int i = 0; i < h; i++) - { - glReadPixels(0, h-i-1, w, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels); - if (TIFFWriteScanline(image, pixels, i, 0) < 0) - { - TIFFClose(image); - delete [] pixels; - return 3; - } - } - - TIFFFlushData(image); - TIFFClose(image); - delete [] pixels; - -#elif defined(GLVIS_USE_LIBPNG) - // Save as png image. Requires libpng. - - png_byte *pixels = new png_byte[3*w]; - if (!pixels) - { - return 1; - } - - png_structp png_ptr = - png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (!png_ptr) - { - delete [] pixels; - return 1; - } - png_infop info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) - { - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); - delete [] pixels; - return 1; - } - - FILE *fp = fopen(filename.c_str(), "wb"); - if (!fp) - { - png_destroy_write_struct(&png_ptr, &info_ptr); - delete [] pixels; - return 2; - } - - if (setjmp(png_jmpbuf(png_ptr))) - { - fclose(fp); - png_destroy_write_struct(&png_ptr, &info_ptr); - delete [] pixels; - return 3; - } - - png_uint_32 ppi = wnd->isHighDpi() ? 144 : 72; // pixels/inch - png_uint_32 ppm = ppi/0.0254 + 0.5; // pixels/meter - png_set_pHYs(png_ptr, info_ptr, ppm, ppm, PNG_RESOLUTION_METER); - - png_init_io(png_ptr, fp); - png_set_IHDR(png_ptr, info_ptr, w, h, 8, PNG_COLOR_TYPE_RGB, - PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, - PNG_FILTER_TYPE_DEFAULT); - - png_write_info(png_ptr, info_ptr); - for (int i = 0; i < h; i++) - { - glReadPixels(0, h-1-i, w, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels); - png_write_row(png_ptr, pixels); - } - png_write_end(png_ptr, info_ptr); - - fclose(fp); - png_destroy_write_struct(&png_ptr, &info_ptr); - delete [] pixels; - -#else - // use SDL for screenshots - - // https://stackoverflow.com/questions/20233469/how-do-i-take-and-save-a-bmp-screenshot-in-sdl-2 - unsigned char * pixels = new unsigned char[w*h*4]; // 4 bytes for RGBA - glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - - SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, rmask, - gmask, bmask, amask); - if (surf == nullptr) - { - std::cerr << "unable to take screenshot: " << SDL_GetError() << std::endl; - } - else - { - if (InvertSurfaceVertical(surf)) - { - std::cerr << "failed to invert surface, your screenshot may be upside down" << - std::endl; - } - SDL_SaveBMP(surf, filename.c_str()); - SDL_FreeSurface(surf); - // automatically convert to png if not being used - if (!call_convert) - { - call_convert = true; - convert_name += ".png"; - } - } - delete [] pixels; -#endif - - if (call_convert) - { - ostringstream cmd; - cmd << "convert " << filename << ' ' << convert_name; - if (system(cmd.str().c_str())) - { - return 1; - } - remove(filename.c_str()); - } - return 0; -#else - cout << "Screenshots not yet implemented for JS" << endl; - return 1; -#endif -} - -void GLVisWindow::Screenshot() +void GLVisWindow::Screenshot(std::string filename) { static int p = 1; @@ -1115,9 +831,16 @@ void GLVisWindow::Screenshot() else { cout << "Taking snapshot number " << p << "... "; - char fname[20]; - snprintf(fname, 20, "GLVis_s%02d", p++); - wnd->screenshot(fname); + if (filename == "") + { + char fname[20]; + snprintf(fname, 20, "GLVis_s%02d", p++); + wnd->screenshot(fname); + } + else + { + wnd->screenshot(filename); + } cout << "done" << endl; } } diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 61c55869..c23edbbe 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -64,7 +64,8 @@ class GLVisWindow void MainLoop(); void ToggleAntialiasing(); - void Screenshot(); + void Screenshot() { Screenshot(""); } + void Screenshot(std::string filename); void PrintToPDF(); private: void InitFont(); @@ -144,7 +145,7 @@ void ResizeWindow(int w, int h); void SetWindowTitle(const char *title); /// Take a screenshot using libtiff, libpng or sdl2 -int Screenshot(const char *fname, bool convert = false); +//int Screenshot(const char *fname, bool convert = false); /// Send a sequence of keystrokes to the visualization window void SendKeySequence(const char *seq); diff --git a/lib/sdl.cpp b/lib/sdl.cpp index 1dbf5bfe..e4177a61 100644 --- a/lib/sdl.cpp +++ b/lib/sdl.cpp @@ -741,7 +741,7 @@ void SdlWindow::mainLoop() } if (takeScreenshot) { - Screenshot(screenshot_file.c_str()); + screenshotHelper(); takeScreenshot = false; } } diff --git a/lib/sdl.hpp b/lib/sdl.hpp index d2a3b8b6..6a2db298 100644 --- a/lib/sdl.hpp +++ b/lib/sdl.hpp @@ -102,6 +102,8 @@ class SdlWindow void keyEvent(char c); void multiGestureEvent(SDL_MultiGestureEvent & e); + int screenshotHelper(bool convert = false); + std::string saved_keys; public: SdlWindow(); diff --git a/lib/sdl_screenshot.cpp b/lib/sdl_screenshot.cpp new file mode 100644 index 00000000..8a262fec --- /dev/null +++ b/lib/sdl_screenshot.cpp @@ -0,0 +1,305 @@ +// Copyright (c) 2010-2020, Lawrence Livermore National Security, LLC. Produced +// at the Lawrence Livermore National Laboratory. All Rights reserved. See files +// LICENSE and NOTICE for details. LLNL-CODE-443271. +// +// This file is part of the GLVis visualization tool and library. For more +// information and source code availability see https://glvis.org. +// +// GLVis is free software; you can redistribute it and/or modify it under the +// terms of the BSD-3 license. We welcome feedback and contributions, see file +// CONTRIBUTING.md for details. + +#include "sdl.hpp" +#include "mfem.hpp" + +#if defined(GLVIS_USE_LIBTIFF) +#include "tiffio.h" +#elif defined(GLVIS_USE_LIBPNG) +#include +#endif + +#if defined(GLVIS_USE_LIBTIFF) +const char *glvis_screenshot_ext = ".tif"; +#elif defined(GLVIS_USE_LIBPNG) +const char *glvis_screenshot_ext = ".png"; +#else +const char *glvis_screenshot_ext = ".bmp"; +#endif + +// https://wiki.libsdl.org/SDL_CreateRGBSurfaceFrom +#if SDL_BYTEORDER == SDL_BIG_ENDIAN +Uint32 rmask = 0xff000000; +Uint32 gmask = 0x00ff0000; +Uint32 bmask = 0x0000ff00: + Uint32 amask = 0x000000ff; +#else // little endian, like x86 +Uint32 rmask = 0x000000ff; +Uint32 gmask = 0x0000ff00; +Uint32 bmask = 0x00ff0000; +Uint32 amask = 0xff000000; +#endif + +// https://halfgeek.org/wiki/Vertically_invert_a_surface_in_SDL +#define SDL_LOCKIFMUST(s) (SDL_MUSTLOCK(s) ? SDL_LockSurface(s) : 0) +#define SDL_UNLOCKIFMUST(s) { if(SDL_MUSTLOCK(s)) SDL_UnlockSurface(s); } + +using namespace std; + +int InvertSurfaceVertical(SDL_Surface *surface) +{ + Uint8 *t, *a, *b, *last; + Uint16 pitch; + + if ( SDL_LOCKIFMUST(surface) < 0 ) + { + return -2; + } + + /* do nothing unless at least two lines */ + if (surface->h < 2) + { + SDL_UNLOCKIFMUST(surface); + return 0; + } + + /* get a place to store a line */ + pitch = surface->pitch; + t = (Uint8*)malloc(pitch); + + if (t == NULL) + { + SDL_UNLOCKIFMUST(surface); + return -2; + } + + /* get first line; it's about to be trampled */ + memcpy(t,surface->pixels,pitch); + + /* now, shuffle the rest so it's almost correct */ + a = (Uint8*)surface->pixels; + last = a + pitch * (surface->h - 1); + b = last; + + while (a < b) + { + memcpy(a,b,pitch); + a += pitch; + memcpy(b,a,pitch); + b -= pitch; + } + + /* in this shuffled state, the bottom slice is too far down */ + memmove( b, b+pitch, last-b ); + + /* now we can put back that first row--in the last place */ + memcpy(last,t,pitch); + + /* everything is in the right place; close up. */ + free(t); + SDL_UNLOCKIFMUST(surface); + + return 0; +} + +int SdlWindow::screenshotHelper(bool convert) +{ +#ifdef GLVIS_DEBUG + cout << "Screenshot: glFinish() ... " << flush; +#endif + glFinish(); +#ifdef GLVIS_DEBUG + cout << "done." << endl; +#endif +#ifndef __EMSCRIPTEN__ + if (isExposePending()) + { + MFEM_WARNING("Expose pending, some events may not have been handled." << endl); + } + string filename = screenshot_file.c_str(); + string convert_name = screenshot_file.c_str(); + bool call_convert = false; + if (convert) + { + // check the extension of 'fname' to see if convert is needed + size_t ext_size = strlen(glvis_screenshot_ext); + if (filename.size() < ext_size || + filename.compare(filename.size() - ext_size, + ext_size, glvis_screenshot_ext) != 0) + { + call_convert = true; + filename += glvis_screenshot_ext; + } + } + else // do not call convert + { + filename += glvis_screenshot_ext; + } + + int w, h; + getGLDrawSize(w, h); + if (isSwapPending()) + { +#if GLVIS_DEBUG + cerr << "Screenshot: reading image data from back buffer..." << endl; +#endif + glReadBuffer(GL_BACK); + } + else + { +#if GLVIS_DEBUG + cerr << "Screenshot: reading image data from front buffer..." << endl; +#endif + glReadBuffer(GL_FRONT); + } +#if defined(GLVIS_USE_LIBTIFF) + // Save a TIFF image. This requires the libtiff library, see www.libtiff.org + TIFF* image; + + // MyExpose(w,h); + + unsigned char *pixels = new unsigned char[3*w]; + if (!pixels) + { + return 1; + } + + image = TIFFOpen(filename.c_str(), "w"); + if (!image) + { + delete [] pixels; + return 2; + } + + TIFFSetField(image, TIFFTAG_IMAGEWIDTH, w); + TIFFSetField(image, TIFFTAG_IMAGELENGTH, h); + TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, 8); + TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS); + TIFFSetField(image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); + TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, 3); + TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, 1); + TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); + TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); + for (int i = 0; i < h; i++) + { + glReadPixels(0, h-i-1, w, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels); + if (TIFFWriteScanline(image, pixels, i, 0) < 0) + { + TIFFClose(image); + delete [] pixels; + return 3; + } + } + + TIFFFlushData(image); + TIFFClose(image); + delete [] pixels; + +#elif defined(GLVIS_USE_LIBPNG) + // Save as png image. Requires libpng. + + png_byte *pixels = new png_byte[3*w]; + if (!pixels) + { + return 1; + } + + png_structp png_ptr = + png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (!png_ptr) + { + delete [] pixels; + return 1; + } + png_infop info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) + { + png_destroy_write_struct(&png_ptr, (png_infopp)NULL); + delete [] pixels; + return 1; + } + + FILE *fp = fopen(filename.c_str(), "wb"); + if (!fp) + { + png_destroy_write_struct(&png_ptr, &info_ptr); + delete [] pixels; + return 2; + } + + if (setjmp(png_jmpbuf(png_ptr))) + { + fclose(fp); + png_destroy_write_struct(&png_ptr, &info_ptr); + delete [] pixels; + return 3; + } + + png_uint_32 ppi = isHighDpi() ? 144 : 72; // pixels/inch + png_uint_32 ppm = ppi/0.0254 + 0.5; // pixels/meter + png_set_pHYs(png_ptr, info_ptr, ppm, ppm, PNG_RESOLUTION_METER); + + png_init_io(png_ptr, fp); + png_set_IHDR(png_ptr, info_ptr, w, h, 8, PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + for (int i = 0; i < h; i++) + { + glReadPixels(0, h-1-i, w, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels); + png_write_row(png_ptr, pixels); + } + png_write_end(png_ptr, info_ptr); + + fclose(fp); + png_destroy_write_struct(&png_ptr, &info_ptr); + delete [] pixels; + +#else + // use SDL for screenshots + + // https://stackoverflow.com/questions/20233469/how-do-i-take-and-save-a-bmp-screenshot-in-sdl-2 + unsigned char * pixels = new unsigned char[w*h*4]; // 4 bytes for RGBA + glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + + SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, rmask, + gmask, bmask, amask); + if (surf == nullptr) + { + std::cerr << "unable to take screenshot: " << SDL_GetError() << std::endl; + } + else + { + if (InvertSurfaceVertical(surf)) + { + std::cerr << "failed to invert surface, your screenshot may be upside down" << + std::endl; + } + SDL_SaveBMP(surf, filename.c_str()); + SDL_FreeSurface(surf); + // automatically convert to png if not being used + if (!call_convert) + { + call_convert = true; + convert_name += ".png"; + } + } + delete [] pixels; +#endif + + if (call_convert) + { + ostringstream cmd; + cmd << "convert " << filename << ' ' << convert_name; + if (system(cmd.str().c_str())) + { + return 1; + } + remove(filename.c_str()); + } + return 0; +#else + cout << "Screenshots not yet implemented for JS" << endl; + return 1; +#endif +} diff --git a/lib/threads.cpp b/lib/threads.cpp index 14b8b961..052d89c5 100644 --- a/lib/threads.cpp +++ b/lib/threads.cpp @@ -459,14 +459,8 @@ int GLVisCommand::Execute() case SCREENSHOT: { cout << "Command: screenshot: " << flush; - if (::Screenshot(screenshot_filename.c_str(), true)) - { - cout << "Screenshot(" << screenshot_filename << ") failed." << endl; - } - else - { - cout << "-> " << screenshot_filename << endl; - } + GetGLVisWindow()->Screenshot(screenshot_filename.c_str()); + cout << "-> " << screenshot_filename << endl; break; } diff --git a/makefile b/makefile index 3c1a2a1a..50f2018f 100644 --- a/makefile +++ b/makefile @@ -215,8 +215,9 @@ ALL_SOURCE_FILES = \ lib/gl/renderer.cpp lib/gl/renderer_core.cpp lib/gl/renderer_ff.cpp \ lib/gl/shader.cpp lib/gl/types.cpp lib/aux_js.cpp lib/aux_vis.cpp lib/font.cpp \ lib/gl2ps.c lib/material.cpp lib/openglvis.cpp lib/palettes.cpp lib/sdl.cpp \ - lib/stream_reader.cpp lib/threads.cpp lib/vsdata.cpp lib/vssolution.cpp \ - lib/vssolution3d.cpp lib/vsvector.cpp lib/vsvector3d.cpp + lib/sdl_screenshot.cpp lib/stream_reader.cpp lib/threads.cpp \ + lib/vsdata.cpp lib/vssolution.cpp lib/vssolution3d.cpp \ + lib/vsvector.cpp lib/vsvector3d.cpp OBJC_SOURCE_FILES = $(if $(NOTMAC),,lib/sdl_mac.mm) DESKTOP_ONLY_SOURCE_FILES = lib/gl/renderer_ff.cpp lib/threads.cpp lib/gl2ps.c WEB_ONLY_SOURCE_FILES = lib/aux_js.cpp From e3cc12aef271e5560286816ebc18c30e7954d59e Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 28 Jan 2021 13:50:31 -0800 Subject: [PATCH 09/39] Move some window control functions to GLVisWindow --- glvis.cpp | 2 +- lib/aux_vis.cpp | 42 +++++++++++++++++++++--------------------- lib/aux_vis.hpp | 23 +++++++++++++---------- lib/threads.cpp | 8 ++++---- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 225dbe22..d0f452f8 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -656,7 +656,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) scr >> window_x >> window_y >> window_w >> window_h; cout << "Script: window: " << window_x << ' ' << window_y << ' ' << window_w << ' ' << window_h << endl; - MoveResizeWindow(window_x, window_y, window_w, window_h); + wnd->MoveResizeWindow(window_x, window_y, window_w, window_h); MyExpose(); } else if (word == "keys") diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index c99e4137..21da78b4 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -164,7 +164,7 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega SetKeyEventHandler (SDLK_s, &GLVisWindow::Screenshot); SetKeyEventHandler ('S', &GLVisWindow::Screenshot); - wnd->setOnKeyDown (SDLK_q, KeyQPressed); + SetKeyEventHandler (SDLK_q, &GLVisWindow::Quit); // wnd->setOnKeyDown (SDLK_Q, KeyQPressed); wnd->setOnKeyDown (SDLK_LEFT, KeyLeftPressed); @@ -210,19 +210,19 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega wnd->setOnKeyDown (SDLK_j, KeyJPressed); // wnd->setOnKeyDown (AUX_J, KeyJPressed); - wnd->setOnKeyDown (SDLK_KP_MULTIPLY, ZoomIn); - wnd->setOnKeyDown (SDLK_KP_DIVIDE, ZoomOut); + SetKeyEventHandler (SDLK_KP_MULTIPLY, &GLVisWindow::ZoomIn); + SetKeyEventHandler (SDLK_KP_DIVIDE, &GLVisWindow::ZoomOut); - wnd->setOnKeyDown (SDLK_ASTERISK, ZoomIn); - wnd->setOnKeyDown (SDLK_SLASH, ZoomOut); + SetKeyEventHandler (SDLK_ASTERISK, &GLVisWindow::ZoomIn); + SetKeyEventHandler (SDLK_SLASH, &GLVisWindow::ZoomOut); - wnd->setOnKeyDown (SDLK_LEFTBRACKET, ScaleDown); - wnd->setOnKeyDown (SDLK_RIGHTBRACKET, ScaleUp); - wnd->setOnKeyDown (SDLK_AT, LookAt); + SetKeyEventHandler (SDLK_LEFTBRACKET, &GLVisWindow::ScaleDown); + SetKeyEventHandler (SDLK_RIGHTBRACKET, &GLVisWindow::ScaleUp); + SetKeyEventHandler (SDLK_AT, &GLVisWindow::LookAt); #ifndef __EMSCRIPTEN__ - wnd->setOnKeyDown(SDLK_LEFTPAREN, ShrinkWindow); - wnd->setOnKeyDown(SDLK_RIGHTPAREN, EnlargeWindow); + SetKeyEventHandler(SDLK_LEFTPAREN, &GLVisWindow::ShrinkWindow); + SetKeyEventHandler(SDLK_RIGHTPAREN, &GLVisWindow::EnlargeWindow); #endif } @@ -929,7 +929,7 @@ void GLVisWindow::PrintToPDF() #endif } -void KeyQPressed() +void GLVisWindow::Quit() { wnd->signalQuit(); visualize = 0; @@ -1178,31 +1178,31 @@ void KeyPlusPressed() SendExposeEvent(); } -void ZoomIn() +void GLVisWindow::ZoomIn() { locscene->Zoom(exp (0.05)); SendExposeEvent(); } -void ZoomOut() +void GLVisWindow::ZoomOut() { locscene->Zoom(exp (-0.05)); SendExposeEvent(); } -void ScaleUp() +void GLVisWindow::ScaleUp() { locscene->Scale(1.025); SendExposeEvent(); } -void ScaleDown() +void GLVisWindow::ScaleDown() { locscene->Scale(1.0/1.025); SendExposeEvent(); } -void LookAt() +void GLVisWindow::LookAt() { cout << "ViewCenter = (" << locscene->ViewCenterX << ',' << locscene->ViewCenterY << ")\nNew x = " << flush; @@ -1214,7 +1214,7 @@ void LookAt() const double window_scale_factor = 1.1; -void ShrinkWindow() +void GLVisWindow::ShrinkWindow() { int w, h; wnd->getWindowSize(w, h); @@ -1226,7 +1226,7 @@ void ShrinkWindow() ResizeWindow(w, h); } -void EnlargeWindow() +void GLVisWindow::EnlargeWindow() { int w, h; wnd->getWindowSize(w, h); @@ -1238,18 +1238,18 @@ void EnlargeWindow() ResizeWindow(w, h); } -void MoveResizeWindow(int x, int y, int w, int h) +void GLVisWindow::MoveResizeWindow(int x, int y, int w, int h) { wnd->setWindowSize(w, h); wnd->setWindowPos(x, y); } -void ResizeWindow(int w, int h) +void GLVisWindow::ResizeWindow(int w, int h) { wnd->setWindowSize(w, h); } -void SetWindowTitle(const char *title) +void GLVisWindow::SetWindowTitle(const char *title) { wnd->setWindowTitle(title); } diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index c23edbbe..75f6e0ca 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -63,10 +63,23 @@ class GLVisWindow void MainLoop(); + void Quit(); + void ToggleAntialiasing(); void Screenshot() { Screenshot(""); } void Screenshot(std::string filename); void PrintToPDF(); + + void ZoomIn(); + void ZoomOut(); + void ScaleUp(); + void ScaleDown(); + void LookAt(); + void ShrinkWindow(); + void EnlargeWindow(); + void MoveResizeWindow(int x, int y, int w, int h); + void ResizeWindow(int w, int h); + void SetWindowTitle(const char *title); private: void InitFont(); bool SetFont(const vector& patterns, int height); @@ -133,16 +146,6 @@ void KeyJPressed(); void KeyMinusPressed(); void KeyPlusPressed(); -void ZoomIn(); -void ZoomOut(); -void ScaleUp(); -void ScaleDown(); -void LookAt(); -void ShrinkWindow(); -void EnlargeWindow(); -void MoveResizeWindow(int x, int y, int w, int h); -void ResizeWindow(int w, int h); -void SetWindowTitle(const char *title); /// Take a screenshot using libtiff, libpng or sdl2 //int Screenshot(const char *fname, bool convert = false); diff --git a/lib/threads.cpp b/lib/threads.cpp index 052d89c5..29bdc670 100644 --- a/lib/threads.cpp +++ b/lib/threads.cpp @@ -468,7 +468,7 @@ int GLVisCommand::Execute() { cout << "Command: keys: '" << key_commands << "'" << endl; // SendKeySequence(key_commands.c_str()); - CallKeySequence(key_commands.c_str()); + GetGLVisWindow()->CallKeySequence(key_commands.c_str()); MyExpose(); break; } @@ -476,7 +476,7 @@ int GLVisCommand::Execute() case WINDOW_SIZE: { cout << "Command: window_size: " << window_w << " x " << window_h << endl; - ResizeWindow(window_w, window_h); + GetGLVisWindow()->ResizeWindow(window_w, window_h); break; } @@ -485,14 +485,14 @@ int GLVisCommand::Execute() cout << "Command: window_geometry: " << "@(" << window_x << "," << window_y << ") " << window_w << " x " << window_h << endl; - MoveResizeWindow(window_x, window_y, window_w, window_h); + GetGLVisWindow()->MoveResizeWindow(window_x, window_y, window_w, window_h); break; } case WINDOW_TITLE: { cout << "Command: window_title: " << window_title << endl; - SetWindowTitle(window_title.c_str()); + GetGLVisWindow()->SetWindowTitle(window_title.c_str()); break; } From e7b9806504f60d9d38fd22d45cc0931ec8911985 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 28 Jan 2021 14:22:38 -0800 Subject: [PATCH 10/39] Move more scene transformation methods into GLVisWindow --- lib/aux_vis.cpp | 115 ++++++++++++++++++++++++---------------------- lib/aux_vis.hpp | 41 +++++++++-------- lib/openglvis.hpp | 4 ++ 3 files changed, 85 insertions(+), 75 deletions(-) diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 21da78b4..b72f6a8d 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -64,9 +64,6 @@ VisualizationScene * GetVisualizationScene() return locscene; } -void MyExpose(GLsizei w, GLsizei h); -void MyExpose(); - struct GLVisWindow::RotationControl { GLVisWindow* wnd; @@ -167,24 +164,24 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega SetKeyEventHandler (SDLK_q, &GLVisWindow::Quit); // wnd->setOnKeyDown (SDLK_Q, KeyQPressed); - wnd->setOnKeyDown (SDLK_LEFT, KeyLeftPressed); - wnd->setOnKeyDown (SDLK_RIGHT, KeyRightPressed); - wnd->setOnKeyDown (SDLK_UP, KeyUpPressed); - wnd->setOnKeyDown (SDLK_DOWN, KeyDownPressed); + SetKeyEventHandler (SDLK_LEFT, &GLVisWindow::KeyLeftPressed); + SetKeyEventHandler (SDLK_RIGHT, &GLVisWindow::KeyRightPressed); + SetKeyEventHandler (SDLK_UP, &GLVisWindow::KeyUpPressed); + SetKeyEventHandler (SDLK_DOWN, &GLVisWindow::KeyDownPressed); wnd->setOnKeyDown (SDLK_KP_0, Key0Pressed); - wnd->setOnKeyDown (SDLK_KP_1, Key1Pressed); - wnd->setOnKeyDown (SDLK_KP_2, Key2Pressed); - wnd->setOnKeyDown (SDLK_KP_3, Key3Pressed); - wnd->setOnKeyDown (SDLK_KP_4, Key4Pressed); - wnd->setOnKeyDown (SDLK_KP_5, Key5Pressed); - wnd->setOnKeyDown (SDLK_KP_6, Key6Pressed); - wnd->setOnKeyDown (SDLK_KP_7, Key7Pressed); - wnd->setOnKeyDown (SDLK_KP_8, Key8Pressed); - wnd->setOnKeyDown (SDLK_KP_9, Key9Pressed); - - wnd->setOnKeyDown (SDLK_KP_MEMSUBTRACT, KeyMinusPressed); - wnd->setOnKeyDown (SDLK_KP_MEMADD, KeyPlusPressed); + SetKeyEventHandler (SDLK_KP_1, &GLVisWindow::Key1Pressed); + SetKeyEventHandler (SDLK_KP_2, &GLVisWindow::Key2Pressed); + SetKeyEventHandler (SDLK_KP_3, &GLVisWindow::Key3Pressed); + SetKeyEventHandler (SDLK_KP_4, &GLVisWindow::Key4Pressed); + SetKeyEventHandler (SDLK_KP_5, &GLVisWindow::Key5Pressed); + SetKeyEventHandler (SDLK_KP_6, &GLVisWindow::Key6Pressed); + SetKeyEventHandler (SDLK_KP_7, &GLVisWindow::Key7Pressed); + SetKeyEventHandler (SDLK_KP_8, &GLVisWindow::Key8Pressed); + SetKeyEventHandler (SDLK_KP_9, &GLVisWindow::Key9Pressed); + + SetKeyEventHandler (SDLK_KP_MEMSUBTRACT, &GLVisWindow::KeyMinusPressed); + SetKeyEventHandler (SDLK_KP_MEMADD, &GLVisWindow::KeyPlusPressed); wnd->setOnKeyDown (SDLK_KP_DECIMAL, KeyDeletePressed); wnd->setOnKeyDown (SDLK_KP_ENTER, KeyEnterPressed); @@ -193,21 +190,21 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega wnd->setOnKeyDown (SDLK_RETURN, KeyEnterPressed); wnd->setOnKeyDown (SDLK_0, Key0Pressed); - wnd->setOnKeyDown (SDLK_1, Key1Pressed); - wnd->setOnKeyDown (SDLK_2, Key2Pressed); - wnd->setOnKeyDown (SDLK_3, Key3Pressed); - wnd->setOnKeyDown (SDLK_4, Key4Pressed); - wnd->setOnKeyDown (SDLK_5, Key5Pressed); - wnd->setOnKeyDown (SDLK_6, Key6Pressed); - wnd->setOnKeyDown (SDLK_7, Key7Pressed); - wnd->setOnKeyDown (SDLK_8, Key8Pressed); - wnd->setOnKeyDown (SDLK_9, Key9Pressed); - - wnd->setOnKeyDown (SDLK_MINUS, KeyMinusPressed); - wnd->setOnKeyDown (SDLK_PLUS, KeyPlusPressed); - wnd->setOnKeyDown (SDLK_EQUALS, KeyPlusPressed); - - wnd->setOnKeyDown (SDLK_j, KeyJPressed); + SetKeyEventHandler (SDLK_1, &GLVisWindow::Key1Pressed); + SetKeyEventHandler (SDLK_2, &GLVisWindow::Key2Pressed); + SetKeyEventHandler (SDLK_3, &GLVisWindow::Key3Pressed); + SetKeyEventHandler (SDLK_4, &GLVisWindow::Key4Pressed); + SetKeyEventHandler (SDLK_5, &GLVisWindow::Key5Pressed); + SetKeyEventHandler (SDLK_6, &GLVisWindow::Key6Pressed); + SetKeyEventHandler (SDLK_7, &GLVisWindow::Key7Pressed); + SetKeyEventHandler (SDLK_8, &GLVisWindow::Key8Pressed); + SetKeyEventHandler (SDLK_9, &GLVisWindow::Key9Pressed); + + SetKeyEventHandler (SDLK_MINUS, &GLVisWindow::KeyMinusPressed); + SetKeyEventHandler (SDLK_PLUS, &GLVisWindow::KeyPlusPressed); + SetKeyEventHandler (SDLK_EQUALS, &GLVisWindow::KeyPlusPressed); + + SetKeyEventHandler (SDLK_j, &GLVisWindow::KeyJPressed); // wnd->setOnKeyDown (AUX_J, KeyJPressed); SetKeyEventHandler (SDLK_KP_MULTIPLY, &GLVisWindow::ZoomIn); @@ -232,6 +229,12 @@ void GLVisWindow::SetKeyEventHandler(int key, void (GLVisWindow::*handler)()) wnd->setOnKeyDown(key, handlerWrapper); } +void GLVisWindow::SetKeyEventHandler(int key, void (GLVisWindow::*handler)(GLenum)) +{ + auto handlerWrapper = [this, handler](GLenum mod) { (this->*handler)(mod); }; + wnd->setOnKeyDown(key, handlerWrapper); +} + void SendKeySequence(const char *seq) { glvis_wnd->SendKeySequence(seq); @@ -1032,31 +1035,31 @@ void GLVisWindow::RotationControl::KeyEnterPressed() CheckSpin(); } -void Key7Pressed() +void GLVisWindow::Key7Pressed() { locscene->PreRotate(1.0, 0.0, -1.0, 0.0); SendExposeEvent(); } -void Key8Pressed() +void GLVisWindow::Key8Pressed() { locscene->Rotate(0.0, -1.0); SendExposeEvent(); } -void Key9Pressed() +void GLVisWindow::Key9Pressed() { locscene->PreRotate(-1.0, 1.0, 0.0, 0.0); SendExposeEvent(); } -void Key4Pressed() +void GLVisWindow::Key4Pressed() { locscene->PreRotate(-1.0, 0.0, 0.0, 1.0); SendExposeEvent(); } -void Key5Pressed() +void GLVisWindow::Key5Pressed() { if (locscene->view == 2) { @@ -1069,31 +1072,31 @@ void Key5Pressed() SendExposeEvent(); } -void Key6Pressed() +void GLVisWindow::Key6Pressed() { locscene->PreRotate(1.0, 0.0, 0.0, 1.0); SendExposeEvent(); } -void Key1Pressed() +void GLVisWindow::Key1Pressed() { locscene->PreRotate(1.0, 1.0, 0.0, 0.0); SendExposeEvent(); } -void Key2Pressed() +void GLVisWindow::Key2Pressed() { locscene->Rotate(1.0, 1.0, 0.0, 0.0); SendExposeEvent(); } -void Key3Pressed() +void GLVisWindow::Key3Pressed() { locscene->PreRotate(1.0, 0.0, 1.0, 0.0); SendExposeEvent(); } -void ShiftView(double dx, double dy) +void ShiftView(VisualizationScene* locscene, double dx, double dy) { double scale; if (locscene->OrthogonalProjection) @@ -1108,11 +1111,11 @@ void ShiftView(double dx, double dy) locscene->ViewCenterY += dy/scale; } -void KeyLeftPressed(GLenum state) +void GLVisWindow::KeyLeftPressed(GLenum state) { if (state & KMOD_CTRL) { - ShiftView(0.05, 0.); + ShiftView(locscene, 0.05, 0.); } else { @@ -1121,11 +1124,11 @@ void KeyLeftPressed(GLenum state) SendExposeEvent(); } -void KeyRightPressed(GLenum state) +void GLVisWindow::KeyRightPressed(GLenum state) { if (state & KMOD_CTRL) { - ShiftView(-0.05, 0.); + ShiftView(locscene, -0.05, 0.); } else { @@ -1134,11 +1137,11 @@ void KeyRightPressed(GLenum state) SendExposeEvent(); } -void KeyUpPressed(GLenum state) +void GLVisWindow::KeyUpPressed(GLenum state) { if (state & KMOD_CTRL) { - ShiftView(0., -0.05); + ShiftView(locscene, 0., -0.05); } else { @@ -1147,11 +1150,11 @@ void KeyUpPressed(GLenum state) SendExposeEvent(); } -void KeyDownPressed(GLenum state) +void GLVisWindow::KeyDownPressed(GLenum state) { if (state & KMOD_CTRL) { - ShiftView(0., 0.05); + ShiftView(locscene, 0., 0.05); } else { @@ -1160,19 +1163,19 @@ void KeyDownPressed(GLenum state) SendExposeEvent(); } -void KeyJPressed() +void GLVisWindow::KeyJPressed() { - locscene->OrthogonalProjection = !(locscene->OrthogonalProjection); + locscene->ToggleProjectionMode(); SendExposeEvent(); } -void KeyMinusPressed() +void GLVisWindow::KeyMinusPressed() { locscene -> Scale(1., 1., 1./1.1); SendExposeEvent(); } -void KeyPlusPressed() +void GLVisWindow::KeyPlusPressed() { locscene -> Scale(1., 1., 1.1); SendExposeEvent(); diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 75f6e0ca..86227683 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -85,11 +85,33 @@ class GLVisWindow bool SetFont(const vector& patterns, int height); void SetKeyEventHandler(int key, void (GLVisWindow::*handler)()); + void SetKeyEventHandler(int key, void (GLVisWindow::*handler)(GLenum)); void MainIdleFunc(); void MyReshape(GLsizei w, GLsizei h); void MyExpose(GLsizei w, GLsizei h); + + // Internal event handlers for small scene rotations + void Key1Pressed(); + void Key2Pressed(); + void Key3Pressed(); + void Key4Pressed(); + void Key5Pressed(); + void Key6Pressed(); + void Key7Pressed(); + void Key8Pressed(); + void Key9Pressed(); + + // Internal event handlers for other scene rotations, transformations + void KeyLeftPressed(GLenum); + void KeyRightPressed(GLenum); + void KeyUpPressed(GLenum); + void KeyDownPressed(GLenum); + void KeyJPressed(); + void KeyMinusPressed(); + void KeyPlusPressed(); + std::unique_ptr wnd; VisualizationScene* locscene; @@ -122,30 +144,11 @@ VisualizationScene * GetVisualizationScene(); void TouchPinch(SDL_MultiGestureEvent & e); -void KeyQPressed(); void ToggleThreads(); void ThreadsPauseFunc(GLenum); void ThreadsStop(); void ThreadsRun(); -void Key1Pressed(); -void Key2Pressed(); -void Key3Pressed(); -void Key4Pressed(); -void Key5Pressed(); -void Key6Pressed(); -void Key7Pressed(); -void Key8Pressed(); -void Key9Pressed(); - -void KeyLeftPressed(GLenum); -void KeyRightPressed(GLenum); -void KeyUpPressed(GLenum); -void KeyDownPressed(GLenum); -void KeyJPressed(); -void KeyMinusPressed(); -void KeyPlusPressed(); - /// Take a screenshot using libtiff, libpng or sdl2 //int Screenshot(const char *fname, bool convert = false); diff --git a/lib/openglvis.hpp b/lib/openglvis.hpp index 82754967..1c9b15b3 100644 --- a/lib/openglvis.hpp +++ b/lib/openglvis.hpp @@ -168,6 +168,10 @@ class VisualizationScene void CenterObject(); void CenterObject2D(); + // Toggles between orthogonal and perspective projections. + void ToggleProjectionMode() + { OrthogonalProjection = !OrthogonalProjection; } + void SetProjectionMtx(glm::mat4 projection) { proj_mtx = projection; } void SetLightMatIdx(unsigned i); int GetLightMatIdx() { return light_mat_idx; } From 93c95f817faf645131f12e9095cacb09683c9a7a Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 28 Jan 2021 15:45:45 -0800 Subject: [PATCH 11/39] Fix a segfault with font ptr in VisualizationScene --- glvis.cpp | 2 -- lib/aux_vis.cpp | 2 +- lib/openglvis.cpp | 1 + lib/openglvis.hpp | 2 -- 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index d0f452f8..e7b48b8d 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -175,8 +175,6 @@ bool GLVisInitVis(int field_type) VisualizationSceneSolution3d * vss; vs = vss = new VisualizationSceneSolution3d(*stream_state.mesh, stream_state.sol); - // HACK: below needs to be called before ToggleAxes - vs->SetFont(mainWindow->getFont()); if (stream_state.grid_f) { vss->SetGridFunction(stream_state.grid_f.get()); diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index b72f6a8d..2657571c 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -354,7 +354,7 @@ void GLVisWindow::SetVisualizationScene(VisualizationScene * scene, int view, const char *keys) { ::locscene = locscene = scene; - locscene->SetFont(&font); + //locscene->SetFont(&font); locscene -> view = view; if (view == 2) { diff --git a/lib/openglvis.cpp b/lib/openglvis.cpp index b5744b46..c70cf378 100644 --- a/lib/openglvis.cpp +++ b/lib/openglvis.cpp @@ -128,6 +128,7 @@ VisualizationScene::VisualizationScene() use_light = true; palette.Init(); + font = GetGLVisWindow()->getFont(); } VisualizationScene::~VisualizationScene() {} diff --git a/lib/openglvis.hpp b/lib/openglvis.hpp index 1c9b15b3..c2dd58b8 100644 --- a/lib/openglvis.hpp +++ b/lib/openglvis.hpp @@ -190,8 +190,6 @@ class VisualizationScene } } - void SetFont(GlVisFont* fnt) { this->font = fnt; }; - void GenerateAlphaTexture() { palette.GenerateAlphaTexture(matAlpha, matAlphaCenter); } From d836ddec6a0d92e768c34fc5b78558c7ec6ee6fe Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 28 Jan 2021 16:15:06 -0800 Subject: [PATCH 12/39] Remove GetUseTexture/SetUseTexture functions, move into aux_js --- lib/aux_js.cpp | 52 +++++++++++++++++++++++++++++++++++++++++-------- lib/aux_vis.cpp | 17 ---------------- lib/aux_vis.hpp | 8 +++----- lib/threads.cpp | 8 -------- 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/lib/aux_js.cpp b/lib/aux_js.cpp index e4b3b6e2..a91a17a1 100644 --- a/lib/aux_js.cpp +++ b/lib/aux_js.cpp @@ -22,6 +22,7 @@ mfem::GeometryRefiner GLVisGeometryRefiner; // used in extern context static VisualizationSceneScalarData * vs = nullptr; StreamState stream_state; +GLVisWindow * mainWindow = nullptr; namespace js { @@ -64,8 +65,20 @@ bool startVisualization(const std::string input, const std::string data_type, return false; } - if (InitVisualization("glvis", 0, 0, w, h)) + try { + mainWindow = new GLVisWindow("glvis", 0, 0, w, h, false); + } + catch (std::runtime_error& ex) + { + cerr << "Initializing the visualization failed: " << endl + << ex.what() << endl; + return false; + } + catch(...) + { + cerr << "Initializing the visualization failed - unknown error." + << endl; return false; } @@ -192,22 +205,22 @@ bool startVisualization(const std::string input, const std::string data_type, } if (stream_state.mesh->SpaceDimension() == 2 && field_type == 2) { - SetVisualizationScene(vs, 2); + mainWindow->SetVisualizationScene(vs, 2); } else { - SetVisualizationScene(vs, 3); + mainWindow->SetVisualizationScene(vs, 3); } } - CallKeySequence(stream_state.keys.c_str()); + mainWindow->CallKeySequence(stream_state.keys.c_str()); if (minv || maxv) { vs->SetValueRange(minv, maxv); } - SendExposeEvent(); + mainWindow->SendExposeEvent(); return true; } @@ -310,6 +323,29 @@ std::string getHelpString() = dynamic_cast(GetVisualizationScene()); return vss->GetHelpString(); } + +void ResizeWindow(int w, int h) +{ + mainWindow->ResizeWindow(w, h); +} + +int GetUseTexture() +{ + return vs->GetPalette().GetSmoothSetting(); +} + +void SetUseTexture(int ut) +{ + if (ut == 0) + { + vs->GetPalette().UseDiscrete(); + } + else + { + vs->GetPalette().UseSmooth(); + } +} + } // namespace js // Info on type conversion: @@ -325,9 +361,9 @@ EMSCRIPTEN_BINDINGS(js_funcs) em::function("enableKeyHandling", &js::enableKeyHandling); em::function("setKeyboardListeningElementId", js::setKeyboardListeningElementId); - em::function("getTextureMode", &GetUseTexture); - em::function("setTextureMode", &SetUseTexture); - em::function("resizeWindow", &ResizeWindow); + em::function("getTextureMode", &js::GetUseTexture); + em::function("setTextureMode", &js::SetUseTexture); + em::function("resizeWindow", &js::ResizeWindow); em::function("setCanvasId", &js::setCanvasId); em::function("setupResizeEventCallback", &js::setupResizeEventCallback); em::function("getHelpString", &js::getHelpString); diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 2657571c..7616616a 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -1257,23 +1257,6 @@ void GLVisWindow::SetWindowTitle(const char *title) wnd->setWindowTitle(title); } -int GetUseTexture() -{ - return locscene->palette.GetSmoothSetting(); -} - -void SetUseTexture(int ut) -{ - if (ut == 0) - { - locscene->palette.UseDiscrete(); - } - else - { - locscene->palette.UseSmooth(); - } -} - int GetMultisample() { return glvis_multisample; diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 86227683..ba1c4c09 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -12,7 +12,7 @@ #ifndef GLVIS_AUX_VIS_HPP #define GLVIS_AUX_VIS_HPP -#include "mfem/mfem.hpp" +#include "mfem.hpp" #include "gl/platform_gl.hpp" #include "gl/types.hpp" @@ -115,10 +115,11 @@ class GLVisWindow std::unique_ptr wnd; VisualizationScene* locscene; + // Idle function callbacks mfem::Array idle_funcs{}; int last_idle_func = 0; - int visualize; + int visualize = 0; bool disableSendExposeEvent = false; @@ -164,9 +165,6 @@ void SendKeySequence(const char *seq); void CallKeySequence(const char *seq); -void SetUseTexture(int ut); -int GetUseTexture(); -void Set_Texture_Image(); int GetMultisample(); void SetMultisample(int m); diff --git a/lib/threads.cpp b/lib/threads.cpp index 29bdc670..0611f663 100644 --- a/lib/threads.cpp +++ b/lib/threads.cpp @@ -627,10 +627,6 @@ int GLVisCommand::Execute() { cout << "Command: palette: " << palette << endl; (*vs)->palette.SetIndex(palette-1); - if (!GetUseTexture()) - { - (*vs)->EventUpdateColors(); - } MyExpose(); break; } @@ -641,10 +637,6 @@ int GLVisCommand::Execute() (*vs)->palette.SetRepeatTimes(palette_repeat); (*vs)->palette.Init(); - if (!GetUseTexture()) - { - (*vs)->EventUpdateColors(); - } MyExpose(); break; } From 69072c173e0fefcd843ff5df2761b9effa822500 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 28 Jan 2021 16:36:19 -0800 Subject: [PATCH 13/39] Move thread control functions into GLVisWindow --- glvis.cpp | 1 - lib/aux_vis.cpp | 27 +++++++++++++++++---------- lib/aux_vis.hpp | 15 +++++++++------ lib/threads.cpp | 12 ++++++------ 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index e7b48b8d..4f3fa472 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -132,7 +132,6 @@ bool GLVisInitVis(int field_type) if (input_streams.Size() > 0) { - mainWindow->getSdl()->setOnKeyDown(SDLK_SPACE, ThreadsPauseFunc); glvis_command = new GLVisCommand(&vs, stream_state, &keep_attr); comm_thread = new communication_thread(input_streams); } diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 7616616a..ecb51a9b 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -217,6 +217,8 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega SetKeyEventHandler (SDLK_RIGHTBRACKET, &GLVisWindow::ScaleUp); SetKeyEventHandler (SDLK_AT, &GLVisWindow::LookAt); + SetKeyEventHandler (SDLK_SPACE, &GLVisWindow::ThreadsPauseFunc); + #ifndef __EMSCRIPTEN__ SetKeyEventHandler(SDLK_LEFTPAREN, &GLVisWindow::ShrinkWindow); SetKeyEventHandler(SDLK_RIGHTPAREN, &GLVisWindow::EnlargeWindow); @@ -938,7 +940,7 @@ void GLVisWindow::Quit() visualize = 0; } -void ToggleThreads() +void GLVisWindow::ToggleThreads() { static const char *state[] = { "running", "stopped" }; if (visualize > 0 && visualize < 3) @@ -948,19 +950,24 @@ void ToggleThreads() } } -void ThreadsPauseFunc(GLenum state) +void GLVisWindow::ThreadsPauseFunc(GLenum state) { - if (state & KMOD_CTRL) - { - glvis_command->ToggleAutopause(); - } - else +#ifndef __EMSCRIPTEN__ + if (glvis_command) { - ToggleThreads(); + if (state & KMOD_CTRL) + { + glvis_command->ToggleAutopause(); + } + else + { + ToggleThreads(); + } } +#endif } -void ThreadsStop() +void GLVisWindow::ThreadsStop() { if (visualize == 1) { @@ -968,7 +975,7 @@ void ThreadsStop() } } -void ThreadsRun() +void GLVisWindow::ThreadsRun() { if (visualize == 2) { diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index ba1c4c09..cc37e677 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -61,6 +61,10 @@ class GLVisWindow void AddIdleFunc(IdleFPtr func); void RemoveIdleFunc(IdleFPtr func); + void ThreadsStop(); + void ThreadsRun(); + void ToggleThreads(); + void MainLoop(); void Quit(); @@ -112,6 +116,9 @@ class GLVisWindow void KeyMinusPressed(); void KeyPlusPressed(); + // Internal event handler for toggling state of threads + void ThreadsPauseFunc(GLenum); + std::unique_ptr wnd; VisualizationScene* locscene; @@ -133,9 +140,9 @@ class GLVisWindow }; /// Send expose event. In our case MyReshape is executed and Draw after it. -void SendExposeEvent(); +[[deprecated]] void SendExposeEvent(); -void MyExpose(); +[[deprecated]] void MyExpose(); void MainLoop(GLVisWindow* wnd); @@ -145,10 +152,6 @@ VisualizationScene * GetVisualizationScene(); void TouchPinch(SDL_MultiGestureEvent & e); -void ToggleThreads(); -void ThreadsPauseFunc(GLenum); -void ThreadsStop(); -void ThreadsRun(); /// Take a screenshot using libtiff, libpng or sdl2 diff --git a/lib/threads.cpp b/lib/threads.cpp index 0611f663..715bb3a5 100644 --- a/lib/threads.cpp +++ b/lib/threads.cpp @@ -451,7 +451,7 @@ int GLVisCommand::Execute() if (autopause) { cout << "Autopause ..." << endl; - ThreadsStop(); + GetGLVisWindow()->ThreadsStop(); } break; } @@ -518,7 +518,7 @@ int GLVisCommand::Execute() case PAUSE: { cout << "Command: pause: "; - ToggleThreads(); + GetGLVisWindow()->ToggleThreads(); break; } @@ -667,11 +667,11 @@ int GLVisCommand::Execute() cout << "Command: autopause: " << strings_off_on[autopause] << endl; if (autopause) { - ThreadsStop(); + GetGLVisWindow()->ThreadsStop(); } else { - ThreadsRun(); // probably not needed + GetGLVisWindow()->ThreadsRun(); // probably not needed } break; } @@ -718,11 +718,11 @@ void GLVisCommand::ToggleAutopause() cout << "Autopause: " << strings_off_on[autopause] << endl; if (autopause) { - ThreadsStop(); + GetGLVisWindow()->ThreadsStop(); } else { - ThreadsRun(); + GetGLVisWindow()->ThreadsRun(); } } From 84815a31e60d7d32bf8b11e948bc71e049f30df2 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Wed, 3 Feb 2021 15:13:50 -0800 Subject: [PATCH 14/39] Remove some global dependencies from glvis.cpp --- glvis.cpp | 50 ++++++++++++++++++++++++------------------------- lib/aux_vis.hpp | 10 ++++++++++ 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 4f3fa472..7bb6db1d 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -488,7 +488,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) if (stream_state.SetNewMeshAndSolution(std::move(new_state), vs)) { - MyExpose(); + wnd->MyExpose(); } else { @@ -520,7 +520,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) scr >> vs->ViewCenterX >> vs->ViewCenterY; cout << "Script: viewcenter: " << vs->ViewCenterX << ' ' << vs->ViewCenterY << endl; - MyExpose(); + wnd->MyExpose(); } else if (word == "perspective") { @@ -539,7 +539,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) cout << '?'; } cout << endl; - MyExpose(); + wnd->MyExpose(); } else if (word == "light") { @@ -558,7 +558,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) cout << '?'; } cout << endl; - MyExpose(); + wnd->MyExpose(); } else if (word == "view") { @@ -566,7 +566,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) scr >> theta >> phi; cout << "Script: view: " << theta << ' ' << phi << endl; vs->SetView(theta, phi); - MyExpose(); + wnd->MyExpose(); } else if (word == "zoom") { @@ -574,7 +574,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) scr >> factor; cout << "Script: zoom: " << factor << endl; vs->Zoom(factor); - MyExpose(); + wnd->MyExpose(); } else if (word == "shading") { @@ -597,7 +597,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) { vs->SetShading(s, false); cout << word << endl; - MyExpose(); + wnd->MyExpose(); } else { @@ -611,7 +611,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) cout << "Script: subdivisions: " << flush; vs->SetRefineFactors(t, b); cout << t << ' ' << b << endl; - MyExpose(); + wnd->MyExpose(); } else if (word == "valuerange") { @@ -620,7 +620,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) cout << "Script: valuerange: " << flush; vs->SetValueRange(min, max); cout << min << ' ' << max << endl; - MyExpose(); + wnd->MyExpose(); } else if (word == "autoscale") { @@ -654,7 +654,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) cout << "Script: window: " << window_x << ' ' << window_y << ' ' << window_w << ' ' << window_h << endl; wnd->MoveResizeWindow(window_x, window_y, window_w, window_h); - MyExpose(); + wnd->MyExpose(); } else if (word == "keys") { @@ -662,7 +662,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) cout << "Script: keys: '" << stream_state.keys << "'" << endl; // SendKeySequence(keys.c_str()); CallKeySequence(stream_state.keys.c_str()); - MyExpose(); + wnd->MyExpose(); } else if (word == "palette") { @@ -670,7 +670,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) scr >> pal; cout << "Script: palette: " << pal << endl; vs->palette.SetIndex(pal-1); - MyExpose(); + wnd->MyExpose(); } else if (word == "palette_repeat") { @@ -679,7 +679,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) cout << "Script: palette_repeat: " << rpt_times << endl; vs->palette.SetRepeatTimes(rpt_times); vs->palette.Init(); - MyExpose(); + wnd->MyExpose(); } else if (word == "toggle_attributes") { @@ -701,7 +701,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) scr.get(); // read the end symbol: ';' cout << endl; vs->ToggleAttributes(attr_list); - MyExpose(); + wnd->MyExpose(); } else if (word == "rotmat") { @@ -712,7 +712,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) cout << ' ' << vs->rotmat[i/4][i%4]; } cout << endl; - MyExpose(); + wnd->MyExpose(); } else if (word == "camera") { @@ -725,7 +725,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) } cout << endl; vs->cam.Set(cam); - MyExpose(); + wnd->MyExpose(); } else if (word == "scale") { @@ -735,7 +735,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) cout << ' ' << scale; cout << endl; vs->Scale(scale); - MyExpose(); + wnd->MyExpose(); } else if (word == "translate") { @@ -745,7 +745,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) cout << ' ' << x << ' ' << y << ' ' << z; cout << endl; vs->Translate(x, y, z); - MyExpose(); + wnd->MyExpose(); } else if (word == "plot_caption") { @@ -753,7 +753,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) scr >> ws >> delim; getline(scr, plot_caption, delim); vs->PrepareCaption(); // turn on or off the caption - MyExpose(); + wnd->MyExpose(); } else { @@ -764,28 +764,28 @@ void ExecuteScriptCommand(GLVisWindow* wnd) } } -void ScriptControl(); +void ScriptControl(GLVisWindow* wnd); void ScriptIdleFunc(GLVisWindow* wnd) { ExecuteScriptCommand(wnd); if (scr_level == 0) { - ScriptControl(); + ScriptControl(wnd); } } -void ScriptControl() +void ScriptControl(GLVisWindow* wnd) { if (scr_running) { scr_running = 0; - mainWindow->RemoveIdleFunc(ScriptIdleFunc); + wnd->RemoveIdleFunc(ScriptIdleFunc); } else { scr_running = 1; - mainWindow->AddIdleFunc(ScriptIdleFunc); + wnd->AddIdleFunc(ScriptIdleFunc); } } @@ -858,7 +858,7 @@ void PlayScript(istream &scr) if (GLVisInitVis((stream_state.grid_f->VectorDim() == 1) ? 0 : 1)) { - GetAppWindow()->setOnKeyDown(SDLK_SPACE, ScriptControl); + mainWindow->AddWindowEvent(SDLK_SPACE, ScriptControl, false); GLVisStartVis(); } diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index cc37e677..fdbe8afb 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -84,6 +84,16 @@ class GLVisWindow void MoveResizeWindow(int x, int y, int w, int h); void ResizeWindow(int w, int h); void SetWindowTitle(const char *title); + + void AddWindowEvent(int key, void (*eh)(GLVisWindow*), bool exposeAfter = true) + { + auto wrapped_eh = [this, eh, exposeAfter](GLenum e) + { + (*eh)(this); + if (exposeAfter) { SendExposeEvent(); } + }; + wnd->setOnKeyDown(key, wrapped_eh); + } private: void InitFont(); bool SetFont(const vector& patterns, int height); From 2707c46ef6afafa8d243b961ac6615a62ca4f9ea Mon Sep 17 00:00:00 2001 From: Max Yang Date: Wed, 12 May 2021 13:57:07 -0700 Subject: [PATCH 15/39] Remove references to global glvis_command instance --- glvis.cpp | 4 +++- lib/aux_vis.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++----- lib/aux_vis.hpp | 13 ++++++++++- lib/sdl.cpp | 52 +++++++++++++------------------------------ lib/sdl.hpp | 5 +++-- lib/threads.cpp | 7 +++--- lib/threads.hpp | 6 ++--- 7 files changed, 92 insertions(+), 54 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 7bb6db1d..32eadef7 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -60,6 +60,7 @@ int input = 1; StreamState stream_state; GLVisWindow * mainWindow = nullptr; VisualizationSceneScalarData *vs = NULL; +GLVisCommand *glvis_command = nullptr; communication_thread *comm_thread = NULL; GeometryRefiner GLVisGeometryRefiner; @@ -133,7 +134,8 @@ bool GLVisInitVis(int field_type) if (input_streams.Size() > 0) { glvis_command = new GLVisCommand(&vs, stream_state, &keep_attr); - comm_thread = new communication_thread(input_streams); + comm_thread = new communication_thread(glvis_command, input_streams); + mainWindow->SetGLVisCommand(glvis_command, comm_thread); } double mesh_range = -1.0; diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index ecb51a9b..25751039 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -380,8 +380,18 @@ void GLVisWindow::SetVisualizationScene(VisualizationScene * scene, int view, wnd->getRenderer().setPalette(&locscene->palette); } +void GLVisWindow::SetGLVisCommand(GLVisCommand* cmd, + communication_thread* cthread) +{ + glvis_command = cmd; + comm_thread = cthread; + use_idle = false; +} + void GLVisWindow::RunVisualization() { + visualize = 1; + wnd->setOnIdle([this](){return MainIdleFunc();}); #ifndef __EMSCRIPTEN__ wnd->mainLoop(); #endif @@ -470,19 +480,55 @@ void GLVisWindow::MyExpose() wnd->signalSwap(); } -void GLVisWindow::MainIdleFunc() +bool GLVisWindow::CommunicationIdleFunc() +{ + int status = glvis_command->Execute(); + if (status < 0) + { + cout << "GLVisCommand signalled exit" << endl; + wnd->signalQuit(); + } + else if (status == 1) + { + // no commands right now - main loop should sleep + return true; + } + return false; +} + +bool GLVisWindow::MainIdleFunc() { - last_idle_func = (last_idle_func + 1) % idle_funcs.Size(); - if (idle_funcs[last_idle_func]) + bool sleep = true; + if (glvis_command && visualize == 1 + && !(idle_funcs.Size() > 0 && use_idle)) { - (*idle_funcs[last_idle_func])(this); + // Execute the next event from the communication thread if: + // - a valid GLVisCommand has been set + // - the communication thread is not stopped + // - The idle function flag is not set, or no idle functions have been + // registered + sleep = CommunicationIdleFunc(); + if (idle_funcs.Size() > 0) { sleep = false; } + } + else if (idle_funcs.Size() > 0) + { + last_idle_func = (last_idle_func + 1) % idle_funcs.Size(); + if (idle_funcs[last_idle_func]) + { + (*idle_funcs[last_idle_func])(this); + } + // Continue executing idle functions + sleep = false; } + use_idle = !use_idle; + return sleep; } void GLVisWindow::AddIdleFunc(GLVisWindow::IdleFPtr Func) { idle_funcs.Union(Func); - wnd->setOnIdle([this](){MainIdleFunc();}); + use_idle = false; + wnd->setOnIdle([this](){return MainIdleFunc();}); } void GLVisWindow::RemoveIdleFunc(GLVisWindow::IdleFPtr Func) @@ -490,7 +536,8 @@ void GLVisWindow::RemoveIdleFunc(GLVisWindow::IdleFPtr Func) idle_funcs.DeleteFirst(Func); if (idle_funcs.Size() == 0) { - wnd->setOnIdle(nullptr); + use_idle = false; + if (!glvis_command) { wnd->setOnIdle(nullptr); } } } diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index fdbe8afb..34fb0c85 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -20,6 +20,10 @@ #include "sdl.hpp" #include "font.hpp" #include "openglvis.hpp" +#include "stream_reader.hpp" + +class GLVisCommand; +class communication_thread; class GLVisWindow { @@ -32,6 +36,9 @@ class GLVisWindow void SetVisualizationScene(VisualizationScene * scene, int view = 3, const char *keys = NULL); + + void SetGLVisCommand(GLVisCommand* cmd, communication_thread* cthread); + void SetFont(const std::string& fn); VisualizationScene* getScene() { return locscene; } @@ -101,7 +108,8 @@ class GLVisWindow void SetKeyEventHandler(int key, void (GLVisWindow::*handler)()); void SetKeyEventHandler(int key, void (GLVisWindow::*handler)(GLenum)); - void MainIdleFunc(); + bool MainIdleFunc(); + bool CommunicationIdleFunc(); void MyReshape(GLsizei w, GLsizei h); void MyExpose(GLsizei w, GLsizei h); @@ -131,6 +139,9 @@ class GLVisWindow std::unique_ptr wnd; VisualizationScene* locscene; + GLVisCommand* glvis_command = nullptr; + communication_thread* comm_thread = nullptr; + bool use_idle = false; // Idle function callbacks mfem::Array idle_funcs{}; diff --git a/lib/sdl.cpp b/lib/sdl.cpp index e4177a61..57f139f4 100644 --- a/lib/sdl.cpp +++ b/lib/sdl.cpp @@ -46,7 +46,6 @@ using std::endl; #endif extern int GetMultisample(); -extern int visualize; struct SdlWindow::Handle { @@ -666,42 +665,22 @@ void SdlWindow::mainIter() #ifndef __EMSCRIPTEN__ else if (onIdle) { - if (glvis_command == NULL || visualize == 2 || useIdle) + bool sleep = onIdle(); + if (sleep) { - onIdle(); - } - else - { - if (glvis_command->Execute() < 0) - { - running = false; - } - } - useIdle = !useIdle; - } - else - { - int status; - if (glvis_command && visualize == 1 && - (status = glvis_command->Execute()) != 1) - { - if (status < 0) { running = false; } - } - else - { - // Wait for the next event (without consuming CPU cycles, if possible) - // See also: SdlWindow::signalLoop() - if (platform) - { - platform->WaitEvent(); - } - else - { - if (!SDL_PollEvent(nullptr)) - { - std::this_thread::sleep_for(std::chrono::milliseconds(8)); - } - } + // Wait for the next event (without consuming CPU cycles, if possible) + // See also: SdlWindow::signalLoop() + if (platform) + { + platform->WaitEvent(); + } + else + { + if (!SDL_PollEvent(nullptr)) + { + std::this_thread::sleep_for(std::chrono::milliseconds(8)); + } + } } } #else @@ -730,7 +709,6 @@ void SdlWindow::mainLoop() ((SdlWindow*) arg)->mainIter(); }, this, 0, true); #else - visualize = 1; while (running) { mainIter(); diff --git a/lib/sdl.hpp b/lib/sdl.hpp index 6a2db298..8f6ef070 100644 --- a/lib/sdl.hpp +++ b/lib/sdl.hpp @@ -32,6 +32,7 @@ typedef std::function MouseDelegate; typedef std::function KeyDelegate; typedef std::function WindowDelegate; typedef std::function Delegate; +typedef std::function IdleDelegate; class SdlWindow { @@ -59,7 +60,7 @@ class SdlWindow bool running; - Delegate onIdle{nullptr}; + IdleDelegate onIdle{nullptr}; Delegate onExpose{nullptr}; WindowDelegate onReshape{nullptr}; std::map onKeyDown; @@ -120,7 +121,7 @@ class SdlWindow // Called by worker threads in GLVisCommand::signal() void signalLoop(); - void setOnIdle(Delegate func) { onIdle = func; } + void setOnIdle(IdleDelegate func) { onIdle = func; } void setOnExpose(Delegate func) { onExpose = func; } void setOnReshape(WindowDelegate func) { onReshape = func; } diff --git a/lib/threads.cpp b/lib/threads.cpp index 715bb3a5..9f392c3e 100644 --- a/lib/threads.cpp +++ b/lib/threads.cpp @@ -21,8 +21,6 @@ using namespace std; extern const char *strings_off_on[]; // defined in vsdata.cpp -GLVisCommand *glvis_command = NULL; - GLVisCommand::GLVisCommand( VisualizationSceneScalarData **_vs, StreamState& state, bool *_keep_attr) : curr_state(state) @@ -737,8 +735,9 @@ GLVisCommand::~GLVisCommand() close(pfd[1]); } -communication_thread::communication_thread(Array &_is) - : is(_is) +communication_thread::communication_thread(GLVisCommand* parent_cmd, + Array &_is) + : is(_is), glvis_command(parent_cmd) { new_m = NULL; new_g = NULL; diff --git a/lib/threads.hpp b/lib/threads.hpp index c9aabc2f..cb19e83f 100644 --- a/lib/threads.hpp +++ b/lib/threads.hpp @@ -138,14 +138,14 @@ class GLVisCommand ~GLVisCommand(); }; -extern GLVisCommand *glvis_command; - class communication_thread { private: // streams to read data from Array &is; + GLVisCommand* glvis_command; + // data that may be dynamically allocated by the thread std::unique_ptr new_m; std::unique_ptr new_g; @@ -159,7 +159,7 @@ class communication_thread void execute(); public: - communication_thread(Array &_is); + communication_thread(GLVisCommand* parent_cmd, Array &_is); ~communication_thread(); }; From e369884fc30676a60b7c47f6bce9d263dce42b10 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 4 Feb 2021 11:47:34 -0800 Subject: [PATCH 16/39] Move command, vis scene construction into GLVisWindow; pass local GLVisWindow into GLVisCommand --- glvis.cpp | 141 +----------------------------------------- lib/aux_vis.cpp | 85 +++++++++++++------------ lib/aux_vis.hpp | 16 ++--- lib/stream_reader.cpp | 120 +++++++++++++++++++++++++++++++++++ lib/stream_reader.hpp | 2 + lib/threads.cpp | 98 +++++++++++++++-------------- lib/threads.hpp | 10 +-- 7 files changed, 232 insertions(+), 240 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 32eadef7..ddfb0c55 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -131,151 +131,16 @@ bool GLVisInitVis(int field_type) mainWindow->SetFont(font_name); } - if (input_streams.Size() > 0) - { - glvis_command = new GLVisCommand(&vs, stream_state, &keep_attr); - comm_thread = new communication_thread(glvis_command, input_streams); - mainWindow->SetGLVisCommand(glvis_command, comm_thread); - } - - double mesh_range = -1.0; - if (field_type == 0 || field_type == 2) - { - if (stream_state.grid_f) - { - stream_state.grid_f->GetNodalValues(stream_state.sol); - } - if (stream_state.mesh->SpaceDimension() == 2) - { - VisualizationSceneSolution * vss; - if (stream_state.normals.Size() > 0) - { - vs = vss = new VisualizationSceneSolution(*stream_state.mesh, stream_state.sol, - &stream_state.normals); - } - else - { - vs = vss = new VisualizationSceneSolution(*stream_state.mesh, stream_state.sol); - } - if (stream_state.grid_f) - { - vss->SetGridFunction(*stream_state.grid_f); - } - if (field_type == 2) - { - vs->OrthogonalProjection = 1; - vs->SetLight(false); - vs->Zoom(1.8); - // Use the 'bone' palette when visualizing a 2D mesh only (otherwise - // the 'jet-like' palette is used in 2D, see vssolution.cpp). - vs->palette.SetIndex(4); - } - } - else if (stream_state.mesh->SpaceDimension() == 3) - { - VisualizationSceneSolution3d * vss; - vs = vss = new VisualizationSceneSolution3d(*stream_state.mesh, - stream_state.sol); - if (stream_state.grid_f) - { - vss->SetGridFunction(stream_state.grid_f.get()); - } - if (field_type == 2) - { - if (stream_state.mesh->Dimension() == 3) - { - // Use the 'white' palette when visualizing a 3D volume mesh only - vss->palette.SetIndex(11); - vss->SetLightMatIdx(4); - } - else - { - // Use the 'bone' palette when visualizing a surface mesh only - vss->palette.SetIndex(4); - } - // Otherwise, the 'vivid' palette is used in 3D see vssolution3d.cpp - vss->ToggleDrawAxes(); - vss->ToggleDrawMesh(); - } - } - if (field_type == 2) - { - if (stream_state.grid_f) - { - mesh_range = stream_state.grid_f->Max() + 1.0; - } - else - { - mesh_range = stream_state.sol.Max() + 1.0; - } - } - } - else if (field_type == 1) - { - if (stream_state.mesh->SpaceDimension() == 2) - { - if (stream_state.grid_f) - { - vs = new VisualizationSceneVector(*stream_state.grid_f); - } - else - { - vs = new VisualizationSceneVector(*stream_state.mesh, stream_state.solu, - stream_state.solv); - } - } - else if (stream_state.mesh->SpaceDimension() == 3) - { - if (stream_state.grid_f) - { - stream_state.grid_f - = ProjectVectorFEGridFunction(std::move(stream_state.grid_f)); - vs = new VisualizationSceneVector3d(*stream_state.grid_f); - } - else - { - vs = new VisualizationSceneVector3d(*stream_state.mesh, stream_state.solu, - stream_state.solv, stream_state.solw); - } - } - } + mainWindow->InitVisualization(field_type, std::move(stream_state), + input_streams, keep_attr); - if (vs) - { - // increase the refinement factors if visualizing a GridFunction - if (stream_state.grid_f) - { - vs->AutoRefine(); - vs->SetShading(2, true); - } - if (mesh_range > 0.0) - { - vs->SetValueRange(-mesh_range, mesh_range); - vs->SetAutoscale(0); - } - if (stream_state.mesh->SpaceDimension() == 2 && field_type == 2) - { - mainWindow->SetVisualizationScene(vs, 2, stream_state.keys.c_str()); - } - else - { - mainWindow->SetVisualizationScene(vs, 3, stream_state.keys.c_str()); - } - } return true; } void GLVisStartVis() { mainWindow->RunVisualization(); // deletes vs - vs = NULL; - if (input_streams.Size() > 0) - { - glvis_command->Terminate(); - delete comm_thread; - delete glvis_command; - glvis_command = NULL; - } + delete mainWindow; cout << "GLVis window closed." << endl; } diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 25751039..9338268b 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -225,6 +225,44 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega #endif } +GLVisWindow::~GLVisWindow() +{ + if (glvis_command) + { + glvis_command->Terminate(); + } +} + +void GLVisWindow::InitVisualization(int field_type, StreamState state, + const mfem::Array& input_streams, + bool& keep_attr) +{ + prob_state = std::move(state); + if (input_streams.Size() > 0) + { + glvis_command.reset(new GLVisCommand(this, prob_state, &keep_attr)); + comm_thread.reset(new communication_thread(glvis_command.get(), input_streams)); + } + + locscene = prob_state.CreateVisualizationScene(field_type); + + if (prob_state.mesh->SpaceDimension() == 2 && field_type == 2) + { + locscene->view = 2; + locscene->CenterObject2D(); + } + else + { + locscene->view = 3; + locscene->CenterObject(); + } + + if (locscene->spinning) + { + AddIdleFunc(::MainLoop); + } +} + void GLVisWindow::SetKeyEventHandler(int key, void (GLVisWindow::*handler)()) { auto handlerWrapper = [this, handler]() { (this->*handler)(); }; @@ -352,42 +390,6 @@ void GLVisWindow::CallKeySequence(const char *seq) disableSendExposeEvent = false; } -void GLVisWindow::SetVisualizationScene(VisualizationScene * scene, int view, - const char *keys) -{ - ::locscene = locscene = scene; - //locscene->SetFont(&font); - locscene -> view = view; - if (view == 2) - { - scene -> CenterObject2D(); - } - else - { - scene -> CenterObject(); - } - - if (scene -> spinning) - { - AddIdleFunc(::MainLoop); - } - - if (keys) - { - // SendKeySequence(keys); - CallKeySequence(keys); - } - wnd->getRenderer().setPalette(&locscene->palette); -} - -void GLVisWindow::SetGLVisCommand(GLVisCommand* cmd, - communication_thread* cthread) -{ - glvis_command = cmd; - comm_thread = cthread; - use_idle = false; -} - void GLVisWindow::RunVisualization() { visualize = 1; @@ -395,9 +397,6 @@ void GLVisWindow::RunVisualization() #ifndef __EMSCRIPTEN__ wnd->mainLoop(); #endif - delete locscene; - //delete wnd; - wnd = nullptr; } void SendExposeEvent() @@ -1169,7 +1168,7 @@ void GLVisWindow::KeyLeftPressed(GLenum state) { if (state & KMOD_CTRL) { - ShiftView(locscene, 0.05, 0.); + ShiftView(locscene.get(), 0.05, 0.); } else { @@ -1182,7 +1181,7 @@ void GLVisWindow::KeyRightPressed(GLenum state) { if (state & KMOD_CTRL) { - ShiftView(locscene, -0.05, 0.); + ShiftView(locscene.get(), -0.05, 0.); } else { @@ -1195,7 +1194,7 @@ void GLVisWindow::KeyUpPressed(GLenum state) { if (state & KMOD_CTRL) { - ShiftView(locscene, 0., -0.05); + ShiftView(locscene.get(), 0., -0.05); } else { @@ -1208,7 +1207,7 @@ void GLVisWindow::KeyDownPressed(GLenum state) { if (state & KMOD_CTRL) { - ShiftView(locscene, 0., 0.05); + ShiftView(locscene.get(), 0., 0.05); } else { diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 34fb0c85..5572abec 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -34,14 +34,15 @@ class GLVisWindow /// Initializes the visualization and some keys. GLVisWindow(std::string name, int x, int y, int w, int h, bool legacyGlOnly); - void SetVisualizationScene(VisualizationScene * scene, - int view = 3, const char *keys = NULL); + ~GLVisWindow(); - void SetGLVisCommand(GLVisCommand* cmd, communication_thread* cthread); + void InitVisualization(int field_type, StreamState state, + const mfem::Array& input_streams, + bool& keep_attr); void SetFont(const std::string& fn); - VisualizationScene* getScene() { return locscene; } + VisualizationScene* getScene() { return locscene.get(); } GlVisFont* getFont() { return &font; } @@ -138,9 +139,10 @@ class GLVisWindow void ThreadsPauseFunc(GLenum); std::unique_ptr wnd; - VisualizationScene* locscene; - GLVisCommand* glvis_command = nullptr; - communication_thread* comm_thread = nullptr; + std::unique_ptr locscene; + std::unique_ptr glvis_command; + std::unique_ptr comm_thread; + StreamState prob_state; bool use_idle = false; // Idle function callbacks diff --git a/lib/stream_reader.cpp b/lib/stream_reader.cpp index 24ba3d95..f35e5dd7 100644 --- a/lib/stream_reader.cpp +++ b/lib/stream_reader.cpp @@ -356,6 +356,126 @@ ProjectVectorFEGridFunction(std::unique_ptr gf) return gf; } +std::unique_ptr +StreamState::CreateVisualizationScene(int field_type) +{ + std::unique_ptr vs; + double mesh_range = -1.0; + if (field_type == 0 || field_type == 2) + { + if (grid_f) + { + grid_f->GetNodalValues(sol); + } + if (mesh->SpaceDimension() == 2) + { + VisualizationSceneSolution *vss; + if (normals.Size() > 0) + { + vss = new VisualizationSceneSolution(*mesh, sol, &normals); + } + else + { + vss = new VisualizationSceneSolution(*mesh, sol); + } + vs.reset(vss); + if (grid_f) + { + vss->SetGridFunction(*grid_f); + } + if (field_type == 2) + { + vss->OrthogonalProjection = 1; + vss->SetLight(false); + vss->Zoom(1.8); + // Use the 'bone' palette when visualizing a 2D mesh only (otherwise + // the 'jet-like' palette is used in 2D, see vssolution.cpp). + vss->palette.SetIndex(4); + } + } + else if (mesh->SpaceDimension() == 3) + { + VisualizationSceneSolution3d *vss = new VisualizationSceneSolution3d(*mesh, sol); + vs.reset(vss); + if (grid_f) + { + vss->SetGridFunction(grid_f.get()); + } + if (field_type == 2) + { + if (mesh->Dimension() == 3) + { + // Use the 'white' palette when visualizing a 3D volume mesh only + vss->palette.SetIndex(11); + vss->SetLightMatIdx(4); + } + else + { + // Use the 'bone' palette when visualizing a surface mesh only + vss->palette.SetIndex(4); + } + // Otherwise, the 'vivid' palette is used in 3D see vssolution3d.cpp + vss->ToggleDrawAxes(); + vss->ToggleDrawMesh(); + } + } + if (field_type == 2) + { + if (grid_f) + { + mesh_range = grid_f->Max() + 1.0; + } + else + { + mesh_range = sol.Max() + 1.0; + } + } + } + else if (field_type == 1) + { + if (mesh->SpaceDimension() == 2) + { + if (grid_f) + { + vs.reset(new VisualizationSceneVector(*grid_f)); + } + else + { + vs.reset(new VisualizationSceneVector(*mesh, solu, solv)); + } + } + else if (mesh->SpaceDimension() == 3) + { + if (grid_f) + { + grid_f = ProjectVectorFEGridFunction(std::move(grid_f)); + vs.reset(new VisualizationSceneVector3d(*grid_f)); + } + else + { + vs.reset(new VisualizationSceneVector3d(*mesh, solu, solv, solw)); + } + } + } + + if (vs) + { + // increase the refinement factors if visualizing a GridFunction + if (grid_f) + { + vs->AutoRefine(); + vs->SetShading(2, true); + } + if (mesh_range > 0.0) + { + vs->SetValueRange(-mesh_range, mesh_range); + vs->SetAutoscale(0); + } + } + return vs; +} + + bool StreamState::SetNewMeshAndSolution(StreamState new_state, VisualizationScene* vs) { diff --git a/lib/stream_reader.hpp b/lib/stream_reader.hpp index 179e4c75..6956fd6e 100644 --- a/lib/stream_reader.hpp +++ b/lib/stream_reader.hpp @@ -35,6 +35,8 @@ struct StreamState int ReadStream(std::istream &is, const std::string &data_type); + std::unique_ptr CreateVisualizationScene(int field_type); + /// Sets a new mesh and solution from another StreamState object, and /// updates the given VisualizationScene pointer with the new data. /// diff --git a/lib/threads.cpp b/lib/threads.cpp index 9f392c3e..b7361226 100644 --- a/lib/threads.cpp +++ b/lib/threads.cpp @@ -22,10 +22,9 @@ using namespace std; extern const char *strings_off_on[]; // defined in vsdata.cpp GLVisCommand::GLVisCommand( - VisualizationSceneScalarData **_vs, StreamState& state, bool *_keep_attr) - : curr_state(state) + GLVisWindow* wnd, StreamState& state, bool *_keep_attr) + : window(wnd), curr_state(state) { - vs = _vs; keep_attr = _keep_attr; num_waiting = 0; @@ -73,7 +72,7 @@ int GLVisCommand::signal() return -1; } - SdlWindow *sdl_window = GetAppWindow(); + SdlWindow *sdl_window = window->getSdl(); if (sdl_window) { sdl_window->signalLoop(); @@ -420,6 +419,9 @@ int GLVisCommand::Execute() return -1; } + VisualizationSceneScalarData* vs + = dynamic_cast(window->getScene()); + switch (command) { case NO_COMMAND: @@ -434,13 +436,13 @@ int GLVisCommand::Execute() new_state.SetMeshSolution(); mesh_range = new_state.grid_f->Max() + 1.0; } - if (curr_state.SetNewMeshAndSolution(std::move(new_state), *vs)) + if (curr_state.SetNewMeshAndSolution(std::move(new_state), vs)) { if (mesh_range > 0.0) { - (*vs)->SetValueRange(-mesh_range, mesh_range); + vs->SetValueRange(-mesh_range, mesh_range); } - MyExpose(); + window->MyExpose(); } else { @@ -449,7 +451,7 @@ int GLVisCommand::Execute() if (autopause) { cout << "Autopause ..." << endl; - GetGLVisWindow()->ThreadsStop(); + window->ThreadsStop(); } break; } @@ -457,7 +459,7 @@ int GLVisCommand::Execute() case SCREENSHOT: { cout << "Command: screenshot: " << flush; - GetGLVisWindow()->Screenshot(screenshot_filename.c_str()); + window->Screenshot(screenshot_filename.c_str()); cout << "-> " << screenshot_filename << endl; break; } @@ -466,15 +468,15 @@ int GLVisCommand::Execute() { cout << "Command: keys: '" << key_commands << "'" << endl; // SendKeySequence(key_commands.c_str()); - GetGLVisWindow()->CallKeySequence(key_commands.c_str()); - MyExpose(); + window->CallKeySequence(key_commands.c_str()); + window->MyExpose(); break; } case WINDOW_SIZE: { cout << "Command: window_size: " << window_w << " x " << window_h << endl; - GetGLVisWindow()->ResizeWindow(window_w, window_h); + window->ResizeWindow(window_w, window_h); break; } @@ -483,14 +485,14 @@ int GLVisCommand::Execute() cout << "Command: window_geometry: " << "@(" << window_x << "," << window_y << ") " << window_w << " x " << window_h << endl; - GetGLVisWindow()->MoveResizeWindow(window_x, window_y, window_w, window_h); + window->MoveResizeWindow(window_x, window_y, window_w, window_h); break; } case WINDOW_TITLE: { cout << "Command: window_title: " << window_title << endl; - GetGLVisWindow()->SetWindowTitle(window_title.c_str()); + window->SetWindowTitle(window_title.c_str()); break; } @@ -498,8 +500,8 @@ int GLVisCommand::Execute() { cout << "Command: plot_caption: " << plot_caption << endl; ::plot_caption = plot_caption; - (*vs)->PrepareCaption(); // turn on or off the caption - MyExpose(); + vs->PrepareCaption(); // turn on or off the caption + window->MyExpose(); break; } @@ -507,16 +509,16 @@ int GLVisCommand::Execute() { cout << "Command: axis_labels: '" << axis_label_x << "' '" << axis_label_y << "' '" << axis_label_z << "'" << endl; - (*vs)->SetAxisLabels(axis_label_x.c_str(), axis_label_y.c_str(), + vs->SetAxisLabels(axis_label_x.c_str(), axis_label_y.c_str(), axis_label_z.c_str()); - MyExpose(); + window->MyExpose(); break; } case PAUSE: { cout << "Command: pause: "; - GetGLVisWindow()->ToggleThreads(); + window->ToggleThreads(); break; } @@ -524,34 +526,34 @@ int GLVisCommand::Execute() { cout << "Command: view: " << view_ang_theta << ' ' << view_ang_phi << endl; - (*vs)->SetView(view_ang_theta, view_ang_phi); - MyExpose(); + vs->SetView(view_ang_theta, view_ang_phi); + window->MyExpose(); break; } case ZOOM: { cout << "Command: zoom: " << zoom_factor << endl; - (*vs)->Zoom(zoom_factor); - MyExpose(); + vs->Zoom(zoom_factor); + window->MyExpose(); break; } case SUBDIVISIONS: { cout << "Command: subdivisions: " << flush; - (*vs)->SetRefineFactors(subdiv_tot, subdiv_bdr); + vs->SetRefineFactors(subdiv_tot, subdiv_bdr); cout << subdiv_tot << ' ' << subdiv_bdr << endl; - MyExpose(); + window->MyExpose(); break; } case VALUE_RANGE: { cout << "Command: valuerange: " << flush; - (*vs)->SetValueRange(val_min, val_max); + vs->SetValueRange(val_min, val_max); cout << val_min << ' ' << val_max << endl; - MyExpose(); + window->MyExpose(); break; } @@ -573,9 +575,9 @@ int GLVisCommand::Execute() } if (s != -1) { - (*vs)->SetShading(s, false); + vs->SetShading(s, false); cout << shading << endl; - MyExpose(); + window->MyExpose(); } else { @@ -588,9 +590,9 @@ int GLVisCommand::Execute() { cout << "Command: viewcenter: " << view_center_x << ' ' << view_center_y << endl; - (*vs)->ViewCenterX = view_center_x; - (*vs)->ViewCenterY = view_center_y; - MyExpose(); + vs->ViewCenterX = view_center_x; + vs->ViewCenterY = view_center_y; + window->MyExpose(); break; } @@ -599,19 +601,19 @@ int GLVisCommand::Execute() cout << "Command: autoscale: " << autoscale_mode; if (autoscale_mode == "off") { - (*vs)->SetAutoscale(0); + vs->SetAutoscale(0); } else if (autoscale_mode == "on") { - (*vs)->SetAutoscale(1); + vs->SetAutoscale(1); } else if (autoscale_mode == "value") { - (*vs)->SetAutoscale(2); + vs->SetAutoscale(2); } else if (autoscale_mode == "mesh") { - (*vs)->SetAutoscale(3); + vs->SetAutoscale(3); } else { @@ -624,18 +626,18 @@ int GLVisCommand::Execute() case PALETTE: { cout << "Command: palette: " << palette << endl; - (*vs)->palette.SetIndex(palette-1); - MyExpose(); + vs->palette.SetIndex(palette-1); + window->MyExpose(); break; } case PALETTE_REPEAT: { cout << "Command: palette_repeat: " << palette_repeat << endl; - (*vs)->palette.SetRepeatTimes(palette_repeat); - (*vs)->palette.Init(); + vs->palette.SetRepeatTimes(palette_repeat); + vs->palette.Init(); - MyExpose(); + window->MyExpose(); break; } @@ -647,8 +649,8 @@ int GLVisCommand::Execute() cout << ' ' << camera[i]; } cout << endl; - (*vs)->cam.Set(camera); - MyExpose(); + vs->cam.Set(camera); + window->MyExpose(); break; } @@ -665,11 +667,11 @@ int GLVisCommand::Execute() cout << "Command: autopause: " << strings_off_on[autopause] << endl; if (autopause) { - GetGLVisWindow()->ThreadsStop(); + window->ThreadsStop(); } else { - GetGLVisWindow()->ThreadsRun(); // probably not needed + window->ThreadsRun(); // probably not needed } break; } @@ -716,11 +718,11 @@ void GLVisCommand::ToggleAutopause() cout << "Autopause: " << strings_off_on[autopause] << endl; if (autopause) { - GetGLVisWindow()->ThreadsStop(); + window->ThreadsStop(); } else { - GetGLVisWindow()->ThreadsRun(); + window->ThreadsRun(); } } @@ -736,7 +738,7 @@ GLVisCommand::~GLVisCommand() } communication_thread::communication_thread(GLVisCommand* parent_cmd, - Array &_is) + const Array &_is) : is(_is), glvis_command(parent_cmd) { new_m = NULL; diff --git a/lib/threads.hpp b/lib/threads.hpp index cb19e83f..bc8887c2 100644 --- a/lib/threads.hpp +++ b/lib/threads.hpp @@ -19,11 +19,13 @@ #include #include +class GLVisWindow; + class GLVisCommand { private: // Pointers to global GLVis data - VisualizationSceneScalarData **vs; + GLVisWindow* window; StreamState& curr_state; bool *keep_attr; @@ -93,7 +95,7 @@ class GLVisCommand public: // called by the main execution thread - GLVisCommand(VisualizationSceneScalarData **_vs, + GLVisCommand(GLVisWindow* parent, StreamState& thread_state, bool *_keep_attr); // to be used by the main execution (visualization) thread @@ -142,7 +144,7 @@ class communication_thread { private: // streams to read data from - Array &is; + Array is; GLVisCommand* glvis_command; @@ -159,7 +161,7 @@ class communication_thread void execute(); public: - communication_thread(GLVisCommand* parent_cmd, Array &_is); + communication_thread(GLVisCommand* parent_cmd, const Array &_is); ~communication_thread(); }; From dfe6aca85945520a8cdd8bcb9808fcec5e5e53af Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 4 Feb 2021 12:22:21 -0800 Subject: [PATCH 17/39] JS-related build fixes --- glvis.cpp | 2 +- lib/aux_js.cpp | 143 ++---------------------------------------------- lib/aux_vis.cpp | 26 ++++++++- lib/aux_vis.hpp | 4 +- 4 files changed, 33 insertions(+), 142 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index ddfb0c55..04803df6 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -132,7 +132,7 @@ bool GLVisInitVis(int field_type) } mainWindow->InitVisualization(field_type, std::move(stream_state), - input_streams, keep_attr); + keep_attr, input_streams); return true; } diff --git a/lib/aux_js.cpp b/lib/aux_js.cpp index a91a17a1..97137bba 100644 --- a/lib/aux_js.cpp +++ b/lib/aux_js.cpp @@ -23,6 +23,7 @@ mfem::GeometryRefiner GLVisGeometryRefiner; // used in extern context static VisualizationSceneScalarData * vs = nullptr; StreamState stream_state; GLVisWindow * mainWindow = nullptr; +bool keep_attr = false; namespace js { @@ -37,9 +38,6 @@ bool startVisualization(const std::string input, const std::string data_type, // 0 - scalar data, 1 - vector data, 2 - mesh only, (-1) - unknown const int field_type = stream_state.ReadStream(ss, data_type); - // reset antialiasing - GetAppWindow()->getRenderer().setAntialiasing(0); - std::string line; double minv = 0.0, maxv = 0.0; while (ss >> line) @@ -86,140 +84,12 @@ bool startVisualization(const std::string input, const std::string data_type, vs = nullptr; double mesh_range = -1.0; - if (field_type == 0 || field_type == 2) - { - if (stream_state.grid_f) - { - stream_state.grid_f->GetNodalValues(stream_state.sol); - } - if (stream_state.mesh->SpaceDimension() == 2) - { - VisualizationSceneSolution * vss; - if (stream_state.normals.Size() > 0) - { - vs = vss = new VisualizationSceneSolution(*stream_state.mesh, stream_state.sol, - &stream_state.normals); - } - else - { - vs = vss = new VisualizationSceneSolution(*stream_state.mesh, stream_state.sol); - } - if (stream_state.grid_f) - { - vss->SetGridFunction(*stream_state.grid_f); - } - if (field_type == 2) - { - vs->OrthogonalProjection = 1; - vs->SetLight(0); - vs->Zoom(1.8); - // Use the 'bone' palette when visualizing a 2D mesh only (otherwise - // the 'jet-like' palette is used in 2D, see vssolution.cpp). - vs->palette.SetIndex(4); - } - } - else if (stream_state.mesh->SpaceDimension() == 3) - { - VisualizationSceneSolution3d * vss; - vs = vss = new VisualizationSceneSolution3d(*stream_state.mesh, - stream_state.sol); - if (stream_state.grid_f) - { - vss->SetGridFunction(stream_state.grid_f.get()); - } - if (field_type == 2) - { - if (stream_state.mesh->Dimension() == 3) - { - // Use the 'white' palette when visualizing a 3D volume mesh only - // vs->palette.SetIndex(4); - vs->palette.SetIndex(11); - vss->SetLightMatIdx(4); - } - else - { - // Use the 'bone' palette when visualizing a surface mesh only - // (the same as when visualizing a 2D mesh only) - vs->palette.SetIndex(4); - } - // Otherwise, the 'vivid' palette is used in 3D see vssolution3d.cpp - - vss->ToggleDrawAxes(); - vss->ToggleDrawMesh(); - } - } - if (field_type == 2) - { - if (stream_state.grid_f) - { - mesh_range = stream_state.grid_f->Max() + 1.0; - } - else - { - mesh_range = stream_state.sol.Max() + 1.0; - } - } - } - else if (field_type == 1) - { - if (stream_state.mesh->SpaceDimension() == 2) - { - if (stream_state.grid_f) - { - vs = new VisualizationSceneVector(*stream_state.grid_f); - } - else - { - vs = new VisualizationSceneVector(*stream_state.mesh, stream_state.solu, - stream_state.solv); - } - } - else if (stream_state.mesh->SpaceDimension() == 3) - { - if (stream_state.grid_f) - { - stream_state.grid_f - = ProjectVectorFEGridFunction(std::move(stream_state.grid_f)); - vs = new VisualizationSceneVector3d(*stream_state.grid_f); - } - else - { - vs = new VisualizationSceneVector3d(*stream_state.mesh, stream_state.solu, - stream_state.solv, stream_state.solw); - } - } - } - - if (vs) - { - // increase the refinement factors if visualizing a GridFunction - if (stream_state.grid_f) - { - vs->AutoRefine(); - vs->SetShading(2, true); - } - if (mesh_range > 0.0) - { - vs->SetValueRange(-mesh_range, mesh_range); - vs->SetAutoscale(0); - } - if (stream_state.mesh->SpaceDimension() == 2 && field_type == 2) - { - mainWindow->SetVisualizationScene(vs, 2); - } - else - { - mainWindow->SetVisualizationScene(vs, 3); - } - } - - mainWindow->CallKeySequence(stream_state.keys.c_str()); + mainWindow->InitVisualization(field_type, std::move(stream_state), keep_attr); if (minv || maxv) { vs->SetValueRange(minv, maxv); } - mainWindow->SendExposeEvent(); return true; } @@ -246,8 +116,7 @@ int updateVisualization(std::string data_type, std::string stream) { vs->SetValueRange(-mesh_range, mesh_range); } - - SendExposeEvent(); + mainWindow->SendExposeEvent(); return 0; } else @@ -331,18 +200,18 @@ void ResizeWindow(int w, int h) int GetUseTexture() { - return vs->GetPalette().GetSmoothSetting(); + return vs->palette.GetSmoothSetting(); } void SetUseTexture(int ut) { if (ut == 0) { - vs->GetPalette().UseDiscrete(); + vs->palette.UseDiscrete(); } else { - vs->GetPalette().UseSmooth(); + vs->palette.UseSmooth(); } } diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 9338268b..e4c8fadc 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -227,22 +227,26 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega GLVisWindow::~GLVisWindow() { +#ifndef __EMSCRIPTEN__ if (glvis_command) { glvis_command->Terminate(); } +#endif } void GLVisWindow::InitVisualization(int field_type, StreamState state, - const mfem::Array& input_streams, - bool& keep_attr) + bool& keep_attr, + const mfem::Array& input_streams) { prob_state = std::move(state); +#ifndef __EMSCRIPTEN__ if (input_streams.Size() > 0) { glvis_command.reset(new GLVisCommand(this, prob_state, &keep_attr)); comm_thread.reset(new communication_thread(glvis_command.get(), input_streams)); } +#endif locscene = prob_state.CreateVisualizationScene(field_type); @@ -261,6 +265,11 @@ void GLVisWindow::InitVisualization(int field_type, StreamState state, { AddIdleFunc(::MainLoop); } + + if (state.keys == "") + { + CallKeySequence(state.keys.c_str()); + } } void GLVisWindow::SetKeyEventHandler(int key, void (GLVisWindow::*handler)()) @@ -498,6 +507,7 @@ bool GLVisWindow::CommunicationIdleFunc() bool GLVisWindow::MainIdleFunc() { bool sleep = true; +#ifndef __EMSCRIPTEN__ if (glvis_command && visualize == 1 && !(idle_funcs.Size() > 0 && use_idle)) { @@ -520,6 +530,18 @@ bool GLVisWindow::MainIdleFunc() sleep = false; } use_idle = !use_idle; +#else + if (idle_funcs.Size() > 0) + { + last_idle_func = (last_idle_func + 1) % idle_funcs.Size(); + if (idle_funcs[last_idle_func]) + { + (*idle_funcs[last_idle_func])(this); + } + // Continue executing idle functions + sleep = false; + } +#endif return sleep; } diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 5572abec..46b97f5f 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -37,8 +37,8 @@ class GLVisWindow ~GLVisWindow(); void InitVisualization(int field_type, StreamState state, - const mfem::Array& input_streams, - bool& keep_attr); + bool& keep_attr, + const mfem::Array& input_streams = {}); void SetFont(const std::string& fn); From 7c4b7a0229541d7bbf86af1f274937783ccab01f Mon Sep 17 00:00:00 2001 From: Max Yang Date: Wed, 10 Feb 2021 10:32:27 -0800 Subject: [PATCH 18/39] Remove global vsdata dependence from Plane class --- lib/vsdata.cpp | 9 +++++---- lib/vsdata.hpp | 3 ++- lib/vssolution.cpp | 3 ++- lib/vssolution3d.cpp | 3 ++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/vsdata.cpp b/lib/vsdata.cpp index 8b5f31c0..9dce751f 100644 --- a/lib/vsdata.cpp +++ b/lib/vsdata.cpp @@ -1577,7 +1577,8 @@ void VisualizationSceneScalarData::ComputeElemAttrCenter() } -Plane::Plane(double A,double B,double C,double D) +Plane::Plane(double A,double B,double C,double D, + double (&bbx)[2], double (&bby)[2], double (&bbz)[2]) { eqn[0] = A; eqn[1] = B; @@ -1586,9 +1587,9 @@ Plane::Plane(double A,double B,double C,double D) CartesianToSpherical(); - double x[2] = {vsdata -> x[0], vsdata -> x[1]}; - double y[2] = {vsdata -> y[0], vsdata -> y[1]}; - double z[2] = {vsdata -> z[0], vsdata -> z[1]}; + double x[2] = {bbx[0], bbx[1]}; + double y[2] = {bby[0], bby[1]}; + double z[2] = {bbz[0], bbz[1]}; bbox_diam = sqrt ( (x[1]-x[0])*(x[1]-x[0]) + (y[1]-y[0])*(y[1]-y[0]) + (z[1]-z[0])*(z[1]-z[0]) ); diff --git a/lib/vsdata.hpp b/lib/vsdata.hpp index c5fbd3c5..ef67d41d 100644 --- a/lib/vsdata.hpp +++ b/lib/vsdata.hpp @@ -36,7 +36,8 @@ class Plane double phi_step, theta_step, rho_step; public: - Plane(double A,double B,double C,double D); + Plane(double A,double B,double C,double D, + double (&bbx)[2], double (&bby)[2], double (&bbz)[2]); inline double * Equation() { return eqn; } inline double Transform(double x, double y, double z) { return eqn[0]*x+eqn[1]*y+eqn[2]*z+eqn[3]; } diff --git a/lib/vssolution.cpp b/lib/vssolution.cpp index cff12196..44eb6b32 100644 --- a/lib/vssolution.cpp +++ b/lib/vssolution.cpp @@ -479,7 +479,8 @@ void VisualizationSceneSolution::Init() palette.SetIndex(2); // use the 'jet-like' palette in 2D double eps = 1e-6; // move the cutting plane a bit to avoid artifacts - CuttingPlane = new Plane(-1.0,0.0,0.0,(0.5-eps)*x[0]+(0.5+eps)*x[1]); + CuttingPlane = new Plane(-1.0,0.0,0.0,(0.5-eps)*x[0]+(0.5+eps)*x[1], + x, y, z); draw_cp = 0; // static int init = 0; diff --git a/lib/vssolution3d.cpp b/lib/vssolution3d.cpp index b3369a06..d634e6b2 100644 --- a/lib/vssolution3d.cpp +++ b/lib/vssolution3d.cpp @@ -696,7 +696,8 @@ void VisualizationSceneSolution3d::Init() palette.SetIndex(12); // use the 'vivid' palette in 3D double eps = 1e-6; // move the cutting plane a bit to avoid artifacts - CuttingPlane = new Plane(-1.0,0.0,0.0,(0.5-eps)*x[0]+(0.5+eps)*x[1]); + CuttingPlane = new Plane(-1.0,0.0,0.0,(0.5-eps)*x[0]+(0.5+eps)*x[1], + x, y, z); nlevels = 1; From 15a2a74259eeec7e8bbeedafbaf122f3a4ccd0e8 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Wed, 10 Feb 2021 14:38:55 -0800 Subject: [PATCH 19/39] Start moving over some key events to member functions --- glvis.cpp | 2 +- lib/aux_vis.hpp | 30 +++++++- lib/openglvis.cpp | 38 ++++++++++ lib/openglvis.hpp | 5 ++ lib/vsdata.cpp | 177 +++++++++++-------------------------------- lib/vssolution.cpp | 88 +++++++-------------- lib/vssolution3d.cpp | 104 ++++++++----------------- lib/vsvector.cpp | 63 ++++++--------- lib/vsvector.hpp | 9 ++- lib/vsvector3d.cpp | 108 ++++++++++---------------- lib/vsvector3d.hpp | 9 ++- 11 files changed, 254 insertions(+), 379 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 04803df6..268428be 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -725,7 +725,7 @@ void PlayScript(istream &scr) if (GLVisInitVis((stream_state.grid_f->VectorDim() == 1) ? 0 : 1)) { - mainWindow->AddWindowEvent(SDLK_SPACE, ScriptControl, false); + mainWindow->AddKeyEvent(SDLK_SPACE, ScriptControl, false); GLVisStartVis(); } diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 46b97f5f..e5f07836 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -93,7 +93,25 @@ class GLVisWindow void ResizeWindow(int w, int h); void SetWindowTitle(const char *title); - void AddWindowEvent(int key, void (*eh)(GLVisWindow*), bool exposeAfter = true) + template + void AddKeyEvent(int key, void(*eh)(Args...)) + { + wnd->setOnKeyDown(key, eh); + } + + template + void AddKeyEvent(int key, void (TScene::*eh)(), bool exposeAfter = true) + { + auto wrapped_eh = [this, eh, exposeAfter](GLenum e) + { + TScene* pScene = dynamic_cast(locscene.get()); + (pScene->*eh)(); + if (exposeAfter) { SendExposeEvent(); } + }; + wnd->setOnKeyDown(key, wrapped_eh); + } + + void AddKeyEvent(int key, void (*eh)(GLVisWindow*), bool exposeAfter = true) { auto wrapped_eh = [this, eh, exposeAfter](GLenum e) { @@ -102,6 +120,16 @@ class GLVisWindow }; wnd->setOnKeyDown(key, wrapped_eh); } + + void AddKeyEvent(int key, void (*eh)(GLVisWindow*, GLenum), bool exposeAfter = true) + { + auto wrapped_eh = [this, eh, exposeAfter](GLenum e) + { + (*eh)(this, e); + if (exposeAfter) { SendExposeEvent(); } + }; + wnd->setOnKeyDown(key, wrapped_eh); + } private: void InitFont(); bool SetFont(const vector& patterns, int height); diff --git a/lib/openglvis.cpp b/lib/openglvis.cpp index c70cf378..e2cf8754 100644 --- a/lib/openglvis.cpp +++ b/lib/openglvis.cpp @@ -465,3 +465,41 @@ glm::mat4 VisualizationScene::GetModelViewMtx() return modelView.mtx; } +void VisualizationScene::DecrementAlpha() +{ + matAlpha -= 0.05; + if (matAlpha < 0.0) + { + matAlpha = 0.0; + } + GenerateAlphaTexture(); +} + +void VisualizationScene::IncrementAlpha() +{ + matAlpha += 0.05; + if (matAlpha > 1.0) + { + matAlpha = 1.0; + } + GenerateAlphaTexture(); +} + +void VisualizationScene::DecrementAlphaCenter() +{ + matAlphaCenter -= 0.25; + GenerateAlphaTexture(); +#ifdef GLVIS_DEBUG + cout << "MatAlphaCenter = " << matAlphaCenter << endl; +#endif +} + +void VisualizationScene::IncrementAlphaCenter() +{ + matAlphaCenter += 0.25; + GenerateAlphaTexture(); +#ifdef GLVIS_DEBUG + cout << "MatAlphaCenter = " << matAlphaCenter << endl; +#endif +} + diff --git a/lib/openglvis.hpp b/lib/openglvis.hpp index c2dd58b8..8a2378db 100644 --- a/lib/openglvis.hpp +++ b/lib/openglvis.hpp @@ -193,6 +193,11 @@ class VisualizationScene void GenerateAlphaTexture() { palette.GenerateAlphaTexture(matAlpha, matAlphaCenter); } + void DecrementAlpha(); + void IncrementAlpha(); + void DecrementAlphaCenter(); + void IncrementAlphaCenter(); + /// This is set by SetVisualizationScene int view; }; diff --git a/lib/vsdata.cpp b/lib/vsdata.cpp index 9dce751f..5ac32448 100644 --- a/lib/vsdata.cpp +++ b/lib/vsdata.cpp @@ -464,13 +464,6 @@ void VisualizationSceneScalarData::PrepareCaption() } VisualizationSceneScalarData * vsdata; -extern VisualizationScene * locscene; - -void KeycPressed() -{ - vsdata->ToggleDrawColorbar(); - SendExposeEvent(); -} void KeyCPressed() { @@ -480,20 +473,10 @@ void KeyCPressed() SendExposeEvent(); } -void KeySPressed() -{ - vsdata -> ToggleScaling(); - SendExposeEvent(); -} - -void KeyaPressed() -{ - vsdata -> ToggleDrawAxes(); - SendExposeEvent(); -} - -void Key_Mod_a_Pressed(GLenum state) +void Key_Mod_a_Pressed(GLVisWindow* wnd, GLenum state) { + VisualizationSceneScalarData * vsdata + = dynamic_cast(wnd->getScene()); if (state & KMOD_CTRL) { static const char *autoscale_modes[] = { "off", "on", "value", "mesh" }; @@ -516,12 +499,6 @@ void KeyHPressed() cout << vsdata->GetHelpString() << flush; } -void KeylPressed() -{ - vsdata -> ToggleLight(); - SendExposeEvent(); -} - void KeyLPressed() { vsdata->ToggleLogscale(true); @@ -530,22 +507,22 @@ void KeyLPressed() void KeyrPressed() { - locscene -> spinning = 0; + vsdata -> spinning = 0; GetGLVisWindow()->RemoveIdleFunc(MainLoop); vsdata -> CenterObject(); - locscene -> ViewAngle = 45.0; - locscene -> ViewScale = 1.0; - locscene -> ViewCenterX = 0.0; - locscene -> ViewCenterY = 0.0; - locscene->cam.Reset(); + vsdata -> ViewAngle = 45.0; + vsdata -> ViewScale = 1.0; + vsdata -> ViewCenterX = 0.0; + vsdata -> ViewCenterY = 0.0; + vsdata->cam.Reset(); vsdata -> key_r_state = 0; SendExposeEvent(); } void KeyRPressed() { - locscene->spinning = 0; + vsdata->spinning = 0; GetGLVisWindow()->RemoveIdleFunc(MainLoop); vsdata->Toggle2DView(); SendExposeEvent(); @@ -559,14 +536,14 @@ void KeypPressed(GLenum state) } else { - locscene->palette.NextIndex(); + vsdata->palette.NextIndex(); SendExposeEvent(); } } void KeyPPressed() { - locscene->palette.PrevIndex(); + vsdata->palette.PrevIndex(); SendExposeEvent(); } @@ -705,11 +682,6 @@ void KeyGPressed() SendExposeEvent(); } -void KeyF1Pressed() -{ - vsdata->PrintState(); -} - void KeyF2Pressed() { vsdata -> EventUpdateColors(); @@ -718,68 +690,6 @@ void KeyF2Pressed() SendExposeEvent(); } -void KeykPressed() -{ - locscene->matAlpha -= 0.05; - if (locscene->matAlpha < 0.0) - { - locscene->matAlpha = 0.0; - } - locscene->GenerateAlphaTexture(); - SendExposeEvent(); -} - -void KeyKPressed() -{ - locscene->matAlpha += 0.05; - if (locscene->matAlpha > 1.0) - { - locscene->matAlpha = 1.0; - } - locscene->GenerateAlphaTexture(); - SendExposeEvent(); -} - -void KeyCommaPressed() -{ - locscene->matAlphaCenter -= 0.25; - //vsdata -> EventUpdateColors(); - locscene->GenerateAlphaTexture(); - SendExposeEvent(); -#ifdef GLVIS_DEBUG - cout << "MatAlphaCenter = " << locscene->matAlphaCenter << endl; -#endif -} - -void KeyLessPressed() -{ - locscene->matAlphaCenter += 0.25; - //vsdata -> EventUpdateColors(); - locscene->GenerateAlphaTexture(); - SendExposeEvent(); -#ifdef GLVIS_DEBUG - cout << "MatAlphaCenter = " << locscene->matAlphaCenter << endl; -#endif -} - -void KeyGravePressed() -{ - vsdata->ToggleRuler(); - SendExposeEvent(); -} - -void KeyTildePressed() -{ - vsdata->RulerPosition(); - SendExposeEvent(); -} - -void KeyToggleTexture() -{ - vsdata->ToggleTexture(); - SendExposeEvent(); -} - void VisualizationSceneScalarData::PrintLogscale(bool warn) { if (warn) @@ -1091,50 +1001,53 @@ void VisualizationSceneScalarData::Init() // if (!init) { // init = 1; + GLVisWindow* wnd = GetGLVisWindow(); + + using SceneType = VisualizationSceneScalarData; - wnd->setOnKeyDown('l', KeylPressed); - wnd->setOnKeyDown('L', KeyLPressed); + wnd->AddKeyEvent('l', &SceneType::ToggleLight); + wnd->AddKeyEvent('L', KeyLPressed); - wnd->setOnKeyDown('s', KeySPressed); + wnd->AddKeyEvent('s', &SceneType::ToggleScaling); - // wnd->setOnKeyDown('a', KeyaPressed); - wnd->setOnKeyDown('a', Key_Mod_a_Pressed); + // wnd->AddKeyEvent('a', KeyaPressed); + wnd->AddKeyEvent('a', Key_Mod_a_Pressed); - wnd->setOnKeyDown('r', KeyrPressed); - wnd->setOnKeyDown('R', KeyRPressed); + wnd->AddKeyEvent('r', KeyrPressed); + wnd->AddKeyEvent('R', KeyRPressed); - wnd->setOnKeyDown('p', KeypPressed); - wnd->setOnKeyDown('P', KeyPPressed); + wnd->AddKeyEvent('p', KeypPressed); + wnd->AddKeyEvent('P', KeyPPressed); - wnd->setOnKeyDown('h', KeyHPressed); - wnd->setOnKeyDown('H', KeyHPressed); + wnd->AddKeyEvent('h', KeyHPressed); + wnd->AddKeyEvent('H', KeyHPressed); - wnd->setOnKeyDown(SDLK_F5, KeyF5Pressed); - wnd->setOnKeyDown(SDLK_F6, KeyF6Pressed); - wnd->setOnKeyDown(SDLK_F7, KeyF7Pressed); + wnd->AddKeyEvent(SDLK_F5, KeyF5Pressed); + wnd->AddKeyEvent(SDLK_F6, KeyF6Pressed); + wnd->AddKeyEvent(SDLK_F7, KeyF7Pressed); - wnd->setOnKeyDown(SDLK_BACKSLASH, KeyBackslashPressed); - wnd->setOnKeyDown('t', KeyTPressed); - wnd->setOnKeyDown('T', KeyTPressed); + wnd->AddKeyEvent(SDLK_BACKSLASH, KeyBackslashPressed); + wnd->AddKeyEvent('t', KeyTPressed); + wnd->AddKeyEvent('T', KeyTPressed); - wnd->setOnKeyDown('g', KeyGPressed); - wnd->setOnKeyDown('G', KeyGPressed); + wnd->AddKeyEvent('g', KeyGPressed); + wnd->AddKeyEvent('G', KeyGPressed); - wnd->setOnKeyDown('c', KeycPressed); - wnd->setOnKeyDown('C', KeyCPressed); + wnd->AddKeyEvent('c', &SceneType::ToggleDrawColorbar); + wnd->AddKeyEvent('C', KeyCPressed); - wnd->setOnKeyDown('k', KeykPressed); - wnd->setOnKeyDown('K', KeyKPressed); + wnd->AddKeyEvent('k', &SceneType::DecrementAlpha); + wnd->AddKeyEvent('K', &SceneType::IncrementAlpha); - wnd->setOnKeyDown(SDLK_F1, KeyF1Pressed); - wnd->setOnKeyDown(SDLK_F2, KeyF2Pressed); + wnd->AddKeyEvent(SDLK_F1, &SceneType::PrintState, false); + wnd->AddKeyEvent(SDLK_F2, KeyF2Pressed); - wnd->setOnKeyDown(SDLK_COMMA, KeyCommaPressed); - wnd->setOnKeyDown(SDLK_LESS, KeyLessPressed); - wnd->setOnKeyDown('~', KeyTildePressed); - wnd->setOnKeyDown('`', KeyGravePressed); + wnd->AddKeyEvent(SDLK_COMMA, &SceneType::DecrementAlphaCenter); + wnd->AddKeyEvent(SDLK_LESS, &SceneType::IncrementAlphaCenter); + wnd->AddKeyEvent('~', &SceneType::RulerPosition); + wnd->AddKeyEvent('`', &SceneType::ToggleRuler); - wnd->setOnKeyDown(SDLK_EXCLAIM, KeyToggleTexture); + wnd->AddKeyEvent(SDLK_EXCLAIM, &SceneType::ToggleTexture); } //Set_Light(); diff --git a/lib/vssolution.cpp b/lib/vssolution.cpp index 44eb6b32..c453286b 100644 --- a/lib/vssolution.cpp +++ b/lib/vssolution.cpp @@ -219,24 +219,6 @@ static void KeyF10Pressed(GLenum state) } } -static void KeyBPressed() -{ - vssol -> ToggleDrawBdr(); - SendExposeEvent(); -} - -static void KeyMPressed() -{ - vssol -> ToggleDrawMesh(); - SendExposeEvent(); -} - -static void KeyNPressed() -{ - vssol -> ToggleDrawNumberings(); - SendExposeEvent(); -} - int refine_func = 0; static void KeyoPressed(GLenum state) @@ -319,24 +301,6 @@ static void KeyOPressed(GLenum state) } } -static void KeyEPressed() -{ - vssol -> ToggleDrawElems(); - SendExposeEvent(); -} - -static void KeyFPressed() -{ - vssol -> ToggleShading(); - SendExposeEvent(); -} - -void KeyiPressed() -{ - vssol->ToggleDrawCP(); - SendExposeEvent(); -} - void KeyIPressed() { // no-op, available @@ -487,40 +451,42 @@ void VisualizationSceneSolution::Init() // if (!init) { // init = 1; + GLVisWindow* wnd = GetGLVisWindow(); + using SceneType = VisualizationSceneSolution; - wnd->setOnKeyDown('b', KeyBPressed); - wnd->setOnKeyDown('B', KeyBPressed); + wnd->AddKeyEvent('b', &SceneType::ToggleDrawBdr); + wnd->AddKeyEvent('B', &SceneType::ToggleDrawBdr); - wnd->setOnKeyDown('m', KeyMPressed); - wnd->setOnKeyDown('M', KeyMPressed); + wnd->AddKeyEvent('m', &SceneType::ToggleDrawMesh); + wnd->AddKeyEvent('M', &SceneType::ToggleDrawMesh); - wnd->setOnKeyDown('n', KeyNPressed); - wnd->setOnKeyDown('N', KeyNPressed); + wnd->AddKeyEvent('n', &SceneType::ToggleDrawNumberings); + wnd->AddKeyEvent('N', &SceneType::ToggleDrawNumberings); - wnd->setOnKeyDown('o', KeyoPressed); - wnd->setOnKeyDown('O', KeyOPressed); + wnd->AddKeyEvent('o', KeyoPressed); + wnd->AddKeyEvent('O', KeyOPressed); - wnd->setOnKeyDown('e', KeyEPressed); - wnd->setOnKeyDown('E', KeyEPressed); + wnd->AddKeyEvent('e', &SceneType::ToggleDrawElems); + wnd->AddKeyEvent('E', &SceneType::ToggleDrawElems); - wnd->setOnKeyDown('f', KeyFPressed); - wnd->setOnKeyDown('F', KeyFPressed); + wnd->AddKeyEvent('f', &SceneType::ToggleShading); + wnd->AddKeyEvent('F', &SceneType::ToggleShading); - wnd->setOnKeyDown('i', KeyiPressed); - wnd->setOnKeyDown('I', KeyIPressed); + wnd->AddKeyEvent('i', &SceneType::ToggleDrawCP); + //wnd->AddKeyEvent('I', KeyIPressed); - wnd->setOnKeyDown('y', KeyyPressed); - wnd->setOnKeyDown('Y', KeyYPressed); - wnd->setOnKeyDown('z', KeyzPressed); - wnd->setOnKeyDown('Z', KeyZPressed); + wnd->AddKeyEvent('y', KeyyPressed); + wnd->AddKeyEvent('Y', KeyYPressed); + wnd->AddKeyEvent('z', KeyzPressed); + wnd->AddKeyEvent('Z', KeyZPressed); - wnd->setOnKeyDown(SDLK_F3, KeyF3Pressed); - wnd->setOnKeyDown(SDLK_F4, KeyF4Pressed); - wnd->setOnKeyDown(SDLK_F8, KeyF8Pressed); - wnd->setOnKeyDown(SDLK_F9, KeyF9Pressed); - wnd->setOnKeyDown(SDLK_F10, KeyF10Pressed); - wnd->setOnKeyDown(SDLK_F11, KeyF11Pressed); - wnd->setOnKeyDown(SDLK_F12, KeyF12Pressed); + wnd->AddKeyEvent(SDLK_F3, KeyF3Pressed); + wnd->AddKeyEvent(SDLK_F4, KeyF4Pressed); + wnd->AddKeyEvent(SDLK_F8, KeyF8Pressed); + wnd->AddKeyEvent(SDLK_F9, KeyF9Pressed); + wnd->AddKeyEvent(SDLK_F10, KeyF10Pressed); + wnd->AddKeyEvent(SDLK_F11, KeyF11Pressed); + wnd->AddKeyEvent(SDLK_F12, KeyF12Pressed); } Prepare(); diff --git a/lib/vssolution3d.cpp b/lib/vssolution3d.cpp index d634e6b2..19953410 100644 --- a/lib/vssolution3d.cpp +++ b/lib/vssolution3d.cpp @@ -105,18 +105,6 @@ std::string VisualizationSceneSolution3d::GetHelpString() const return os.str(); } -static void KeyiPressed() -{ - vssol3d -> ToggleCuttingPlane(); - SendExposeEvent(); -} - -static void KeyIPressed() -{ - vssol3d -> ToggleCPAlgorithm(); - SendExposeEvent(); -} - void VisualizationSceneSolution3d::PrepareOrderingCurve() { bool color = draworder < 3; @@ -296,36 +284,6 @@ static void KeyZPressed() SendExposeEvent(); } -static void KeymPressed() -{ - vssol3d -> ToggleDrawMesh(); - SendExposeEvent(); -} - -static void KeyePressed() -{ - vssol3d -> ToggleDrawElems(); - SendExposeEvent(); -} - -static void KeyMPressed() -{ - vssol3d -> ToggleCPDrawMesh(); - SendExposeEvent(); -} - -static void KeyEPressed() -{ - vssol3d -> ToggleCPDrawElems(); - SendExposeEvent(); -} - -static void KeyFPressed() -{ - vssol3d -> ToggleShading(); - SendExposeEvent(); -} - static void KeyoPressed(GLenum state) { if (state & KMOD_CTRL) @@ -707,50 +665,52 @@ void VisualizationSceneSolution3d::Init() // if (!init) { // init = 1; + GLVisWindow* wnd = GetGLVisWindow(); + using SceneType = VisualizationSceneSolution3d; - wnd->setOnKeyDown('m', KeymPressed); - wnd->setOnKeyDown('M', KeyMPressed); + wnd->AddKeyEvent('m', &SceneType::ToggleDrawMesh); + wnd->AddKeyEvent('M', &SceneType::ToggleCPDrawMesh); - wnd->setOnKeyDown('e', KeyePressed); - wnd->setOnKeyDown('E', KeyEPressed); + wnd->AddKeyEvent('e', &SceneType::ToggleDrawElems); + wnd->AddKeyEvent('E', &SceneType::ToggleCPDrawElems); - wnd->setOnKeyDown('f', KeyFPressed); - // wnd->setOnKeyDown('F', KeyFPressed); + wnd->AddKeyEvent('f', &SceneType::ToggleShading); + // wnd->AddKeyEvent('F', KeyFPressed); - wnd->setOnKeyDown('i', KeyiPressed); - wnd->setOnKeyDown('I', KeyIPressed); + wnd->AddKeyEvent('i', &SceneType::ToggleCuttingPlane); + wnd->AddKeyEvent('I', &SceneType::ToggleCPAlgorithm); - wnd->setOnKeyDown('o', KeyoPressed); - wnd->setOnKeyDown('O', KeyOPressed); + wnd->AddKeyEvent('o', KeyoPressed); + wnd->AddKeyEvent('O', KeyOPressed); - wnd->setOnKeyDown('w', KeywPressed); - wnd->setOnKeyDown('W', KeyWPressed); + wnd->AddKeyEvent('w', KeywPressed); + wnd->AddKeyEvent('W', KeyWPressed); - wnd->setOnKeyDown('x', KeyxPressed); - wnd->setOnKeyDown('X', KeyXPressed); + wnd->AddKeyEvent('x', KeyxPressed); + wnd->AddKeyEvent('X', KeyXPressed); - wnd->setOnKeyDown('y', KeyyPressed); - wnd->setOnKeyDown('Y', KeyYPressed); + wnd->AddKeyEvent('y', KeyyPressed); + wnd->AddKeyEvent('Y', KeyYPressed); - wnd->setOnKeyDown('z', KeyzPressed); - wnd->setOnKeyDown('Z', KeyZPressed); + wnd->AddKeyEvent('z', KeyzPressed); + wnd->AddKeyEvent('Z', KeyZPressed); - wnd->setOnKeyDown('u', KeyuPressed); - wnd->setOnKeyDown('U', KeyUPressed); + wnd->AddKeyEvent('u', KeyuPressed); + wnd->AddKeyEvent('U', KeyUPressed); - wnd->setOnKeyDown('v', KeyvPressed); - wnd->setOnKeyDown('V', KeyVPressed); + wnd->AddKeyEvent('v', KeyvPressed); + wnd->AddKeyEvent('V', KeyVPressed); - wnd->setOnKeyDown(SDLK_F3, KeyF3Pressed); - wnd->setOnKeyDown(SDLK_F4, KeyF4Pressed); - wnd->setOnKeyDown(SDLK_NUMLOCKCLEAR, ToggleMagicKey); + wnd->AddKeyEvent(SDLK_F3, KeyF3Pressed); + wnd->AddKeyEvent(SDLK_F4, KeyF4Pressed); + wnd->AddKeyEvent(SDLK_NUMLOCKCLEAR, ToggleMagicKey); - wnd->setOnKeyDown(SDLK_F8, KeyF8Pressed); - wnd->setOnKeyDown(SDLK_F9, KeyF9Pressed); - wnd->setOnKeyDown(SDLK_F10, KeyF10Pressed); + wnd->AddKeyEvent(SDLK_F8, KeyF8Pressed); + wnd->AddKeyEvent(SDLK_F9, KeyF9Pressed); + wnd->AddKeyEvent(SDLK_F10, KeyF10Pressed); - wnd->setOnKeyDown(SDLK_F11, KeyF11Pressed); - wnd->setOnKeyDown(SDLK_F12, KeyF12Pressed); + wnd->AddKeyEvent(SDLK_F11, KeyF11Pressed); + wnd->AddKeyEvent(SDLK_F12, KeyF12Pressed); } Prepare(); PrepareLines(); diff --git a/lib/vsvector.cpp b/lib/vsvector.cpp index 67cf1dfc..495cfbd3 100644 --- a/lib/vsvector.cpp +++ b/lib/vsvector.cpp @@ -104,53 +104,34 @@ extern VisualizationScene * locscene; extern VisualizationSceneSolution * vssol; extern GeometryRefiner GLVisGeometryRefiner; -static int ianim = 0; -static int ianimmax = 10; - -void KeyDPressed() -{ - vsvector -> ToggleDisplacements(); - SendExposeEvent(); -} - -void KeyNPressed() +void VisualizationSceneVector::NextDisplacement() { ianim = (ianim + 1) % (ianimmax + 1); - vsvector -> NPressed(); + if (drawdisp == 0) + { + drawdisp = 1; + ianim = 0; + } + PrepareDisplacedMesh(); } -void KeyBPressed() +void VisualizationSceneVector::PrevDisplacement() { ianim = (ianim + ianimmax) % (ianimmax + 1); - vsvector -> NPressed(); -} - -void VisualizationSceneVector::NPressed() -{ if (drawdisp == 0) { drawdisp = 1; ianim = 0; } - PrepareDisplacedMesh(); - - SendExposeEvent(); } -void KeyvPressed() -{ - vsvector -> ToggleVectorField(); - SendExposeEvent(); -} - -void KeyVPressed() +void VisualizationSceneVector::QueryArrowScaling() { cout << "New arrow scale: " << flush; - cin >> vsvector -> ArrowScale; - cout << "New arrow scale = " << vsvector -> ArrowScale << endl; - vsvector -> PrepareVectorField(); - SendExposeEvent(); + cin >> ArrowScale; + cout << "New arrow scale = " << ArrowScale << endl; + PrepareVectorField(); } int key_u_func = 0; @@ -500,15 +481,17 @@ void VisualizationSceneVector::Init() // if (!init) { // init = 1; - - wnd->setOnKeyDown('d', KeyDPressed); - wnd->setOnKeyDown('D', KeyDPressed); - wnd->setOnKeyDown('n', KeyNPressed); - wnd->setOnKeyDown('b', KeyBPressed); - wnd->setOnKeyDown('v', KeyvPressed); - wnd->setOnKeyDown('V', KeyVPressed); - wnd->setOnKeyDown('u', KeyuPressed); - wnd->setOnKeyDown('U', KeyUPressed); + GLVisWindow* wnd = GetGLVisWindow(); + using SceneType = VisualizationSceneVector; + + wnd->AddKeyEvent('d', &SceneType::ToggleDisplacements); + wnd->AddKeyEvent('D', &SceneType::ToggleDisplacements); + wnd->AddKeyEvent('n', &SceneType::NextDisplacement); + wnd->AddKeyEvent('b', &SceneType::PrevDisplacement); + wnd->AddKeyEvent('v', &SceneType::ToggleVectorField); + wnd->AddKeyEvent('V', &SceneType::QueryArrowScaling); + wnd->AddKeyEvent('u', KeyuPressed); + wnd->AddKeyEvent('U', KeyUPressed); } // Vec2Scalar is VecLength diff --git a/lib/vsvector.hpp b/lib/vsvector.hpp index ee56f1c2..c8d4522f 100644 --- a/lib/vsvector.hpp +++ b/lib/vsvector.hpp @@ -44,6 +44,14 @@ class VisualizationSceneVector : public VisualizationSceneSolution Vector vc0; IsoparametricTransformation T0; + int ianim = 0; + const int ianimmax = 10; + + void NextDisplacement(); + void PrevDisplacement(); + + void QueryArrowScaling(); + public: VisualizationSceneVector(Mesh &m, Vector &sx, Vector &sy); VisualizationSceneVector(GridFunction &vgf); @@ -54,7 +62,6 @@ class VisualizationSceneVector : public VisualizationSceneSolution virtual std::string GetHelpString() const; - void NPressed(); void PrepareDisplacedMesh(); virtual void PrepareLines() { VisualizationSceneSolution::PrepareLines(); PrepareDisplacedMesh(); } diff --git a/lib/vsvector3d.cpp b/lib/vsvector3d.cpp index d3b223b7..28c835fa 100644 --- a/lib/vsvector3d.cpp +++ b/lib/vsvector3d.cpp @@ -104,34 +104,22 @@ VisualizationSceneVector3d *vsvector3d; extern VisualizationScene *locscene; extern GeometryRefiner GLVisGeometryRefiner; -static void KeyDPressed() +void VisualizationSceneVector3d::NextDisplacement() { - vsvector3d -> ToggleDisplacements(); - SendExposeEvent(); -} - -static void KeyNPressed() -{ - if (vsvector3d -> drawdisp) - vsvector3d -> ianimd =( (vsvector3d ->ianimd + 1) % - (vsvector3d -> ianimmax + 1) ); + if (drawdisp) + ianimd =( (ianimd + 1) % (ianimmax + 1) ); else - vsvector3d -> ianim =( (vsvector3d ->ianim + 1) % - (vsvector3d -> ianimmax + 1) ); - vsvector3d -> NPressed(); + ianim =( (ianim + 1) % (ianimmax + 1) ); + NPressed(); } -static void KeyBPressed() +void VisualizationSceneVector3d::PrevDisplacement() { - if (vsvector3d -> drawdisp) - vsvector3d ->ianimd = ((vsvector3d ->ianimd + - vsvector3d ->ianimmax) % - (vsvector3d ->ianimmax + 1)); + if (drawdisp) + ianimd = ((ianimd + ianimmax) % (ianimmax + 1)); else - vsvector3d ->ianim = ((vsvector3d ->ianim + - vsvector3d ->ianimmax) % - (vsvector3d ->ianimmax + 1)); - vsvector3d -> NPressed(); + ianim = ((ianim + ianimmax) % (ianimmax + 1)); + NPressed(); } static void KeyrPressed() @@ -178,16 +166,14 @@ void VisualizationSceneVector3d::NPressed() SendExposeEvent(); } -static void KeyuPressed() +void VisualizationSceneVector3d::NextVectorFieldLevel() { - vsvector3d -> ToggleVectorFieldLevel(+1); - SendExposeEvent(); + ToggleVectorFieldLevel(+1); } -static void KeyUPressed() +void VisualizationSceneVector3d::PrevVectorFieldLevel() { - vsvector3d -> ToggleVectorFieldLevel(-1); - SendExposeEvent(); + ToggleVectorFieldLevel(-1); } void VisualizationSceneVector3d::ToggleVectorFieldLevel(int v) @@ -206,19 +192,7 @@ void VisualizationSceneVector3d::ToggleVectorFieldLevel(int v) { dvflevel[i] = level[vflevel[i]]; } - vsvector3d -> PrepareVectorField(); -} - -static void KeywPressed() -{ - vsvector3d -> AddVectorFieldLevel(); - SendExposeEvent(); -} - -static void KeyWPressed() -{ - vsvector3d -> RemoveVectorFieldLevel(); - SendExposeEvent(); + PrepareVectorField(); } void VisualizationSceneVector3d::AddVectorFieldLevel() @@ -227,32 +201,24 @@ void VisualizationSceneVector3d::AddVectorFieldLevel() next = (next + 1) % (nl+1); vflevel.Append(next); dvflevel.Append(level[next]); - vsvector3d -> PrepareVectorField(); + PrepareVectorField(); } void VisualizationSceneVector3d::RemoveVectorFieldLevel() { vflevel.DeleteLast(); dvflevel.DeleteLast(); - vsvector3d -> PrepareVectorField(); -} - -static void KeyvPressed() -{ - vsvector3d -> ToggleVectorField(1); - SendExposeEvent(); + PrepareVectorField(); } -static void KeyVPressed() +void VisualizationSceneVector3d::NextVectorField() { - vsvector3d -> ToggleVectorField(-1); - SendExposeEvent(); + ToggleVectorField(1); } -static void VectorKeyFPressed() +void VisualizationSceneVector3d::PrevVectorField() { - vsvector3d->ToggleScalarFunction(); - SendExposeEvent(); + ToggleVectorField(-1); } void VisualizationSceneVector3d::ToggleVectorField(int i) @@ -399,29 +365,31 @@ void VisualizationSceneVector3d::Init() // if (!init) { // init = 1; + GLVisWindow* wnd = GetGLVisWindow(); + using SceneType = VisualizationSceneVector3d; - wnd->setOnKeyDown('d', KeyDPressed); - wnd->setOnKeyDown('D', KeyDPressed); + wnd->AddKeyEvent('d', &SceneType::ToggleDisplacements); + wnd->AddKeyEvent('D', &SceneType::ToggleDisplacements); - wnd->setOnKeyDown('n', KeyNPressed); - wnd->setOnKeyDown('N', KeyNPressed); + wnd->AddKeyEvent('n', &SceneType::NextDisplacement); + wnd->AddKeyEvent('N', &SceneType::NextDisplacement); - wnd->setOnKeyDown('b', KeyBPressed); - wnd->setOnKeyDown('B', KeyBPressed); + wnd->AddKeyEvent('b', &SceneType::PrevDisplacement); + wnd->AddKeyEvent('B', &SceneType::PrevDisplacement); - wnd->setOnKeyDown('r', KeyrPressed); // adds another function to 'r' and 'R' - wnd->setOnKeyDown('R', KeyRPressed); // the other function is in vsdata.cpp + wnd->AddKeyEvent('r', KeyrPressed); // adds another function to 'r' and 'R' + wnd->AddKeyEvent('R', KeyRPressed); // the other function is in vsdata.cpp - wnd->setOnKeyDown('u', KeyuPressed); // Keys u, U are also used in - wnd->setOnKeyDown('U', KeyUPressed); // VisualizationSceneSolution3d + wnd->AddKeyEvent('u', &SceneType::NextVectorFieldLevel); // Keys u, U are also used in + wnd->AddKeyEvent('U', &SceneType::PrevVectorFieldLevel); // VisualizationSceneSolution3d - wnd->setOnKeyDown('w', KeywPressed); // Keys w, W are also used in - wnd->setOnKeyDown('W', KeyWPressed); // VisualizationSceneSolution3d + wnd->AddKeyEvent('w', &SceneType::AddVectorFieldLevel); // Keys w, W are also used in + wnd->AddKeyEvent('W', &SceneType::RemoveVectorFieldLevel); // VisualizationSceneSolution3d - wnd->setOnKeyDown('v', KeyvPressed); // Keys v, V are also used in - wnd->setOnKeyDown('V', KeyVPressed); // VisualizationSceneSolution3d + wnd->AddKeyEvent('v', &SceneType::NextVectorField); // Keys v, V are also used in + wnd->AddKeyEvent('V', &SceneType::PrevVectorField); // VisualizationSceneSolution3d - wnd->setOnKeyDown('F', VectorKeyFPressed); + wnd->AddKeyEvent('F', &SceneType::ToggleScalarFunction); } } diff --git a/lib/vsvector3d.hpp b/lib/vsvector3d.hpp index ab1cfaab..4c823147 100644 --- a/lib/vsvector3d.hpp +++ b/lib/vsvector3d.hpp @@ -33,6 +33,14 @@ class VisualizationSceneVector3d : public VisualizationSceneSolution3d Array vflevel; Array dvflevel; + void NextDisplacement(); + void PrevDisplacement(); + void NPressed(); + + void NextVectorField(); + void PrevVectorField(); + void NextVectorFieldLevel(); + void PrevVectorFieldLevel(); public: int ianim, ianimd, ianimmax, drawdisp; @@ -45,7 +53,6 @@ class VisualizationSceneVector3d : public VisualizationSceneSolution3d virtual std::string GetHelpString() const; - void NPressed(); virtual void PrepareFlat(); virtual void Prepare(); virtual void PrepareLines(); From eb59307e37a564ed5999aa803d6e5813acf3b234 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Wed, 10 Feb 2021 17:37:01 -0800 Subject: [PATCH 20/39] Add support for stackable events, improve handling of scene view resets --- lib/aux_vis.cpp | 46 +++++++++++++++++++++++++++++++++---------- lib/aux_vis.hpp | 18 ++++++++++++++--- lib/vsdata.cpp | 45 ++++++++++++++---------------------------- lib/vsdata.hpp | 3 ++- lib/vsvector3d.cpp | 49 ++++++++++++++++------------------------------ lib/vsvector3d.hpp | 4 ++++ 6 files changed, 89 insertions(+), 76 deletions(-) diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index e4c8fadc..04a7fe6c 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -101,6 +101,13 @@ KeyDelegate CreateKeyEvent(T* inst, void (T::*func)()) return [inst, func](GLenum) { (inst->*func)(); }; } +bool KeyPrint(GLVisWindow* wnd, GLenum mod) +{ + if (mod & KMOD_CTRL) + { + wnd->PrintToPDF(); + } +} GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool legacyGlOnly) : locscene(nullptr) @@ -157,7 +164,9 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega wnd->setTouchPinchCallback(TouchPinch); SetKeyEventHandler('A', &GLVisWindow::ToggleAntialiasing); - // auxKeyFunc (AUX_p, KeyCtrlP); // handled in vsdata.cpp + SetKeyEventHandler ('p', &GLVisWindow::PrintToPDF); + SetKeyEventHandler ('r', &GLVisWindow::StopSpinning); + SetKeyEventHandler ('R', &GLVisWindow::StopSpinning); SetKeyEventHandler (SDLK_s, &GLVisWindow::Screenshot); SetKeyEventHandler ('S', &GLVisWindow::Screenshot); @@ -272,16 +281,30 @@ void GLVisWindow::InitVisualization(int field_type, StreamState state, } } +void GLVisWindow::SetupHandledKey(int key) +{ + wnd->setOnKeyDown(key, + [this, key](GLenum e) + { + if (internal_keyevents.find(key) != internal_keyevents.end()) + { internal_keyevents[key](e); } + if (keyevents.find(key) != keyevents.end()) + { keyevents[key](e); } + }); +} + void GLVisWindow::SetKeyEventHandler(int key, void (GLVisWindow::*handler)()) { - auto handlerWrapper = [this, handler]() { (this->*handler)(); }; - wnd->setOnKeyDown(key, handlerWrapper); + auto handlerWrapper = [this, handler](GLenum) { (this->*handler)(); }; + internal_keyevents[key] = handlerWrapper; + SetupHandledKey(key); } void GLVisWindow::SetKeyEventHandler(int key, void (GLVisWindow::*handler)(GLenum)) { auto handlerWrapper = [this, handler](GLenum mod) { (this->*handler)(mod); }; - wnd->setOnKeyDown(key, handlerWrapper); + internal_keyevents[key] = handlerWrapper; + SetupHandledKey(key); } void SendKeySequence(const char *seq) @@ -629,8 +652,7 @@ inline void ComputeSphereAngles(int viewport_w, int viewport_h, void GLVisWindow::RotationControl::LeftButtonDown (EventInfo *event) { - wnd->locscene -> spinning = 0; - wnd->RemoveIdleFunc(::MainLoop); + wnd->StopSpinning(); oldx = event->mouse_x; oldy = event->mouse_y; @@ -1064,8 +1086,7 @@ void GLVisWindow::RotationControl::CheckSpin() } else { - wnd->locscene->spinning = 0; - wnd->RemoveIdleFunc(::MainLoop); + wnd->StopSpinning(); } cout << "Spin angle: " << xang << " degrees / frame" << endl; } @@ -1087,8 +1108,7 @@ void GLVisWindow::RotationControl::KeyDeletePressed() if (wnd->locscene -> spinning) { xang = yang = 0.; - wnd->locscene -> spinning = 0; - wnd->RemoveIdleFunc(::MainLoop); + wnd->StopSpinning(); constrained_spinning = 1; } else @@ -1256,6 +1276,12 @@ void GLVisWindow::KeyPlusPressed() SendExposeEvent(); } +void GLVisWindow::StopSpinning() +{ + locscene -> spinning = 0; + RemoveIdleFunc(::MainLoop); +} + void GLVisWindow::ZoomIn() { locscene->Zoom(exp (0.05)); diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index e5f07836..e7f1510c 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -108,7 +108,8 @@ class GLVisWindow (pScene->*eh)(); if (exposeAfter) { SendExposeEvent(); } }; - wnd->setOnKeyDown(key, wrapped_eh); + keyevents[key] = wrapped_eh; + SetupHandledKey(key); } void AddKeyEvent(int key, void (*eh)(GLVisWindow*), bool exposeAfter = true) @@ -118,7 +119,8 @@ class GLVisWindow (*eh)(this); if (exposeAfter) { SendExposeEvent(); } }; - wnd->setOnKeyDown(key, wrapped_eh); + keyevents[key] = wrapped_eh; + SetupHandledKey(key); } void AddKeyEvent(int key, void (*eh)(GLVisWindow*, GLenum), bool exposeAfter = true) @@ -128,12 +130,15 @@ class GLVisWindow (*eh)(this, e); if (exposeAfter) { SendExposeEvent(); } }; - wnd->setOnKeyDown(key, wrapped_eh); + keyevents[key] = wrapped_eh; + SetupHandledKey(key); } private: void InitFont(); bool SetFont(const vector& patterns, int height); + void SetupHandledKey(int key); + void SetKeyEventHandler(int key, void (GLVisWindow::*handler)()); void SetKeyEventHandler(int key, void (GLVisWindow::*handler)(GLenum)); @@ -163,6 +168,8 @@ class GLVisWindow void KeyMinusPressed(); void KeyPlusPressed(); + void StopSpinning(); + // Internal event handler for toggling state of threads void ThreadsPauseFunc(GLenum); @@ -177,6 +184,11 @@ class GLVisWindow mfem::Array idle_funcs{}; int last_idle_func = 0; + // internal_keyevents keeps internal event handlers, which will be called + // before an event handler in keyevents + std::unordered_map internal_keyevents; + std::unordered_map keyevents; + int visualize = 0; bool disableSendExposeEvent = false; diff --git a/lib/vsdata.cpp b/lib/vsdata.cpp index 5ac32448..8d9272d3 100644 --- a/lib/vsdata.cpp +++ b/lib/vsdata.cpp @@ -485,12 +485,10 @@ void Key_Mod_a_Pressed(GLVisWindow* wnd, GLenum state) cout << "Autoscale: " << flush; vsdata->SetAutoscale(autoscale); cout << autoscale_modes[autoscale] << endl; - SendExposeEvent(); } else { vsdata->ToggleDrawAxes(); - SendExposeEvent(); } } @@ -505,39 +503,26 @@ void KeyLPressed() SendExposeEvent(); } -void KeyrPressed() +void VisualizationSceneScalarData::Reset3DView() { - vsdata -> spinning = 0; - GetGLVisWindow()->RemoveIdleFunc(MainLoop); - vsdata -> CenterObject(); - - vsdata -> ViewAngle = 45.0; - vsdata -> ViewScale = 1.0; - vsdata -> ViewCenterX = 0.0; - vsdata -> ViewCenterY = 0.0; - vsdata->cam.Reset(); - vsdata -> key_r_state = 0; - SendExposeEvent(); -} + CenterObject(); -void KeyRPressed() -{ - vsdata->spinning = 0; - GetGLVisWindow()->RemoveIdleFunc(MainLoop); - vsdata->Toggle2DView(); - SendExposeEvent(); + ViewAngle = 45.0; + ViewScale = 1.0; + ViewCenterX = 0.0; + ViewCenterY = 0.0; + cam.Reset(); + key_r_state = 0; } -void KeypPressed(GLenum state) +void KeypPressed(GLVisWindow* wnd, GLenum state) { - if (state & KMOD_CTRL) - { - GetGLVisWindow()->PrintToPDF(); - } - else + VisualizationSceneScalarData * vsdata + = dynamic_cast(wnd->getScene()); + // KMOD_CTRL handled by KeyPrintPDF in aux_vis.cpp + if (!(state & KMOD_CTRL)) { vsdata->palette.NextIndex(); - SendExposeEvent(); } } @@ -1013,8 +998,8 @@ void VisualizationSceneScalarData::Init() // wnd->AddKeyEvent('a', KeyaPressed); wnd->AddKeyEvent('a', Key_Mod_a_Pressed); - wnd->AddKeyEvent('r', KeyrPressed); - wnd->AddKeyEvent('R', KeyRPressed); + wnd->AddKeyEvent('r', &SceneType::Reset3DView); + wnd->AddKeyEvent('R', &SceneType::Toggle2DView); wnd->AddKeyEvent('p', KeypPressed); wnd->AddKeyEvent('P', KeyPPressed); diff --git a/lib/vsdata.hpp b/lib/vsdata.hpp index ef67d41d..a7d5aeed 100644 --- a/lib/vsdata.hpp +++ b/lib/vsdata.hpp @@ -264,7 +264,8 @@ class VisualizationSceneScalarData : public VisualizationScene void ToggleTexture(); - void Toggle2DView(); + virtual void Reset3DView(); + virtual void Toggle2DView(); void SetAutoscale(int _autoscale); int GetAutoscale() const { return autoscale; } diff --git a/lib/vsvector3d.cpp b/lib/vsvector3d.cpp index 28c835fa..a84baddb 100644 --- a/lib/vsvector3d.cpp +++ b/lib/vsvector3d.cpp @@ -100,8 +100,6 @@ std::string VisualizationSceneVector3d::GetHelpString() const return os.str(); } -VisualizationSceneVector3d *vsvector3d; -extern VisualizationScene *locscene; extern GeometryRefiner GLVisGeometryRefiner; void VisualizationSceneVector3d::NextDisplacement() @@ -122,33 +120,27 @@ void VisualizationSceneVector3d::PrevDisplacement() NPressed(); } -static void KeyrPressed() +void VisualizationSceneVector3d::Reset3DView() { - locscene -> spinning = 0; - GetGLVisWindow()->RemoveIdleFunc(MainLoop); - vsvector3d -> CenterObject(); - locscene -> ViewAngle = 45.0; - locscene -> ViewScale = 1.0; - locscene -> ViewCenterX = 0.0; - locscene -> ViewCenterY = 0.0; - vsvector3d -> ianim = vsvector3d -> ianimd = 0; - vsvector3d -> Prepare(); - vsvector3d -> PrepareLines(); - vsvector3d -> PrepareDisplacedMesh(); - vsvector3d -> key_r_state = 0; - SendExposeEvent(); + CenterObject(); + ViewAngle = 45.0; + ViewScale = 1.0; + ViewCenterX = 0.0; + ViewCenterY = 0.0; + ianim = ianimd = 0; + Prepare(); + PrepareLines(); + PrepareDisplacedMesh(); + key_r_state = 0; } -static void KeyRPressed() +void VisualizationSceneVector3d::Toggle2DView() { - locscene->spinning = 0; - GetGLVisWindow()->RemoveIdleFunc(MainLoop); - vsvector3d -> ianim = vsvector3d -> ianimd = 0; - vsvector3d -> Prepare(); - vsvector3d -> PrepareLines(); - vsvector3d -> PrepareDisplacedMesh(); - vsvector3d->Toggle2DView(); - SendExposeEvent(); + ianim = ianimd = 0; + Prepare(); + PrepareLines(); + PrepareDisplacedMesh(); + VisualizationSceneScalarData::Toggle2DView(); } void VisualizationSceneVector3d::NPressed() @@ -162,8 +154,6 @@ void VisualizationSceneVector3d::NPressed() Prepare(); PrepareLines(); } - - SendExposeEvent(); } void VisualizationSceneVector3d::NextVectorFieldLevel() @@ -359,8 +349,6 @@ void VisualizationSceneVector3d::Init() vflevel.Append(0); dvflevel.Append(level[0]); - vsvector3d = this; - // static int init = 0; // if (!init) { @@ -377,9 +365,6 @@ void VisualizationSceneVector3d::Init() wnd->AddKeyEvent('b', &SceneType::PrevDisplacement); wnd->AddKeyEvent('B', &SceneType::PrevDisplacement); - wnd->AddKeyEvent('r', KeyrPressed); // adds another function to 'r' and 'R' - wnd->AddKeyEvent('R', KeyRPressed); // the other function is in vsdata.cpp - wnd->AddKeyEvent('u', &SceneType::NextVectorFieldLevel); // Keys u, U are also used in wnd->AddKeyEvent('U', &SceneType::PrevVectorFieldLevel); // VisualizationSceneSolution3d diff --git a/lib/vsvector3d.hpp b/lib/vsvector3d.hpp index 4c823147..6d1306f9 100644 --- a/lib/vsvector3d.hpp +++ b/lib/vsvector3d.hpp @@ -60,6 +60,10 @@ class VisualizationSceneVector3d : public VisualizationSceneSolution3d void PrepareFlat2(); void PrepareLines2(); + // Overrides original event handlers defined in vsdata.cpp + virtual void Reset3DView(); + virtual void Toggle2DView(); + void DrawVector (gl3::GlBuilder& builder, int type, double v0, double v1, double v2, double sx, double sy, double sz, double s); From a8dcb8628752a4102e00ad2d969202a2501f6028 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 11 Feb 2021 10:53:47 -0800 Subject: [PATCH 21/39] Add support for conditionally-updatable event handlers --- lib/aux_vis.hpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index e7f1510c..6a7c11b7 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -99,6 +99,20 @@ class GLVisWindow wnd->setOnKeyDown(key, eh); } + /// Adds a conditionally-updatable scene event. + template + void AddKeyEvent(int key, bool (TScene::*eh)()) + { + auto wrapped_eh = [this, eh](GLenum e) + { + TScene* pScene = dynamic_cast(locscene.get()); + bool exposeAfter = (pScene->*eh)(); + if (exposeAfter) { SendExposeEvent(); } + }; + keyevents[key] = wrapped_eh; + SetupHandledKey(key); + } + template void AddKeyEvent(int key, void (TScene::*eh)(), bool exposeAfter = true) { From ca18aa209585bb16aa7812cead9be82b9fad3be9 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 11 Feb 2021 10:54:12 -0800 Subject: [PATCH 22/39] Make all vssolution3d event handlers member functions --- lib/aux_vis.hpp | 6 - lib/vssolution3d.cpp | 305 +++++++++++++++++++++---------------------- lib/vssolution3d.hpp | 25 ++++ 3 files changed, 173 insertions(+), 163 deletions(-) diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 6a7c11b7..0d91d580 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -93,12 +93,6 @@ class GLVisWindow void ResizeWindow(int w, int h); void SetWindowTitle(const char *title); - template - void AddKeyEvent(int key, void(*eh)(Args...)) - { - wnd->setOnKeyDown(key, eh); - } - /// Adds a conditionally-updatable scene event. template void AddKeyEvent(int key, bool (TScene::*eh)()) diff --git a/lib/vssolution3d.cpp b/lib/vssolution3d.cpp index 19953410..fa08b3fa 100644 --- a/lib/vssolution3d.cpp +++ b/lib/vssolution3d.cpp @@ -21,7 +21,6 @@ using namespace mfem; using namespace std; -VisualizationSceneSolution3d *vssol3d; extern GeometryRefiner GLVisGeometryRefiner; // Definitions of some more keys @@ -236,61 +235,57 @@ void VisualizationSceneSolution3d::CPMoved() } } -static void KeyxPressed() +void VisualizationSceneSolution3d::RotateCPPhi() { - vssol3d -> CuttingPlane -> IncreasePhi(); - vssol3d -> FindNodePos(); - vssol3d -> CPMoved(); - SendExposeEvent(); + CuttingPlane -> IncreasePhi(); + FindNodePos(); + CPMoved(); } -static void KeyXPressed() +void VisualizationSceneSolution3d::RotateCPPhiBack() { - vssol3d -> CuttingPlane -> DecreasePhi(); - vssol3d -> FindNodePos(); - vssol3d -> CPMoved(); - SendExposeEvent(); + CuttingPlane -> DecreasePhi(); + FindNodePos(); + CPMoved(); } -static void KeyyPressed() +void VisualizationSceneSolution3d::RotateCPTheta() { - vssol3d -> CuttingPlane -> IncreaseTheta(); - vssol3d -> FindNodePos(); - vssol3d -> CPMoved(); - SendExposeEvent(); + CuttingPlane -> IncreaseTheta(); + FindNodePos(); + CPMoved(); } -static void KeyYPressed() +void VisualizationSceneSolution3d::RotateCPThetaBack() { - vssol3d -> CuttingPlane -> DecreaseTheta(); - vssol3d -> FindNodePos(); - vssol3d -> CPMoved(); - SendExposeEvent(); + CuttingPlane -> DecreaseTheta(); + FindNodePos(); + CPMoved(); } -static void KeyzPressed() +void VisualizationSceneSolution3d::TranslateCP() { - vssol3d -> CuttingPlane -> IncreaseDistance(); - vssol3d -> FindNodePos(); - vssol3d -> CPMoved(); - SendExposeEvent(); + CuttingPlane -> IncreaseDistance(); + FindNodePos(); + CPMoved(); } -static void KeyZPressed() +void VisualizationSceneSolution3d::TranslateCPBack() { - vssol3d -> CuttingPlane -> DecreaseDistance(); - vssol3d -> FindNodePos(); - vssol3d -> CPMoved(); - SendExposeEvent(); + CuttingPlane -> DecreaseDistance(); + FindNodePos(); + CPMoved(); } -static void KeyoPressed(GLenum state) +void KeyoPressed(GLVisWindow* wnd, GLenum state) { + VisualizationSceneSolution3d* vssol3d + = dynamic_cast(wnd->getScene()); if (state & KMOD_CTRL) { vssol3d -> ToggleDrawOrdering(); vssol3d -> PrepareOrderingCurve(); - SendExposeEvent(); + wnd -> SendExposeEvent(); } else { @@ -304,14 +299,16 @@ static void KeyoPressed(GLenum state) vssol3d -> PrepareLines(); vssol3d -> CPPrepare(); vssol3d -> PrepareLevelSurf(); - SendExposeEvent(); + wnd -> SendExposeEvent(); } } } } -static void KeyOPressed() +static void KeyOPressed(GLVisWindow* wnd) { + VisualizationSceneSolution3d* vssol3d + = dynamic_cast(wnd->getScene()); if (vssol3d -> TimesToRefine > 1) { cout << "Subdivision factor = " << --vssol3d->TimesToRefine << endl; @@ -322,166 +319,167 @@ static void KeyOPressed() vssol3d -> PrepareLines(); vssol3d -> CPPrepare(); vssol3d -> PrepareLevelSurf(); - SendExposeEvent(); + wnd -> SendExposeEvent(); } } } -static void KeywPressed() +bool VisualizationSceneSolution3d::MoveUpBdrElems() { - if (vssol3d -> GetShading() == 2) + if (GetShading() == 2) { - if ( fabs(vssol3d -> FaceShiftScale += 0.01) < 0.001 ) + if ( fabs(FaceShiftScale += 0.01) < 0.001 ) { - vssol3d -> FaceShiftScale = 0.0; + FaceShiftScale = 0.0; } - cout << "New Shift Scale: " << vssol3d -> FaceShiftScale + cout << "New Shift Scale: " << FaceShiftScale << endl; - vssol3d -> Prepare(); - vssol3d -> PrepareLines(); - vssol3d -> CPPrepare(); - SendExposeEvent(); + Prepare(); + PrepareLines(); + CPPrepare(); + return true; } + return false; } -static void KeyWPressed() +bool VisualizationSceneSolution3d::MoveDownBdrElems() { - if (vssol3d -> GetShading() == 2) + if (GetShading() == 2) { - if ( fabs(vssol3d -> FaceShiftScale -= 0.01) < 0.001 ) + if ( fabs(FaceShiftScale -= 0.01) < 0.001 ) { - vssol3d -> FaceShiftScale = 0.0; + FaceShiftScale = 0.0; } - cout << "New Shift Scale: " << vssol3d -> FaceShiftScale + cout << "New Shift Scale: " << FaceShiftScale << endl; - vssol3d -> Prepare(); - vssol3d -> PrepareLines(); - vssol3d -> CPPrepare(); - SendExposeEvent(); + Prepare(); + PrepareLines(); + CPPrepare(); + return true; } + return false; } -static void KeyuPressed() +void VisualizationSceneSolution3d::MoveUpLevelSurf() { - vssol3d -> MoveLevelSurf(+1); - SendExposeEvent(); + MoveLevelSurf(+1); } -static void KeyUPressed() +void VisualizationSceneSolution3d::MoveDownLevelSurf() { - vssol3d -> MoveLevelSurf(-1); - SendExposeEvent(); + MoveLevelSurf(-1); } -static void KeyvPressed() +void VisualizationSceneSolution3d::AddLevelSurf() { - vssol3d -> NumberOfLevelSurf(+1); - SendExposeEvent(); + NumberOfLevelSurf(+1); } -static void KeyVPressed() +void VisualizationSceneSolution3d::RemoveLevelSurf() { - vssol3d -> NumberOfLevelSurf(-1); - SendExposeEvent(); + NumberOfLevelSurf(-1); } static int magic_key_pressed = 0; -void ToggleMagicKey() +void ToggleMagicKey(GLVisWindow* wnd) { magic_key_pressed = 1-magic_key_pressed; } -static void KeyF3Pressed() +bool VisualizationSceneSolution3d::ShrinkBoundaryElems() { - if (vssol3d->GetShading() == 2) + if (GetShading() == 2) { - if (vssol3d->GetMesh()->Dimension() == 3 && vssol3d->bdrc.Width() == 0) + if (GetMesh()->Dimension() == 3 && bdrc.Width() == 0) { - vssol3d->ComputeBdrAttrCenter(); + ComputeBdrAttrCenter(); } - if (vssol3d->GetMesh()->Dimension() == 2 && vssol3d->matc.Width() == 0) + if (GetMesh()->Dimension() == 2 && matc.Width() == 0) { - vssol3d->ComputeElemAttrCenter(); + ComputeElemAttrCenter(); } - vssol3d->shrink *= 0.9; + shrink *= 0.9; if (magic_key_pressed) { - vssol3d -> Scale(1.11111111111111111111111); + Scale(1.11111111111111111111111); } - vssol3d->Prepare(); - vssol3d->PrepareLines(); - SendExposeEvent(); + Prepare(); + PrepareLines(); + return true; } + return false; } -static void KeyF4Pressed() +bool VisualizationSceneSolution3d::ZoomBoundaryElems() { - if (vssol3d->GetShading() == 2) + if (GetShading() == 2) { - if (vssol3d->GetMesh()->Dimension() == 3 && vssol3d->bdrc.Width() == 0) + if (GetMesh()->Dimension() == 3 && bdrc.Width() == 0) { - vssol3d->ComputeBdrAttrCenter(); + ComputeBdrAttrCenter(); } - if (vssol3d->GetMesh()->Dimension() == 2 && vssol3d->matc.Width() == 0) + if (GetMesh()->Dimension() == 2 && matc.Width() == 0) { - vssol3d->ComputeElemAttrCenter(); + ComputeElemAttrCenter(); } - vssol3d->shrink *= 1.11111111111111111111111; + shrink *= 1.11111111111111111111111; if (magic_key_pressed) { - vssol3d -> Scale(0.9); + Scale(0.9); } - vssol3d->Prepare(); - vssol3d->PrepareLines(); - SendExposeEvent(); + Prepare(); + PrepareLines(); + return true; } + return false; } -static void KeyF11Pressed() +bool VisualizationSceneSolution3d::ShrinkMatSubdomains() { - if (vssol3d->GetShading() == 2) + if (GetShading() == 2) { - if (vssol3d->matc.Width() == 0) + if (matc.Width() == 0) { - vssol3d->ComputeElemAttrCenter(); + ComputeElemAttrCenter(); } - vssol3d->shrinkmat *= 0.9; + shrinkmat *= 0.9; if (magic_key_pressed) { - vssol3d -> Scale(1.11111111111111111111111); + Scale(1.11111111111111111111111); } - vssol3d->Prepare(); - vssol3d->PrepareLines(); - SendExposeEvent(); + Prepare(); + PrepareLines(); + return true; } + return false; } -static void KeyF12Pressed() +bool VisualizationSceneSolution3d::ZoomMatSubdomains() { - if (vssol3d->GetShading() == 2) + if (GetShading() == 2) { - if (vssol3d->matc.Width() == 0) + if (matc.Width() == 0) { - vssol3d->ComputeElemAttrCenter(); + ComputeElemAttrCenter(); } - vssol3d->shrinkmat *= 1.11111111111111111111111; + shrinkmat *= 1.11111111111111111111111; if (magic_key_pressed) { - vssol3d -> Scale(0.9); + Scale(0.9); } - vssol3d->Prepare(); - vssol3d->PrepareLines(); - SendExposeEvent(); + Prepare(); + PrepareLines(); + return true; } + return false; } -static void KeyF8Pressed() +void VisualizationSceneSolution3d::QueryToggleSubdomains() { - Mesh &mesh = *vssol3d->GetMesh(); - int dim = mesh.Dimension(); - const Array &all_attr = ((dim == 3) ? mesh.bdr_attributes : - mesh.attributes); - Array &attr_marker = vssol3d->bdr_attr_to_show; + int dim = mesh->Dimension(); + const Array &all_attr = ((dim == 3) ? mesh->bdr_attributes : + mesh->attributes); + Array &attr_marker = bdr_attr_to_show; int attr; Array attr_list(&attr, 1); @@ -495,17 +493,15 @@ static void KeyF8Pressed() cout << ((dim == 3) ? "Bdr a" : "A") << "ttribute to toggle : " << flush; cin >> attr; - vssol3d->ToggleAttributes(attr_list); - SendExposeEvent(); + ToggleAttributes(attr_list); } -static void KeyF9Pressed() +void VisualizationSceneSolution3d::WalkNextSubdomain() { - Mesh &mesh = *vssol3d->GetMesh(); - int dim = mesh.Dimension(); - const Array &attr_list = ((dim == 3) ? mesh.bdr_attributes : - mesh.attributes); - Array &attr_marker = vssol3d->bdr_attr_to_show; + int dim = mesh->Dimension(); + const Array &attr_list = ((dim == 3) ? mesh->bdr_attributes : + mesh->attributes); + Array &attr_marker = bdr_attr_to_show; int attr, n, j, i; if (attr_list.Size() == 0) @@ -539,18 +535,16 @@ static void KeyF9Pressed() cout << "Showing " << ((dim == 3) ? "bdr " : "") << "attribute " << attr << endl; } - vssol3d -> PrepareLines(); - vssol3d -> Prepare(); - SendExposeEvent(); + PrepareLines(); + Prepare(); } -static void KeyF10Pressed() +void VisualizationSceneSolution3d::WalkPrevSubdomain() { - Mesh &mesh = *vssol3d->GetMesh(); - int dim = mesh.Dimension(); - const Array &attr_list = ((dim == 3) ? mesh.bdr_attributes : - mesh.attributes); - Array &attr_marker = vssol3d->bdr_attr_to_show; + int dim = mesh->Dimension(); + const Array &attr_list = ((dim == 3) ? mesh->bdr_attributes : + mesh->attributes); + Array &attr_marker = bdr_attr_to_show; int attr, n, j, i; if (attr_list.Size() == 0) @@ -585,9 +579,8 @@ static void KeyF10Pressed() cout << "Showing " << ((dim == 3) ? "bdr " : "") << "attribute " << attr << endl; } - vssol3d -> PrepareLines(); - vssol3d -> Prepare(); - SendExposeEvent(); + PrepareLines(); + Prepare(); } VisualizationSceneSolution3d::VisualizationSceneSolution3d() @@ -605,8 +598,6 @@ VisualizationSceneSolution3d::VisualizationSceneSolution3d(Mesh &m, Vector &s) void VisualizationSceneSolution3d::Init() { - vssol3d = this; - cplane = 0; cp_drawmesh = 0; cp_drawelems = 1; drawlsurf = 0; @@ -680,37 +671,37 @@ void VisualizationSceneSolution3d::Init() wnd->AddKeyEvent('i', &SceneType::ToggleCuttingPlane); wnd->AddKeyEvent('I', &SceneType::ToggleCPAlgorithm); - wnd->AddKeyEvent('o', KeyoPressed); - wnd->AddKeyEvent('O', KeyOPressed); + wnd->AddKeyEvent('o', KeyoPressed, false); + wnd->AddKeyEvent('O', KeyOPressed, false); - wnd->AddKeyEvent('w', KeywPressed); - wnd->AddKeyEvent('W', KeyWPressed); + wnd->AddKeyEvent('w', &SceneType::MoveUpBdrElems); + wnd->AddKeyEvent('W', &SceneType::MoveDownBdrElems); - wnd->AddKeyEvent('x', KeyxPressed); - wnd->AddKeyEvent('X', KeyXPressed); + wnd->AddKeyEvent('x', &SceneType::RotateCPPhi); + wnd->AddKeyEvent('X', &SceneType::RotateCPPhiBack); - wnd->AddKeyEvent('y', KeyyPressed); - wnd->AddKeyEvent('Y', KeyYPressed); + wnd->AddKeyEvent('y', &SceneType::RotateCPTheta); + wnd->AddKeyEvent('Y', &SceneType::RotateCPThetaBack); - wnd->AddKeyEvent('z', KeyzPressed); - wnd->AddKeyEvent('Z', KeyZPressed); + wnd->AddKeyEvent('z', &SceneType::TranslateCP); + wnd->AddKeyEvent('Z', &SceneType::TranslateCPBack); - wnd->AddKeyEvent('u', KeyuPressed); - wnd->AddKeyEvent('U', KeyUPressed); + wnd->AddKeyEvent('u', &SceneType::MoveUpLevelSurf); + wnd->AddKeyEvent('U', &SceneType::MoveDownLevelSurf); - wnd->AddKeyEvent('v', KeyvPressed); - wnd->AddKeyEvent('V', KeyVPressed); + wnd->AddKeyEvent('v', &SceneType::AddLevelSurf); + wnd->AddKeyEvent('V', &SceneType::RemoveLevelSurf); - wnd->AddKeyEvent(SDLK_F3, KeyF3Pressed); - wnd->AddKeyEvent(SDLK_F4, KeyF4Pressed); - wnd->AddKeyEvent(SDLK_NUMLOCKCLEAR, ToggleMagicKey); + wnd->AddKeyEvent(SDLK_F3, &SceneType::ShrinkBoundaryElems); + wnd->AddKeyEvent(SDLK_F4, &SceneType::ZoomBoundaryElems); + wnd->AddKeyEvent(SDLK_NUMLOCKCLEAR, ToggleMagicKey, false); - wnd->AddKeyEvent(SDLK_F8, KeyF8Pressed); - wnd->AddKeyEvent(SDLK_F9, KeyF9Pressed); - wnd->AddKeyEvent(SDLK_F10, KeyF10Pressed); + wnd->AddKeyEvent(SDLK_F8, &SceneType::QueryToggleSubdomains); + wnd->AddKeyEvent(SDLK_F9, &SceneType::WalkNextSubdomain); + wnd->AddKeyEvent(SDLK_F10, &SceneType::WalkPrevSubdomain); - wnd->AddKeyEvent(SDLK_F11, KeyF11Pressed); - wnd->AddKeyEvent(SDLK_F12, KeyF12Pressed); + wnd->AddKeyEvent(SDLK_F11, &SceneType::ShrinkMatSubdomains); + wnd->AddKeyEvent(SDLK_F12, &SceneType::ZoomMatSubdomains); } Prepare(); PrepareLines(); diff --git a/lib/vssolution3d.hpp b/lib/vssolution3d.hpp index 2b1d8ee3..3b3f8104 100644 --- a/lib/vssolution3d.hpp +++ b/lib/vssolution3d.hpp @@ -93,6 +93,31 @@ class VisualizationSceneSolution3d : public VisualizationSceneScalarData return (n < vertices.Size()); } + void RotateCPPhi(); + void RotateCPPhiBack(); + void RotateCPTheta(); + void RotateCPThetaBack(); + void TranslateCP(); + void TranslateCPBack(); + + bool MoveUpBdrElems(); + bool MoveDownBdrElems(); + + void MoveUpLevelSurf(); + void MoveDownLevelSurf(); + void AddLevelSurf(); + void RemoveLevelSurf(); + + void QueryToggleSubdomains(); + void WalkNextSubdomain(); + void WalkPrevSubdomain(); + + bool ShrinkBoundaryElems(); + bool ZoomBoundaryElems(); + bool ShrinkMatSubdomains(); + bool ZoomMatSubdomains(); + + public: int TimesToRefine; double FaceShiftScale; From 664b7f19e474f233b6d453f29ef7d10044ad2716 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 11 Feb 2021 11:16:52 -0800 Subject: [PATCH 23/39] Make vssolution event handlers member functions --- lib/vssolution.cpp | 274 +++++++++++++++++++++++---------------------- lib/vssolution.hpp | 18 +++ 2 files changed, 159 insertions(+), 133 deletions(-) diff --git a/lib/vssolution.cpp b/lib/vssolution.cpp index c453286b..e31b19d4 100644 --- a/lib/vssolution.cpp +++ b/lib/vssolution.cpp @@ -24,8 +24,6 @@ using namespace mfem; using namespace std; -VisualizationSceneSolution *vssol; -extern VisualizationScene *locscene; extern GeometryRefiner GLVisGeometryRefiner; #ifdef GLVIS_ISFINITE @@ -122,15 +120,15 @@ std::string VisualizationSceneSolution::GetHelpString() const return os.str(); } -static void KeyF8Pressed() +void VisualizationSceneSolution::QueryToggleSubdomains() { int attr; Array attr_list(&attr, 1); - const Array &all_attr = vssol->GetMesh()->attributes; + const Array &all_attr = GetMesh()->attributes; cout << "El attributes ON: "; for (int i = 0; i < all_attr.Size(); i++) - if (vssol->el_attr_to_show[all_attr[i]-1]) + if (el_attr_to_show[all_attr[i]-1]) { cout << " " << all_attr[i]; } @@ -138,11 +136,11 @@ static void KeyF8Pressed() cout << "El attribute to toggle : " << flush; cin >> attr; - vssol->ToggleAttributes(attr_list); - SendExposeEvent(); + ToggleAttributes(attr_list); } -static void SwitchAttribute(int increment, int &attribute, +static void SwitchAttribute(VisualizationSceneSolution* vssol, + int increment, int &attribute, Array &attribute_marker, bool bdr) { @@ -190,97 +188,108 @@ static void SwitchAttribute(int increment, int &attribute, vssol->PrepareLines(); vssol->Prepare(); } - SendExposeEvent(); } -static void KeyF9Pressed(GLenum state) +static void KeyF9Pressed(GLVisWindow* wnd, GLenum state) { + VisualizationSceneSolution* vssol + = dynamic_cast(wnd->getScene()); if (!(state & KMOD_SHIFT)) { - SwitchAttribute(+1, vssol->attr_to_show, vssol->el_attr_to_show, false); + SwitchAttribute(vssol, +1, vssol->attr_to_show, vssol->el_attr_to_show, false); } else { - SwitchAttribute(+1, vssol->bdr_attr_to_show, vssol->bdr_el_attr_to_show, + SwitchAttribute(vssol, +1, vssol->bdr_attr_to_show, vssol->bdr_el_attr_to_show, true); } } -static void KeyF10Pressed(GLenum state) +static void KeyF10Pressed(GLVisWindow* wnd, GLenum state) { + VisualizationSceneSolution* vssol + = dynamic_cast(wnd->getScene()); if (!(state & KMOD_SHIFT)) { - SwitchAttribute(-1, vssol->attr_to_show, vssol->el_attr_to_show, false); + SwitchAttribute(vssol, -1, vssol->attr_to_show, vssol->el_attr_to_show, false); } else { - SwitchAttribute(-1, vssol->bdr_attr_to_show, vssol->bdr_el_attr_to_show, + SwitchAttribute(vssol, -1, vssol->bdr_attr_to_show, vssol->bdr_el_attr_to_show, true); } } -int refine_func = 0; - -static void KeyoPressed(GLenum state) +static void KeyoPressed(GLVisWindow* wnd, GLenum state) { + VisualizationSceneSolution* vssol + = dynamic_cast(wnd->getScene()); if (state & KMOD_CTRL) { vssol -> ToggleDrawOrdering(); vssol -> PrepareOrderingCurve(); - SendExposeEvent(); + wnd->SendExposeEvent(); } else { - int update = 1; - switch (refine_func) - { - case 0: - vssol -> TimesToRefine += vssol -> EdgeRefineFactor; - break; - case 1: - if (vssol -> TimesToRefine > vssol -> EdgeRefineFactor) - { - vssol -> TimesToRefine -= vssol -> EdgeRefineFactor; - } - else - { - update = 0; - } - break; - case 2: - vssol -> TimesToRefine /= vssol -> EdgeRefineFactor; - vssol -> EdgeRefineFactor ++; - vssol -> TimesToRefine *= vssol -> EdgeRefineFactor; - break; - case 3: - if (vssol -> EdgeRefineFactor > 1) - { - vssol -> TimesToRefine /= vssol -> EdgeRefineFactor; - vssol -> EdgeRefineFactor --; - vssol -> TimesToRefine *= vssol -> EdgeRefineFactor; - } - else - { - update = 0; - } - break; - } - if (update && vssol -> shading == 2) - { - vssol -> DoAutoscale(false); - vssol -> PrepareLines(); - vssol -> PrepareBoundary(); - vssol -> Prepare(); - vssol -> PrepareLevelCurves(); - vssol -> PrepareCP(); - SendExposeEvent(); - } - cout << "Subdivision factors = " << vssol -> TimesToRefine - << ", " << vssol -> EdgeRefineFactor << endl; + vssol -> DoRefineFunc(); + wnd->SendExposeEvent(); } } -static void KeyOPressed(GLenum state) +bool VisualizationSceneSolution::DoRefineFunc() +{ + int update = 1; + switch (refine_func) + { + case 0: + TimesToRefine += EdgeRefineFactor; + break; + case 1: + if (TimesToRefine > EdgeRefineFactor) + { + TimesToRefine -= EdgeRefineFactor; + } + else + { + update = 0; + } + break; + case 2: + TimesToRefine /= EdgeRefineFactor; + EdgeRefineFactor ++; + TimesToRefine *= EdgeRefineFactor; + break; + case 3: + if (EdgeRefineFactor > 1) + { + TimesToRefine /= EdgeRefineFactor; + EdgeRefineFactor --; + TimesToRefine *= EdgeRefineFactor; + } + else + { + update = 0; + } + break; + } + cout << "Subdivision factors = " << TimesToRefine + << ", " << EdgeRefineFactor << endl; + if (update && shading == 2) + { + DoAutoscale(false); + PrepareLines(); + PrepareBoundary(); + Prepare(); + PrepareLevelCurves(); + PrepareCP(); + return true; + } + return false; +} + +//static void KeyOPressed(GLenum state) +void VisualizationSceneSolution::ToggleRefineFunc() { refine_func = (refine_func+1)%4; cout << "Key 'o' will: "; @@ -306,95 +315,95 @@ void KeyIPressed() // no-op, available } -static void KeyyPressed() +void VisualizationSceneSolution::RotateCP() { - vssol->CuttingPlane->IncreaseTheta(); - vssol->PrepareCP(); - SendExposeEvent(); + CuttingPlane->IncreaseTheta(); + PrepareCP(); } -static void KeyYPressed() +void VisualizationSceneSolution::RotateCPBack() { - vssol->CuttingPlane->DecreaseTheta(); - vssol->PrepareCP(); - SendExposeEvent(); + CuttingPlane->DecreaseTheta(); + PrepareCP(); } -static void KeyzPressed() +void VisualizationSceneSolution::TranslateCP() { - vssol->CuttingPlane->IncreaseDistance(); - vssol->PrepareCP(); - SendExposeEvent(); + CuttingPlane->IncreaseDistance(); + PrepareCP(); } -static void KeyZPressed() +void VisualizationSceneSolution::TranslateCPBack() { - vssol->CuttingPlane->DecreaseDistance(); - vssol->PrepareCP(); - SendExposeEvent(); + CuttingPlane->DecreaseDistance(); + PrepareCP(); } -static void KeyF3Pressed() +bool VisualizationSceneSolution::ShrinkElements() { - if (vssol->shading == 2) + if (shading == 2) { - vssol->shrink *= 0.9; - vssol->Prepare(); - vssol->PrepareLines(); - vssol->PrepareLevelCurves(); - vssol->PrepareNumbering(); - vssol->PrepareOrderingCurve(); - SendExposeEvent(); + shrink *= 0.9; + Prepare(); + PrepareLines(); + PrepareLevelCurves(); + PrepareNumbering(); + PrepareOrderingCurve(); + return true; } + return false; } -static void KeyF4Pressed() +bool VisualizationSceneSolution::ZoomElements() { - if (vssol->shading == 2) + if (shading == 2) { - vssol->shrink *= 1.11111111111111111111111; - vssol->Prepare(); - vssol->PrepareLines(); - vssol->PrepareLevelCurves(); - vssol->PrepareNumbering(); - SendExposeEvent(); + shrink *= 1.11111111111111111111111; + Prepare(); + PrepareLines(); + PrepareLevelCurves(); + PrepareNumbering(); + return true; } + return false; } -static void KeyF11Pressed() +bool VisualizationSceneSolution::ShrinkMatSubdomains() { - if (vssol->shading == 2) + if (shading == 2) { - if (vssol->matc.Width() == 0) + if (matc.Width() == 0) { - vssol->ComputeElemAttrCenter(); + ComputeElemAttrCenter(); } - vssol->shrinkmat *= 0.9; - vssol->Prepare(); - vssol->PrepareLines(); - vssol->PrepareBoundary(); - vssol->PrepareLevelCurves(); - vssol->PrepareNumbering(); - SendExposeEvent(); + shrinkmat *= 0.9; + Prepare(); + PrepareLines(); + PrepareBoundary(); + PrepareLevelCurves(); + PrepareNumbering(); + return true; } + return false; } -static void KeyF12Pressed() +bool VisualizationSceneSolution::ZoomMatSubdomains() { - if (vssol->shading == 2) + if (shading == 2) { - if (vssol->matc.Width() == 0) + if (matc.Width() == 0) { - vssol->ComputeElemAttrCenter(); + ComputeElemAttrCenter(); } - vssol->shrinkmat *= 1.11111111111111111111111; - vssol->Prepare(); - vssol->PrepareLines(); - vssol->PrepareBoundary(); - vssol->PrepareLevelCurves(); - vssol->PrepareNumbering(); - SendExposeEvent(); + shrinkmat *= 1.11111111111111111111111; + Prepare(); + PrepareLines(); + PrepareBoundary(); + PrepareLevelCurves(); + PrepareNumbering(); + return true; } + return false; } VisualizationSceneSolution::VisualizationSceneSolution() @@ -415,7 +424,6 @@ VisualizationSceneSolution::VisualizationSceneSolution( void VisualizationSceneSolution::Init() { rsol = NULL; - vssol = this; drawelems = shading = 1; drawmesh = 0; @@ -463,8 +471,8 @@ void VisualizationSceneSolution::Init() wnd->AddKeyEvent('n', &SceneType::ToggleDrawNumberings); wnd->AddKeyEvent('N', &SceneType::ToggleDrawNumberings); - wnd->AddKeyEvent('o', KeyoPressed); - wnd->AddKeyEvent('O', KeyOPressed); + wnd->AddKeyEvent('o', KeyoPressed, false); // expose handled in function + wnd->AddKeyEvent('O', &SceneType::ToggleRefineFunc); wnd->AddKeyEvent('e', &SceneType::ToggleDrawElems); wnd->AddKeyEvent('E', &SceneType::ToggleDrawElems); @@ -475,18 +483,18 @@ void VisualizationSceneSolution::Init() wnd->AddKeyEvent('i', &SceneType::ToggleDrawCP); //wnd->AddKeyEvent('I', KeyIPressed); - wnd->AddKeyEvent('y', KeyyPressed); - wnd->AddKeyEvent('Y', KeyYPressed); - wnd->AddKeyEvent('z', KeyzPressed); - wnd->AddKeyEvent('Z', KeyZPressed); + wnd->AddKeyEvent('y', &SceneType::RotateCP); + wnd->AddKeyEvent('Y', &SceneType::RotateCPBack); + wnd->AddKeyEvent('z', &SceneType::TranslateCP); + wnd->AddKeyEvent('Z', &SceneType::TranslateCPBack); - wnd->AddKeyEvent(SDLK_F3, KeyF3Pressed); - wnd->AddKeyEvent(SDLK_F4, KeyF4Pressed); - wnd->AddKeyEvent(SDLK_F8, KeyF8Pressed); + wnd->AddKeyEvent(SDLK_F3, &SceneType::ShrinkElements); + wnd->AddKeyEvent(SDLK_F4, &SceneType::ZoomElements); + wnd->AddKeyEvent(SDLK_F8, &SceneType::QueryToggleSubdomains); wnd->AddKeyEvent(SDLK_F9, KeyF9Pressed); wnd->AddKeyEvent(SDLK_F10, KeyF10Pressed); - wnd->AddKeyEvent(SDLK_F11, KeyF11Pressed); - wnd->AddKeyEvent(SDLK_F12, KeyF12Pressed); + wnd->AddKeyEvent(SDLK_F11, &SceneType::ShrinkMatSubdomains); + wnd->AddKeyEvent(SDLK_F12, &SceneType::ZoomMatSubdomains); } Prepare(); diff --git a/lib/vssolution.hpp b/lib/vssolution.hpp index 5c178c5a..2a1de86a 100644 --- a/lib/vssolution.hpp +++ b/lib/vssolution.hpp @@ -74,6 +74,22 @@ class VisualizationSceneSolution : public VisualizationSceneScalarData // slow. Turn it off above some entity count. static const int MAX_RENDER_NUMBERING = 1000; + void RotateCP(); + void RotateCPBack(); + void TranslateCP(); + void TranslateCPBack(); + + bool ShrinkElements(); + bool ZoomElements(); + + void QueryToggleSubdomains(); + + bool ShrinkMatSubdomains(); + bool ZoomMatSubdomains(); + + int refine_func = 0; + + public: int shading, TimesToRefine, EdgeRefineFactor; @@ -153,6 +169,8 @@ class VisualizationSceneSolution : public VisualizationSceneScalarData void ToggleDrawCP() { draw_cp = !draw_cp; PrepareCP(); } + void ToggleRefineFunc(); + bool DoRefineFunc(); virtual void SetRefineFactors(int, int); virtual void AutoRefine(); virtual void ToggleAttributes(Array &attr_list); From bfb252c4c1917eb0adfbd740207cb259990fd54c Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 11 Feb 2021 11:22:31 -0800 Subject: [PATCH 24/39] Make vsvector event handlers member functions --- lib/vsvector.cpp | 33 +++++++++++---------------------- lib/vsvector.hpp | 4 ++++ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/lib/vsvector.cpp b/lib/vsvector.cpp index 495cfbd3..c0aea798 100644 --- a/lib/vsvector.cpp +++ b/lib/vsvector.cpp @@ -99,9 +99,6 @@ std::string VisualizationSceneVector::GetHelpString() const return os.str(); } -VisualizationSceneVector * vsvector; -extern VisualizationScene * locscene; -extern VisualizationSceneSolution * vssol; extern GeometryRefiner GLVisGeometryRefiner; void VisualizationSceneVector::NextDisplacement() @@ -134,22 +131,19 @@ void VisualizationSceneVector::QueryArrowScaling() PrepareVectorField(); } -int key_u_func = 0; - -void KeyuPressed() +void VisualizationSceneVector::DoKeyU() { int update = 1; - switch (key_u_func) { case 0: - vsvector->RefineFactor++; + RefineFactor++; break; case 1: - if (vsvector->RefineFactor > 1) + if (RefineFactor > 1) { - vsvector->RefineFactor--; + RefineFactor--; } else { @@ -158,8 +152,7 @@ void KeyuPressed() break; case 2: - vsvector->CycleVec2Scalar(1); - SendExposeEvent(); + CycleVec2Scalar(1); break; } @@ -167,13 +160,11 @@ void KeyuPressed() { case 0: case 1: - if (update && vsvector->shading == 2) + if (update && shading == 2) { - vsvector->PrepareVectorField(); - SendExposeEvent(); + PrepareVectorField(); } - cout << "Vector subdivision factor = " - << vsvector->RefineFactor << endl; + cout << "Vector subdivision factor = " << RefineFactor << endl; break; case 2: @@ -181,7 +172,7 @@ void KeyuPressed() } } -void KeyUPressed() +void VisualizationSceneVector::ToggleKeyUFunc() { key_u_func = (key_u_func+1)%3; cout << "Key 'u' will: "; @@ -475,8 +466,6 @@ void VisualizationSceneVector::Init() PrepareVectorField(); // PrepareDisplacedMesh(); // called by PrepareLines() - vsvector = this; - // static int init = 0; // if (!init) { @@ -490,8 +479,8 @@ void VisualizationSceneVector::Init() wnd->AddKeyEvent('b', &SceneType::PrevDisplacement); wnd->AddKeyEvent('v', &SceneType::ToggleVectorField); wnd->AddKeyEvent('V', &SceneType::QueryArrowScaling); - wnd->AddKeyEvent('u', KeyuPressed); - wnd->AddKeyEvent('U', KeyUPressed); + wnd->AddKeyEvent('u', &SceneType::DoKeyU); + wnd->AddKeyEvent('U', &SceneType::ToggleKeyUFunc); } // Vec2Scalar is VecLength diff --git a/lib/vsvector.hpp b/lib/vsvector.hpp index c8d4522f..b4319009 100644 --- a/lib/vsvector.hpp +++ b/lib/vsvector.hpp @@ -52,6 +52,10 @@ class VisualizationSceneVector : public VisualizationSceneSolution void QueryArrowScaling(); + int key_u_func = 0; + void ToggleKeyUFunc(); + void DoKeyU(); + public: VisualizationSceneVector(Mesh &m, Vector &sx, Vector &sy); VisualizationSceneVector(GridFunction &vgf); From 8100b38ea3863ccff3e1cada4c712f4fdd4ef884 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 11 Feb 2021 11:41:31 -0800 Subject: [PATCH 25/39] Convert last of vsdata event handlers --- glvis.cpp | 1 + lib/threads.cpp | 2 +- lib/vsdata.cpp | 121 +++++++++++++++++++++++------------------------- lib/vsdata.hpp | 16 +++++-- 4 files changed, 73 insertions(+), 67 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 268428be..028db3b8 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -619,6 +619,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) char delim; scr >> ws >> delim; getline(scr, plot_caption, delim); + vs->SetCaption(plot_caption); vs->PrepareCaption(); // turn on or off the caption wnd->MyExpose(); } diff --git a/lib/threads.cpp b/lib/threads.cpp index b7361226..2ec9e46e 100644 --- a/lib/threads.cpp +++ b/lib/threads.cpp @@ -499,7 +499,7 @@ int GLVisCommand::Execute() case PLOT_CAPTION: { cout << "Command: plot_caption: " << plot_caption << endl; - ::plot_caption = plot_caption; + vs->SetCaption(plot_caption); vs->PrepareCaption(); // turn on or off the caption window->MyExpose(); break; diff --git a/lib/vsdata.cpp b/lib/vsdata.cpp index 8d9272d3..b10b890f 100644 --- a/lib/vsdata.cpp +++ b/lib/vsdata.cpp @@ -463,14 +463,12 @@ void VisualizationSceneScalarData::PrepareCaption() font->getObjectSize(caption, caption_w, caption_h); } -VisualizationSceneScalarData * vsdata; - -void KeyCPressed() +//void KeyCPressed() +void VisualizationSceneScalarData::QueryCaption() { cout << "Enter new caption: " << flush; std::getline(cin, plot_caption); - vsdata->PrepareCaption(); // turn on or off the caption - SendExposeEvent(); + PrepareCaption(); // turn on or off the caption } void Key_Mod_a_Pressed(GLVisWindow* wnd, GLenum state) @@ -492,15 +490,15 @@ void Key_Mod_a_Pressed(GLVisWindow* wnd, GLenum state) } } -void KeyHPressed() +//void KeyHPressed() +void VisualizationSceneScalarData::PrintHelpString() { - cout << vsdata->GetHelpString() << flush; + cout << GetHelpString() << flush; } -void KeyLPressed() +void VisualizationSceneScalarData::ToggleLogscale() { - vsdata->ToggleLogscale(true); - SendExposeEvent(); + ToggleLogscale(true); } void VisualizationSceneScalarData::Reset3DView() @@ -526,13 +524,15 @@ void KeypPressed(GLVisWindow* wnd, GLenum state) } } -void KeyPPressed() +void KeyPPressed(GLVisWindow* wnd) { + VisualizationSceneScalarData * vsdata + = dynamic_cast(wnd->getScene()); vsdata->palette.PrevIndex(); - SendExposeEvent(); } -static void KeyF5Pressed() +//static void KeyF5Pressed() +void VisualizationSceneScalarData::QueryLevelLines() { int n; double min, max; @@ -544,15 +544,15 @@ static void KeyF5Pressed() cout << "Enter n : " << flush; cin >> n; - vsdata -> SetLevelLines (min, max, n, 0); + SetLevelLines (min, max, n, 0); - vsdata -> UpdateLevelLines(); - SendExposeEvent(); + UpdateLevelLines(); } -void KeyF6Pressed() +//void KeyF6Pressed() +void VisualizationSceneScalarData::QueryPaletteSettings() { - int RepeatPaletteTimes = vsdata->palette.GetRepeatTimes(); + int RepeatPaletteTimes = palette.GetRepeatTimes(); cout << "Palette is repeated " << RepeatPaletteTimes << " times.\n" << "(Negative value means the palette is flipped.)\n" << "Enter new value: " << flush; @@ -563,31 +563,32 @@ void KeyF6Pressed() } cout << "Palette will be repeated " << RepeatPaletteTimes << " times now.\n\n"; - vsdata->palette.SetRepeatTimes(RepeatPaletteTimes); + palette.SetRepeatTimes(RepeatPaletteTimes); - int pal = vsdata->palette.ChoosePalette(); + int pal = palette.ChoosePalette(); - int colors_used = vsdata->palette.GetNumColors(pal); - int palette_size = vsdata->palette.GetSize(pal); + int colors_used = palette.GetNumColors(pal); + int palette_size = palette.GetSize(pal); cout << "\nPalette is using " << colors_used << " colors.\n" << "Enter new value (0 = use original " << palette_size << " colors): " << flush; cin >> colors_used; if (colors_used == 1) { colors_used = 0; } - vsdata->palette.SetNumColors(colors_used); + palette.SetNumColors(colors_used); - vsdata->palette.Init(); - vsdata->palette.SetIndex(pal); + palette.Init(); + palette.SetIndex(pal); - colors_used = vsdata->palette.GetNumColors(); + colors_used = palette.GetNumColors(); cout << "Palette will be using " << colors_used << " colors now.\n"; - vsdata->EventUpdateColors(); - SendExposeEvent(); + EventUpdateColors(); } -void KeyF7Pressed(GLenum state) +void KeyF7Pressed(GLVisWindow* wnd, GLenum state) { + VisualizationSceneScalarData * vsdata + = dynamic_cast(wnd->getScene()); if (state & KMOD_SHIFT) { cout << "Current bounding box:\n" @@ -614,7 +615,6 @@ void KeyF7Pressed(GLenum state) << " max: (" << vsdata->x[1] << ',' << vsdata->y[1] << ',' << vsdata->z[1] << ")\n" << flush; vsdata->UpdateBoundingBox(); - SendExposeEvent(); } else { @@ -625,11 +625,11 @@ void KeyF7Pressed(GLenum state) cout << "New value for maxv: " << flush; cin >> vsdata->GetMaxV(); vsdata->UpdateValueRange(true); - SendExposeEvent(); } } -void KeyBackslashPressed() +//void KeyBackslashPressed() +void VisualizationSceneScalarData::QueryLightPosition() { float x, y, z, w; @@ -645,34 +645,32 @@ void KeyBackslashPressed() cout << "w = " << flush; cin >> w; - vsdata->SetLight0CustomPos({x, y, z, w}); - SendExposeEvent(); + SetLight0CustomPos({x, y, z, w}); } -void KeyTPressed() +//void KeyTPressed() +void VisualizationSceneScalarData::NextLightMatSetting() { int ml; - ml = (vsdata->GetLightMatIdx() + 1) % 5; - vsdata->SetLightMatIdx(ml); - SendExposeEvent(); + ml = (GetLightMatIdx() + 1) % 5; + SetLightMatIdx(ml); cout << "New material/light : " << ml << endl; } -void KeyGPressed() +void VisualizationSceneScalarData::ToggleBackground() { - vsdata->ToggleBackground(); - vsdata->PrepareAxes(); - vsdata->EventUpdateBackground(); - SendExposeEvent(); + VisualizationScene::ToggleBackground(); + PrepareAxes(); + EventUpdateBackground(); } -void KeyF2Pressed() +//void KeyF2Pressed() +void VisualizationSceneScalarData::RedrawColors() { - vsdata -> EventUpdateColors(); - vsdata -> PrepareLines(); - // vsdata->CPPrepare(); - SendExposeEvent(); + EventUpdateColors(); + PrepareLines(); + // CPPrepare(); } void VisualizationSceneScalarData::PrintLogscale(bool warn) @@ -964,9 +962,6 @@ VisualizationSceneScalarData::VisualizationSceneScalarData( void VisualizationSceneScalarData::Init() { - vsdata = this; - wnd = GetAppWindow(); - arrow_type = arrow_scaling_type = 0; scaling = 0; drawaxes = colorbar = 0; @@ -991,7 +986,7 @@ void VisualizationSceneScalarData::Init() using SceneType = VisualizationSceneScalarData; wnd->AddKeyEvent('l', &SceneType::ToggleLight); - wnd->AddKeyEvent('L', KeyLPressed); + wnd->AddKeyEvent('L', &SceneType::ToggleLogscale); wnd->AddKeyEvent('s', &SceneType::ToggleScaling); @@ -1004,28 +999,28 @@ void VisualizationSceneScalarData::Init() wnd->AddKeyEvent('p', KeypPressed); wnd->AddKeyEvent('P', KeyPPressed); - wnd->AddKeyEvent('h', KeyHPressed); - wnd->AddKeyEvent('H', KeyHPressed); + wnd->AddKeyEvent('h', &SceneType::PrintHelpString); + wnd->AddKeyEvent('H', &SceneType::PrintHelpString); - wnd->AddKeyEvent(SDLK_F5, KeyF5Pressed); - wnd->AddKeyEvent(SDLK_F6, KeyF6Pressed); + wnd->AddKeyEvent(SDLK_F5, &SceneType::QueryLevelLines); + wnd->AddKeyEvent(SDLK_F6, &SceneType::QueryPaletteSettings); wnd->AddKeyEvent(SDLK_F7, KeyF7Pressed); - wnd->AddKeyEvent(SDLK_BACKSLASH, KeyBackslashPressed); - wnd->AddKeyEvent('t', KeyTPressed); - wnd->AddKeyEvent('T', KeyTPressed); + wnd->AddKeyEvent(SDLK_BACKSLASH, &SceneType::QueryLightPosition); + wnd->AddKeyEvent('t', &SceneType::NextLightMatSetting); + wnd->AddKeyEvent('T', &SceneType::NextLightMatSetting); - wnd->AddKeyEvent('g', KeyGPressed); - wnd->AddKeyEvent('G', KeyGPressed); + wnd->AddKeyEvent('g', &SceneType::ToggleBackground); + wnd->AddKeyEvent('G', &SceneType::ToggleBackground); wnd->AddKeyEvent('c', &SceneType::ToggleDrawColorbar); - wnd->AddKeyEvent('C', KeyCPressed); + wnd->AddKeyEvent('C', &SceneType::QueryCaption); wnd->AddKeyEvent('k', &SceneType::DecrementAlpha); wnd->AddKeyEvent('K', &SceneType::IncrementAlpha); wnd->AddKeyEvent(SDLK_F1, &SceneType::PrintState, false); - wnd->AddKeyEvent(SDLK_F2, KeyF2Pressed); + wnd->AddKeyEvent(SDLK_F2, &SceneType::RedrawColors); wnd->AddKeyEvent(SDLK_COMMA, &SceneType::DecrementAlphaCenter); wnd->AddKeyEvent(SDLK_LESS, &SceneType::IncrementAlphaCenter); diff --git a/lib/vsdata.hpp b/lib/vsdata.hpp index a7d5aeed..449cbfba 100644 --- a/lib/vsdata.hpp +++ b/lib/vsdata.hpp @@ -19,9 +19,6 @@ using namespace mfem; -extern std::string plot_caption; // defined in glvis.cpp -extern std::string extra_caption; // defined in glvis.cpp - class Plane { private: @@ -61,6 +58,8 @@ class VisualizationSceneScalarData : public VisualizationScene double minv, maxv; + std::string plot_caption = ""; + std::string extra_caption = ""; std::string a_label_x, a_label_y, a_label_z; int scaling, colorbar, drawaxes; @@ -127,6 +126,16 @@ class VisualizationSceneScalarData : public VisualizationScene void Cone(gl3::GlBuilder& builder, glm::mat4 transform); + void PrintHelpString(); + void QueryCaption(); + void QueryLevelLines(); + void QueryPaletteSettings(); + void QueryLightPosition(); + void NextLightMatSetting(); + void RedrawColors(); + void ToggleBackground(); + void ToggleLogscale(); + public: Plane *CuttingPlane; int key_r_state; @@ -240,6 +249,7 @@ class VisualizationSceneScalarData : public VisualizationScene Array * level = NULL, Array * levels = NULL); + void SetCaption(std::string caption) { plot_caption = caption; } void SetAxisLabels(const char * a_x, const char * a_y, const char * a_z); void PrepareAxes(); From 4b6f7b745c7c733b2e968c10c69b05e0aa85fbd3 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 11 Feb 2021 12:21:57 -0800 Subject: [PATCH 26/39] Remove SdlWindow reference from vsdata --- lib/aux_vis.cpp | 3 +++ lib/openglvis.hpp | 4 ++-- lib/vsdata.cpp | 10 +++------- lib/vsdata.hpp | 8 ++++++++ 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 04a7fe6c..1f95badc 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -495,6 +495,9 @@ void GLVisWindow::MyExpose(GLsizei w, GLsizei h) std::array bgcol = locscene->GetBackgroundColor(); wnd->getRenderer().setClearColor(bgcol[0], bgcol[1], bgcol[2], bgcol[3]); + int wnd_w, wnd_h; + wnd->getWindowSize(wnd_w, wnd_h); + locscene->UpdateWindowSize(wnd_w, wnd_h, w, h); gl3::SceneInfo frame = locscene->GetSceneObjs(); for (auto drawable_ptr : frame.needs_buffering) { diff --git a/lib/openglvis.hpp b/lib/openglvis.hpp index 8a2378db..c2ca6cdf 100644 --- a/lib/openglvis.hpp +++ b/lib/openglvis.hpp @@ -65,8 +65,6 @@ class VisualizationScene // How to scale the visualized object(s) double xscale, yscale, zscale; - SdlWindow * wnd; - glm::mat4 proj_mtx; enum @@ -152,6 +150,8 @@ class VisualizationScene float matAlpha = 1.0; float matAlphaCenter = 0.5; + /// Needs to be called before GetSceneObjs() + virtual void UpdateWindowSize(int w, int h, int gl_w, int gl_h) = 0; virtual gl3::SceneInfo GetSceneObjs() = 0; void SetView(double theta, double phi); diff --git a/lib/vsdata.cpp b/lib/vsdata.cpp index b10b890f..30c9abfd 100644 --- a/lib/vsdata.cpp +++ b/lib/vsdata.cpp @@ -859,8 +859,6 @@ void VisualizationSceneScalarData::Toggle2DView() gl3::SceneInfo VisualizationSceneScalarData::GetSceneObjs() { - int w, h; - wnd->getWindowSize(w, h); gl3::SceneInfo scene {}; scene.needs_buffering = std::move(updated_bufs); updated_bufs.clear(); @@ -885,13 +883,11 @@ gl3::SceneInfo VisualizationSceneScalarData::GetSceneObjs() { // caption size is in screen pixels and needs to be centered with // GL pixel size - int gl_w, gl_h; - wnd->getGLDrawSize(gl_w, gl_h); // add caption to draw list double v_pos = 2.; double line_h = font->getFontLineSpacing(); - params.model_view.translate(-(double)caption_w / gl_w, - 1.0 - 2 * v_pos * line_h / gl_h, 0.0); + params.model_view.translate(-(double)caption_w / draw_w, + 1.0 - 2 * v_pos * line_h / draw_h, 0.0); scene.queue.emplace_back(params, &caption_buf); } params.contains_translucent = true; @@ -901,7 +897,7 @@ gl3::SceneInfo VisualizationSceneScalarData::GetSceneObjs() params.projection.ortho(-1.,1.,-1.,1.,-2.,2.); params.model_view.identity(); params.model_view.translate(-1, -1, 0.0); - params.model_view.scale(40.0 / w, 40.0 / h, 1); + params.model_view.scale(40.0 / window_w, 40.0 / window_h, 1); params.model_view.translate(2.0, 2.0, 0.0); params.model_view.mult(cam.RotMatrix()); params.model_view.mult(rotmat); diff --git a/lib/vsdata.hpp b/lib/vsdata.hpp index 449cbfba..f749d888 100644 --- a/lib/vsdata.hpp +++ b/lib/vsdata.hpp @@ -58,6 +58,9 @@ class VisualizationSceneScalarData : public VisualizationScene double minv, maxv; + int window_w, window_h; + int draw_w, draw_h; + std::string plot_caption = ""; std::string extra_caption = ""; std::string a_label_x, a_label_y, a_label_z; @@ -206,6 +209,11 @@ class VisualizationSceneScalarData : public VisualizationScene Mesh *GetMesh() { return mesh; } + void UpdateWindowSize(int w, int h, int gl_w, int gl_h) + { + window_w = w; window_h = h; + draw_w = gl_w; draw_h = gl_h; + } virtual gl3::SceneInfo GetSceneObjs(); double &GetMinV() { return minv; } From 2e9032d4c109ed40e6cd8972119549b766a5b791 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 11 Feb 2021 12:42:24 -0800 Subject: [PATCH 27/39] Fix for printing --- lib/aux_vis.cpp | 6 +++--- lib/aux_vis.hpp | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 1f95badc..86a2ac6b 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -101,11 +101,11 @@ KeyDelegate CreateKeyEvent(T* inst, void (T::*func)()) return [inst, func](GLenum) { (inst->*func)(); }; } -bool KeyPrint(GLVisWindow* wnd, GLenum mod) +void GLVisWindow::KeyPrint(GLenum mod) { if (mod & KMOD_CTRL) { - wnd->PrintToPDF(); + PrintToPDF(); } } @@ -164,7 +164,7 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega wnd->setTouchPinchCallback(TouchPinch); SetKeyEventHandler('A', &GLVisWindow::ToggleAntialiasing); - SetKeyEventHandler ('p', &GLVisWindow::PrintToPDF); + SetKeyEventHandler ('p', &GLVisWindow::KeyPrint); SetKeyEventHandler ('r', &GLVisWindow::StopSpinning); SetKeyEventHandler ('R', &GLVisWindow::StopSpinning); SetKeyEventHandler (SDLK_s, &GLVisWindow::Screenshot); diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 0d91d580..7b0a5755 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -181,6 +181,8 @@ class GLVisWindow // Internal event handler for toggling state of threads void ThreadsPauseFunc(GLenum); + void KeyPrint(GLenum); + std::unique_ptr wnd; std::unique_ptr locscene; std::unique_ptr glvis_command; From 24485b3a69a70b9edee8f63620c9688e458487c4 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Thu, 11 Feb 2021 12:45:55 -0800 Subject: [PATCH 28/39] Cleanup globals from aux_vis --- lib/aux_vis.cpp | 54 ++----------------------------------------- lib/aux_vis.hpp | 23 ------------------ lib/openglvis.cpp | 6 ++++- lib/openglvis.hpp | 4 ++++ lib/stream_reader.cpp | 8 ++++++- lib/stream_reader.hpp | 2 +- lib/vsdata.cpp | 15 +++++++----- lib/vsdata.hpp | 8 +++++-- lib/vssolution.cpp | 7 ++---- lib/vssolution.hpp | 4 ++-- lib/vssolution3d.cpp | 7 ++---- lib/vssolution3d.hpp | 4 ++-- lib/vsvector.cpp | 8 ++----- lib/vsvector.hpp | 4 ++-- lib/vsvector3d.cpp | 9 ++------ lib/vsvector3d.hpp | 4 ++-- 16 files changed, 50 insertions(+), 117 deletions(-) diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 86a2ac6b..412de455 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -34,9 +34,6 @@ using namespace mfem; #include #endif -int visualize = 0; -VisualizationScene * locscene; - #ifdef GLVIS_MULTISAMPLE static int glvis_multisample = GLVIS_MULTISAMPLE; #else @@ -46,23 +43,7 @@ static int glvis_multisample = -1; float line_w = 1.f; float line_w_aa = gl3::LINE_WIDTH_AA; -[[deprecated]] SdlWindow* wnd; -[[deprecated]] GLVisWindow * glvis_wnd = nullptr; - -SdlWindow * GetAppWindow() -{ - return wnd; -} - -GLVisWindow * GetGLVisWindow() -{ - return glvis_wnd; -} - -VisualizationScene * GetVisualizationScene() -{ - return locscene; -} +void MainLoop(GLVisWindow* wnd); struct GLVisWindow::RotationControl { @@ -119,13 +100,11 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega cout << "OpenGL Visualization" << endl; #endif rot_data->wnd = this; - ::glvis_wnd = this; wnd.reset(new SdlWindow()); if (!wnd->createWindow(name, x, y, w, h, legacyGlOnly)) { throw std::runtime_error("Could not create an SDL window."); } - ::wnd = wnd.get(); #ifdef GLVIS_DEBUG cout << "Window should be up" << endl; #endif @@ -257,7 +236,7 @@ void GLVisWindow::InitVisualization(int field_type, StreamState state, } #endif - locscene = prob_state.CreateVisualizationScene(field_type); + locscene = prob_state.CreateVisualizationScene(this, field_type); if (prob_state.mesh->SpaceDimension() == 2 && field_type == 2) { @@ -307,16 +286,6 @@ void GLVisWindow::SetKeyEventHandler(int key, void (GLVisWindow::*handler)(GLenu SetupHandledKey(key); } -void SendKeySequence(const char *seq) -{ - glvis_wnd->SendKeySequence(seq); -} - -void CallKeySequence(const char *seq) -{ - glvis_wnd->CallKeySequence(seq); -} - void GLVisWindow::SendKeySequence(const char *seq) { for (const char* key = seq; *key != '\0'; key++) @@ -431,16 +400,6 @@ void GLVisWindow::RunVisualization() #endif } -void SendExposeEvent() -{ - glvis_wnd->SendExposeEvent(); -} - -void MyExpose() -{ - glvis_wnd->MyExpose(); -} - void GLVisWindow::SendExposeEvent() { if (disableSendExposeEvent) { return; } @@ -1381,20 +1340,11 @@ void SetMultisample(int m) void SetLineWidth(float width) { line_w = width; - if (wnd) - { - wnd->getRenderer().setLineWidth(line_w); - } } void SetLineWidthMS(float width_ms) { line_w_aa = width_ms; - if (wnd) - { - wnd->getRenderer().setLineWidthMS(line_w_aa); - } - } float GetLineWidth() diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 7b0a5755..58e8a4b5 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -211,18 +211,6 @@ class GLVisWindow std::unique_ptr rot_data; }; - -/// Send expose event. In our case MyReshape is executed and Draw after it. -[[deprecated]] void SendExposeEvent(); - -[[deprecated]] void MyExpose(); - -void MainLoop(GLVisWindow* wnd); - -[[deprecated]] SdlWindow * GetAppWindow(); -[[deprecated]] GLVisWindow * GetGLVisWindow(); -VisualizationScene * GetVisualizationScene(); - void TouchPinch(SDL_MultiGestureEvent & e); @@ -230,17 +218,6 @@ void TouchPinch(SDL_MultiGestureEvent & e); /// Take a screenshot using libtiff, libpng or sdl2 //int Screenshot(const char *fname, bool convert = false); -/// Send a sequence of keystrokes to the visualization window -void SendKeySequence(const char *seq); - -// Directly call the functions assigned to the given keys. Unlike the above -// function, SendKeySequence(), this function does not send X events and -// actually disables the function SendExposeEvent() used by many of the -// functions assigned to keys. Call MyExpose() after calling this function to -// update the visualization window. -void CallKeySequence(const char *seq); - - int GetMultisample(); void SetMultisample(int m); diff --git a/lib/openglvis.cpp b/lib/openglvis.cpp index e2cf8754..c7a60e04 100644 --- a/lib/openglvis.cpp +++ b/lib/openglvis.cpp @@ -128,11 +128,15 @@ VisualizationScene::VisualizationScene() use_light = true; palette.Init(); - font = GetGLVisWindow()->getFont(); } VisualizationScene::~VisualizationScene() {} +void VisualizationScene::Init(GLVisWindow* wnd) +{ + font = wnd->getFont(); +} + void VisualizationScene ::DrawTriangle(gl3::GlDrawable& buff, const double (&pts)[4][3], const double (&cv)[4], diff --git a/lib/openglvis.hpp b/lib/openglvis.hpp index c2ca6cdf..4712fe08 100644 --- a/lib/openglvis.hpp +++ b/lib/openglvis.hpp @@ -21,6 +21,8 @@ #include "mfem.hpp" #include "geom_utils.hpp" +class GLVisWindow; + // Visualization header file class Camera @@ -134,6 +136,8 @@ class VisualizationScene VisualizationScene(); virtual ~VisualizationScene(); + virtual void Init(GLVisWindow* wnd); + int spinning, OrthogonalProjection, print, movie; double ViewAngle, ViewScale; double ViewCenterX, ViewCenterY; diff --git a/lib/stream_reader.cpp b/lib/stream_reader.cpp index f35e5dd7..b62f940e 100644 --- a/lib/stream_reader.cpp +++ b/lib/stream_reader.cpp @@ -357,7 +357,7 @@ ProjectVectorFEGridFunction(std::unique_ptr gf) } std::unique_ptr -StreamState::CreateVisualizationScene(int field_type) +StreamState::CreateVisualizationScene(GLVisWindow* wnd, int field_type) { std::unique_ptr vs; double mesh_range = -1.0; @@ -378,6 +378,8 @@ StreamState::CreateVisualizationScene(int field_type) { vss = new VisualizationSceneSolution(*mesh, sol); } + // initialize events and initial scene state + vss->Init(wnd); vs.reset(vss); if (grid_f) { @@ -396,6 +398,8 @@ StreamState::CreateVisualizationScene(int field_type) else if (mesh->SpaceDimension() == 3) { VisualizationSceneSolution3d *vss = new VisualizationSceneSolution3d(*mesh, sol); + // initialize events and initial scene state + vss->Init(wnd); vs.reset(vss); if (grid_f) { @@ -456,6 +460,8 @@ StreamState::CreateVisualizationScene(int field_type) vs.reset(new VisualizationSceneVector3d(*mesh, solu, solv, solw)); } } + // initialize events and initial scene state + vs->Init(wnd); } if (vs) diff --git a/lib/stream_reader.hpp b/lib/stream_reader.hpp index 6956fd6e..d80a2b04 100644 --- a/lib/stream_reader.hpp +++ b/lib/stream_reader.hpp @@ -35,7 +35,7 @@ struct StreamState int ReadStream(std::istream &is, const std::string &data_type); - std::unique_ptr CreateVisualizationScene(int field_type); + std::unique_ptr CreateVisualizationScene(GLVisWindow* wnd, int field_type); /// Sets a new mesh and solution from another StreamState object, and /// updates the given VisualizationScene pointer with the new data. diff --git a/lib/vsdata.cpp b/lib/vsdata.cpp index 30c9abfd..e17d8ffd 100644 --- a/lib/vsdata.cpp +++ b/lib/vsdata.cpp @@ -952,12 +952,13 @@ VisualizationSceneScalarData::VisualizationSceneScalarData( { mesh = &m; sol = &s; - - Init(); } -void VisualizationSceneScalarData::Init() +void VisualizationSceneScalarData::Init(GLVisWindow* wnd) { + // perform base class initialization first + VisualizationScene::Init(wnd); + arrow_type = arrow_scaling_type = 0; scaling = 0; drawaxes = colorbar = 0; @@ -977,8 +978,6 @@ void VisualizationSceneScalarData::Init() // if (!init) { // init = 1; - GLVisWindow* wnd = GetGLVisWindow(); - using SceneType = VisualizationSceneScalarData; wnd->AddKeyEvent('l', &SceneType::ToggleLight); @@ -1057,6 +1056,10 @@ void VisualizationSceneScalarData::Init() PrepareRuler(); autoscale = 1; + + saved_key_func = [wnd]() -> std::string { + return wnd->getSdl()->getSavedKeys(); + }; } VisualizationSceneScalarData::~VisualizationSceneScalarData() @@ -1321,7 +1324,7 @@ void VisualizationSceneScalarData::SetLevelLines ( void VisualizationSceneScalarData::PrintState() { - cout << "\nkeys: " << GetAppWindow()->getSavedKeys() << "\n" + cout << "\nkeys: " << saved_key_func() << "\n" << "\nlight " << strings_off_on[use_light ? 1 : 0] << "\nperspective " << strings_off_on[OrthogonalProjection ? 0 : 1] << "\nviewcenter " << ViewCenterX << ' ' << ViewCenterY diff --git a/lib/vsdata.hpp b/lib/vsdata.hpp index f749d888..eff73422 100644 --- a/lib/vsdata.hpp +++ b/lib/vsdata.hpp @@ -13,6 +13,7 @@ #define GLVIS_VSDATA_HPP #include +#include #include "openglvis.hpp" #include "mfem.hpp" @@ -76,8 +77,6 @@ class VisualizationSceneScalarData : public VisualizationScene gl3::GlDrawable caption_buf; int caption_w, caption_h; - void Init(); - int arrow_type, arrow_scaling_type; int nl; @@ -129,6 +128,9 @@ class VisualizationSceneScalarData : public VisualizationScene void Cone(gl3::GlBuilder& builder, glm::mat4 transform); + // stored function that gets saved keys from SdlWindow + std::function saved_key_func; + void PrintHelpString(); void QueryCaption(); void QueryLevelLines(); @@ -154,6 +156,8 @@ class VisualizationSceneScalarData : public VisualizationScene virtual ~VisualizationSceneScalarData(); + virtual void Init(GLVisWindow* wnd); + virtual std::string GetHelpString() const { return ""; } // Determine 'xscale', 'yscale', and 'zscale' using the current bounding diff --git a/lib/vssolution.cpp b/lib/vssolution.cpp index e31b19d4..4e4339b1 100644 --- a/lib/vssolution.cpp +++ b/lib/vssolution.cpp @@ -417,11 +417,9 @@ VisualizationSceneSolution::VisualizationSceneSolution( mesh = &m; sol = &s; v_normals = normals; - - Init(); } -void VisualizationSceneSolution::Init() +void VisualizationSceneSolution::Init(GLVisWindow* wnd) { rsol = NULL; @@ -446,7 +444,7 @@ void VisualizationSceneSolution::Init() drawbdr = 0; - VisualizationSceneScalarData::Init(); // Calls FindNewBox() !!! + VisualizationSceneScalarData::Init(wnd); // Calls FindNewBox() !!! palette.SetIndex(2); // use the 'jet-like' palette in 2D @@ -459,7 +457,6 @@ void VisualizationSceneSolution::Init() // if (!init) { // init = 1; - GLVisWindow* wnd = GetGLVisWindow(); using SceneType = VisualizationSceneSolution; wnd->AddKeyEvent('b', &SceneType::ToggleDrawBdr); diff --git a/lib/vssolution.hpp b/lib/vssolution.hpp index 2a1de86a..1a7e9563 100644 --- a/lib/vssolution.hpp +++ b/lib/vssolution.hpp @@ -44,8 +44,6 @@ class VisualizationSceneSolution : public VisualizationSceneScalarData gl3::GlDrawable order_buf; gl3::GlDrawable order_noarrow_buf; - void Init(); - void FindNewBox(double rx[], double ry[], double rval[]); void DrawCPLine(gl3::GlBuilder& bld, @@ -101,6 +99,8 @@ class VisualizationSceneSolution : public VisualizationSceneScalarData virtual ~VisualizationSceneSolution(); + virtual void Init(GLVisWindow* wnd); + virtual std::string GetHelpString() const; void SetGridFunction(GridFunction & u) { rsol = &u; } diff --git a/lib/vssolution3d.cpp b/lib/vssolution3d.cpp index fa08b3fa..3a1ceeea 100644 --- a/lib/vssolution3d.cpp +++ b/lib/vssolution3d.cpp @@ -591,12 +591,10 @@ VisualizationSceneSolution3d::VisualizationSceneSolution3d(Mesh &m, Vector &s) mesh = &m; sol = &s; GridF = NULL; - - Init(); } -void VisualizationSceneSolution3d::Init() +void VisualizationSceneSolution3d::Init(GLVisWindow* wnd) { cplane = 0; cp_drawmesh = 0; cp_drawelems = 1; @@ -636,7 +634,7 @@ void VisualizationSceneSolution3d::Init() } bdr_attr_to_show = 1; - VisualizationSceneScalarData::Init(); // calls FindNewBox + VisualizationSceneScalarData::Init(wnd); // calls FindNewBox FindNewValueRange(false); @@ -656,7 +654,6 @@ void VisualizationSceneSolution3d::Init() // if (!init) { // init = 1; - GLVisWindow* wnd = GetGLVisWindow(); using SceneType = VisualizationSceneSolution3d; wnd->AddKeyEvent('m', &SceneType::ToggleDrawMesh); diff --git a/lib/vssolution3d.hpp b/lib/vssolution3d.hpp index 3b3f8104..37ad1a8b 100644 --- a/lib/vssolution3d.hpp +++ b/lib/vssolution3d.hpp @@ -44,8 +44,6 @@ class VisualizationSceneSolution3d : public VisualizationSceneScalarData GridFunction *GridF; - void Init(); - void GetFaceNormals(const int FaceNo, const int side, const IntegrationRule &ir, DenseMatrix &normals); @@ -127,6 +125,8 @@ class VisualizationSceneSolution3d : public VisualizationSceneScalarData VisualizationSceneSolution3d(); VisualizationSceneSolution3d(Mesh & m, Vector & s); + virtual void Init(GLVisWindow* wnd); + void SetGridFunction (GridFunction *gf) { GridF = gf; } void NewMeshAndSolution(Mesh *new_m, Vector *new_sol, diff --git a/lib/vsvector.cpp b/lib/vsvector.cpp index c0aea798..fd63d035 100644 --- a/lib/vsvector.cpp +++ b/lib/vsvector.cpp @@ -234,8 +234,6 @@ VisualizationSceneVector::VisualizationSceneVector(Mesh & m, sol = new Vector(mesh -> GetNV()); VecGridF = NULL; - - Init(); } VisualizationSceneVector::VisualizationSceneVector(GridFunction &vgf) @@ -261,7 +259,6 @@ VisualizationSceneVector::VisualizationSceneVector(GridFunction &vgf) // VisualizationSceneSolution::Init() sets rsol = NULL ! { - Init(); SetGridFunction(vgf); } @@ -447,7 +444,7 @@ void VisualizationSceneVector::NewMeshAndSolution(GridFunction &vgf) PrepareVectorField(); } -void VisualizationSceneVector::Init() +void VisualizationSceneVector::Init(GLVisWindow* wnd) { drawdisp = 0; drawvector = 0; @@ -461,7 +458,7 @@ void VisualizationSceneVector::Init() (*sol)(i) = Vec2Scalar((*solx)(i), (*soly)(i)); } - VisualizationSceneSolution::Init(); + VisualizationSceneSolution::Init(wnd); PrepareVectorField(); // PrepareDisplacedMesh(); // called by PrepareLines() @@ -470,7 +467,6 @@ void VisualizationSceneVector::Init() // if (!init) { // init = 1; - GLVisWindow* wnd = GetGLVisWindow(); using SceneType = VisualizationSceneVector; wnd->AddKeyEvent('d', &SceneType::ToggleDisplacements); diff --git a/lib/vsvector.hpp b/lib/vsvector.hpp index b4319009..cfa4e8c3 100644 --- a/lib/vsvector.hpp +++ b/lib/vsvector.hpp @@ -27,8 +27,6 @@ class VisualizationSceneVector : public VisualizationSceneSolution gl3::GlDrawable displine_buf; GridFunction *VecGridF; - void Init(); - virtual void GetRefinedValues(int i, const IntegrationRule &ir, Vector &vals, DenseMatrix &tr); virtual int GetRefinedValuesAndNormals(int i, const IntegrationRule &ir, @@ -64,6 +62,8 @@ class VisualizationSceneVector : public VisualizationSceneSolution virtual ~VisualizationSceneVector(); + virtual void Init(GLVisWindow* wnd); + virtual std::string GetHelpString() const; void PrepareDisplacedMesh(); diff --git a/lib/vsvector3d.cpp b/lib/vsvector3d.cpp index a84baddb..1d35ea48 100644 --- a/lib/vsvector3d.cpp +++ b/lib/vsvector3d.cpp @@ -295,8 +295,6 @@ VisualizationSceneVector3d::VisualizationSceneVector3d(Mesh &m, Vector &sx, sfes = NULL; VecGridF = NULL; - - Init(); } VisualizationSceneVector3d::VisualizationSceneVector3d(GridFunction &vgf) @@ -324,11 +322,9 @@ VisualizationSceneVector3d::VisualizationSceneVector3d(GridFunction &vgf) vgf.GetNodalValues(*solz, 3); sol = new Vector(mesh->GetNV()); - - Init(); } -void VisualizationSceneVector3d::Init() +void VisualizationSceneVector3d::Init(GLVisWindow* wnd) { key_r_state = 0; @@ -341,7 +337,7 @@ void VisualizationSceneVector3d::Init() SetScalarFunction(); - VisualizationSceneSolution3d::Init(); + VisualizationSceneSolution3d::Init(wnd); PrepareVectorField(); PrepareDisplacedMesh(); @@ -353,7 +349,6 @@ void VisualizationSceneVector3d::Init() // if (!init) { // init = 1; - GLVisWindow* wnd = GetGLVisWindow(); using SceneType = VisualizationSceneVector3d; wnd->AddKeyEvent('d', &SceneType::ToggleDisplacements); diff --git a/lib/vsvector3d.hpp b/lib/vsvector3d.hpp index 6d1306f9..d13b3265 100644 --- a/lib/vsvector3d.hpp +++ b/lib/vsvector3d.hpp @@ -28,8 +28,6 @@ class VisualizationSceneVector3d : public VisualizationSceneSolution3d GridFunction *VecGridF; FiniteElementSpace *sfes; - void Init(); - Array vflevel; Array dvflevel; @@ -51,6 +49,8 @@ class VisualizationSceneVector3d : public VisualizationSceneSolution3d virtual ~VisualizationSceneVector3d(); + virtual void Init(GLVisWindow* wnd); + virtual std::string GetHelpString() const; virtual void PrepareFlat(); From 53e7370a543de202b311649874b936aec3f33c93 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Wed, 12 May 2021 15:07:10 -0700 Subject: [PATCH 29/39] Make TouchPinch a member of GLVisWindow --- glvis.cpp | 2 +- lib/aux_vis.cpp | 7 +++++-- lib/aux_vis.hpp | 4 +++- lib/sdl.hpp | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 028db3b8..7e16abb8 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -528,7 +528,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) scr >> stream_state.keys; cout << "Script: keys: '" << stream_state.keys << "'" << endl; // SendKeySequence(keys.c_str()); - CallKeySequence(stream_state.keys.c_str()); + wnd->CallKeySequence(stream_state.keys.c_str()); wnd->MyExpose(); } else if (word == "palette") diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 412de455..b1a4d957 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -140,7 +140,10 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega wnd->setOnMouseUp(SDL_BUTTON_RIGHT, RightButtonUp); wnd->setOnMouseMove(SDL_BUTTON_RIGHT, RightButtonLoc); - wnd->setTouchPinchCallback(TouchPinch); + TouchDelegate onTouch = [this](SDL_MultiGestureEvent& e) + { this->TouchPinch(e); }; + + wnd->setTouchPinchCallback(onTouch); SetKeyEventHandler('A', &GLVisWindow::ToggleAntialiasing); SetKeyEventHandler ('p', &GLVisWindow::KeyPrint); @@ -861,7 +864,7 @@ void GLVisWindow::ToggleAntialiasing() SendExposeEvent(); } -void TouchPinch(SDL_MultiGestureEvent & e) +void GLVisWindow::TouchPinch(SDL_MultiGestureEvent & e) { // Scale or Zoom? locscene->Zoom(exp(e.dDist*10)); diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 58e8a4b5..788e8a2a 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -156,6 +156,9 @@ class GLVisWindow void MyReshape(GLsizei w, GLsizei h); void MyExpose(GLsizei w, GLsizei h); + // Internal event handler for touch screen actions + void TouchPinch(SDL_MultiGestureEvent & e); + // Internal event handlers for small scene rotations void Key1Pressed(); void Key2Pressed(); @@ -211,7 +214,6 @@ class GLVisWindow std::unique_ptr rot_data; }; -void TouchPinch(SDL_MultiGestureEvent & e); diff --git a/lib/sdl.hpp b/lib/sdl.hpp index 8f6ef070..bbf188ea 100644 --- a/lib/sdl.hpp +++ b/lib/sdl.hpp @@ -27,7 +27,7 @@ struct EventInfo SDL_Keymod keymod; }; -typedef void (*TouchDelegate)(SDL_MultiGestureEvent&); +typedef std::function TouchDelegate; typedef std::function MouseDelegate; typedef std::function KeyDelegate; typedef std::function WindowDelegate; From 99908356ce7ab6b339889203222c636f4edf249f Mon Sep 17 00:00:00 2001 From: Max Yang Date: Wed, 12 May 2021 15:09:26 -0700 Subject: [PATCH 30/39] Run astyle --- lib/aux_js.cpp | 10 +- lib/aux_vis.cpp | 223 ++++++++++++++------------- lib/aux_vis.hpp | 333 +++++++++++++++++++++-------------------- lib/font.hpp | 6 +- lib/gl/attr_traits.hpp | 6 +- lib/gl/types.hpp | 8 +- lib/openglvis.cpp | 16 +- lib/sdl.cpp | 29 ++-- lib/stream_reader.cpp | 3 +- lib/stream_reader.hpp | 3 +- lib/threads.cpp | 6 +- lib/threads.hpp | 3 +- lib/vsdata.cpp | 13 +- lib/vssolution.cpp | 6 +- lib/vssolution3d.cpp | 4 +- lib/vsvector3d.cpp | 26 +++- 16 files changed, 365 insertions(+), 330 deletions(-) diff --git a/lib/aux_js.cpp b/lib/aux_js.cpp index 97137bba..582f6257 100644 --- a/lib/aux_js.cpp +++ b/lib/aux_js.cpp @@ -65,7 +65,7 @@ bool startVisualization(const std::string input, const std::string data_type, try { - mainWindow = new GLVisWindow("glvis", 0, 0, w, h, false); + mainWindow = new GLVisWindow("glvis", 0, 0, w, h, false); } catch (std::runtime_error& ex) { @@ -73,10 +73,10 @@ bool startVisualization(const std::string input, const std::string data_type, << ex.what() << endl; return false; } - catch(...) + catch (...) { - cerr << "Initializing the visualization failed - unknown error." - << endl; + cerr << "Initializing the visualization failed - unknown error." + << endl; return false; } @@ -195,7 +195,7 @@ std::string getHelpString() void ResizeWindow(int w, int h) { - mainWindow->ResizeWindow(w, h); + mainWindow->ResizeWindow(w, h); } int GetUseTexture() diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index b1a4d957..8ebbb7fa 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -47,53 +47,55 @@ void MainLoop(GLVisWindow* wnd); struct GLVisWindow::RotationControl { - GLVisWindow* wnd; - double xang = 0., yang = 0.; - gl3::GlMatrix srot; - double sph_t, sph_u; - GLint oldx, oldy, startx, starty; - - bool constrained_spinning = 0; - - void LeftButtonDown (EventInfo *event); - void LeftButtonLoc (EventInfo *event); - void LeftButtonUp (EventInfo *event); - void MiddleButtonDown(EventInfo *event); - void MiddleButtonLoc (EventInfo *event); - void MiddleButtonUp (EventInfo *event); - void RightButtonDown (EventInfo *event); - void RightButtonLoc (EventInfo *event); - void RightButtonUp (EventInfo *event); - - void CheckSpin(); - void Key0Pressed(); - void KeyDeletePressed(); - void KeyEnterPressed(); - MouseDelegate CreateMouseEvent(void (GLVisWindow::RotationControl::*func)(EventInfo*)) - { - return [this, func](EventInfo* ei) { (this->*func)(ei); }; - } + GLVisWindow* wnd; + double xang = 0., yang = 0.; + gl3::GlMatrix srot; + double sph_t, sph_u; + GLint oldx, oldy, startx, starty; + + bool constrained_spinning = 0; + + void LeftButtonDown (EventInfo *event); + void LeftButtonLoc (EventInfo *event); + void LeftButtonUp (EventInfo *event); + void MiddleButtonDown(EventInfo *event); + void MiddleButtonLoc (EventInfo *event); + void MiddleButtonUp (EventInfo *event); + void RightButtonDown (EventInfo *event); + void RightButtonLoc (EventInfo *event); + void RightButtonUp (EventInfo *event); + + void CheckSpin(); + void Key0Pressed(); + void KeyDeletePressed(); + void KeyEnterPressed(); + MouseDelegate CreateMouseEvent(void (GLVisWindow::RotationControl::*func)( + EventInfo*)) + { + return [this, func](EventInfo* ei) { (this->*func)(ei); }; + } }; template KeyDelegate CreateKeyEvent(T* inst, void (T::*func)()) { - return [inst, func](GLenum) { (inst->*func)(); }; + return [inst, func](GLenum) { (inst->*func)(); }; } void GLVisWindow::KeyPrint(GLenum mod) { - if (mod & KMOD_CTRL) - { - PrintToPDF(); - } + if (mod & KMOD_CTRL) + { + PrintToPDF(); + } } -GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool legacyGlOnly) - : locscene(nullptr) - , idle_funcs(0) - , rot_data(new RotationControl) +GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, + bool legacyGlOnly) + : locscene(nullptr) + , idle_funcs(0) + , rot_data(new RotationControl) { #ifdef GLVIS_DEBUG @@ -116,19 +118,31 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega // auxReshapeFunc (MyReshape); // not needed, MyExpose calls it // auxReshapeFunc (NULL); wnd->setOnExpose([this]() { MyExpose(); }); - auto LeftButtonDown = rot_data->CreateMouseEvent(&RotationControl::LeftButtonDown); - auto LeftButtonUp = rot_data->CreateMouseEvent(&RotationControl::LeftButtonUp); - auto LeftButtonLoc = rot_data->CreateMouseEvent(&RotationControl::LeftButtonLoc); - auto MiddleButtonDown = rot_data->CreateMouseEvent(&RotationControl::MiddleButtonDown); - auto MiddleButtonUp = rot_data->CreateMouseEvent(&RotationControl::MiddleButtonUp); - auto MiddleButtonLoc = rot_data->CreateMouseEvent(&RotationControl::MiddleButtonLoc); - auto RightButtonDown = rot_data->CreateMouseEvent(&RotationControl::RightButtonDown); - auto RightButtonUp = rot_data->CreateMouseEvent(&RotationControl::RightButtonUp); - auto RightButtonLoc = rot_data->CreateMouseEvent(&RotationControl::RightButtonLoc); - - auto Key0Pressed = CreateKeyEvent(rot_data.get(), &RotationControl::Key0Pressed); - auto KeyEnterPressed = CreateKeyEvent(rot_data.get(), &RotationControl::KeyEnterPressed); - auto KeyDeletePressed = CreateKeyEvent(rot_data.get(), &RotationControl::KeyDeletePressed); + auto LeftButtonDown = rot_data->CreateMouseEvent( + &RotationControl::LeftButtonDown); + auto LeftButtonUp = rot_data->CreateMouseEvent( + &RotationControl::LeftButtonUp); + auto LeftButtonLoc = rot_data->CreateMouseEvent( + &RotationControl::LeftButtonLoc); + auto MiddleButtonDown = rot_data->CreateMouseEvent( + &RotationControl::MiddleButtonDown); + auto MiddleButtonUp = rot_data->CreateMouseEvent( + &RotationControl::MiddleButtonUp); + auto MiddleButtonLoc = rot_data->CreateMouseEvent( + &RotationControl::MiddleButtonLoc); + auto RightButtonDown = rot_data->CreateMouseEvent( + &RotationControl::RightButtonDown); + auto RightButtonUp = rot_data->CreateMouseEvent( + &RotationControl::RightButtonUp); + auto RightButtonLoc = rot_data->CreateMouseEvent( + &RotationControl::RightButtonLoc); + + auto Key0Pressed = CreateKeyEvent(rot_data.get(), + &RotationControl::Key0Pressed); + auto KeyEnterPressed = CreateKeyEvent(rot_data.get(), + &RotationControl::KeyEnterPressed); + auto KeyDeletePressed = CreateKeyEvent(rot_data.get(), + &RotationControl::KeyDeletePressed); wnd->setOnMouseDown(SDL_BUTTON_LEFT, LeftButtonDown); wnd->setOnMouseUp(SDL_BUTTON_LEFT, LeftButtonUp); @@ -219,10 +233,10 @@ GLVisWindow::GLVisWindow(std::string name, int x, int y, int w, int h, bool lega GLVisWindow::~GLVisWindow() { #ifndef __EMSCRIPTEN__ - if (glvis_command) - { - glvis_command->Terminate(); - } + if (glvis_command) + { + glvis_command->Terminate(); + } #endif } @@ -266,27 +280,28 @@ void GLVisWindow::InitVisualization(int field_type, StreamState state, void GLVisWindow::SetupHandledKey(int key) { wnd->setOnKeyDown(key, - [this, key](GLenum e) - { - if (internal_keyevents.find(key) != internal_keyevents.end()) - { internal_keyevents[key](e); } - if (keyevents.find(key) != keyevents.end()) - { keyevents[key](e); } - }); + [this, key](GLenum e) + { + if (internal_keyevents.find(key) != internal_keyevents.end()) + { internal_keyevents[key](e); } + if (keyevents.find(key) != keyevents.end()) + { keyevents[key](e); } + }); } void GLVisWindow::SetKeyEventHandler(int key, void (GLVisWindow::*handler)()) { - auto handlerWrapper = [this, handler](GLenum) { (this->*handler)(); }; - internal_keyevents[key] = handlerWrapper; - SetupHandledKey(key); + auto handlerWrapper = [this, handler](GLenum) { (this->*handler)(); }; + internal_keyevents[key] = handlerWrapper; + SetupHandledKey(key); } -void GLVisWindow::SetKeyEventHandler(int key, void (GLVisWindow::*handler)(GLenum)) +void GLVisWindow::SetKeyEventHandler(int key, + void (GLVisWindow::*handler)(GLenum)) { - auto handlerWrapper = [this, handler](GLenum mod) { (this->*handler)(mod); }; - internal_keyevents[key] = handlerWrapper; - SetupHandledKey(key); + auto handlerWrapper = [this, handler](GLenum mod) { (this->*handler)(mod); }; + internal_keyevents[key] = handlerWrapper; + SetupHandledKey(key); } void GLVisWindow::SendKeySequence(const char *seq) @@ -397,7 +412,7 @@ void GLVisWindow::CallKeySequence(const char *seq) void GLVisWindow::RunVisualization() { visualize = 1; - wnd->setOnIdle([this](){return MainIdleFunc();}); + wnd->setOnIdle([this]() {return MainIdleFunc();}); #ifndef __EMSCRIPTEN__ wnd->mainLoop(); #endif @@ -481,13 +496,13 @@ bool GLVisWindow::CommunicationIdleFunc() int status = glvis_command->Execute(); if (status < 0) { - cout << "GLVisCommand signalled exit" << endl; - wnd->signalQuit(); + cout << "GLVisCommand signalled exit" << endl; + wnd->signalQuit(); } else if (status == 1) { - // no commands right now - main loop should sleep - return true; + // no commands right now - main loop should sleep + return true; } return false; } @@ -499,35 +514,35 @@ bool GLVisWindow::MainIdleFunc() if (glvis_command && visualize == 1 && !(idle_funcs.Size() > 0 && use_idle)) { - // Execute the next event from the communication thread if: - // - a valid GLVisCommand has been set - // - the communication thread is not stopped - // - The idle function flag is not set, or no idle functions have been - // registered - sleep = CommunicationIdleFunc(); - if (idle_funcs.Size() > 0) { sleep = false; } + // Execute the next event from the communication thread if: + // - a valid GLVisCommand has been set + // - the communication thread is not stopped + // - The idle function flag is not set, or no idle functions have been + // registered + sleep = CommunicationIdleFunc(); + if (idle_funcs.Size() > 0) { sleep = false; } } else if (idle_funcs.Size() > 0) { - last_idle_func = (last_idle_func + 1) % idle_funcs.Size(); - if (idle_funcs[last_idle_func]) - { - (*idle_funcs[last_idle_func])(this); - } - // Continue executing idle functions - sleep = false; + last_idle_func = (last_idle_func + 1) % idle_funcs.Size(); + if (idle_funcs[last_idle_func]) + { + (*idle_funcs[last_idle_func])(this); + } + // Continue executing idle functions + sleep = false; } use_idle = !use_idle; #else if (idle_funcs.Size() > 0) { - last_idle_func = (last_idle_func + 1) % idle_funcs.Size(); - if (idle_funcs[last_idle_func]) - { - (*idle_funcs[last_idle_func])(this); - } - // Continue executing idle functions - sleep = false; + last_idle_func = (last_idle_func + 1) % idle_funcs.Size(); + if (idle_funcs[last_idle_func]) + { + (*idle_funcs[last_idle_func])(this); + } + // Continue executing idle functions + sleep = false; } #endif return sleep; @@ -537,7 +552,7 @@ void GLVisWindow::AddIdleFunc(GLVisWindow::IdleFPtr Func) { idle_funcs.Union(Func); use_idle = false; - wnd->setOnIdle([this](){return MainIdleFunc();}); + wnd->setOnIdle([this]() {return MainIdleFunc();}); } void GLVisWindow::RemoveIdleFunc(GLVisWindow::IdleFPtr Func) @@ -552,7 +567,7 @@ void GLVisWindow::RemoveIdleFunc(GLVisWindow::IdleFPtr Func) void MainLoop(GLVisWindow* wnd) { - wnd->MainLoop(); + wnd->MainLoop(); } void GLVisWindow::MainLoop() @@ -897,9 +912,9 @@ void GLVisWindow::Screenshot(std::string filename) snprintf(fname, 20, "GLVis_s%02d", p++); wnd->screenshot(fname); } - else + else { - wnd->screenshot(filename); + wnd->screenshot(filename); } cout << "done" << endl; } @@ -1010,14 +1025,14 @@ void GLVisWindow::ThreadsPauseFunc(GLenum state) #ifndef __EMSCRIPTEN__ if (glvis_command) { - if (state & KMOD_CTRL) - { - glvis_command->ToggleAutopause(); - } - else - { - ToggleThreads(); - } + if (state & KMOD_CTRL) + { + glvis_command->ToggleAutopause(); + } + else + { + ToggleThreads(); + } } #endif } diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 788e8a2a..2aafb63d 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -28,190 +28,191 @@ class communication_thread; class GLVisWindow { public: - using IdleFPtr = void(*)(GLVisWindow* wnd); - using KeyEvent = void(*)(GLVisWindow* wnd, int keystate); - - /// Initializes the visualization and some keys. - GLVisWindow(std::string name, int x, int y, int w, int h, bool legacyGlOnly); - - ~GLVisWindow(); - - void InitVisualization(int field_type, StreamState state, - bool& keep_attr, - const mfem::Array& input_streams = {}); - - void SetFont(const std::string& fn); - - VisualizationScene* getScene() { return locscene.get(); } - - GlVisFont* getFont() { return &font; } - - SdlWindow* getSdl() { return wnd.get(); } - - /// Start the infinite visualization loop. - void RunVisualization(); - - /// Send expose event. In our case MyReshape is executed and Draw after it. - void SendExposeEvent(); - - void MyExpose(); - - /// Send a sequence of keystrokes to the visualization window - void SendKeySequence(const char *seq); - - // Directly call the functions assigned to the given keys. Unlike the above - // function, SendKeySequence(), this function does not send X events and - // actually disables the function SendExposeEvent() used by many of the - // functions assigned to keys. Call MyExpose() after calling this function to - // update the visualization window. - void CallKeySequence(const char *seq); - - void AddIdleFunc(IdleFPtr func); - void RemoveIdleFunc(IdleFPtr func); - - void ThreadsStop(); - void ThreadsRun(); - void ToggleThreads(); - - void MainLoop(); - - void Quit(); - - void ToggleAntialiasing(); - void Screenshot() { Screenshot(""); } - void Screenshot(std::string filename); - void PrintToPDF(); - - void ZoomIn(); - void ZoomOut(); - void ScaleUp(); - void ScaleDown(); - void LookAt(); - void ShrinkWindow(); - void EnlargeWindow(); - void MoveResizeWindow(int x, int y, int w, int h); - void ResizeWindow(int w, int h); - void SetWindowTitle(const char *title); - - /// Adds a conditionally-updatable scene event. - template - void AddKeyEvent(int key, bool (TScene::*eh)()) - { - auto wrapped_eh = [this, eh](GLenum e) - { - TScene* pScene = dynamic_cast(locscene.get()); - bool exposeAfter = (pScene->*eh)(); - if (exposeAfter) { SendExposeEvent(); } - }; - keyevents[key] = wrapped_eh; - SetupHandledKey(key); - } - - template - void AddKeyEvent(int key, void (TScene::*eh)(), bool exposeAfter = true) - { - auto wrapped_eh = [this, eh, exposeAfter](GLenum e) - { - TScene* pScene = dynamic_cast(locscene.get()); - (pScene->*eh)(); - if (exposeAfter) { SendExposeEvent(); } - }; - keyevents[key] = wrapped_eh; - SetupHandledKey(key); - } - - void AddKeyEvent(int key, void (*eh)(GLVisWindow*), bool exposeAfter = true) - { - auto wrapped_eh = [this, eh, exposeAfter](GLenum e) - { - (*eh)(this); - if (exposeAfter) { SendExposeEvent(); } - }; - keyevents[key] = wrapped_eh; - SetupHandledKey(key); - } - - void AddKeyEvent(int key, void (*eh)(GLVisWindow*, GLenum), bool exposeAfter = true) - { - auto wrapped_eh = [this, eh, exposeAfter](GLenum e) - { - (*eh)(this, e); - if (exposeAfter) { SendExposeEvent(); } - }; - keyevents[key] = wrapped_eh; - SetupHandledKey(key); - } + using IdleFPtr = void(*)(GLVisWindow* wnd); + using KeyEvent = void(*)(GLVisWindow* wnd, int keystate); + + /// Initializes the visualization and some keys. + GLVisWindow(std::string name, int x, int y, int w, int h, bool legacyGlOnly); + + ~GLVisWindow(); + + void InitVisualization(int field_type, StreamState state, + bool& keep_attr, + const mfem::Array& input_streams = {}); + + void SetFont(const std::string& fn); + + VisualizationScene* getScene() { return locscene.get(); } + + GlVisFont* getFont() { return &font; } + + SdlWindow* getSdl() { return wnd.get(); } + + /// Start the infinite visualization loop. + void RunVisualization(); + + /// Send expose event. In our case MyReshape is executed and Draw after it. + void SendExposeEvent(); + + void MyExpose(); + + /// Send a sequence of keystrokes to the visualization window + void SendKeySequence(const char *seq); + + // Directly call the functions assigned to the given keys. Unlike the above + // function, SendKeySequence(), this function does not send X events and + // actually disables the function SendExposeEvent() used by many of the + // functions assigned to keys. Call MyExpose() after calling this function to + // update the visualization window. + void CallKeySequence(const char *seq); + + void AddIdleFunc(IdleFPtr func); + void RemoveIdleFunc(IdleFPtr func); + + void ThreadsStop(); + void ThreadsRun(); + void ToggleThreads(); + + void MainLoop(); + + void Quit(); + + void ToggleAntialiasing(); + void Screenshot() { Screenshot(""); } + void Screenshot(std::string filename); + void PrintToPDF(); + + void ZoomIn(); + void ZoomOut(); + void ScaleUp(); + void ScaleDown(); + void LookAt(); + void ShrinkWindow(); + void EnlargeWindow(); + void MoveResizeWindow(int x, int y, int w, int h); + void ResizeWindow(int w, int h); + void SetWindowTitle(const char *title); + + /// Adds a conditionally-updatable scene event. + template + void AddKeyEvent(int key, bool (TScene::*eh)()) + { + auto wrapped_eh = [this, eh](GLenum e) + { + TScene* pScene = dynamic_cast(locscene.get()); + bool exposeAfter = (pScene->*eh)(); + if (exposeAfter) { SendExposeEvent(); } + }; + keyevents[key] = wrapped_eh; + SetupHandledKey(key); + } + + template + void AddKeyEvent(int key, void (TScene::*eh)(), bool exposeAfter = true) + { + auto wrapped_eh = [this, eh, exposeAfter](GLenum e) + { + TScene* pScene = dynamic_cast(locscene.get()); + (pScene->*eh)(); + if (exposeAfter) { SendExposeEvent(); } + }; + keyevents[key] = wrapped_eh; + SetupHandledKey(key); + } + + void AddKeyEvent(int key, void (*eh)(GLVisWindow*), bool exposeAfter = true) + { + auto wrapped_eh = [this, eh, exposeAfter](GLenum e) + { + (*eh)(this); + if (exposeAfter) { SendExposeEvent(); } + }; + keyevents[key] = wrapped_eh; + SetupHandledKey(key); + } + + void AddKeyEvent(int key, void (*eh)(GLVisWindow*, GLenum), + bool exposeAfter = true) + { + auto wrapped_eh = [this, eh, exposeAfter](GLenum e) + { + (*eh)(this, e); + if (exposeAfter) { SendExposeEvent(); } + }; + keyevents[key] = wrapped_eh; + SetupHandledKey(key); + } private: - void InitFont(); - bool SetFont(const vector& patterns, int height); + void InitFont(); + bool SetFont(const vector& patterns, int height); - void SetupHandledKey(int key); + void SetupHandledKey(int key); - void SetKeyEventHandler(int key, void (GLVisWindow::*handler)()); - void SetKeyEventHandler(int key, void (GLVisWindow::*handler)(GLenum)); + void SetKeyEventHandler(int key, void (GLVisWindow::*handler)()); + void SetKeyEventHandler(int key, void (GLVisWindow::*handler)(GLenum)); - bool MainIdleFunc(); - bool CommunicationIdleFunc(); + bool MainIdleFunc(); + bool CommunicationIdleFunc(); - void MyReshape(GLsizei w, GLsizei h); - void MyExpose(GLsizei w, GLsizei h); + void MyReshape(GLsizei w, GLsizei h); + void MyExpose(GLsizei w, GLsizei h); - // Internal event handler for touch screen actions - void TouchPinch(SDL_MultiGestureEvent & e); + // Internal event handler for touch screen actions + void TouchPinch(SDL_MultiGestureEvent & e); - // Internal event handlers for small scene rotations - void Key1Pressed(); - void Key2Pressed(); - void Key3Pressed(); - void Key4Pressed(); - void Key5Pressed(); - void Key6Pressed(); - void Key7Pressed(); - void Key8Pressed(); - void Key9Pressed(); + // Internal event handlers for small scene rotations + void Key1Pressed(); + void Key2Pressed(); + void Key3Pressed(); + void Key4Pressed(); + void Key5Pressed(); + void Key6Pressed(); + void Key7Pressed(); + void Key8Pressed(); + void Key9Pressed(); - // Internal event handlers for other scene rotations, transformations - void KeyLeftPressed(GLenum); - void KeyRightPressed(GLenum); - void KeyUpPressed(GLenum); - void KeyDownPressed(GLenum); - void KeyJPressed(); - void KeyMinusPressed(); - void KeyPlusPressed(); + // Internal event handlers for other scene rotations, transformations + void KeyLeftPressed(GLenum); + void KeyRightPressed(GLenum); + void KeyUpPressed(GLenum); + void KeyDownPressed(GLenum); + void KeyJPressed(); + void KeyMinusPressed(); + void KeyPlusPressed(); - void StopSpinning(); + void StopSpinning(); - // Internal event handler for toggling state of threads - void ThreadsPauseFunc(GLenum); + // Internal event handler for toggling state of threads + void ThreadsPauseFunc(GLenum); - void KeyPrint(GLenum); + void KeyPrint(GLenum); - std::unique_ptr wnd; - std::unique_ptr locscene; - std::unique_ptr glvis_command; - std::unique_ptr comm_thread; - StreamState prob_state; - bool use_idle = false; + std::unique_ptr wnd; + std::unique_ptr locscene; + std::unique_ptr glvis_command; + std::unique_ptr comm_thread; + StreamState prob_state; + bool use_idle = false; - // Idle function callbacks - mfem::Array idle_funcs{}; - int last_idle_func = 0; + // Idle function callbacks + mfem::Array idle_funcs{}; + int last_idle_func = 0; - // internal_keyevents keeps internal event handlers, which will be called - // before an event handler in keyevents - std::unordered_map internal_keyevents; - std::unordered_map keyevents; + // internal_keyevents keeps internal event handlers, which will be called + // before an event handler in keyevents + std::unordered_map internal_keyevents; + std::unordered_map keyevents; - int visualize = 0; + int visualize = 0; - bool disableSendExposeEvent = false; + bool disableSendExposeEvent = false; - GlVisFont font; - std::string priority_font; - int font_size = 12; + GlVisFont font; + std::string priority_font; + int font_size = 12; - struct RotationControl; - std::unique_ptr rot_data; + struct RotationControl; + std::unique_ptr rot_data; }; diff --git a/lib/font.hpp b/lib/font.hpp index ddd8b29f..7654f736 100644 --- a/lib/font.hpp +++ b/lib/font.hpp @@ -54,9 +54,9 @@ class GlVisFont void SetDPIParams(bool is_hidpi, int ppi_w, int ppi_h) { - this->is_hidpi = is_hidpi; - this->ppi_w = ppi_w; - this->ppi_h = ppi_h; + this->is_hidpi = is_hidpi; + this->ppi_w = ppi_w; + this->ppi_h = ppi_h; } bool LoadFont(const std::string& path, int font_index, int font_size); diff --git a/lib/gl/attr_traits.hpp b/lib/gl/attr_traits.hpp index 662ea078..b993c115 100644 --- a/lib/gl/attr_traits.hpp +++ b/lib/gl/attr_traits.hpp @@ -125,9 +125,9 @@ AttrNormal> const static GLenum FFArrayIdx = GL_NORMAL_ARRAY; static void FFSetupFunc(GLint /*size*/, GLenum type, GLsizei stride, const GLvoid* ptr) -{ - glNormalPointer(type, stride, ptr); -} + { + glNormalPointer(type, stride, ptr); + } }; template diff --git a/lib/gl/types.hpp b/lib/gl/types.hpp index 506d9e20..0cf9bc61 100644 --- a/lib/gl/types.hpp +++ b/lib/gl/types.hpp @@ -203,10 +203,10 @@ struct alignas(16) Vertex std::array coord; static Vertex create(double * d) -{ - return Vertex {(float) d[0], (float) d[1], (float) d[2]}; -} -static const int layout = LAYOUT_VTX; + { + return Vertex {(float) d[0], (float) d[1], (float) d[2]}; + } + static const int layout = LAYOUT_VTX; }; struct alignas(16) VertexColor diff --git a/lib/openglvis.cpp b/lib/openglvis.cpp index c7a60e04..195c7ec1 100644 --- a/lib/openglvis.cpp +++ b/lib/openglvis.cpp @@ -73,10 +73,10 @@ glm::mat4 Camera::RotMatrix() double mat[16] = { -left[0], up[0], -dir[0], 0.0, - -left[1], up[1], -dir[1], 0.0, - -left[2], up[2], -dir[2], 0.0, - 0.0, 0.0, 0.0, 1.0 - }; + -left[1], up[1], -dir[1], 0.0, + -left[2], up[2], -dir[2], 0.0, + 0.0, 0.0, 0.0, 1.0 + }; return glm::make_mat4(mat); } @@ -86,10 +86,10 @@ glm::mat4 Camera::TransposeRotMatrix() double mat_t[16] = { -left[0], -left[1], -left[2], 0.0, - up[0], up[1], up[2], 0.0, - -dir[0], -dir[1], -dir[2], 0.0, - 0.0, 0.0, 0.0, 1.0 - }; + up[0], up[1], up[2], 0.0, + -dir[0], -dir[1], -dir[2], 0.0, + 0.0, 0.0, 0.0, 1.0 + }; return glm::make_mat4(mat_t); } diff --git a/lib/sdl.cpp b/lib/sdl.cpp index 57f139f4..c2a263c7 100644 --- a/lib/sdl.cpp +++ b/lib/sdl.cpp @@ -204,7 +204,8 @@ void SdlWindow::probeGLContextSupport(bool legacyGlOnly) #endif } -bool SdlWindow::createWindow(const std::string& title, int x, int y, int w, int h, +bool SdlWindow::createWindow(const std::string& title, int x, int y, int w, + int h, bool legacyGlOnly) { if (!SDL_WasInit(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) @@ -668,19 +669,19 @@ void SdlWindow::mainIter() bool sleep = onIdle(); if (sleep) { - // Wait for the next event (without consuming CPU cycles, if possible) - // See also: SdlWindow::signalLoop() - if (platform) - { - platform->WaitEvent(); - } - else - { - if (!SDL_PollEvent(nullptr)) - { - std::this_thread::sleep_for(std::chrono::milliseconds(8)); - } - } + // Wait for the next event (without consuming CPU cycles, if possible) + // See also: SdlWindow::signalLoop() + if (platform) + { + platform->WaitEvent(); + } + else + { + if (!SDL_PollEvent(nullptr)) + { + std::this_thread::sleep_for(std::chrono::milliseconds(8)); + } + } } } #else diff --git a/lib/stream_reader.cpp b/lib/stream_reader.cpp index b62f940e..09e79122 100644 --- a/lib/stream_reader.cpp +++ b/lib/stream_reader.cpp @@ -397,7 +397,8 @@ StreamState::CreateVisualizationScene(GLVisWindow* wnd, int field_type) } else if (mesh->SpaceDimension() == 3) { - VisualizationSceneSolution3d *vss = new VisualizationSceneSolution3d(*mesh, sol); + VisualizationSceneSolution3d *vss = new VisualizationSceneSolution3d(*mesh, + sol); // initialize events and initial scene state vss->Init(wnd); vs.reset(vss); diff --git a/lib/stream_reader.hpp b/lib/stream_reader.hpp index d80a2b04..7c53ea78 100644 --- a/lib/stream_reader.hpp +++ b/lib/stream_reader.hpp @@ -35,7 +35,8 @@ struct StreamState int ReadStream(std::istream &is, const std::string &data_type); - std::unique_ptr CreateVisualizationScene(GLVisWindow* wnd, int field_type); + std::unique_ptr CreateVisualizationScene(GLVisWindow* wnd, + int field_type); /// Sets a new mesh and solution from another StreamState object, and /// updates the given VisualizationScene pointer with the new data. diff --git a/lib/threads.cpp b/lib/threads.cpp index 2ec9e46e..8f29a08e 100644 --- a/lib/threads.cpp +++ b/lib/threads.cpp @@ -420,7 +420,7 @@ int GLVisCommand::Execute() } VisualizationSceneScalarData* vs - = dynamic_cast(window->getScene()); + = dynamic_cast(window->getScene()); switch (command) { @@ -440,7 +440,7 @@ int GLVisCommand::Execute() { if (mesh_range > 0.0) { - vs->SetValueRange(-mesh_range, mesh_range); + vs->SetValueRange(-mesh_range, mesh_range); } window->MyExpose(); } @@ -510,7 +510,7 @@ int GLVisCommand::Execute() cout << "Command: axis_labels: '" << axis_label_x << "' '" << axis_label_y << "' '" << axis_label_z << "'" << endl; vs->SetAxisLabels(axis_label_x.c_str(), axis_label_y.c_str(), - axis_label_z.c_str()); + axis_label_z.c_str()); window->MyExpose(); break; } diff --git a/lib/threads.hpp b/lib/threads.hpp index bc8887c2..702744a3 100644 --- a/lib/threads.hpp +++ b/lib/threads.hpp @@ -161,7 +161,8 @@ class communication_thread void execute(); public: - communication_thread(GLVisCommand* parent_cmd, const Array &_is); + communication_thread(GLVisCommand* parent_cmd, + const Array &_is); ~communication_thread(); }; diff --git a/lib/vsdata.cpp b/lib/vsdata.cpp index e17d8ffd..f5fc48af 100644 --- a/lib/vsdata.cpp +++ b/lib/vsdata.cpp @@ -474,7 +474,7 @@ void VisualizationSceneScalarData::QueryCaption() void Key_Mod_a_Pressed(GLVisWindow* wnd, GLenum state) { VisualizationSceneScalarData * vsdata - = dynamic_cast(wnd->getScene()); + = dynamic_cast(wnd->getScene()); if (state & KMOD_CTRL) { static const char *autoscale_modes[] = { "off", "on", "value", "mesh" }; @@ -516,7 +516,7 @@ void VisualizationSceneScalarData::Reset3DView() void KeypPressed(GLVisWindow* wnd, GLenum state) { VisualizationSceneScalarData * vsdata - = dynamic_cast(wnd->getScene()); + = dynamic_cast(wnd->getScene()); // KMOD_CTRL handled by KeyPrintPDF in aux_vis.cpp if (!(state & KMOD_CTRL)) { @@ -527,7 +527,7 @@ void KeypPressed(GLVisWindow* wnd, GLenum state) void KeyPPressed(GLVisWindow* wnd) { VisualizationSceneScalarData * vsdata - = dynamic_cast(wnd->getScene()); + = dynamic_cast(wnd->getScene()); vsdata->palette.PrevIndex(); } @@ -588,7 +588,7 @@ void VisualizationSceneScalarData::QueryPaletteSettings() void KeyF7Pressed(GLVisWindow* wnd, GLenum state) { VisualizationSceneScalarData * vsdata - = dynamic_cast(wnd->getScene()); + = dynamic_cast(wnd->getScene()); if (state & KMOD_SHIFT) { cout << "Current bounding box:\n" @@ -1057,8 +1057,9 @@ void VisualizationSceneScalarData::Init(GLVisWindow* wnd) autoscale = 1; - saved_key_func = [wnd]() -> std::string { - return wnd->getSdl()->getSavedKeys(); + saved_key_func = [wnd]() -> std::string + { + return wnd->getSdl()->getSavedKeys(); }; } diff --git a/lib/vssolution.cpp b/lib/vssolution.cpp index 4e4339b1..36e6f8b9 100644 --- a/lib/vssolution.cpp +++ b/lib/vssolution.cpp @@ -193,7 +193,7 @@ static void SwitchAttribute(VisualizationSceneSolution* vssol, static void KeyF9Pressed(GLVisWindow* wnd, GLenum state) { VisualizationSceneSolution* vssol - = dynamic_cast(wnd->getScene()); + = dynamic_cast(wnd->getScene()); if (!(state & KMOD_SHIFT)) { SwitchAttribute(vssol, +1, vssol->attr_to_show, vssol->el_attr_to_show, false); @@ -208,7 +208,7 @@ static void KeyF9Pressed(GLVisWindow* wnd, GLenum state) static void KeyF10Pressed(GLVisWindow* wnd, GLenum state) { VisualizationSceneSolution* vssol - = dynamic_cast(wnd->getScene()); + = dynamic_cast(wnd->getScene()); if (!(state & KMOD_SHIFT)) { SwitchAttribute(vssol, -1, vssol->attr_to_show, vssol->el_attr_to_show, false); @@ -223,7 +223,7 @@ static void KeyF10Pressed(GLVisWindow* wnd, GLenum state) static void KeyoPressed(GLVisWindow* wnd, GLenum state) { VisualizationSceneSolution* vssol - = dynamic_cast(wnd->getScene()); + = dynamic_cast(wnd->getScene()); if (state & KMOD_CTRL) { vssol -> ToggleDrawOrdering(); diff --git a/lib/vssolution3d.cpp b/lib/vssolution3d.cpp index 3a1ceeea..9d4d0f54 100644 --- a/lib/vssolution3d.cpp +++ b/lib/vssolution3d.cpp @@ -280,7 +280,7 @@ void VisualizationSceneSolution3d::TranslateCPBack() void KeyoPressed(GLVisWindow* wnd, GLenum state) { VisualizationSceneSolution3d* vssol3d - = dynamic_cast(wnd->getScene()); + = dynamic_cast(wnd->getScene()); if (state & KMOD_CTRL) { vssol3d -> ToggleDrawOrdering(); @@ -308,7 +308,7 @@ void KeyoPressed(GLVisWindow* wnd, GLenum state) static void KeyOPressed(GLVisWindow* wnd) { VisualizationSceneSolution3d* vssol3d - = dynamic_cast(wnd->getScene()); + = dynamic_cast(wnd->getScene()); if (vssol3d -> TimesToRefine > 1) { cout << "Subdivision factor = " << --vssol3d->TimesToRefine << endl; diff --git a/lib/vsvector3d.cpp b/lib/vsvector3d.cpp index 1d35ea48..b8f89429 100644 --- a/lib/vsvector3d.cpp +++ b/lib/vsvector3d.cpp @@ -105,18 +105,26 @@ extern GeometryRefiner GLVisGeometryRefiner; void VisualizationSceneVector3d::NextDisplacement() { if (drawdisp) + { ianimd =( (ianimd + 1) % (ianimmax + 1) ); + } else + { ianim =( (ianim + 1) % (ianimmax + 1) ); + } NPressed(); } void VisualizationSceneVector3d::PrevDisplacement() { if (drawdisp) + { ianimd = ((ianimd + ianimmax) % (ianimmax + 1)); + } else + { ianim = ((ianim + ianimmax) % (ianimmax + 1)); + } NPressed(); } @@ -360,14 +368,20 @@ void VisualizationSceneVector3d::Init(GLVisWindow* wnd) wnd->AddKeyEvent('b', &SceneType::PrevDisplacement); wnd->AddKeyEvent('B', &SceneType::PrevDisplacement); - wnd->AddKeyEvent('u', &SceneType::NextVectorFieldLevel); // Keys u, U are also used in - wnd->AddKeyEvent('U', &SceneType::PrevVectorFieldLevel); // VisualizationSceneSolution3d + wnd->AddKeyEvent('u', + &SceneType::NextVectorFieldLevel); // Keys u, U are also used in + wnd->AddKeyEvent('U', + &SceneType::PrevVectorFieldLevel); // VisualizationSceneSolution3d - wnd->AddKeyEvent('w', &SceneType::AddVectorFieldLevel); // Keys w, W are also used in - wnd->AddKeyEvent('W', &SceneType::RemoveVectorFieldLevel); // VisualizationSceneSolution3d + wnd->AddKeyEvent('w', + &SceneType::AddVectorFieldLevel); // Keys w, W are also used in + wnd->AddKeyEvent('W', + &SceneType::RemoveVectorFieldLevel); // VisualizationSceneSolution3d - wnd->AddKeyEvent('v', &SceneType::NextVectorField); // Keys v, V are also used in - wnd->AddKeyEvent('V', &SceneType::PrevVectorField); // VisualizationSceneSolution3d + wnd->AddKeyEvent('v', + &SceneType::NextVectorField); // Keys v, V are also used in + wnd->AddKeyEvent('V', + &SceneType::PrevVectorField); // VisualizationSceneSolution3d wnd->AddKeyEvent('F', &SceneType::ToggleScalarFunction); } From 1e4c54ea620a9b3e05f3d2ae57b1ef40ecddf615 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Wed, 12 May 2021 15:24:14 -0700 Subject: [PATCH 31/39] Remove more globals --- glvis.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 7e16abb8..8eb17fe0 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -59,9 +59,6 @@ string extra_caption; int input = 1; StreamState stream_state; GLVisWindow * mainWindow = nullptr; -VisualizationSceneScalarData *vs = NULL; -GLVisCommand *glvis_command = nullptr; -communication_thread *comm_thread = NULL; GeometryRefiner GLVisGeometryRefiner; @@ -288,6 +285,8 @@ void ExecuteScriptCommand(GLVisWindow* wnd) return; } + VisualizationSceneScalarData* vs + = static_cast(wnd->getScene()); istream &scr = *script; string word; int done_one_command = 0; From 942c6324b885c334dcfda913fd60d3ca4cbf1679 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Wed, 12 May 2021 15:59:06 -0700 Subject: [PATCH 32/39] Remove global stream_state from desktop side code --- glvis.cpp | 20 ++++++++++++-------- lib/aux_vis.hpp | 2 ++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 8eb17fe0..8ddc462b 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -57,7 +57,6 @@ string extra_caption; // Global variables int input = 1; -StreamState stream_state; GLVisWindow * mainWindow = nullptr; GeometryRefiner GLVisGeometryRefiner; @@ -96,7 +95,7 @@ void CloseInputStreams(bool); // Visualize the data in the global variables mesh, sol/grid_f, etc // 0 - scalar data, 1 - vector data, 2 - mesh only, (-1) - unknown -bool GLVisInitVis(int field_type) +bool GLVisInitVis(StreamState stream_state, int field_type) { if (field_type < 0 || field_type > 2) { @@ -287,6 +286,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) VisualizationSceneScalarData* vs = static_cast(wnd->getScene()); + StreamState& stream_state = wnd->getStreamState(); istream &scr = *script; string word; int done_one_command = 0; @@ -656,13 +656,14 @@ void ScriptControl(GLVisWindow* wnd) } } -void PlayScript(istream &scr) +void PlayScript(istream &scr, StreamState stream_state) { string word; scr_min_val = numeric_limits::infinity(); scr_max_val = -scr_min_val; + // read initializing commands while (1) { @@ -722,8 +723,9 @@ void PlayScript(istream &scr) scr_level = scr_running = 0; script = &scr; stream_state.keys.clear(); + int ftype = (stream_state.grid_f->VectorDim() == 1) ? 0 : 1; - if (GLVisInitVis((stream_state.grid_f->VectorDim() == 1) ? 0 : 1)) + if (GLVisInitVis(std::move(stream_state), ftype)) { mainWindow->AddKeyEvent(SDLK_SPACE, ScriptControl, false); GLVisStartVis(); @@ -752,6 +754,8 @@ int main (int argc, char *argv[]) double ms_line_width = gl3::LINE_WIDTH_AA; int geom_ref_type = Quadrature1D::ClosedUniform; + StreamState stream_state; + OptionsParser args(argc, argv); args.AddOption(&mesh_file, "-m", "--mesh", @@ -899,7 +903,7 @@ int main (int argc, char *argv[]) ifs >> data_type >> ws; int ft = stream_state.ReadStream(ifs, data_type); input_streams.Append(&ifs); - if (GLVisInitVis(ft)) + if (GLVisInitVis(std::move(stream_state), ft)) { GLVisStartVis(); } @@ -915,7 +919,7 @@ int main (int argc, char *argv[]) cout << "Can not open script: " << script_file << endl; return 1; } - PlayScript(scr); + PlayScript(scr, std::move(stream_state)); return 0; } @@ -1160,7 +1164,7 @@ int main (int argc, char *argv[]) delete isock; ft = ReadInputStreams(stream_state); } - if (GLVisInitVis(ft)) + if (GLVisInitVis(std::move(stream_state), ft)) { GLVisStartVis(); } @@ -1205,7 +1209,7 @@ int main (int argc, char *argv[]) { field_type = (use_soln) ? 0 : 2; } - if (GLVisInitVis(field_type)) + if (GLVisInitVis(std::move(stream_state), field_type)) { GLVisStartVis(); } diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index 2aafb63d..d329622a 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -42,6 +42,8 @@ class GLVisWindow void SetFont(const std::string& fn); + StreamState& getStreamState() { return prob_state; } + VisualizationScene* getScene() { return locscene.get(); } GlVisFont* getFont() { return &font; } From 2bb63be759aa3d9901d92bb0e7149812b2dcbb28 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Mon, 24 May 2021 16:29:24 -0700 Subject: [PATCH 33/39] Remove unused variable --- lib/sdl.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/sdl.cpp b/lib/sdl.cpp index c2a263c7..79e806d1 100644 --- a/lib/sdl.cpp +++ b/lib/sdl.cpp @@ -605,7 +605,6 @@ void SdlWindow::multiGestureEvent(SDL_MultiGestureEvent & e) void SdlWindow::mainIter() { SDL_Event e; - static bool useIdle = false; static bool disable_mouse = false; if (SDL_PollEvent(&e)) { From 5e023b8eaa48986678d833f78496056d8db982df Mon Sep 17 00:00:00 2001 From: Tom Stitt Date: Tue, 1 Jun 2021 16:42:51 -0700 Subject: [PATCH 34/39] fix js build --- lib/aux_js.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/aux_js.cpp b/lib/aux_js.cpp index 582f6257..2f417844 100644 --- a/lib/aux_js.cpp +++ b/lib/aux_js.cpp @@ -128,13 +128,13 @@ int updateVisualization(std::string data_type, std::string stream) void iterVisualization() { - GetAppWindow()->mainIter(); + mainWindow->getSdl()->mainIter(); } void setCanvasId(const std::string & id) { std::cout << "glvis: setting canvas id to " << id << std::endl; - GetAppWindow()->setCanvasId(id); + mainWindow->getSdl()->setCanvasId(id); } void disableKeyHandling() @@ -156,7 +156,7 @@ void setKeyboardListeningElementId(const std::string & id) void processKeys(const std::string & keys) { - CallKeySequence(keys.c_str()); + mainWindow->CallKeySequence(keys.c_str()); } void processKey(int sym, bool ctrl=false, bool shift=false, bool alt=false) @@ -165,7 +165,7 @@ void processKey(int sym, bool ctrl=false, bool shift=false, bool alt=false) mod |= ctrl ? KMOD_CTRL : 0; mod |= shift ? KMOD_SHIFT : 0; mod |= alt ? KMOD_ALT : 0; - GetAppWindow()->callKeyDown(sym, mod); + mainWindow->getSdl()->callKeyDown(sym, mod); } void setupResizeEventCallback(const std::string & id) @@ -189,7 +189,7 @@ void setupResizeEventCallback(const std::string & id) std::string getHelpString() { VisualizationSceneScalarData* vss - = dynamic_cast(GetVisualizationScene()); + = dynamic_cast(mainWindow->getScene()); return vss->GetHelpString(); } @@ -215,6 +215,11 @@ void SetUseTexture(int ut) } } +void SendExposeEvent() +{ + mainWindow->SendExposeEvent(); +} + } // namespace js // Info on type conversion: @@ -225,7 +230,7 @@ EMSCRIPTEN_BINDINGS(js_funcs) em::function("startVisualization", &js::startVisualization); em::function("updateVisualization", &js::updateVisualization); em::function("iterVisualization", &js::iterVisualization); - em::function("sendExposeEvent", &SendExposeEvent); + em::function("sendExposeEvent", &js::SendExposeEvent); em::function("disableKeyHanding", &js::disableKeyHandling); em::function("enableKeyHandling", &js::enableKeyHandling); em::function("setKeyboardListeningElementId", From 51f67e8967b8a7410d8351bd38db465b57f03559 Mon Sep 17 00:00:00 2001 From: Tom Stitt Date: Sat, 5 Jun 2021 11:42:02 -0700 Subject: [PATCH 35/39] fix some bugs & emscripten incompatibilities closer to working but not quite there --- lib/aux_js.cpp | 30 ++++++++++++++---------------- lib/aux_vis.cpp | 10 ++++++++-- lib/aux_vis.hpp | 6 ++++++ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/aux_js.cpp b/lib/aux_js.cpp index 2f417844..4fa39268 100644 --- a/lib/aux_js.cpp +++ b/lib/aux_js.cpp @@ -20,10 +20,9 @@ std::string plot_caption; std::string extra_caption; // used in extern context mfem::GeometryRefiner GLVisGeometryRefiner; // used in extern context -static VisualizationSceneScalarData * vs = nullptr; -StreamState stream_state; -GLVisWindow * mainWindow = nullptr; -bool keep_attr = false; +static GLVisWindow * mainWindow = nullptr; +static VisualizationSceneScalarData * vssd = nullptr; +static bool keep_attr = false; namespace js { @@ -33,8 +32,10 @@ using namespace mfem; bool startVisualization(const std::string input, const std::string data_type, int w, int h) { + delete mainWindow; std::stringstream ss(input); + StreamState stream_state; // 0 - scalar data, 1 - vector data, 2 - mesh only, (-1) - unknown const int field_type = stream_state.ReadStream(ss, data_type); @@ -80,15 +81,13 @@ bool startVisualization(const std::string input, const std::string data_type, return false; } - delete vs; - vs = nullptr; - double mesh_range = -1.0; mainWindow->InitVisualization(field_type, std::move(stream_state), keep_attr); + vssd = dynamic_cast(mainWindow->getScene()); if (minv || maxv) { - vs->SetValueRange(minv, maxv); + vssd->SetValueRange(minv, maxv); } mainWindow->SendExposeEvent(); return true; @@ -110,11 +109,12 @@ int updateVisualization(std::string data_type, std::string stream) new_state.ReadStream(ss, data_type); double mesh_range = -1.0; - if (stream_state.SetNewMeshAndSolution(std::move(new_state), vs)) + StreamState & stream_state = mainWindow->getStreamState(); + if (stream_state.SetNewMeshAndSolution(std::move(new_state), vssd)) { if (mesh_range > 0.0) { - vs->SetValueRange(-mesh_range, mesh_range); + vssd->SetValueRange(-mesh_range, mesh_range); } mainWindow->SendExposeEvent(); return 0; @@ -188,9 +188,7 @@ void setupResizeEventCallback(const std::string & id) std::string getHelpString() { - VisualizationSceneScalarData* vss - = dynamic_cast(mainWindow->getScene()); - return vss->GetHelpString(); + return vssd->GetHelpString(); } void ResizeWindow(int w, int h) @@ -200,18 +198,18 @@ void ResizeWindow(int w, int h) int GetUseTexture() { - return vs->palette.GetSmoothSetting(); + return vssd->palette.GetSmoothSetting(); } void SetUseTexture(int ut) { if (ut == 0) { - vs->palette.UseDiscrete(); + vssd->palette.UseDiscrete(); } else { - vs->palette.UseSmooth(); + vssd->palette.UseSmooth(); } } diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 8ebbb7fa..0f9a5fda 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -271,9 +271,9 @@ void GLVisWindow::InitVisualization(int field_type, StreamState state, AddIdleFunc(::MainLoop); } - if (state.keys == "") + if (!prob_state.keys.empty()) { - CallKeySequence(state.keys.c_str()); + CallKeySequence(prob_state.keys.c_str()); } } @@ -491,6 +491,7 @@ void GLVisWindow::MyExpose() wnd->signalSwap(); } +#ifndef __EMSCRIPTEN__ bool GLVisWindow::CommunicationIdleFunc() { int status = glvis_command->Execute(); @@ -506,6 +507,7 @@ bool GLVisWindow::CommunicationIdleFunc() } return false; } +#endif bool GLVisWindow::MainIdleFunc() { @@ -561,7 +563,11 @@ void GLVisWindow::RemoveIdleFunc(GLVisWindow::IdleFPtr Func) if (idle_funcs.Size() == 0) { use_idle = false; +#ifndef __EMSCRIPTEN__ if (!glvis_command) { wnd->setOnIdle(nullptr); } +#else + wnd->setOnIdle(nullptr); +#endif } } diff --git a/lib/aux_vis.hpp b/lib/aux_vis.hpp index d329622a..c3ef18bc 100644 --- a/lib/aux_vis.hpp +++ b/lib/aux_vis.hpp @@ -22,8 +22,10 @@ #include "openglvis.hpp" #include "stream_reader.hpp" +#ifndef __EMSCRIPTEN__ class GLVisCommand; class communication_thread; +#endif class GLVisWindow { @@ -154,7 +156,9 @@ class GLVisWindow void SetKeyEventHandler(int key, void (GLVisWindow::*handler)(GLenum)); bool MainIdleFunc(); +#ifndef __EMSCRIPTEN__ bool CommunicationIdleFunc(); +#endif void MyReshape(GLsizei w, GLsizei h); void MyExpose(GLsizei w, GLsizei h); @@ -191,8 +195,10 @@ class GLVisWindow std::unique_ptr wnd; std::unique_ptr locscene; +#ifndef __EMSCRIPTEN__ std::unique_ptr glvis_command; std::unique_ptr comm_thread; +#endif StreamState prob_state; bool use_idle = false; From 44fd6d8e94d589ce0adc1d150cc8b8f86ef762ad Mon Sep 17 00:00:00 2001 From: Tom Stitt Date: Sun, 13 Jun 2021 13:18:10 -0700 Subject: [PATCH 36/39] 'fix' issue where the canvas size doubles on new vis; not sure why this is happening yet but reusing the existing GLVisWindow does the trick (and is what we were doing previously) --- lib/aux_js.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/aux_js.cpp b/lib/aux_js.cpp index 4fa39268..75c97b25 100644 --- a/lib/aux_js.cpp +++ b/lib/aux_js.cpp @@ -32,7 +32,6 @@ using namespace mfem; bool startVisualization(const std::string input, const std::string data_type, int w, int h) { - delete mainWindow; std::stringstream ss(input); StreamState stream_state; @@ -64,21 +63,24 @@ bool startVisualization(const std::string input, const std::string data_type, return false; } - try + if (mainWindow == nullptr) { - mainWindow = new GLVisWindow("glvis", 0, 0, w, h, false); - } - catch (std::runtime_error& ex) - { - cerr << "Initializing the visualization failed: " << endl - << ex.what() << endl; - return false; - } - catch (...) - { - cerr << "Initializing the visualization failed - unknown error." - << endl; - return false; + try + { + mainWindow = new GLVisWindow("glvis", 0, 0, w, h, false); + } + catch (std::runtime_error& ex) + { + cerr << "Initializing the visualization failed: " << endl + << ex.what() << endl; + return false; + } + catch (...) + { + cerr << "Initializing the visualization failed - unknown error." + << endl; + return false; + } } double mesh_range = -1.0; From c0de2933de9d58e9e2a6647a133ef0e6e4f3a3d2 Mon Sep 17 00:00:00 2001 From: Tzanio Date: Tue, 15 Jun 2021 17:23:57 -0700 Subject: [PATCH 37/39] make style --- glvis.cpp | 14 +++++++------- lib/gl/attr_traits.hpp | 6 +++--- lib/gl/types.hpp | 8 ++++---- lib/openglvis.cpp | 16 ++++++++-------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/glvis.cpp b/glvis.cpp index 8ddc462b..47e61e8b 100644 --- a/glvis.cpp +++ b/glvis.cpp @@ -107,8 +107,8 @@ bool GLVisInitVis(StreamState stream_state, int field_type) try { - mainWindow = new GLVisWindow(win_title, window_x, window_y, - window_w, window_h, legacy_gl_ctx); + mainWindow = new GLVisWindow(win_title, window_x, window_y, + window_w, window_h, legacy_gl_ctx); } catch (std::runtime_error& ex) { @@ -116,15 +116,15 @@ bool GLVisInitVis(StreamState stream_state, int field_type) << ex.what() << endl; return false; } - catch(...) + catch (...) { - cerr << "Initializing the visualization failed - unknown error." - << endl; + cerr << "Initializing the visualization failed - unknown error." + << endl; return false; } if (font_name != string_default) { - mainWindow->SetFont(font_name); + mainWindow->SetFont(font_name); } mainWindow->InitVisualization(field_type, std::move(stream_state), @@ -285,7 +285,7 @@ void ExecuteScriptCommand(GLVisWindow* wnd) } VisualizationSceneScalarData* vs - = static_cast(wnd->getScene()); + = static_cast(wnd->getScene()); StreamState& stream_state = wnd->getStreamState(); istream &scr = *script; string word; diff --git a/lib/gl/attr_traits.hpp b/lib/gl/attr_traits.hpp index b993c115..662ea078 100644 --- a/lib/gl/attr_traits.hpp +++ b/lib/gl/attr_traits.hpp @@ -125,9 +125,9 @@ AttrNormal> const static GLenum FFArrayIdx = GL_NORMAL_ARRAY; static void FFSetupFunc(GLint /*size*/, GLenum type, GLsizei stride, const GLvoid* ptr) - { - glNormalPointer(type, stride, ptr); - } +{ + glNormalPointer(type, stride, ptr); +} }; template diff --git a/lib/gl/types.hpp b/lib/gl/types.hpp index 0cf9bc61..506d9e20 100644 --- a/lib/gl/types.hpp +++ b/lib/gl/types.hpp @@ -203,10 +203,10 @@ struct alignas(16) Vertex std::array coord; static Vertex create(double * d) - { - return Vertex {(float) d[0], (float) d[1], (float) d[2]}; - } - static const int layout = LAYOUT_VTX; +{ + return Vertex {(float) d[0], (float) d[1], (float) d[2]}; +} +static const int layout = LAYOUT_VTX; }; struct alignas(16) VertexColor diff --git a/lib/openglvis.cpp b/lib/openglvis.cpp index 195c7ec1..c7a60e04 100644 --- a/lib/openglvis.cpp +++ b/lib/openglvis.cpp @@ -73,10 +73,10 @@ glm::mat4 Camera::RotMatrix() double mat[16] = { -left[0], up[0], -dir[0], 0.0, - -left[1], up[1], -dir[1], 0.0, - -left[2], up[2], -dir[2], 0.0, - 0.0, 0.0, 0.0, 1.0 - }; + -left[1], up[1], -dir[1], 0.0, + -left[2], up[2], -dir[2], 0.0, + 0.0, 0.0, 0.0, 1.0 + }; return glm::make_mat4(mat); } @@ -86,10 +86,10 @@ glm::mat4 Camera::TransposeRotMatrix() double mat_t[16] = { -left[0], -left[1], -left[2], 0.0, - up[0], up[1], up[2], 0.0, - -dir[0], -dir[1], -dir[2], 0.0, - 0.0, 0.0, 0.0, 1.0 - }; + up[0], up[1], up[2], 0.0, + -dir[0], -dir[1], -dir[2], 0.0, + 0.0, 0.0, 0.0, 1.0 + }; return glm::make_mat4(mat_t); } From 9ba13ce6114d060300147345af90bc9388ae6bd1 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Tue, 15 Jun 2021 18:06:52 -0700 Subject: [PATCH 38/39] Fix for vsvector grid function initialization --- lib/vsvector.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/vsvector.cpp b/lib/vsvector.cpp index fd63d035..41362fb7 100644 --- a/lib/vsvector.cpp +++ b/lib/vsvector.cpp @@ -257,11 +257,6 @@ VisualizationSceneVector::VisualizationSceneVector(GridFunction &vgf) sol = new Vector(mesh -> GetNV()); - // VisualizationSceneSolution::Init() sets rsol = NULL ! - { - SetGridFunction(vgf); - } - mesh->GetNodes(vc0); if (vc0.Size() != vgf.Size()) { @@ -458,7 +453,12 @@ void VisualizationSceneVector::Init(GLVisWindow* wnd) (*sol)(i) = Vec2Scalar((*solx)(i), (*soly)(i)); } + // VisualizationSceneSolution::Init() sets rsol = null VisualizationSceneSolution::Init(wnd); + if (VecGridF) + { + SetGridFunction(*VecGridF); + } PrepareVectorField(); // PrepareDisplacedMesh(); // called by PrepareLines() From 31cc55bc5f35023350baf470d52a0c1f49223021 Mon Sep 17 00:00:00 2001 From: Max Yang Date: Tue, 15 Jun 2021 18:12:36 -0700 Subject: [PATCH 39/39] Simplify GLVisCommand constructors --- lib/aux_vis.cpp | 2 +- lib/threads.cpp | 13 ++++++++----- lib/threads.hpp | 10 ++++------ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/aux_vis.cpp b/lib/aux_vis.cpp index 0f9a5fda..d0be1a95 100644 --- a/lib/aux_vis.cpp +++ b/lib/aux_vis.cpp @@ -248,7 +248,7 @@ void GLVisWindow::InitVisualization(int field_type, StreamState state, #ifndef __EMSCRIPTEN__ if (input_streams.Size() > 0) { - glvis_command.reset(new GLVisCommand(this, prob_state, &keep_attr)); + glvis_command.reset(new GLVisCommand(this, keep_attr)); comm_thread.reset(new communication_thread(glvis_command.get(), input_streams)); } #endif diff --git a/lib/threads.cpp b/lib/threads.cpp index 54a36220..f5df5fc2 100644 --- a/lib/threads.cpp +++ b/lib/threads.cpp @@ -17,11 +17,9 @@ using namespace std; extern const char *strings_off_on[]; // defined in vsdata.cpp GLVisCommand::GLVisCommand( - GLVisWindow* wnd, StreamState& state, bool *_keep_attr) - : window(wnd), curr_state(state) + GLVisWindow* wnd, bool _keep_attr) + : window(wnd), keep_attr(_keep_attr) { - keep_attr = _keep_attr; - num_waiting = 0; terminating = false; command = NO_COMMAND; @@ -29,6 +27,11 @@ GLVisCommand::GLVisCommand( autopause = 0; } +bool GLVisCommand::FixElementOrientations() const +{ + return window->getStreamState().fix_elem_orient; +} + int GLVisCommand::lock() { int my_id; @@ -414,7 +417,7 @@ int GLVisCommand::Execute() new_state.SetMeshSolution(); mesh_range = new_state.grid_f->Max() + 1.0; } - if (curr_state.SetNewMeshAndSolution(std::move(new_state), vs)) + if (window->getStreamState().SetNewMeshAndSolution(std::move(new_state), vs)) { if (mesh_range > 0.0) { diff --git a/lib/threads.hpp b/lib/threads.hpp index aa49ed1d..87d8d8ac 100644 --- a/lib/threads.hpp +++ b/lib/threads.hpp @@ -26,8 +26,7 @@ class GLVisCommand private: // Pointers to global GLVis data GLVisWindow* window; - StreamState& curr_state; - bool *keep_attr; + bool keep_attr; std::mutex glvis_mutex; std::condition_variable glvis_cond; @@ -96,12 +95,11 @@ class GLVisCommand public: // called by the main execution thread - GLVisCommand(GLVisWindow* parent, - StreamState& thread_state, bool *_keep_attr); + GLVisCommand(GLVisWindow* parent, bool _keep_attr); // to be used by worker threads - bool KeepAttrib() { return *keep_attr; } // may need to sync this - bool FixElementOrientations() { return curr_state.fix_elem_orient; } + bool KeepAttrib() const { return keep_attr; } // may need to sync this + bool FixElementOrientations() const; // called by worker threads int NewMeshAndSolution(std::unique_ptr _new_m,