From e65d2803b0eb02c30b0e659413555a9712585e37 Mon Sep 17 00:00:00 2001 From: Thomas Fischer <thomas.fischer@ufz.de> Date: Mon, 3 Mar 2014 10:33:00 +0100 Subject: [PATCH] [MathLib] Vector3: Impl. and test for normalization. --- MathLib/Vector3.h | 10 ++++++++++ Tests/MathLib/TestVector3.cpp | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/MathLib/Vector3.h b/MathLib/Vector3.h index 55b36bc8347..4650d1f4268 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 056fd9372fc..97274075ca4 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()); + } -- GitLab