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>
73 template <u
int32_t _N = N,
typename std::enable_if<_N >= 3,
int>
::type = 0>
76 template <u
int32_t _N = N,
typename std::enable_if<_N >= 4,
int>
::type = 0>
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; }
118 void zerosh() {
for(
unsigned i = 0; i < N - 1; ++i) v[i] = T(0); v[N - 1] = T(1); }
126 const fvec<T, N - 1>&
down()
const {
return *
reinterpret_cast<const fvec<T, N-1
>*>(
this); }
128 fvec<T, N - 1>&
down() {
return *
reinterpret_cast<fvec<T, N-1
>*>(
this); }
142 T&
x() {
return v[0]; }
144 const T&
x()
const {
return v[0]; }
146 T&
y() {
return v[1]; }
148 const T&
y()
const {
return v[1]; }
150 T&
z() {
return v[2]; }
152 const T&
z()
const {
return v[2]; }
154 T&
w() {
return v[3]; }
156 const T&
w()
const {
return v[3]; }
168 const T*
data()
const {
return v; }
176 iterator
end() {
return v + N; }
178 const_iterator
begin()
const {
return v; }
180 const_iterator
end()
const {
return v + N; }
182 reverse_iterator
rbegin() {
return reverse_iterator(
end()); }
184 reverse_iterator
rend() {
return reverse_iterator(
begin()); }
186 const_reverse_iterator
rbegin()
const {
return const_reverse_iterator(
end()); }
188 const_reverse_iterator
rend()
const {
return const_reverse_iterator(
begin()); }
202 template <
typename S>
205 template <
typename S>
208 template <
typename S>
211 template <
typename S>
214 template <
typename S>
217 template <
typename S>
220 template <
typename S>
223 template <
typename S>
238 template <
typename S>
240 for(
unsigned i = 0; i < N; ++i)
241 if(
operator[](i) !=
static_cast<T
>(v[i]))
return false;
245 template <
typename S>
247 for(
unsigned i = 0; i < N; ++i)
248 if(
operator[](i) !=
static_cast<T
>(v[i]))
return true;
252 template <
typename S>
254 for (
unsigned i = N; i > 0; ) {
255 if (
operator[](i) ==
static_cast<T
>(v[i]))
268 for(
unsigned i = 0; i < N; ++i)
278 for(
unsigned i = 0; i < N; ++i)
279 v[i] = cgv::math::sign(v[i]);
283 for(
unsigned i = 0; i < N; ++i)
284 v[i] = cgv::math::step(v[i], r[i]);
288 if(std::numeric_limits<T>::is_signed) {
289 for(
unsigned i = 0; i < N; ++i)
290 v[i] = std::abs(v[i]);
295 for(
unsigned i = 0; i < N; ++i)
296 v[i] = std::ceil(v[i]);
300 for(
unsigned i = 0; i < N; ++i)
301 v[i] = std::floor(v[i]);
305 for(
unsigned i = 0; i < N; ++i)
306 v[i] = std::round(v[i]);
312 for(
unsigned i = 0; i < N; ++i)
319 if(std::abs(l) < std::numeric_limits<T>::epsilon())
322 for(
unsigned i = 0; i < N; ++i)
330#define CGV_MATH_FVEC_DECLARED
333template<
typename T, cgv::type::u
int32_type N>
334fvec<T, N> normalize(
const fvec<T, N>& v) { fvec<T, N> w(v); w.normalize();
return w; }
337template<
typename T, cgv::type::u
int32_type N>
338fvec<T, N> safe_normalize(
const fvec<T, N>& v) { fvec<T, N> w(v); w.safe_normalize();
return w; }
341template<
typename T, cgv::type::u
int32_type N>
342std::ostream& operator<<(std::ostream& out,
const fvec<T, N>& v) {
343 for(
unsigned i = 0; i < N - 1; ++i)
351template<
typename T, cgv::type::u
int32_type N>
352std::istream& operator>>(std::istream& in,
fvec<T, N>& v) {
353 for(
unsigned i = 0; i < N; ++i) {
355 if(in.fail() && i == 1) {
356 for(
unsigned i = 1; i < N; ++i)
365template<
typename T, cgv::type::u
int32_type N>
367 std::ostringstream ss;
373template<
typename T, cgv::type::u
int32_type N>
374bool from_string(
const std::string& s,
fvec<T, N>& v) {
375 std::istringstream iss(s);
381template <
typename T, cgv::type::u
int32_type N>
385template <
typename T, cgv::type::u
int32_type N>
388 for(
unsigned i = 0; i < N; ++i)
394template <
typename T,
typename S, cgv::type::u
int32_type N>
397 for(
unsigned i = 0; i < N; ++i)
398 r +=
static_cast<T
>(v[i] * w[i]);
404template <
typename T,
typename S, cgv::type::u
int32_type N>
407 for(
unsigned i = 0; i < N; ++i)
413template <
typename T,
typename S, cgv::type::u
int32_type N>
416 for(
unsigned i = 0; i < N; ++i)
423template <
typename T,
typename S, cgv::type::u
int32_type N>
426 for(
unsigned i = 0; i < N; ++i)
432template <
typename T,
typename S, cgv::type::u
int32_type N>
435 for(
unsigned i = 0; i < N; ++i)
441template <
typename T, cgv::type::u
int32_type N>
444 r[0] = v[1] * w[2] - v[2] * w[1];
445 r[1] = v[2] * w[0] - v[0] * w[2];
446 r[2] = v[0] * w[1] - v[1] * w[0];
451template <
typename T, cgv::type::u
int32_type N>
452inline T sqr_length(
const fvec<T, N>& v) {
return dot(v, v); }
455template <
typename T, cgv::type::u
int32_type N>
456inline T length(
const fvec<T, N>& v) {
return std::sqrt(dot(v, v)); }
459template <
typename T, cgv::type::u
int32_type N>
462 for(
unsigned i = 0; i < N; ++i)
463 sum += std::numeric_limits<T>::is_signed ? std::abs(v[i]) : v[i];
468template <
typename T, cgv::type::u
int32_type N>
472template <
typename T, cgv::type::u
int32_type N>
476template <
typename T, cgv::type::u
int32_type N>
480template <
typename T, cgv::type::u
int32_type N>
484template <
typename T, cgv::type::u
int32_type N>
488template <
typename T, cgv::type::u
int32_type N>
492template <
typename T, cgv::type::u
int32_type N>
498template <
typename T, cgv::type::u
int32_type N>
500 return *(std::min_element(v.begin(), v.end()));
504template <
typename T, cgv::type::u
int32_type N>
506 return static_cast<unsigned>(std::distance(v.begin(), std::min_element(v.begin(), v.end())));
510template <
typename T, cgv::type::u
int32_type N>
512 return *(std::max_element(v.begin(), v.end()));
516template <
typename T, cgv::type::u
int32_type N>
518 return static_cast<unsigned>(std::distance(v.begin(), std::max_element(v.begin(), v.end())));
522template <
typename T, cgv::type::u
int32_type N>
524 return (T(1) - t) * v0 + t * v1;
528template <
typename T, cgv::type::u
int32_type N>
534template <
typename T, cgv::type::u
int32_type N>
536 T dotv0v1 = dot(v0, v1);
544 T theta = std::acos(dotv0v1) * t;
545 auto v2 = normalize(v1 - (dotv0v1)*v0);
546 return T(std::cos(theta)) * v0 + T(std::sin(theta)) * v2;
550template <
typename T, cgv::type::u
int32_type N>
553 for(
unsigned i = 0; i < N; ++i)
554 c(i) = std::min(v[i], s);
559template <
typename T, cgv::type::u
int32_type N>
562 for(
unsigned i = 0; i < N; ++i)
563 c[i] = std::min(v0[i], v1[i]);
568template <
typename T, cgv::type::u
int32_type N>
571 for(
unsigned i = 0; i < N; ++i)
572 c[i] = std::max(v[i], s);
577template <
typename T, cgv::type::u
int32_type N>
580 for(
unsigned i = 0; i < N; ++i)
581 c[i] = std::max(v0[i], v1[i]);
586template <
typename T, cgv::type::u
int32_type N>
589 for(
unsigned i = 0; i < N; ++i)
590 c[i] = cgv::math::clamp(v[i], l, r);
595template <
typename T, cgv::type::u
int32_type N>
598 for(
unsigned i = 0; i < N; ++i)
599 c[i] = cgv::math::clamp(v[i], vl[i], vr[i]);
604template <
typename T, cgv::type::u
int32_type N>
606 return clamp(v, T(0), T(1));
610template <
typename T, cgv::type::u
int32_type N>
613 for(
unsigned i = 0; i < N; ++i)
614 c[i] = std::pow(v[i], e);
619template <
typename T, cgv::type::u
int32_type N>
622 for(
unsigned i = 0; i < N; ++i)
623 c[i] = std::pow(v[i], e[i]);
628template <
typename T, cgv::type::u
int32_type N>
640 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());
644template <
typename T, cgv::type::u
int32_type N>
651 float a = std::atan2(v.y(), v.x());
653 a += T(2) *
static_cast<T
>(cgv::math::constants::pi);
658template <
typename T, cgv::type::u
int32_type N>
668template <
typename T, cgv::type::u
int32_type N>
670 return w * dot(v, w) / dot(w, w);
674template <
typename T, cgv::type::u
int32_type N>
676 return v - n * dot(v, n) / dot(n, n);
754template <
typename T, cgv::type::u
int32_type N>
762template <
typename T, cgv::type::u
int32_type N>
765 return fvec<T, N>(N < v.dim() ? N : v.dim(), &v[0]);
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 safe_normalize()
normalize the vector using the L2-Norm and return the length; if length is zero the vector remains un...
fvec(cgv::type::uint32_type n, const S *a)
create a vector from an array of size n of a different type; if n is less than N the remaining N-n co...
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
fvec< T, N+1 > lift() const
convert to homogeneous version by adding a 1
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 - 1 > & down()
provide reference to vector where last dimension is dropped
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
bool operator<(const fvec< S, N > &v) const
define smaller operator based on component comparison with significance increasing from 0 to N-1,...
T sqr_length() const
square length of vector
void ones()
fill the vector with ones
fvec(const fvec< S1, N - 1 > &other, S2 s)
construct from a vector of one dimension less plus a scalar
void fill(const T &x)
fill the vector with constant value x
const fvec< T, N - 1 > & down() const
provide const reference to vector where last dimension is dropped
fvec(const T &x, const T &y, const T &z, const T &w)
construct and initialize the first four components to the given values
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)
create a vector from an array of size n; if n is less than N the remaining N-n components are set to ...
void set(const T &x, const T &y, const T &z, const T &w)
set the first four components
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 unequal
const_iterator end() const
return an iterator past the last component of *this
fvec(const T &x, const T &y, const T &z)
construct and initialize the first three components to the given values
vec< T > to_vec() const
conversion to vector type
fvec< T, N - 1 > clip() const
convert back from homogeneous representation by performing a w-clip
void set(const T &x, const T &y, const T &z)
set the first three components
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...
fvec(const T &x, const T &y)
construct and initialize the first two components to the given values
void set(const T &x, const T &y)
set the first two components
reverse_iterator rbegin()
return a reverse iterator to the first component of the reversed *this that corresponds to the last c...
void zerosh()
fill the vector with zeros except for the last component, which will be set to one
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
fvec< T, N > operator-() const
negate the vector
fvec(const fvec< S, N > &other)
construct by copy from a vector of a different type
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
void zeros()
fill the vector with zeros
void step(const fvec< T, N > &r)
componentwise step function using stored values as edge
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
namespace for compile time type information
unsigned int uint32_type
this type provides an 32 bit unsigned integer type
this header is dependency free
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