Main MRPT website > C++ reference for MRPT 1.5.7
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  ASSERT_(!m_bins.empty());
45  if (x < m_min || x > m_max) return;
46 
47  size_t ind = static_cast<size_t>(m_binSizeInv * (x - m_min));
48  if (ind>=m_bins.size()) ind=m_bins.size()-1;
49 
50  m_bins[ind]++;
51  m_count++;
52 }
53 
54 /*---------------------------------------------------------------
55  getBinCount
56  ---------------------------------------------------------------*/
57 size_t CHistogram::getBinCount(const size_t index) const
58 {
59  if (index>=m_bins.size()) THROW_EXCEPTION("Index out of bounds")
60 
61  return m_bins[index];
62 }
63 
64 /*---------------------------------------------------------------
65  getBinRatio
66  ---------------------------------------------------------------*/
67 double CHistogram::getBinRatio(const size_t index) const
68 {
69  if (index>=m_bins.size()) THROW_EXCEPTION("Index out of bounds")
70 
71  if (m_count) return m_bins[index]/double(m_count);
72  else return 0;
73 }
74 
75 /*---------------------------------------------------------------
76  getHistogram
77  ---------------------------------------------------------------*/
78 void CHistogram::getHistogram( std::vector<double> &x, std::vector<double> &hits ) const
79 {
80  linspace(m_min,m_max,m_bins.size(), x);
81  const size_t N= m_bins.size();
82  hits.resize(N);
83  for (size_t i=0;i<N;i++) hits[i] = static_cast<double>(m_bins[i]); // metaprogramming::copy_container_typecasting(m_bins,);
84 }
85 
86 
87 /*---------------------------------------------------------------
88  getHistogramNormalized
89  ---------------------------------------------------------------*/
90 void CHistogram::getHistogramNormalized( std::vector<double> &x, std::vector<double> &hits ) const
91 {
92  const size_t N = m_bins.size();
93  linspace(m_min,m_max,N, x);
94 
95  hits.resize(N);
96  const double K=m_binSizeInv/m_count;
97  for (size_t i=0;i<N;i++)
98  hits[i]=K*m_bins[i];
99 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
#define min(a, b)
#define THROW_EXCEPTION(msg)
std::vector< size_t > m_bins
The bins counter.
Definition: CHistogram.h:40
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:39
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
GLuint index
Definition: glext.h:3891
void add(const double x)
Add an element to the histogram.
Definition: CHistogram.cpp:42
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:41
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:57
#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:67
void clear()
Clear the histogram:
Definition: CHistogram.cpp:33
GLenum GLint x
Definition: glext.h:3516
double m_max
The histogram limits.
Definition: CHistogram.h:38
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:90



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 8277875f6 Mon Jun 11 02:47:32 2018 +0200 at lun oct 28 01:50:49 CET 2019