MRPT  1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
11 #include <mrpt/img/TColor.h>
12 #include <mrpt/math/TLine3D.h>
13 #include <mrpt/math/TObject3D.h>
14 #include <mrpt/math/geometry.h>
15 #include <mrpt/opengl/CAxis.h>
16 #include <mrpt/opengl/CBox.h>
18 #include <mrpt/opengl/CSphere.h>
19 #include <mrpt/opengl/CText.h>
20 #include <mrpt/system/CTicTac.h>
21 #include <mrpt/system/os.h>
22 #include <iostream>
23 
24 using namespace std;
25 using namespace mrpt;
26 using namespace mrpt::gui;
27 using namespace mrpt::math;
28 using namespace mrpt::opengl;
29 using namespace mrpt::img;
30 using namespace mrpt::system;
31 // ------------------------------------------------------
32 // TestDisplay3D
33 // ------------------------------------------------------
34 void TestDisplay3D()
35 {
36  CDisplayWindow3D win("Example of 3D Scene Visualization - MRPT", 640, 480);
37 
38  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
39 
40  // Add a clone viewport, using [0,1] factor X,Y,Width,Height coordinates:
41  {
42  COpenGLViewport::Ptr vi = theScene->createViewport("myClone");
43  vi->setViewportPosition(0.7, 0.05, 0.28, 0.28);
44  vi->setCloneView("main");
45  vi->setTransparent(true);
46  vi->getCamera().setAzimuthDegrees(45);
47  vi->getCamera().setElevationDegrees(45);
48  vi->getCamera().setZoomDistance(10);
49  }
50 
51  // Another clone viewport, using absolute coordinates
52  {
53  COpenGLViewport::Ptr vi = theScene->createViewport("myClone2");
54  vi->setViewportPosition(
55  /*x px*/ -250, /*y px*/ -250, /*width px*/ 250,
56  /*height px*/ 200); // x,y negative means pixels from the
57  // top/right, instead of from the bottom/left.
58  vi->setCloneView("main");
59  vi->setTransparent(false);
60  vi->getCamera().setAzimuthDegrees(-95);
61  vi->getCamera().setElevationDegrees(30);
62  vi->getCamera().setZoomDistance(8);
63  }
64 
65  // And another transparent viewport just to show 3D text:
66  if (false)
67  {
69  COpenGLViewport::Ptr vi = theScene->createViewport("flat_viewport");
70  vi->setViewportPosition(0, 0, 0.3, 0.3);
71  vi->setTransparent(true);
72  vi->setBorderSize(0);
73  vi->getCamera().setAzimuthDegrees(0);
74  vi->getCamera().setElevationDegrees(90);
75  vi->getCamera().setZoomDistance(5);
76  vi->getCamera().setOrthogonal(true);
77 
78  vi->insert(txt1);
79  }
80 
81  // Modify the scene:
82  // ------------------------------------------------------
83  {
85  opengl::CGridPlaneXY::Create(-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 = opengl::CAxis::Create();
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 = opengl::CBox::Create();
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 = opengl::CSphere::Create();
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 = opengl::CSphere::Create();
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 = opengl::CSphere::Create();
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 (const std::exception& e)
285  {
286  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
287  return -1;
288  }
289  catch (...)
290  {
291  printf("Untyped exception!!");
292  return -1;
293  }
294 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
GLdouble GLdouble t
Definition: glext.h:3695
double x
X,Y,Z coordinates.
Definition: TPoint3D.h:83
static Ptr Create(Args &&... args)
Definition: CText.h:37
A high-performance stopwatch, with typical resolution of nanoseconds.
mrptKeyModifier
Definition: keycodes.h:156
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:86
Standard object for storing any 3D lightweight object.
Definition: TObject3D.h:25
STL namespace.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
This base provides a set of functions for maths stuff.
GLuint GLuint end
Definition: glext.h:3532
3D Plane, represented by its equation
Definition: TPlane.h:22
mrpt::gui::CDisplayWindow3D::Ptr win
bool getPoint(TPoint3D &p) const
Gets the content as a point, returning false if the type is not adequate.
Definition: TObject3D.h:120
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A RGB color - floats in the range [0,1].
Definition: TColor.h:77
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: TPose3D.h:23
renders glyphs filled with antialiased outlines
Definition: opengl_fonts.h:37
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:394
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
GLenum GLsizei GLenum format
Definition: glext.h:3535
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:75
Lightweight 3D point.
Definition: TPoint3D.h:90
std::string dateTimeLocalToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (in local time): YEAR/MONTH/DAY,HH:MM:SS.MMM.
Definition: datetime.cpp:170
renders glyphs as filled polygons
Definition: opengl_fonts.h:35
bool intersect(const TSegment3D &s1, const TSegment3D &s2, TObject3D &obj)
Gets the intersection between two 3D segments.
Definition: geometry.cpp:638
void TestDisplay3D()
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
3D line, represented by a base point and a director vector.
Definition: TLine3D.h:19



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019