#ifndef Magnum_Math_Vector_h #define Magnum_Math_Vector_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** @file * @brief Class @ref Magnum::Math::Vector */ #include #ifdef _MSC_VER #include /* std::max() */ #endif #include #include #include #include "Magnum/visibility.h" #include "Magnum/Math/Angle.h" #include "Magnum/Math/BoolVector.h" #include "Magnum/Math/TypeTraits.h" #ifdef MAGNUM_BUILD_DEPRECATED #include #endif namespace Magnum { namespace Math { namespace Implementation { template struct VectorConverter; } /** @relatesalso Vector @brief Dot product of two vectors Returns `0` when two vectors are perpendicular, `1` when two *normalized* vectors are parallel and `-1` when two *normalized* vectors are antiparallel. @f[ \boldsymbol a \cdot \boldsymbol b = \sum_{i=0}^{n-1} \boldsymbol a_i \boldsymbol b_i @f] @see @ref Vector::dot() const, @ref Vector::operator-(), @ref Vector2::perpendicular() */ template inline T dot(const Vector& a, const Vector& b) { return (a*b).sum(); } /** @relatesalso Vector @brief Angle between normalized vectors Expects that both vectors are normalized. @f[ \theta = acos \left( \frac{\boldsymbol a \cdot \boldsymbol b}{|\boldsymbol a| |\boldsymbol b|} \right) = acos (\boldsymbol a \cdot \boldsymbol b) @f] @see @ref Vector::isNormalized(), @ref angle(const Complex&, const Complex&), @ref angle(const Quaternion&, const Quaternion&) */ template inline Rad angle(const Vector& normalizedA, const Vector& normalizedB) { CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(), "Math::angle(): vectors must be normalized", {}); return Rad(std::acos(dot(normalizedA, normalizedB))); } /** @brief Vector @tparam size Vector size @tparam T Underlying data type See @ref matrix-vector for brief introduction. @configurationvalueref{Magnum::Math::Vector} */ template class Vector { static_assert(size != 0, "Vector cannot have zero elements"); template friend class Vector; #ifdef CORRADE_MSVC2015_COMPATIBILITY /* Delegating constexpr constructor workarounds */ friend class Vector2; friend class Vector3; friend class Vector4; #endif public: typedef T Type; /**< @brief Underlying data type */ enum: std::size_t { Size = size /**< Vector size */ }; /** * @brief Vector from array * @return Reference to the data as if it was Vector, thus doesn't * perform any copying. * * @attention Use with caution, the function doesn't check whether the * array is long enough. */ static Vector& from(T* data) { return *reinterpret_cast*>(data); } /** @overload */ static const Vector& from(const T* data) { return *reinterpret_cast*>(data); } /** * @brief Pad vector * * If size of @p a is smaller than @ref Size, it is padded from right * with @p value, otherwise it's cut. * @see @ref Vector4::pad(const Vector&, T, T) */ template constexpr static Vector pad(const Vector& a, T value = T(0)) { return padInternal(typename Implementation::GenerateSequence::Type(), a, value); } #ifdef MAGNUM_BUILD_DEPRECATED /** * @copybrief Math::dot(const Vector&, const Vector&) * @deprecated Use @ref Math::dot(const Vector&, const Vector&) * instead. */ CORRADE_DEPRECATED("use Math::dot() instead") static T dot(const Vector& a, const Vector& b) { return Math::dot(a, b); } /** * @copybrief Math::angle(const Vector&, const Vector&) * @deprecated Use @ref Math::angle(const Vector&, const Vector&) * instead. */ CORRADE_DEPRECATED("use Math::angle() instead") static Rad angle(const Vector& normalizedA, const Vector& normalizedB) { return Math::angle(normalizedA, normalizedB); } #endif /** * @brief Default constructor * * @f[ * \boldsymbol v = \boldsymbol 0 * @f] */ constexpr /*implicit*/ Vector(ZeroInitT = ZeroInit): _data{} {} /** @brief Construct vector without initializing the contents */ explicit Vector(NoInitT) {} /** @todo Creating Vector from combination of vector and scalar types */ /** * @brief Construct vector from values * @param first First value * @param next Next values */ #ifdef DOXYGEN_GENERATING_OUTPUT template constexpr /*implicit*/ Vector(T first, U... next); #else template::type> constexpr /*implicit*/ Vector(T first, U... next): _data{first, next...} {} #endif /** @brief Construct vector with one value for all fields */ #ifdef DOXYGEN_GENERATING_OUTPUT constexpr explicit Vector(T value); #else template::value && size != 1, T>::type> #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Can't use delegating constructors with constexpr -- https://connect.microsoft.com/VisualStudio/feedback/details/1579279/c-constexpr-does-not-work-with-delegating-constructors */ constexpr #endif explicit Vector(U value): Vector(typename Implementation::GenerateSequence::Type(), value) {} #endif /** * @brief Construct 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, Byte> integral(floatingPoint); * // integral == {1, 2, -15, 7} * @endcode */ template #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Can't use delegating constructors with constexpr -- https://connect.microsoft.com/VisualStudio/feedback/details/1579279/c-constexpr-does-not-work-with-delegating-constructors */ constexpr #endif explicit Vector(const Vector& other): Vector(typename Implementation::GenerateSequence::Type(), other) {} /** @brief Construct vector from external representation */ template::from(std::declval()))> #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Can't use delegating constructors with constexpr -- https://connect.microsoft.com/VisualStudio/feedback/details/1579279/c-constexpr-does-not-work-with-delegating-constructors */ constexpr #endif explicit Vector(const U& other): Vector(Implementation::VectorConverter::from(other)) {} /** @brief Copy constructor */ constexpr Vector(const Vector&) = default; /** @brief Assignment operator */ Vector& operator=(const Vector&) = default; /** @brief Convert vector to external representation */ template::to(std::declval>()))> constexpr explicit operator U() const { return Implementation::VectorConverter::to(*this); } /** * @brief Raw data * @return One-dimensional array of `size` length. * * @see @ref operator[]() */ T* data() { return _data; } constexpr const T* data() const { return _data; } /**< @overload */ /** * @brief Value at given position * * @see @ref data() */ T& operator[](std::size_t pos) { return _data[pos]; } constexpr T operator[](std::size_t pos) const { return _data[pos]; } /**< @overload */ /** @brief Equality comparison */ bool operator==(const Vector& other) const { for(std::size_t i = 0; i != size; ++i) if(!TypeTraits::equals(_data[i], other._data[i])) return false; return true; } /** @brief Non-equality comparison */ bool operator!=(const Vector& other) const { return !operator==(other); } /** @brief Component-wise less than */ BoolVector operator<(const Vector& other) const; /** @brief Component-wise less than or equal */ BoolVector operator<=(const Vector& other) const; /** @brief Component-wise greater than or equal */ BoolVector operator>=(const Vector& other) const; /** @brief Component-wise greater than */ BoolVector operator>(const Vector& other) const; /** * @brief Whether the vector is zero * * @f[ * |\boldsymbol a \cdot \boldsymbol a - 0| < \epsilon^2 \cong \epsilon * @f] * @see @ref dot(), @ref normalized() */ bool isZero() const { return Implementation::isZeroSquared(dot()); } /** * @brief Whether the vector is normalized * * The vector is normalized if it has unit length: @f[ * |\boldsymbol a \cdot \boldsymbol a - 1| < 2 \epsilon + \epsilon^2 \cong 2 \epsilon * @f] * @see @ref dot(), @ref normalized() */ bool isNormalized() const { return Implementation::isNormalizedSquared(dot()); } /** * @brief Negated vector * * @f[ * \boldsymbol b_i = -\boldsymbol a_i * @f] * @see @ref Vector2::perpendicular() */ Vector operator-() const; /** * @brief Add and assign vector * * The computation is done in-place. @f[ * \boldsymbol a_i = \boldsymbol a_i + \boldsymbol b_i * @f] */ Vector& operator+=(const Vector& other) { for(std::size_t i = 0; i != size; ++i) _data[i] += other._data[i]; return *this; } /** * @brief Add vector * * @see @ref operator+=(), @ref sum() */ Vector operator+(const Vector& other) const { return Vector(*this) += other; } /** * @brief Subtract and assign vector * * The computation is done in-place. @f[ * \boldsymbol a_i = \boldsymbol a_i - \boldsymbol b_i * @f] */ Vector& operator-=(const Vector& other) { for(std::size_t i = 0; i != size; ++i) _data[i] -= other._data[i]; return *this; } /** * @brief Subtract vector * * @see @ref operator-=() */ Vector operator-(const Vector& other) const { return Vector(*this) -= other; } /** * @brief Multiply vector with number and assign * * The computation is done in-place. @f[ * \boldsymbol a_i = b \boldsymbol a_i * @f] * @see @ref operator*=(const Vector&), * @ref operator*=(Vector&, FloatingPoint) */ Vector& operator*=(T number) { for(std::size_t i = 0; i != size; ++i) _data[i] *= number; return *this; } /** * @brief Multiply vector with number * * @see @ref operator*(const Vector&) const, * @ref operator*=(T), operator*(T, const Vector&), * @ref operator*(const Vector&, FloatingPoint) */ Vector operator*(T number) const { return Vector(*this) *= number; } /** * @brief Divide vector with number and assign * * The computation is done in-place. @f[ * \boldsymbol a_i = \frac{\boldsymbol a_i} b * @f] * @see @ref operator/=(const Vector&), * @ref operator/=(Vector&, FloatingPoint) */ Vector& operator/=(T number) { for(std::size_t i = 0; i != size; ++i) _data[i] /= number; return *this; } /** * @brief Divide vector with number * * @see @ref operator/(const Vector&) const, * @ref operator/=(T), operator/(T, const Vector&), * @ref operator/(const Vector&, FloatingPoint) */ Vector operator/(T number) const { return Vector(*this) /= number; } /** * @brief Multiply vector component-wise and assign * * The computation is done in-place. @f[ * \boldsymbol a_i = \boldsymbol a_i \boldsymbol b_i * @f] * @see @ref operator*=(T), * @ref operator*=(Vector&, const Vector&) */ Vector& operator*=(const Vector& other) { for(std::size_t i = 0; i != size; ++i) _data[i] *= other._data[i]; return *this; } /** * @brief Multiply vector component-wise * * @see @ref operator*(T) const, @ref operator*=(const Vector&), * @ref operator*(const Vector&, const Vector&), * @ref product() */ Vector operator*(const Vector& other) const { return Vector(*this) *= other; } /** * @brief Divide vector component-wise and assign * * The computation is done in-place. @f[ * \boldsymbol a_i = \frac{\boldsymbol a_i}{\boldsymbol b_i} * @f] * @see @ref operator/=(T), * @ref operator/=(Vector&, const Vector&) */ Vector& operator/=(const Vector& other) { for(std::size_t i = 0; i != size; ++i) _data[i] /= other._data[i]; return *this; } /** * @brief Divide vector component-wise * * @see @ref operator/(T) const, @ref operator/=(const Vector&), * @ref operator/(const Vector&, const Vector&) */ Vector operator/(const Vector& other) const { return Vector(*this) /= other; } /** * @brief Dot product of the vector * * Should be used instead of @ref length() for comparing vector length * with other values, because it doesn't compute the square root. @f[ * \boldsymbol a \cdot \boldsymbol a = \sum_{i=0}^{n-1} \boldsymbol a_i^2 * @f] * @see @ref dot(const Vector&, const Vector&), * @ref isNormalized() */ T dot() const { return Math::dot(*this, *this); } /** * @brief Vector length * * See also @ref dot() const which is faster for comparing length with * other values. @f[ * |\boldsymbol a| = \sqrt{\boldsymbol a \cdot \boldsymbol a} * @f] * @see @ref lengthInverted(), @ref Math::sqrt(), @ref normalized(), * @ref resized() * @todo something like std::hypot() for possibly better precision? */ T length() const { return std::sqrt(dot()); } /** * @brief Inverse vector length * * @f[ * \frac{1}{|\boldsymbol a|} = \frac{1}{\sqrt{\boldsymbol a \cdot \boldsymbol a}} * @f] * @see @ref length(), @ref Math::sqrtInverted(), @ref normalized(), * @ref resized() */ T lengthInverted() const { return T(1)/length(); } /** * @brief Normalized vector (of unit length) * * @see @ref isNormalized(), @ref lengthInverted(), @ref resized() */ Vector normalized() const { return *this*lengthInverted(); } /** * @brief Resized vector * * Convenience equivalent to the following code. Due to operation order * this function is faster than the obvious way of sizing * @ref normalized() vector. * @code * vec*(vec.lengthInverted()*length) // the brackets are important * @endcode * @see @ref normalized() */ Vector resized(T length) const { return *this*(lengthInverted()*length); } /** * @brief Vector projected onto line * * Returns vector projected onto @p line. @f[ * \boldsymbol a_1 = \frac{\boldsymbol a \cdot \boldsymbol b}{\boldsymbol b \cdot \boldsymbol b} \boldsymbol b * @f] * @see @ref dot(), @ref projectedOntoNormalized() */ Vector projected(const Vector& line) const { return line*Math::dot(*this, line)/line.dot(); } /** * @brief Vector projected onto normalized line * * Slightly faster alternative to @ref projected(), expects @p line to * be normalized. @f[ * \boldsymbol a_1 = \frac{\boldsymbol a \cdot \boldsymbol b}{\boldsymbol b \cdot \boldsymbol b} \boldsymbol b = * (\boldsymbol a \cdot \boldsymbol b) \boldsymbol b * @f] * @see @ref dot() const */ Vector projectedOntoNormalized(const Vector& line) const; /** * @brief Sum of values in the vector * * @see @ref operator+() */ T sum() const; /** * @brief Product of values in the vector * * @see @ref operator*(const Vector&) const */ T product() const; /** * @brief Minimal value in the vector * * @see @ref Math::min(), @ref Vector2::minmax() */ T min() const; /** * @brief Maximal value in the vector * * @see @ref Math::max(), @ref Vector2::minmax() */ T max() const; private: /* Implementation for Vector::Vector(const Vector&) */ template constexpr explicit Vector(Implementation::Sequence, const Vector& vector): _data{T(vector._data[sequence])...} {} /* Implementation for Vector::Vector(U) */ template constexpr explicit Vector(Implementation::Sequence, T value): _data{Implementation::repeat(value, sequence)...} {} template constexpr static Vector padInternal(Implementation::Sequence, const Vector& a, T value) { return {sequence < otherSize ? a[sequence] : value...}; } T _data[size]; }; /** @relates Vector @brief Multiply number with vector Same as @ref Vector::operator*(T) const. */ template inline Vector operator*( #ifdef DOXYGEN_GENERATING_OUTPUT T #else typename std::common_type::type #endif number, const Vector& vector) { return vector*number; } /** @relates Vector @brief Divide vector with number and invert @f[ \boldsymbol c_i = \frac b {\boldsymbol a_i} @f] @see @ref Vector::operator/(T) const */ template inline Vector operator/( #ifdef DOXYGEN_GENERATING_OUTPUT T #else typename std::common_type::type #endif number, const Vector& vector) { Vector out; for(std::size_t i = 0; i != size; ++i) out[i] = number/vector[i]; return out; } /** @relates Vector @brief Do modulo of integral vector and assign The computation is done in-place. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector& #else typename std::enable_if::value, Vector&>::type #endif operator%=(Vector& a, Integral b) { for(std::size_t i = 0; i != size; ++i) a[i] %= b; return a; } /** @relates Vector @brief Modulo of integral vector */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value, Vector>::type #endif operator%(const Vector& a, Integral b) { Vector copy(a); return copy %= b; } /** @relates Vector @brief Do modulo of two integral vectors and assign The computation is done in-place. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector& #else typename std::enable_if::value, Vector&>::type #endif operator%=(Vector& a, const Vector& b) { for(std::size_t i = 0; i != size; ++i) a[i] %= b[i]; return a; } /** @relates Vector @brief Modulo of two integral vectors */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value, Vector>::type #endif operator%(const Vector& a, const Vector& b) { Vector copy(a); return copy %= b; } /** @relates Vector @brief Bitwise NOT of integral vector */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value, Vector>::type #endif operator~(const Vector& vector) { Vector out; for(std::size_t i = 0; i != size; ++i) out[i] = ~vector[i]; return out; } /** @relates Vector @brief Do bitwise AND of two integral vectors and assign The computation is done in-place. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector& #else typename std::enable_if::value, Vector&>::type #endif operator&=(Vector& a, const Vector& b) { for(std::size_t i = 0; i != size; ++i) a[i] &= b[i]; return a; } /** @relates Vector @brief Bitwise AND of two integral vectors */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value, Vector>::type #endif operator&(const Vector& a, const Vector& b) { Vector copy(a); return copy &= b; } /** @relates Vector @brief Do bitwise OR of two integral vectors and assign The computation is done in-place. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector& #else typename std::enable_if::value, Vector&>::type #endif operator|=(Vector& a, const Vector& b) { for(std::size_t i = 0; i != size; ++i) a[i] |= b[i]; return a; } /** @relates Vector @brief Bitwise OR of two integral vectors */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value, Vector>::type #endif operator|(const Vector& a, const Vector& b) { Vector copy(a); return copy |= b; } /** @relates Vector @brief Do bitwise XOR of two integral vectors and assign The computation is done in-place. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector& #else typename std::enable_if::value, Vector&>::type #endif operator^=(Vector& a, const Vector& b) { for(std::size_t i = 0; i != size; ++i) a[i] ^= b[i]; return a; } /** @relates Vector @brief Bitwise XOR of two integral vectors */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value, Vector>::type #endif operator^(const Vector& a, const Vector& b) { Vector copy(a); return copy ^= b; } /** @relates Vector @brief Do bitwise left shift of integral vector and assign The computation is done in-place. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector& #else typename std::enable_if::value, Vector&>::type #endif operator<<=(Vector& vector, #ifdef DOXYGEN_GENERATING_OUTPUT Integral #else typename std::common_type::type #endif shift) { for(std::size_t i = 0; i != size; ++i) vector[i] <<= shift; return vector; } /** @relates Vector @brief Bitwise left shift of integral vector */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value, Vector>::type #endif operator<<(const Vector& vector, #ifdef DOXYGEN_GENERATING_OUTPUT Integral #else typename std::common_type::type #endif shift) { Vector copy(vector); return copy <<= shift; } /** @relates Vector @brief Do bitwise right shift of integral vector and assign The computation is done in-place. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector& #else typename std::enable_if::value, Vector&>::type #endif operator>>=(Vector& vector, #ifdef DOXYGEN_GENERATING_OUTPUT Integral #else typename std::common_type::type #endif shift) { for(std::size_t i = 0; i != size; ++i) vector[i] >>= shift; return vector; } /** @relates Vector @brief Bitwise left shift of integral vector */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value, Vector>::type #endif operator>>(const Vector& vector, #ifdef DOXYGEN_GENERATING_OUTPUT Integral #else typename std::common_type::type #endif shift) { Vector copy(vector); return copy >>= shift; } /** @relates Vector @brief Multiply integral vector with floating-point number and assign Similar to @ref Vector::operator*=(T), except that the multiplication is done in floating-point. The computation is done in-place. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector& #else typename std::enable_if::value && std::is_floating_point::value, Vector&>::type #endif operator*=(Vector& vector, FloatingPoint number) { for(std::size_t i = 0; i != size; ++i) vector[i] = Integral(vector[i]*number); return vector; } /** @relates Vector @brief Multiply integral vector with floating-point number Similar to @ref Vector::operator*(T) const, except that the multiplication is done in floating-point. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value && std::is_floating_point::value, Vector>::type #endif operator*(const Vector& vector, FloatingPoint number) { Vector copy(vector); return copy *= number; } /** @relates Vector @brief Multiply floating-point number with integral vector Same as @ref operator*(const Vector&, FloatingPoint). */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value && std::is_floating_point::value, Vector>::type #endif operator*(FloatingPoint number, const Vector& vector) { return vector*number; } /** @relates Vector @brief Divide integral vector with floating-point number and assign Similar to @ref Vector::operator/=(T), except that the division is done in floating-point. The computation is done in-place. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector& #else typename std::enable_if::value && std::is_floating_point::value, Vector&>::type #endif operator/=(Vector& vector, FloatingPoint number) { for(std::size_t i = 0; i != size; ++i) vector[i] = Integral(vector[i]/number); return vector; } /** @relates Vector @brief Divide integral vector with floating-point number Similar to @ref Vector::operator/(T) const, except that the division is done in floating-point. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value && std::is_floating_point::value, Vector>::type #endif operator/(const Vector& vector, FloatingPoint number) { Vector copy(vector); return copy /= number; } /** @relates Vector @brief Multiply integral vector with floating-point vector component-wise and assign Similar to @ref Vector::operator*=(const Vector&), except that the multiplication is done in floating-point. The computation is done in-place. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector& #else typename std::enable_if::value && std::is_floating_point::value, Vector&>::type #endif operator*=(Vector& a, const Vector& b) { for(std::size_t i = 0; i != size; ++i) a[i] = Integral(a[i]*b[i]); return a; } /** @relates Vector @brief Multiply integral vector with floating-point vector component-wise Similar to @ref Vector::operator*(const Vector&) const, except that the multiplication is done in floating-point. The result is always integral vector, convert both arguments to the same floating-point type to have floating-point result. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value && std::is_floating_point::value, Vector>::type #endif operator*(const Vector& a, const Vector& b) { Vector copy(a); return copy *= b; } /** @relates Vector @brief Multiply floating-point vector with integral vector component-wise Same as @ref operator*(const Vector&, const Vector&). */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value && std::is_floating_point::value, Vector>::type #endif operator*(const Vector& a, const Vector& b) { return b*a; } /** @relates Vector @brief Divide integral vector with floating-point vector component-wise and assign Similar to @ref Vector::operator/=(const Vector&), except that the division is done in floating-point. The computation is done in-place. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector& #else typename std::enable_if::value && std::is_floating_point::value, Vector&>::type #endif operator/=(Vector& a, const Vector& b) { for(std::size_t i = 0; i != size; ++i) a[i] = Integral(a[i]/b[i]); return a; } /** @relates Vector @brief Divide integral vector with floating-point vector component-wise Similar to @ref Vector::operator/(const Vector&) const, except that the division is done in floating-point. The result is always integral vector, convert both arguments to the same floating-point type to have floating-point result. */ template inline #ifdef DOXYGEN_GENERATING_OUTPUT Vector #else typename std::enable_if::value && std::is_floating_point::value, Vector>::type #endif operator/(const Vector& a, const Vector& b) { Vector copy(a); return copy /= b; } /** @debugoperator{Magnum::Math::Vector} */ template Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Vector& value) { debug << "Vector("; debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, false); for(std::size_t i = 0; i != size; ++i) { if(i != 0) debug << ", "; debug << value[i]; } debug << ")"; debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, true); return debug; } /* Explicit instantiation for types used in OpenGL */ #ifndef DOXYGEN_GENERATING_OUTPUT extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<2, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<3, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<4, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<2, Int>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<3, Int>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<4, Int>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<2, UnsignedInt>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<3, UnsignedInt>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<4, UnsignedInt>&); #ifndef MAGNUM_TARGET_GLES extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<2, Double>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<3, Double>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Vector<4, Double>&); #endif #endif #ifndef DOXYGEN_GENERATING_OUTPUT #define MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(size, Type) \ static Type& from(T* data) { \ return *reinterpret_cast*>(data); \ } \ static const Type& from(const T* data) { \ return *reinterpret_cast*>(data); \ } \ template constexpr static Type pad(const Math::Vector& a, T value = T(0)) { \ return Math::Vector::pad(a, value); \ } \ \ Type& operator=(const Type& other) { \ Math::Vector::operator=(other); \ return *this; \ } \ \ Type operator-() const { \ return Math::Vector::operator-(); \ } \ Type& operator+=(const Math::Vector& other) { \ Math::Vector::operator+=(other); \ return *this; \ } \ Type operator+(const Math::Vector& other) const { \ return Math::Vector::operator+(other); \ } \ Type& operator-=(const Math::Vector& other) { \ Math::Vector::operator-=(other); \ return *this; \ } \ Type operator-(const Math::Vector& other) const { \ return Math::Vector::operator-(other); \ } \ Type& operator*=(T number) { \ Math::Vector::operator*=(number); \ return *this; \ } \ Type operator*(T number) const { \ return Math::Vector::operator*(number); \ } \ Type& operator/=(T number) { \ Math::Vector::operator/=(number); \ return *this; \ } \ Type operator/(T number) const { \ return Math::Vector::operator/(number); \ } \ Type& operator*=(const Math::Vector& other) { \ Math::Vector::operator*=(other); \ return *this; \ } \ Type operator*(const Math::Vector& other) const { \ return Math::Vector::operator*(other); \ } \ Type& operator/=(const Math::Vector& other) { \ Math::Vector::operator/=(other); \ return *this; \ } \ Type operator/(const Math::Vector& other) const { \ return Math::Vector::operator/(other); \ } \ \ Type normalized() const { \ return Math::Vector::normalized(); \ } \ Type resized(T length) const { \ return Math::Vector::resized(length); \ } \ Type projected(const Math::Vector& other) const { \ return Math::Vector::projected(other); \ } \ Type projectedOntoNormalized(const Math::Vector& other) const { \ return Math::Vector::projectedOntoNormalized(other); \ } #define MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(size, Type) \ template inline Type operator*(typename std::common_type::type number, const Type& vector) { \ return number*static_cast&>(vector); \ } \ template inline Type operator/(typename std::common_type::type number, const Type& vector) { \ return number/static_cast&>(vector); \ } \ \ template inline typename std::enable_if::value, Type&>::type operator%=(Type& a, Integral b) { \ static_cast&>(a) %= b; \ return a; \ } \ template inline typename std::enable_if::value, Type>::type operator%(const Type& a, Integral b) { \ return static_cast&>(a) % b; \ } \ template inline typename std::enable_if::value, Type&>::type operator%=(Type& a, const Math::Vector& b) { \ static_cast&>(a) %= b; \ return a; \ } \ template inline typename std::enable_if::value, Type>::type operator%(const Type& a, const Math::Vector& b) { \ return static_cast&>(a) % b; \ } \ \ template inline typename std::enable_if::value, Type>::type operator~(const Type& vector) { \ return ~static_cast&>(vector); \ } \ template inline typename std::enable_if::value, Type&>::type operator&=(Type& a, const Math::Vector& b) { \ static_cast&>(a) &= b; \ return a; \ } \ template inline typename std::enable_if::value, Type>::type operator&(const Type& a, const Math::Vector& b) { \ return static_cast&>(a) & b; \ } \ template inline typename std::enable_if::value, Type&>::type operator|=(Type& a, const Math::Vector& b) { \ static_cast&>(a) |= b; \ return a; \ } \ template inline typename std::enable_if::value, Type>::type operator|(const Type& a, const Math::Vector& b) { \ return static_cast&>(a) | b; \ } \ template inline typename std::enable_if::value, Type&>::type operator^=(Type& a, const Math::Vector& b) { \ static_cast&>(a) ^= b; \ return a; \ } \ template inline typename std::enable_if::value, Type>::type operator^(const Type& a, const Math::Vector& b) { \ return static_cast&>(a) ^ b; \ } \ template inline typename std::enable_if::value, Type&>::type operator<<=(Type& vector, typename std::common_type::type shift) { \ static_cast&>(vector) <<= shift; \ return vector; \ } \ template inline typename std::enable_if::value, Type>::type operator<<(const Type& vector, typename std::common_type::type shift) { \ return static_cast&>(vector) << shift; \ } \ template inline typename std::enable_if::value, Type&>::type operator>>=(Type& vector, typename std::common_type::type shift) { \ static_cast&>(vector) >>= shift; \ return vector; \ } \ template inline typename std::enable_if::value, Type>::type operator>>(const Type& vector, typename std::common_type::type shift) { \ return static_cast&>(vector) >> shift; \ } \ template inline typename std::enable_if::value && std::is_floating_point::value, Type&>::type operator*=(Type& vector, FloatingPoint number) { \ static_cast&>(vector) *= number; \ return vector; \ } \ template inline typename std::enable_if::value && std::is_floating_point::value, Type>::type operator*(const Type& vector, FloatingPoint number) { \ return static_cast&>(vector)*number; \ } \ template inline typename std::enable_if::value && std::is_floating_point::value, Type>::type operator*(FloatingPoint number, const Type& vector) { \ return number*static_cast&>(vector); \ } \ template inline typename std::enable_if::value && std::is_floating_point::value, Type&>::type operator/=(Type& vector, FloatingPoint number) { \ static_cast&>(vector) /= number; \ return vector; \ } \ template inline typename std::enable_if::value && std::is_floating_point::value, Type>::type operator/(const Type& vector, FloatingPoint number) { \ return static_cast&>(vector)/number; \ } \ \ template inline typename std::enable_if::value && std::is_floating_point::value, Type&>::type operator*=(Type& a, const Math::Vector& b) { \ static_cast&>(a) *= b; \ return a; \ } \ template inline typename std::enable_if::value && std::is_floating_point::value, Type>::type operator*(const Type& a, const Math::Vector& b) { \ return static_cast&>(a)*b; \ } \ template inline typename std::enable_if::value && std::is_floating_point::value, Type>::type operator*(const Math::Vector& a, const Type& b) { \ return a*static_cast&>(b); \ } \ template inline typename std::enable_if::value && std::is_floating_point::value, Type&>::type operator/=(Type& a, const Math::Vector& b) { \ static_cast&>(a) /= b; \ return a; \ } \ template inline typename std::enable_if::value && std::is_floating_point::value, Type>::type operator/(const Type& a, const Math::Vector& b) { \ return static_cast&>(a)/b; \ } #endif template inline BoolVector Vector::operator<(const Vector& other) const { BoolVector out; for(std::size_t i = 0; i != size; ++i) out.set(i, _data[i] < other._data[i]); return out; } template inline BoolVector Vector::operator<=(const Vector& other) const { BoolVector out; for(std::size_t i = 0; i != size; ++i) out.set(i, _data[i] <= other._data[i]); return out; } template inline BoolVector Vector::operator>=(const Vector& other) const { BoolVector out; for(std::size_t i = 0; i != size; ++i) out.set(i, _data[i] >= other._data[i]); return out; } template inline BoolVector Vector::operator>(const Vector& other) const { BoolVector out; for(std::size_t i = 0; i != size; ++i) out.set(i, _data[i] > other._data[i]); return out; } template inline Vector Vector::operator-() const { Vector out; for(std::size_t i = 0; i != size; ++i) out._data[i] = -_data[i]; return out; } template inline Vector Vector::projectedOntoNormalized(const Vector& line) const { CORRADE_ASSERT(line.isNormalized(), "Math::Vector::projectedOntoNormalized(): line must be normalized", {}); return line*Math::dot(*this, line); } template inline T Vector::sum() const { T out(_data[0]); for(std::size_t i = 1; i != size; ++i) out += _data[i]; return out; } template inline T Vector::product() const { T out(_data[0]); for(std::size_t i = 1; i != size; ++i) out *= _data[i]; return out; } template inline T Vector::min() const { T out(_data[0]); for(std::size_t i = 1; i != size; ++i) out = std::min(out, _data[i]); return out; } template inline T Vector::max() const { T out(_data[0]); for(std::size_t i = 1; i != size; ++i) out = std::max(out, _data[i]); return out; } }} namespace Corrade { namespace Utility { /** @configurationvalue{Magnum::Math::RectangularMatrix} */ template struct ConfigurationValue> { ConfigurationValue() = delete; /** @brief Writes elements separated with spaces */ static std::string toString(const Magnum::Math::Vector& value, ConfigurationValueFlags flags) { std::string output; for(std::size_t i = 0; i != size; ++i) { if(!output.empty()) output += ' '; output += ConfigurationValue::toString(value[i], flags); } return output; } /** @brief Reads elements separated with whitespace */ static Magnum::Math::Vector fromString(const std::string& stringValue, ConfigurationValueFlags flags) { Magnum::Math::Vector result; std::size_t oldpos = 0, pos = std::string::npos, i = 0; do { pos = stringValue.find(' ', oldpos); std::string part = stringValue.substr(oldpos, pos-oldpos); if(!part.empty()) { result[i] = ConfigurationValue::fromString(part, flags); ++i; } oldpos = pos+1; } while(pos != std::string::npos); return result; } }; #if !defined(DOXYGEN_GENERATING_OUTPUT) && !defined(__MINGW32__) /* Vectors */ extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; #ifndef MAGNUM_TARGET_GLES extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; extern template struct MAGNUM_EXPORT ConfigurationValue>; #endif #endif }} #endif