Add cross product.

This commit is contained in:
Kyle Isom 2019-08-04 23:29:56 -07:00
parent e157ee61e6
commit 3a5dd0490c
2 changed files with 44 additions and 2 deletions

View File

@ -15,6 +15,7 @@
namespace wr {
namespace geom {
/**
* Vector provides a standard interface for dimensionless fixed-size
* vectors. Once instantiated, they cannot be modified. Note that while
@ -203,6 +204,24 @@ public:
}
/**
* Compute the cross product of two vectors. This is only defined
* over three-dimensional vectors.
* @param other Another 3D vector.
* @return The cross product vector.
*/
Vector
cross(const Vector<T, N> &other)
{
assert(N == 3);
return Vector<T, N> {
(this->arr[1] * other.arr[2]) - (other.arr[1] * this->arr[2]),
-((this->arr[0] * other.arr[2]) - (other.arr[0] * this->arr[2])),
(this->arr[0] * other.arr[1]) - (other.arr[0] * this->arr[1])
};
}
/**
* Perform vector addition with another vector.
* @param other The vector to be added.

View File

@ -172,7 +172,7 @@ TEST(Vector3FloatTests, ParallelOrthogonalVectors)
}
TEST(Vector3FloatTests, Projections)
TEST(Vector4FloatTests, Projections)
{
geom::Vector4f a {3.009, -6.172, 3.692, -2.510};
geom::Vector4f b {6.404, -9.144, 2.759, 8.718};
@ -184,6 +184,17 @@ TEST(Vector3FloatTests, Projections)
}
TEST(Vector3FloatTests, CrossProduct)
{
geom::Vector3f a {8.462, 7.893, -8.187};
geom::Vector3f b {6.984, -5.975, 4.778};
geom::Vector3f c {-11.2046, -97.6094, -105.685};
c.setEpsilon(0.001);
EXPECT_EQ(c, a.cross(b));
}
TEST(Vector3DoubleTests, Magnitude)
{
geom::Vector3d v3d{1.0, -2.0, 3.0};
@ -302,7 +313,7 @@ TEST(Vector3DoubleTests, ParallelOrthogonalVectors)
}
TEST(Vector3DoubleTests, Projections)
TEST(Vector4DoubleTests, Projections)
{
geom::Vector4d a {3.009, -6.172, 3.692, -2.510};
geom::Vector4d b {6.404, -9.144, 2.759, 8.718};
@ -314,6 +325,18 @@ TEST(Vector3DoubleTests, Projections)
}
TEST(Vector3DoubleTests, CrossProduct)
{
geom::Vector3d a {8.462, 7.893, -8.187};
geom::Vector3d b {6.984, -5.975, 4.778};
geom::Vector3d c {-11.2046, -97.6094, -105.685};
c.setEpsilon(0.001); // double trouble
EXPECT_EQ(c, a.cross(b));
}
int
main(int argc, char **argv)
{