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 /**
11  * slerp_demo
12  * Execute a Spherical Linear Interpolation given 2 poses.
13  */
14 
15 #include <mrpt/math/slerp.h>
16 #include <mrpt/system/CTicTac.h>
17 #include <mrpt/img/TColor.h>
21 #include <iostream>
22 
23 using namespace std;
24 using namespace mrpt;
25 using namespace mrpt::math;
26 using namespace mrpt::gui;
27 using namespace mrpt::opengl;
28 using namespace mrpt::poses;
29 using namespace mrpt::img;
30 
31 // ------------------------------------------------------
32 // TestSLERP
33 // ------------------------------------------------------
34 void TestSLERP()
35 {
36  CDisplayWindow3D win("Example of SLERP animation", 640, 480);
37 
38  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
39 
40  win.setCameraAzimuthDeg(-50);
41  win.setCameraElevationDeg(40);
42  win.setCameraZoom(19);
43  win.setCameraPointingToPoint(2, 2, 0);
44 
45  // Modify the scene:
46  // ------------------------------------------------------
47  {
49  mrpt::make_aligned_shared<opengl::CGridPlaneXY>(
50  -20, 20, -20, 20, 0, 1);
51  obj->setColor(0.4, 0.4, 0.4);
52  theScene->insert(obj);
53  }
54 
55  // Initialize the start, end pose of the animation
56  const TPose3D pose_a(0, 0, 0, DEG2RAD(0), DEG2RAD(0), DEG2RAD(0));
57  const TPose3D pose_b(3, 4, 1, DEG2RAD(120), DEG2RAD(40), DEG2RAD(50));
58 
59  {
60  // XYZ corner at A:
63  obj->setPose(pose_a);
64  theScene->insert(obj);
65  }
66  {
67  // XYZ corner at B:
70  obj->setPose(pose_b);
71  theScene->insert(obj);
72  }
73  {
74  // SLERP animated corner:
77  obj->setName("slerp_obj");
78  obj->setPose(pose_a);
79  theScene->insert(obj);
80  }
81 
82  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
83  win.unlockAccess3DScene();
84 
85  cout << "\n Close the window to exit.\n";
86 
88  static const double MOVE_PERIOD = 1.0;
89  static const double MOVE_PERIOD2 = 2 * MOVE_PERIOD;
90 
91  while (win.isOpen())
92  {
93  // Compute the time:
94  double t = ::fmod(tic.Tac(), MOVE_PERIOD2);
95  if (t < MOVE_PERIOD)
96  t /= MOVE_PERIOD;
97  else
98  t = 1 - (t - MOVE_PERIOD) / MOVE_PERIOD;
99 
100  // SLERP & LERP interpolation:
101  TPose3D pose_interp;
102  mrpt::math::slerp(pose_a, pose_b, t, pose_interp);
103 
104  // Move the scene:
105  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
106 
107  opengl::CRenderizable::Ptr obj1 = theScene->getByName("slerp_obj");
108  obj1->setPose(pose_interp);
109 
110  // Show text:
111  win.addTextMessage(
112  5, 5, format("t=%.03f", t), TColorf(1, 1, 1), 0,
114 
115  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
116  win.unlockAccess3DScene();
117 
118  // Update window:
119  win.forceRepaint();
120  std::this_thread::sleep_for(5ms);
121  };
122 }
123 
124 // ------------------------------------------------------
125 // MAIN
126 // ------------------------------------------------------
127 int main()
128 {
129  try
130  {
131  TestSLERP();
132  return 0;
133  }
134  catch (std::exception& e)
135  {
136  std::cout << "MRPT exception caught: " << e.what() << std::endl;
137  return -1;
138  }
139  catch (...)
140  {
141  printf("Untyped exception!!");
142  return -1;
143  }
144 }
DEG2RAD
#define DEG2RAD
Definition: core/include/mrpt/core/bits_math.h:59
mrpt::opengl::CGridPlaneXY::Ptr
std::shared_ptr< CGridPlaneXY > Ptr
Definition: CGridPlaneXY.h:34
format
GLenum GLsizei GLenum format
Definition: glext.h:3531
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
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
stock_objects.h
mrpt::opengl::stock_objects::CornerXYZSimple
CSetOfObjects::Ptr CornerXYZSimple(float scale=1.0, float lineWidth=1.0)
Returns three arrows representing a X,Y,Z 3D corner (just thick lines instead of complex arrows for f...
Definition: StockObjects.cpp:419
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
CDisplayWindow3D.h
mrpt::math::slerp
void slerp(const CQuaternion< T > &q0, const CQuaternion< T > &q1, const double t, CQuaternion< T > &q)
SLERP interpolation between two quaternions.
Definition: slerp.h:34
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::img
Definition: CCanvas.h:17
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
slerp.h
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::CSetOfObjects::Ptr
std::shared_ptr< CSetOfObjects > Ptr
Definition: CSetOfObjects.h:30
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
CTicTac.h
TestSLERP
void TestSLERP()
Definition: vision_stereo_rectify/test.cpp:34
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
CGridPlaneXY.h
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
mrpt::opengl::MRPT_GLUT_BITMAP_TIMES_ROMAN_24
@ MRPT_GLUT_BITMAP_TIMES_ROMAN_24
Definition: opengl_fonts.h:29
TColor.h



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