cgv
Loading...
Searching...
No Matches
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
21namespace cgv {
22namespace math {
23
24
26template <typename T>
27class vec
28{
29
30protected:
34 unsigned _size;
37public:
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 typedef T* iterator;
46 typedef const T* const_iterator;
47 typedef std::reverse_iterator<iterator> reverse_iterator;
48 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
49
50 iterator begin() { return _data; }
51 iterator end() { return _data+_size; }
52 const_iterator begin() const { return _data; }
53 const_iterator end() const { return _data+_size; }
54 reverse_iterator rbegin() { return reverse_iterator(end()); }
55 reverse_iterator rend() { return reverse_iterator(begin()); }
56 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
57 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
59 unsigned size() const { return _size; }
61 unsigned dim() const { return _size; }
63 vec() : _data(0), _size(0), data_is_external(false) {}
65 explicit vec(unsigned dim, const T& value = T(0)) {
66 _size = dim;
67 if (dim > 0) {
68 _data = new T[_size];
69 std::fill(_data, _data + _size, value);
70 }
71 else
72 _data = 0;
73 data_is_external = false;
74 }
76 vec(unsigned dim, const T* marray) {
77 _size = dim;
78 if(dim > 0) {
79 _data = new T[_size];
80 memcpy(_data,marray,_size*sizeof(T));
81 }
82 else
83 _data = NULL;
84 data_is_external = false;
85 }
87 vec(const vec<T>&v) {
88 _size = v.size();
89 if (v._data) {
90 _data = new T[v.size()];
91 memcpy(_data,v._data,_size*sizeof(T));
92 }
93 else
94 _data=NULL;
95 data_is_external = false;
96 }
98 template <typename S>
99 vec(const vec<S>& v) {
100 _size = v.size();
101 if(v.size() > 0) {
102 _data = new T[v.size()];
103 for(unsigned i = 0; i < _size;i++)
104 _data [i]=(T)v(i);
105 }
106 else
107 _data =NULL;
108 data_is_external = false;
109 }
111 vec(const T& c0, const T& c1) {
112 _size = 2;
113 _data = new T[2];
114 _data[0] = c0;
115 _data[1] = c1;
116 data_is_external = false;
117 }
119 vec(const T& c0, const T& c1, const T& c2)
120 {
121 _size = 3;
122 _data = new T[3];
123 _data[0] = c0;
124 _data[1] = c1;
125 _data[2] = c2;
126 data_is_external = false;
127 }
128
130 vec(const T& c0, const T& c1, const T& c2, const T& c3)
131 {
132 _size = 4;
133 _data = new T[4];
134 _data[0] = c0;
135 _data[1] = c1;
136 _data[2] = c2;
137 _data[3] = c3;
138 data_is_external = false;
139 }
140
142 void set(const T& c0, const T& c1)
143 {
144 assert(_size == 2);
145 _data[0] = c0;
146 _data[1] = c1;
147 }
148
150 void set(const T& c0, const T& c1, const T& c2)
151 {
152 assert(_size == 3);
153 _data[0] = c0;
154 _data[1] = c1;
155 _data[2] = c2;
156 }
157
159 void set(const T& c0, const T& c1, const T& c2, const T& c3)
160 {
161 assert(_size == 4);
162 _data[0] = c0;
163 _data[1] = c1;
164 _data[2] = c2;
165 _data[3] = c3;
166 }
168 void set_extern_data(unsigned dim, T* data)
169 {
170 destruct();
171 _size = dim;
172 _data = data;
173 data_is_external = true;
174 }
176 virtual ~vec()
177 {
178 destruct();
179 }
180
181 void destruct()
182 {
183 if(_data && !data_is_external)
184 {
185 delete[] _data;
186 _data=NULL;
187 _size=0;
188 }
189 }
190
192 T* data()
193 {
194 return _data;
195 }
196
198 const T* data() const
199 {
200 return _data;
201 }
202
205 {
206 if(v.size() == 0)
207 destruct();
208 else {
209 if (size() != v.size())
210 resize(v.size());
211 memcpy(_data,v._data,_size*sizeof(T));
212 }
213 return *this;
214 }
215
217 vec<T>& operator = (const T& s)
218 {
219 fill(s);
220 return *this;
221 }
222
224 template <typename S>
226 {
227 resize(v.size());
228 for (unsigned i=0;i<_size;++i) _data[i]=(T)v(i);
229 return *this;
230 }
231
233 T& operator () (unsigned i)
234 {
235 assert(i < _size);
236 return _data[i];
237 }
238
240 const T& operator () (unsigned i) const {
241 assert(i < _size);
242 return _data[i];
243 }
245 T& operator [] (unsigned i) {
246 assert(i < _size);
247 return _data[i];
248 }
250 const T& operator [] (unsigned i) const {
251 assert(i < _size);
252 return _data[i];
253 }
255 T& first()
256 {
257 assert( _size > 0);
258 return _data[0];
259 }
260
262 const T& first() const
263 {
264 assert( _size > 0);
265 return _data[0];
266 }
268 T& last()
269 {
270 assert( _size > 0);
271 return _data[_size-1];
272 }
274 const T& last() const
275 {
276 assert( _size > 0);
277 return _data[_size-1];
278 }
280 T& x()
281 {
282 assert( _size > 0);
283 return _data[0];
284 }
285
287 const T& x() const
288 {
289 assert( _size > 0);
290 return _data[0];
291 }
292
294 T& y()
295 {
296 assert( _size > 1);
297 return _data[1];
298 }
299
301 const T& y() const
302 {
303 assert( _size > 1);
304 return _data[1];
305 }
306
308 T& z()
309 {
310 assert( _size > 2);
311 return _data[2];
312 }
313
315 const T& z() const
316 {
317 assert( _size > 2);
318 return _data[2];
319 }
320
322 T& w()
323 {
324 assert( _size > 3);
325 return _data[3];
326 }
327
329 const T& w() const
330 {
331 assert( _size > 3);
332 return _data[3];
333 }
334
336 vec<T>& operator += (const T& s)
337 {
338 for (unsigned i=0;i<_size;++i)
339 _data[i] += s;
340 return *this;
341 }
342
344 vec<T>& operator -= (const T& s)
345 {
346 for (unsigned i=0;i<_size; ++i)
347 _data[i] -= s;
348 return *this;
349 }
350
352 vec<T>& operator *= (const T& s)
353 {
354 for (unsigned i=0;i<_size;++i) _data[i] *= s; return *this;
355 }
356
358 vec<T>& operator /= (const T& s)
359 {
360 for (unsigned i=0;i<_size;++i) _data[i] /= s; return *this;
361 }
362
364 template <typename S>
366 {
367 assert(_size == v._size);
368 for (unsigned i=0;i<_size;++i) _data[i] += v(i); return *this;
369 }
370
372 template <typename S>
374 {
375 assert(_size == v._size);
376 for (unsigned i=0;i<_size;++i) _data[i] -= v(i); return *this;
377 }
378
380 template <typename S>
382 {
383 assert(_size == v._size);
384 for (unsigned i=0;i<_size;++i) _data[i] *= v(i); return *this;
385 }
386
388 template <typename S>
390 {
391 assert(_size == v._size);
392 for (unsigned i=0;i<_size;++i) _data[i] /= v(i); return *this;
393 }
394
396 template <typename S>
397 const vec<T> operator + (const vec<S>& v) const
398 {
399 vec<T> r = *this; r += v; return r;
400 }
401
403 const vec<T> operator + (const T& s) const
404 {
405 vec<T> r = *this; r += s; return r;
406 }
407
409 const vec<T> operator - (const T& s) const
410 {
411 vec<T> r = *this; r -= s; return r;
412 }
413
415 template <typename S>
416 vec<T> operator - (const vec<S>& v) const
417 {
418 vec<T> r = *this; r -= v; return r;
419 }
420
422 template <typename S>
423 const vec<T> operator * (const vec<S>& v) const
424 {
425 vec<T> r = *this; r *= v; return r;
426 }
427
428
430 template <typename S>
431 const vec<T> operator / (const vec<S>& v) const
432 {
433 vec<T> r = *this; r /= v; return r;
434 }
435
436
437
439 vec<T> operator-(void) const
440 {
441 vec<T> r=(*this);
442 r=(T)(-1)*r;
443 return r;
444 }
445
447 vec<T> operator * (const T& s) const
448 {
449 vec<T> r = *this; r *= s; return r;
450 }
451
452
454 vec<T> operator / (const T& s)const
455 {
456 vec<T> r = *this;
457 r /= s;
458 return r;
459 }
460
461
463 void fill(const T& v)
464 {
465 for (unsigned i=0; i<_size; ++i)
466 _data[i]= (T)v;
467 }
468
470 void zeros()
471 {
472 fill((T)0);
473 }
474
476 void ones()
477 {
478 fill((T)1);
479 }
480
482 void zeros(unsigned n)
483 {
484 resize(n);
485 fill((T)0);
486 }
487
489 void ones(unsigned n)
490 {
491 resize(n);
492 fill((T)1);
493 }
494
496 void resize(unsigned dim)
497 {
498
499 if(_data)
500 {
501 if(dim != _size)
502 {
503 destruct();
504 _size=dim;
505 if(dim > 0)
506 _data = new T[dim];
507 else
508 _data = NULL;
509 data_is_external = false;
510 }
511 }else
512 {
513 _size=dim;
514 if(dim > 0)
515 _data = new T[dim];
516 else
517 _data = NULL;
518 data_is_external = false;
519 }
520 }
521
523 template <typename S>
524 bool operator == (const vec<S>& v) const
525 {
526 for (unsigned i=0;i<_size;++i)
527 if(operator()(i) != (T)v(i)) return false;
528 return true;
529 }
530
532 template <typename S>
533 bool operator != (const vec<S>& v) const
534 {
535 for (unsigned i=0;i<_size;++i)
536 if(operator()(i) != (T)v(i)) return true;
537 return false;
538 }
539
541 T length() const
542 {
543 return (T)sqrt((double)sqr_length());
544 }
545
547 void abs()
548 {
549 if(std::numeric_limits<T>::is_signed)
550 {
551 for(unsigned i = 0; i < _size;i++)
552 _data[i]=(T)std::abs((double)_data[i]);
553 }
554 }
555
557 void ceil()
558 {
559 for(unsigned i = 0; i < _size;i++)
560 _data[i]=(T)::ceil((double)_data[i]);
561 }
562
564 void floor()
565 {
566 for(unsigned i = 0; i < _size;i++)
567 _data[i]=(T)::floor((double)_data[i]);
568 }
569
571 void round()
572 {
573 for(unsigned i = 0; i < _size;i++)
574 _data[i]=(T)::floor((double)_data[i]+0.5);
575 }
576
577
579 T sqr_length() const
580 {
581 T l=0;
582 for(unsigned i = 0; i!=_size;i++)
583 l+= operator()(i)*operator()(i);
584 return l;
585 }
586
588 void normalize()
589 {
590 T l = (T)1.0/length();
591 for(unsigned i = 0; i<_size; i++)
592 operator()(i)=l*operator()(i);
593 }
594
597 {
598 T l = length();
599 if(std::abs(l) > std::numeric_limits<T>::epsilon()) {
600 l = (T)1.0 / l;
601 for(unsigned i = 0; i < _size; i++)
602 operator()(i) = l * operator()(i);
603 }
604 }
605
607 vec<T> sub_vec(unsigned ifrom, unsigned size) const
608 {
609
610 vec<T> vnew(size);
611
612 for(unsigned i = 0; i < size; i++)
613 vnew(i)=operator()(i+ifrom);
614
615 return vnew;
616 }
617
619 void copy(unsigned ifrom, unsigned s,vec<T>& subvec) const
620 {
621 assert(subvec.size() == s);
622 assert(ifrom+s <=size());
623
624 for(unsigned i = 0; i < s; i++)
625 subvec(i)=operator()(i+ifrom);
626 }
627
629 void paste(unsigned ifrom,const vec<T>& v)
630 {
631
632 assert(ifrom+v.size() <= size());
633 for(unsigned i = 0; i < v.size(); i++)
634 operator()(i+ifrom) = v(i);
635 }
636
637
638
639};
640
642#define CGV_MATH_VEC_DECLARED
643
645template<typename T>
646vec<T> normalize(const vec<T>& v)
647{
648 vec<T> r = v;
649 r.normalize();
650 return r;
651}
652
653
655template<typename T>
656vec<T> safe_normalize(const vec<T>& v)
657{
658 vec<T> r = v;
659 r.safe_normalize();
660 return r;
661}
662
663
665template <typename T,typename S>
666T p_norm(const vec<T>& values,const S& p=1)
667{
668 assert(p > 0);
669 unsigned N = values.size();
670
671 T n=0;
672
673 for(unsigned i = 0; i < N;i++)
674 {
675 n+=pow(fabs(values(i)),(T)p);
676 }
677
678 return pow(n,(T)(1.0/(T)p));
679}
680
682template <typename T>
683T inf_norm(const vec<T>& values)
684{
685 vec<T> r = abs(values);
686
687 return max_value(r);
688}
689
690
692template<typename T>
693T length(const vec<T>& v)
694{
695 return v.length();
696}
697
699template<typename T>
700T sqr_length(const vec<T>& v)
701{
702 return v.sqr_length();
703}
704
706template<typename T>
707std::ostream& operator<<(std::ostream& out, const vec<T>& v)
708{
709
710 for (unsigned i=0;i<v.size()-1;++i)
711 {
712 out << v(i)<<" ";
713 }
714 out << v(v.size()-1);
715 return out;
716
717}
718
720template<typename T>
721std::istream& operator>>(std::istream& in, vec<T>& v)
722{
723
724 for (unsigned i=0;i<v.size();++i)
725 {
726 in >> v(i);
727 }
728
729 return in;
730
731}
732
733
735template <typename T>
736const vec<T> operator * (const T& s, const vec<T>& v)
737{
738 vec<T> r = v; r *= s; return r;
739}
740
742template <typename T>
743inline T dot(const vec<T>& v, const vec<T>& w)
744{
745 T r = 0;
746 for (unsigned i=0;i<v.size();++i) r += v(i)*(T)w(i);
747 return r;
748}
749
751template < typename T>
752inline vec<T> cross(const vec<T>& v, const vec<T>& w)
753{
754 vec<T> r(3);
755 r(0)= v(1)*(T)w(2) - v(2)*(T)w(1);
756 r(1)= -v(0)*(T)w(2) + v(2)*(T)w(0);
757 r(2)= v(0)*(T)w(1) - v(1)*(T)w(0);
758 return r;
759}
760
762template < typename T, typename S, typename U>
763vec<T> dbl_cross(const vec<T> &a, const vec<S> &b, vec<U> &c)
764{
765 return dot(a,c)*b - dot(a,b)*c;
766}
767
769template < typename T, typename S, typename U>
770T spat(const vec<T> &a,const vec<S> &b,const vec<U> &c)
771{
772 return dot(cross(a,b),c);
773}
774
776template <typename T>
777vec<T> project(const vec<T> &v, const vec<T> &n)
778{
779 return dot(v,n)/dot(n,n)*n;
780}
781
782
784template <typename T>
785vec<T> reflect(const vec<T> &v, const vec<T> &n)
786{
787 return v-(T)2.0*dot(v,n)/dot(n,n)*n;
788}
789
792template <typename T>
793vec<T> refract(const vec<T> &v,const vec<T> &n,T c1, T c2,bool* total_reflection=NULL)
794{
795
796 T NdotV =-dot(n,v)/dot(n,n);
797 T c = c2/c1;
798
799 T cosasqr = (T)1.0-(c*c)*((T)1.0-NdotV*NdotV);
800
801 //total reflection
802 if(cosasqr < 0)
803 {
804 if(total_reflection)
805 *total_reflection=true;
806 return reflect(v,n);
807 }
808 else
809 {
810 if(total_reflection)
811 *total_reflection=false;
812 return c*v + (c*NdotV - sqrt(cosasqr)/dot(n,n) )*n;
813
814 }
815
816}
817
818
819
821template <typename T>
822vec<T> zeros(const unsigned dim)
823{
824 vec<T> v;
825 v.zeros(dim);
826 return v;
827}
828
830template <typename T>
831vec<T> ones(const unsigned dim)
832{
833 vec<T> v;
834 v.ones(dim);
835 return v;
836}
837
838
840template <typename T>
841vec<T> floor(const vec<T> &v)
842{
843 vec<T> r(v.size());
844 for(unsigned i = 0; i < v.size();i++)
845 r(i)=::floor((T)v(i));
846
847 return r;
848}
849
850
852template <typename T>
853vec<T> ceil(const vec<T> &v)
854{
855 vec<T> r(v.size());
856 for(unsigned i = 0; i < v.size();i++)
857 r(i)=::ceil(v(i));
858
859 return r;
860}
861
863template <typename T>
864vec<T> round(const vec<T> &v)
865{
866 vec<T> r(v.size());
867 for(unsigned i = 0; i < v.size();i++)
868 r(i)=::floor(v(i)+0.5);
869
870 return r;
871}
872
873
875template <typename T>
876vec<T> abs(const vec<T> &v)
877{
878 vec<T> r(v.size());
879 for(unsigned i = 0; i < v.size();i++)
880 r(i)=std::abs(v(i));
881
882 return r;
883}
884
886template <typename T>
887T min_value(const vec<T> &v)
888{
889 return *(std::min_element(&v(0),&v(v.size()-1)+1));
890}
891
892
893
895template <typename T>
896unsigned min_index(const vec<T> &v)
897{
898 return (unsigned) (std::min_element(&v(0),&v(v.size()-1)+1)-&v(0));
899}
900
902template <typename T>
903unsigned max_index(const vec<T> &v)
904{
905 return (unsigned) (std::max_element(&v(0),&v(v.size()-1)+1)-&v(0));
906}
907
909template <typename T>
910T max_value(const vec<T> &v)
911{
912 return *(std::max_element(&v(0),&v(v.size()-1)+1));
913}
914
915
917template<typename T>
918T mean_value(const vec<T>& values)
919{
920
921 unsigned N = values.size();
922
923 T mu=0;
924
925 for(unsigned i = 0; i < N;i++)
926 {
927 mu+=values(i);
928 }
929 mu/=(T)N;
930 return mu;
931}
932
935template<typename T>
936T var_value(const vec<T>& values)
937{
938 unsigned N = values.size();
939
940 T mu=mean_value(values);
941 T v = 0;
942
943 for(unsigned i = 0; i < N;i++)
944 {
945 v+=(values(i)-mu)*(values(i)-mu);
946 }
947
948 if(N > 1)
949 v/=(T)(N-1);
950
951 return v;
952
953}
954
956template<typename T>
957T range_value(const vec<T>& values)
958{
959 return max_value(values)-min_value(values);
960}
961
963template<typename T>
964T mad_value(const vec<T>& values)
965{
966 return median_value(abs(values-median_value(values)));
967}
968
969
972template<typename T>
973T std_value(const vec<T>& values)
974
975{
976 T v = var_value(values);
977 return sqrt(v);
978}
979
980
981
983template<typename T>
984void var_and_mean_value(const vec<T>& values, T& mu, T&var)
985{
986
987 unsigned N = values.size();
988 mu=0;
989
990 for(unsigned i = 0; i < N;i++)
991 {
992 mu+=values(i);
993 }
994
995 mu/=(T)N;
996 var = 0;
997 for(unsigned i = 0; i < N;i++)
998 {
999 var+=sqr(values(i)-mu);
1000 }
1001
1002 var/=(T)(N-1);
1003}
1004
1005
1007template <typename T>
1008void sort_values(vec<T>& values, bool ascending=true)
1009{
1010
1011 if(ascending)
1012 std::sort(&values(0),&values(values.size()-1)+1,std::less<T>());
1013 else
1014 std::sort(&values(0),&values(values.size()-1)+1,std::greater<T>());
1015}
1016
1017
1018
1020template <typename T>
1021T sum_values(const vec<T>& values)
1022{
1023 T v =0;
1024 for(unsigned i = 0; i < values.size(); i++)
1025 v+=values(i);
1026 return v;
1027}
1028
1032template <typename T>
1033T cumsum_values(const vec<T>& values, vec<T>& cumsumvalues)
1034{
1035 cumsumvalues.resize(values.size());
1036 T v =0;
1037 for(unsigned i = 0; i < values.size(); i++)
1038 {
1039 cumsumvalues(i)=v;
1040 v+=values(i);
1041 }
1042 return v;
1043}
1044
1045
1046
1048template <typename T>
1049T prod_values(vec<T>& values)
1050{
1051 T v =1;
1052 for(unsigned i = 0; i < values.size(); i++)
1053 v*=values(i);
1054 return v;
1055}
1056
1057
1062template <typename T>
1063T select_value(unsigned k, vec<T>& values)
1064{
1065 if (k >= values.size())
1066 k = values.size()-1;
1067 std::nth_element(&values(0),&values(k),&values(values.size()-1)+1);
1068 return values(k);
1069}
1070
1071
1072
1078template <typename T>
1079T select_median_value( vec<T>& values)
1080{
1081 return select_value((values.size())/2,values);
1082}
1083
1084
1085
1087template <typename T>
1088T median_value(const vec<T>& values)
1089{
1090 vec<T> c = values;
1091 return select_value((c.size())/2,c);
1092}
1093
1094
1095
1097template <typename T>
1098const vec<T> lin_space(const T& first_val, const T& last_val, unsigned N=10)
1099{
1100 vec<T> lv(N);
1101 if(N == 1)
1102 {
1103 lv(0) = last_val;
1104 return lv;
1105 }
1106 T diff = last_val-first_val;
1107
1108 for(unsigned i = 0; i < N; i++)
1109 {
1110 lv(i) = first_val + i*diff/((T)N-(T)1.0);
1111 }
1112 return lv;
1113}
1114
1115//create N chebychev sample points for interval [first_val,last_val]
1116template <typename T>
1117const vec<T> cheb_points(const T& first_val, const T& last_val,unsigned N=10)
1118{
1119 vec<T> lv(N) ;
1120 if(N == 1)
1121 {
1122 lv(0) = (T)(first_val+last_val)/2.0;
1123 return lv;
1124 }
1125 T diff = (last_val-first_val)/(T)2.0;
1126
1127 for(unsigned i = 0; i < N; i++)
1128 {
1129 lv(i) = diff*((first_val+1.0)-cos((T)((2*i+1)*3.14159)/((T)(2.0*(N-1)+2))));
1130 }
1131 return lv;
1132}
1133
1134
1135
1138template <typename T>
1139const vec<T> log_space(const T& first_pow_of_10, const T& last_pow_of_10, unsigned N=10)
1140{
1141
1142 vec<T> lv(N);
1143 if(N == 1)
1144 {
1145 lv(0) = pow((T)10.0,last_pow_of_10);
1146 return lv;
1147 }
1148 T diff = last_pow_of_10 - first_pow_of_10;
1149
1150 for(unsigned i = 0; i < N; i++)
1151 {
1152 lv(i) = pow((T)10.0,(T)first_pow_of_10 + (T)i*diff/((T)N-(T)1.0));
1153 }
1154 return lv;
1155}
1156
1158template <typename T>
1159const vec<T> lerp(const vec<T>& v1, const vec<T>& v2, T t)
1160{
1161 return (1-t)*v1+t*v2;
1162}
1163
1165template <typename T>
1166const vec<T> slerp(const vec<T>& v0, const vec<T>& v1, T t)
1167{
1168 T dotv0v1 = dot(v0,v1);
1169 //clamp between [-1,1]
1170 if(dotv0v1 < -1)
1171 dotv0v1 = -1;
1172
1173 if(dotv0v1 > 1)
1174 dotv0v1 = 1;
1175
1176 T theta = acos(dotv0v1)*t;
1177 cgv::math::vec<T> v2 = normalize(v1 - (dotv0v1)*v0);
1178 return cos(theta)*v0 + sin(theta)*v2;
1179}
1180
1181} // namespace math
1182
1185
1190
1192
1193}// namespace cgv
A column vector class.
Definition vec.h:28
vec()
standard constructor
Definition vec.h:63
bool operator==(const vec< S > &v) const
test for equality
Definition vec.h:524
vec(const T &c0, const T &c1, const T &c2)
creates a 3d vector (c0,c1,c2)^T
Definition vec.h:119
T & first()
element accessor for the first element
Definition vec.h:255
void ceil()
ceil componentwise
Definition vec.h:557
void set_extern_data(unsigned dim, T *data)
set data pointer to an external data array
Definition vec.h:168
void set(const T &c0, const T &c1, const T &c2)
set entries of a 3d vector
Definition vec.h:150
void fill(const T &v)
fill elements of vector with scalar v
Definition vec.h:463
const T & last() const
const element accessor for the last element
Definition vec.h:274
virtual ~vec()
destructor
Definition vec.h:176
T & z()
element accessor for the third element
Definition vec.h:308
const vec< T > operator/(const vec< S > &v) const
componentwise vector division
Definition vec.h:431
const T * data() const
cast into const array
Definition vec.h:198
void round()
round componentwise
Definition vec.h:571
T & last()
element accessor for the flast element
Definition vec.h:268
vec< T > & operator=(const vec< T > &v)
assignment of a vector v
Definition vec.h:204
T length() const
length of the vector L2-Norm
Definition vec.h:541
void resize(unsigned dim)
resize the vector
Definition vec.h:496
void set(const T &c0, const T &c1, const T &c2, const T &c3)
set entries of a 4d vector
Definition vec.h:159
const vec< T > operator*(const vec< S > &v) const
componentwise vector multiplication
Definition vec.h:423
T & x()
element accessor for the first element
Definition vec.h:280
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:619
unsigned size() const
number of elements
Definition vec.h:59
vec< T > & operator*=(const T &s)
in place multiplication with s
Definition vec.h:352
vec< T > sub_vec(unsigned ifrom, unsigned size) const
extracts sub vector beginning at index ifrom with given size
Definition vec.h:607
void zeros(unsigned n)
resize the vector to size n and fills the vector with zeros
Definition vec.h:482
bool data_is_external
store whether data is not owned by vector
Definition vec.h:36
vec(const vec< S > &v)
copy constructor for vectors with different element type
Definition vec.h:99
const T & y() const
const element accessor for the second element
Definition vec.h:301
vec(unsigned dim, const T &value=T(0))
creates a vector with dim elements
Definition vec.h:65
vec< T > & operator+=(const T &s)
in place addition of a scalar s
Definition vec.h:336
const T & first() const
const element accessor for the first element
Definition vec.h:262
unsigned _size
number or elements
Definition vec.h:34
void zeros()
fill the vector with zeros
Definition vec.h:470
vec(const T &c0, const T &c1)
creates a 2d vector (c0,c1)^T
Definition vec.h:111
unsigned dim() const
number of elements
Definition vec.h:61
const vec< T > operator+(const vec< S > &v) const
vector addition
Definition vec.h:397
vec(unsigned dim, const T *marray)
creates a vector with dim elements from an array
Definition vec.h:76
vec< T > operator-(void) const
negates the vector
Definition vec.h:439
T * _data
pointer to _data storage
Definition vec.h:32
void ones(unsigned n)
resize the vector to size n and fills thevector with ones
Definition vec.h:489
T & operator()(unsigned i)
element accessor
Definition vec.h:233
const T & z() const
const element accessor for the third element
Definition vec.h:315
bool operator!=(const vec< S > &v) const
test for inequality
Definition vec.h:533
const T & x() const
const element accessor for the first element
Definition vec.h:287
void safe_normalize()
normalize the vector if length is not zero using the L2-Norm
Definition vec.h:596
const T & w() const
const element accessor for the fourth element
Definition vec.h:329
T sqr_length() const
square length of vector
Definition vec.h:579
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:130
vec< T > & operator-=(const T &s)
in place subtraction by scalar s
Definition vec.h:344
vec(const vec< T > &v)
copy constructor for vectors with equal element type
Definition vec.h:87
vec< T > & operator/=(const T &s)
in place division by scalar s
Definition vec.h:358
void set(const T &c0, const T &c1)
set entries of a 2d vector
Definition vec.h:142
T * data()
cast into non const array
Definition vec.h:192
void normalize()
normalize the vector using the L2-Norm
Definition vec.h:588
void paste(unsigned ifrom, const vec< T > &v)
paste v into vector beginning at index pos ifrom
Definition vec.h:629
T & y()
element accessor for the second element
Definition vec.h:294
void floor()
floor componentwise
Definition vec.h:564
T & w()
element accessor for the fourth element
Definition vec.h:322
void ones()
fill the vector with ones
Definition vec.h:476
void abs()
componentwise absolute values
Definition vec.h:547
T & operator[](unsigned i)
element accessor
Definition vec.h:245
the cgv namespace
Definition print.h:11
cgv::math::vec< float > vecn
declare type of single precision floating point vector with varying dimension
Definition vec.h:1187
cgv::math::vec< double > dvecn
declare type of double precision floating point vector with varying dimension
Definition vec.h:1189
std::ostream & operator<<(std::ostream &os, const vr_device_info &di)
stream out operator for device infos
Definition vr_info.cxx:10