13 #ifndef OGLPLUS_VECTOR_1107121519_HPP
14 #define OGLPLUS_VECTOR_1107121519_HPP
16 #include <oglplus/config/compiler.hpp>
17 #include <oglplus/utils/nothing.hpp>
23 #include <type_traits>
27 template <
typename T, std::
size_t Rows, std::
size_t Cols>
30 template<
typename T, std::
size_t R, std::
size_t C>
33 template <
typename T, std::
size_t N>
36 #if OGLPLUS_DOCUMENTATION_ONLY || defined(GL_INT)
62 #if OGLPLUS_DOCUMENTATION_ONLY || defined(GL_FLOAT)
88 #if OGLPLUS_DOCUMENTATION_ONLY || defined(GL_DOUBLE)
116 template <
typename T, std::
size_t N>
127 std::fill(_elem, _elem+N,
T(0));
132 std::fill(_elem, _elem+N, v);
137 std::copy(v, v+N, _elem);
145 std::copy(v, v+N, _elem);
151 std::copy(v, v+n, _elem);
152 std::fill(_elem+n, _elem+N, def);
155 template <std::
size_t M>
158 typename std::enable_if<(N < M)>::type* =
nullptr
161 std::copy(v.
Data(), v.
Data()+N, _elem);
164 template <
typename U, std::
size_t M>
166 const VectorBase<U, M>& v,
167 typename std::enable_if<
168 (N <= M) && (!std::is_same<T, U>::value)
172 for(std::size_t i=0; i!=N; ++i)
173 _elem[i] =
T(v.At(i));
176 explicit VectorBase(
const Matrix<T, 1, N>& matrix)
178 for(std::size_t i=0; i!=N; ++i)
179 _elem[i] =
At(matrix, 0, i);
182 template <std::
size_t M>
184 const Matrix<T, M, 1>& matrix,
185 typename std::enable_if<M != 1 && M == N, void>::type* =
nullptr
188 for(std::size_t i=0; i!=N; ++i)
189 _elem[i] =
At(matrix, i, 0);
194 #if !OGLPLUS_NO_DEFAULTED_FUNCTIONS
236 T At(std::size_t i,
T fallback)
const
238 if(i < N)
return _elem[i];
239 else return fallback;
265 for(std::size_t i=0; i!=N; ++i)
266 if(a._elem[i] != b._elem[i])
274 for(std::size_t i=0; i!=N; ++i)
275 _elem[i] += v._elem[i];
281 for(std::size_t i=0; i!=N; ++i)
282 _elem[i] -= v._elem[i];
288 for(std::size_t i=0; i!=N; ++i)
295 for(std::size_t i=0; i!=N; ++i)
296 _elem[i] *= that._elem[i];
302 for(std::size_t i=0; i!=N; ++i)
309 for(std::size_t i=0; i!=N; ++i)
310 _elem[i] /= that._elem[i];
322 return std::abs(
DotProduct(*
this, *
this) -
T(1)) <= eps;
329 if(l !=
T(0) && l !=
T(1))
336 T result = (a._elem[0] * b._elem[0]);
337 for(std::size_t i=1; i!=N; ++i)
338 result += (a._elem[i] * b._elem[i]);
344 #include <oglplus/math/vector_1.ipp>
345 #include <oglplus/math/vector_2.ipp>
346 #include <oglplus/math/vector_3.ipp>
347 #include <oglplus/math/vector_4.ipp>
349 #if OGLPLUS_DOCUMENTATION_ONLY || !OGLPLUS_NO_VARIADIC_TEMPLATES
350 #include <oglplus/math/vector_n.ipp>
353 #include <oglplus/math/vector_swizzle.ipp>
355 template <
typename T, std::
size_t N>
361 template <
typename T, std::
size_t N>
367 template <
typename T, std::
size_t N>
373 template <
typename T, std::
size_t N>
374 inline T At(
const Vector<T, N>& a, std::size_t i, T fallback)
376 return a.At(i, fallback);
379 template <
typename T, std::
size_t N>
380 inline Vector<T, 1> Extract(
381 const Vector<T, N>& a,
385 return Vector<T, 1>(a[d0]);
388 template <
typename T, std::
size_t N>
389 inline Vector<T, 2> Extract(
390 const Vector<T, N>& a,
395 return Vector<T, 2>(a[d0], a[d1]);
398 template <
typename T, std::
size_t N>
399 inline Vector<T, 3> Extract(
400 const Vector<T, N>& a,
406 return Vector<T, 3>(a[d0], a[d1], a[d2]);
409 template <
typename T, std::
size_t N>
410 inline Vector<T, 4> Extract(
411 const Vector<T, N>& a,
418 return Vector<T, 4>(a[d0], a[d1], a[d2], a[d3]);
421 template <
typename T, std::
size_t N>
427 template <
typename T, std::
size_t N>
430 return std::sqrt(Dot(a, a));
434 template <
typename T, std::
size_t N>
437 return Length(Subtracted(a, b));
440 template <
typename T, std::
size_t N>
444 if(l != T(0) && l != T(1))
445 a = Multiplied(a, T(1) / l);
449 template <
typename T>
455 template <
typename T>
456 inline Vector<T, 3> Cross(
const Vector<T, 3>& a,
const Vector<T, 3>& b)
459 a[1] * b[2] - a[2] * b[1],
460 a[2] * b[0] - a[0] * b[2],
461 a[0] * b[1] - a[1] * b[0]
465 template <
typename T, std::
size_t N>
466 inline bool operator == (
const Vector<T, N>& a,
const Vector<T, N>& b)
471 template <
typename T, std::
size_t N>
472 inline bool operator != (
const Vector<T, N>& a,
const Vector<T, N>& b)
477 template <
typename T, std::
size_t N>
483 template <
typename T, std::
size_t N>
489 template <
typename T, std::
size_t N>
495 template <
typename T, std::
size_t N>
498 return Subtracted(a, b);
501 template <
typename T,
typename V, std::
size_t N>
502 inline typename std::enable_if<
503 std::is_convertible<V, T>::value,
507 return Multiplied(a, T(v));
510 template <
typename T,
typename V, std::
size_t N>
511 inline typename std::enable_if<
512 std::is_convertible<V, T>::value,
514 >::type operator * (V v,
const Vector<T, N>& a)
516 return Multiplied(a, T(v));
520 template <
typename T,
typename V, std::
size_t N>
521 inline typename std::enable_if<
522 std::is_convertible<V, T>::value,
524 >::type operator / (
const Vector<T, N>& a, V v)
526 return Divided(a, v);
529 template <
typename T, std::
size_t N, std::
size_t Cols>
536 for(std::size_t c=0; c!=Cols; ++c)
539 for(std::size_t r=0; r!=N; ++r)
541 tmp[c] += v.
At(r) * m.
At(r, c);
547 template <
typename T, std::
size_t N, std::
size_t Rows>
554 for(std::size_t r=0; r!=Rows; ++r)
557 for(std::size_t c=0; c!=N; ++c)
559 tmp[r] += m.
At(r, c) * v.
At(c);
568 #endif // include guard
Vector< GLfloat, 4 > Vec4f
4D float vector
Definition: vector.hpp:85
friend bool Equal(const VectorBase &a, const VectorBase &b)
Equality comparison.
Definition: vector.hpp:263
Common base class for vectors.
Definition: vector.hpp:117
Vector< GLdouble, 1 > Vec1d
1D double-precision (degenerate) vector
Definition: vector.hpp:93
T * Data(void)
Pointer to the components of this vector.
Definition: vector.hpp:211
Vector< GLint, 3 > Vec3i
3D int vector
Definition: vector.hpp:53
Vector< GLfloat, 2 > Vec2f
2D float vector
Definition: vector.hpp:73
Vector< GLfloat, 3 > Vec3f
3D float vector
Definition: vector.hpp:79
bool IsNormal(T eps=T(0)) const
Returns true if the vector is normal.
Definition: vector.hpp:320
void Multiply(T v)
Multiplies this vector by a scalar value.
Definition: vector.hpp:286
T At(const Vector &vector, std::size_t i) const T x(void) const
Returns the value of the i-th coordinate of the vector.
Definition: vector.hpp:221
void Divide(T v)
Divides this vector by a scalar value.
Definition: vector.hpp:300
Vector< GLdouble, 4 > Vec4d
4D double-precision vector
Definition: vector.hpp:111
Vector< GLdouble, 3 > Vec3d
3D double-precision vector
Definition: vector.hpp:105
static std::size_t Size(void)
The size (the number of components) of this vector.
Definition: vector.hpp:205
T Length(void) const
Returns the lenght of this vector.
Definition: vector.hpp:314
static T DotProduct(const VectorBase &a, const VectorBase &b)
Computes the dot product of vectors a and b.
Definition: vector.hpp:334
Vector< GLint, 1 > Vec1i
1D int (degenerate) vector
Definition: vector.hpp:34
friend const T * Data(const Vector &vector)
Returns a pointer to an array containing the vectors coordinates.
Definition: vector.hpp:356
void Normalize(void)
Normalizes this vector.
Definition: vector.hpp:326
Vector< GLint, 2 > Vec2i
2D int vector
Definition: vector.hpp:47
T At(std::size_t i, T fallback) const
Access to the i-th component of this vector with a fallback.
Definition: vector.hpp:236
T At(std::size_t i, std::size_t j) const
Returns the value of the element at position i, j.
Definition: matrix.hpp:416
void Divide(const VectorBase &that)
Divides the elements of this and that vector.
Definition: vector.hpp:307
const T * Data(void) const
Pointer to the components of this vector.
Definition: vector.hpp:217
void Multiply(const VectorBase &that)
Multiplies the elements of this and that vector.
Definition: vector.hpp:293
Vector< GLdouble, 2 > Vec2d
2D double-precision vector
Definition: vector.hpp:99
Base template for Matrix.
Definition: fwd.hpp:63
void Add(const VectorBase &v)
Adds v to this vector.
Definition: vector.hpp:272
Vector< GLint, 4 > Vec4i
4D int vector
Definition: vector.hpp:59
Basic template for vector types.
Definition: fwd.hpp:43
void Subtract(const VectorBase &v)
Subtracts v from this vector.
Definition: vector.hpp:279
T At(std::size_t i) const
Access to the i-th component of this vector.
Definition: vector.hpp:226
T & operator[](std::size_t i)
Access to the i-th component of this vector.
Definition: vector.hpp:246
Vector< GLfloat, 1 > Vec1f
1D float (degenerate) vector
Definition: vector.hpp:67
Definition: vector.hpp:14