Commit 3a3b3a27 authored by reichenbach's avatar reichenbach

[ADD] added colors to histogram dataset bins

parent 20fa73ce
......@@ -78,27 +78,29 @@ std::pair< double, double > WHistogramBasic::getIntervalForIndex( std::size_t in
return std::make_pair( first, second );
}
void WHistogramBasic::insert( double value )
std::size_t WHistogramBasic::insert( double value )
{
if( value > m_maximum || value < m_minimum )
{
wlog::warn( "WHistogramBasic" ) << std::scientific << std::setprecision( 16 ) << "Inserted value out of bounds, thread: "
<< value << " as min, resp. max: " << m_minimum << "," << m_maximum;
return; // value = ( value > m_maximum ? m_maximum : m_minimum );
return 0u - 1u;
}
if( std::abs( m_minimum - m_maximum ) <= 2.0 * wlimits::DBL_EPS )
{
m_bins.at( m_nbBuckets - 1 )++;
return m_nbBuckets - 1;
}
else if( value >= ( m_maximum - m_intervalWidth ) && value <= m_maximum ) // last bin deserves extra treatment due to possbl out of bounds index
{
m_bins.at( m_nbBuckets - 1 )++;
return m_nbBuckets - 1;
}
else
{
m_bins.at( static_cast< std::size_t >( ( value - m_minimum ) / std::abs( m_maximum - m_minimum ) * ( m_nbBuckets ) ) )++;
}
std::size_t bin = static_cast< std::size_t >( ( value - m_minimum ) / std::abs( m_maximum - m_minimum ) * ( m_nbBuckets ) );
m_bins.at( bin )++;
return bin;
}
size_t WHistogramBasic::valuesSize() const
......
......@@ -109,8 +109,10 @@ public:
* Inserts a given value within the given range (min, max) into exactly one bin and increment its size.
*
* \param value Value to insert.
*
* \return The index of the bin the value was inserted into or the maximum size_t if it was out of range.
*/
virtual void insert( double value );
virtual std::size_t insert( double value );
protected:
private:
......
......@@ -23,6 +23,7 @@
//---------------------------------------------------------------------------
#include <string>
#include <vector>
#include "WDataSetHistogram1D.h"
......@@ -31,7 +32,16 @@ boost::shared_ptr< WPrototyped > WDataSetHistogram1D::m_prototype = boost::share
WDataSetHistogram1D::WDataSetHistogram1D( boost::shared_ptr< WHistogramBasic const > const& histo )
: WDataSet(),
m_histogram( new WHistogramBasic( *histo ) )
m_histogram( new WHistogramBasic( *histo ) ),
m_colors()
{
}
WDataSetHistogram1D::WDataSetHistogram1D( boost::shared_ptr< WHistogramBasic const > const& histo,
boost::shared_ptr< std::vector< WColor > const > const& colors )
: WDataSet(),
m_histogram( new WHistogramBasic( *histo ) ),
m_colors( colors )
{
}
......@@ -70,3 +80,13 @@ boost::shared_ptr< WHistogramBasic const > const& WDataSetHistogram1D::getHistog
return m_histogram;
}
bool WDataSetHistogram1D::hasColors() const
{
return m_histogram && m_colors && m_colors->size() == m_histogram->size();
}
WColor WDataSetHistogram1D::getColor( std::size_t bin ) const
{
return m_colors->at( bin );
}
......@@ -26,8 +26,10 @@
#define WDATASETHISTOGRAM1D_H
#include <string>
#include <vector>
#include "../common/WHistogramBasic.h"
#include "../common/WColor.h"
#include "WDataSet.h"
......@@ -56,6 +58,14 @@ public:
*/
explicit WDataSetHistogram1D( boost::shared_ptr< WHistogramBasic const > const& histo );
/**
* Construct a histogram and allows to set an array of colors used for the bins.
*
* \param histo The histogram.
* \param colors An array of one color per bin.
*/
WDataSetHistogram1D( boost::shared_ptr< WHistogramBasic const > const& histo, boost::shared_ptr< std::vector< WColor > const > const& colors );
/**
* Construct an empty and unusable instance. This is needed for the prototype mechanism.
*/
......@@ -94,6 +104,22 @@ public:
*/
static boost::shared_ptr< WPrototyped > getPrototype();
/**
* Whether this dataset has colors associated with the bins.
*
* \return true, if this dataset has colors.
*/
bool hasColors() const;
/**
* Get the color of a bin.
*
* \param bin The index of the bin to get the color from.
*
* \return The color of the bin.
*/
WColor getColor( std::size_t bin ) const;
protected:
/**
* The prototype as singleton.
......@@ -103,6 +129,9 @@ protected:
private:
//! The histogram.
boost::shared_ptr< WHistogramBasic const > const m_histogram;
//! The colors for the bins.
boost::shared_ptr< std::vector< WColor > const > const m_colors;
};
#endif // WDATASETHISTOGRAM1D_H
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment