OpenWalnut  1.4.0
WHistogramND.h
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 #ifndef WHISTOGRAMND_H
26 #define WHISTOGRAMND_H
27 
28 #include <utility>
29 
30 #ifndef Q_MOC_RUN
31 #include <boost/array.hpp>
32 #endif
33 
34 /**
35  * This template should handly arbitrary N-dimensional histograms.
36  *
37  * \tparam N specifies the dimensionality
38  * \tparam T specifies the type of data. Normally this should be double or float.
39  */
40 template< std::size_t N, typename T = double >
41 /**
42  * Interface for an N-dimensional histogram.
43  */
44 class WHistogramND // NOLINT
45 {
46 public:
47  /**
48  * Shorthand for N-dimensional indices, counter, etc.
49  */
50  typedef boost::array< size_t, N > SizeArray;
51 
52  /**
53  * Shorthand for N-dimensional values of type T.
54  */
55  typedef boost::array< T, N > TArray; // e.g. DoubleArray for T == double
56 
57  /**
58  * Default constructor. Creates an empty N-dimensional histogram covering the specified min and max values with the specified number of buckets.
59  *
60  * \param min the smallest value(s) in each dimension
61  * \param max the largest value(s) in each dimension
62  * \param buckets the number of buckets in each direction (may be non uniform).
63  */
64  WHistogramND( TArray min, TArray max, SizeArray buckets );
65 
66  /**
67  * Copy constructor. Creates a deep copy of the specified ND histogram.
68  *
69  * \param hist the HistogramND to copy.
70  */
71  WHistogramND( const WHistogramND& hist );
72 
73  /**
74  * Default destructor.
75  */
76  virtual ~WHistogramND();
77 
78  /**
79  * Get the count of the specified bucket.
80  *
81  * \param index in each dimension
82  *
83  * \return elements in the bucket.
84  */
85  virtual size_t operator()( SizeArray index ) const = 0;
86 
87  /**
88  * Returns the number of buckets in the HistogramND with the actual mapping.
89  *
90  * \return number of buckets
91  */
92  virtual size_t size() const;
93 
94  /**
95  * Returns the minimum value(s).
96  *
97  * \return minimum
98  */
99  virtual TArray getMinima() const;
100 
101  /**
102  * Returns the maximum value(s).
103  *
104  * \return maximum
105  */
106  virtual TArray getMaxima() const;
107 
108  /**
109  * Return the measure of one specific bucket. For one dimensional Histograms this is the width of the bucket, for two
110  * dimensions this is the area, for three dims this is the volume, etc.
111  *
112  * \param index the measure for this bucket is queried.
113  *
114  * \return the size of a bucket.
115  */
116  virtual T getBucketSize( SizeArray index ) const = 0;
117 
118  /**
119  * Returns the actual (right-open) interval in each dimension associated with the given index.
120  *
121  * \param index for this bucket the intervals will be returned
122  *
123  * \return the right-open interval in each dimension.
124  */
125  virtual boost::array< std::pair< T, T >, N > getIntervalForIndex( SizeArray index ) const = 0;
126 
127 protected:
128  /**
129  * Default constructor is protected to allow subclassing constructors not to use initialization lists which would
130  * imply C++11 or GNU++11 style initializers for boost::array members. To workaround, reset() member function is
131  * provided.
132  */
133  WHistogramND();
134 
135  /**
136  * Initializes all members. This exists because to circumvent boost::array initializers in c'tor init lists and
137  * reduces code duplication, as it is used in this abstract class as well as in its base classes.
138  *
139  * \param min Minimal values in each dimension.
140  * \param max Maximal values in each dimension.
141  * \param buckets \#buckets in each dimension.
142  */
143  void reset( TArray min, TArray max, SizeArray buckets );
144 
145  /**
146  * The smallest value in each dimension.
147  */
148  TArray m_min;
149 
150  /**
151  * The biggest value in each dimension.
152  */
153  TArray m_max;
154 
155  /**
156  * The number of buckets.
157  */
158  SizeArray m_buckets;
159 
160  /**
161  * Total number of buckets.
162  */
163  size_t m_nbBuckets;
164 
165 private:
166 };
167 
168 template< std::size_t N, typename T >
170 {
171 }
172 
173 template< std::size_t N, typename T >
175 {
176  reset( min, max, buckets );
177 }
178 
179 template< std::size_t N, typename T >
181 {
182  m_min = min;
183  m_max = max;
184 
185  WAssert( min.size() == max.size(), "Error, WHistogram initialized with wrong dimensionality" );
186  for( size_t i = 0; i < min.size(); ++i )
187  {
188  WAssert( min[i] <= max[i], "Error, WHistogram has at least one dimension where max is smaller than min" );
189  }
190 
191  m_buckets = buckets;
192  m_nbBuckets = 1;
193  for( typename SizeArray::const_iterator cit = buckets.begin(); cit != buckets.end(); ++cit )
194  {
195  m_nbBuckets *= *cit;
196  }
197 }
198 
199 template< std::size_t N, typename T >
201 {
202 }
203 
204 template< std::size_t N, typename T >
206 {
207  m_min = other.m_min;
208  m_max = other.m_max;
209  m_buckets = other.m_buckets;
210  m_nbBuckets = other.m_nbBuckets;
211 }
212 
213 template< std::size_t N, typename T >
215 {
216  return m_nbBuckets;
217 }
218 
219 template< std::size_t N, typename T >
221 {
222  return m_min;
223 }
224 
225 template< std::size_t N, typename T >
227 {
228  return m_max;
229 }
230 
231 #endif // WHISTOGRAMND_H
This template should handly arbitrary N-dimensional histograms.
Definition: WHistogramND.h:44
virtual size_t size() const
Returns the number of buckets in the HistogramND with the actual mapping.
Definition: WHistogramND.h:214
virtual ~WHistogramND()
Default destructor.
Definition: WHistogramND.h:200
size_t m_nbBuckets
Total number of buckets.
Definition: WHistogramND.h:163
TArray m_max
The biggest value in each dimension.
Definition: WHistogramND.h:153
virtual boost::array< std::pair< T, T >, N > getIntervalForIndex(SizeArray index) const =0
Returns the actual (right-open) interval in each dimension associated with the given index...
virtual size_t operator()(SizeArray index) const =0
Get the count of the specified bucket.
boost::array< T, N > TArray
Shorthand for N-dimensional values of type T.
Definition: WHistogramND.h:55
virtual T getBucketSize(SizeArray index) const =0
Return the measure of one specific bucket.
TArray m_min
The smallest value in each dimension.
Definition: WHistogramND.h:148
void reset(TArray min, TArray max, SizeArray buckets)
Initializes all members.
Definition: WHistogramND.h:180
virtual TArray getMaxima() const
Returns the maximum value(s).
Definition: WHistogramND.h:226
boost::array< size_t, N > SizeArray
Shorthand for N-dimensional indices, counter, etc.
Definition: WHistogramND.h:50
WHistogramND()
Default constructor is protected to allow subclassing constructors not to use initialization lists wh...
Definition: WHistogramND.h:169
SizeArray m_buckets
The number of buckets.
Definition: WHistogramND.h:158
virtual TArray getMinima() const
Returns the minimum value(s).
Definition: WHistogramND.h:220