Skip to content

Commit

Permalink
binary operators: added constexpr + pass by copy of simple arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Nov 16, 2021
1 parent 1446961 commit cab8d18
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 111 deletions.
16 changes: 8 additions & 8 deletions src/modm/math/geometry/vector1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ namespace modm
T* ptr();
const T* ptr() const;

Vector operator - () const;
Vector operator + (const Vector &rhs) const;
Vector operator - (const Vector &rhs) const;
T operator * (const Vector &rhs) const;
Vector operator * (const T &rhs) const;
Vector operator / (const T &rhs) const;
constexpr Vector operator - () const;
constexpr Vector operator + (const Vector &rhs) const;
constexpr Vector operator - (const Vector &rhs) const;
constexpr T operator * (const Vector &rhs) const;
constexpr Vector operator * (T rhs) const;
constexpr Vector operator / (T rhs) const;

Vector& operator += (const Vector &rhs);
Vector& operator -= (const Vector &rhs);
Vector& operator *= (const T &rhs);
Vector& operator /= (const T &rhs);
Vector& operator *= (T rhs);
Vector& operator /= (T rhs);

T getLength() const;
WideType getLengthSquared() const;
Expand Down
33 changes: 16 additions & 17 deletions src/modm/math/geometry/vector1_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,43 @@ modm::Vector<T, 1>::ptr() const

// ----------------------------------------------------------------------------
template<typename T>
modm::Vector<T, 1>
constexpr modm::Vector<T, 1>
modm::Vector<T, 1>::operator - () const
{
return Vector<T, 1>(-x);
}

template<typename T>
constexpr modm::Vector<T, 1>
modm::Vector<T, 1>::operator + (const modm::Vector<T, 1> &rhs) const
{
return modm::Vector<T, 1>(x+rhs.x);
}

template<typename T>
modm::Vector<T, 1>
constexpr modm::Vector<T, 1>
modm::Vector<T, 1>::operator - (const modm::Vector<T, 1> &rhs) const
{
return modm::Vector<T, 1>(x-rhs.x);
}

template<typename T>
T
constexpr T
modm::Vector<T, 1>::operator * (const modm::Vector<T, 1> &rhs) const
{
return x*rhs.x;
}

template<typename T>
modm::Vector<T, 1>
modm::Vector<T, 1>::operator * (const T &rhs) const
constexpr modm::Vector<T, 1>
modm::Vector<T, 1>::operator * (T rhs) const
{
return modm::Vector<T, 1>(x*rhs);
}

template<typename T>
modm::Vector<T, 1>
modm::Vector<T, 1>::operator / (const T &rhs) const
constexpr modm::Vector<T, 1>
modm::Vector<T, 1>::operator / (T rhs) const
{
return modm::Vector<T, 1>(x/rhs);
}
Expand All @@ -111,28 +118,20 @@ modm::Vector<T, 1>::operator -= (const modm::Vector<T, 1> &rhs)

template<typename T>
modm::Vector<T, 1>&
modm::Vector<T, 1>::operator *= (const T &rhs)
modm::Vector<T, 1>::operator *= (T rhs)
{
x *= rhs;
return *this;
}

template<typename T>
modm::Vector<T, 1>&
modm::Vector<T, 1>::operator /= (const T &rhs)
modm::Vector<T, 1>::operator /= (T rhs)
{
x /= rhs;
return *this;
}

// ----------------------------------------------------------------------------
template<typename T>
modm::Vector<T, 1>
modm::Vector<T, 1>::operator - () const
{
return Vector<T, 1>(-x);
}

// ----------------------------------------------------------------------------
template<typename T>
T
Expand Down
17 changes: 9 additions & 8 deletions src/modm/math/geometry/vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,19 @@ namespace modm
T* ptr();
const T* ptr() const;

Vector operator - () const;
constexpr Vector operator - (const Vector &rhs) const;
constexpr Vector operator - () const;
constexpr Vector operator + (const Vector &rhs) const;
T operator * (const Vector &rhs) const;
T operator ^ (const Vector &rhs) const;
Vector operator * (float rhs) const;
Vector operator / (float rhs) const;
constexpr Vector operator - (const Vector &rhs) const;
constexpr T operator * (const Vector &rhs) const;
constexpr T operator ^ (const Vector &rhs) const;
// TODO Why is this float for 2D-vector
constexpr Vector operator * (float rhs) const;
constexpr Vector operator / (float rhs) const;

