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 
10 /**
11  * slerp_demo
12  * Execute a Spherical Linear Interpolation given 2 poses.
13  */
14 
16 #include <mrpt/img/TColor.h>
17 #include <mrpt/math/slerp.h>
20 #include <mrpt/system/CTicTac.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  {
48  auto obj = opengl::CGridPlaneXY::Create(-20, 20, -20, 20, 0, 1);
49  obj->setColor(0.4, 0.4, 0.4);
50  theScene->insert(obj);
51  }
52 
53  // Initialize the start, end pose of the animation
54  const TPose3D pose_a(0, 0, 0, DEG2RAD(0), DEG2RAD(0), DEG2RAD(0));
55  const TPose3D pose_b(3, 4, 1, DEG2RAD(120), DEG2RAD(40), DEG2RAD(50));
56 
57  {
58  // XYZ corner at A:
61  obj->setPose(pose_a);
62  theScene->insert(obj);
63  }
64  {
65  // XYZ corner at B:
68  obj->setPose(pose_b);
69  theScene->insert(obj);
70  }
71  {
72  // SLERP animated corner:
75  obj->setName("slerp_obj");
76  obj->setPose(pose_a);
77  theScene->insert(obj);
78  }
79 
80  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
81  win.unlockAccess3DScene();
82 
83  cout << "\n Close the window to exit.\n";
84 
86  static const double MOVE_PERIOD = 1.0;
87  static const double MOVE_PERIOD2 = 2 * MOVE_PERIOD;
88 
89  while (win.isOpen())
90  {
91  // Compute the time:
92  double t = ::fmod(tic.Tac(), MOVE_PERIOD2);
93  if (t < MOVE_PERIOD)
94  t /= MOVE_PERIOD;
95  else
96  t = 1 - (t - MOVE_PERIOD) / MOVE_PERIOD;
97 
98  // SLERP & LERP interpolation:
99  TPose3D pose_interp;
100  mrpt::math::slerp(pose_a, pose_b, t, pose_interp);
101 
102  // Move the scene:
103  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
104 
105  opengl::CRenderizable::Ptr obj1 = theScene->getByName("slerp_obj");
106  obj1->setPose(pose_interp);
107 
108  // Show text:
109  win.addTextMessage(
110  5, 5, format("t=%.03f", t), TColorf(1, 1, 1), 0,
112 
113  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
114  win.unlockAccess3DScene();
115 
116  // Update window:
117  win.forceRepaint();
118  std::this_thread::sleep_for(5ms);
119  };
120 }
121 
122 // ------------------------------------------------------
123 // MAIN
124 // ------------------------------------------------------
125 int main()
126 {
127  try
128  {
129  TestSLERP();
130  return 0;
131  }
132  catch (const std::exception& e)
133  {
134  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
135  return -1;
136  }
137  catch (...)
138  {
139  printf("Untyped exception!!");
140  return -1;
141  }
142 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
GLdouble GLdouble t
Definition: glext.h:3695
void slerp(const CQuaternion< T > &q0, const CQuaternion< T > &q1, const double t, CQuaternion< T > &q)
SLERP interpolation between two quaternions.
Definition: slerp.h:32
A high-performance stopwatch, with typical resolution of nanoseconds.
STL namespace.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
This base provides a set of functions for maths stuff.
mrpt::gui::CDisplayWindow3D::Ptr win
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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...
void TestSLERP()
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
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
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