Skip to content

Commit

Permalink
Vector3 negation now implemented by Eigen
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Mott committed Apr 25, 2021
1 parent a6bdc34 commit df6f951
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
23 changes: 16 additions & 7 deletions libs/math/Vector3.h
Expand Up @@ -30,13 +30,18 @@
template<typename T>
class BasicVector3
{
// Internal Eigen vector for storage and calculations
using Vec = Eigen::Matrix<T, 3, 1>;
Vec _v;

public:
/// Eigen vector type to store a BasicVector3's data
using Eigen_T = Eigen::Matrix<T, 3, 1>;

// Public typedef to read the type of our elements
typedef T ElementType;
using ElementType = T;

private:
// Eigen vector for storage and calculations
Eigen_T _v;

public:

/// Initialise Vector with all zeroes.
BasicVector3(): _v(0, 0, 0)
Expand All @@ -46,6 +51,10 @@ class BasicVector3
BasicVector3(T x, T y, T z): _v(x, y, z)
{}

/// Construct directly from the underlying Eigen vector type
BasicVector3(const Eigen_T& vec): _v(vec)
{}

/**
* \brief Construct a BasicVector3 from a 3-element array.
*
Expand All @@ -69,7 +78,7 @@ class BasicVector3
/// Set all 3 components to the provided values.
void set(T x, T y, T z)
{
_v = Vec(x, y, z);
_v = Eigen_T(x, y, z);
}

// Return mutable references to the vector components
Expand Down Expand Up @@ -105,7 +114,7 @@ class BasicVector3
/// Return the componentwise negation of this vector
BasicVector3<T> operator- () const
{
return BasicVector3<T>(-_v[0], -_v[1], -_v[2]);
return BasicVector3<T>(-_v);
}

/// Return the Pythagorean length of this vector.
Expand Down
1 change: 1 addition & 0 deletions test/math/Vector.cpp
Expand Up @@ -90,6 +90,7 @@ TEST(MathTest, NegateVector3)
Vector3 vec(5, 10, 125);

EXPECT_EQ(-vec, Vector3(-5, -10, -125));
EXPECT_EQ(-(-vec), vec);
}

TEST(MathTest, VectorLength)
Expand Down

0 comments on commit df6f951

Please sign in to comment.