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 
11 #include <mrpt/system/CTicTac.h>
12 #include <mrpt/math/geometry.h>
13 #include <mrpt/system/os.h>
14 #include <mrpt/opengl/CText.h>
16 #include <mrpt/opengl/CAxis.h>
17 #include <mrpt/opengl/CBox.h>
18 #include <mrpt/opengl/CSphere.h>
19 #include <mrpt/img/TColor.h>
20 #include <iostream>
21 
22 using namespace std;
23 using namespace mrpt;
24 using namespace mrpt::gui;
25 using namespace mrpt::math;
26 using namespace mrpt::opengl;
27 using namespace mrpt::img;
28 using namespace mrpt::system;
29 // ------------------------------------------------------
30 // TestDisplay3D
31 // ------------------------------------------------------
32 void TestDisplay3D()
33 {
34  CDisplayWindow3D win("Example of 3D Scene Visualization - MRPT", 640, 480);
35 
36  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
37 
38  // Add a clone viewport, using [0,1] factor X,Y,Width,Height coordinates:
39  {
40  COpenGLViewport::Ptr vi = theScene->createViewport("myClone");
41  vi->setViewportPosition(0.7, 0.05, 0.28, 0.28);
42  vi->setCloneView("main");
43  vi->setTransparent(true);
44  vi->getCamera().setAzimuthDegrees(45);
45  vi->getCamera().setElevationDegrees(45);
46  vi->getCamera().setZoomDistance(10);
47  }
48 
49  // Another clone viewport, using absolute coordinates
50  {
51  COpenGLViewport::Ptr vi = theScene->createViewport("myClone2");
52  vi->setViewportPosition(
53  /*x px*/ -250, /*y px*/ -250, /*width px*/ 250,
54  /*height px*/ 200); // x,y negative means pixels from the
55  // top/right, instead of from the bottom/left.
56  vi->setCloneView("main");
57  vi->setTransparent(false);
58  vi->getCamera().setAzimuthDegrees(-95);
59  vi->getCamera().setElevationDegrees(30);
60  vi->getCamera().setZoomDistance(8);
61  }
62 
63  // And another transparent viewport just to show 3D text:
64  if (0)
65  {
67  mrpt::make_aligned_shared<mrpt::opengl::CText>();
68  COpenGLViewport::Ptr vi = theScene->createViewport("flat_viewport");
69  vi->setViewportPosition(0, 0, 0.3, 0.3);
70  vi->setTransparent(true);
71  vi->setBorderSize(0);
72  vi->getCamera().setAzimuthDegrees(0);
73  vi->getCamera().setElevationDegrees(90);
74  vi->getCamera().setZoomDistance(5);
75  vi->getCamera().setOrthogonal(true);
76 
77  vi->insert(txt1);
78  }
79 
80  // Modify the scene:
81  // ------------------------------------------------------
82  {
84  mrpt::make_aligned_shared<opengl::CGridPlaneXY>(
85  -20, 20, -20, 20, 0, 1);
86  obj->setColor(0.8, 0.8, 0.8);
87  theScene->insert(obj);
88  }
89 
90  {
91  opengl::CAxis::Ptr obj = mrpt::make_aligned_shared<opengl::CAxis>();
92  obj->setFrequency(5);
93  obj->enableTickMarks();
94  obj->setAxisLimits(-10, -10, -10, 10, 10, 10);
95  theScene->insert(obj);
96  }
97 
98  {
99  opengl::CBox::Ptr obj = mrpt::make_aligned_shared<opengl::CBox>();
100  obj->setWireframe(false);
101  obj->setColor(1, 0, 0);
102  obj->setLineWidth(3.0);
103  obj->setPose(TPose3D(10, 0, 0, 0.2, 0.3, 0.1));
104  theScene->insert(obj);
105  }
106 
107  {
108  opengl::CSphere::Ptr obj = mrpt::make_aligned_shared<opengl::CSphere>();
109  obj->setColor(0, 0, 1);
110  obj->setRadius(0.3);
111  obj->setLocation(0, 0, 1);
112  obj->setName("ball_1");
113  theScene->insert(obj);
114  }
115  {
116  opengl::CSphere::Ptr obj = mrpt::make_aligned_shared<opengl::CSphere>();
117  obj->setColor(1, 0, 0);
118  obj->setRadius(0.3);
119  obj->setLocation(-1, -1, 1);
120  obj->setName("ball_2");
121  theScene->insert(obj);
122  }
123 
124  {
125  opengl::CSphere::Ptr obj = mrpt::make_aligned_shared<opengl::CSphere>();
126  obj->setColor(0, 1, 0);
127  obj->setRadius(0.5);
128  obj->setLocation(0, 0, 0);
129  obj->setName("USER_MOUSE_PICK");
130  theScene->insert(obj);
131  }
132 
133  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
134  win.unlockAccess3DScene();
135 
136  // Texts:
137  win.addTextMessage(
138  0.01, 0.85, "This is a 2D message", TColorf(1, 1, 1), "sans", 11,
139  mrpt::opengl::NICE, 0);
140 
141  win.setCameraElevationDeg(25.0f);
142  // win.setCameraProjective(false);
143 
144  cout << endl;
145  cout << "Control with mouse or keyboard. Valid keys:" << endl;
146  cout << " ESC -> Exit" << endl;
147  cout << " Left/right cursor arrow -> Camera azimuth" << endl;
148  cout << " P -> Enable / disable 'place object' "
149  "mode"
150  << endl;
151  cout << endl;
152 
153  bool end = false;
154  bool placeMode = false;
155 
156  CTicTac timer;
157  timer.Tic();
158 
159  while (!end && win.isOpen())
160  {
161  const double t = timer.Tac();
162 
163  // Move the scene:
164  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
165 
166  const double R1 = 8;
167  const double W1 = 5.0, Q1 = 3.3;
168  opengl::CRenderizable::Ptr obj1 = theScene->getByName("ball_1");
169  obj1->setLocation(
170  R1 * cos(W1 * t) * sin(Q1 * t), R1 * sin(W1 * t),
171  R1 * cos(W1 * t) * cos(Q1 * t));
172 
173  const double R2 = 6;
174  const double W2 = 1.3, Q2 = 7.2;
175  opengl::CRenderizable::Ptr obj2 = theScene->getByName("ball_2");
176  obj2->setLocation(
177  R2 * cos(W2 * t) * sin(Q2 * t), R2 * sin(W2 * t),
178  R2 * cos(W2 * t) * cos(Q2 * t));
179 
180  win.addTextMessage(
181  0.01, 0.85, "This is a 2D message", TColorf(1, 0, 0), "sans", 8,
182  mrpt::opengl::NICE, 0);
183 
184  win.addTextMessage(
185  0.02, 0.02, // X,Y<=1 means coordinates are factors over the entire
186  // viewport area.
187  format(
188  "ball#1 pos: %.02f %.02f %.02f ", obj1->getPoseX(),
189  obj1->getPoseY(), obj1->getPoseZ()),
190  TColorf(.8, .8, .8), "sans", 14, // font name & size
192  10 // An arbitrary ID to always overwrite the same, previous 2D
193  // text message
194  );
195 
196  win.addTextMessage(
197  5, -15, // |X|,|Y|>1 means absolute coordinates, negative means
198  // from the top instead of the bottom.
199  format(
200  "Time: %s",
202  .c_str()),
203  TColorf(1, 1, 1), "mono", 9, // font name & size
205  20 // An arbitrary ID to always overwrite the same, previous 2D
206  // text message
207  );
208 
209  // Show management of (x,y) mouse coordinates and 3D rays:
210  // ------------------------------------------------------------
211  int mouse_x, mouse_y;
212  if (placeMode &&
213  win.getLastMousePosition(
214  mouse_x, mouse_y)) // See also: getLastMousePositionRay()
215  {
216  // Get the ray in 3D for the latest mouse (X,Y):
218  theScene->getViewport("main")->get3DRayForPixelCoord(
219  mouse_x, mouse_y, ray);
220 
221  // Create a 3D plane, e.g. Z=0
222  const mrpt::math::TPlane ground_plane(
223  TPoint3D(0, 0, 0), TPoint3D(1, 0, 0), TPoint3D(0, 1, 0));
224 
225  // Intersection of the line with the plane:
226  mrpt::math::TObject3D inters;
227  mrpt::math::intersect(ray, ground_plane, inters);
228 
229  // Interpret the intersection as a point, if there is an
230  // intersection:
231  mrpt::math::TPoint3D inters_pt;
232  if (inters.getPoint(inters_pt))
233  {
234  // Move an object to the position picked by the user:
235  // printf("PT: %f %f %f\n",);
236  theScene->getByName("USER_MOUSE_PICK")
237  ->setLocation(inters_pt.x, inters_pt.y, inters_pt.z);
238  }
239  }
240 
241  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
242  win.unlockAccess3DScene();
243 
244  // Update window:
245  win.forceRepaint();
246  std::this_thread::sleep_for(20ms);
247 
248  if (mrpt::system::os::kbhit()) end = true;
249  if (win.keyHit())
250  {
251  mrptKeyModifier kmods;
252  int key = win.getPushedKey(&kmods);
253  printf(
254  "Key pushed: %c (%i) - modifiers: 0x%04X\n", char(key), key,
255  kmods);
256 
257  if (key == MRPTK_ESCAPE) end = true;
258 
259  if (key == MRPTK_RIGHT)
260  win.setCameraAzimuthDeg(win.getCameraAzimuthDeg() + 5);
261  if (key == MRPTK_LEFT)
262  win.setCameraAzimuthDeg(win.getCameraAzimuthDeg() - 5);
263 
264  if (key == 'p' || key == 'P')
265  {
266  placeMode = !placeMode;
267  win.setCursorCross(placeMode);
268  }
269  }
270  };
271 }
272 
273 // ------------------------------------------------------
274 // MAIN
275 // ------------------------------------------------------
276 int main()
277 {
278  try
279  {
280  TestDisplay3D();
281 
282  return 0;
283  }
284  catch (std::exception& e)
285  {
286  std::cout << "MRPT exception caught: " << e.what() << std::endl;
287  return -1;
288  }
289  catch (...)
290  {
291  printf("Untyped exception!!");
292  return -1;
293  }
294 }
mrpt::system::os::kbhit
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:390
os.h
mrpt::opengl::CGridPlaneXY::Ptr
std::shared_ptr< CGridPlaneXY > Ptr
Definition: CGridPlaneXY.h:34
format
GLenum GLsizei GLenum format
Definition: glext.h:3531
mrpt::system::dateTimeLocalToString
std::string dateTimeLocalToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (in local time): YEAR/MONTH/DAY,HH:MM:SS....
Definition: datetime.cpp:270
geometry.h
t
GLdouble GLdouble t
Definition: glext.h:3689
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
CSphere.h
mrpt::opengl::CSphere::Ptr
std::shared_ptr< CSphere > Ptr
Definition: CSphere.h:33
mrpt::opengl::NICE
@ NICE
renders glyphs filled with antialiased outlines
Definition: opengl_fonts.h:40
end
GLuint GLuint end
Definition: glext.h:3528
mrpt::math::TPoint3D::z
double z
Definition: lightweight_geom_data.h:385
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
mrpt::system::now
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:75
mrpt::math::TObject3D
Standard object for storing any 3D lightweight object.
Definition: lightweight_geom_data.h:1809
mrpt::opengl::CRenderizable::Ptr
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
CDisplayWindow3D.h
mrpt::gui::MRPTK_RIGHT
@ MRPTK_RIGHT
Definition: keycodes.h:50
mrpt::opengl::CBox::Ptr
std::shared_ptr< CBox > Ptr
Definition: CBox.h:44
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::math::TObject3D::getPoint
bool getPoint(TPoint3D &p) const
Gets the content as a point, returning false if the type is not adequate.
Definition: lightweight_geom_data.h:1904
mrpt::img
Definition: CCanvas.h:17
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::math::TPoint3D::x
double x
X,Y,Z coordinates.
Definition: lightweight_geom_data.h:385
mrpt::img::TColorf
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
mrpt::math::TPose3D
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: lightweight_geom_data.h:603
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::opengl::CAxis::Ptr
std::shared_ptr< CAxis > Ptr
Definition: CAxis.h:33
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::math::intersect
bool intersect(const TSegment3D &s1, const TSegment3D &s2, TObject3D &obj)
Gets the intersection between two 3D segments.
Definition: geometry.cpp:631
mrpt::opengl::FILL
@ FILL
renders glyphs as filled polygons
Definition: opengl_fonts.h:38
CBox.h
mrpt::math::TPoint3D
Lightweight 3D point.
Definition: lightweight_geom_data.h:378
mrpt::math::TPlane
3D Plane, represented by its equation
Definition: lightweight_geom_data.h:1309
mrpt::math::TPoint3D::y
double y
Definition: lightweight_geom_data.h:385
CAxis.h
CTicTac.h
mrpt::gui::MRPTK_ESCAPE
@ MRPTK_ESCAPE
Definition: keycodes.h:30
mrpt::gui::mrptKeyModifier
mrptKeyModifier
Definition: keycodes.h:159
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
mrpt::math::TLine3D
3D line, represented by a base point and a director vector.
Definition: lightweight_geom_data.h:1244
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
TestDisplay3D
void TestDisplay3D()
Definition: vision_stereo_rectify/test.cpp:26
mrpt::gui::MRPTK_LEFT
@ MRPTK_LEFT
Definition: keycodes.h:48
CGridPlaneXY.h
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::opengl::COpenGLViewport::Ptr
std::shared_ptr< COpenGLViewport > Ptr
Definition: COpenGLViewport.h:63
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
TColor.h
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
CText.h
mrpt::opengl::CText::Ptr
std::shared_ptr< CText > Ptr
Definition: CText.h:40



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