#ifndef Magnum_Math_Vector_h #define Magnum_Math_Vector_h /* Copyright © 2010, 2011, 2012 Vladimír Vondruš This file is part of Magnum. Magnum is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3 only, as published by the Free Software Foundation. Magnum is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License version 3 for more details. */ /** @file * @brief Class Magnum::Math::Vector */ #include "RectangularMatrix.h" namespace Magnum { namespace Math { template class Vector; #ifndef DOXYGEN_GENERATING_OUTPUT namespace Implementation { /* Implementation for Vector::from(const Vector&) */ template inline constexpr Math::Vector vectorFrom(Sequence, const Math::Vector& vector) { return {T(vector[sequence])...}; } } #endif /** @brief %Vector @configurationvalueref{Magnum::Math::Vector} @todo Constexprize all for loops */ template class Vector: public RectangularMatrix<1, s, T> { public: const static size_t size = s; /**< @brief %Vector size */ /** * @brief %Vector from another of different type * * Performs only default casting on the values, no rounding or * anything else. Example usage: * @code * Vector<4, float> floatingPoint(1.3f, 2.7f, -15.0f, 7.0f); * Vector<4, int> integral(Vector<4, int>::from(floatingPoint)); * // integral == {1, 2, -15, 7} * @endcode */ template inline constexpr static Vector from(const Vector& other) { return Implementation::vectorFrom(typename Implementation::GenerateSequence::Type(), other); } /** * @brief Dot product * * @f[ * a \cdot b = \sum_{i=0}^{n-1} a_ib_i * @f] * @see dot() const */ static T dot(const Vector& a, const Vector& b) { T out(0); for(size_t i = 0; i != size; ++i) out += a[i]*b[i]; return out; } /** * @brief Angle between normalized vectors * * @f[ * \phi = \frac{a \cdot b}{|a| \cdot |b|} * @f] * @attention If any of the parameters is not normalized (and * assertions are enabled), returns NaN. */ inline static T angle(const Vector& a, const Vector& b) { CORRADE_ASSERT(MathTypeTraits::equals(a.dot(), T(1)) && MathTypeTraits::equals(b.dot(), T(1)), "Math::Vector::angle(): vectors must be normalized!", std::numeric_limits::quiet_NaN()); return std::acos(dot(a, b)); } /** @brief Default constructor */ inline constexpr Vector() {} /** * @brief Initializer-list constructor * @param first First value * @param next Next values */ #ifndef DOXYGEN_GENERATING_OUTPUT template inline constexpr Vector(T first, U... next): RectangularMatrix<1, size, T>(first, next...) {} #else template inline constexpr Vector(T first, U... next); #endif /** * @brief Constructor * @param value Value for all fields */ #ifndef DOXYGEN_GENERATING_OUTPUT template inline explicit Vector(typename std::enable_if::value && size != 1, U>::type value) { #else inline explicit Vector(T value) { #endif for(size_t i = 0; i != size; ++i) (*this)[i] = value; } /** @brief Copy constructor */ inline constexpr Vector(const RectangularMatrix<1, size, T>& other): RectangularMatrix<1, size, T>(other) {} /** @brief Value at given position */ inline T& operator[](size_t pos) { return RectangularMatrix<1, size, T>::_data[pos]; } inline constexpr T operator[](size_t pos) const { return RectangularMatrix<1, size, T>::_data[pos]; } /**< @overload */ /** * @brief Multiply vector component-wise * * @see operator*=(const Vector&) */ Vector operator*(const Vector& other) const { return Vector(*this)*=other; } /** * @brief Multiply vector component-wise and assign * * More efficient than operator*(const Vector&) const, * because it does the computation in-place. */ Vector& operator*=(const Vector& other) { for(size_t i = 0; i != size; ++i) (*this)[i] *= other[i]; return *this; } /** * @brief Divide vector component-wise * * @see operator/=(const Vector&) */ Vector operator/(const Vector& other) const { return Vector(*this)/=other; } /** * @brief Divide vector component-wise and assign * * More efficient than operator/(const Vector&) const, * because it does the computation in-place. */ Vector& operator/=(const Vector& other) { for(size_t i = 0; i != size; ++i) (*this)[i] /= other[i]; return *this; } /** @brief Negative vector */ Vector operator-() const { Vector out; for(size_t i = 0; i != size; ++i) out[i] = -(*this)[i]; return out; } /** * @brief Dot product of the vector * * Should be used instead of length() for comparing vector length with * other values, because it doesn't compute the square root, just the * dot product: @f$ a \cdot a < length \cdot length @f$ is faster * than @f$ \sqrt{a \cdot a} < length @f$. * * @see dot(const Vector&, const Vector&) */ inline T dot() const { return dot(*this, *this); } /** * @brief %Vector length * * @see dot() const */ inline T length() const { return std::sqrt(dot()); } /** @brief Normalized vector (of length 1) */ inline Vector normalized() const { return *this/length(); } /** @brief Sum of values in the vector */ T sum() const { T out(0); for(size_t i = 0; i != size; ++i) out += (*this)[i]; return out; } /** @brief Product of values in the vector */ T product() const { T out(1); for(size_t i = 0; i != size; ++i) out *= (*this)[i]; return out; } /** @brief Minimal value in the vector */ T min() const { T out((*this)[0]); for(size_t i = 1; i != size; ++i) out = std::min(out, (*this)[i]); return out; } /** @brief Maximal value in the vector */ T max() const { T out((*this)[0]); for(size_t i = 1; i != size; ++i) out = std::max(out, (*this)[i]); return out; } MAGNUM_RECTANGULARMATRIX_SUBCLASS_IMPLEMENTATION(1, size, Vector) MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(1, size, Vector) private: /* Hiding unused things from RectangularMatrix */ using RectangularMatrix<1, size, T>::cols; using RectangularMatrix<1, size, T>::rows; using RectangularMatrix<1, size, T>::operator[]; using RectangularMatrix<1, size, T>::operator(); }; #ifndef DOXYGEN_GENERATING_OUTPUT template inline Vector operator*(U number, const Vector& vector) { return number*RectangularMatrix<1, size, T>(vector); } template inline Vector operator/(U number, const Vector& vector) { return number/RectangularMatrix<1, size, T>(vector); } #endif /** @debugoperator{Magnum::Math::Vector} */ template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector& value) { debug << "Vector("; debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, false); for(size_t i = 0; i != size; ++i) { if(i != 0) debug << ", "; debug << typename MathTypeTraits::NumericType(value[i]); } debug << ')'; debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, true); return debug; } #ifndef DOXYGEN_GENERATING_OUTPUT #define MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Type, size) \ inline constexpr static Type& from(T* data) { \ return *reinterpret_cast*>(data); \ } \ inline constexpr static const Type& from(const T* data) { \ return *reinterpret_cast*>(data); \ } \ template inline constexpr static Type from(const Vector& other) { \ return Vector::from(other); \ } \ \ inline Type& operator=(const Type& other) { \ Vector::operator=(other); \ return *this; \ } \ \ inline Type operator*(const Vector& other) const { \ return Vector::operator*(other); \ } \ inline Type& operator*=(const Vector& other) { \ Vector::operator*=(other); \ return *this; \ } \ inline Type operator/(const Vector& other) const { \ return Vector::operator/(other); \ } \ inline Type& operator/=(const Vector& other) { \ Vector::operator/=(other); \ return *this; \ } \ \ inline Type operator-() const { return Vector::operator-(); } \ inline Type normalized() const { return Vector::normalized(); } #define MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Type, size) \ template inline Type operator*(U number, const Type& vector) { \ return number*Vector(vector); \ } \ template inline Type operator/(U number, const Type& vector) { \ return number/Vector(vector); \ } #endif }} namespace Corrade { namespace Utility { /** @configurationvalue{Magnum::Math::Vector} */ template struct ConfigurationValue> { /** @brief Writes elements separated with spaces */ static std::string toString(const Magnum::Math::Vector& value, int flags = 0) { std::string output; for(size_t pos = 0; pos != size; ++pos) { if(!output.empty()) output += ' '; output += ConfigurationValue::toString(value[pos], flags); } return output; } /** @brief Reads elements separated with whitespace */ static Magnum::Math::Vector fromString(const std::string& stringValue, int flags = 0) { Magnum::Math::Vector result; std::istringstream in(stringValue); std::string num; for(size_t pos = 0; pos != size; ++pos) { in >> num; result[pos] = ConfigurationValue::fromString(num, flags); } return result; } }; }} #endif