Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <mrpt/gui.h>
11 #include <mrpt/system/CObserver.h>
13 #include <iostream>
14 
15 using namespace mrpt;
16 using namespace mrpt::gui;
17 using namespace mrpt::math;
18 using namespace mrpt::system;
19 using namespace mrpt::img;
20 using namespace std;
21 
22 #include <mrpt/examples_config.h>
23 string myExampleImage(
24  MRPT_EXAMPLES_BASE_DIRECTORY + string("img_basic_example/frame_color.jpg"));
25 
27 {
28  protected:
29  void OnEvent(const mrptEvent& e)
30  {
32  cout << "[MyObserver] Event received: mrptEventOnDestroy\n";
33  else if (e.isOfType<mrptEventWindowResize>())
34  {
35  const mrptEventWindowResize& ee =
36  static_cast<const mrptEventWindowResize&>(e);
37  cout << "[MyObserver] Resize event received from: "
38  << ee.source_object << ", new size: " << ee.new_width << " x "
39  << ee.new_height << "\n";
40  }
41  else if (e.isOfType<mrptEventWindowChar>())
42  {
43  const mrptEventWindowChar& ee =
44  static_cast<const mrptEventWindowChar&>(e);
45  cout << "[MyObserver] Char event received from: "
46  << ee.source_object << ". Char code: " << ee.char_code
47  << " modif: " << ee.key_modifiers << "\n";
48  }
49  else if (e.isOfType<mrptEventWindowClosed>())
50  {
51  const mrptEventWindowClosed& ee =
52  static_cast<const mrptEventWindowClosed&>(e);
53  cout << "[MyObserver] Window closed event received from: "
54  << ee.source_object << "\n";
55  }
56  else if (e.isOfType<mrptEventMouseDown>())
57  {
58  const mrptEventMouseDown& ee =
59  static_cast<const mrptEventMouseDown&>(e);
60  cout << "[MyObserver] Mouse down event received from: "
61  << ee.source_object << "pt: " << ee.coords.x << ","
62  << ee.coords.y << "\n";
63  }
64  else
65  cout << "[MyObserver] Event received: Another mrptEvent \n";
66  }
67 };
68 
69 // Observe windows for events.
70 // Declared out of the scope of windows so we can observe their destructors
72 
74 {
75  // Create some windows:
76  CDisplayWindow win2D("Bitmap window", 300, 300);
77 
78  {
79  mrpt::img::CImage img(300, 300, 3);
80  img.filledRectangle(0, 0, 300, 300, TColor(0, 0, 255));
81  img.textOut(50, 50, "Hello world!", TColor(255, 255, 255));
82  win2D.showImage(img);
83  }
84 
85  CDisplayWindow3D win3D("3D window", 300, 300);
86 
87  {
88  mrpt::opengl::COpenGLScene::Ptr& scene = win3D.get3DSceneAndLock();
89  scene->insert(mrpt::make_aligned_shared<mrpt::opengl::CGridPlaneXY>());
90  win3D.unlockAccess3DScene();
91  win3D.repaint();
92  }
93 
94  CDisplayWindowPlots winPlot("Plots window", 300, 300);
95 
96  // Tile windows nicely:
97  win2D.setPos(10, 10);
98  win3D.setPos(340, 10);
99  winPlot.setPos(10, 340);
100 
101  observer.observeBegin(win2D);
102  observer.observeBegin(win3D);
103  observer.observeBegin(winPlot);
104 
105  // Wait until end:
106  cout << "Waiting for window events...\n";
107  cout << "** Close any window to end the program **\n";
108 
109  while (win2D.isOpen() && win3D.isOpen() && winPlot.isOpen())
110  {
111  std::this_thread::sleep_for(100ms);
112  }
113 }
114 
115 // ------------------------------------------------------
116 // MAIN
117 // ------------------------------------------------------
118 int main()
119 {
120  try
121  {
123  return 0;
124  }
125  catch (std::exception& e)
126  {
127  std::cout << "MRPT exception caught: " << e.what() << std::endl;
128  return -1;
129  }
130  catch (...)
131  {
132  printf("Untyped exception!!");
133  return -1;
134  }
135 }
mrpt::system::mrptEvent
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:33
MyObserver
Definition: vision_stereo_rectify/test.cpp:26
mrpt::img::TPixelCoord::y
int y
Definition: TPixelCoord.h:46
mrpt::gui::mrptEventWindowClosed::source_object
CBaseGUIWindow * source_object
Definition: CBaseGUIWindow.h:291
mrpt::system::CObserver::observeBegin
void observeBegin(CObservable &obj)
Starts the subscription of this observer to the given object.
Definition: CObserver.cpp:26
mrpt::gui::mrptEventWindowChar
An event sent by a window upon a char pressed by the user.
Definition: CBaseGUIWindow.h:167
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::gui::mrptEventWindowResize::new_height
size_t new_height
Definition: CBaseGUIWindow.h:206
myExampleImage
string myExampleImage(MRPT_EXAMPLES_BASE_DIRECTORY+string("img_basic_example/frame_color.jpg"))
mrpt::gui::mrptEventWindowResize
An event sent by a window upon resize.
Definition: CBaseGUIWindow.h:193
mrpt::system::mrptEvent::isOfType
bool isOfType() const
Definition: mrptEvent.h:42
mrpt::img::TPixelCoord::x
int x
Definition: TPixelCoord.h:46
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::gui::mrptEventMouseDown
An event sent by a window upon a mouse click, giving the (x,y) pixel coordinates.
Definition: CBaseGUIWindow.h:218
mrpt::img
Definition: CCanvas.h:17
TestGuiWindowsEvents
void TestGuiWindowsEvents()
Definition: vision_stereo_rectify/test.cpp:73
mrpt::gui::mrptEventMouseDown::source_object
CBaseGUIWindow * source_object
Definition: CBaseGUIWindow.h:234
mrpt::gui::mrptEventWindowResize::source_object
CBaseGUIWindow * source_object
Definition: CBaseGUIWindow.h:205
mrpt::gui::mrptEventWindowChar::key_modifiers
mrptKeyModifier key_modifiers
Modifiers (Shift, Control, etc...)
Definition: CBaseGUIWindow.h:184
mrpt::img::TColor
A RGB color - 8bit.
Definition: TColor.h:22
observer
MyObserver observer
Definition: vision_stereo_rectify/test.cpp:71
mrpt::system::mrptEventOnDestroy
An event sent by any CObservable object (automatically) just before being destroyed and telling its o...
Definition: mrptEvent.h:67
mrpt::gui::CDisplayWindow
This class creates a window as a graphical user interface (GUI) for displaying images to the user.
Definition: CDisplayWindow.h:30
CObserver.h
mrpt::gui::mrptEventWindowChar::char_code
int char_code
The virtual key code, as defined in <mrpt/gui/keycodes.h> (a replication of wxWidgets key codes).
Definition: CBaseGUIWindow.h:182
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
mrpt::gui::CDisplayWindowPlots
Create a GUI window and display plots with MATLAB-like interfaces and commands.
Definition: CDisplayWindowPlots.h:31
img
GLint GLvoid * img
Definition: glext.h:3763
mrpt::system::CObserver
Inherit from this class to get notified about events from any CObservable object after subscribing to...
Definition: CObserver.h:36
mrpt::gui::mrptEventMouseDown::coords
mrpt::img::TPixelCoord coords
Definition: CBaseGUIWindow.h:235
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
gui.h
mrpt::gui::mrptEventWindowResize::new_width
size_t new_width
Definition: CBaseGUIWindow.h:206
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
mrpt::gui::mrptEventWindowClosed
An event sent by a window upon when it's about to be closed, either manually by the user or programma...
Definition: CBaseGUIWindow.h:281
CGridPlaneXY.h
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
mrpt::gui::mrptEventWindowChar::source_object
CBaseGUIWindow * source_object
Definition: CBaseGUIWindow.h:179



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST