Browse Source

Math: added convenience function Vector::resized().

pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
fc58631aa5
  1. 12
      src/Math/Test/VectorTest.cpp
  2. 28
      src/Math/Vector.h

12
src/Math/Test/VectorTest.cpp

@ -80,6 +80,7 @@ class VectorTest: public Corrade::TestSuite::Tester {
void length();
void lengthInverted();
void normalized();
void resized();
void sum();
void product();
@ -126,6 +127,7 @@ VectorTest::VectorTest() {
&VectorTest::length,
&VectorTest::lengthInverted,
&VectorTest::normalized,
&VectorTest::resized,
&VectorTest::sum,
&VectorTest::product,
@ -317,7 +319,15 @@ void VectorTest::lengthInverted() {
}
void VectorTest::normalized() {
CORRADE_COMPARE(Vector4(1.0f, 1.0f, 1.0f, 1.0f).normalized(), Vector4(0.5f, 0.5f, 0.5f, 0.5f));
const auto vec = Vector4(1.0f, 1.0f, 1.0f, 1.0f).normalized();
CORRADE_COMPARE(vec, Vector4(0.5f, 0.5f, 0.5f, 0.5f));
CORRADE_COMPARE(vec.length(), 1.0f);
}
void VectorTest::resized() {
const auto vec = Vector4(2.0f, 2.0f, 0.0f, 1.0f).resized(9.0f);
CORRADE_COMPARE(vec, Vector4(6.0f, 6.0f, 0.0f, 3.0f));
CORRADE_COMPARE(vec.length(), 9.0f);
}
void VectorTest::sum() {

28
src/Math/Vector.h

@ -455,7 +455,7 @@ template<std::size_t size, class T> class Vector {
* values. @f[
* |\boldsymbol a| = \sqrt{\boldsymbol a \cdot \boldsymbol a}
* @f]
* @see lengthInverted(), Math::sqrt(), normalized()
* @see lengthInverted(), Math::sqrt(), normalized(), resized()
* @todo something like std::hypot() for possibly better precision?
*/
inline T length() const {
@ -468,7 +468,7 @@ template<std::size_t size, class T> class Vector {
* @f[
* \frac{1}{|\boldsymbol a|} = \frac{1}{\sqrt{\boldsymbol a \cdot \boldsymbol a}}
* @f]
* @see length(), Math::sqrtInverted(), normalized()
* @see length(), Math::sqrtInverted(), normalized(), resized()
*/
inline T lengthInverted() const {
return T(1)/length();
@ -477,12 +477,27 @@ template<std::size_t size, class T> class Vector {
/**
* @brief Normalized vector (of unit length)
*
* @see isNormalized()
* @see isNormalized(), lengthInverted(), resized()
*/
inline Vector<size, T> 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 normalized()
* vector.
* @code
* vec*(vec.lengthInverted()*length) // the brackets are important
* @endcode
* @see normalized()
*/
inline Vector<size, T> resized(T length) const {
return *this*(lengthInverted()*length);
}
/**
* @brief %Vector projected onto line
*
@ -701,7 +716,12 @@ extern template Corrade::Utility::Debug MAGNUM_EXPORT operator<<(Corrade::Utilit
return Math::Vector<size, T>::operator/(other); \
} \
\
inline Type<T> normalized() const { return Math::Vector<size, T>::normalized(); } \
inline Type<T> normalized() const { \
return Math::Vector<size, T>::normalized(); \
} \
inline Type<T> resized(T length) const { \
return Math::Vector<size, T>::resized(length); \
} \
inline Type<T> projected(const Math::Vector<size, T>& other) const { \
return Math::Vector<size, T>::projected(other); \
}

Loading…
Cancel
Save