PICCANTE  0.4
The hottest HDR imaging library!
indexed_array.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_UTIL_INDEXED_ARRAY_HPP
19 #define PIC_UTIL_INDEXED_ARRAY_HPP
20 
21 #include <vector>
22 
23 #include "../util/math.hpp"
24 
25 namespace pic {
26 
30 typedef std::vector<int> IntCoord;
31 
35 template <class T>
37 {
38 public:
40  {
41  }
42 
48  static bool bFuncNotNeg(T val)
49  {
50  return val > T(0);
51  }
52 
58  static bool bFuncNotZero(T val)
59  {
60  return (val < T(0)) || (val > T(0));
61  }
62 
68  static bool bFuncNeg(T val)
69  {
70  return (val < T(0));
71  }
72 
80  static void findSimple(T *data, int nData, bool(*func)(float), IntCoord &ret, int stride = 1)
81  {
82  for(int i = 0; i < nData; i += stride) {
83  if(func(data[i])) {
84  ret.push_back(i);
85  }
86  }
87  }
88 
96  static void find(float *data, int nData, bool(*func)(float,
97  std::vector<float>), std::vector<float> param, IntCoord &ret)
98  {
99  for(int i = 0; i < nData; i++) {
100  if(func(data[i], param)) {
101  ret.push_back(i);
102  }
103  }
104  }
105 
112  static T mean(T *data, IntCoord &coord)
113  {
114  if(coord.empty()) {
115  return T(0);
116  }
117 
118  T ret = data[coord[0]];
119 
120  for(unsigned int i = 1; i < coord.size(); i++) {
121  int j = coord[i];
122  ret += data[j];
123  }
124 
125  ret /= T(coord.size());
126 
127  return ret;
128  }
129 
136  static T min(T *data, IntCoord &coord)
137  {
138  if(coord.empty()) {
139  return T(0);
140  }
141 
142  float ret = data[coord[0]];
143 
144  for(unsigned int i = 1; i < coord.size(); i++) {
145  int j = coord[i];
146  ret = MIN(ret, data[j]);
147  }
148 
149  return ret;
150  }
151 
158  static T max(T *data, IntCoord &coord)
159  {
160  if(coord.empty()) {
161  return T(0);
162  }
163 
164  T ret = data[coord[0]];
165 
166  for(unsigned int i = 1; i < coord.size(); i++) {
167  int j = coord[i];
168  ret = MAX(ret, data[j]);
169  }
170 
171  return ret;
172  }
173 
181  static T percentile(T *data, IntCoord &coord, float percent)
182  {
183  if(coord.empty()) {
184  return T(0);
185  }
186 
187  int n = int(coord.size());
188  T *tmp = new T[n];
189 
190  for(int i = 0; i < n; i++) {
191  int j = coord[i];
192  tmp[i] = data[j];
193  }
194 
195  std::sort(tmp, tmp + n);
196 
197  percent = CLAMPi(percent, 0.0f, 1.0f);
198 
199  T ret = tmp[int(float(n - 1) * percent)];
200  delete[] tmp;
201 
202  return ret;
203  }
204 
210  static void scale(IntCoord &coord, int scale)
211  {
212  for(unsigned int i = 0; i < coord.size(); i++) {
213  coord.at(i) = coord.at(i) * scale;
214  }
215  }
216 
223  static float log10Mean(float *data, IntCoord &coord)
224  {
225  if(coord.empty()) {
226  return FLT_MAX;
227  }
228 
229  float delta = 1e-6f;
230  float ret = 0.0f;
231 
232  for(unsigned int i = 0; i < coord.size(); i++) {
233  int j = coord[i];
234  ret += log10f(data[j] + delta);
235  }
236 
237  return ret / float(coord.size());
238  }
239 
246  static float log2Mean(float *data, IntCoord &coord)
247  {
248  if(coord.empty()) {
249  return FLT_MAX;
250  }
251 
252  float delta = 1e-6f;
253  float ret = 0.0f;
254  float log2f = logf(2.0f);
255 
256  for(unsigned int i = 0; i < coord.size(); i++) {
257  int j = coord[i];
258  ret += logf(data[j] + delta) / log2f;
259  }
260 
261  return ret / float(coord.size());
262  }
263 
270  static void negative(T *data, IntCoord &coord, T referencePoint = T(1))
271  {
272  for(unsigned int i = 0; i < coord.size(); i++) {
273  int j = coord[i];
274  data[j] = referencePoint - data[j];
275  }
276  }
277 
284  static void add(T *data, IntCoord &coord, T val)
285  {
286  for(unsigned int i = 0; i < coord.size(); i++) {
287  int j = coord[i];
288  data[j] += val;
289  }
290  }
291 
298  static void sub(T *data, IntCoord &coord, T val)
299  {
300  for(unsigned int i = 0; i < coord.size(); i++) {
301  int j = coord[i];
302  data[j] -= val;
303  }
304  }
305 
312  static void mul(T *data, IntCoord &coord, T val)
313  {
314  for(unsigned int i = 0; i < coord.size(); i++) {
315  int j = coord[i];
316  data[j] *= val;
317  }
318  }
319 
326  static void div(T *dataDst, IntCoord &coord, T val)
327  {
328  for(unsigned int i = 0; i < coord.size(); i++) {
329  int j = coord[i];
330  dataDst[j] /= val;
331  }
332  }
333 
340  static void assign(T *dataDst, IntCoord &coord, T dataSrc)
341  {
342  for(unsigned int i = 0; i < coord.size(); i++) {
343  int j = coord[i];
344  dataDst[j] = dataSrc;
345  }
346  }
347 
354  static void assign(T *dataDst, IntCoord &coord, T *dataSrc)
355  {
356  for(unsigned int i = 0; i < coord.size(); i++) {
357  int j = coord[i];
358  dataDst[j] = dataSrc[j];
359  }
360  }
361 };
362 
364 
366 
368 
369 } // end namespace pic
370 
371 #endif /* PIC_UTIL_INDEXED_ARRAY_HPP */
372 
IndexedArray< int > IndexedArrayi
Definition: indexed_array.hpp:365
static void negative(T *data, IntCoord &coord, T referencePoint=T(1))
negative computes the negative value given a val reference point.
Definition: indexed_array.hpp:270
static void assign(T *dataDst, IntCoord &coord, T dataSrc)
assign
Definition: indexed_array.hpp:340
static T max(T *data, IntCoord &coord)
max computes the max value.
Definition: indexed_array.hpp:158
static void findSimple(T *data, int nData, bool(*func)(float), IntCoord &ret, int stride=1)
findSimple collects coordinates of data which satisfies a bool function func.
Definition: indexed_array.hpp:80
static bool bFuncNotNeg(T val)
bFuncNotNeg
Definition: indexed_array.hpp:48
static void find(float *data, int nData, bool(*func)(float, std::vector< float >), std::vector< float > param, IntCoord &ret)
find collects coordinates of data which satisfies a bool function func.
Definition: indexed_array.hpp:96
static T min(T *data, IntCoord &coord)
min computes the min value.
Definition: indexed_array.hpp:136
static void mul(T *data, IntCoord &coord, T val)
mul is the multiplicative operator.
Definition: indexed_array.hpp:312
std::vector< int > IntCoord
IntCoord.
Definition: indexed_array.hpp:30
static void assign(T *dataDst, IntCoord &coord, T *dataSrc)
Assign.
Definition: indexed_array.hpp:354
PIC_INLINE float log2f(float x)
log2f logarithm in base 2 for floating point
Definition: math.hpp:375
static void div(T *dataDst, IntCoord &coord, T val)
div is the division operator.
Definition: indexed_array.hpp:326
IndexedArray< unsigned int > IndexedArrayui
Definition: indexed_array.hpp:367
#define MIN(a, b)
Definition: math.hpp:69
The IndexedArray class.
Definition: indexed_array.hpp:36
static void add(T *data, IntCoord &coord, T val)
add is the additive operator.
Definition: indexed_array.hpp:284
static T percentile(T *data, IntCoord &coord, float percent)
percentile
Definition: indexed_array.hpp:181
static float log10Mean(float *data, IntCoord &coord)
log10Mean computes mean in the log10 domain.
Definition: indexed_array.hpp:223
static void scale(IntCoord &coord, int scale)
scale scales values.
Definition: indexed_array.hpp:210
static bool bFuncNotZero(T val)
bFuncNotZero
Definition: indexed_array.hpp:58
static float log2Mean(float *data, IntCoord &coord)
log2Mean computes mean in the log2 domain.
Definition: indexed_array.hpp:246
#define CLAMPi(x, a, b)
Definition: math.hpp:81
static void sub(T *data, IntCoord &coord, T val)
sub is the subtractive operator.
Definition: indexed_array.hpp:298
Definition: bilateral_separation.hpp:25
#define MAX(a, b)
Definition: math.hpp:73
IndexedArray< float > IndexedArrayf
Definition: indexed_array.hpp:363
IndexedArray()
Definition: indexed_array.hpp:39
static T mean(T *data, IntCoord &coord)
mean computes the mean value.
Definition: indexed_array.hpp:112
static bool bFuncNeg(T val)
bFuncNeg
Definition: indexed_array.hpp:68