OpenWalnut  1.5.0dev
WHistogram2D.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <utility>
26 
27 #include <core/common/math/WMath.h>
28 #include <core/graphicsEngine/WGETexture.h>
29 #include "WAssert.h"
30 #include "WHistogram2D.h"
31 #include "WLimits.h"
32 #include "WLogger.h"
33 
34 WHistogram2D::WHistogram2D( double minX, double maxX, double minY, double maxY, size_t bucketsX, size_t bucketsY )
35 {
36  // use protecte default ctor to workaround missing initializer lists which are part of C++11 and GNU++11 only.
37  TArray min = {{ minX, minY }}; // NOLINT braces
38  TArray max = {{ maxX, maxY }}; // NOLINT braces
39  SizeArray buckets = {{ bucketsX, bucketsY }}; // NOLINT braces
40  reset( min, max, buckets );
41 
42  m_intervalWidth[0] = std::abs( maxX - minX ) / static_cast< double >( bucketsX );
43  m_intervalWidth[1] = std::abs( maxY - minY ) / static_cast< double >( bucketsY );
44  m_bins = BinType::Zero( bucketsX, bucketsY );
45 }
46 
48 {
49 }
50 
52  : WHistogramND( other )
53 {
54  m_bins = other.m_bins;
55 }
56 
57 size_t WHistogram2D::operator()( SizeArray index ) const
58 {
59  return m_bins( index[0], index[1] );
60 }
61 
62 size_t WHistogram2D::operator()( size_t i, size_t j ) const
63 {
64  SizeArray index = {{ i, j }};
65  return operator()( index );
66 }
67 
68 double WHistogram2D::getBucketSize( SizeArray /* index */ ) const
69 {
70  return m_intervalWidth[0] * m_intervalWidth[1];
71 }
72 
73 boost::array< std::pair< double, double >, 2 > WHistogram2D::getIntervalForIndex( SizeArray index ) const
74 {
75  boost::array< std::pair< double, double >, 2 > result;
76 
77  for( size_t i = 0; i < 2; ++i )
78  {
79  result[i] = std::make_pair( m_intervalWidth[i] * index[i] + m_min[i],
80  m_intervalWidth[i] * ( index[i] + 1 ) + m_min[i] );
81  }
82 
83  return result;
84 }
85 
86 void WHistogram2D::insert( TArray values )
87 {
88  if( values[0] > m_max[0] || values[0] < m_min[0] || values[1] > m_max[1] || values[1] < m_min[1] )
89  {
90  wlog::warn( "WHistogram2D" ) << std::scientific << std::setprecision( 16 ) << "Inserted value out of bounds, thread: ("
91  << values[0] << "," << values[1] << ") whereas min,max are: dim0: (" << m_min[0] << "," << m_max[0] << ") "
92  << "dim1:(" << m_min[1] << "," << m_max[1] << ")";
93  return;
94  }
95 
96  SizeArray coord = {{ 0, 0 }};
97 
98  for( size_t i = 0; i < 2; ++i )
99  {
100  if( std::abs( m_min[i] - m_max[i] ) <= 2.0 * wlimits::DBL_EPS )
101  {
102  coord[i] = m_buckets[i] - 1;
103  }
104  else if( values[i] >= ( m_max[i] - m_intervalWidth[i] ) && values[i] <= m_max[i] )
105  {
106  coord[i] = m_buckets[i] - 1;
107  }
108  else
109  {
110  coord[i] = static_cast< size_t >( ( values[i] - m_min[i] ) / std::abs( m_max[i] - m_min[i] ) * ( m_buckets[i] ) );
111  }
112  }
113 
114  m_bins( coord[0], coord[1] )++;
115 }
116 
117 void WHistogram2D::insert( double x, double y )
118 {
119  TArray values = {{ x, y }};
120  insert( values );
121 }
122 
124 {
125  osg::ref_ptr< osg::Image > image = new osg::Image();
126  size_t imageWidth = m_buckets[0];
127  size_t imageHeight = m_buckets[1];
128  float maxCount = 0;
129 
130  for( size_t j = 0; j < imageHeight; ++j ) // get max bin for scaling
131  {
132  for( size_t i = 0; i < imageWidth; ++i )
133  {
134  if( m_bins( i, j ) > maxCount )
135  {
136  maxCount = static_cast< float >( m_bins( i, j ) );
137  }
138  }
139  }
140 
141  image->allocateImage( imageWidth, imageHeight, 1, GL_RED, GL_FLOAT );
142  image->setInternalTextureFormat( GL_RED );
143  float* data = reinterpret_cast< float* >( image->data() );
144 
145  for( size_t j = 0; j < imageHeight; ++j )
146  {
147  for( size_t i = 0; i < imageWidth; ++i )
148  {
149  data[i + j * imageWidth] = static_cast< float >( m_bins( i, j ) ) / maxCount;
150  }
151  }
152 
153  return WGETexture2D::RPtr( new WGETexture2D( image ) );
154 }
155 
156 /**
157  * Unnamed namespace for helper functions keeping the code DRY as possible.
158  */
159 namespace
160 {
161  double calcAreaScale( const double bucket, const size_t j )
162  {
163  double theta = pi() - ( j * bucket + ( bucket / 2.0 ) );
164  return 1.0 / sin( theta );
165  }
166 }
167 
169 {
170  osg::ref_ptr< osg::Image > image = new osg::Image();
171  size_t imageWidth = m_buckets[0];
172  size_t imageHeight = m_buckets[1];
173  double maxCount = 0.0;
174  const double bucket = pi() / static_cast< double >( imageHeight );
175  double areaScale = 0.0;
176 
177  for( size_t j = 0; j < imageHeight; ++j ) // get max bin for scaling
178  {
179  areaScale = calcAreaScale( bucket, j );
180  for( size_t i = 0; i < imageWidth; ++i )
181  {
182  if( areaScale * m_bins( i, j ) > maxCount )
183  {
184  maxCount = areaScale * static_cast< double >( m_bins( i, j ) );
185  }
186  }
187  }
188 
189  image->allocateImage( imageWidth, imageHeight, 1, GL_RED, GL_FLOAT );
190  image->setInternalTextureFormat( GL_RED );
191  float* data = reinterpret_cast< float* >( image->data() );
192 
193  for( size_t j = 0; j < imageHeight; ++j )
194  {
195  areaScale = calcAreaScale( bucket, j );
196  for( size_t i = 0; i < imageWidth; ++i )
197  {
198  data[i + j * imageWidth] = areaScale * static_cast< double >( m_bins( i, j ) ) / maxCount;
199  }
200  }
201 
202  return WGETexture2D::RPtr( new WGETexture2D( image ) );
203 }
204 
This calls serves a simple purpose: have a texture and its scaling information together which allows ...
Definition: WGETexture.h:53
osg::ref_ptr< WGETexture< TextureType > > RPtr
Convenience type for OSG reference pointer on WGETextures.
Definition: WGETexture.h:58
Uniform two dimensional histogram for double values.
Definition: WHistogram2D.h:42
virtual size_t operator()(SizeArray index) const
Get the count of the specified bucket.
BinType m_bins
Storing the bucket counts, how often a value occurs.
Definition: WHistogram2D.h:150
~WHistogram2D()
Cleans up!
WGETexture2D::RPtr getTexture()
Copy-convert this into a texture.
TArray m_intervalWidth
For each dimension this stores the uniform interval width.
Definition: WHistogram2D.h:155
virtual boost::array< std::pair< double, double >, 2 > getIntervalForIndex(SizeArray index) const
Returns the actual (right-open) interval in each dimension associated with the given index.
void insert(TArray values)
Given a value the corresponding bucket is determined and incremented by one.
WHistogram2D(double minX, double maxX, double minY, double maxY, size_t bucketsX, size_t bucketsY)
Creates a two dimensional histogram field, bounded by the given limits, containing the demanded numbe...
WGETexture2D::RPtr getSphereTexture()
Copy-convert this into a spherical texture.
virtual double getBucketSize(SizeArray index) const
Return the measure of one specific bucket.
This template should handly arbitrary N-dimensional histograms.
Definition: WHistogramND.h:43
void reset(TArray min, TArray max, SizeArray buckets)
Initializes all members.
Definition: WHistogramND.h:178
SizeArray m_buckets
The number of buckets.
Definition: WHistogramND.h:156
boost::array< double, N > TArray
Shorthand for N-dimensional values of type T.
Definition: WHistogramND.h:53
TArray m_min
The smallest value in each dimension.
Definition: WHistogramND.h:146
TArray m_max
The biggest value in each dimension.
Definition: WHistogramND.h:151
boost::array< size_t, N > SizeArray
Shorthand for N-dimensional indices, counter, etc.
Definition: WHistogramND.h:48
const double DBL_EPS
Smallest double such: 1.0 + DBL_EPS == 1.0 is still true.
Definition: WLimits.cpp:46
WStreamedLogger warn(const std::string &source)
Logging a warning message.
Definition: WLogger.h:309