27 #if MRPT_HAS_WXWIDGETS 39 wxWindow* parent, wxWindowID winID,
int x,
int y,
int width,
int height)
42 this->Create(parent, winID, wxPoint(x, y), wxSize(width, height));
44 Bind(wxEVT_PAINT, &CWindowDialog::wxMRPTImageControl::OnPaint,
this);
45 Bind(wxEVT_MOTION, &CWindowDialog::wxMRPTImageControl::OnMouseMove,
this);
47 wxEVT_LEFT_DOWN, &CWindowDialog::wxMRPTImageControl::OnMouseClick,
50 wxEVT_CHAR, &CWindowDialog::wxMRPTImageControl::OnChar,
this, wxID_ANY);
51 Bind(wxEVT_CHAR, &CWindowDialog::wxMRPTImageControl::OnChar,
this);
54 CWindowDialog::wxMRPTImageControl::~wxMRPTImageControl()
56 std::lock_guard<std::mutex> lock(m_img_cs);
57 if (m_img) m_img.reset();
60 void CWindowDialog::wxMRPTImageControl::OnMouseMove(wxMouseEvent& ev)
63 m_last_mouse_point = ev.GetPosition();
66 void CWindowDialog::wxMRPTImageControl::OnMouseClick(wxMouseEvent& ev)
69 m_last_mouse_click = ev.GetPosition();
72 void CWindowDialog::wxMRPTImageControl::OnChar(wxKeyEvent& ev) {}
73 void CWindowDialog::wxMRPTImageControl::AssignImage(wxBitmap* img)
75 std::lock_guard<std::mutex> lock(m_img_cs);
79 void CWindowDialog::wxMRPTImageControl::OnPaint(wxPaintEvent& ev)
83 std::lock_guard<std::mutex> lock(m_img_cs);
90 dc.DrawBitmap(*m_img, 0, 0);
93 void CWindowDialog::wxMRPTImageControl::GetBitmap(wxBitmap& bmp)
95 std::lock_guard<std::mutex> lock(m_img_cs);
100 CWindowDialog::CWindowDialog(
102 const std::string& caption, wxSize initialSize)
103 : m_win2D(win2D), m_mainFrame(parent)
106 parent,
id, caption.c_str(), wxDefaultPosition, initialSize,
107 wxDEFAULT_FRAME_STYLE, _T(
"id"));
108 SetClientSize(initialSize);
119 SetWindowStyle(GetWindowStyle() | wxCLIP_CHILDREN);
122 auto* MenuBar1 =
new wxMenuBar();
124 auto* Menu1 =
new wxMenu();
125 wxMenuItem* MenuItem3 =
new wxMenuItem(
126 Menu1,
ID_MENUITEM3, _(
"Save to file..."), _(
""), wxITEM_NORMAL);
127 Menu1->Append(MenuItem3);
128 wxMenuItem* MenuItem1 =
129 new wxMenuItem(Menu1,
ID_MENUITEM1, _(
"Close"), _(
""), wxITEM_NORMAL);
130 Menu1->Append(MenuItem1);
131 MenuBar1->Append(Menu1, _(
"&File"));
133 auto* Menu2 =
new wxMenu();
134 wxMenuItem* MenuItem2 =
new wxMenuItem(
135 Menu2,
ID_MENUITEM2, _(
"About..."), _(
""), wxITEM_NORMAL);
136 Menu2->Append(MenuItem2);
137 MenuBar1->Append(Menu2, _(
"&Help"));
139 SetMenuBar(MenuBar1);
170 bool allow_close =
true;
180 if (!allow_close)
return;
202 const int code =
event.GetKeyCode();
229 m_win2D, event.GetSize().GetWidth(),
230 event.GetSize().GetHeight()));
248 event.LeftDown(),
event.RightDown()));
266 event.LeftDown(),
event.RightDown()));
281 _(
"Image viewer\n Class gui::CDisplayWindow\n MRPT C++ library"),
289 this, wxT(
"Save image as..."), wxT(
"."), wxT(
"image.png"),
290 wxT(
"PNG image files (*.png)|*.png"),
291 wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
293 if (wxID_OK == dialog.ShowModal())
299 bmp.SaveFile(dialog.GetPath(), wxBITMAP_TYPE_PNG);
310 const std::string& windowCaption,
unsigned int initWidth,
311 unsigned int initHeight)
313 return std::make_shared<CDisplayWindow>(
314 windowCaption, initWidth, initHeight);
320 const std::string& windowCaption,
unsigned int initWidth,
321 unsigned int initHeight)
322 :
CBaseGUIWindow(static_cast<void*>(this), 200, 299, windowCaption)
336 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT 339 win->m_image->SetCursor(
340 *(cursorIsCross ? wxCROSS_CURSOR : wxSTANDARD_CURSOR));
351 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT 353 if (!
win)
return false;
354 x =
win->m_image->m_last_mouse_point.x;
355 y =
win->m_image->m_last_mouse_point.y;
369 #if MRPT_HAS_WXWIDGETS 380 REQ->voidPtr2 = (
void*)newImg;
394 const TColor& color,
bool showNumbers)
396 std::vector<float> x(x_.
size()), y(y_.
size());
397 for (
size_t i = 0; i < x.size(); i++) x[i] = x_[i];
398 for (
size_t i = 0; i < y.size(); i++) y[i] = y_[i];
403 const CImage& img,
const std::vector<float>& x,
const std::vector<float>& y,
404 const TColor& color,
bool showNumbers)
406 #if MRPT_HAS_WXWIDGETS 411 for (
size_t i = 0; i < x.size(); i++)
419 imgColor.textOut(
round(x[i]) - 10,
round(y[i]), buf, color);
449 imgColor.
line(40, 40, 560, 40, TColor::black(), 3);
450 imgColor.
line(40, 40, 40, 440, TColor::black(), 3);
451 imgColor.
line(560, 40, 555, 45, TColor::black(), 3);
452 imgColor.
line(560, 40, 555, 35, TColor::black(), 3);
453 imgColor.
line(40, 440, 35, 435, TColor::black(), 3);
454 imgColor.
line(40, 440, 45, 435, TColor::black(), 3);
461 itymx = std::max_element(y.
begin(), y.
end());
462 itymn = std::min_element(y.
begin(), y.
end());
463 float px = (x[x.
size() - 1] - x[0]) / 520;
464 float py = (*itymx - *itymn) / 400;
466 float tpxA = 0, tpyA = 0;
468 for (itx = x.
begin(), ity = y.
begin(); itx != x.
end(); ++itx, ++ity)
470 float tpx = (*itx - x[0]) / px + ox;
471 float tpy = (*ity - *itymn) / py + oy;
473 if (itx != x.
begin())
502 imgColor.
line(40, 40, 560, 40, TColor::black(), 3);
503 imgColor.
line(40, 40, 40, 440, TColor::black(), 3);
504 imgColor.
line(560, 40, 555, 45, TColor::black(), 3);
505 imgColor.
line(560, 40, 555, 35, TColor::black(), 3);
506 imgColor.
line(40, 440, 35, 435, TColor::black(), 3);
507 imgColor.
line(40, 440, 45, 435, TColor::black(), 3);
509 imgColor.
textOut(550, 25,
"x", TColor::black());
510 imgColor.
textOut(25, 430,
"y", TColor::black());
514 itymx = std::max_element(y.
begin(), y.
end());
515 itymn = std::min_element(y.
begin(), y.
end());
516 float px = y.
size() / 520.0f;
517 float py = (*itymx - *itymn) / 400.0f;
518 int tpxA = 0, tpyA = 0;
522 for (k = 0, ity = y.
begin(); ity != y.
end(); ++k, ++ity)
524 auto tpx =
round(k / px + ox);
525 auto tpy =
round((*ity - *itymn) / py + oy);
526 imgColor.
drawMark(tpx, tpy, TColor::red(),
'x');
527 if (k > 0) imgColor.
line(tpxA, tpyA, tpx, tpy, TColor::blue(), 3);
542 #if MRPT_HAS_WXWIDGETS 545 cerr <<
"[CDisplayWindow::resize] Window closed!: " <<
m_caption 568 #if MRPT_HAS_WXWIDGETS 571 cerr <<
"[CDisplayWindow::setPos] Window closed!: " <<
m_caption 594 #if MRPT_HAS_WXWIDGETS 597 cerr <<
"[CDisplayWindow::setWindowTitle] Window closed!: " <<
m_caption An event sent by a window upon resize.
static void pushPendingWxRequest(TRequestToWxMainThread *data)
Thread-safe method to insert a new pending request (The memory must be dinamically allocated with "ne...
void line(int x0, int y0, int x1, int y1, const mrpt::img::TColor color, unsigned int width=1, TPenStyle penStyle=psSolid) override
Draws a line.
An event sent by a window upon a mouse click, giving the (x,y) pixel coordinates. ...
std::atomic_int m_keyPushedCode
void OnMenuSave(wxCommandEvent &event)
void OnResize(wxSizeEvent &event)
The data structure for each inter-thread request:
Template for column vectors of dynamic size, compatible with Eigen.
mrpt::void_ptr_noncopy m_hwnd
The window handle.
void setCursorCross(bool cursorIsCross) override
Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) ...
std::string m_caption
The caption of the window.
static int notifyWindowCreation()
Atomically increments the number of windows created with the main frame as parent.
~CDisplayWindow() override
Destructor.
void GetBitmap(wxBitmap &bmp)
size_type size() const
Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))
static wxBitmap getMRPTDefaultIcon()
wxImage * MRPTImage2wxImage(const mrpt::img::CImage &img)
Create a wxImage from a MRPT image.
An event sent by a window upon when it's about to be closed, either manually by the user or programma...
An event sent by a window when the mouse is moved over it.
void setWindowTitle(const std::string &str) override
Changes the window title text.
void plot(const mrpt::math::CVectorFloat &x, const mrpt::math::CVectorFloat &y)
Plots a graph in MATLAB-like style.
void notifyChildWindowDestruction()
Called by wx main thread to set m_hwnd to NULL.
wxMRPTImageControl * m_image
CImage colorImage() const
Returns a color (RGB) version of the grayscale image, or a shallow copy of itself if it is already a ...
void OnKeyDown(wxKeyEvent &event)
void OnMouseDown(wxMouseEvent &event)
#define ASSERT_(f)
Defines an assertion mechanism.
void OnMenuClose(wxCommandEvent &event)
This base provides a set of functions for maths stuff.
A custom control to display the bitmap and avoid flicker.
void showImageAndPoints(const mrpt::img::CImage &img, const mrpt::math::CVectorFloat &x, const mrpt::math::CVectorFloat &y, const mrpt::img::TColor &color=mrpt::img::TColor::red(), bool showNumbers=false)
Show a given color or grayscale image on the window and print a set of points on it.
virtual void filledRectangle(int x0, int y0, int x1, int y1, const mrpt::img::TColor color)
Draws a filled rectangle.
bool getLastMousePosition(int &x, int &y) const override
Gets the last x,y pixel coordinates of the mouse.
This class creates a window as a graphical user interface (GUI) for displaying images to the user...
bool isOpen()
Returns false if the user has already closed the window.
std::atomic< mrptKeyModifier > m_keyPushedModifier
The main frame of the wxWidgets application.
A pair (x,y) of pixel coordinates (integer resolution).
The wx dialog for gui::CDisplayWindow.
typename vec_t::const_iterator const_iterator
An event sent by a window upon a char pressed by the user.
void OnMouseMove(wxMouseEvent &event)
mrpt::gui::CDisplayWindow3D::Ptr win
void resize(unsigned int width, unsigned int height) override
Resizes the window, stretching the image to fit into the display area.
mrptKeyModifier keyEventToMrptKeyModifier(const wxKeyEvent &ev)
Extracts the key modifiers from a wxKeyEvent.
void setPos(int x, int y) override
Changes the position of the window on the screen.
void OnChar(wxKeyEvent &event)
void drawMark(int x0, int y0, const mrpt::img::TColor color, char type, int size=5, unsigned int width=1)
Draw a mark.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void OnClose(wxCloseEvent &event)
void OnMenuAbout(wxCommandEvent &event)
std::atomic_bool m_keyPushed
bool hasSubscribers() const
Can be called by a derived class before preparing an event for publishing with publishEvent to determ...
void publishEvent(const mrptEvent &e) const
Called when you want this object to emit an event to all the observers currently subscribed to this o...
static const long ID_IMAGE_BITMAP
void showImage(const mrpt::img::CImage &img)
Show a given color or grayscale image on the window.
void destroyWxWindow()
Must be called by child classes in their destructors.
virtual void textOut(int x0, int y0, const std::string &str, const mrpt::img::TColor color)
Renders 2D text using bitmap fonts.
CDisplayWindow(const std::string &windowCaption=std::string(), unsigned int initWidth=400, unsigned int initHeight=400)
Constructor.
Classes for creating GUI windows for 2D and 3D visualization.
void createWxWindow(unsigned int initialWidth, unsigned int initialHeight)
Must be called by child classes just within the constructor.
static CDisplayWindow::Ptr Create(const std::string &windowCaption, unsigned int initWidth=400, unsigned int initHeight=400)
Class factory returning a smart pointer.
mrpt::gui::CDisplayWindow * source2D
Only one of source* can be non-nullptr, indicating the class that generated the request.
~CWindowDialog() override
std::promise< void > m_windowDestroyed
This semaphore will be signaled when the wx window is destroyed.
The base class for GUI window classes based on wxWidgets.
static int notifyWindowDestruction()
Atomically decrements the number of windows created with the main frame as parent.
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
A class for storing images as grayscale or RGB bitmaps.
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
int round(const T value)
Returns the closer integer (int) to x.