Vector& operator += (const Vector &rhs);
Vector& operator -= (const Vector &rhs);
Vector& operator *= (const T &rhs);
Vector& operator /= (const T &rhs);
Vector& operator *= (T rhs);
Vector& operator /= (T rhs);
Vector& operator ~ ();

Matrix<T, 2, 1>&
Expand Down
15 changes: 7 additions & 8 deletions src/modm/math/geometry/vector2_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,12 @@ modm::Vector<T, 2>::ptr() const

// ----------------------------------------------------------------------------
template<typename T>
modm::Vector<T, 2>
constexpr modm::Vector<T, 2>
modm::Vector<T, 2>::operator - () const
{
return Vector<T, 2>(-x, -y);
}

// ----------------------------------------------------------------------------
template<typename T>
constexpr modm::Vector<T, 2>
modm::Vector<T, 2>::operator - (const modm::Vector<T, 2> &rhs) const
Expand All @@ -254,29 +253,29 @@ modm::Vector<T, 2>::operator + (const modm::Vector<T, 2> &rhs) const
}

template<typename T>
T
constexpr T
modm::Vector<T, 2>::operator * (const modm::Vector<T, 2> &rhs) const
{
return (x * rhs.x + y * rhs.y);
}

template<typename T>
T
constexpr T
modm::Vector<T, 2>::operator ^ (const modm::Vector<T, 2> &rhs) const
{
return (x * rhs.y - y * rhs.x);
}

template<typename T>
modm::Vector<T, 2>
constexpr modm::Vector<T, 2>
modm::Vector<T, 2>::operator * (float scale) const
{
return Vector<T, 2>(GeometricTraits<T>::round(x * scale),
GeometricTraits<T>::round(y * scale));
}

