Skip to content

Commit

Permalink
BasicVector4 operator+ and operator+= are now non-members
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Mott committed Sep 1, 2021
1 parent b09d41a commit f507bc0
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions libs/math/Vector4.h
Expand Up @@ -100,28 +100,6 @@ class BasicVector4
return !(*this == other);
}

/* Define the addition operators + and += with any other BasicVector4 of type OtherElement
* The vectors are added to each other element-wise
*/
template<typename OtherElement>
BasicVector4<Element> operator+ (const BasicVector4<OtherElement>& other) const {
return BasicVector4<Element>(
_v[0] + static_cast<Element>(other.x()),
_v[1] + static_cast<Element>(other.y()),
_v[2] + static_cast<Element>(other.z()),
_v[3] + static_cast<Element>(other.w())
);
}

template<typename OtherElement>
BasicVector4<Element>& operator+= (const BasicVector4<OtherElement>& other) {
_v[0] += static_cast<Element>(other.x());
_v[1] += static_cast<Element>(other.y());
_v[2] += static_cast<Element>(other.z());
_v[3] += static_cast<Element>(other.w());
return *this;
}

/* Define the substraction operators - and -= with any other BasicVector4 of type OtherElement
* The vectors are substracted from each other element-wise
*/
Expand Down Expand Up @@ -278,6 +256,26 @@ class BasicVector4
}
}; // BasicVector4

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

template <typename T>
BasicVector4<T>& operator+=(BasicVector4<T>& v1, const BasicVector4<T>& v2)
{
v1.x() += v2.x();
v1.y() += v2.y();
v1.z() += v2.z();
v1.w() += v2.w();
return v1;
}

/// Multiply BasicVector4 with a scalar
template <
typename T, typename S,
Expand Down

0 comments on commit f507bc0

Please sign in to comment.