Main MRPT website > C++ reference for MRPT 1.9.9
CHistogram.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-2017, 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 "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/math/CHistogram.h>
13 #include <mrpt/math/utils.h>
14 
15 using namespace mrpt;
16 using namespace mrpt::math;
17 using namespace mrpt::utils;
18 
19 /*---------------------------------------------------------------
20  Constructor
21  ---------------------------------------------------------------*/
22 CHistogram::CHistogram(const double min, const double max, const size_t nBins)
23  : m_min(min), m_max(max), m_bins(nBins, 0), m_count(0)
24 {
25  ASSERT_(nBins > 0)
26  ASSERT_(max > min)
27  m_binSizeInv = nBins / (m_max - m_min);
28 }
29 
30 /*---------------------------------------------------------------
31  clear
32  ---------------------------------------------------------------*/
34 {
35  m_bins.assign(m_bins.size(), 0);
36  m_count = 0;
37 }
38 
39 /*---------------------------------------------------------------
40  add
41  ---------------------------------------------------------------*/
42 void CHistogram::add(const double x)
43 {
44  if (x < m_min || x > m_max) return;
45 
46  size_t ind = static_cast<size_t>(m_binSizeInv * (x - m_min));
47 
48  m_bins[ind]++;
49  m_count++;
50 }
51 
52 /*---------------------------------------------------------------
53  getBinCount
54  ---------------------------------------------------------------*/
55 size_t CHistogram::getBinCount(const size_t index) const
56 {
57  if (index >= m_bins.size()) THROW_EXCEPTION("Index out of bounds")
58 
59  return m_bins[index];
60 }
61 
62 /*---------------------------------------------------------------
63  getBinRatio
64  ---------------------------------------------------------------*/
65 double CHistogram::getBinRatio(const size_t index) const
66 {
67  if (index >= m_bins.size()) THROW_EXCEPTION("Index out of bounds")
68 
69  if (m_count)
70  return m_bins[index] / double(m_count);
71  else
72  return 0;
73 }
74 
75 /*---------------------------------------------------------------
76  getHistogram
77  ---------------------------------------------------------------*/
79  std::vector<double>& x, std::vector<double>& hits) const
80 {
81  linspace(m_min, m_max, m_bins.size(), x);
82  const size_t N = m_bins.size();
83  hits.resize(N);
84  for (size_t i = 0; i < N; i++)
85  hits[i] = static_cast<double>(
86  m_bins
87  [i]); // metaprogramming::copy_container_typecasting(m_bins,);
88 }
89 
90 /*---------------------------------------------------------------
91  getHistogramNormalized
92  ---------------------------------------------------------------*/
94  std::vector<double>& x, std::vector<double>& hits) const
95 {
96  const size_t N = m_bins.size();
97  linspace(m_min, m_max, N, x);
98 
99  hits.resize(N);
100  const double K = m_binSizeInv / m_count;
101  for (size_t i = 0; i < N; i++) hits[i] = K * m_bins[i];
102 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#define min(a, b)
#define THROW_EXCEPTION(msg)
std::vector< size_t > m_bins
The bins counter.
Definition: CHistogram.h:45
CHistogram(const double min, const double max, const size_t nBins)
Constructor.
Definition: CHistogram.cpp:22
double m_binSizeInv
((max-min)/nBins)^-1
Definition: CHistogram.h:43
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
GLuint index
Definition: glext.h:4054
void add(const double x)
Add an element to the histogram.
Definition: CHistogram.cpp:42
double m_min
The histogram limits.
Definition: CHistogram.h:41
void linspace(T first, T last, size_t count, VECTOR &out_vector)
Generates an equidistant sequence of numbers given the first one, the last one and the desired number...
void getHistogram(std::vector< double > &x, std::vector< double > &hits) const
Returns the list of bin centers & hit counts.
Definition: CHistogram.cpp:78
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t m_count
The total elements count.
Definition: CHistogram.h:47
size_t getBinCount(const size_t index) const
Retuns the elements count into the selected bin index, where first one is 0.
Definition: CHistogram.cpp:55
#define ASSERT_(f)
double getBinRatio(const size_t index) const
Retuns the ratio in [0,1] range for the selected bin index, where first one is 0. ...
Definition: CHistogram.cpp:65
void clear()
Clear the histogram:
Definition: CHistogram.cpp:33
GLenum GLint x
Definition: glext.h:3538
void getHistogramNormalized(std::vector< double > &x, std::vector< double > &hits) const
Returns the list of bin centers & hit counts, normalized such as the integral of the histogram...
Definition: CHistogram.cpp:93



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019