PICCANTE  0.4
The hottest HDR imaging library!
vec.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_VEC_HPP
19 #define PIC_UTIL_VEC_HPP
20 
21 #include <random>
22 #include <assert.h>
23 
24 #include "../base.hpp"
25 #include "../util/math.hpp"
26 
27 #include "../util/string.hpp"
28 
29 namespace pic {
30 
34 template<uint N, class T>
35 class Vec
36 {
37 public:
38  T data[N];
39 
44  {
45  for(uint i = 0; i < N; i++) {
46  this->data[i] = T(0);
47  }
48  }
49 
55  Vec<N, T>(T data0, T data1)
56  {
57  assert(N >= 2);
58  data[0] = data0;
59  data[1] = data1;
60  }
61 
68  Vec<N, T>(T data0, T data1, T data2)
69  {
70  assert(N >= 3);
71  data[0] = data0;
72  data[1] = data1;
73  data[2] = data2;
74  }
75 
81  {
82  for (auto i = 0; i < N; i++) {
83  this->data[i] = data[i];
84  }
85  }
86 
90  void setZero()
91  {
92  for(auto i = 0; i < N; i++) {
93  data[i] = T(0);
94  }
95  }
96 
100  void setOne()
101  {
102  for(auto i = 0; i < N; i++) {
103  data[i] = T(1);
104  }
105  }
106 
111  Vec<N, T> inverse(T maxVal = T(-1))
112  {
113  if(maxVal <= T(0)) {
114  maxVal = this->getMax();
115  }
116 
117  Vec<N, T> ret;
118 
119  for (auto i = 0; i < N; i++) {
120  ret.data[i] = maxVal - this->data[i];
121  }
122 
123  return ret;
124  }
125 
130  T *convertToArray(T *ret)
131  {
132  if(ret == NULL) {
133  ret = new T[N];
134  }
135 
136  memcpy(ret, this->data, sizeof(T) * N);
137 
138  return ret;
139  }
140 
146  {
147  bool ret = true;
148  T zero = T(0);
149  for (auto i = 0; i < N; i++) {
150  ret = ret && (this->data[i] > zero);
151  }
152 
153  return ret;
154  }
155 
160  bool hasNegative()
161  {
162  bool ret = false;
163  T zero = T(0);
164  for (auto i = 0; i < N; i++) {
165  ret = ret || (this->data[i] < zero);
166  }
167 
168  return ret;
169  }
170 
172  {
173  Vec<N, T> ret;
174  memcpy(ret.data, data, N * sizeof(T));
175  return ret;
176  }
177 
183  const T &operator[](std::size_t i) const
184  {
185  return data[i];
186  }
187 
193  T &operator[](std::size_t i)
194  {
195  return data[i];
196  }
197 
203  bool equal(Vec<N, T> a)
204  {
205  for(auto i = 0; i < N; i++) {
206  if(a[i] != data[i]) {
207  return false;
208  }
209  }
210 
211  return true;
212  }
213 
219  {
220  T ret = T(0);
221  for (auto i = 0; i < N; i++) {
222  ret += this->data[i];
223  }
224 
225  return ret / T(N);
226  }
227 
232  T getSum()
233  {
234  T ret = T(0);
235  for (auto i = 0; i < N; i++) {
236  ret += this->data[i];
237  }
238 
239  return ret;
240  }
241 
246  T getMax()
247  {
248  T ret = this->data[0];
249  for (auto i = 1; i < N; i++) {
250  ret = this->data[i] > ret ? this->data[i] : ret;
251  }
252  return ret;
253  }
254 
260  {
261  float valMax = getMax();
262 
263  for (auto i = 1; i < N; i++) {
264  if (valMax == this->data[i]) {
265  return i;
266  }
267  }
268 
269  return -1;
270  }
271 
277  {
278  T out = T(0);
279  for(auto i=0; i<N; i++) {
280  out += data[i] * a[i];
281  }
282  return out;
283  }
284 
290  {
291  T ret = this->data[0] * this->data[0];
292 
293  for(auto i = 1; i < N; i++) {
294  ret += this->data[i] * this->data[i];
295  }
296 
297  return ret;
298  }
299 
306  {
307  T tmp = data[0] - x[0];
308  T d2 = tmp * tmp;
309 
310  for(auto i = 1; i < N; i++) {
311  tmp = data[i] - x[i];
312  d2 += tmp * tmp;
313  }
314 
315  return d2;
316  }
317 
324  void clamp(T min, T max)
325  {
326  for (auto i = 0; i < N; i++) {
327  data[i] = CLAMPi(data[i], min, max);
328  }
329  }
330 
336  {
337  T out = data[0] * data[0];
338 
339  for(auto i = 1; i < N; i++) {
340  out += data[i] * data[i];
341  }
342 
343  return out;
344  }
345 
351  std::string toString()
352  {
353  std::string ret = "[";
354  for (auto i = 0; i < N; i++) {
355  ret += fromNumberToString(data[i]);
356  if (i != (N - 1)) {
357  ret += ", ";
358  }
359  }
360  ret += "]";
361 
362  return ret;
363  }
364 
368  void print()
369  {
370  std::string vec_str = toString();
371  printf("%s\n", vec_str.c_str());
372  }
373 
374  /*
375  *
376  * Scalar Operands
377  *
378  */
379 
384  void operator =(const T &a)
385  {
386  for (auto i = 0; i < N; i++) {
387  this->data[i] = a;
388  }
389  }
390 
395  void operator =(const T *a)
396  {
397  for (auto i = 0; i < N; i++) {
398  this->data[i] = a[i];
399  }
400  }
401 
406  void operator =(const Vec<N, T> &a)
407  {
408  for (auto i = 0; i < N; i++) {
409  this->data[i] = a.data[i];
410  }
411  }
412 
417  void operator +=(const T &a)
418  {
419  for (auto i = 0; i < N; i++) {
420  this->data[i] += a;
421  }
422  }
423 
428  void operator +=(const T *a)
429  {
430  for (auto i = 0; i < N; i++) {
431  this->data[i] = a[i];
432  }
433  }
434 
440  Vec<N, T> operator +(const T &a) const
441  {
442  Vec<N, T> ret = this->clone();
443  ret += a;
444  return ret;
445  }
446 
451  void operator -=(const T &a)
452  {
453  for (auto i = 0; i < N; i++) {
454  this->data[i] -= a;
455  }
456  }
457 
463  Vec<N, T> operator -(const T &a) const
464  {
465  Vec<N, T> ret;
466  memcpy(ret.data, this->data, sizeof(T) * N);
467  ret -= a;
468  return ret;
469  }
470 
471  void mul(const T &a)
472  {
473  for (auto i = 0; i < N; i++) {
474  this->data[i] *= a;
475  }
476  }
477 
482  void operator *=(const T &a)
483  {
484  this->mul(a);
485  }
486 
491  void operator *=(const T *a)
492  {
493  for (auto i = 0; i < N; i++) {
494  this->data[i] *= a[i];
495  }
496  }
497 
503  Vec<N, T> operator *(const T &a) const
504  {
505  Vec<N, T> ret;
506  memcpy(ret.data, this->data, sizeof(T) * N);
507  ret.mul(a);
508  return ret;
509  }
510 
515  void operator /=(const float &a)
516  {
517  for (auto i = 0; i < N; i++) {
518  this->data[i] /= a;
519  }
520  }
521 
527  Vec<N, T> operator /(const float &a) const
528  {
529  Vec<N, T> ret;
530  memcpy(ret.data, this->data, sizeof(T) * N);
531  ret /= a;
532  return ret;
533  }
534 
535  /*
536  *
537  * Vec Operands
538  *
539  */
540 
545  void operator +=(const Vec<N, T> &col)
546  {
547  for (auto i = 0; i < N; i++) {
548  this->data[i] += col.data[i];
549  }
550  }
551 
557  Vec<N, T> operator +(const Vec<N, T> &col) const
558  {
559  Vec<N, T> ret;
560  memcpy(ret.data, this->data, sizeof(T) * N);
561  ret += col;
562  return ret;
563  }
564 
569  void operator -=(const Vec<N, T> &col)
570  {
571  for (auto i = 0; i < N; i++) {
572  this->data[i] -= col.data[i];
573  }
574  }
575 
581  {
582  Vec<N, T> ret;
583  for (auto i = 0; i < N; i++) {
584  ret.data[i] = -this->data[i];
585  }
586 
587  return ret;
588  }
589 
595  Vec<N, T> operator -(const Vec<N, T> &col) const
596  {
597  Vec<N, T> ret;
598  memcpy(ret.data, this->data, sizeof(T) * N);
599  ret -= col;
600  return ret;
601  }
602 
603  void mul(const Vec<N, T> &a)
604  {
605  for (auto i = 0; i < N; i++) {
606  this->data[i] *= a.data[i];
607  }
608  }
609 
614  void operator *=(const Vec<N, T> &a)
615  {
616  this->mul(a);
617  }
618 
625  {
626  Vec<N, T> ret;
627  memcpy(ret.data, this->data, sizeof(T) * N);
628  ret.mul(a);
629  return ret;
630  }
631 
637  {
638  for (auto i = 0; i < N; i++) {
639  this->data[i] /= a.data[i];
640  }
641  }
642 
649  {
650  Vec<N, T> ret;
651  memcpy(ret.data, this->data, sizeof(T) * N);
652  ret /= a;
653  return ret;
654  }
655 
662  {
663  bool ret = false;
664  for (auto i = 0; i < N; i++) {
665  ret = ret || (this->data[i] != a.data[i]);
666  }
667  return ret;
668  }
669 
676  {
677  bool ret = true;
678  for (auto i = 0; i < N; i++) {
679  ret = ret && (this->data[i] == a.data[i]);
680  }
681  return ret;
682  }
683 
684 };
685 
691 template<uint N>
693 {
694  for(auto i = 0; i < N; i++) {
695  if((sample[i] < -1.0f) || (sample[i] > 1.0f)) {
696  return false;
697  }
698  }
699 
700  return true;
701 }
702 
703 template<uint N>
705 {
706  float length = x.squaredSum();
707 
708  if(length > 0.0f) {
709  length = sqrtf(length);
710  for(auto i = 0; i < N; i++) {
711  x[i] /= length;
712  }
713  }
714 
715  return x;
716 }
717 
723 template<uint N>
725 {
726  Vec<N, float> x;
727 
728  for(auto i = 0; i < N; i++) {
729  x[i] = getRandom((*m)()) * 2.0f - 1.0f;
730  }
731 
732  return x;
733 }
734 
735 template<uint N>
737 {
738  printf("\n Values :");
739  for(auto i = 0; i < N; i++) {
740  printf("%d ", ret.data[i]);
741  }
742  printf("\n");
743 }
744 
752 template<uint N>
753 PIC_INLINE Vec<N, float> annulusSampling(std::mt19937 *m, Vec<N, float> center, float radius)
754 {
755  Vec<N, float> x;
756 
757  while(true) {
758  for(auto i = 0; i < N; i++) {
759  x[i] = getRandom((*m)()) * 4.0f - 2.0f;
760  }
761 
762  float t = x.lengthSq();
763 
764  if((t < 1.0f) || (t > 4.0f)) {
765  break;
766  }
767  }
768 
769  for(auto i = 0; i < N; i++) {
770  x[i] = x[i] * radius + center[i];
771  }
772 
773  return x;
774 }
775 
776 template<uint N>
777 void vecGamma(Vec<N, float> &ret, float g)
778 {
779  for (auto i = 0; i < N; i++) {
780  ret.data[i] = powf(ret.data[i], g);
781  }
782 }
783 
784 template<uint N>
786 {
787  for (auto i = 0; i < N; i++) {
788  ret.data[i] = sqrtf(ret.data[i]);
789  }
790 }
791 
792 template<uint N>
794 {
795  Vec<N, float> ret;
796  for (auto i = 0; i < N; i++) {
797  ret.data[i] = value / in.data[i];
798  }
799 
800  return ret;
801 }
802 
803 template<uint N, class T>
804 void transferFromVecToPlain(std::vector< Vec<N, T> > &in, std::vector< T > &out)
805 {
806  for(auto i = 0; i < in.size(); i++) {
807  for(auto j = 0; j < N; j++) {
808  out.push_back(in[i][j]);
809  }
810  }
811 }
812 
813 template<uint N, class T>
814 void transferFromPlainToVec(std::vector< T > &in, std::vector< Vec<N, T> > &out)
815 {
816  for(auto i = 0; i < in.size(); i+= N) {
817  Vec<N, T> tmp;
818  for(auto j = 0; j < N; j++) {
819  tmp[j] = in[i + j];
820  }
821 
822  out.push_back(tmp);
823  }
824 }
825 
830 
835 
840 
845 
850 
851 } // end namespace pic
852 
853 #endif /* PIC_UTIL_VEC_HPP */
854 
T getMax()
getMax
Definition: vec.hpp:246
void vecGamma(Vec< N, float > &ret, float g)
Definition: vec.hpp:777
void vecrint(Vec< N, float > &ret)
Definition: vec.hpp:736
unsigned int uint
Definition: base.hpp:23
T squaredSum()
squaredSum
Definition: vec.hpp:289
bool operator!=(Vec< N, T > &a)
operator !=
Definition: vec.hpp:661
PIC_INLINE Vec< N, float > randomPoint(std::mt19937 *m)
randomPoint
Definition: vec.hpp:724
T * convertToArray(T *ret)
convertToArray
Definition: vec.hpp:130
std::string fromNumberToString(T num)
fromNumberToString converts a number into a string.
Definition: string.hpp:102
void mul(const Vec< N, T > &a)
Definition: vec.hpp:603
Vec< 3, int > Vec3i
Vec3i.
Definition: vec.hpp:834
void operator=(const T &a)
operator =
Definition: vec.hpp:384
void setZero()
setZero
Definition: vec.hpp:90
bool isGreaterThanZero()
isGreaterThanZero
Definition: vec.hpp:145
Vec< 2, float > Vec2f
Vec2f.
Definition: vec.hpp:844
int getMaxChannel()
getMaxChannel
Definition: vec.hpp:259
bool hasNegative()
hasNegative
Definition: vec.hpp:160
Vec< 4, int > Vec4i
Vec4i.
Definition: vec.hpp:839
Vec< N, T > operator-() const
operator -
Definition: vec.hpp:580
const T & operator[](std::size_t i) const
operator []
Definition: vec.hpp:183
T getMean()
Mean.
Definition: vec.hpp:218
PIC_INLINE Vec< N, float > normalize(Vec< N, float > x)
Definition: vec.hpp:704
Vec< 2, int > Vec2i
Vec2i.
Definition: vec.hpp:829
Vec< N, T > operator+(const T &a) const
operator +
Definition: vec.hpp:440
void transferFromPlainToVec(std::vector< T > &in, std::vector< Vec< N, T > > &out)
Definition: vec.hpp:814
Vec< N, T > operator*(const T &a) const
operator *
Definition: vec.hpp:503
T getSum()
getSum
Definition: vec.hpp:232
T data[N]
Definition: vec.hpp:38
void operator/=(const float &a)
operator /=
Definition: vec.hpp:515
void print()
print
Definition: vec.hpp:368
PIC_INLINE Vec< N, float > annulusSampling(std::mt19937 *m, Vec< N, float > center, float radius)
annulusSampling
Definition: vec.hpp:753
void clamp(T min, T max)
clamp
Definition: vec.hpp:324
#define PIC_INLINE
Definition: base.hpp:33
std::string toString()
toString
Definition: vec.hpp:351
Vec< N, T > clone()
Definition: vec.hpp:171
void operator-=(const T &a)
operator -=
Definition: vec.hpp:451
T dot(Vec< N, T > a)
dot
Definition: vec.hpp:276
Vec< N, T > operator/(const float &a) const
operator /
Definition: vec.hpp:527
void setOne()
setOne
Definition: vec.hpp:100
PIC_INLINE bool insideVecBBox(const Vec< N, float > &sample)
insideVecBBox
Definition: vec.hpp:692
void operator+=(const T &a)
operator +=
Definition: vec.hpp:417
T & operator[](std::size_t i)
operator []
Definition: vec.hpp:193
#define CLAMPi(x, a, b)
Definition: math.hpp:81
Definition: bilateral_separation.hpp:25
bool equal(Vec< N, T > a)
equal
Definition: vec.hpp:203
PIC_INLINE float getRandom(unsigned int n)
Random returns a number in [0, 2^32 - 1] to a float in [0, 1].
Definition: math.hpp:138
void transferFromVecToPlain(std::vector< Vec< N, T > > &in, std::vector< T > &out)
Definition: vec.hpp:804
bool operator==(Vec< N, T > &a)
operator ==
Definition: vec.hpp:675
Vec< N, float > vecValOver(Vec< N, float > &in, float value)
Definition: vec.hpp:793
void vecSqrt(Vec< N, float > &ret)
Definition: vec.hpp:785
T lengthSq()
lengthSq
Definition: vec.hpp:335
The Vec class.
Definition: vec.hpp:35
Vec< N, T > inverse(T maxVal=T(-1))
inverse
Definition: vec.hpp:111
Vec< 3, float > Vec3f
Vec3f.
Definition: vec.hpp:849
T distanceSq(Vec< N, T > &x)
distanceSq
Definition: vec.hpp:305
void mul(const T &a)
Definition: vec.hpp:471
void operator*=(const T &a)
operator *=
Definition: vec.hpp:482