Skip to content

Commit

Permalink
Test and de-member Vector3 componentwise product
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Mott committed Apr 4, 2021
1 parent d795d57 commit 5762253
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
53 changes: 17 additions & 36 deletions libs/math/Vector3.h
Expand Up @@ -116,31 +116,6 @@ class BasicVector3
return BasicVector3<T>(-_v[0], -_v[1], -_v[2]);
}

/* Define the multiplication operators * and *= with another Vector3 of type OtherElement
*
* The vectors are multiplied element-wise
*
* greebo: This is mathematically kind of senseless, as this is a mixture of
* a dot product and scalar multiplication. It can be used to scale each
* vector component by a different factor, so maybe this comes in handy.
*/
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 division operators / and /= with another Vector3 of type OtherElement
* The vectors are divided element-wise
*/
Expand Down Expand Up @@ -182,22 +157,13 @@ class BasicVector3
return *this;
}

/*
* Mathematical operations on the BasicVector3
*/

/** Return the length of this vector.
*
* @returns
* The Pythagorean length of this vector.
*/
/// Return the Pythagorean length of this vector.
float getLength() const {
float lenSquared = getLengthSquared();
return sqrt(lenSquared);
}

/** Return the squared length of this vector.
*/
/// Return the squared length of this vector.
float getLengthSquared() const {
float lenSquared = float(_v[0]) * float(_v[0]) +
float(_v[1]) * float(_v[1]) +
Expand Down Expand Up @@ -432,6 +398,21 @@ BasicVector3<T>& operator-= (BasicVector3<T>& v1, const BasicVector3<T>& v2)
return v1;
}

/// Componentwise (Hadamard) product 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 (Hadamard) product of two vectors, in place
template<typename T>
BasicVector3<T>& operator*= (BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
v1 = v1 * v2;
return v1;
}

/// Stream insertion for BasicVector3
template<typename T>
inline std::ostream& operator<<(std::ostream& st, BasicVector3<T> vec)
Expand Down
17 changes: 17 additions & 0 deletions test/math/Vector.cpp
Expand Up @@ -170,6 +170,23 @@ TEST(MathTest, ScalarMultiplyVector4)
EXPECT_EQ(0.5 * vec, Vector4(4, -7, 13, 0.9));
}

TEST(MathTest, ComponentwiseMultiplyVector3)
{
Vector3 v(0.8, 24, -300);
const Vector3 vOrig = v;
const Vector3 scale(2, 0.5, 3);

// Multiply and return
const Vector3 prod = v * scale;
EXPECT_EQ(prod, Vector3(1.6, 12, -900));
EXPECT_EQ(scale * v, prod);

// Multiply in place
EXPECT_EQ(v, vOrig);
v *= scale;
EXPECT_EQ(v, prod);
}

TEST(MathTest, Vector3AsCArray)
{
Vector3 vec(256, -10, 10000);
Expand Down

0 comments on commit 5762253

Please sign in to comment.