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/opengl/CAxis.h>
12 #include <mrpt/opengl/CBox.h>
13 #include <mrpt/opengl/CSphere.h>
15 #include <mrpt/system/os.h>
16 #include <iostream>
17 
18 using namespace std;
19 using namespace mrpt;
20 using namespace mrpt::gui;
21 using namespace mrpt::opengl;
22 
23 // ------------------------------------------------------
24 // TestDisplay3D
25 // ------------------------------------------------------
26 void TestDisplay3D()
27 {
28  CDisplayWindow3D win("Example of 3D Scene Visualization - MRPT", 640, 480);
29 
30  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
31 
32  // Add a clone viewport:
33  if (1)
34  {
35  COpenGLViewport::Ptr vi = theScene->createViewport("myClone");
36  vi->setViewportPosition(0.7, 0.05, 0.28, 0.28);
37  vi->setCloneView("main");
38  vi->setTransparent(true);
39  vi->getCamera().setAzimuthDegrees(45);
40  vi->getCamera().setElevationDegrees(45);
41  vi->getCamera().setZoomDistance(10);
42  }
43 
44  // Modify the scene:
45  // ------------------------------------------------------
46  {
48  mrpt::make_aligned_shared<opengl::CGridPlaneXY>(
49  -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 = mrpt::make_aligned_shared<opengl::CAxis>();
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 = mrpt::make_aligned_shared<opengl::CBox>();
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 = mrpt::make_aligned_shared<opengl::CSphere>();
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 = mrpt::make_aligned_shared<opengl::CSphere>();
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 = mrpt::make_aligned_shared<opengl::CSphere>();
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, format(
135  "ball#1 pos: %.02f %.02f %.02f ", obj1->getPoseX(),
136  obj1->getPoseY(), obj1->getPoseZ()),
137  mrpt::img::TColorf(0, 0, 1),
138  10, // An arbitrary ID to always overwrite the same, previous 2D
139  // text message
141 
142  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
143  win.unlockAccess3DScene();
144 
145  // Update window:
146  win.forceRepaint();
147  std::this_thread::sleep_for(10ms);
148 
149  // Grab frame:
150  mrpt::img::CImage::Ptr img = win.getLastWindowImagePtr();
151  if (img)
152  {
153  static int i = 0;
154  const string s = format("GRAB_%06i.png", ++i);
155  img->saveToFile(s);
156  printf("Saved frame image to: %s \r", s.c_str()); // "\ r" is to
157  // overwrite the
158  // same line over
159  // and over
160  // again..
161  }
162 
163  if (mrpt::system::os::kbhit()) end = true;
164  if (win.keyHit())
165  {
166  mrptKeyModifier kmods;
167  int key = win.getPushedKey(&kmods);
168  printf(
169  "Key pushed: %c (%i) - modifiers: 0x%04X\n", char(key), key,
170  kmods);
171 
172  if (key == MRPTK_ESCAPE) end = true;
173 
174  if (key == MRPTK_RIGHT)
175  win.setCameraAzimuthDeg(win.getCameraAzimuthDeg() + 5);
176  if (key == MRPTK_LEFT)
177  win.setCameraAzimuthDeg(win.getCameraAzimuthDeg() - 5);
178  }
179  };
180 }
181 
182 // ------------------------------------------------------
183 // MAIN
184 // ------------------------------------------------------
185 int main()
186 {
187  try
188  {
189  TestDisplay3D();
190 
191  return 0;
192  }
193  catch (std::exception& e)
194  {
195  std::cout << "MRPT exception caught: " << e.what() << std::endl;
196  return -1;
197  }
198  catch (...)
199  {
200  printf("Untyped exception!!");
201  return -1;
202  }
203 }
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
s
GLdouble s
Definition: glext.h:3676
CSphere.h
mrpt::opengl::CSphere::Ptr
std::shared_ptr< CSphere > Ptr
Definition: CSphere.h:33
end
GLuint GLuint end
Definition: glext.h:3528
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
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
mrpt::opengl::MRPT_GLUT_BITMAP_HELVETICA_12
@ MRPT_GLUT_BITMAP_HELVETICA_12
Definition: opengl_fonts.h:31
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
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
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::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
CBox.h
CAxis.h
mrpt::img::CImage::Ptr
std::shared_ptr< CImage > Ptr
Definition: img/CImage.h:132
img
GLint GLvoid * img
Definition: glext.h:3763
mrpt::gui::MRPTK_ESCAPE
@ MRPTK_ESCAPE
Definition: keycodes.h:30
mrpt::gui::mrptKeyModifier
mrptKeyModifier
Definition: keycodes.h:159
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



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