cgv
vec.h
1 #pragma once
2 
3 #include <iostream>
4 #include <cmath>
5 #include <cassert>
6 #include <algorithm>
7 #include <functional>
8 #include <iterator>
9 #include <limits>
10 #include <string.h>
11 #include <vector>
12 
13 #ifdef max
14 #undef max
15 #endif
16 
17 #ifdef min
18 #undef min
19 #endif
20 
21 namespace cgv {
22  namespace math {
23 
24 
26 template <typename T>
27 class vec
28 {
29 
30 protected:
32  T *_data;
34  unsigned _size;
37 public:
38  typedef T value_type;
39  typedef T& reference;
40  typedef const T& const_reference;
41  typedef std::size_t size_type;
42  typedef std::ptrdiff_t difference_type;
43  typedef T* pointer;
44  typedef const T* const_pointer;
45 
46  typedef T* iterator;
47  typedef const T* const_iterator;
48 
49 
50  typedef std::reverse_iterator<iterator> reverse_iterator;
51  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
52 
53  iterator begin()
54  {
55  return _data;
56  }
57 
58  iterator end()
59  {
60  return _data+_size;
61  }
62 
63  const_iterator begin() const
64  {
65  return _data;
66  }
67 
68  const_iterator end() const
69  {
70  return _data+_size;
71  }
72 
73  reverse_iterator rbegin()
74  {
75  return reverse_iterator(end());
76  }
77 
78  reverse_iterator rend()
79  {
80  return reverse_iterator(begin());
81  }
82 
83  const_reverse_iterator rbegin() const
84  {
85  return const_reverse_iterator(end());
86  }
87 
88  const_reverse_iterator rend() const
89  {
90  return const_reverse_iterator(begin());
91  }
92 
93 
94 
95 
97  unsigned size() const
98  {
99  return _size;
100  }
101 
103  unsigned dim() const
104  {
105  return _size;
106  }
107 
109  vec()
110  {
111  _data = NULL;
112  _size = 0;
113  data_is_external = false;
114  }
115 
117  explicit vec(unsigned dim, const T& value = T(0))
118  {
119  _size=dim;
120  if (dim > 0) {
121  _data = new T[_size];
122  std::fill(_data, _data + _size, value);
123  }
124  else
125  _data = NULL;
126  data_is_external = false;
127  }
128 
129 
131  vec(unsigned dim, const T* marray)
132  {
133  _size = dim;
134  if(dim > 0)
135  {
136  _data = new T[_size];
137  memcpy(_data,marray,_size*sizeof(T));
138  }else
139  _data = NULL;
140  data_is_external = false;
141  }
142 
144  vec(const vec<T>&v)
145  {
146  _size = v.size();
147  if (v._data) {
148  _data = new T[v.size()];
149  memcpy(_data,v._data,_size*sizeof(T));
150  }
151  else
152  _data=NULL;
153  data_is_external = false;
154  }
155 
157  template <typename S>
158  vec(const vec<S>& v)
159  {
160  _size = v.size();
161  if(v.size() > 0) {
162  _data = new T[v.size()];
163  for(unsigned i = 0; i < _size;i++)
164  _data [i]=(T)v(i);
165  }
166  else
167  _data =NULL;
168  data_is_external = false;
169  }
170 
172  vec(const T& c0, const T& c1)
173  {
174  _size = 2;
175  _data = new T[2];
176  _data[0] = c0;
177  _data[1] = c1;
178  data_is_external = false;
179  }
180 
181 
183  vec(const T& c0, const T& c1, const T& c2)
184  {
185  _size = 3;
186  _data = new T[3];
187  _data[0] = c0;
188  _data[1] = c1;
189  _data[2] = c2;
190  data_is_external = false;
191  }
192 
194  vec(const T& c0, const T& c1, const T& c2, const T& c3)
195  {
196  _size = 4;
197  _data = new T[4];
198  _data[0] = c0;
199  _data[1] = c1;
200  _data[2] = c2;
201  _data[3] = c3;
202  data_is_external = false;
203  }
204 
206  void set(const T& c0, const T& c1)
207  {
208  assert(_size == 2);
209  _data[0] = c0;
210  _data[1] = c1;
211  }
212 
214  void set(const T& c0, const T& c1, const T& c2)
215  {
216  assert(_size == 3);
217  _data[0] = c0;
218  _data[1] = c1;
219  _data[2] = c2;
220  }
221 
223  void set(const T& c0, const T& c1, const T& c2, const T& c3)
224  {
225  assert(_size == 4);
226  _data[0] = c0;
227  _data[1] = c1;
228  _data[2] = c2;
229  _data[3] = c3;
230  }
232  void set_extern_data(unsigned dim, T* data)
233  {
234  destruct();
235  _size = dim;
236  _data = data;
237  data_is_external = true;
238  }
240  virtual ~vec()
241  {
242  destruct();
243  }
244 
245  void destruct()
246  {
247  if(_data && !data_is_external)
248  {
249  delete[] _data;
250  _data=NULL;
251  _size=0;
252  }
253  }
254 
256  operator T*()
257  {
258  return _data;
259  }
260 
262  operator const T*() const
263  {
264  return _data;
265  }
266 
269  {
270  if(v.size() == 0)
271  destruct();
272  else {
273  if (size() != v.size())
274  resize(v.size());
275  memcpy(_data,v._data,_size*sizeof(T));
276  }
277  return *this;
278  }
279 
281  vec<T>& operator = (const T& s)
282  {
283  fill(s);
284  return *this;
285  }
286 
288  template <typename S>
290  {
291  resize(v.size());
292  for (unsigned i=0;i<_size;++i) _data[i]=(T)v(i);
293  return *this;
294  }
295 
297  T& operator () (unsigned i)
298  {
299  assert(i < _size);
300  return _data[i];
301  }
302 
304  const T& operator () (unsigned i) const
305  {
306  assert(i < _size);
307  return _data[i];
308  }
309 
311  T& first()
312  {
313  assert( _size > 0);
314  return _data[0];
315  }
316 
318  const T& first() const
319  {
320  assert( _size > 0);
321  return _data[0];
322  }
323 
324 
325 
327  T& last()
328  {
329  assert( _size > 0);
330  return _data[_size-1];
331  }
332 
334  const T& last() const
335  {
336  assert( _size > 0);
337  return _data[_size-1];
338  }
339 
341  T& x()
342  {
343  assert( _size > 0);
344  return _data[0];
345  }
346 
348  const T& x() const
349  {
350  assert( _size > 0);
351  return _data[0];
352  }
353 
355  T& y()
356  {
357  assert( _size > 1);
358  return _data[1];
359  }
360 
362  const T& y() const
363  {
364  assert( _size > 1);
365  return _data[1];
366  }
367 
369  T& z()
370  {
371  assert( _size > 2);
372  return _data[2];
373  }
374 
376  const T& z() const
377  {
378  assert( _size > 2);
379  return _data[2];
380  }
381 
383  T& w()
384  {
385  assert( _size > 3);
386  return _data[3];
387  }
388 
390  const T& w() const
391  {
392  assert( _size > 3);
393  return _data[3];
394  }
395 
397  vec<T>& operator += (const T& s)
398  {
399  for (unsigned i=0;i<_size;++i)
400  _data[i] += s;
401  return *this;
402  }
403 
405  vec<T>& operator -= (const T& s)
406  {
407  for (unsigned i=0;i<_size; ++i)
408  _data[i] -= s;
409  return *this;
410  }
411 
413  vec<T>& operator *= (const T& s)
414  {
415  for (unsigned i=0;i<_size;++i) _data[i] *= s; return *this;
416  }
417 
419  vec<T>& operator /= (const T& s)
420  {
421  for (unsigned i=0;i<_size;++i) _data[i] /= s; return *this;
422  }
423 
425  template <typename S>
427  {
428  assert(_size == v._size);
429  for (unsigned i=0;i<_size;++i) _data[i] += v(i); return *this;
430  }
431 
433  template <typename S>
435  {
436  assert(_size == v._size);
437  for (unsigned i=0;i<_size;++i) _data[i] -= v(i); return *this;
438  }
439 
441  template <typename S>
443  {
444  assert(_size == v._size);
445  for (unsigned i=0;i<_size;++i) _data[i] *= v(i); return *this;
446  }
447 
449  template <typename S>
451  {
452  assert(_size == v._size);
453  for (unsigned i=0;i<_size;++i) _data[i] /= v(i); return *this;
454  }
455 
457  template <typename S>
458  const vec<T> operator + (const vec<S>& v) const
459  {
460  vec<T> r = *this; r += v; return r;
461  }
462 
464  const vec<T> operator + (const T& s) const
465  {
466  vec<T> r = *this; r += s; return r;
467  }
468 
470  const vec<T> operator - (const T& s) const
471  {
472  vec<T> r = *this; r -= s; return r;
473  }
474 
476  template <typename S>
477  vec<T> operator - (const vec<S>& v) const
478  {
479  vec<T> r = *this; r -= v; return r;
480  }
481 
483  template <typename S>
484  const vec<T> operator * (const vec<S>& v) const
485  {
486  vec<T> r = *this; r *= v; return r;
487  }
488 
489 
491  template <typename S>
492  const vec<T> operator / (const vec<S>& v) const
493  {
494  vec<T> r = *this; r /= v; return r;
495  }
496 
497 
498 
500  vec<T> operator-(void) const
501  {
502  vec<T> r=(*this);
503  r=(T)(-1)*r;
504  return r;
505  }
506 
508  vec<T> operator * (const T& s) const
509  {
510  vec<T> r = *this; r *= s; return r;
511  }
512 
513 
515  vec<T> operator / (const T& s)const
516  {
517  vec<T> r = *this;
518  r /= s;
519  return r;
520  }
521 
522 
524  void fill(const T& v)
525  {
526  for (unsigned i=0; i<_size; ++i)
527  _data[i]= (T)v;
528  }
529 
531  void zeros()
532  {
533  fill((T)0);
534  }
535 
537  void ones()
538  {
539  fill((T)1);
540  }
541 
543  void zeros(unsigned n)
544  {
545  resize(n);
546  fill((T)0);
547  }
548 
550  void ones(unsigned n)
551  {
552  resize(n);
553  fill((T)1);
554  }
555 
557  void resize(unsigned dim)
558  {
559 
560  if(_data)
561  {
562  if(dim != _size)
563  {
564  destruct();
565  _size=dim;
566  if(dim > 0)
567  _data = new T[dim];
568  else
569  _data = NULL;
570  data_is_external = false;
571  }
572  }else
573  {
574  _size=dim;
575  if(dim > 0)
576  _data = new T[dim];
577  else
578  _data = NULL;
579  data_is_external = false;
580  }
581  }
582 
584  template <typename S>
585  bool operator == (const vec<S>& v) const
586  {
587  for (unsigned i=0;i<_size;++i)
588  if(operator()(i) != (T)v(i)) return false;
589  return true;
590  }
591 
593  template <typename S>
594  bool operator != (const vec<S>& v) const
595  {
596  for (unsigned i=0;i<_size;++i)
597  if(operator()(i) != (T)v(i)) return true;
598  return false;
599  }
600 
602  T length() const
603  {
604  return (T)sqrt((double)sqr_length());
605  }
606 
608  void abs()
609  {
610  if(std::numeric_limits<T>::is_signed)
611  {
612  for(unsigned i = 0; i < _size;i++)
613  _data[i]=(T)std::abs((double)_data[i]);
614  }
615  }
616 
618  void ceil()
619  {
620  for(unsigned i = 0; i < _size;i++)
621  _data[i]=(T)::ceil((double)_data[i]);
622  }
623 
625  void floor()
626  {
627  for(unsigned i = 0; i < _size;i++)
628  _data[i]=(T)::floor((double)_data[i]);
629  }
630 
632  void round()
633  {
634  for(unsigned i = 0; i < _size;i++)
635  _data[i]=(T)::floor((double)_data[i]+0.5);
636  }
637 
638 
640  T sqr_length() const
641  {
642  T l=0;
643  for(unsigned i = 0; i!=_size;i++)
644  l+= operator()(i)*operator()(i);
645  return l;
646  }
647 
649  void normalize()
650  {
651  T l = (T)1.0/length();
652  for(unsigned i = 0; i<_size; i++)
653  operator()(i)=l*operator()(i);
654  }
655 
657  vec<T> sub_vec(unsigned ifrom, unsigned size) const
658  {
659 
660  vec<T> vnew(size);
661 
662  for(unsigned i = 0; i < size; i++)
663  vnew(i)=operator()(i+ifrom);
664 
665  return vnew;
666  }
667 
669  void copy(unsigned ifrom, unsigned s,vec<T>& subvec) const
670  {
671  assert(subvec.size() == s);
672  assert(ifrom+s <=size());
673 
674  for(unsigned i = 0; i < s; i++)
675  subvec(i)=operator()(i+ifrom);
676  }
677 
679  void paste(unsigned ifrom,const vec<T>& v)
680  {
681 
682  assert(ifrom+v.size() <= size());
683  for(unsigned i = 0; i < v.size(); i++)
684  operator()(i+ifrom) = v(i);
685  }
686 
687 
688 
689 };
690 
692 template<typename T>
694 {
695  vec<T> r = v;
696  r.normalize();
697  return r;
698 }
699 
700 
702 template <typename T,typename S>
703 T p_norm(const vec<T>& values,const S& p=1)
704 {
705  assert(p > 0);
706  unsigned N = values.size();
707 
708  T n=0;
709 
710  for(unsigned i = 0; i < N;i++)
711  {
712  n+=pow(fabs(values(i)),(T)p);
713  }
714 
715  return pow(n,(T)(1.0/(T)p));
716 }
717 
719 template <typename T>
720 T inf_norm(const vec<T>& values)
721 {
722  vec<T> r = abs(values);
723 
724  return max_value(r);
725 }
726 
727 
729 template<typename T>
730 T length(const vec<T>& v)
731 {
732  return v.length();
733 }
734 
736 template<typename T>
737 T sqr_length(const vec<T>& v)
738 {
739  return v.sqr_length();
740 }
741 
743 template<typename T>
744 std::ostream& operator<<(std::ostream& out, const vec<T>& v)
745 {
746 
747  for (unsigned i=0;i<v.size()-1;++i)
748  {
749  out << v(i)<<" ";
750  }
751  out << v(v.size()-1);
752  return out;
753 
754 }
755 
757 template<typename T>
758 std::istream& operator>>(std::istream& in, vec<T>& v)
759 {
760 
761  for (unsigned i=0;i<v.size();++i)
762  {
763  in >> v(i);
764  }
765 
766  return in;
767 
768 }
769 
770 
772 template <typename T>
773 const vec<T> operator * (const T& s, const vec<T>& v)
774 {
775  vec<T> r = v; r *= s; return r;
776 }
777 
779 template <typename T>
780 inline T dot(const vec<T>& v, const vec<T>& w)
781 {
782  T r = 0;
783  for (unsigned i=0;i<v.size();++i) r += v(i)*(T)w(i);
784  return r;
785 }
786 
788 template < typename T>
789 inline vec<T> cross(const vec<T>& v, const vec<T>& w)
790 {
791  vec<T> r(3);
792  r(0)= v(1)*(T)w(2) - v(2)*(T)w(1);
793  r(1)= -v(0)*(T)w(2) + v(2)*(T)w(0);
794  r(2)= v(0)*(T)w(1) - v(1)*(T)w(0);
795  return r;
796 }
797 
799 template < typename T, typename S, typename U>
800 vec<T> dbl_cross(const vec<T> &a, const vec<S> &b, vec<U> &c)
801 {
802  return dot(a,c)*b - dot(a,b)*c;
803 }
804 
806 template < typename T, typename S, typename U>
807 T spat(const vec<T> &a,const vec<S> &b,const vec<U> &c)
808 {
809  return dot(cross(a,b),c);
810 }
811 
813 template <typename T>
814 vec<T> project(const vec<T> &v, const vec<T> &n)
815 {
816  return dot(v,n)/dot(n,n)*n;
817 }
818 
819 
821 template <typename T>
822 vec<T> reflect(const vec<T> &v, const vec<T> &n)
823 {
824  return v-(T)2.0*dot(v,n)/dot(n,n)*n;
825 }
826 
829 template <typename T>
830 vec<T> refract(const vec<T> &v,const vec<T> &n,T c1, T c2,bool* total_reflection=NULL)
831 {
832 
833  T NdotV =-dot(n,v)/dot(n,n);
834  T c = c2/c1;
835 
836  T cosasqr = (T)1.0-(c*c)*((T)1.0-NdotV*NdotV);
837 
838  //total reflection
839  if(cosasqr < 0)
840  {
841  if(total_reflection)
842  *total_reflection=true;
843  return reflect(v,n);
844  }
845  else
846  {
847  if(total_reflection)
848  *total_reflection=false;
849  return c*v + (c*NdotV - sqrt(cosasqr)/dot(n,n) )*n;
850 
851  }
852 
853 }
854 
855 
856 
858 template <typename T>
859 vec<T> zeros(const unsigned dim)
860 {
861  vec<T> v;
862  v.zeros(dim);
863  return v;
864 }
865 
867 template <typename T>
868 vec<T> ones(const unsigned dim)
869 {
870  vec<T> v;
871  v.ones(dim);
872  return v;
873 }
874 
875 
877 template <typename T>
878 vec<T> floor(const vec<T> &v)
879 {
880  vec<T> r(v.size());
881  for(unsigned i = 0; i < v.size();i++)
882  r(i)=::floor((T)v(i));
883 
884  return r;
885 }
886 
887 
889 template <typename T>
890 vec<T> ceil(const vec<T> &v)
891 {
892  vec<T> r(v.size());
893  for(unsigned i = 0; i < v.size();i++)
894  r(i)=::ceil(v(i));
895 
896  return r;
897 }
898 
900 template <typename T>
901 vec<T> round(const vec<T> &v)
902 {
903  vec<T> r(v.size());
904  for(unsigned i = 0; i < v.size();i++)
905  r(i)=::floor(v(i)+0.5);
906 
907  return r;
908 }
909 
910 
912 template <typename T>
913 vec<T> abs(const vec<T> &v)
914 {
915  vec<T> r(v.size());
916  for(unsigned i = 0; i < v.size();i++)
917  r(i)=std::abs(v(i));
918 
919  return r;
920 }
921 
923 template <typename T>
924 T min_value(const vec<T> &v)
925 {
926  return *(std::min_element(&v(0),&v(v.size()-1)+1));
927 }
928 
929 
930 
932 template <typename T>
933 unsigned min_index(const vec<T> &v)
934 {
935  return (unsigned) (std::min_element(&v(0),&v(v.size()-1)+1)-&v(0));
936 }
937 
939 template <typename T>
940 unsigned max_index(const vec<T> &v)
941 {
942  return (unsigned) (std::max_element(&v(0),&v(v.size()-1)+1)-&v(0));
943 }
944 
946 template <typename T>
947 T max_value(const vec<T> &v)
948 {
949  return *(std::max_element(&v(0),&v(v.size()-1)+1));
950 }
951 
952 
954 template<typename T>
955 T mean_value(const vec<T>& values)
956 {
957 
958  unsigned N = values.size();
959 
960  T mu=0;
961 
962  for(unsigned i = 0; i < N;i++)
963  {
964  mu+=values(i);
965  }
966  mu/=(T)N;
967  return mu;
968 }
969 
972 template<typename T>
973 T var_value(const vec<T>& values)
974 {
975  unsigned N = values.size();
976 
977  T mu=mean_value(values);
978  T v = 0;
979 
980  for(unsigned i = 0; i < N;i++)
981  {
982  v+=(values(i)-mu)*(values(i)-mu);
983  }
984 
985  if(N > 1)
986  v/=(T)(N-1);
987 
988  return v;
989 
990 }
991 
993 template<typename T>
994 T range_value(const vec<T>& values)
995 {
996  return max_value(values)-min_value(values);
997 }
998 
1000 template<typename T>
1001 T mad_value(const vec<T>& values)
1002 {
1003  return median_value(abs(values-median_value(values)));
1004 }
1005 
1006 
1009 template<typename T>
1010 T std_value(const vec<T>& values)
1011 
1012 {
1013  T v = var_value(values);
1014  return sqrt(v);
1015 }
1016 
1017 
1018 
1020 template<typename T>
1021 void var_and_mean_value(const vec<T>& values, T& mu, T&var)
1022 {
1023 
1024  unsigned N = values.size();
1025  mu=0;
1026 
1027  for(unsigned i = 0; i < N;i++)
1028  {
1029  mu+=values(i);
1030  }
1031 
1032  mu/=(T)N;
1033  var = 0;
1034  for(unsigned i = 0; i < N;i++)
1035  {
1036  var+=sqr(values(i)-mu);
1037  }
1038 
1039  var/=(T)(N-1);
1040 }
1041 
1042 
1044 template <typename T>
1045 void sort_values(vec<T>& values, bool ascending=true)
1046 {
1047 
1048  if(ascending)
1049  std::sort(&values(0),&values(values.size()-1)+1,std::less<T>());
1050  else
1051  std::sort(&values(0),&values(values.size()-1)+1,std::greater<T>());
1052 }
1053 
1054 
1055 
1057 template <typename T>
1058 T sum_values(const vec<T>& values)
1059 {
1060  T v =0;
1061  for(unsigned i = 0; i < values.size(); i++)
1062  v+=values(i);
1063  return v;
1064 }
1065 
1069 template <typename T>
1070 T cumsum_values(const vec<T>& values, vec<T>& cumsumvalues)
1071 {
1072  cumsumvalues.resize(values.size());
1073  T v =0;
1074  for(unsigned i = 0; i < values.size(); i++)
1075  {
1076  cumsumvalues(i)=v;
1077  v+=values(i);
1078  }
1079  return v;
1080 }
1081 
1082 
1083 
1085 template <typename T>
1087 {
1088  T v =1;
1089  for(unsigned i = 0; i < values.size(); i++)
1090  v*=values(i);
1091  return v;
1092 }
1093 
1094 
1099 template <typename T>
1100 T select_value(unsigned k, vec<T>& values)
1101 {
1102  if (k >= values.size())
1103  k = values.size()-1;
1104  std::nth_element(&values(0),&values(k),&values(values.size()-1)+1);
1105  return values(k);
1106 }
1107 
1108 
1109 
1115 template <typename T>
1117 {
1118  return select_value((values.size())/2,values);
1119 }
1120 
1121 
1122 
1124 template <typename T>
1125 T median_value(const vec<T>& values)
1126 {
1127  vec<T> c = values;
1128  return select_value((c.size())/2,c);
1129 }
1130 
1131 
1132 
1134 template <typename T>
1135 const vec<T> lin_space(const T& first_val, const T& last_val, unsigned N=10)
1136 {
1137  vec<T> lv(N);
1138  if(N == 1)
1139  {
1140  lv(0) = last_val;
1141  return lv;
1142  }
1143  T diff = last_val-first_val;
1144 
1145  for(unsigned i = 0; i < N; i++)
1146  {
1147  lv(i) = first_val + i*diff/((T)N-(T)1.0);
1148  }
1149  return lv;
1150 }
1151 
1152 //create N chebychev sample points for interval [first_val,last_val]
1153 template <typename T>
1154 const vec<T> cheb_points(const T& first_val, const T& last_val,unsigned N=10)
1155 {
1156  vec<T> lv(N) ;
1157  if(N == 1)
1158  {
1159  lv(0) = (T)(first_val+last_val)/2.0;
1160  return lv;
1161  }
1162  T diff = (last_val-first_val)/(T)2.0;
1163 
1164  for(unsigned i = 0; i < N; i++)
1165  {
1166  lv(i) = diff*((first_val+1.0)-cos((T)((2*i+1)*3.14159)/((T)(2.0*(N-1)+2))));
1167  }
1168  return lv;
1169 }
1170 
1171 
1172 
1175 template <typename T>
1176 const vec<T> log_space(const T& first_pow_of_10, const T& last_pow_of_10, unsigned N=10)
1177 {
1178 
1179  vec<T> lv(N);
1180  if(N == 1)
1181  {
1182  lv(0) = pow((T)10.0,last_pow_of_10);
1183  return lv;
1184  }
1185  T diff = last_pow_of_10 - first_pow_of_10;
1186 
1187  for(unsigned i = 0; i < N; i++)
1188  {
1189  lv(i) = pow((T)10.0,(T)first_pow_of_10 + (T)i*diff/((T)N-(T)1.0));
1190  }
1191  return lv;
1192 }
1193 
1194 
1195 
1197 template <typename T>
1198 const vec<T> lerp(const vec<T>& v1, const vec<T>& v2, T t)
1199 {
1200  return (1-t)*v1+t*v2;
1201 }
1202 
1203 
1204 
1206 template <typename T>
1207 const T lerp(const T& s1, const T& s2, T t)
1208 {
1209  return (1-t)*s1+t*s2;
1210 }
1211 
1212 
1213 
1215 template <typename T>
1216 const vec<T> slerp(const vec<T>& v0, const vec<T>& v1, T t)
1217 {
1218  T dotv0v1 = dot(v0,v1);
1219  //clamp between [-1,1]
1220  if(dotv0v1 < -1)
1221  dotv0v1 = -1;
1222 
1223  if(dotv0v1 > 1)
1224  dotv0v1 = 1;
1225 
1226  T theta = acos(dotv0v1)*t;
1227  cgv::math::vec<T> v2 = normalize(v1 - (dotv0v1)*v0);
1228  return cos(theta)*v0 + sin(theta)*v2;
1229 }
1230 
1231 
1232 
1233  }
1234 }
cgv::math::vec::vec
vec(unsigned dim, const T &value=T(0))
creates a vector with dim elements
Definition: vec.h:117
cgv::math::refract
vec< T > refract(const vec< T > &v, const vec< T > &n, T c1, T c2, bool *total_reflection=NULL)
Definition: vec.h:830
cgv::math::vec::w
T & w()
element accessor for the fourth element
Definition: vec.h:383
cgv::math::vec::vec
vec(const T &c0, const T &c1, const T &c2, const T &c3)
creates a 4d vector (c0,c1,c2,c3)^T
Definition: vec.h:194
cgv::math::cumsum_values
T cumsum_values(const vec< T > &values, vec< T > &cumsumvalues)
Definition: vec.h:1070
cgv::math::vec::z
T & z()
element accessor for the third element
Definition: vec.h:369
cgv::math::vec::resize
void resize(unsigned dim)
resize the vector
Definition: vec.h:557
cgv::math::vec::operator/=
vec< T > & operator/=(const T &s)
in place division by scalar s
Definition: vec.h:419
cgv::math::vec::operator()
T & operator()(unsigned i)
element accessor
Definition: vec.h:297
cgv::math::vec::~vec
virtual ~vec()
destructor
Definition: vec.h:240
cgv::math::lin_space
const vec< T > lin_space(const T &first_val, const T &last_val, unsigned N=10)
create a linearly spaced vector with N values starting at first_val and ending ant last_val
Definition: vec.h:1135
cgv::math::var_value
T var_value(const vec< T > &values)
Definition: vec.h:973
cgv::math::vec::w
const T & w() const
const element accessor for the fourth element
Definition: vec.h:390
cgv::math::vec::length
T length() const
length of the vector L2-Norm
Definition: vec.h:602
cgv::math::vec::sub_vec
vec< T > sub_vec(unsigned ifrom, unsigned size) const
extracts sub vector beginning at index ifrom with given size
Definition: vec.h:657
cgv::math::cross
fvec< T, N > cross(const fvec< T, N > &v, const fvec< T, N > &w)
returns the cross product of vector v and w
Definition: fvec.h:338
cgv::math::vec::set
void set(const T &c0, const T &c1)
set entries of a 2d vector
Definition: vec.h:206
cgv::math::round
fvec< T, N > round(const fvec< T, N > &v)
apply round function component wise to vector
Definition: fvec.h:318
cgv::math::vec::z
const T & z() const
const element accessor for the third element
Definition: vec.h:376
cgv::math::vec::paste
void paste(unsigned ifrom, const vec< T > &v)
paste v into vector beginning at index pos ifrom
Definition: vec.h:679
cgv::math::vec::vec
vec(const vec< T > &v)
copy constructor for vectors with equal element type
Definition: vec.h:144
cgv::math::vec::operator+
const vec< T > operator+(const vec< S > &v) const
vector addition
Definition: vec.h:458
cgv::math::vec::_data
T * _data
pointer to _data storage
Definition: vec.h:32
cgv::math::vec::floor
void floor()
floor componentwise
Definition: vec.h:625
cgv::math::std_value
T std_value(const vec< T > &values)
Definition: vec.h:1010
cgv::math::vec::set_extern_data
void set_extern_data(unsigned dim, T *data)
set data pointer to an external data array
Definition: vec.h:232
cgv::math::vec::set
void set(const T &c0, const T &c1, const T &c2)
set entries of a 3d vector
Definition: vec.h:214
cgv::math::vec::vec
vec(unsigned dim, const T *marray)
creates a vector with dim elements from an array
Definition: vec.h:131
cgv::math::vec
A column vector class.
Definition: fvec.h:13
cgv::math::vec::operator+=
vec< T > & operator+=(const T &s)
in place addition of a scalar s
Definition: vec.h:397
cgv::math::mad_value
T mad_value(const vec< T > &values)
compute the median absolut deviation MAD
Definition: vec.h:1001
cgv::math::vec::last
const T & last() const
const element accessor for the last element
Definition: vec.h:334
cgv::math::vec::operator-=
vec< T > & operator-=(const T &s)
in place subtraction by scalar s
Definition: vec.h:405
cgv::math::vec::vec
vec(const T &c0, const T &c1)
creates a 3d vector (c0,c1,c2)^T
Definition: vec.h:172
cgv::math::vec::size
unsigned size() const
number of elements
Definition: vec.h:97
cgv::math::vec::zeros
void zeros(unsigned n)
resize the vector to size n and fills the vector with zeros
Definition: vec.h:543
cgv::math::vec::ones
void ones()
fill the vector with ones
Definition: vec.h:537
cgv::math::p_norm
T p_norm(const vec< T > &values, const S &p=1)
return the p-norm of the vector default is p == 1
Definition: vec.h:703
cgv::math::vec::copy
void copy(unsigned ifrom, unsigned s, vec< T > &subvec) const
copy sub vector beginning at index ifrom with given size s into subvec
Definition: vec.h:669
cgv::math::vec::first
const T & first() const
const element accessor for the first element
Definition: vec.h:318
cgv::math::range_value
T range_value(const vec< T > &values)
compute the range of all values
Definition: vec.h:994
cgv::math::floor
fvec< T, N > floor(const fvec< T, N > &v)
apply floor function component wise to vector
Definition: fvec.h:322
cgv::math::var_and_mean_value
void var_and_mean_value(const vec< T > &values, T &mu, T &var)
compute the mean and the variance (sigma^2) of all values
Definition: vec.h:1021
cgv::math::vec::normalize
void normalize()
normalize the vector using the L2-Norm
Definition: vec.h:649
cgv::math::vec::last
T & last()
element accessor for the flast element
Definition: vec.h:327
cgv::math::project
vec< T > project(const vec< T > &v, const vec< T > &n)
calculates the projection of v onto n
Definition: vec.h:814
cgv::math::operator<<
std::ostream & operator<<(std::ostream &out, const diag_mat< T > &m)
output of a diagonal matrix onto an ostream
Definition: diag_mat.h:442
cgv::math::mean_value
T mean_value(const vec< T > &values)
compute the mean of all values
Definition: vec.h:955
cgv::math::max_index
unsigned max_index(const fvec< T, N > &v)
return the index of the largest entry
Definition: fvec.h:374
cgv::math::abs
fvec< T, N > abs(const fvec< T, N > &v)
apply abs function component wise to vector
Definition: fvec.h:314
cgv::math::vec::operator=
vec< T > & operator=(const vec< T > &v)
assignment of a vector v
Definition: vec.h:268
cgv::math::vec::vec
vec(const vec< S > &v)
copy constructor for vectors with different element type
Definition: vec.h:158
cgv::math::vec::sqr_length
T sqr_length() const
square length of vector
Definition: vec.h:640
cgv::math::vec::data_is_external
bool data_is_external
store whether data is not owned by vector
Definition: vec.h:36
cgv::math::vec::round
void round()
round componentwise
Definition: vec.h:632
cgv::math::vec::x
T & x()
element accessor for the first element
Definition: vec.h:341
cgv::math::lerp
const fmat< T, N, M > lerp(const fmat< T, N, M > &m1, const fmat< T, N, M > &m2, T t)
linear interpolation returns (1-t)*m1 + t*m2
Definition: fmat.h:259
cgv::math::vec::dim
unsigned dim() const
number of elements
Definition: vec.h:103
cgv::math::vec::fill
void fill(const T &v)
fill elements of vector with scalar v
Definition: vec.h:524
cgv::math::min_value
T min_value(const fvec< T, N > &v)
returns the minimal entry
Definition: fvec.h:360
cgv::math::ceil
fvec< T, N > ceil(const fvec< T, N > &v)
apply ceil function component wise to vector
Definition: fvec.h:326
cgv::math::sqr_length
T sqr_length(const fvec< T, N > &v)
returns the squared length of vector v
Definition: fvec.h:330
cgv::math::sort_values
void sort_values(vec< T > &values, bool ascending=true)
sort vector elements in ascending or descending order
Definition: vec.h:1045
cgv::math::operator>>
std::istream & operator>>(std::istream &in, diag_mat< T > &m)
input of a diagonal matrix from an istream
Definition: diag_mat.h:453
cgv::math::vec::ones
void ones(unsigned n)
resize the vector to size n and fills thevector with ones
Definition: vec.h:550
cgv::math::select_median_value
T select_median_value(vec< T > &values)
Definition: vec.h:1116
cgv::math::vec::zeros
void zeros()
fill the vector with zeros
Definition: vec.h:531
cgv::math::log_space
const vec< T > log_space(const T &first_pow_of_10, const T &last_pow_of_10, unsigned N=10)
Definition: vec.h:1176
cgv::math::max_value
T max_value(const fvec< T, N > &v)
return the value of the largest entry
Definition: fvec.h:381
cgv::math::sum_values
T sum_values(const vec< T > &values)
returns the sum of all entries
Definition: vec.h:1058
cgv::math::vec::abs
void abs()
componentwise absolute values
Definition: vec.h:608
cgv::math::reflect
vec< T > reflect(const vec< T > &v, const vec< T > &n)
calculates the reflected direction of v; n is the normal of the reflecting surface
Definition: vec.h:822
cgv::math::vec::set
void set(const T &c0, const T &c1, const T &c2, const T &c3)
set entries of a 4d vector
Definition: vec.h:223
cgv::math::vec::operator/
const vec< T > operator/(const vec< S > &v) const
componentwise vector division
Definition: vec.h:492
cgv::math::vec::y
const T & y() const
const element accessor for the second element
Definition: vec.h:362
cgv::math::prod_values
T prod_values(vec< T > &values)
returns the product of all entries
Definition: vec.h:1086
cgv::math::vec::x
const T & x() const
const element accessor for the first element
Definition: vec.h:348
cgv::math::dot
T dot(const fvec< T, N > &v, const fvec< T, N > &w)
returns the dot product of vector v and w
Definition: fvec.h:300
cgv::math::vec::vec
vec(const T &c0, const T &c1, const T &c2)
creates a 3d vector (c0,c1,c2)^T
Definition: vec.h:183
cgv::math::vec::operator*=
vec< T > & operator*=(const T &s)
in place multiplication with s
Definition: vec.h:413
cgv::math::slerp
const vec< T > slerp(const vec< T > &v0, const vec< T > &v1, T t)
spherical linear interpolation
Definition: vec.h:1216
cgv::math::vec::operator!=
bool operator!=(const vec< S > &v) const
test for inequality
Definition: vec.h:594
cgv::math::vec::vec
vec()
standard constructor
Definition: vec.h:109
cgv::math::vec::operator*
const vec< T > operator*(const vec< S > &v) const
componentwise vector multiplication
Definition: vec.h:484
cgv::math::vec::operator-
vec< T > operator-(void) const
negates the vector
Definition: vec.h:500
cgv::math::dbl_cross
vec< T > dbl_cross(const vec< T > &a, const vec< S > &b, vec< U > &c)
returns the double cross product of vector a, b and c a x(b x c)
Definition: vec.h:800
cgv::math::vec::ceil
void ceil()
ceil componentwise
Definition: vec.h:618
cgv::math::vec::operator==
bool operator==(const vec< S > &v) const
test for equality
Definition: vec.h:585
cgv::math::median_value
T median_value(const vec< T > &values)
returns median element without modifying the given vector
Definition: vec.h:1125
cgv::math::select_value
T select_value(unsigned k, vec< T > &values)
Definition: vec.h:1100
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::math::vec::y
T & y()
element accessor for the second element
Definition: vec.h:355
cgv::math::min_index
unsigned min_index(const fvec< T, N > &v)
returns the index of the smallest value
Definition: fvec.h:367
cgv::math::normalize
fvec< T, N > normalize(const fvec< T, N > &v)
return normalized vector
Definition: fvec.h:272
cgv::math::length
T length(const fvec< T, N > &v)
returns the length of vector v
Definition: fvec.h:310
cgv::math::vec::_size
unsigned _size
number or elements
Definition: vec.h:34
cgv::math::vec::first
T & first()
element accessor for the first element
Definition: vec.h:311
cgv::math::inf_norm
T inf_norm(const vec< T > &values)
return the infinity norm of the vector
Definition: vec.h:720
cgv::math::spat
T spat(const vec< T > &a, const vec< S > &b, const vec< U > &c)
returns the spat product (mixed vector product) of the vectors a, b and c
Definition: vec.h:807