Skip to content

Commit

Permalink
Simplify Vector3 addition operators
Browse files Browse the repository at this point in the history
Operators are now non-member functions, and no longer have a separate template
type for the second vector (this would prevent adding a BasicVector3<double> to
a BasicVector3<float>, but it does not seem that we have any code which needs
to do this).
  • Loading branch information
Matthew Mott committed Apr 4, 2021
1 parent 72450cf commit 71c0050
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
37 changes: 17 additions & 20 deletions libs/math/Vector3.h
Expand Up @@ -116,26 +116,6 @@ class BasicVector3
return BasicVector3<T>(-_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<typename OtherElement>
BasicVector3<T> operator+ (const BasicVector3<OtherElement>& other) const {
return BasicVector3<T>(
_v[0] + static_cast<T>(other.x()),
_v[1] + static_cast<T>(other.y()),
_v[2] + static_cast<T>(other.z())
);
}

template<typename OtherElement>
BasicVector3<T>& operator+= (const BasicVector3<OtherElement>& other) {
_v[0] += static_cast<T>(other.x());
_v[1] += static_cast<T>(other.y());
_v[2] += static_cast<T>(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
*/
Expand Down Expand Up @@ -443,6 +423,23 @@ BasicVector3<T>& operator*=(BasicVector3<T>& v, S other)
return v;
}

/// Componentwise addition of two vectors
template <typename T>
BasicVector3<T> operator+(const BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
return BasicVector3<T>(v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z());
}

/// Componentwise vector addition in place
template <typename T>
BasicVector3<T>& operator+=(BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
v1.x() += v2.x();
v1.y() += v2.y();
v1.z() += v2.z();
return v1;
}

/// Stream insertion for BasicVector3
template<typename T>
inline std::ostream& operator<<(std::ostream& st, BasicVector3<T> vec)
Expand Down
24 changes: 20 additions & 4 deletions test/math/Vector.cpp
Expand Up @@ -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)
Expand Down

0 comments on commit 71c0050

Please sign in to comment.