From 0d497e0aca842e6a175a6b4b4a01efca913d5f44 Mon Sep 17 00:00:00 2001 From: Lucas Fernando Date: Wed, 22 Jul 2020 19:41:03 -0300 Subject: [PATCH 1/3] Sum function and test Signed-off-by: Lucas Fernando --- include/ignition/math/Vector4.hh | 7 +++++++ src/Vector4_TEST.cc | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/ignition/math/Vector4.hh b/include/ignition/math/Vector4.hh index 7091ce8fa..a536582f7 100644 --- a/include/ignition/math/Vector4.hh +++ b/include/ignition/math/Vector4.hh @@ -164,6 +164,13 @@ namespace ignition return *std::min_element(this->data, this->data+4); } + /// \brief Return the sum of the values + /// \return the sum + public: T Sum() const + { + return this->data[0] + this->data[1] + this->data[2] + this->data[3]; + } + /// \brief Assignment operator /// \param[in] _v the vector /// \return a reference to this vector diff --git a/src/Vector4_TEST.cc b/src/Vector4_TEST.cc index b14cb8515..105dd756a 100644 --- a/src/Vector4_TEST.cc +++ b/src/Vector4_TEST.cc @@ -199,6 +199,16 @@ TEST(Vector2Test, EqualTolerance) EXPECT_TRUE(math::Vector4d::Zero.Equal(math::Vector4d::One, 1.1)); } +///////////////////////////////////////////////// +TEST(Vector4dTest, Sum) +{ + math::Vector4d vec1(1.5, 2.5, 3.5, -4.5); + + EXPECT_TRUE(math::equal(math::Vector4d::Zero.Sum(), 0.0, 1e-6)); + EXPECT_TRUE(math::equal(math::Vector4d::One.Sum(), 4.0, 1e-6)); + EXPECT_TRUE(math::equal(vec1.Sum(), 3.0, 1e-6)); +} + ///////////////////////////////////////////////// TEST(Vector4dTest, Add) { From 8b7a8ebcabd5af0297c6baa8ce01d43531c447ff Mon Sep 17 00:00:00 2001 From: Lucas Fernando Date: Wed, 22 Jul 2020 21:20:52 -0300 Subject: [PATCH 2/3] Normalized function and test Signed-off-by: Lucas Fernando --- include/ignition/math/Vector4.hh | 9 +++++++++ src/Vector4_TEST.cc | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/include/ignition/math/Vector4.hh b/include/ignition/math/Vector4.hh index a536582f7..6f4021a34 100644 --- a/include/ignition/math/Vector4.hh +++ b/include/ignition/math/Vector4.hh @@ -115,6 +115,15 @@ namespace ignition } } + /// \brief Return a normalized vector + /// \return unit length vector + public: Vector4 Normalized() const + { + Vector4 result = *this; + result.Normalize(); + return result; + } + /// \brief Set the contents of the vector /// \param[in] _x value along x axis /// \param[in] _y value along y axis diff --git a/src/Vector4_TEST.cc b/src/Vector4_TEST.cc index 105dd756a..240829632 100644 --- a/src/Vector4_TEST.cc +++ b/src/Vector4_TEST.cc @@ -57,6 +57,10 @@ TEST(Vector4dTest, Vector4d) v.Normalize(); EXPECT_EQ(v, math::Vector4d(0.182574, 0.365148, 0.547723, 0.730297)); + // ::Normalized + v.Set(1, 2, 3, 4); + EXPECT_EQ(v.Normalized(), math::Vector4d(0.182574, 0.365148, 0.547723, 0.730297)); + // ::Set v.Set(2, 4, 6, 8); EXPECT_EQ(v, math::Vector4d(2, 4, 6, 8)); From 5efd645eec6cedce40206be8afbf7b369b2c4565 Mon Sep 17 00:00:00 2001 From: Lucas Fernando Date: Fri, 31 Jul 2020 14:01:07 -0300 Subject: [PATCH 3/3] Break too long line Signed-off-by: Lucas Fernando --- src/Vector4_TEST.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Vector4_TEST.cc b/src/Vector4_TEST.cc index f4217e1bc..dc65da4ea 100644 --- a/src/Vector4_TEST.cc +++ b/src/Vector4_TEST.cc @@ -59,7 +59,8 @@ TEST(Vector4dTest, Vector4d) // ::Normalized v.Set(1, 2, 3, 4); - EXPECT_EQ(v.Normalized(), math::Vector4d(0.182574, 0.365148, 0.547723, 0.730297)); + EXPECT_EQ(v.Normalized(), + math::Vector4d(0.182574, 0.365148, 0.547723, 0.730297)); // ::Set v.Set(2, 4, 6, 8);