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 #include <mrpt/random.h>
11 #include <mrpt/math/kmeans.h>
13 #include <mrpt/system/CTicTac.h>
16 #include <iostream>
17 
18 using namespace mrpt::math;
19 using namespace mrpt::gui;
20 using namespace mrpt::random;
21 using namespace mrpt::system;
22 using namespace std;
23 
24 // ------------------------------------------------------
25 // TestKMeans
26 // ------------------------------------------------------
27 void TestKMeans()
28 {
29  typedef CArrayDouble<2> CPointType;
30  // typedef CArrayFloat<2> CPointType;
31 
33  CTicTac tictac;
34 
35  CDisplayWindowPlots win("k-means results");
36 
37  cout << "Close the window to end.\n";
38 
39  while (win.isOpen())
40  {
41  // Generate N clusters of random points:
43  const size_t nClusters =
45 
46  for (size_t cl = 0; cl < nClusters; cl++)
47  {
48  const size_t nPts = getRandomGenerator().drawUniform(5, 50);
49 
50  TPoint2D clCenter;
51  clCenter.x = getRandomGenerator().drawUniform(0, 10);
52  clCenter.y = getRandomGenerator().drawUniform(0, 10);
53 
54  for (size_t p = 0; p < nPts; p++)
55  {
56  CPointType v;
57  v[0] = clCenter.x + getRandomGenerator().drawGaussian1D(0, 1);
58  v[1] = clCenter.y + getRandomGenerator().drawGaussian1D(0, 1);
59  points.push_back(v);
60  }
61  }
62 
63  // do k-means
65  vector<int> assignments;
66  tictac.Tic();
67 
68  const double cost =
69  mrpt::math::kmeanspp(nClusters, points, assignments, &centers);
70 
71  cout << "Took: " << tictac.Tac() * 1e3 << " ms.\n";
72  cout << "cost: " << cost << endl;
73 
74  // Show:
75  win.clf();
76  win.hold_on();
77  static const char colors[6] = {'b', 'r', 'k', 'g', 'm', 'c'};
78 
79  for (size_t c = 0; c < nClusters; c++)
80  {
81  CVectorDouble xs, ys;
82 
83  for (size_t i = 0; i < points.size(); i++)
84  {
85  if (size_t(assignments[i]) == c)
86  {
87  xs.push_back(points[i][0]);
88  ys.push_back(points[i][1]);
89  }
90  }
91  win.plot(xs, ys, mrpt::format(".4%c", colors[c % 6]));
92  }
93  win.axis_fit();
94  win.axis_equal();
95 
96  cout << "Press any key to generate another random dataset...\n";
97  win.waitForKey();
98  };
99 }
100 
101 int main(int argc, char** argv)
102 {
103  try
104  {
105  TestKMeans();
106  return 0;
107  }
108  catch (std::exception& e)
109  {
110  std::cout << "MRPT exception caught: " << e.what() << std::endl;
111  return -1;
112  }
113  catch (...)
114  {
115  printf("Another exception!!");
116  return -1;
117  }
118 }
mrpt::math::kmeanspp
double kmeanspp(const size_t k, const LIST_OF_VECTORS1 &points, std::vector< int > &assignments, LIST_OF_VECTORS2 *out_centers=nullptr, const size_t attempts=3)
k-means++ algorithm to cluster a list of N points of arbitrary dimensionality into exactly K clusters...
Definition: include/mrpt/math/kmeans.h:159
kmeans.h
mrpt::math::TPoint2D::y
double y
Definition: lightweight_geom_data.h:49
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::math::dynamic_vector
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction.
Definition: eigen_frwds.h:44
c
const GLubyte * c
Definition: glext.h:6313
TestKMeans
void TestKMeans()
Definition: vision_stereo_rectify/test.cpp:27
points
GLsizei const GLfloat * points
Definition: glext.h:5339
mrpt::math::TPoint2D::x
double x
X,Y coordinates.
Definition: lightweight_geom_data.h:49
p
GLfloat GLfloat p
Definition: glext.h:6305
random.h
mrpt::random::CRandomGenerator::randomize
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
Definition: RandomGenerator.cpp:32
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
v
const GLdouble * v
Definition: glext.h:3678
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::aligned_std_vector
std::vector< T, mrpt::aligned_allocator_cpp11< T > > aligned_std_vector
Definition: aligned_std_vector.h:15
mrpt::random::CRandomGenerator::drawUniform
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm,...
Definition: RandomGenerators.h:111
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
aligned_std_vector.h
mrpt::math::CArrayNumeric
CArrayNumeric is an array for numeric types supporting several mathematical operations (actually,...
Definition: CArrayNumeric.h:25
mrpt::math::TPoint2D
Lightweight 2D point.
Definition: lightweight_geom_data.h:42
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::gui::CDisplayWindowPlots
Create a GUI window and display plots with MATLAB-like interfaces and commands.
Definition: CDisplayWindowPlots.h:31
mrpt::random::getRandomGenerator
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Definition: RandomGenerator.cpp:19
CTicTac.h
CArrayNumeric.h
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
mrpt::random
A namespace of pseudo-random numbers generators of diferent distributions.
Definition: random_shuffle.h:17
mrpt::random::CRandomGenerator::drawGaussian1D
double drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
Definition: RandomGenerators.h:162
CDisplayWindowPlots.h
mrpt::random::CRandomGenerator::drawUniform32bit
uint32_t drawUniform32bit()
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm,...
Definition: RandomGenerator.cpp:31
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



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