5#ifndef _USE_MATH_DEFINES
6 #define _USE_MATH_DEFINES 1
16#include <cgv/type/standard_types.h>
17#include <cgv/math/functions.h>
22template <
typename T>
class vec;
25template <
typename T, cgv::type::u
int32_type N>
39 typedef const T& const_reference;
41 typedef std::size_t size_type;
43 typedef std::ptrdiff_t difference_type;
47 typedef const T* const_pointer;
51 typedef const T* const_iterator;
53 typedef std::reverse_iterator<iterator> reverse_iterator;
55 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
64 iterator begin() {
return v; }
66 iterator end() {
return v+N; }
68 const_iterator begin()
const {
return v; }
70 const_iterator end()
const {
return v+N; }
72 reverse_iterator rbegin() {
return reverse_iterator(end()); }
74 reverse_iterator rend() {
return reverse_iterator(begin()); }
76 const_reverse_iterator rbegin()
const {
return const_reverse_iterator(end()); }
78 const_reverse_iterator
rend()
const {
return const_reverse_iterator(begin()); }
86 fvec(
const T &a) { std::fill(v, v+N, a); }
96 memmove(v, a, min_n*
sizeof(T));
97 for (i = min_n; i < N; ++i) v[i] = T(0);
100 template <
typename S>
103 for (i=0; i<min_n; ++i) v[i] = (T)a[i];
104 for (; i < N; ++i) v[i] = T(0);
107 fvec(
const fvec<T,N> &rhs) {
if (
this != &rhs) std::copy(rhs.v, rhs.v+N, v); }
109 template <
typename S>
112 template <
typename S1,
typename S2>
115 template <
typename S>
124 void assign(
const std::array<T, N>& arr) { std::copy(arr.cbegin(), arr.cend(), v); }
126 void set(
const T &
x,
const T &
y) { v[0] =
x; v[1] =
y; }
128 void set(
const T &
x,
const T &
y,
const T &
z) { v[0] =
x; v[1] =
y; v[2] =
z; }
130 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; }
132 void fill(
const T& a) { std::fill(v, v+N, a); }
136 void zerosh() { std::fill(v, v+N-1, (T)0); v[N-1] = (T)1; }
144 std::fill(r.v, r.v+N-1, (T)0); r.v[N-1] = (T)1;
156 T&
x() {
return v[0]; }
158 const T&
x()
const {
return v[0]; }
160 T&
y() {
return v[1]; }
162 const T&
y()
const {
return v[1]; }
164 T&
z() {
return v[2]; }
166 const T&
z()
const {
return v[2]; }
168 T&
w() {
return v[3]; }
170 const T&
w()
const {
return v[3]; }
184 const T*
data()
const {
return v; }
198 template <
typename S>
201 template <
typename S>
204 template <
typename S>
207 template <
typename S>
210 template <
typename S>
217 template <
typename S>
220 template <
typename S>
223 template <
typename S>
232 template <
typename S>
234 for (
unsigned i=0;i<N;++i)
235 if(
operator()(i) != (T)v(i))
return false;
239 template <
typename S>
241 for (
unsigned i=0;i<N;++i)
242 if(
operator()(i) != (T)v(i))
return true;
257 for (
unsigned i = 0; i < N; i++)
258 v[i] = cgv::math::sign(v[i]);
263 for (
unsigned i = 0; i < N; i++)
264 v[i] = cgv::math::step(v[i], r[i]);
269 if(std::numeric_limits<T>::is_signed) {
270 for(
unsigned i = 0; i < N;i++)
271 v[i]=(T)std::abs((
double)v[i]);
277 for(
unsigned i = 0; i < N;i++)
278 v[i]=(T)
::ceil((
double)v[i]);
283 for(
unsigned i = 0; i < N;i++)
289 for(
unsigned i = 0; i < N;i++)
290 v[i]=(T)
::floor((
double)v[i]+0.5);
297 for(
unsigned i = 0; i!=N;i++)
298 l+=
operator()(i)*
operator()(i);
306 for(
unsigned i = 0; i<N; i++)
307 operator()(i)=inv_l*
operator()(i);
314 if(std::abs(l) < std::numeric_limits<T>::epsilon())
316 T inv_l = (T)1.0 / l;
317 for(
unsigned i = 0; i < N; i++)
318 operator()(i) = inv_l *
operator()(i);
325#define CGV_MATH_FVEC_DECLARED
328template<
typename T, cgv::type::u
int32_type N>
329fvec<T,N> normalize(
const fvec<T,N>& v) { fvec<T,N> w(v); w.normalize();
return w; }
332template<
typename T, cgv::type::u
int32_type N>
333fvec<T, N> safe_normalize(
const fvec<T, N>& v) { fvec<T, N> w(v); w.safe_normalize();
return w; }
336template<
typename T, cgv::type::u
int32_type N>
337std::ostream&
operator<<(std::ostream& out,
const fvec<T,N>& v)
339 for (
unsigned i=0;i<N-1;++i)
347template<
typename T, cgv::type::u
int32_type N>
348std::istream& operator>>(std::istream& in, fvec<T,N>& v)
350 for (
unsigned i = 0; i < N; ++i) {
352 if (in.fail() && i == 1) {
353 for (
unsigned i = 1; i < N; ++i)
362template<
typename T, cgv::type::u
int32_type N>
363std::string
to_string(
const fvec<T, N>& v)
365 std::ostringstream ss;
371template<
typename T, cgv::type::u
int32_type N>
372bool from_string(
const std::string& s, fvec<T, N>& v)
374 std::istringstream iss(s);
380template <
typename T, cgv::type::u
int32_type N>
381fvec<T,N> operator * (
const T& s,
const fvec<T,N>& v) { fvec<T,N> r = v; r *= s;
return r; }
384template <
typename T, cgv::type::u
int32_type N>
385fvec<T,N> operator / (
const T& s,
const fvec<T,N>& v)
388 for (
unsigned i=0;i<N;++i)
394template <
typename T,
typename S, cgv::type::u
int32_type N>
395inline T dot(
const fvec<T,N>& v,
const fvec<S,N>& w)
398 for (
unsigned i=0;i<N;++i)
405template <
typename T,
typename S, cgv::type::u
int32_type N>
406inline S dot_pos(
const fvec<T,N>& v,
const fvec<S,N+1>& w)
409 for (
unsigned i=0;i<N;++i)
415template <
typename T,
typename S, cgv::type::u
int32_type N>
416inline S dot_pos(
const fvec<T,N+1>& v,
const fvec<S,N>& w)
419 for (
unsigned i=0;i<N;++i)
426template <
typename T,
typename S, cgv::type::u
int32_type N>
427inline S dot_dir(
const fvec<T,N>& v,
const fvec<S,N+1>& w)
430 for (
unsigned i=0;i<N;++i)
436template <
typename T,
typename S, cgv::type::u
int32_type N>
437inline S dot_dir(
const fvec<T,N+1>& v,
const fvec<S,N>& w)
440 for (
unsigned i=0;i<N;++i)
446template <
typename T, cgv::type::u
int32_type N>
447inline T length(
const fvec<T, N>& v) {
return std::sqrt(dot(v, v)); }
450template <
typename T, cgv::type::u
int32_type N>
451inline fvec<T, N> sign(
const fvec<T, N>& v) { fvec<T, N> r(v); r.sign();
return r; }
454template <
typename T, cgv::type::u
int32_type N>
455inline fvec<T, N> step(
const fvec<T, N>& a,
const fvec<T, N>& b) { fvec<T, N> r(a); r.step(b);
return r; }
458template <
typename T, cgv::type::u
int32_type N>
459inline fvec<T, N> abs(
const fvec<T, N>& v) { fvec<T, N> r(v); r.abs();
return r; }
462template <
typename T, cgv::type::u
int32_type N>
463inline fvec<T, N> round(
const fvec<T, N>& v) { fvec<T, N> r(v); r.round();
return r; }
466template <
typename T, cgv::type::u
int32_type N>
467inline fvec<T, N> floor(
const fvec<T, N>& v) { fvec<T, N> r(v); r.floor();
return r; }
470template <
typename T, cgv::type::u
int32_type N>
471inline fvec<T, N> ceil(
const fvec<T, N>& v) { fvec<T, N> r(v); r.ceil();
return r; }
474template <
typename T, cgv::type::u
int32_type N>
475inline T sqr_length(
const fvec<T,N>& v)
481template <
typename T, cgv::type::u
int32_type N>
482inline fvec<T,N> cross(
const fvec<T,N>& v,
const fvec<T,N>& w)
485 r(0)= v(1)*w(2) - v(2)*w(1);
486 r(1)= v(2)*w(0) - v(0)*w(2);
487 r(2)= v(0)*w(1) - v(1)*w(0);
492template <
typename T, cgv::type::u
int32_type N>
493inline fvec<T,N+1> hom(
const fvec<T,N>& v)
496 for (
unsigned i = 0; i<N; ++i)
503template <
typename T, cgv::type::u
int32_type N>
506 return *(std::min_element(&v(0),&v(N-1)+1));
510template <
typename T, cgv::type::u
int32_type N>
511unsigned min_index(
const fvec<T,N> &v)
513 return (
unsigned) (std::min_element(&v(0),&v(N-1)+1)-&v(0));
517template <
typename T, cgv::type::u
int32_type N>
518unsigned max_index(
const fvec<T,N> &v)
520 return (
unsigned) (std::max_element(&v(0),&v(N-1)+1)-&v(0));
524template <
typename T, cgv::type::u
int32_type N>
527 return *(std::max_element(&v(0),&v(N-1)+1));
531template <
typename T, cgv::type::u
int32_type N>
532const fvec<T, N> lerp(
const fvec<T, N>& v1,
const fvec<T, N>& v2, T t)
534 return ((T)1 - t)*v1 + t * v2;
538template <
typename T, cgv::type::u
int32_type N>
539const fvec<T, N> lerp(
const fvec<T, N>& v1,
const fvec<T, N>& v2,
const fvec<T, N>& t)
541 return (fvec<T, N>(1) - t)*v1 + t * v2;
545template <
typename T, cgv::type::u
int32_type N>
546const fvec<T, N> slerp(
const fvec<T, N> &v0,
const fvec<T, N> &v1, T t) {
547 T dotv0v1 = dot(v0, v1);
555 T theta = acos(dotv0v1) * t;
556 auto v2 = normalize(v1 - (dotv0v1)*v0);
557 return T(cos(theta)) * v0 + T(sin(theta)) * v2;
561template <
typename T, cgv::type::u
int32_type N>
562const fvec<T, N> min(
const fvec<T, N>& v, T t) {
564 for(
unsigned i = 0; i < N; ++i)
565 c(i) = std::min(v(i), t);
570template <
typename T, cgv::type::u
int32_type N>
571const fvec<T, N> min(
const fvec<T, N>& v,
const fvec<T, N>& t) {
573 for(
unsigned i = 0; i < N; ++i)
574 c(i) = std::min(v(i), t(i));
579template <
typename T, cgv::type::u
int32_type N>
580const fvec<T, N> max(
const fvec<T, N>& v, T t) {
582 for(
unsigned i = 0; i < N; ++i)
583 c(i) = std::max(v(i), t);
588template <
typename T, cgv::type::u
int32_type N>
589const fvec<T, N> max(
const fvec<T, N>& v,
const fvec<T, N>& t) {
591 for(
unsigned i = 0; i < N; ++i)
592 c(i) = std::max(v(i), t(i));
597template <
typename T, cgv::type::u
int32_type N>
598const fvec<T, N> clamp(
const fvec<T, N>& v, T l, T r) {
600 for(
unsigned i = 0; i < N; ++i)
601 c(i) = cgv::math::clamp(v(i), l, r);
606template <
typename T, cgv::type::u
int32_type N>
607const fvec<T, N> clamp(
const fvec<T, N>& v,
const fvec<T, N>& vl,
const fvec<T, N>&
vr)
610 for(
unsigned i = 0; i < N; ++i)
611 c(i) = cgv::math::clamp(v(i), vl(i),
vr(i));
616template <
typename T, cgv::type::u
int32_type N>
617const fvec<T, N> saturate(
const fvec<T, N>& v) {
618 return clamp(v, T(0), T(1));
622template <
typename T, cgv::type::u
int32_type N>
623const fvec<T, N> pow(
const fvec<T, N>& v, T e) {
625 for(
unsigned i = 0; i < N; ++i)
626 c(i) = std::pow(v(i), e);
631template <
typename T, cgv::type::u
int32_type N>
632const fvec<T, N> pow(
const fvec<T, N>& v,
const fvec<T, N>& e) {
634 for(
unsigned i = 0; i < N; ++i)
635 c(i) = std::pow(v(i), e(i));
640template <
typename T, cgv::type::u
int32_type N>
641fvec<T, N> ortho(
const fvec<T, N>& v) =
delete;
645fvec<T, 2> ortho(
const fvec<T, 2>& v) {
646 return fvec<T, 2>(-v.y(), v.x());
651fvec<T, 3> ortho(
const fvec<T, 3>& v) {
652 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());
730template <
typename T, cgv::type::u
int32_type N>
738template <
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
T min_value(ForwardIt first, ForwardIt last, T fallback)
Find the minimum value in the range [first, last) or return fallback if the range is empty.
T max_value(ForwardIt first, ForwardIt last, T fallback)
Find the maximum value in the range [first, last) or return fallback if the range is empty.
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