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 
12 #include <mrpt/opengl/CSphere.h>
13 #include <mrpt/random.h>
14 #include <mrpt/system/CTicTac.h>
15 #include <mrpt/system/os.h>
16 #include <iostream>
17 
18 using namespace mrpt;
19 using namespace std;
20 using namespace mrpt::gui;
21 using namespace mrpt::random;
22 using namespace mrpt::opengl;
23 
24 const size_t N_MASSES = 750;
25 
26 const double BOX = 500;
27 const double V0 = 100;
28 const double MASS_MIN = log(40.0), MASS_MAX = log(100.0);
29 const double M2R = 2.0;
30 const double LARGEST_STEP = 0.001;
31 const double G = 300;
32 const double COLLIS_LOSS = 0.98;
33 
34 struct TMass
35 {
36  TMass() : obj3d() {}
37  double x{0}, y{0}, z{0};
38  double vx{0}, vy{0}, vz{0};
39  double mass{1};
40  double radius;
42 };
43 
44 void simulateGravity(vector<TMass>& objs, double At);
45 
46 // ------------------------------------------------------
47 // GravityDemo
48 // ------------------------------------------------------
49 void GravityDemo()
50 {
52  "MRPT example: 3D gravity simulator- JLBC 2008", 1000, 700);
53 
55 
56  win.setCameraElevationDeg(50.0f);
57  win.setCameraZoom(1000);
58 
59  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
60 
61  // Modify the scene:
62  // ------------------------------------------------------
63  {
65  opengl::CGridPlaneXY::Create(-2000, 2000, -2000, 2000, 0, 100);
66  obj->setColor(0.3, 0.3, 0.3);
67  theScene->insert(obj);
68  }
69 
70  // Create the masses:
71  // ----------------------------------------------------
72 
73  vector<TMass> masses(N_MASSES);
74 
75  // Init at random poses & create opengl objects:
76  for (size_t i = 0; i < N_MASSES; i++)
77  {
78  masses[i].x = getRandomGenerator().drawUniform(-BOX, BOX);
79  masses[i].y = getRandomGenerator().drawUniform(-BOX, BOX);
80  masses[i].z = getRandomGenerator().drawUniform(-BOX, BOX) / 10;
81 
82  double a = atan2(masses[i].y, masses[i].x);
83 
84  masses[i].vx = -V0 * sin(a) +
85  getRandomGenerator().drawUniform(-V0 * 0.01, V0 * 0.01);
86  masses[i].vy = V0 * cos(a) +
87  getRandomGenerator().drawUniform(-V0 * 0.01, V0 * 0.01);
88  masses[i].vz = 0; // getRandomGenerator().drawUniform(-V0,V0);
89 
90  masses[i].mass =
91  exp(getRandomGenerator().drawUniform(MASS_MIN, MASS_MAX));
92  opengl::CSphere::Ptr& obj = masses[i].obj3d = opengl::CSphere::Create();
93 
94  obj->setColor(
95  getRandomGenerator().drawUniform(0.1, 0.9),
96  getRandomGenerator().drawUniform(0.1, 0.9),
97  getRandomGenerator().drawUniform(0.1, 0.9));
98 
99  masses[i].radius = M2R * pow(masses[i].mass, 1.0 / 3.0);
100  obj->setRadius(masses[i].radius); // Guess why ^(1/3) ;-)
101 
102  obj->setLocation(masses[i].x, masses[i].y, masses[i].z);
103  theScene->insert(obj);
104  }
105 
106  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
107  win.unlockAccess3DScene();
108 
109  mrpt::system::CTicTac tictac;
110  tictac.Tic();
111 
112  double t0 = tictac.Tac();
113 
114  while (!mrpt::system::os::kbhit() && win.isOpen())
115  {
116  // Move the scene:
117  double t1 = tictac.Tac();
118  double At = t1 - t0;
119  t0 = t1;
120 
121  // Simulate a At, possibly in several small steps:
122  // Update the 3D scene:
123  win.get3DSceneAndLock();
124 
125  size_t n_steps = ceil(At / LARGEST_STEP) + 1;
126  double At_steps = At / n_steps;
127  n_steps = min(n_steps, size_t(3));
128  for (size_t j = 0; j < n_steps; j++) simulateGravity(masses, At_steps);
129 
130  for (size_t i = 0; i < masses.size(); i++)
131  {
132  opengl::CSphere::Ptr& obj = masses[i].obj3d;
133  obj->setLocation(masses[i].x, masses[i].y, masses[i].z);
134  }
135  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
136  win.unlockAccess3DScene();
137 
138  // Update window:
139  win.forceRepaint();
140  std::this_thread::sleep_for(1ms);
141  };
142 }
143 
144 struct TForce
145 {
146  TForce() { f[0] = f[1] = f[2] = 0; }
147  double f[3];
148 };
149 
150 void simulateGravity(vector<TMass>& objs, double At)
151 {
152  const size_t N = objs.size();
153 
154  vector<TForce> forces(N);
155 
156  // Index in the array must be larger than its content!!
157  vector<pair<size_t, double>> lstMass_i_joins_j(
158  N, pair<size_t, double>(string::npos, 10000.0));
159 
160  for (size_t i = 0; i < (N - 1); i++)
161  {
162  const double Ri = objs[i].radius;
163 
164  // Compute overall gravity force:
165  for (size_t j = i + 1; j < N; j++)
166  {
167  double Ax = objs[j].x - objs[i].x;
168  double Ay = objs[j].y - objs[i].y;
169  double Az = objs[j].z - objs[i].z;
170  double D2 = square(Ax) + square(Ay) + square(Az);
171 
172  double D = sqrt(D2);
173  if (D == 0) continue;
174 
175  const double Rj = objs[j].radius;
176 
177  if (D < (Ri + Rj)) // Collission!!
178  {
179  // Index in the array must be larger than its content!!
180  if (D < lstMass_i_joins_j[j].second)
181  {
182  lstMass_i_joins_j[j].first = i;
183  lstMass_i_joins_j[j].second = D;
184  }
185  }
186  else
187  {
188  double K =
189  G * objs[i].mass * objs[j].mass / square(max(D, 1.0));
190  double D_1 = 1.0 / D;
191  Ax *= D_1;
192  Ay *= D_1;
193  Az *= D_1;
194 
195  const double fx = Ax * K;
196  const double fy = Ay * K;
197  const double fz = Az * K;
198 
199  forces[i].f[0] += fx;
200  forces[i].f[1] += fy;
201  forces[i].f[2] += fz;
202 
203  forces[j].f[0] -= fx;
204  forces[j].f[1] -= fy;
205  forces[j].f[2] -= fz;
206  }
207  }
208  }
209 
210  // F = m a
211  for (size_t i = 0; i < N; i++)
212  {
213  const double M_1 = 1.0 / objs[i].mass;
214 
215  forces[i].f[0] *= M_1;
216  forces[i].f[1] *= M_1;
217  forces[i].f[2] *= M_1;
218 
219  objs[i].vx += forces[i].f[0] * At;
220  objs[i].vy += forces[i].f[1] * At;
221  objs[i].vz += forces[i].f[2] * At;
222 
223  objs[i].x += objs[i].vx * At;
224  objs[i].y += objs[i].vy * At;
225  objs[i].z += objs[i].vz * At;
226  }
227 
228  // return;
229  // Join masses that collided:
230  for (int i = N - 1; i >= 0; i--)
231  {
232  const size_t newObj = lstMass_i_joins_j[i].first;
233  if (newObj == string::npos) continue;
234 
235  const double Mi = objs[i].mass;
236  const double Mj = objs[newObj].mass;
237  const double newMass = Mi + Mj;
238  const double newMass_1 = 1.0 / newMass;
239 
240  objs[newObj].vx =
241  COLLIS_LOSS * newMass_1 * (Mj * objs[newObj].vx + Mi * objs[i].vx);
242  objs[newObj].vy =
243  COLLIS_LOSS * newMass_1 * (Mj * objs[newObj].vy + Mi * objs[i].vy);
244  objs[newObj].vz =
245  COLLIS_LOSS * newMass_1 * (Mj * objs[newObj].vz + Mi * objs[i].vz);
246 
247  objs[newObj].x = newMass_1 * (Mj * objs[newObj].x + Mi * objs[i].x);
248  objs[newObj].y = newMass_1 * (Mj * objs[newObj].y + Mi * objs[i].y);
249  objs[newObj].z = newMass_1 * (Mj * objs[newObj].z + Mi * objs[i].z);
250 
251  objs[newObj].mass = newMass;
252  objs[newObj].radius = M2R * pow(newMass, 1.0 / 3.0);
253  objs[newObj].obj3d->setRadius(objs[newObj].radius);
254 
255  objs[i].obj3d->setVisibility(false); // Hide sphere
256  objs.erase(objs.begin() + i);
257  }
258 }
259 
260 // ------------------------------------------------------
261 // MAIN
262 // ------------------------------------------------------
263 int main()
264 {
265  try
266  {
267  GravityDemo();
268  return 0;
269  }
270  catch (const std::exception& e)
271  {
272  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
273  return -1;
274  }
275  catch (...)
276  {
277  printf("Untyped exception!!");
278  return -1;
279  }
280 }
A namespace of pseudo-random numbers generators of diferent distributions.
const double MASS_MAX
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
const double LARGEST_STEP
GLdouble GLdouble z
Definition: glext.h:3879
#define min(a, b)
void GravityDemo()
const double G
const double V0
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
A high-performance stopwatch, with typical resolution of nanoseconds.
STL namespace.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
T square(const T x)
Inline function for the square of a number.
mrpt::gui::CDisplayWindow3D::Ptr win
const size_t N_MASSES
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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
static Ptr Create(Args &&... args)
Definition: CSphere.h:30
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
void simulateGravity(vector< TMass > &objs, double At)
GLenum GLint GLint y
Definition: glext.h:3542
const double COLLIS_LOSS
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
GLenum GLint x
Definition: glext.h:3542
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:75
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
GLubyte GLubyte GLubyte a
Definition: glext.h:6372
static Ptr Create(Args &&... args)
Definition: CGridPlaneXY.h:31
const double M2R
const double MASS_MIN
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
const double BOX



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