5#ifndef _USE_MATH_DEFINES
6 #define _USE_MATH_DEFINES 1
15#include <cgv/type/standard_types.h>
16#include <cgv/math/functions.h>
21template <
typename T>
class vec;
24template <
typename T, cgv::type::u
int32_type N>
38 typedef const T& const_reference;
40 typedef std::size_t size_type;
42 typedef std::ptrdiff_t difference_type;
46 typedef const T* const_pointer;
50 typedef const T* const_iterator;
52 typedef std::reverse_iterator<iterator> reverse_iterator;
54 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
63 iterator begin() {
return v; }
65 iterator end() {
return v+N; }
67 const_iterator begin()
const {
return v; }
69 const_iterator end()
const {
return v+N; }
71 reverse_iterator rbegin() {
return reverse_iterator(end()); }
73 reverse_iterator rend() {
return reverse_iterator(begin()); }
75 const_reverse_iterator rbegin()
const {
return const_reverse_iterator(end()); }
77 const_reverse_iterator
rend()
const {
return const_reverse_iterator(begin()); }
85 fvec(
const T &a) { std::fill(v, v+N, a); }
95 std::copy(a, a+min_n, v);
96 for (i = min_n; i < N; ++i) v[i] = T(0);
102 for (i=0; i<min_n; ++i) v[i] = (T)a[i];
103 for (; i < N; ++i) v[i] = T(0);
106 fvec(
const fvec<T,N> &rhs) {
if (
this != &rhs) std::copy(rhs.v, rhs.v+N, v); }
108 template <
typename S>
111 template <
typename S1,
typename S2>
114 template <
typename S>
123 void assign(
const std::array<T, N>& arr) { std::copy(arr.cbegin(), arr.cend(), v); }
125 void set(
const T &
x,
const T &
y) { v[0] =
x; v[1] =
y; }
127 void set(
const T &
x,
const T &
y,
const T &
z) { v[0] =
x; v[1] =
y; v[2] =
z; }
129 void set(
const T &
x,
const T &
y,
const T &
z,
const T &
w) { v[0] =
x; v[1] =
y; v[2] =
z; v[3] =
w; }
131 void fill(
const T& a) { std::fill(v, v+N, a); }
135 void zerosh() { std::fill(v, v+N-1, (T)0); v[N-1] = (T)1; }
143 std::fill(r.v, r.v+N-1, (T)0); r.v[N-1] = (T)1;
155 T&
x() {
return v[0]; }
157 const T&
x()
const {
return v[0]; }
159 T&
y() {
return v[1]; }
161 const T&
y()
const {
return v[1]; }
163 T&
z() {
return v[2]; }
165 const T&
z()
const {
return v[2]; }
167 T&
w() {
return v[3]; }
169 const T&
w()
const {
return v[3]; }
183 const T*
data()
const {
return v; }
197 template <
typename S>
200 template <
typename S>
203 template <
typename S>
206 template <
typename S>
209 template <
typename S>
216 template <
typename S>
219 template <
typename S>
222 template <
typename S>
231 template <
typename S>
233 for (
unsigned i=0;i<N;++i)
234 if(
operator()(i) != (T)v(i))
return false;
238 template <
typename S>
240 for (
unsigned i=0;i<N;++i)
241 if(
operator()(i) != (T)v(i))
return true;
256 for (
unsigned i = 0; i < N; i++)
257 v[i] = cgv::math::sign(v[i]);
262 for (
unsigned i = 0; i < N; i++)
263 v[i] = cgv::math::step(v[i], r[i]);
268 if(std::numeric_limits<T>::is_signed) {
269 for(
unsigned i = 0; i < N;i++)
270 v[i]=(T)std::abs((
double)v[i]);
276 for(
unsigned i = 0; i < N;i++)
277 v[i]=(T)
::ceil((
double)v[i]);
282 for(
unsigned i = 0; i < N;i++)
288 for(
unsigned i = 0; i < N;i++)
289 v[i]=(T)
::floor((
double)v[i]+0.5);
296 for(
unsigned i = 0; i!=N;i++)
297 l+=
operator()(i)*
operator()(i);
305 for(
unsigned i = 0; i<N; i++)
306 operator()(i)=inv_l*
operator()(i);
313 if(std::abs(l) < std::numeric_limits<T>::epsilon())
315 T inv_l = (T)1.0 / l;
316 for(
unsigned i = 0; i < N; i++)
317 operator()(i) = inv_l *
operator()(i);
324#define CGV_MATH_FVEC_DECLARED
327template<
typename T, cgv::type::u
int32_type N>
328fvec<T,N> normalize(
const fvec<T,N>& v) { fvec<T,N> w(v); w.normalize();
return w; }
331template<
typename T, cgv::type::u
int32_type N>
332fvec<T, N> safe_normalize(
const fvec<T, N>& v) { fvec<T, N> w(v); w.safe_normalize();
return w; }
335template<
typename T, cgv::type::u
int32_type N>
336std::ostream&
operator<<(std::ostream& out,
const fvec<T,N>& v)
338 for (
unsigned i=0;i<N-1;++i)
346template<
typename T, cgv::type::u
int32_type N>
347std::istream& operator>>(std::istream& in, fvec<T,N>& v)
349 for (
unsigned i = 0; i < N; ++i) {
351 if (in.fail() && i == 1) {
352 for (
unsigned i = 1; i < N; ++i)
361template<
typename T, cgv::type::u
int32_type N>
362std::string
to_string(
const fvec<T, N>& v)
364 std::ostringstream ss;
370template<
typename T, cgv::type::u
int32_type N>
371bool from_string(
const std::string& s, fvec<T, N>& v)
373 std::istringstream iss(s);
379template <
typename T, cgv::type::u
int32_type N>
380fvec<T,N> operator * (
const T& s,
const fvec<T,N>& v) { fvec<T,N> r = v; r *= s;
return r; }
383template <
typename T, cgv::type::u
int32_type N>
384fvec<T,N> operator / (
const T& s,
const fvec<T,N>& v)
387 for (
unsigned i=0;i<N;++i)
393template <
typename T,
typename S, cgv::type::u
int32_type N>
394inline T dot(
const fvec<T,N>& v,
const fvec<S,N>& w)
397 for (
unsigned i=0;i<N;++i)
404template <
typename T,
typename S, cgv::type::u
int32_type N>
405inline S dot_pos(
const fvec<T,N>& v,
const fvec<S,N+1>& w)
408 for (
unsigned i=0;i<N;++i)
414template <
typename T,
typename S, cgv::type::u
int32_type N>
415inline S dot_pos(
const fvec<T,N+1>& v,
const fvec<S,N>& w)
418 for (
unsigned i=0;i<N;++i)
425template <
typename T,
typename S, cgv::type::u
int32_type N>
426inline S dot_dir(
const fvec<T,N>& v,
const fvec<S,N+1>& w)
429 for (
unsigned i=0;i<N;++i)
435template <
typename T,
typename S, cgv::type::u
int32_type N>
436inline S dot_dir(
const fvec<T,N+1>& v,
const fvec<S,N>& w)
439 for (
unsigned i=0;i<N;++i)
445template <
typename T, cgv::type::u
int32_type N>
446inline T length(
const fvec<T, N>& v) {
return std::sqrt(dot(v, v)); }
449template <
typename T, cgv::type::u
int32_type N>
450inline fvec<T, N> sign(
const fvec<T, N>& v) { fvec<T, N> r(v); r.sign();
return r; }
453template <
typename T, cgv::type::u
int32_type N>
454inline fvec<T, N> step(
const fvec<T, N>& a,
const fvec<T, N>& b) { fvec<T, N> r(a); r.step(b);
return r; }
457template <
typename T, cgv::type::u
int32_type N>
458inline fvec<T, N> abs(
const fvec<T, N>& v) { fvec<T, N> r(v); r.abs();
return r; }
461template <
typename T, cgv::type::u
int32_type N>
462inline fvec<T, N> round(
const fvec<T, N>& v) { fvec<T, N> r(v); r.round();
return r; }
465template <
typename T, cgv::type::u
int32_type N>
466inline fvec<T, N> floor(
const fvec<T, N>& v) { fvec<T, N> r(v); r.floor();
return r; }
469template <
typename T, cgv::type::u
int32_type N>
470inline fvec<T, N> ceil(
const fvec<T, N>& v) { fvec<T, N> r(v); r.ceil();
return r; }
473template <
typename T, cgv::type::u
int32_type N>
474inline T sqr_length(
const fvec<T,N>& v)
480template <
typename T, cgv::type::u
int32_type N>
481inline fvec<T,N> cross(
const fvec<T,N>& v,
const fvec<T,N>& w)
484 r(0)= v(1)*w(2) - v(2)*w(1);
485 r(1)= v(2)*w(0) - v(0)*w(2);
486 r(2)= v(0)*w(1) - v(1)*w(0);
491template <
typename T, cgv::type::u
int32_type N>
492inline fvec<T,N+1> hom(
const fvec<T,N>& v)
495 for (
unsigned i = 0; i<N; ++i)
502template <
typename T, cgv::type::u
int32_type N>
503T min_value(
const fvec<T,N> &v)
505 return *(std::min_element(&v(0),&v(N-1)+1));
509template <
typename T, cgv::type::u
int32_type N>
510unsigned min_index(
const fvec<T,N> &v)
512 return (
unsigned) (std::min_element(&v(0),&v(N-1)+1)-&v(0));
516template <
typename T, cgv::type::u
int32_type N>
517unsigned max_index(
const fvec<T,N> &v)
519 return (
unsigned) (std::max_element(&v(0),&v(N-1)+1)-&v(0));
523template <
typename T, cgv::type::u
int32_type N>
524T max_value(
const fvec<T,N> &v)
526 return *(std::max_element(&v(0),&v(N-1)+1));
530template <
typename T, cgv::type::u
int32_type N>
531const fvec<T, N> lerp(
const fvec<T, N>& v1,
const fvec<T, N>& v2, T t)
533 return ((T)1 - t)*v1 + t * v2;
537template <
typename T, cgv::type::u
int32_type N>
538const fvec<T, N> lerp(
const fvec<T, N>& v1,
const fvec<T, N>& v2,
const fvec<T, N>& t)
540 return (fvec<T, N>(1) - t)*v1 + t * v2;
544template <
typename T, cgv::type::u
int32_type N>
545const fvec<T, N> slerp(
const fvec<T, N> &v0,
const fvec<T, N> &v1, T t) {
546 T dotv0v1 = dot(v0, v1);
554 T theta = acos(dotv0v1) * t;
555 auto v2 = normalize(v1 - (dotv0v1)*v0);
556 return T(cos(theta)) * v0 + T(sin(theta)) * v2;
560template <
typename T, cgv::type::u
int32_type N>
561const fvec<T, N> min(
const fvec<T, N>& v, T t) {
563 for(
unsigned i = 0; i < N; ++i)
564 c(i) = std::min(v(i), t);
569template <
typename T, cgv::type::u
int32_type N>
570const fvec<T, N> min(
const fvec<T, N>& v,
const fvec<T, N>& t) {
572 for(
unsigned i = 0; i < N; ++i)
573 c(i) = std::min(v(i), t(i));
578template <
typename T, cgv::type::u
int32_type N>
579const fvec<T, N> max(
const fvec<T, N>& v, T t) {
581 for(
unsigned i = 0; i < N; ++i)
582 c(i) = std::max(v(i), t);
587template <
typename T, cgv::type::u
int32_type N>
588const fvec<T, N> max(
const fvec<T, N>& v,
const fvec<T, N>& t) {
590 for(
unsigned i = 0; i < N; ++i)
591 c(i) = std::max(v(i), t(i));
596template <
typename T, cgv::type::u
int32_type N>
597const fvec<T, N> clamp(
const fvec<T, N>& v, T l, T r) {
599 for(
unsigned i = 0; i < N; ++i)
600 c(i) = cgv::math::clamp(v(i), l, r);
605template <
typename T, cgv::type::u
int32_type N>
606const fvec<T, N> clamp(
const fvec<T, N>& v,
const fvec<T, N>& vl,
const fvec<T, N>&
vr)
609 for(
unsigned i = 0; i < N; ++i)
610 c(i) = cgv::math::clamp(v(i), vl(i),
vr(i));
615template <
typename T, cgv::type::u
int32_type N>
616const fvec<T, N> saturate(
const fvec<T, N>& v) {
617 return clamp(v, T(0), T(1));
621template <
typename T, cgv::type::u
int32_type N>
622const fvec<T, N> pow(
const fvec<T, N>& v, T e) {
624 for(
unsigned i = 0; i < N; ++i)
625 c(i) = std::pow(v(i), e);
630template <
typename T, cgv::type::u
int32_type N>
631const fvec<T, N> pow(
const fvec<T, N>& v,
const fvec<T, N>& e) {
633 for(
unsigned i = 0; i < N; ++i)
634 c(i) = std::pow(v(i), e(i));
639template <
typename T, cgv::type::u
int32_type N>
640fvec<T, N> ortho(
const fvec<T, N>& v) =
delete;
644fvec<T, 2> ortho(
const fvec<T, 2>& v) {
645 return fvec<T, 2>(-v.y(), v.x());
650fvec<T, 3> ortho(
const fvec<T, 3>& v) {
651 return std::abs(v.x()) > std::abs(v.z()) ? fvec<T, 3>(-v.y(), v.x(), T(0)) : fvec<T, 3>(T(0), -v.z(), v.y());
729template <
typename T, cgv::type::u
int32_type N>
737template <
typename T, cgv::type::u
int32_type N>
complete implementation of method actions that only call one method when entering a node
A vector with zero based index.
fvec(const T &x, const T &y, const T &z, const T &w)
construct and init first four coordinates to the given values
T safe_normalize()
normalize the vector if length is not zero using the L2-Norm and return the length
fvec(cgv::type::uint32_type n, const S *a)
creates a column vector initialized to array of a different type with zeros filled to not copied comp...
T normalize()
normalize the vector using the L2-Norm and return the length
fvec(const T &x, const T &y, const T &z)
construct and init first three coordinates to the given values
static fvec< T, N > zeroh()
creates a homogeneous zero-vector (yields same result as calling fvec<T,N-1>(0).lift() but is faster)
void round()
round componentwise
fvec< T, N+1 > lift() const
convert to homogeneous version by adding a 1
const T & z() const
third element of const vector
bool operator==(const fvec< S, N > &v) const
test for equality
fvec< T, N > & operator*=(const T &s)
in place multiplication with s
fvec< T, N > operator*(const fvec< S, N > &v) const
componentwise vector multiplication
fvec< T, N > operator-(void) const
negates the vector
fvec< T, N > & operator-=(const T &s)
in place subtraction by scalar s
fvec< T, N > & operator/=(const T &s)
in place division by scalar s
void sign()
componentwise sign values
T & operator()(const int i)
access i'th element
T * data()
cast into array. This allows calls like glVertex<N><T>v(p.data()) instead of glVertex<N><T,...
T sqr_length() const
square length of vector
void ones()
fill the vector with ones
void set(const T &x, const T &y, const T &z)
set the first three components
T length() const
length of the vector L2-Norm
void floor()
floor componentwise
static fvec< T, N > from_vec(const vec< T > &)
conversion from vector
fvec(cgv::type::uint32_type n, const T *a)
creates a vector from a n-element array a, if n < N remaining N-n elements are set to zero
fvec< T, N > operator+(const fvec< S, N > &v) const
vector addition
void assign(const std::array< T, N > &arr)
set to the contents of the given std::array with same size
fvec< T, N > & operator+=(const T &s)
in place addition of a scalar s
fvec & operator=(const fvec< T, N > &rhs)
assign vector rhs, if vector and rhs have different sizes, vector has been resized to match the size ...
fvec< T, N > operator/(const fvec< S, N > &v) const
componentwise vector division
bool operator!=(const fvec< S, N > &v) const
test for inequality
fvec(const fvec< T, N > &rhs)
copy constructor
fvec(const fvec< S, N+1 > &fv)
construct from vector of one dimension higher by cutting of the highest dimension
fvec(const fvec< S1, N - 1 > &fv, S2 w)
construct from vector of one dimension less plus a scalar
vec< T > to_vec() const
conversion to vector type
fvec(const fvec< S, N > &fv)
copies a column vector of a different type
const_reverse_iterator rend() const
reverse iterator pointing to the end of reverse iteration
void set(const T &x, const T &y, const T &z, const T &w)
set the first four components
void zerosh()
fill the vector with zeros except for the last component, which will be set to one
fvec(const T &x, const T &y)
construct and init first two coordinates to the given values
void set(const T &x, const T &y)
set the first two components
const T & y() const
second element of const vector
fvec(const std::array< T, N > &arr)
construct from std::array of same size
fvec()
creates a vector not initialized
const T & operator[](const int i) const
access i'th element of const vector
const T & w() const
fourth element of const vector
void abs()
componentwise absolute values
const T & operator()(const int i) const
access i'th element of const vector
T & operator[](const int i)
access i'th element
const T & x() const
first element of const vector
void ceil()
ceil componentwise
void zeros()
fill the vector with zeros
fvec(const T &a)
creates a vector, where all N components are initialized to the constant value a
void fill(const T &a)
fill elements of vector with scalar v
void step(const fvec< T, N > &r)
componentwise sign values
const T * data() const
cast into const array
static cgv::type::uint32_type size()
return number of elements
void set_extern_data(unsigned dim, T *data)
set data pointer to an external data array
unsigned dim() const
number of elements
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
bool from_string(std::string &v, const std::string &s)
specialization to extract string value from string
cgv::math::fvec< int64_t, 2 > lvec2
declare type of 2d 64 bit integer vectors
cgv::math::fvec< uint16_t, 4 > usvec4
declare type of 4d 16 bit unsigned integer vectors
cgv::math::fvec< float, 4 > vec4
declare type of 4d single precision floating point vectors (used for homogeneous coordinates)
cgv::math::fvec< uint64_t, 4 > ulvec4
declare type of 4d 64 bit unsigned integer vectors
cgv::math::fvec< double, 4 > dvec4
declare type of 4d double precision floating point vectors (used for homogeneous coordinates)
cgv::math::fvec< double, 3 > dvec3
declare type of 3d double precision floating point vectors
cgv::math::fvec< uint32_t, 3 > uvec3
declare type of 3d 32 bit unsigned integer vectors
cgv::math::fvec< int32_t, 3 > ivec3
declare type of 3d 32 bit integer vectors
cgv::math::fvec< int32_t, 4 > ivec4
declare type of 4d 32 bit integer vectors
cgv::math::fvec< uint32_t, 4 > uvec4
declare type of 4d 32 bit unsigned integer vectors
cgv::math::fvec< int32_t, 2 > ivec2
declare type of 2d 32 bit integer vectors
cgv::math::fvec< int16_t, 3 > svec3
declare type of 3d 16 bit integer vectors
cgv::math::fvec< uint16_t, 3 > usvec3
declare type of 3d 16 bit unsigned integer vectors
cgv::math::fvec< float, 2 > vec2
declare type of 2d single precision floating point vectors
cgv::math::fvec< int64_t, 3 > lvec3
declare type of 3d 64 bit integer vectors
cgv::math::fvec< bool, 4 > bvec4
declare type of 4d boolean vectors
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
cgv::math::fvec< double, 2 > dvec2
declare type of 2d double precision floating point vectors
cgv::math::fvec< uint16_t, 2 > usvec2
declare type of 2d 16 bit unsigned integer vectors
cgv::math::fvec< uint32_t, 2 > uvec2
declare type of 2d 32 bit unsigned integer vectors
cgv::math::fvec< int16_t, 2 > svec2
declare type of 2d 16 bit integer vectors
cgv::math::fvec< uint64_t, 3 > ulvec3
declare type of 3d 64 bit unsigned integer vectors
cgv::math::fvec< int64_t, 4 > lvec4
declare type of 4d 64 bit integer vectors
cgv::math::fvec< bool, 3 > bvec3
declare type of 3d boolean vectors
cgv::math::fvec< int16_t, 4 > svec4
declare type of 4d 16 bit integer vectors
cgv::math::fvec< bool, 2 > bvec2
declare type of 2d boolean vectors
cgv::math::fvec< uint64_t, 2 > ulvec2
declare type of 2d 64 bit unsigned integer vectors
the vr namespace for virtual reality support
std::ostream & operator<<(std::ostream &os, const vr_device_info &di)
stream out operator for device infos