diff --git a/src/Math/Test/Vector2Test.cpp b/src/Math/Test/Vector2Test.cpp index ec05fab05..24ede0935 100644 --- a/src/Math/Test/Vector2Test.cpp +++ b/src/Math/Test/Vector2Test.cpp @@ -26,7 +26,7 @@ #include #include -#include "Math/Vector2.h" +#include "Math/Vector3.h" struct Vec2 { float x, y; @@ -63,6 +63,7 @@ class Vector2Test: public Corrade::TestSuite::Tester { void convert(); void access(); + void cross(); void axes(); void scales(); @@ -70,6 +71,7 @@ class Vector2Test: public Corrade::TestSuite::Tester { void configuration(); }; +typedef Math::Vector3 Vector3i; typedef Math::Vector2 Vector2; typedef Math::Vector2 Vector2i; @@ -83,6 +85,7 @@ Vector2Test::Vector2Test() { &Vector2Test::convert, &Vector2Test::access, + &Vector2Test::cross, &Vector2Test::axes, &Vector2Test::scales, @@ -162,6 +165,14 @@ void Vector2Test::access() { CORRADE_COMPARE(y, -2.0f); } +void Vector2Test::cross() { + Vector2i a(1, -1); + Vector2i b(4, 3); + + CORRADE_COMPARE(Vector2i::cross(a, b), 7); + CORRADE_COMPARE(Vector3i::cross({a, 0}, {b, 0}), Vector3i(0, 0, Vector2i::cross(a, b))); +} + void Vector2Test::axes() { constexpr Vector2 x = Vector2::xAxis(5.0f); constexpr Vector2 y = Vector2::yAxis(6.0f); diff --git a/src/Math/Test/Vector3Test.cpp b/src/Math/Test/Vector3Test.cpp index 649cd1851..586bc4e9a 100644 --- a/src/Math/Test/Vector3Test.cpp +++ b/src/Math/Test/Vector3Test.cpp @@ -180,10 +180,10 @@ void Vector3Test::access() { } void Vector3Test::cross() { - Vector3 a(1, -1, 1); - Vector3 b(4, 3, 7); + Vector3i a(1, -1, 1); + Vector3i b(4, 3, 7); - CORRADE_COMPARE(Vector3::cross(a, b), Vector3(-10, -3, 7)); + CORRADE_COMPARE(Vector3i::cross(a, b), Vector3i(-10, -3, 7)); } void Vector3Test::axes() { diff --git a/src/Math/Vector2.h b/src/Math/Vector2.h index 148174cfb..b19624d6f 100644 --- a/src/Math/Vector2.h +++ b/src/Math/Vector2.h @@ -80,6 +80,20 @@ template class Vector2: public Vector<2, T> { */ inline constexpr static Vector2 yScale(T scale) { return Vector2(T(1), scale); } + /** + * @brief 2D cross product + * + * 2D version of cross product, equivalent to calling Vector3::cross() + * with Z coordinate set to `0` and extracting only Z coordinate from + * the result (X and Y coordinates are always zero). + * @f[ + * \boldsymbol a \times \boldsymbol b = a_xb_y - a_yb_x + * @f] + */ + inline static T cross(const Vector2& a, const Vector2& b) { + return a.x()*b.y() - a.y()*b.x(); + } + /** @copydoc Vector::Vector() */ inline constexpr /*implicit*/ Vector2() {} diff --git a/src/Math/Vector3.h b/src/Math/Vector3.h index cbab75330..933d11591 100644 --- a/src/Math/Vector3.h +++ b/src/Math/Vector3.h @@ -102,12 +102,12 @@ template class Vector3: public Vector<3, T> { * @brief Cross product * * @f[ - * \boldsymbol a \times \boldsymbol b = - * \begin{pmatrix} c_0 \\ c_1 \\ c_2 \end{pmatrix} = - * \begin{pmatrix}a_1b_2 - a_2b_1 \\ a_2b_0 - a_0b_2 \\ a_0b_1 - a_1b_0 \end{pmatrix} + * \boldsymbol a \times \boldsymbol b = + * \begin{pmatrix}a_yb_z - a_zb_y \\ a_zb_y - a_xb_z \\ a_xb_y - a_yb_x \end{pmatrix} * @f] + * @see Vector2::cross() */ - inline constexpr static Vector3 cross(const Vector3& a, const Vector3& b) { + inline static Vector3 cross(const Vector3& a, const Vector3& b) { return swizzle<'y', 'z', 'x'>(a)*swizzle<'z', 'x', 'y'>(b) - swizzle<'z', 'x', 'y'>(a)*swizzle<'y', 'z', 'x'>(b); }