Skip to content

Commit

Permalink
viz API: add enqueue_custom_nanogui_code()
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Jan 18, 2024
1 parent e0d1458 commit ceb9106
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion mola_kernel/include/mola_kernel/interfaces/VizInterface.h
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -52,6 +52,9 @@ class VizInterface
const std::string& viewportName = "main",
const std::string& parentWindow = "main") = 0;

virtual std::future<void> enqueue_custom_nanogui_code(
const std::function<void(void)>& userCode) = 0;

virtual std::future<bool> output_console_message(
const std::string& msg, const std::string& parentWindow = "main") = 0;
};
Expand Down
8 changes: 7 additions & 1 deletion mola_viz/include/mola_viz/MolaViz.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<nanogui::Window*> create_subwindow(
const std::string& subWindowTitle,
const std::string& parentWindow = DEFAULT_WINDOW_NAME) override;
Expand Down Expand Up @@ -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<void> enqueue_custom_nanogui_code(
const std::function<void(void)>& userCode) override;

/** @} */

/** @name mola-viz GUI update handlers registry
Expand Down
13 changes: 13 additions & 0 deletions mola_viz/src/MolaViz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,19 @@ std::future<bool> MolaViz::output_console_message(
return task->get_future();
}

std::future<void> MolaViz::enqueue_custom_nanogui_code(
const std::function<void(void)>& userCode)
{
using return_type = void;

auto task = std::make_shared<std::packaged_task<return_type()>>(
[=]() { userCode(); });

auto lck = mrpt::lockHelper(guiThreadPendingTasksMtx_);
guiThreadPendingTasks_.emplace_back([=]() { (*task)(); });
return task->get_future();
}

#if 0
// Visualize GT:
if (1)
Expand Down

0 comments on commit ceb9106

Please sign in to comment.