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/math/TPose3D.h>
12 #include <mrpt/opengl/CAxis.h>
13 #include <mrpt/opengl/CBox.h>
15 #include <mrpt/opengl/CSphere.h>
16 #include <mrpt/system/os.h>
17 #include <iostream>
18 
19 using namespace std;
20 using namespace mrpt;
21 using namespace mrpt::gui;
22 using namespace mrpt::opengl;
23 
24 // ------------------------------------------------------
25 // TestDisplay3D
26 // ------------------------------------------------------
27 void TestDisplay3D()
28 {
29  CDisplayWindow3D win("Example of 3D Scene Visualization - MRPT", 640, 480);
30 
31  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
32 
33  // Add a clone viewport:
34  if (true)
35  {
36  COpenGLViewport::Ptr vi = theScene->createViewport("myClone");
37  vi->setViewportPosition(0.7, 0.05, 0.28, 0.28);
38  vi->setCloneView("main");
39  vi->setTransparent(true);
40  vi->getCamera().setAzimuthDegrees(45);
41  vi->getCamera().setElevationDegrees(45);
42  vi->getCamera().setZoomDistance(10);
43  }
44 
45  // Modify the scene:
46  // ------------------------------------------------------
47  {
49  opengl::CGridPlaneXY::Create(-20, 20, -20, 20, 0, 1);
50  obj->setColor(0.4, 0.4, 0.4);
51  theScene->insert(obj);
52  }
53 
54  {
55  opengl::CAxis::Ptr obj = opengl::CAxis::Create();
56  obj->setFrequency(5);
57  obj->enableTickMarks();
58  obj->setAxisLimits(-10, -10, -10, 10, 10, 10);
59  theScene->insert(obj);
60  }
61 
62  {
63  opengl::CBox::Ptr obj = opengl::CBox::Create();
64  obj->setWireframe(false);
65  obj->setColor(1, 0, 0);
66  obj->setLineWidth(3.0);
67  obj->setPose(mrpt::math::TPose3D(10, 0, 0, 0.2, 0.3, 0.1));
68  theScene->insert(obj);
69  }
70 
71  {
72  opengl::CSphere::Ptr obj = opengl::CSphere::Create();
73  obj->setColor(0, 0, 1);
74  obj->setRadius(0.3);
75  obj->setLocation(0, 0, 1);
76  obj->setName("ball_1");
77  theScene->insert(obj);
78  }
79  {
80  opengl::CSphere::Ptr obj = opengl::CSphere::Create();
81  obj->setColor(1, 0, 0);
82  obj->setRadius(0.3);
83  obj->setLocation(-1, -1, 1);
84  obj->setName("ball_2");
85  theScene->insert(obj);
86  }
87 
88  {
89  opengl::CSphere::Ptr obj = opengl::CSphere::Create();
90  obj->setColor(0, 1, 0);
91  obj->setRadius(0.5);
92  obj->setLocation(0, 0, 0);
93  obj->setName("USER_MOUSE_PICK");
94  theScene->insert(obj);
95  }
96 
97  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
98  win.unlockAccess3DScene();
99 
100  win.captureImagesStart();
101 
102  // Texts:
103  win.addTextMessage(0.05, 0.05, "This is a 2D message");
104 
105  win.setCameraElevationDeg(25.0f);
106  // win.setCameraProjective(false);
107 
108  cout << endl;
109  cout << "Control with mouse or keyboard. Valid keys:" << endl;
110  cout << " ESC -> Exit" << endl;
111  cout << " Left/right cursor arrow -> Camera azimuth" << endl;
112  cout << endl;
113 
114  bool end = false;
115 
116  while (!end && win.isOpen())
117  {
118  // Move the scene:
119  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
120 
121  opengl::CRenderizable::Ptr obj1 = theScene->getByName("ball_1");
122  obj1->setLocation(
123  obj1->getPoseX() + cos(obj1->getPoseY() / 2) * 0.05,
124  obj1->getPoseY() - sin(obj1->getPoseX() / 2) * 0.09,
125  obj1->getPoseZ() - sin(obj1->getPoseX() / 2) * 0.08);
126 
127  obj1 = theScene->getByName("ball_2");
128  obj1->setLocation(
129  obj1->getPoseX() + cos(obj1->getPoseY() / 2) * 0.05,
130  obj1->getPoseY() - sin(obj1->getPoseX() / 2) * 0.09,
131  obj1->getPoseZ() - sin(obj1->getPoseX() / 2) * 0.08);
132 
133  win.addTextMessage(
134  0.02, 0.98,
135  format(
136  "ball#1 pos: %.02f %.02f %.02f ", obj1->getPoseX(),
137  obj1->getPoseY(), obj1->getPoseZ()),
138  mrpt::img::TColorf(0, 0, 1),
139  10, // An arbitrary ID to always overwrite the same, previous 2D
140  // text message
142 
143  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
144  win.unlockAccess3DScene();
145 
146  // Update window:
147  win.forceRepaint();
148  std::this_thread::sleep_for(10ms);
149 
150  // Grab frame:
151  mrpt::img::CImage::Ptr img = win.getLastWindowImagePtr();
152  if (img)
153  {
154  static int i = 0;
155  const string s = format("GRAB_%06i.png", ++i);
156  img->saveToFile(s);
157  printf("Saved frame image to: %s \r", s.c_str()); // "\ r" is to
158  // overwrite the
159  // same line over
160  // and over
161  // again..
162  }
163 
164  if (mrpt::system::os::kbhit()) end = true;
165  if (win.keyHit())
166  {
167  mrptKeyModifier kmods;
168  int key = win.getPushedKey(&kmods);
169  printf(
170  "Key pushed: %c (%i) - modifiers: 0x%04X\n", char(key), key,
171  kmods);
172 
173  if (key == MRPTK_ESCAPE) end = true;
174 
175  if (key == MRPTK_RIGHT)
176  win.setCameraAzimuthDeg(win.getCameraAzimuthDeg() + 5);
177  if (key == MRPTK_LEFT)
178  win.setCameraAzimuthDeg(win.getCameraAzimuthDeg() - 5);
179  }
180  };
181 }
182 
183 // ------------------------------------------------------
184 // MAIN
185 // ------------------------------------------------------
186 int main()
187 {
188  try
189  {
190  TestDisplay3D();
191 
192  return 0;
193  }
194  catch (const std::exception& e)
195  {
196  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
197  return -1;
198  }
199  catch (...)
200  {
201  printf("Untyped exception!!");
202  return -1;
203  }
204 }
mrptKeyModifier
Definition: keycodes.h:156
STL namespace.
GLdouble s
Definition: glext.h:3682
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
std::shared_ptr< CImage > Ptr
Definition: img/CImage.h:149
GLint GLvoid * img
Definition: glext.h:3769
GLuint GLuint end
Definition: glext.h:3532
mrpt::gui::CDisplayWindow3D::Ptr win
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
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 TestDisplay3D()
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.



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