diff --git a/MathLib/Vector3.h b/MathLib/Vector3.h index 55b36bc8347efbfa4f0482765e3d4c069884b3b9..4650d1f4268d1543239258c0dd52618b6cf4a677 100644 --- a/MathLib/Vector3.h +++ b/MathLib/Vector3.h @@ -127,6 +127,16 @@ public: return *this; } + /** + * After applying the normalize operator to the vector its length is 1.0. + */ + void normalize() + { + const double s(1/length()); + for (std::size_t i(0); i < 3; i++) + this->_x[i] *= s; + } + /// Returns the length double length(void) const { diff --git a/Tests/MathLib/TestVector3.cpp b/Tests/MathLib/TestVector3.cpp index 056fd9372fc3c02fd758d7dbb746d11c03f90048..97274075ca4b17e351beb891fcc3dd0f6340dc2a 100644 --- a/Tests/MathLib/TestVector3.cpp +++ b/Tests/MathLib/TestVector3.cpp @@ -155,5 +155,10 @@ TEST(MathLib, TestVector3Multiplications) ASSERT_NEAR(1.0, v[0], std::numeric_limits<double>::epsilon()); ASSERT_NEAR(3.0, v[1], std::numeric_limits<double>::epsilon()); ASSERT_NEAR(5.0, v[2], std::numeric_limits<double>::epsilon()); + + // test normalisation + v.normalize(); + ASSERT_NEAR(1.0, v.length(), std::numeric_limits<double>::epsilon()); + }