Skip to content

Commit

Permalink
mola_viz: show console messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Jan 6, 2024
1 parent e125412 commit debe035
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
3 changes: 3 additions & 0 deletions mola_kernel/include/mola_kernel/interfaces/VizInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class VizInterface
const mrpt::math::TPoint3Df& lookAt,
const std::string& viewportName = "main",
const std::string& parentWindow = "main") = 0;

virtual std::future<bool> output_console_message(
const std::string& msg, const std::string& parentWindow = "main") = 0;
};

} // namespace mola
20 changes: 19 additions & 1 deletion mola_viz/include/mola_viz/MolaViz.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ class MolaViz : public ExecutableBase, public VizInterface
const std::string& viewportName = "main",
const std::string& parentWindow = DEFAULT_WINDOW_NAME) override;

std::future<bool> output_console_message(
const std::string& msg,
const std::string& parentWindow = "main") override;

/** @} */

/** @name mola-viz GUI update handlers registry
Expand All @@ -109,6 +113,14 @@ class MolaViz : public ExecutableBase, public VizInterface

/** @} */

/** @name mola-viz module parameters
* @{ */

double console_text_font_size_ = 9.0;
unsigned int max_console_lines_ = 5;

/** @} */

void markWindowForReLayout(const window_name_t& name)
{
guiThreadMustReLayoutTheseWindows_.insert(name);
Expand All @@ -121,7 +133,13 @@ class MolaViz : public ExecutableBase, public VizInterface
mrpt::gui::CDisplayWindowGUI::Ptr create_and_add_window(
const window_name_t& name);

std::map<window_name_t, mrpt::gui::CDisplayWindowGUI::Ptr> windows_;
struct PerWindowData
{
mrpt::gui::CDisplayWindowGUI::Ptr win;
std::vector<std::string> console_messages = {};
};

std::map<window_name_t, PerWindowData> windows_;
std::map<window_name_t, std::map<subwindow_name_t, nanogui::Window*>>
subWindows_;

Expand Down
63 changes: 57 additions & 6 deletions mola_viz/src/MolaViz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ void MolaViz::initialize(const Yaml& c)
auto cfg = c["params"];
MRPT_LOG_DEBUG_STREAM("Loading these params:\n" << cfg);

YAML_LOAD_MEMBER_OPT(max_console_lines, unsigned int);
YAML_LOAD_MEMBER_OPT(console_text_font_size, double);

// Mark as initialized and up:
instanceMtx_.lock();
instance_ = this;
Expand All @@ -421,12 +424,13 @@ mrpt::gui::CDisplayWindowGUI::Ptr MolaViz::create_and_add_window(

mrpt::gui::CDisplayWindowGUI_Params cp;
cp.maximized = true;
windows_[name] = mrpt::gui::CDisplayWindowGUI::Create(name, 1000, 800, cp);
windows_[name] = {
mrpt::gui::CDisplayWindowGUI::Create(name, 1000, 800, cp)};

// create empty list of subwindows too:
subWindows_[name];

auto& win = windows_[name];
auto& win = windows_[name].win;

// Add a background scene:
auto scene = mrpt::opengl::COpenGLScene::Create();
Expand Down Expand Up @@ -494,7 +498,7 @@ void MolaViz::gui_thread()
lckHandlers.unlock();

for (const auto& winName : winsToReLayout)
windows_.at(winName)->performLayout();
windows_.at(winName).win->performLayout();
});

// A call to "nanogui::leave()" is required to end the infinite loop
Expand Down Expand Up @@ -594,7 +598,7 @@ std::future<nanogui::Window*> MolaViz::create_subwindow(
<< "'");

ASSERT_(windows_.count(parentWindow));
auto topWin = windows_.at(parentWindow);
auto topWin = windows_.at(parentWindow).win;
ASSERT_(topWin);

auto subw = topWin->createManagedSubWindow(subWindowTitle);
Expand Down Expand Up @@ -664,7 +668,7 @@ std::future<bool> MolaViz::update_3d_object(
"update_3d_object() objName='" << objName << "'");

ASSERT_(windows_.count(parentWindow));
auto topWin = windows_.at(parentWindow);
auto topWin = windows_.at(parentWindow).win;
ASSERT_(topWin);

// No need to acquire the mutex, since this task will be run
Expand Down Expand Up @@ -715,7 +719,7 @@ std::future<bool> MolaViz::update_viewport_look_at(
"update_viewport_look_at() lookAt=" << lookAt.asString());

ASSERT_(windows_.count(parentWindow));
auto topWin = windows_.at(parentWindow);
auto topWin = windows_.at(parentWindow).win;
ASSERT_(topWin);

// No need to acquire the mutex, since this task will be run
Expand All @@ -732,6 +736,53 @@ std::future<bool> MolaViz::update_viewport_look_at(
return task->get_future();
}

std::future<bool> MolaViz::output_console_message(
const std::string& msg, const std::string& parentWindow)
{
using return_type = bool;

auto task = std::make_shared<std::packaged_task<return_type()>>(
[this, msg, parentWindow]() {
MRPT_LOG_DEBUG_STREAM("output_console_message() msg=" << msg);

ASSERT_(windows_.count(parentWindow));
auto& winData = windows_.at(parentWindow);

// Append msg:
winData.console_messages.push_back(msg);
// remove older ones:
while (winData.console_messages.size() > max_console_lines_)
winData.console_messages.erase(
winData.console_messages.begin());

mrpt::gui::CDisplayWindowGUI::Ptr topWin = winData.win;
ASSERT_(topWin);

// No need to acquire the mutex, since this task will be run
// in the proper moment in the proper thread:
ASSERT_(topWin->background_scene);

const double LINE_HEIGHT = console_text_font_size_;
const double LINE_SPACING = 3.0;
mrpt::opengl::TFontParams fp;
fp.vfont_scale = LINE_HEIGHT;

for (size_t i = 0; i < winData.console_messages.size(); i++)
{
topWin->background_scene->getViewport()->addTextMessage(
3.0, LINE_SPACING + (LINE_SPACING + LINE_HEIGHT) * i,
winData.console_messages.at(i), i, fp);
}

return true;
});

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

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

0 comments on commit debe035

Please sign in to comment.