Go to the documentation of this file.
32 #include <QTextStream>
48 template <
size_t dim,
class T =
float>
class Vector {
88 for(
size_t i = 0;
i < d1; ++
i)
90 for(
size_t i = d1;
i < dim; ++
i)
93 for(
size_t i = 0;
i < dim; ++
i)
110 for(
size_t i = 0;
i < dim;
i++)
121 for(
size_t i = 0;
i < dim; ++
i)
152 explicit Vector(
const T&
x,
const T&
y,
const T&
z,
const T&
t)
165 Vector(
const T&
x,
const T&
y,
const T&
z,
const T& a,
const T& b)
179 Vector(
const T&
x,
const T&
y,
const T&
z,
const T& a,
const T& b,
const T& c)
194 Vector(
const T&
x,
const T&
y,
const T&
z,
const T& a,
const T& b,
const T& c,
const T& d)
210 Vector(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e,
const T& f,
const T& g,
const T& h)
227 Vector(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e,
const T& f,
228 const T& g,
const T& h,
const T&
i)
246 Vector(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e,
const T& f,
const T& g,
247 const T& h,
const T&
i,
const T&
j,
const T&
k,
const T&
l)
268 Vector(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e,
const T& f,
269 const T& g,
const T& h,
const T&
i,
const T&
j,
const T&
k,
const T&
l,
270 const T& m,
const T&
n,
const T& o,
const T& p,
const T& q,
const T& r)
297 static size_t size() {
return dim; }
340 for(
size_t i = 0;
i < dim;
i++)
375 for(
size_t i = 0;
i < dim;
i++)
419 for(
size_t i = 0;
i < dim; ++
i)
431 for(
size_t i = 0;
i < dim;
i++)
432 ans[
i] = scalar * vec.
elems[
i];
444 for(
size_t i = 0;
i < dim;
i++)
468 for(
size_t i = 0;
i < dim;
i++)
479 for(
size_t i = 0;
i < dim;
i++)
490 for(
size_t i = 0;
i < dim;
i++)
501 for(
size_t i = 0;
i < dim;
i++)
512 for(
size_t i = 0;
i < dim;
i++)
523 for(
size_t i = 0;
i < dim;
i++)
534 for(
size_t i = 0;
i < dim; ++
i)
545 for(
size_t i = 0;
i < dim; ++
i)
556 for(
size_t i = 0;
i < dim;
i++)
568 return (!((*
this) == vec));
595 return std::sqrt((
float)
normsq());
597 return std::sqrt(
normsq());
608 for(
size_t i = 0;
i < dim;
i++)
621 return ((*
this) /= sz);
636 for(
size_t i = 0;
i < dim;
i++)
644 for(
size_t i = 0;
i < dim;
i++)
674 void set(
const T&
x,
const T&
y,
const T&
z)
686 void set(
const T&
x,
const T&
y,
const T&
z,
const T&
t)
701 for(
size_t i = 0;
i < dim; ++
i) {
714 return (*
this) ^ other;
953 for(
size_t i = 0;
i < dim; ++
i) {
970 for(
size_t i = 0;
i < dim; ++
i) {
987 for(
size_t i = 0;
i < dim; ++
i) {
1004 for(
size_t i = 0;
i < dim; ++
i) {
1015 for(
size_t i = 0;
i < dim;
i++) {
1026 for(
size_t i = 1;
i < dim && in;
i++)
1027 in >> std::ws >> vec[
i];
1034 for(
size_t i = 0;
i < dim;
i++) {
1045 for(
size_t i = 1;
i < dim && !in.atEnd();
i++)
1071 return ((v1[0] * v2[1]) - (v1[1] * v2[0]));
1104 ans[0] = v1[1] * v2[2] - v1[2] * v2[1];
1105 ans[1] = v1[2] * v2[0] - v1[0] * v2[2];
1106 ans[2] = v1[0] * v2[1] - v1[1] * v2[0];
1118 return atan2(v.
y(), v.
x());
1130 float y =
norm(v1 ^ v2);
1155 return (v1 * v2 < 0) ? -1 : 1;
1170 return atan2(-y, x);
1211 return (s < 0) ? -s : s;
1259 for(
size_t i = 0; i < dim; ++i) {
1260 result[i] = fabs(v[i]);
1275 for(
size_t i = 0; i < dim; ++i) {
1276 result[i] = (v1[i] >= v2[i] ? v1[i] : v2[i]);
1291 for(
size_t i = 0; i < dim; ++i) {
1292 result[i] = (v1[i] <= v2[i] ? v1[i] : v2[i]);
1302 template <
size_t dim,
typename T>
1306 result =
max(minv,
min(maxv, v));
1316 template <
size_t dim,
typename T>
1320 for(
size_t i = 0; i < dim; ++i) {
1321 result[i] = v1[i] * v2[i];
1347 template <
size_t dim,
typename T>
1351 for(
size_t i = 0; i < dim; ++i) {
1352 result[i] = v1[i] / v2[i];
1365 const float ratio = 1 - 1e-8;
1366 if((std::abs(v.
y()) >= ratio * std::abs(v.
x())) && (std::abs(v.
z()) >= ratio * std::abs(v.
x())))
1368 else if((std::abs(v.
x()) >= ratio * std::abs(v.
y())) && (std::abs(v.
z()) >= ratio * std::abs(v.
y())))
1379 template <
size_t dim,
typename T,
typename T1>
1383 for(
size_t i = 0; i < dim; ++i) {
1384 result[i] = (*fct)(v[i]);
1394 template <
size_t dim,
typename T,
typename T1>
1398 for(
size_t i = 0; i < dim; ++i) {
1399 result[i] = (*fct)(v[i]);
1413 for(
size_t i = 0; i < dim; ++i) {
1414 result[i] = (*fct)(v[i]);
1424 template <
size_t dim,
typename T>
1429 for(
size_t i = 0; i < dim; ++i) {
1430 result[i] = (*fct)(v1[i], v2[i]);
1440 template <
size_t dim,
typename T>
1444 for(
size_t i = 0; i < dim; ++i) {
1445 result[i] = (*fct)(v1[i], v2[i]);
1455 template <
size_t dim,
typename T>
1459 for(
size_t i = 0; i < dim; ++i) {
1460 result[i] = (*fct)(v1[i], v2[i]);
1470 template <
size_t dim,
typename T,
typename T1,
typename T2>
1475 for(
size_t i = 0; i < dim; ++i) {
1476 result[i] = (*fct)(v1[i], v2[i]);
1486 template <
size_t dim,
typename T,
typename T1,
typename T2>
CU_HOST_DEVICE
1491 for(
size_t i = 0; i < dim; ++i) {
1492 result[i] = (*fct)(v1[i], v2[i]);
1502 template <
size_t dim,
typename T,
typename T1,
typename T2>
CU_HOST_DEVICE
1506 for(
size_t i = 0; i < dim; ++i) {
1507 result[i] = (*fct)(v1[i], v2[i]);
1521 for(
size_t i = 0; i < dim; ++i)
1522 res[i] = v[i] + value;
1535 for(
size_t i = 0; i < dim; ++i)
1536 res[i] = v[i] + value;
1549 for(
size_t i = 0; i < dim; ++i)
1550 res[i] = v[i] - value;
1564 for(
size_t i = 0; i < dim; ++i)
1565 res[i] = value - v[i];
1594 Vector<dim - 1, T> res(v);
1603 #include <boost/functional/hash.hpp>
1608 template <
size_t dim,
typename T>
struct hash<
mgx::Vector<dim, T> > {
1611 std::size_t seed = 0;
1612 for(
size_t i = 1; i < dim; ++i)
1613 boost::hash_combine(seed, v[i]);
CU_HOST_DEVICE void set(const T &x, const T &y, const T &z)
Set the values of a 3-D vector.
CU_HOST_DEVICE T * data()
Returns a raw pointer on the data.
CU_HOST_DEVICE const_iterator begin() const
Stl-iteration constant begin.
CU_HOST_DEVICE float angle(const Vector< 2, T > &v1, const Vector< 2, T > &v2)
Oriented angle between v1 and v2.
CU_HOST_DEVICE Vector< dim, T > map(const T &(*fct)(const T &, const T &), const Vector< dim, T > &v1, const Vector< dim, T > &v2)
Map a function to all elements of a vector.
CU_HOST_DEVICE Vector & operator/=(const T1 &scalar)
In-place division by a scalar.
CU_HOST_DEVICE Vector< dim, T > map(T(*fct)(T1, T2), const Vector< dim, T1 > &v1, const Vector< dim, T2 > &v2)
Map a function to all elements of a vector.
CU_HOST_DEVICE T & k()
Short access to the third element.
CU_HOST_DEVICE Vector & operator=(const T &value)
Set all the elements to value.
CU_HOST_DEVICE Vector< dim, T > operator+(const Vector< dim, T > &v, const T &value)
Add a value to all elements of a vector.
CU_HOST_DEVICE Vector(const T &a, const T &b, const T &c, const T &d, const T &e, const T &f, const T &g, const T &h)
Initialize a 8D vector.
const CU_HOST_DEVICE T & operator[](size_t idx) const
Access to the element idx.
CU_HOST_DEVICE Vector< 2, T > projectXY(void)
Extract the two first elements of the vector.
CU_HOST_DEVICE Vector(const T &a, const T &b, const T &c, const T &d, const T &e, const T &f, const T &g, const T &h, const T &i, const T &j, const T &k, const T &l, const T &m, const T &n, const T &o, const T &p, const T &q, const T &r)
Initialize a 18D vector.
CU_HOST_DEVICE bool operator==(const Vector &vec) const
Element-wise equality.
size_t operator()(const mgx::Vector< dim, T > &v) const
CU_HOST_DEVICE iterator end()
STL-iteration end.
CU_HOST_DEVICE Vector(const T &x, const T &y, const T &z, const T &a, const T &b, const T &c)
Initialize a 6D vector.
CU_HOST_DEVICE T & z()
Short access to the third element.
friend std::ostream & operator<<(std::ostream &out, const Vector &vec)
CU_HOST_DEVICE Vector normalized(void) const
Returns a normalized version of the vector.
CU_HOST_DEVICE const_iterator end() const
Stl-iteration constant end.
CU_HOST_DEVICE void l(const T &v)
Short access to the fourth element.
CU_HOST_DEVICE Vector(const T &a, const T &b, const T &c, const T &d, const T &e, const T &f, const T &g, const T &h, const T &i)
Initialize a 9D vector.
CU_HOST_DEVICE float normalized(float)
Normalized real.
CU_HOST_DEVICE Vector mult(const Vector &vec) const
Element-wise multiplcation.
CU_HOST_DEVICE Vector< dim, T > map(T(*fct)(const T1 &, const T2 &), const Vector< dim, T1 > &v1, const Vector< dim, T2 > &v2)
Map a function to all elements of a vector.
CU_HOST_DEVICE bool operator<(const Vector &other) const
Comparison operator.
CU_HOST_DEVICE Vector< dim, T > operator-(const Vector< dim, T > &v, const T &value)
Substact a value to all elements of a vector.
CU_HOST_DEVICE Vector & operator/=(const Vector &vec)
In-place element-wise division by a scalar.
CU_HOST_DEVICE Vector< 3, T > operator%(const Vector< 3, T > &v1, const Vector< 3, T > &v2)
Cross product v1 x v2.
CU_HOST_DEVICE Vector< dim, T > fabs(const Vector< dim, T > &v)
Return the vector whose component is the absolute value of the input vector.
CU_HOST_DEVICE bool operator>=(const Vector &other) const
Comparison operator.
CU_HOST_DEVICE Vector< dim+1, T > homogeneous(const Vector< dim, T > &v)
Create a homogeneous coordinate vector from a cartesian one.
CU_HOST_DEVICE Vector< dim, T > operator+(const T &value, const Vector< dim, T > &v)
Add a value to all elements of a vector.
CU_HOST_DEVICE T norm() const
Euclidean norm of the vector.
CU_HOST_DEVICE Vector & operator+=(const T &val)
In-place constant addition.
CU_HOST_DEVICE void z(const T &v)
Short access to the third element.
CU_HOST_DEVICE Vector< dim - 1, T > cartesian(const Vector< dim, T > &v)
Extract the cartesion coordinates from a homogeneous vector.
CU_HOST_DEVICE Vector< dim, T > max(const Vector< dim, T > &v1, const Vector< dim, T > &v2)
Return the vector whose component is the max of the two input vectors components.
CU_HOST_DEVICE T & x()
Short access to the first element.
CU_HOST_DEVICE Vector< dim, T > divide(const Vector< dim, T > &v1, const Vector< dim, T > &v2)
Return the vector whose component is the ratio of the two input vectors components.
CU_HOST_DEVICE Vector operator-(void) const
Vector negation.
CU_HOST_DEVICE friend Vector operator*(const T &scalar, const Vector &vec)
Multiplication by a scalar.
CU_HOST_DEVICE T norm(const Vector< dim, T > &v)
Function-version of the norm.
CU_HOST_DEVICE Vector & operator/=(const T &scalar)
In-place division by a scalar.
const CU_HOST_DEVICE T & l() const
Short access to the fourth element.
CU_HOST_DEVICE float normsq(float s)
Euclidean square norm of a real.
CU_HOST_DEVICE Vector operator/(const T &scalar) const
Division by a scalar.
CU_HOST_DEVICE T operator*(const Vector &vec) const
Dot product.
CU_HOST_DEVICE Vector(const T &x, const T &y, const T &z)
Initialize a 3D vector.
CU_HOST_DEVICE void t(const T &v)
Short access to the fourth element.
Distributed matrix library.
CU_HOST_DEVICE Vector & operator+=(const Vector &vec)
Vector copy.
const typedef T & const_reference_type
CU_HOST_DEVICE float angle(const Vector< 3, T > &v1, const Vector< 3, T > &v2)
Non-oriented angle between v1 and v2.
CU_HOST_DEVICE Vector operator/(const Vector &vec) const
Element-wise division.
friend std::istream & operator>>(std::istream &in, Vector &vec)
CU_HOST_DEVICE Vector< dim, T > min(const Vector< dim, T > &v1, const Vector< dim, T > &v2)
Return the vector whose component is the min of the two input vectors components.
friend QTextStream & operator<<(QTextStream &out, const Vector &vec)
CU_HOST_DEVICE T & t()
Short access to the fourth element.
CU_HOST_DEVICE T & j()
Short access to the second element.
CU_HOST_DEVICE T normsq() const
Square of the Euclidean norm of the vector.
T CU_HOST_DEVICE max(const T a, const T b)
CU_HOST_DEVICE Vector & operator*=(const T1 &scalar)
In-place multiplication by a scalar.
CU_HOST_DEVICE Vector operator+(const Vector &vec) const
Vector addition.
CU_HOST_DEVICE void j(const T &v)
Short access to the second element.
CU_HOST_DEVICE Vector(const T &x=T())
Initialize a vector with all values to x.
CU_HOST_DEVICE Vector cross(const Vector &other) const
Compute the cross product as this x other.
CU_HOST_DEVICE Vector & operator*=(const T &scalar)
In-place multiplication by a scalar.
CU_HOST_DEVICE bool operator>(const Vector &other) const
Comparison operator.
CU_HOST_DEVICE void y(const T &v)
Short access to the second element.
CU_HOST_DEVICE Vector< dim, T > map(const T &(*fct)(const T1 &, const T2 &), const Vector< dim, T1 > &v1, const Vector< dim, T2 > &v2)
Map a function to all elements of a vector.
const CU_HOST_DEVICE T & x() const
Short access to the first element.
CU_HOST_DEVICE void i(const T &v)
Short access to the first element.
CU_HOST_DEVICE float norm(float s)
Euclidean norm of a real.
CU_HOST_DEVICE Vector< dim, T > normalized(const Vector< dim, T > &v)
Function-version of the square norm.
const CU_HOST_DEVICE T & y() const
Short access to the second element.
CU_HOST_DEVICE void set(const T &x)
Set the values of a 1-D vector.
static CU_HOST_DEVICE size_t size()
Returns the size of the vector (i.e.
CU_HOST_DEVICE Vector(const Vec &el)
Initialize a vector from any object behaving like an array.
CU_HOST_DEVICE T & operator[](size_t idx)
Access to the element idx.
CU_HOST_DEVICE Vector(const T &x, const T &y)
Initialize a 2D vector.
CU_HOST_DEVICE Vector(const T &a, const T &b, const T &c, const T &d, const T &e, const T &f, const T &g, const T &h, const T &i, const T &j, const T &k, const T &l)
Initialize a 12D vector.
CU_HOST_DEVICE bool operator<=(const Vector &other) const
Comparison operator.
friend QTextStream & operator>>(QTextStream &in, Vector &vec)
const CU_HOST_DEVICE T & t() const
Short access to the fourth element.
Namespace containing all the utility classes.
CU_HOST_DEVICE void set(const T &x, const T &y)
Set the values of a 2-D vector.
CU_HOST_DEVICE Vector< dim, T > trim(const Vector< dim, T > &v, const Vector< dim, T > &minv, const Vector< dim, T > &maxv)
Return the vector with components clipped to min and max vectors.
ScalarT norm(DVector< DistNhbdT, MatrixT, VectorT, ScalarT > &v)
CU_HOST_DEVICE float angle(const Vector< 3, T > &v1, const Vector< 3, T > &v2, const Vector< 3, T > &ref)
Oriented angle between v1 and v2 with ref to orient the space.
CU_HOST_DEVICE Vector< dim, T > map(T(*fct)(T1), const Vector< dim, T1 > &v)
Map a function to all elements of a vector.
CU_HOST_DEVICE Vector< dim, T > operator-(const T &value, const Vector< dim, T > &v)
Equivalent to substracting a vector with all component the same to another one.
CU_HOST_DEVICE T normsq(const Vector< dim, T > &v)
Function-version of the square norm.
CU_HOST_DEVICE T min(const T a, const T b)
const typedef T * const_iterator
const CU_HOST_DEVICE T * c_data() const
Returns a constant raw pointer on the data.
CU_HOST_DEVICE Vector< dim, T > multiply(const Vector< dim, T > &v1, const Vector< dim, T > &v2)
Return the vector whose component is the product of the two input vectors components.
CU_HOST_DEVICE float angle(const Vector< 1, T > &v1, const Vector< 1, T > &v2)
Oriented angle between v1 and v2.
const typedef T * const_pointer_type
CU_HOST_DEVICE T & i()
Short access to the first element.
const CU_HOST_DEVICE T & z() const
Short access to the third element.
CU_HOST_DEVICE Vector(const Vector< d1, T1 > &vec)
Copy another vector with different number of elements.
CU_HOST_DEVICE T & l()
Short access to the fourth element.
CU_HOST_DEVICE Vector & operator-=(const Vector &vec)
In-place vector subtraction.
CU_HOST_DEVICE iterator begin()
STL-iteration begin.
CU_HOST_DEVICE Vector< dim, T > map(T(*fct)(const T &, const T &), const Vector< dim, T > &v1, const Vector< dim, T > &v2)
Map a function to all elements of a vector.
CU_HOST_DEVICE Vector< dim, T > map(T(*fct)(T, T), const Vector< dim, T > &v1, const Vector< dim, T > &v2)
Map a function to all elements of a vector.
CU_HOST_DEVICE void set(const T &x, const T &y, const T &z, const T &t)
Set the values of a 4-D vector.
CU_HOST_DEVICE T & y()
Short access to the second element.
CU_HOST_DEVICE void x(const T &v)
Short access to the first element.
const CU_HOST_DEVICE T & j() const
Short access to the second element.
CU_HOST_DEVICE T operator^(const Vector< 1, T > &, const Vector< 1, T > &)
Cross product v1 x v2 (French notation)
CU_HOST_DEVICE Vector(const T &x, const T &y, const T &z, const T &t)
Initialize a 4D vector.
CU_HOST_DEVICE T operator%(const Vector< 2, T > &v1, const Vector< 2, T > &v2)
Cross product v1 x v2.
CU_HOST_DEVICE Vector operator*(const T &scalar) const
Multiplication by a scalar.
CU_HOST_DEVICE float angle(const Vector< 2, T > &v)
Angle of the vector with (0,1)
CU_HOST_DEVICE Vector< 3, T > operator^(const Vector< 3, T > &v1, const Vector< 3, T > &v2)
Cross product v1 x v2 (French notation)
CU_HOST_DEVICE Vector & operator-=(const T &val)
In-place value subtraction.
static const size_t numElems
CU_HOST_DEVICE bool operator!=(const Vector &vec) const
Element-wise inequality.
CU_HOST_DEVICE Vector & normalize(void)
Normalize the vector.
CU_HOST_DEVICE T operator^(const Vector< 2, T > &v1, const Vector< 2, T > &v2)
Cross product v1 x v2 (French notation)
CU_HOST_DEVICE Vector(const T &x, const T &y, const T &z, const T &a, const T &b, const T &c, const T &d)
Initialize a 7D vector.
CU_HOST_DEVICE Vector< 3, T > orthogonal(const Vector< 3, T > &v)
Find a vector orthogonal to v.
CU_HOST_DEVICE void k(const T &v)
Short access to the third element.
const CU_HOST_DEVICE T & k() const
Short access to the third element.
const CU_HOST_DEVICE T & i() const
Short access to the first element.
CU_HOST_DEVICE Vector< dim, T > map(const T &(*fct)(const T1 &), const Vector< dim, T1 > &v)
CU_HOST_DEVICE Vector operator-(const Vector &vec) const
Vector subtraction.
CU_HOST_DEVICE Vector(const T &x, const T &y, const T &z, const T &a, const T &b)
Initialize a 5D vector.