From ceb91064d5409afdc43ef0ad45a57d8123fedffb Mon Sep 17 00:00:00 2001 From: Jose Luis Blanco-Claraco Date: Thu, 18 Jan 2024 23:35:11 +0100 Subject: [PATCH] viz API: add enqueue_custom_nanogui_code() --- .../include/mola_kernel/interfaces/VizInterface.h | 5 ++++- mola_viz/include/mola_viz/MolaViz.h | 8 +++++++- mola_viz/src/MolaViz.cpp | 13 +++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/mola_kernel/include/mola_kernel/interfaces/VizInterface.h b/mola_kernel/include/mola_kernel/interfaces/VizInterface.h index 2d473c52..0f6d9f83 100644 --- a/mola_kernel/include/mola_kernel/interfaces/VizInterface.h +++ b/mola_kernel/include/mola_kernel/interfaces/VizInterface.h @@ -1,4 +1,4 @@ -/* ------------------------------------------------------------------------- +/* ------------------------------------------------------------------------- * A Modular Optimization framework for Localization and mApping (MOLA) * Copyright (C) 2018-2024 Jose Luis Blanco, University of Almeria * See LICENSE for license information. @@ -52,6 +52,9 @@ class VizInterface const std::string& viewportName = "main", const std::string& parentWindow = "main") = 0; + virtual std::future enqueue_custom_nanogui_code( + const std::function& userCode) = 0; + virtual std::future output_console_message( const std::string& msg, const std::string& parentWindow = "main") = 0; }; diff --git a/mola_viz/include/mola_viz/MolaViz.h b/mola_viz/include/mola_viz/MolaViz.h index 218edb04..48896a17 100644 --- a/mola_viz/include/mola_viz/MolaViz.h +++ b/mola_viz/include/mola_viz/MolaViz.h @@ -62,7 +62,8 @@ class MolaViz : public ExecutableBase, public VizInterface static bool IsRunning(); static MolaViz* Instance(); - /** Returned object is owned by the VizInterface, do NOT delete it. */ + /** Returned object is owned by the VizInterface, do NOT delete it. Updates + * to it must be done via enqueue_custom_nanogui_code()*/ std::future create_subwindow( const std::string& subWindowTitle, const std::string& parentWindow = DEFAULT_WINDOW_NAME) override; @@ -98,6 +99,11 @@ class MolaViz : public ExecutableBase, public VizInterface const std::string& msg, const std::string& parentWindow = "main") override; + /// Updates to nanogui window controls must happen via this method to ensure + /// it is run by the correct thread, in the next available time slot. + std::future enqueue_custom_nanogui_code( + const std::function& userCode) override; + /** @} */ /** @name mola-viz GUI update handlers registry diff --git a/mola_viz/src/MolaViz.cpp b/mola_viz/src/MolaViz.cpp index b9345474..3ab050fb 100644 --- a/mola_viz/src/MolaViz.cpp +++ b/mola_viz/src/MolaViz.cpp @@ -792,6 +792,19 @@ std::future MolaViz::output_console_message( return task->get_future(); } +std::future MolaViz::enqueue_custom_nanogui_code( + const std::function& userCode) +{ + using return_type = void; + + auto task = std::make_shared>( + [=]() { userCode(); }); + + auto lck = mrpt::lockHelper(guiThreadPendingTasksMtx_); + guiThreadPendingTasks_.emplace_back([=]() { (*task)(); }); + return task->get_future(); +} + #if 0 // Visualize GT: if (1)