5#ifndef _USE_MATH_DEFINES
6 #define _USE_MATH_DEFINES 1
17#include <cgv/type/standard_types.h>
25template <
typename T>
class vec;
28template <
typename T, cgv::type::u
int32_type N>
42 typedef const T& const_reference;
44 typedef std::size_t size_type;
46 typedef std::ptrdiff_t difference_type;
50 typedef const T* const_pointer;
54 typedef const T* const_iterator;
56 typedef std::reverse_iterator<iterator> reverse_iterator;
58 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
68 fvec(
const T&
x) {
for(
unsigned i = 0; i < N; ++i) v[i] =
x; }
70 template <u
int32_t _N = N,
typename std::enable_if<_N >= 2,
int>::type = 0>
71 fvec(
const T&
x,
const T&
y) { set(
x,
y); }
73 template <u
int32_t _N = N,
typename std::enable_if<_N >= 3,
int>::type = 0>
74 fvec(
const T&
x,
const T&
y,
const T&
z) { set(
x,
y,
z); }
76 template <u
int32_t _N = N,
typename std::enable_if<_N >= 4,
int>::type = 0>
77 fvec(
const T&
x,
const T&
y,
const T&
z,
const T&
w) { set(
x,
y,
z,
w); }
81 memmove(v, a, min_n *
sizeof(T));
82 for(i = min_n; i < N; ++i) v[i] = T(0);
88 for(i = 0; i < min_n; ++i) v[i] = static_cast<T>(a[i]);
89 for(; i < N; ++i) v[i] = T(0);
93 fvec(
const fvec<S, N>& other) {
for(
unsigned i = 0; i < N; ++i) v[i] = static_cast<T>(other[i]); }
95 template <
typename S1,
typename S2>
96 fvec(
const fvec<S1, N - 1>& other, S2 s) {
for(
unsigned i = 0; i < N - 1; ++i) v[i] =
static_cast<T
>(other[i]); v[N - 1] =
static_cast<T
>(s); }
99 fvec(
const fvec<S, N + 1>& other) {
for(
unsigned i = 0; i < N; ++i) v[i] = static_cast<T>(other[i]); }
103 void assign(
const std::array<T, N>& a) {
for(
unsigned i = 0; i < N; ++i) v[i] = a[i]; }
105 template <u
int32_t _N = N,
typename std::enable_if<_N >= 2,
int>::type = 0>
106 void set(
const T&
x,
const T&
y) { v[0] =
x; v[1] =
y; }
108 template <u
int32_t _N = N,
typename std::enable_if<_N >= 3,
int>::type = 0>
109 void set(
const T&
x,
const T&
y,
const T&
z) { v[0] =
x; v[1] =
y; v[2] =
z; }
111 template <u
int32_t _N = N,
typename std::enable_if<_N >= 4,
int>::type = 0>
112 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; }
114 void fill(
const T&
x) {
for(
unsigned i = 0; i < N; ++i) v[i] =
x; }
116 void zeros() { fill(T(0)); }
118 void zerosh() {
for(
unsigned i = 0; i < N - 1; ++i) v[i] = T(0); v[N - 1] = T(1); }
120 void ones() { fill(T(1)); }
122 fvec<T, N + 1> lift()
const { fvec<T, N + 1> h; (fvec<T, N>&)h = *
this; h[N] = T(1);
return h; }
136 T&
x() {
return v[0]; }
138 const T&
x()
const {
return v[0]; }
140 T&
y() {
return v[1]; }
142 const T&
y()
const {
return v[1]; }
144 T&
z() {
return v[2]; }
146 const T&
z()
const {
return v[2]; }
148 T&
w() {
return v[3]; }
150 const T&
w()
const {
return v[3]; }
162 const T*
data()
const {
return v; }
170 iterator
end() {
return v + N; }
172 const_iterator
begin()
const {
return v; }
174 const_iterator
end()
const {
return v + N; }
176 reverse_iterator
rbegin() {
return reverse_iterator(
end()); }
178 reverse_iterator
rend() {
return reverse_iterator(
begin()); }
180 const_reverse_iterator
rbegin()
const {
return const_reverse_iterator(
end()); }
182 const_reverse_iterator
rend()
const {
return const_reverse_iterator(
begin()); }
196 template <
typename S>
199 template <
typename S>
202 template <
typename S>
205 template <
typename S>
208 template <
typename S>
211 template <
typename S>
214 template <
typename S>
217 template <
typename S>
232 template <
typename S>
234 for(
unsigned i = 0; i < N; ++i)
235 if(
operator[](i) !=
static_cast<T
>(v[i]))
return false;
239 template <
typename S>
241 for(
unsigned i = 0; i < N; ++i)
242 if(
operator[](i) !=
static_cast<T
>(v[i]))
return true;
252 for(
unsigned i = 0; i < N; ++i)
262 for(
unsigned i = 0; i < N; ++i)
263 v[i] = cgv::math::sign(v[i]);
267 for(
unsigned i = 0; i < N; ++i)
268 v[i] = cgv::math::step(v[i], r[i]);
272 if(std::numeric_limits<T>::is_signed) {
273 for(
unsigned i = 0; i < N; ++i)
274 v[i] = std::abs(v[i]);
279 for(
unsigned i = 0; i < N; ++i)
280 v[i] = std::ceil(v[i]);
284 for(
unsigned i = 0; i < N; ++i)
285 v[i] = std::floor(v[i]);
289 for(
unsigned i = 0; i < N; ++i)
290 v[i] = std::round(v[i]);
296 for(
unsigned i = 0; i < N; ++i)
303 if(std::abs(l) < std::numeric_limits<T>::epsilon())
306 for(
unsigned i = 0; i < N; ++i)
314#define CGV_MATH_FVEC_DECLARED
317template<
typename T, cgv::type::u
int32_type N>
318fvec<T, N> normalize(
const fvec<T, N>& v) { fvec<T, N> w(v); w.normalize();
return w; }
321template<
typename T, cgv::type::u
int32_type N>
322fvec<T, N> safe_normalize(
const fvec<T, N>& v) { fvec<T, N> w(v); w.safe_normalize();
return w; }
325template<
typename T, cgv::type::u
int32_type N>
326std::ostream&
operator<<(std::ostream& out,
const fvec<T, N>& v) {
327 for(
unsigned i = 0; i < N - 1; ++i)
335template<
typename T, cgv::type::u
int32_type N>
336std::istream& operator>>(std::istream& in, fvec<T, N>& v) {
337 for(
unsigned i = 0; i < N; ++i) {
339 if(in.fail() && i == 1) {
340 for(
unsigned i = 1; i < N; ++i)
349template<
typename T, cgv::type::u
int32_type N>
350std::string
to_string(
const fvec<T, N>& v) {
351 std::ostringstream ss;
357template<
typename T, cgv::type::u
int32_type N>
358bool from_string(
const std::string& s, fvec<T, N>& v) {
359 std::istringstream iss(s);
365template <
typename T, cgv::type::u
int32_type N>
366fvec<T, N> operator * (
const T& s,
const fvec<T, N>& v) { fvec<T, N> r = v; r *= s;
return r; }
369template <
typename T, cgv::type::u
int32_type N>
370fvec<T, N> operator / (
const T& s,
const fvec<T, N>& v) {
372 for(
unsigned i = 0; i < N; ++i)
378template <
typename T,
typename S, cgv::type::u
int32_type N>
379inline T dot(
const fvec<T, N>& v,
const fvec<S, N>& w) {
381 for(
unsigned i = 0; i < N; ++i)
382 r +=
static_cast<T
>(v[i] * w[i]);
388template <
typename T,
typename S, cgv::type::u
int32_type N>
389inline S dot_pos(
const fvec<T, N>& v,
const fvec<S, N + 1>& w) {
391 for(
unsigned i = 0; i < N; ++i)
397template <
typename T,
typename S, cgv::type::u
int32_type N>
398inline S dot_pos(
const fvec<T, N + 1>& v,
const fvec<S, N>& w) {
400 for(
unsigned i = 0; i < N; ++i)
407template <
typename T,
typename S, cgv::type::u
int32_type N>
408inline S dot_dir(
const fvec<T, N>& v,
const fvec<S, N + 1>& w) {
410 for(
unsigned i = 0; i < N; ++i)
416template <
typename T,
typename S, cgv::type::u
int32_type N>
417inline S dot_dir(
const fvec<T, N + 1>& v,
const fvec<S, N>& w) {
419 for(
unsigned i = 0; i < N; ++i)
425template <
typename T, cgv::type::u
int32_type N>
426inline fvec<T, N> cross(
const fvec<T, N>& v,
const fvec<T, N>& w) {
428 r[0] = v[1] * w[2] - v[2] * w[1];
429 r[1] = v[2] * w[0] - v[0] * w[2];
430 r[2] = v[0] * w[1] - v[1] * w[0];
435template <
typename T, cgv::type::u
int32_type N>
436inline T sqr_length(
const fvec<T, N>& v) {
return dot(v, v); }
439template <
typename T, cgv::type::u
int32_type N>
440inline T length(
const fvec<T, N>& v) {
return std::sqrt(dot(v, v)); }
443template <
typename T, cgv::type::u
int32_type N>
444inline fvec<T, N> sign(
const fvec<T, N>& v) { fvec<T, N> r(v); r.sign();
return r; }
447template <
typename T, cgv::type::u
int32_type N>
448inline fvec<T, N> step(
const fvec<T, N>& a,
const fvec<T, N>& b) { fvec<T, N> r(a); r.step(b);
return r; }
451template <
typename T, cgv::type::u
int32_type N>
452inline fvec<T, N> abs(
const fvec<T, N>& v) { fvec<T, N> r(v); r.abs();
return r; }
455template <
typename T, cgv::type::u
int32_type N>
456inline fvec<T, N> round(
const fvec<T, N>& v) { fvec<T, N> r(v); r.round();
return r; }
459template <
typename T, cgv::type::u
int32_type N>
460inline fvec<T, N> floor(
const fvec<T, N>& v) { fvec<T, N> r(v); r.floor();
return r; }
463template <
typename T, cgv::type::u
int32_type N>
464inline fvec<T, N> ceil(
const fvec<T, N>& v) { fvec<T, N> r(v); r.ceil();
return r; }
467template <
typename T, cgv::type::u
int32_type N>
468inline fvec<T, N + 1> hom(
const fvec<T, N>& v) {
470 for(
unsigned i = 0; i < N; ++i)
477template <
typename T, cgv::type::u
int32_type N>
479 return *(std::min_element(v.begin(), v.end()));
483template <
typename T, cgv::type::u
int32_type N>
484unsigned min_index(
const fvec<T, N>& v) {
485 return static_cast<unsigned>(std::distance(v.begin(), std::min_element(v.begin(), v.end())));
489template <
typename T, cgv::type::u
int32_type N>
491 return *(std::max_element(v.begin(), v.end()));
495template <
typename T, cgv::type::u
int32_type N>
496unsigned max_index(
const fvec<T, N>& v) {
497 return static_cast<unsigned>(std::distance(v.begin(), std::max_element(v.begin(), v.end())));
501template <
typename T, cgv::type::u
int32_type N>
502const fvec<T, N> lerp(
const fvec<T, N>& v0,
const fvec<T, N>& v1, T t) {
503 return (T(1) - t) * v0 + t * v1;
507template <
typename T, cgv::type::u
int32_type N>
508const fvec<T, N> lerp(
const fvec<T, N>& v0,
const fvec<T, N>& v1,
const fvec<T, N>& t) {
509 return (fvec<T, N>(1) - t) * v0 + t * v1;
513template <
typename T, cgv::type::u
int32_type N>
514const fvec<T, N> slerp(
const fvec<T, N>& v0,
const fvec<T, N>& v1, T t) {
515 T dotv0v1 = dot(v0, v1);
523 T theta = std::acos(dotv0v1) * t;
524 auto v2 = normalize(v1 - (dotv0v1)*v0);
525 return T(std::cos(theta)) * v0 + T(std::sin(theta)) * v2;
529template <
typename T, cgv::type::u
int32_type N>
530const fvec<T, N> min(
const fvec<T, N>& v, T s) {
532 for(
unsigned i = 0; i < N; ++i)
533 c(i) = std::min(v[i], s);
538template <
typename T, cgv::type::u
int32_type N>
539const fvec<T, N> min(
const fvec<T, N>& v0,
const fvec<T, N>& v1) {
541 for(
unsigned i = 0; i < N; ++i)
542 c[i] = std::min(v0[i], v1[i]);
547template <
typename T, cgv::type::u
int32_type N>
548const fvec<T, N> max(
const fvec<T, N>& v, T s) {
550 for(
unsigned i = 0; i < N; ++i)
551 c[i] = std::max(v[i], s);
556template <
typename T, cgv::type::u
int32_type N>
557const fvec<T, N> max(
const fvec<T, N>& v0,
const fvec<T, N>& v1) {
559 for(
unsigned i = 0; i < N; ++i)
560 c[i] = std::max(v0[i], v1[i]);
565template <
typename T, cgv::type::u
int32_type N>
566const fvec<T, N> clamp(
const fvec<T, N>& v, T l, T r) {
568 for(
unsigned i = 0; i < N; ++i)
569 c[i] = cgv::math::clamp(v[i], l, r);
574template <
typename T, cgv::type::u
int32_type N>
575const fvec<T, N> clamp(
const fvec<T, N>& v,
const fvec<T, N>& vl,
const fvec<T, N>&
vr) {
577 for(
unsigned i = 0; i < N; ++i)
578 c[i] = cgv::math::clamp(v[i], vl[i],
vr[i]);
583template <
typename T, cgv::type::u
int32_type N>
584const fvec<T, N> saturate(
const fvec<T, N>& v) {
585 return clamp(v, T(0), T(1));
589template <
typename T, cgv::type::u
int32_type N>
590const fvec<T, N> pow(
const fvec<T, N>& v, T e) {
592 for(
unsigned i = 0; i < N; ++i)
593 c[i] = std::pow(v[i], e);
598template <
typename T, cgv::type::u
int32_type N>
599const fvec<T, N> pow(
const fvec<T, N>& v,
const fvec<T, N>& e) {
601 for(
unsigned i = 0; i < N; ++i)
602 c[i] = std::pow(v[i], e[i]);
607template <
typename T, cgv::type::u
int32_type N>
608fvec<T, N> ortho(
const fvec<T, N>& v) =
delete;
612fvec<T, 2> ortho(
const fvec<T, 2>& v) {
613 return fvec<T, 2>(-v.y(), v.x());
618fvec<T, 3> ortho(
const fvec<T, 3>& v) {
619 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());
623template <
typename T, cgv::type::u
int32_type N>
624T to_angle(fvec<T, N> v) =
delete;
628T to_angle(fvec<T, 2> v) {
630 float a = std::atan2(v.y(), v.x());
632 a += T(2) *
static_cast<T
>(cgv::math::constants::pi);
637template <
typename T, cgv::type::u
int32_type N>
638T to_direction(fvec<T, N> v) =
delete;
642fvec<T, 2> to_direction(T a) {
643 return fvec<T, 2>(std::cos(a), std::sin(a));
721template <
typename T, cgv::type::u
int32_type N>
729template <
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.
reverse_iterator rend()
return a reverse iterator past the last component of the reversed *this that corresponds to the compo...
fvec(const fvec< S, N+1 > &other)
construct from vector of one dimension higher by dropping the highest dimension
T & z()
return third component
T & y()
return second component
T safe_normalize()
normalize the vector using the L2-Norm and return the length; if length is zero the vector remains un...
T normalize()
normalize the vector using the L2-Norm and return the length
static fvec< T, N > zeroh()
constuct a homogeneous zero-vector (yields same result as calling fvec<T, N-1>(0)....
T & operator()(int i)
return a reference to the component at specified index i
void round()
round componentwise
const T & z() const
return third component
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-=(const T &s)
in place subtraction by scalar s
T & operator[](int i)
return a reference to the component at specified index i
fvec< T, N > & operator/=(const T &s)
in place division by scalar s
void sign()
componentwise sign values
iterator begin()
return an iterator to the first component of *this
T * data()
return a pointer to the underlying array serving as component storage
T sqr_length() const
square length of vector
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< T, N > operator+(const fvec< S, N > &v) const
vector addition
fvec(const std::array< T, N > &a)
construct from std::array of same size
fvec< T, N > & operator+=(const T &s)
in place addition of a scalar s
const_reverse_iterator rbegin() const
return a reverse iterator to the first component of the reversed *this that corresponds to the last c...
fvec< T, N > operator/(const fvec< S, N > &v) const
componentwise vector division
bool operator!=(const fvec< S, N > &v) const
test for inequality
const_iterator end() const
return an iterator past the last component of *this
vec< T > to_vec() const
conversion to vector type
fvec(const T &x)
create a vector where all N components are initialized to the constant value x
const_reverse_iterator rend() const
return a reverse iterator past the last component of the reversed *this that corresponds to the compo...
reverse_iterator rbegin()
return a reverse iterator to the first component of the reversed *this that corresponds to the last c...
const T & y() const
return second component
iterator end()
return an iterator past the last component of *this
fvec()
create an uninitialized vector
const T & operator()(int i) const
return a reference to the component at specified index i
const T & w() const
return fourth component
T & x()
return first component
fvec< T, N > operator-() const
negate the vector
void abs()
componentwise absolute values
const T & x() const
return first component
void ceil()
ceil componentwise
const_iterator begin() const
return an iterator to the first component of *this
T & w()
return fourth component
void step(const fvec< T, N > &r)
componentwise sign values
const T * data() const
return a pointer to the underlying array serving as component storage
const T & operator[](int i) const
return a reference to the component at specified index i
void assign(const std::array< T, N > &a)
set to the contents of the given std::array with same size
static cgv::type::uint32_type size()
return number of components
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