template<typename T>
modm::Vector<T, 2>
constexpr modm::Vector<T, 2>
modm::Vector<T, 2>::operator / (float scale) const
{
return Vector<T, 2>(GeometricTraits<T>::round(x / scale),
Expand Down Expand Up @@ -306,7 +305,7 @@ modm::Vector<T, 2>::operator -= (const modm::Vector<T, 2> &rhs)

template<typename T>
modm::Vector<T, 2>&
modm::Vector<T, 2>::operator *= (const T &rhs)
modm::Vector<T, 2>::operator *= (T rhs)
{
x *= rhs;
y *= rhs;
Expand All @@ -316,7 +315,7 @@ modm::Vector<T, 2>::operator *= (const T &rhs)

template<typename T>
modm::Vector<T, 2>&
modm::Vector<T, 2>::operator /= (const T &rhs)
modm::Vector<T, 2>::operator /= (T rhs)
{
x /= rhs;
y /= rhs;
Expand Down
18 changes: 9 additions & 9 deletions src/modm/math/geometry/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ namespace modm
T* ptr();
const T* ptr() const;

Vector operator - () const;
Vector operator + (const Vector &rhs) const;
Vector operator - (const Vector &rhs) const;
T operator * (const Vector &rhs) const;
Vector operator ^ (const Vector &rhs) const;
Vector operator * (const T &rhs) const;
Vector operator / (const T &rhs) const;
constexpr Vector operator - () const;
constexpr Vector operator + (const Vector &rhs) const;
constexpr Vector operator - (const Vector &rhs) const;
constexpr T operator * (const Vector &rhs) const;
constexpr Vector operator ^ (const Vector &rhs) const;
constexpr Vector operator * (T rhs) const;
constexpr Vector operator / (T rhs) const;

Vector& operator += (const Vector &rhs);
Vector& operator -= (const Vector &rhs);
Vector& operator *= (const T &rhs);
Vector& operator /= (const T &rhs);
Vector& operator *= (T rhs);
Vector& operator /= (T rhs);

float getLength() const;
float getLengthSquared() const;
Expand Down
35 changes: 17 additions & 18 deletions src/modm/math/geometry/vector3_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,43 +63,50 @@ modm::Vector<T, 3>::ptr() const

// ----------------------------------------------------------------------------
template<typename T>
modm::Vector<T, 3>
constexpr modm::Vector<T, 3>
modm::Vector<T, 3>::operator - () const
{
return modm::Vector<T, 3>(-x, -y, -z);
}

template<typename T>
constexpr modm::Vector<T, 3>
modm::Vector<T, 3>::operator + (const modm::Vector<T, 3> &rhs) const
{
return modm::Vector<T, 3>(x+rhs.x, y+rhs.y, z+rhs.z);
}

template<typename T>
modm::Vector<T, 3>
constexpr modm::Vector<T, 3>
modm::Vector<T, 3>::operator - (const modm::Vector<T, 3> &rhs) const
{
return modm::Vector<T, 3>(x-rhs.x, y-rhs.y, z-rhs.z);
}

template<typename T>
T
constexpr T
modm::Vector<T, 3>::operator * (const modm::Vector<T, 3> &rhs) const
{
return x*rhs.x + y*rhs.y + z*rhs.z;
}

template<typename T>
modm::Vector<T, 3>
constexpr modm::Vector<T, 3>
modm::Vector<T, 3>::operator ^ (const modm::Vector<T, 3> &rhs) const
{
return modm::Vector<T, 3>(y*rhs.z-z*rhs.y, z*rhs.x-x*rhs.z, x*rhs.y-y*rhs.x);
}

template<typename T>
modm::Vector<T, 3>
modm::Vector<T, 3>::operator * (const T &rhs) const
constexpr modm::Vector<T, 3>
modm::Vector<T, 3>::operator * (T rhs) const
{
return modm::Vector<T, 3>(x*rhs, y*rhs, z*rhs);
}

template<typename T>
modm::Vector<T, 3>
modm::Vector<T, 3>::operator / (const T &rhs) const
constexpr modm::Vector<T, 3>
modm::Vector<T, 3>::operator / (T rhs) const
{
return modm::Vector<T, 3>(x/rhs, y/rhs, z/rhs);
}
Expand Down Expand Up @@ -129,7 +136,7 @@ modm::Vector<T, 3>::operator -= (const modm::Vector<T, 3> &rhs)

template<typename T>
modm::Vector<T, 3>&
modm::Vector<T, 3>::operator *= (const T &rhs)
modm::Vector<T, 3>::operator *= (T rhs)
{
x *= rhs;
y *= rhs;
Expand All @@ -140,7 +147,7 @@ modm::Vector<T, 3>::operator *= (const T &rhs)

template<typename T>
modm::Vector<T, 3>&
modm::Vector<T, 3>::operator /= (const T &rhs)
modm::Vector<T, 3>::operator /= (T rhs)
{
x /= rhs;
y /= rhs;
Expand All @@ -149,14 +156,6 @@ modm::Vector<T, 3>::operator /= (const T &rhs)
return *this;
}

// ----------------------------------------------------------------------------
template<typename T>
modm::Vector<T, 3>
modm::Vector<T, 3>::operator - () const
{
return modm::Vector<T, 3>(-x, -y, -z);
}

// ----------------------------------------------------------------------------
template<typename T>
float
Expand Down
16 changes: 8 additions & 8 deletions src/modm/math/geometry/vector4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,17 @@ namespace modm
T* ptr();
const T* ptr() const;

Vector operator - () const;
Vector operator + (const Vector &rhs) const;
Vector operator - (const Vector &rhs) const;
T operator * (const Vector &rhs) const;
Vector operator * (const T &rhs) const;
Vector operator / (const T &rhs) const;
constexpr Vector operator - () const;
constexpr Vector operator + (const Vector &rhs) const;
constexpr Vector operator - (const Vector &rhs) const;
constexpr T operator * (const Vector &rhs) const;
constexpr Vector operator * (T rhs) const;
constexpr Vector operator / (T rhs) const;

Vector& operator += (const Vector &rhs);
Vector& operator -= (const Vector &rhs);
Vector& operator *= (const T &rhs);
Vector& operator /= (const T &rhs);
Vector& operator *= (T rhs);
Vector& operator /= (T rhs);

float getLength() const;
float getLengthSquared() const;
Expand Down
Loading

0 comments on commit cab8d18

Please sign in to comment.