40 typedef const T& const_reference;
41 typedef std::size_t size_type;
42 typedef std::ptrdiff_t difference_type;
44 typedef const T* const_pointer;
46 typedef const T* const_iterator;
47 typedef std::reverse_iterator<iterator> reverse_iterator;
48 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
50 iterator begin() {
return _data; }
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()); }
65 explicit vec(
unsigned dim,
const T& value = T(0)) {
103 for(
unsigned i = 0; i <
_size;i++)
111 vec(
const T& c0,
const T& c1) {
119 vec(
const T& c0,
const T& c1,
const T& c2)
130 vec(
const T& c0,
const T& c1,
const T& c2,
const T& c3)
142 void set(
const T& c0,
const T& c1)
150 void set(
const T& c0,
const T& c1,
const T& c2)
159 void set(
const T& c0,
const T& c1,
const T& c2,
const T& c3)
224 template <
typename S>
228 for (
unsigned i=0;i<
_size;++i)
_data[i]=(T)v(i);
338 for (
unsigned i=0;i<
_size;++i)
346 for (
unsigned i=0;i<
_size; ++i)
354 for (
unsigned i=0;i<
_size;++i)
_data[i] *= s;
return *
this;
360 for (
unsigned i=0;i<
_size;++i)
_data[i] /= s;
return *
this;
364 template <
typename S>
368 for (
unsigned i=0;i<
_size;++i)
_data[i] += v(i);
return *
this;
372 template <
typename S>
376 for (
unsigned i=0;i<
_size;++i)
_data[i] -= v(i);
return *
this;
380 template <
typename S>
384 for (
unsigned i=0;i<
_size;++i)
_data[i] *= v(i);
return *
this;
388 template <
typename S>
392 for (
unsigned i=0;i<
_size;++i)
_data[i] /= v(i);
return *
this;
396 template <
typename S>
399 vec<T> r = *
this; r += v;
return r;
405 vec<T> r = *
this; r += s;
return r;
411 vec<T> r = *
this; r -= s;
return r;
415 template <
typename S>
418 vec<T> r = *
this; r -= v;
return r;
422 template <
typename S>
425 vec<T> r = *
this; r *= v;
return r;
430 template <
typename S>
433 vec<T> r = *
this; r /= v;
return r;
449 vec<T> r = *
this; r *= s;
return r;
465 for (
unsigned i=0; i<
_size; ++i)
523 template <
typename S>
526 for (
unsigned i=0;i<
_size;++i)
527 if(
operator()(i) != (T)v(i))
return false;
532 template <
typename S>
535 for (
unsigned i=0;i<
_size;++i)
536 if(
operator()(i) != (T)v(i))
return true;
549 if(std::numeric_limits<T>::is_signed)
551 for(
unsigned i = 0; i <
_size;i++)
559 for(
unsigned i = 0; i <
_size;i++)
566 for(
unsigned i = 0; i <
_size;i++)
573 for(
unsigned i = 0; i <
_size;i++)
582 for(
unsigned i = 0; i!=
_size;i++)
583 l+=
operator()(i)*
operator()(i);
591 for(
unsigned i = 0; i<
_size; i++)
592 operator()(i)=l*
operator()(i);
599 if(std::abs(l) > std::numeric_limits<T>::epsilon()) {
601 for(
unsigned i = 0; i <
_size; i++)
602 operator()(i) = l *
operator()(i);
612 for(
unsigned i = 0; i <
size; i++)
621 assert(subvec.
size() == s);
622 assert(ifrom+s <=
size());
624 for(
unsigned i = 0; i < s; i++)
633 for(
unsigned i = 0; i < v.
size(); i++)
634 operator()(i+ifrom) = v(i);
642#define CGV_MATH_VEC_DECLARED
646vec<T> normalize(
const vec<T>& v)
656vec<T> safe_normalize(
const vec<T>& v)
665template <
typename T,
typename S>
666T p_norm(
const vec<T>& values,
const S& p=1)
669 unsigned N = values.size();
673 for(
unsigned i = 0; i < N;i++)
675 n+=pow(fabs(values(i)),(T)p);
678 return pow(n,(T)(1.0/(T)p));
683T inf_norm(
const vec<T>& values)
685 vec<T> r = abs(values);
693T length(
const vec<T>& v)
700T sqr_length(
const vec<T>& v)
702 return v.sqr_length();
707std::ostream&
operator<<(std::ostream& out,
const vec<T>& v)
710 for (
unsigned i=0;i<v.size()-1;++i)
714 out << v(v.size()-1);
721std::istream& operator>>(std::istream& in, vec<T>& v)
724 for (
unsigned i=0;i<v.size();++i)
736const vec<T> operator * (
const T& s,
const vec<T>& v)
738 vec<T> r = v; r *= s;
return r;
743inline T dot(
const vec<T>& v,
const vec<T>& w)
746 for (
unsigned i=0;i<v.size();++i) r += v(i)*(T)w(i);
751template <
typename T>
752inline vec<T> cross(
const vec<T>& v,
const vec<T>& w)
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);
762template <
typename T,
typename S,
typename U>
763vec<T> dbl_cross(
const vec<T> &a,
const vec<S> &b, vec<U> &c)
765 return dot(a,c)*b - dot(a,b)*c;
769template <
typename T,
typename S,
typename U>
770T spat(
const vec<T> &a,
const vec<S> &b,
const vec<U> &c)
772 return dot(cross(a,b),c);
777vec<T> project(
const vec<T> &v,
const vec<T> &n)
779 return dot(v,n)/dot(n,n)*n;
785vec<T> reflect(
const vec<T> &v,
const vec<T> &n)
787 return v-(T)2.0*dot(v,n)/dot(n,n)*n;
793vec<T> refract(
const vec<T> &v,
const vec<T> &n,T c1, T c2,
bool* total_reflection=NULL)
796 T NdotV =-dot(n,v)/dot(n,n);
799 T cosasqr = (T)1.0-(c*c)*((T)1.0-NdotV*NdotV);
805 *total_reflection=
true;
811 *total_reflection=
false;
812 return c*v + (c*NdotV - sqrt(cosasqr)/dot(n,n) )*n;
822vec<T> zeros(
const unsigned dim)
831vec<T> ones(
const unsigned dim)
841vec<T> floor(
const vec<T> &v)
844 for(
unsigned i = 0; i < v.size();i++)
845 r(i)=::floor((T)v(i));
853vec<T> ceil(
const vec<T> &v)
856 for(
unsigned i = 0; i < v.size();i++)
864vec<T> round(
const vec<T> &v)
867 for(
unsigned i = 0; i < v.size();i++)
868 r(i)=::floor(v(i)+0.5);
876vec<T> abs(
const vec<T> &v)
879 for(
unsigned i = 0; i < v.size();i++)
887T min_value(
const vec<T> &v)
889 return *(std::min_element(&v(0),&v(v.size()-1)+1));
896unsigned min_index(
const vec<T> &v)
898 return (
unsigned) (std::min_element(&v(0),&v(v.size()-1)+1)-&v(0));
903unsigned max_index(
const vec<T> &v)
905 return (
unsigned) (std::max_element(&v(0),&v(v.size()-1)+1)-&v(0));
910T max_value(
const vec<T> &v)
912 return *(std::max_element(&v(0),&v(v.size()-1)+1));
918T mean_value(
const vec<T>& values)
921 unsigned N = values.size();
925 for(
unsigned i = 0; i < N;i++)
936T var_value(
const vec<T>& values)
938 unsigned N = values.size();
940 T mu=mean_value(values);
943 for(
unsigned i = 0; i < N;i++)
945 v+=(values(i)-mu)*(values(i)-mu);
957T range_value(
const vec<T>& values)
959 return max_value(values)-min_value(values);
964T mad_value(
const vec<T>& values)
966 return median_value(abs(values-median_value(values)));
973T std_value(
const vec<T>& values)
976 T v = var_value(values);
984void var_and_mean_value(
const vec<T>& values, T& mu, T&var)
987 unsigned N = values.size();
990 for(
unsigned i = 0; i < N;i++)
997 for(
unsigned i = 0; i < N;i++)
999 var+=sqr(values(i)-mu);
1007template <
typename T>
1008void sort_values(vec<T>& values,
bool ascending=
true)
1012 std::sort(&values(0),&values(values.size()-1)+1,std::less<T>());
1014 std::sort(&values(0),&values(values.size()-1)+1,std::greater<T>());
1020template <
typename T>
1021T sum_values(
const vec<T>& values)
1024 for(
unsigned i = 0; i < values.size(); i++)
1032template <
typename T>
1033T cumsum_values(
const vec<T>& values, vec<T>& cumsumvalues)
1035 cumsumvalues.resize(values.size());
1037 for(
unsigned i = 0; i < values.size(); i++)
1048template <
typename T>
1049T prod_values(vec<T>& values)
1052 for(
unsigned i = 0; i < values.size(); i++)
1062template <
typename T>
1063T select_value(
unsigned k, vec<T>& values)
1065 if (k >= values.size())
1066 k = values.size()-1;
1067 std::nth_element(&values(0),&values(k),&values(values.size()-1)+1);
1078template <
typename T>
1079T select_median_value( vec<T>& values)
1081 return select_value((values.size())/2,values);
1087template <
typename T>
1088T median_value(
const vec<T>& values)
1091 return select_value((c.size())/2,c);
1097template <
typename T>
1098const vec<T> lin_space(
const T& first_val,
const T& last_val,
unsigned N=10)
1106 T diff = last_val-first_val;
1108 for(
unsigned i = 0; i < N; i++)
1110 lv(i) = first_val + i*diff/((T)N-(T)1.0);
1116template <
typename T>
1117const vec<T> cheb_points(
const T& first_val,
const T& last_val,
unsigned N=10)
1122 lv(0) = (T)(first_val+last_val)/2.0;
1125 T diff = (last_val-first_val)/(T)2.0;
1127 for(
unsigned i = 0; i < N; i++)
1129 lv(i) = diff*((first_val+1.0)-cos((T)((2*i+1)*3.14159)/((T)(2.0*(N-1)+2))));
1138template <
typename T>
1139const vec<T> log_space(
const T& first_pow_of_10,
const T& last_pow_of_10,
unsigned N=10)
1145 lv(0) = pow((T)10.0,last_pow_of_10);
1148 T diff = last_pow_of_10 - first_pow_of_10;
1150 for(
unsigned i = 0; i < N; i++)
1152 lv(i) = pow((T)10.0,(T)first_pow_of_10 + (T)i*diff/((T)N-(T)1.0));
1158template <
typename T>
1159const vec<T> lerp(
const vec<T>& v1,
const vec<T>& v2, T t)
1161 return (1-t)*v1+t*v2;
1165template <
typename T>
1166const vec<T> slerp(
const vec<T>& v0,
const vec<T>& v1, T t)
1168 T dotv0v1 = dot(v0,v1);
1176 T theta = acos(dotv0v1)*t;
1178 return cos(theta)*v0 + sin(theta)*v2;
vec()
standard constructor
bool operator==(const vec< S > &v) const
test for equality
vec(const T &c0, const T &c1, const T &c2)
creates a 3d vector (c0,c1,c2)^T
T & first()
element accessor for the first element
void ceil()
ceil componentwise
void set_extern_data(unsigned dim, T *data)
set data pointer to an external data array
void set(const T &c0, const T &c1, const T &c2)
set entries of a 3d vector
void fill(const T &v)
fill elements of vector with scalar v
const T & last() const
const element accessor for the last element
T & z()
element accessor for the third element
const vec< T > operator/(const vec< S > &v) const
componentwise vector division
const T * data() const
cast into const array
void round()
round componentwise
T & last()
element accessor for the flast element
vec< T > & operator=(const vec< T > &v)
assignment of a vector v
T length() const
length of the vector L2-Norm
void resize(unsigned dim)
resize the vector
void set(const T &c0, const T &c1, const T &c2, const T &c3)
set entries of a 4d vector
const vec< T > operator*(const vec< S > &v) const
componentwise vector multiplication
T & x()
element accessor for the first element
void copy(unsigned ifrom, unsigned s, vec< T > &subvec) const
copy sub vector beginning at index ifrom with given size s into subvec
unsigned size() const
number of elements
vec< T > & operator*=(const T &s)
in place multiplication with s
vec< T > sub_vec(unsigned ifrom, unsigned size) const
extracts sub vector beginning at index ifrom with given size
void zeros(unsigned n)
resize the vector to size n and fills the vector with zeros
bool data_is_external
store whether data is not owned by vector
vec(const vec< S > &v)
copy constructor for vectors with different element type
const T & y() const
const element accessor for the second element
vec(unsigned dim, const T &value=T(0))
creates a vector with dim elements
vec< T > & operator+=(const T &s)
in place addition of a scalar s
const T & first() const
const element accessor for the first element
unsigned _size
number or elements
void zeros()
fill the vector with zeros
vec(const T &c0, const T &c1)
creates a 2d vector (c0,c1)^T
unsigned dim() const
number of elements
const vec< T > operator+(const vec< S > &v) const
vector addition
vec(unsigned dim, const T *marray)
creates a vector with dim elements from an array
vec< T > operator-(void) const
negates the vector
T * _data
pointer to _data storage
void ones(unsigned n)
resize the vector to size n and fills thevector with ones
T & operator()(unsigned i)
element accessor
const T & z() const
const element accessor for the third element
bool operator!=(const vec< S > &v) const
test for inequality
const T & x() const
const element accessor for the first element
void safe_normalize()
normalize the vector if length is not zero using the L2-Norm
const T & w() const
const element accessor for the fourth element
T sqr_length() const
square length of vector
vec(const T &c0, const T &c1, const T &c2, const T &c3)
creates a 4d vector (c0,c1,c2,c3)^T
vec< T > & operator-=(const T &s)
in place subtraction by scalar s
vec(const vec< T > &v)
copy constructor for vectors with equal element type
vec< T > & operator/=(const T &s)
in place division by scalar s
void set(const T &c0, const T &c1)
set entries of a 2d vector
T * data()
cast into non const array
void normalize()
normalize the vector using the L2-Norm
void paste(unsigned ifrom, const vec< T > &v)
paste v into vector beginning at index pos ifrom
T & y()
element accessor for the second element
void floor()
floor componentwise
T & w()
element accessor for the fourth element
void ones()
fill the vector with ones
void abs()
componentwise absolute values
T & operator[](unsigned i)
element accessor
cgv::math::vec< float > vecn
declare type of single precision floating point vector with varying dimension
cgv::math::vec< double > dvecn
declare type of double precision floating point vector with varying dimension
std::ostream & operator<<(std::ostream &os, const vr_device_info &di)
stream out operator for device infos