diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index 74f2960bd..f7b1c6e72 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -38,6 +38,7 @@ VectorTest::VectorTest() { &VectorTest::dotSelf, &VectorTest::length, &VectorTest::normalized, + &VectorTest::projected, &VectorTest::sum, &VectorTest::product, &VectorTest::min, @@ -93,6 +94,14 @@ void VectorTest::normalized() { CORRADE_COMPARE(Vector4(1.0f, 1.0f, 1.0f, 1.0f).normalized(), Vector4(0.5f, 0.5f, 0.5f, 0.5f)); } +void VectorTest::projected() { + Vector3 line(1.0f, -1.0f, 0.5f); + Vector3 projected = Vector3(1.0f, 2.0f, 3.0f).projected(line); + + CORRADE_COMPARE(projected, Vector3(0.222222f, -0.222222f, 0.111111f)); + CORRADE_COMPARE(projected.normalized(), line.normalized()); +} + void VectorTest::sum() { CORRADE_COMPARE(Vector3(1.0f, 2.0f, 4.0f).sum(), 7.0f); } diff --git a/src/Math/Test/VectorTest.h b/src/Math/Test/VectorTest.h index 6c70e6e02..94590d604 100644 --- a/src/Math/Test/VectorTest.h +++ b/src/Math/Test/VectorTest.h @@ -30,6 +30,7 @@ class VectorTest: public Corrade::TestSuite::Tester { void dotSelf(); void length(); void normalized(); + void projected(); void sum(); void product(); void min(); diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 99b0bc32b..39589af74 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -224,6 +224,15 @@ template class Vector: public RectangularMatrix<1, si return *this/length(); } + /** + * @brief Vector projected onto another + * + * Returns vector projected onto line defined by @p other. + */ + inline Vector projected(const Vector& other) const { + return other*dot(*this, other)/other.dot(); + } + /** @brief Sum of values in the vector */ T sum() const { T out(0);