From 71c0050cf4371c4727434403c776888aeec7e740 Mon Sep 17 00:00:00 2001 From: Matthew Mott Date: Sun, 4 Apr 2021 13:32:58 +0100 Subject: [PATCH] Simplify Vector3 addition operators Operators are now non-member functions, and no longer have a separate template type for the second vector (this would prevent adding a BasicVector3 to a BasicVector3, but it does not seem that we have any code which needs to do this). --- libs/math/Vector3.h | 37 +++++++++++++++++-------------------- test/math/Vector.cpp | 24 ++++++++++++++++++++---- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/libs/math/Vector3.h b/libs/math/Vector3.h index 0c5c4f29a9..ed60fec429 100644 --- a/libs/math/Vector3.h +++ b/libs/math/Vector3.h @@ -116,26 +116,6 @@ class BasicVector3 return BasicVector3(-_v[0], -_v[1], -_v[2]); } - /* Define the addition operators + and += with any other BasicVector3 of type OtherElement - * The vectors are added to each other element-wise - */ - template - BasicVector3 operator+ (const BasicVector3& other) const { - return BasicVector3( - _v[0] + static_cast(other.x()), - _v[1] + static_cast(other.y()), - _v[2] + static_cast(other.z()) - ); - } - - template - BasicVector3& operator+= (const BasicVector3& other) { - _v[0] += static_cast(other.x()); - _v[1] += static_cast(other.y()); - _v[2] += static_cast(other.z()); - return *this; - } - /* Define the substraction operators - and -= with any other BasicVector3 of type OtherElement * The vectors are substracted from each other element-wise */ @@ -443,6 +423,23 @@ BasicVector3& operator*=(BasicVector3& v, S other) return v; } +/// Componentwise addition of two vectors +template +BasicVector3 operator+(const BasicVector3& v1, const BasicVector3& v2) +{ + return BasicVector3(v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z()); +} + +/// Componentwise vector addition in place +template +BasicVector3& operator+=(BasicVector3& v1, const BasicVector3& v2) +{ + v1.x() += v2.x(); + v1.y() += v2.y(); + v1.z() += v2.z(); + return v1; +} + /// Stream insertion for BasicVector3 template inline std::ostream& operator<<(std::ostream& st, BasicVector3 vec) diff --git a/test/math/Vector.cpp b/test/math/Vector.cpp index 0eaa774cac..732d2cdfdc 100644 --- a/test/math/Vector.cpp +++ b/test/math/Vector.cpp @@ -88,17 +88,33 @@ TEST(MathTest, NormaliseVector3) TEST(MathTest, AddVector3) { Vector3 v1(2, -5, 17); - Vector3 v2(11, 12, -0.5); + const Vector3 v1Orig = v1; + const Vector3 v2(11, 12, -0.5); - EXPECT_EQ(v1 + v2, Vector3(13, 7, 16.5)); + // Add and return + const Vector3 sum = v1 + v2; + EXPECT_EQ(sum, Vector3(13, 7, 16.5)); + EXPECT_EQ(v1, v1Orig); + + // Add in place + v1 += v2; + EXPECT_EQ(v1, sum); } TEST(MathTest, AddVector4) { Vector4 v1(10, 0.25, -60, 11); - Vector4 v2(0, -18, 276, 0.75); + const Vector4 v1Orig = v1; + const Vector4 v2(0, -18, 276, 0.75); + + // Add and return + const Vector4 sum = v1 + v2; + EXPECT_EQ(sum, Vector4(10, -17.75, 216, 11.75)); + EXPECT_EQ(v1, v1Orig); - EXPECT_EQ(v1 + v2, Vector4(10, -17.75, 216, 11.75)); + // Add in place + v1 += v2; + EXPECT_EQ(v1, sum); } TEST(MathTest, SubtractVector3)