From 265a0cb03f13120f50dbeab299e669af777b9ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20D=C3=BCrrenberger?= Date: Fri, 6 Sep 2024 00:00:29 +0200 Subject: [PATCH] Rename Vector2 & Vector3 functions to better fit the naming convention --- doc/migration.md | 50 ++++++++++++------------- examples/shader/Shader.cpp | 3 +- examples/sound_effects/SoundEffects.cpp | 2 +- include/SFML/System/Vector2.hpp | 8 ++-- include/SFML/System/Vector2.inl | 10 ++--- include/SFML/System/Vector3.hpp | 8 ++-- include/SFML/System/Vector3.inl | 12 +++--- src/SFML/Graphics/RenderTarget.cpp | 12 +++--- src/SFML/Graphics/Shape.cpp | 4 +- src/SFML/System/Vector2.cpp | 2 +- test/System/Vector2.test.cpp | 18 ++++----- test/System/Vector3.test.cpp | 10 ++--- 12 files changed, 71 insertions(+), 68 deletions(-) diff --git a/doc/migration.md b/doc/migration.md index 3f80953aa..7ed33ca97 100644 --- a/doc/migration.md +++ b/doc/migration.md @@ -54,7 +54,7 @@ Take `sf::Transformable::setPosition(float, float)` for example. The two parameters combine to represent a position in world space. SFML 3 takes all of the APIs with pairs of parameters like `(float, float)` or `(unsigned int, unsigned int)` and converts them to their corresponding `sf::Vector2` type like `sf::Vector2f` or `sf::Vector2u` to make the interface more expressive and composable. -This transition is often as simple as wrapping the two adjacement parameters with braces to construct the vector. +This transition is often as simple as wrapping the two adjacent parameters with braces to construct the vector. v2: ```cpp @@ -353,31 +353,31 @@ const sf::SoundBuffer soundBuffer("sound.wav"); `sf::Vector2` and `sf::Vector3` gained a number of new functions for performing common mathematic operations on vectors. -| `sf::Vector2` Function | Description | -| ---------------------------- | ---------------------------------------- | -| `Vector2(T, sf::Angle)` | Construct from polar coordinates | -| `length()` | Get length | -| `lengthSq()` | Get length squared | -| `normalized()` | Get vector normalized to unit circle | -| `angleTo(sf::Vector2)` | Get angle to another vector | -| `angle()` | Get angle from X axis | -| `rotatedBy(sf::Angle)` | Get vector rotated by a given angle | -| `projectedOnto(sf::Vector2)` | Get vector projected onto another vector | -| `perpendicular()` | Get perpendicular vector | -| `dot(sf::Vector2)` | Get dot product | -| `cross(sf::Vector2)` | Get Z component of cross product | -| `cwiseMul(sf::Vector2)` | Get component-wise multiple | -| `cwiseDiv(sf::Vector2)` | Get component-wise divisor | +| `sf::Vector2` Function | Description | +| ------------------------------- | ---------------------------------------- | +| `Vector2(T, sf::Angle)` | Construct from polar coordinates | +| `length()` | Get length | +| `lengthSquared()` | Get length squared | +| `normalized()` | Get vector normalized to unit circle | +| `angleTo(sf::Vector2)` | Get angle to another vector | +| `angle()` | Get angle from X axis | +| `rotatedBy(sf::Angle)` | Get vector rotated by a given angle | +| `projectedOnto(sf::Vector2)` | Get vector projected onto another vector | +| `perpendicular()` | Get perpendicular vector | +| `dot(sf::Vector2)` | Get dot product | +| `cross(sf::Vector2)` | Get Z component of cross product | +| `componentWiseMul(sf::Vector2)` | Get component-wise multiple | +| `componentWiseDiv(sf::Vector2)` | Get component-wise divisor | -| `sf::Vector3` Function | Description | -| ------------------------- | ------------------------------------- | -| `length()` | Get length | -| `lengthSq()` | Get length squared | -| `normalized()` | Get vector normalized to unit circle | -| `dot(sf::Vector3)` | Get dot product | -| `cross(sf::Vector3)` | Get cross product | -| `cwiseMul(sf::Vector3)` | Get component-wise multiple | -| `cwiseDiv(sf::Vector3)` | Get component-wise divisor | +| `sf::Vector3` Function | Description | +| ------------------------------- | ------------------------------------- | +| `length()` | Get length | +| `lengthSquared()` | Get length squared | +| `normalized()` | Get vector normalized to unit circle | +| `dot(sf::Vector3)` | Get dot product | +| `cross(sf::Vector3)` | Get cross product | +| `componentWiseMul(sf::Vector3)` | Get component-wise multiple | +| `componentWiseDiv(sf::Vector3)` | Get component-wise divisor | ## Threading Primitives diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp index cedb20b26..461f16361 100644 --- a/examples/shader/Shader.cpp +++ b/examples/shader/Shader.cpp @@ -487,7 +487,8 @@ int main() if (Effect* currentEffect = effects[current]) { // Update the current example - const auto [x, y] = sf::Vector2f(sf::Mouse::getPosition(window)).cwiseDiv(sf::Vector2f(window.getSize())); + const auto [x, + y] = sf::Vector2f(sf::Mouse::getPosition(window)).componentWiseDiv(sf::Vector2f(window.getSize())); currentEffect->update(clock.getElapsedTime().asSeconds(), x, y); // Clear the window diff --git a/examples/sound_effects/SoundEffects.cpp b/examples/sound_effects/SoundEffects.cpp index d05df334a..e64c99514 100644 --- a/examples/sound_effects/SoundEffects.cpp +++ b/examples/sound_effects/SoundEffects.cpp @@ -1213,7 +1213,7 @@ int main() } // Update the current example - const auto [x, y] = sf::Vector2f(sf::Mouse::getPosition(window)).cwiseDiv(sf::Vector2f(window.getSize())); + const auto [x, y] = sf::Vector2f(sf::Mouse::getPosition(window)).componentWiseDiv(sf::Vector2f(window.getSize())); effects[current]->update(clock.getElapsedTime().asSeconds(), x, y); // Clear the window diff --git a/include/SFML/System/Vector2.hpp b/include/SFML/System/Vector2.hpp index f8e6204b4..66fbe6abd 100644 --- a/include/SFML/System/Vector2.hpp +++ b/include/SFML/System/Vector2.hpp @@ -90,7 +90,7 @@ public: //////////////////////////////////////////////////////////// /// \brief Length of the vector (floating-point). /// - /// If you are not interested in the actual length, but only in comparisons, consider using lengthSq(). + /// If you are not interested in the actual length, but only in comparisons, consider using lengthSquared(). /// //////////////////////////////////////////////////////////// [[nodiscard]] SFML_SYSTEM_API T length() const; @@ -101,7 +101,7 @@ public: /// Suitable for comparisons, more efficient than length(). /// //////////////////////////////////////////////////////////// - [[nodiscard]] constexpr T lengthSq() const; + [[nodiscard]] constexpr T lengthSquared() const; //////////////////////////////////////////////////////////// /// \brief Vector with same direction but length 1 (floating-point). @@ -189,7 +189,7 @@ public: /// This operation is also known as the Hadamard or Schur product. /// //////////////////////////////////////////////////////////// - [[nodiscard]] constexpr Vector2 cwiseMul(Vector2 rhs) const; + [[nodiscard]] constexpr Vector2 componentWiseMul(Vector2 rhs) const; //////////////////////////////////////////////////////////// /// \brief Component-wise division of \c *this and \c rhs. @@ -201,7 +201,7 @@ public: /// \pre Neither component of \c rhs is zero. /// //////////////////////////////////////////////////////////// - [[nodiscard]] constexpr Vector2 cwiseDiv(Vector2 rhs) const; + [[nodiscard]] constexpr Vector2 componentWiseDiv(Vector2 rhs) const; //////////////////////////////////////////////////////////// diff --git a/include/SFML/System/Vector2.inl b/include/SFML/System/Vector2.inl index a514617c0..ed1ce017e 100644 --- a/include/SFML/System/Vector2.inl +++ b/include/SFML/System/Vector2.inl @@ -56,7 +56,7 @@ constexpr Vector2::Vector2(Vector2 vector) : x(static_cast(vector.x)), //////////////////////////////////////////////////////////// template -constexpr T Vector2::lengthSq() const +constexpr T Vector2::lengthSquared() const { return dot(*this); } @@ -88,7 +88,7 @@ constexpr T Vector2::cross(Vector2 rhs) const //////////////////////////////////////////////////////////// template -constexpr Vector2 Vector2::cwiseMul(Vector2 rhs) const +constexpr Vector2 Vector2::componentWiseMul(Vector2 rhs) const { return Vector2(x * rhs.x, y * rhs.y); } @@ -96,10 +96,10 @@ constexpr Vector2 Vector2::cwiseMul(Vector2 rhs) const //////////////////////////////////////////////////////////// template -constexpr Vector2 Vector2::cwiseDiv(Vector2 rhs) const +constexpr Vector2 Vector2::componentWiseDiv(Vector2 rhs) const { - assert(rhs.x != 0 && "Vector2::cwiseDiv() cannot divide by 0"); - assert(rhs.y != 0 && "Vector2::cwiseDiv() cannot divide by 0"); + assert(rhs.x != 0 && "Vector2::componentWiseDiv() cannot divide by 0"); + assert(rhs.y != 0 && "Vector2::componentWiseDiv() cannot divide by 0"); return Vector2(x / rhs.x, y / rhs.y); } diff --git a/include/SFML/System/Vector3.hpp b/include/SFML/System/Vector3.hpp index fd840ed9d..15820e5ca 100644 --- a/include/SFML/System/Vector3.hpp +++ b/include/SFML/System/Vector3.hpp @@ -73,7 +73,7 @@ public: //////////////////////////////////////////////////////////// /// \brief Length of the vector (floating-point). /// - /// If you are not interested in the actual length, but only in comparisons, consider using lengthSq(). + /// If you are not interested in the actual length, but only in comparisons, consider using lengthSquared(). /// //////////////////////////////////////////////////////////// [[nodiscard]] SFML_SYSTEM_API T length() const; @@ -84,7 +84,7 @@ public: /// Suitable for comparisons, more efficient than length(). /// //////////////////////////////////////////////////////////// - [[nodiscard]] constexpr T lengthSq() const; + [[nodiscard]] constexpr T lengthSquared() const; //////////////////////////////////////////////////////////// /// \brief Vector with same direction but length 1 (floating-point). @@ -115,7 +115,7 @@ public: /// This operation is also known as the Hadamard or Schur product. /// //////////////////////////////////////////////////////////// - [[nodiscard]] constexpr Vector3 cwiseMul(const Vector3& rhs) const; + [[nodiscard]] constexpr Vector3 componentWiseMul(const Vector3& rhs) const; //////////////////////////////////////////////////////////// /// \brief Component-wise division of \c *this and \c rhs. @@ -127,7 +127,7 @@ public: /// \pre Neither component of \c rhs is zero. /// //////////////////////////////////////////////////////////// - [[nodiscard]] constexpr Vector3 cwiseDiv(const Vector3& rhs) const; + [[nodiscard]] constexpr Vector3 componentWiseDiv(const Vector3& rhs) const; //////////////////////////////////////////////////////////// // Member data diff --git a/include/SFML/System/Vector3.inl b/include/SFML/System/Vector3.inl index 923c3c36e..1d0a45106 100644 --- a/include/SFML/System/Vector3.inl +++ b/include/SFML/System/Vector3.inl @@ -59,7 +59,7 @@ z(static_cast(vector.z)) //////////////////////////////////////////////////////////// template -constexpr T Vector3::lengthSq() const +constexpr T Vector3::lengthSquared() const { return dot(*this); } @@ -83,7 +83,7 @@ constexpr Vector3 Vector3::cross(const Vector3& rhs) const //////////////////////////////////////////////////////////// template -constexpr Vector3 Vector3::cwiseMul(const Vector3& rhs) const +constexpr Vector3 Vector3::componentWiseMul(const Vector3& rhs) const { return Vector3(x * rhs.x, y * rhs.y, z * rhs.z); } @@ -91,11 +91,11 @@ constexpr Vector3 Vector3::cwiseMul(const Vector3& rhs) const //////////////////////////////////////////////////////////// template -constexpr Vector3 Vector3::cwiseDiv(const Vector3& rhs) const +constexpr Vector3 Vector3::componentWiseDiv(const Vector3& rhs) const { - assert(rhs.x != 0 && "Vector3::cwiseDiv() cannot divide by 0"); - assert(rhs.y != 0 && "Vector3::cwiseDiv() cannot divide by 0"); - assert(rhs.z != 0 && "Vector3::cwiseDiv() cannot divide by 0"); + assert(rhs.x != 0 && "Vector3::componentWiseDiv() cannot divide by 0"); + assert(rhs.y != 0 && "Vector3::componentWiseDiv() cannot divide by 0"); + assert(rhs.z != 0 && "Vector3::componentWiseDiv() cannot divide by 0"); return Vector3(x / rhs.x, y / rhs.y, z / rhs.z); } diff --git a/src/SFML/Graphics/RenderTarget.cpp b/src/SFML/Graphics/RenderTarget.cpp index c16fb7058..64b6584e2 100644 --- a/src/SFML/Graphics/RenderTarget.cpp +++ b/src/SFML/Graphics/RenderTarget.cpp @@ -306,9 +306,10 @@ Vector2f RenderTarget::mapPixelToCoords(Vector2i point) const Vector2f RenderTarget::mapPixelToCoords(Vector2i point, const View& view) const { // First, convert from viewport coordinates to homogeneous coordinates - const FloatRect viewport = FloatRect(getViewport(view)); - const Vector2f normalized = Vector2f(-1, 1) + - Vector2f(2, -2).cwiseMul(Vector2f(point) - viewport.position).cwiseDiv(viewport.size); + const FloatRect viewport = FloatRect(getViewport(view)); + const Vector2f + normalized = Vector2f(-1, 1) + + Vector2f(2, -2).componentWiseMul(Vector2f(point) - viewport.position).componentWiseDiv(viewport.size); // Then transform by the inverse of the view matrix return view.getInverseTransform().transformPoint(normalized); @@ -330,8 +331,9 @@ Vector2i RenderTarget::mapCoordsToPixel(Vector2f point, const View& view) const // Then convert to viewport coordinates const FloatRect viewport = FloatRect(getViewport(view)); - return Vector2i((normalized.cwiseMul({1, -1}) + sf::Vector2f(1, 1)).cwiseDiv({2, 2}).cwiseMul(viewport.size) + - viewport.position); + return Vector2i( + (normalized.componentWiseMul({1, -1}) + sf::Vector2f(1, 1)).componentWiseDiv({2, 2}).componentWiseMul(viewport.size) + + viewport.position); } diff --git a/src/SFML/Graphics/Shape.cpp b/src/SFML/Graphics/Shape.cpp index 9b4eb40cf..e6d576af7 100644 --- a/src/SFML/Graphics/Shape.cpp +++ b/src/SFML/Graphics/Shape.cpp @@ -271,8 +271,8 @@ void Shape::updateTexCoords() for (std::size_t i = 0; i < m_vertices.getVertexCount(); ++i) { - const Vector2f ratio = (m_vertices[i].position - m_insideBounds.position).cwiseDiv(safeInsideSize); - m_vertices[i].texCoords = convertedTextureRect.position + convertedTextureRect.size.cwiseMul(ratio); + const Vector2f ratio = (m_vertices[i].position - m_insideBounds.position).componentWiseDiv(safeInsideSize); + m_vertices[i].texCoords = convertedTextureRect.position + convertedTextureRect.size.componentWiseMul(ratio); } } diff --git a/src/SFML/System/Vector2.cpp b/src/SFML/System/Vector2.cpp index 2a1191c7e..b7c35991f 100644 --- a/src/SFML/System/Vector2.cpp +++ b/src/SFML/System/Vector2.cpp @@ -88,7 +88,7 @@ Vector2 Vector2::projectedOnto(Vector2 axis) const static_assert(std::is_floating_point_v, "Vector2::projectedOnto() is only supported for floating point types"); assert(axis != Vector2() && "Vector2::projectedOnto() cannot project onto a zero vector"); - return dot(axis) / axis.lengthSq() * axis; + return dot(axis) / axis.lengthSquared() * axis; } diff --git a/test/System/Vector2.test.cpp b/test/System/Vector2.test.cpp index aa47501fe..cb0622fc6 100644 --- a/test/System/Vector2.test.cpp +++ b/test/System/Vector2.test.cpp @@ -254,13 +254,13 @@ TEMPLATE_TEST_CASE("[System] sf::Vector2", "", int, float) constexpr sf::Vector2f v(2.4f, 3.0f); CHECK(v.length() == Approx(3.84187f)); - CHECK(v.lengthSq() == Approx(14.7599650969f)); + CHECK(v.lengthSquared() == Approx(14.7599650969f)); CHECK(v.normalized() == Approx(sf::Vector2f(0.624695f, 0.780869f))); constexpr sf::Vector2f w(-0.7f, -2.2f); CHECK(w.length() == Approx(2.30868f)); - CHECK(w.lengthSq() == Approx(5.3300033f)); + CHECK(w.lengthSquared() == Approx(5.3300033f)); CHECK(w.normalized() == Approx(sf::Vector2f(-0.303204f, -0.952926f))); } @@ -304,10 +304,10 @@ TEMPLATE_TEST_CASE("[System] sf::Vector2", "", int, float) CHECK(v.cross(w) == Approx(-3.18f)); CHECK(w.cross(v) == Approx(+3.18f)); - CHECK(v.cwiseMul(w) == Approx(sf::Vector2f(-1.68f, -6.6f))); - CHECK(w.cwiseMul(v) == Approx(sf::Vector2f(-1.68f, -6.6f))); - CHECK(v.cwiseDiv(w) == Approx(sf::Vector2f(-3.428571f, -1.363636f))); - CHECK(w.cwiseDiv(v) == Approx(sf::Vector2f(-0.291666f, -0.733333f))); + CHECK(v.componentWiseMul(w) == Approx(sf::Vector2f(-1.68f, -6.6f))); + CHECK(w.componentWiseMul(v) == Approx(sf::Vector2f(-1.68f, -6.6f))); + CHECK(v.componentWiseDiv(w) == Approx(sf::Vector2f(-3.428571f, -1.363636f))); + CHECK(w.componentWiseDiv(v) == Approx(sf::Vector2f(-0.291666f, -0.733333f))); } SECTION("Projection") @@ -334,12 +334,12 @@ TEMPLATE_TEST_CASE("[System] sf::Vector2", "", int, float) STATIC_CHECK(v.y == 2); STATIC_CHECK(v + w == sf::Vector2(3, -4)); - STATIC_CHECK(v.lengthSq() == 5); + STATIC_CHECK(v.lengthSquared() == 5); STATIC_CHECK(v.perpendicular() == sf::Vector2(-2, 1)); STATIC_CHECK(v.dot(w) == -10); STATIC_CHECK(v.cross(w) == -10); - STATIC_CHECK(v.cwiseMul(w) == sf::Vector2(2, -12)); - STATIC_CHECK(w.cwiseDiv(v) == sf::Vector2(2, -3)); + STATIC_CHECK(v.componentWiseMul(w) == sf::Vector2(2, -12)); + STATIC_CHECK(w.componentWiseDiv(v) == sf::Vector2(2, -3)); } } diff --git a/test/System/Vector3.test.cpp b/test/System/Vector3.test.cpp index 33043bb44..29da14bba 100644 --- a/test/System/Vector3.test.cpp +++ b/test/System/Vector3.test.cpp @@ -211,7 +211,7 @@ TEMPLATE_TEST_CASE("[System] sf::Vector3", "", int, float) constexpr sf::Vector3f v(2.4f, 3.0f, 5.2f); CHECK(v.length() == Approx(6.46529f)); - CHECK(v.lengthSq() == Approx(41.79997f)); + CHECK(v.lengthSquared() == Approx(41.79997f)); CHECK(v.normalized() == Approx(sf::Vector3f(0.37121f, 0.46401f, 0.80429f))); } @@ -226,9 +226,9 @@ TEMPLATE_TEST_CASE("[System] sf::Vector3", "", int, float) CHECK(v.cross(w) == Approx(sf::Vector3f(-2.96f, 7.88f, -3.18f))); CHECK(w.cross(v) == Approx(sf::Vector3f(2.96f, -7.88f, 3.18f))); - CHECK(v.cwiseMul(w) == Approx(sf::Vector3f(-1.68f, -6.6f, -24.96f))); - CHECK(w.cwiseMul(v) == Approx(sf::Vector3f(-1.68f, -6.6f, -24.96f))); - CHECK(v.cwiseDiv(w) == Approx(sf::Vector3f(-3.428571f, -1.363636f, -1.0833333f))); - CHECK(w.cwiseDiv(v) == Approx(sf::Vector3f(-0.291666f, -0.733333f, -0.9230769f))); + CHECK(v.componentWiseMul(w) == Approx(sf::Vector3f(-1.68f, -6.6f, -24.96f))); + CHECK(w.componentWiseMul(v) == Approx(sf::Vector3f(-1.68f, -6.6f, -24.96f))); + CHECK(v.componentWiseDiv(w) == Approx(sf::Vector3f(-3.428571f, -1.363636f, -1.0833333f))); + CHECK(w.componentWiseDiv(v) == Approx(sf::Vector3f(-0.291666f, -0.733333f, -0.9230769f))); } }