Main MRPT website > C++ reference for MRPT 1.5.5
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) return m_bins[index]/double(m_count);
70  else return 0;
71 }
72 
73 /*---------------------------------------------------------------
74  getHistogram
75  ---------------------------------------------------------------*/
76 void CHistogram::getHistogram( std::vector<double> &x, std::vector<double> &hits ) const
77 {
78  linspace(m_min,m_max,m_bins.size(), x);
79  const size_t N= m_bins.size();
80  hits.resize(N);
81  for (size_t i=0;i<N;i++) hits[i] = static_cast<double>(m_bins[i]); // metaprogramming::copy_container_typecasting(m_bins,);
82 }
83 
84 
85 /*---------------------------------------------------------------
86  getHistogramNormalized
87  ---------------------------------------------------------------*/
88 void CHistogram::getHistogramNormalized( std::vector<double> &x, std::vector<double> &hits ) const
89 {
90  const size_t N = m_bins.size();
91  linspace(m_min,m_max,N, x);
92 
93  hits.resize(N);
94  const double K=m_binSizeInv/m_count;
95  for (size_t i=0;i<N;i++)
96  hits[i]=K*m_bins[i];
97 }
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:76
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: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: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:88



Page generated by Doxygen 1.8.14 for MRPT 1.5.5 Git: e06b63dbf Fri Dec 1 14:41:11 2017 +0100 at lun oct 28 01:31:35 CET 2019