From 690a66b3fae216126a8bb1376f4738cb4de865f2 Mon Sep 17 00:00:00 2001 From: Lucas Fernando Date: Mon, 13 Jul 2020 10:40:48 -0300 Subject: [PATCH 1/5] Add max and min functions to Vector4 Signed-off-by: Lucas Fernando --- include/ignition/math/Vector4.hh | 52 ++++++++++++++++++++++++++++++++ src/Vector4_TEST.cc | 33 ++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/include/ignition/math/Vector4.hh b/include/ignition/math/Vector4.hh index 4453d65ba..655b7ca6a 100644 --- a/include/ignition/math/Vector4.hh +++ b/include/ignition/math/Vector4.hh @@ -17,6 +17,8 @@ #ifndef IGNITION_MATH_VECTOR4_HH_ #define IGNITION_MATH_VECTOR4_HH_ +#include + #include #include #include @@ -126,6 +128,56 @@ namespace ignition this->data[3] = _w; } + /// \brief Set this vector's components to the maximum of itself and the + /// passed in vector + /// \param[in] _v the maximum clamping vector + public: void Max(const Vector4 &_v) + { + if (_v[0] > this->data[0]) + this->data[0] = _v[0]; + if (_v[1] > this->data[1]) + this->data[1] = _v[1]; + if (_v[2] > this->data[2]) + this->data[2] = _v[2]; + if (_v[3] > this->data[3]) + this->data[3] = _v[3]; + } + + /// \brief Set this vector's components to the minimum of itself and the + /// passed in vector + /// \param[in] _v the minimum clamping vector + public: void Min(const Vector4 &_v) + { + if (_v[0] < this->data[0]) + this->data[0] = _v[0]; + if (_v[1] < this->data[1]) + this->data[1] = _v[1]; + if (_v[2] < this->data[2]) + this->data[2] = _v[2]; + if (_v[3] < this->data[3]) + this->data[3] = _v[3]; + } + + /// \brief Get the maximum value in the vector + /// \return the maximum element + public: T Max() const + { + return std::max(std::max(std::max(this->data[0], + this->data[1]), + this->data[2]), + this->data[3]); + } + + /// \brief Get the minimum value in the vector + /// \return the minimum element + public: T Min() const + { + return std::min(std::min(std::min(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 9c7c67976..e8ab4e8e2 100644 --- a/src/Vector4_TEST.cc +++ b/src/Vector4_TEST.cc @@ -20,6 +20,7 @@ #include "ignition/math/Helpers.hh" #include "ignition/math/Matrix4.hh" #include "ignition/math/Vector4.hh" +#include "../include/ignition/math/Vector4.hh" using namespace ignition; @@ -156,6 +157,38 @@ TEST(Vector4dTest, NoException) EXPECT_DOUBLE_EQ(v[4], 4.0); } +///////////////////////////////////////////////// +TEST(Vector4dTest, Max) +{ + math::Vector4d vec1(0.1, 0.2, 0.3, 0.2); + math::Vector4d vec2(0.2, 0.3, 0.4, 0.3); + math::Vector4d vec3(0.1, 0.2, 0.3, 0.4); + + EXPECT_DOUBLE_EQ(vec1.Max(), 0.3); + + vec1.Max(vec2); + EXPECT_EQ(vec1, math::Vector4d(0.2, 0.3, 0.4, 0.3)); + + vec1.Max(vec3); + EXPECT_EQ(vec1, math::Vector4d(0.2, 0.3, 0.4, 0.4)); +} + +///////////////////////////////////////////////// +TEST(Vector4dTest, Min) +{ + math::Vector4d vec1(0.1, 0.2, 0.3, 0.4); + math::Vector4d vec2(0.2, 0.3, 0.4, 0.3); + math::Vector4d vec3(0.05, 0.1, 0.2, 0.2); + + EXPECT_DOUBLE_EQ(vec1.Min(), 0.1); + + vec1.Min(vec2); + EXPECT_EQ(vec1, math::Vector4d(0.1, 0.2, 0.3, 0.3)); + + vec1.Min(vec3); + EXPECT_EQ(vec1, math::Vector4d(0.05, 0.1, 0.2, 0.2)); +} + ///////////////////////////////////////////////// // Test Equal function with specified tolerance TEST(Vector2Test, EqualTolerance) From ffc3d828b2185fe79d0154ac15f8cc34e0c84130 Mon Sep 17 00:00:00 2001 From: Lucas Fernando Date: Mon, 13 Jul 2020 10:43:53 -0300 Subject: [PATCH 2/5] Include Vector4 in swig and limits library in RollingMean Signed-off-by: Lucas Fernando --- include/ignition/math/RollingMean.hh | 1 + src/CMakeLists.txt | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/include/ignition/math/RollingMean.hh b/include/ignition/math/RollingMean.hh index ff49dbdec..50aee00a1 100644 --- a/include/ignition/math/RollingMean.hh +++ b/include/ignition/math/RollingMean.hh @@ -18,6 +18,7 @@ #define IGNITION_MATH_ROLLINGMEAN_HH_ #include +#include #include #include diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 657d6ca2f..a6d2885eb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,7 +26,8 @@ if (SWIG_FOUND) GaussMarkovProcess Rand Vector2 - Vector3) + Vector3 + Vector4) endif() ################################################# From 8632a940aebdd8b136fcb598910afa4373d07fae Mon Sep 17 00:00:00 2001 From: Lucas Fernando Date: Tue, 14 Jul 2020 22:37:49 -0300 Subject: [PATCH 3/5] Changes to follow suggestions for conciseness Signed-off-by: Lucas Fernando --- include/ignition/math/Vector4.hh | 34 ++++++++++---------------------- 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/include/ignition/math/Vector4.hh b/include/ignition/math/Vector4.hh index 655b7ca6a..7091ce8fa 100644 --- a/include/ignition/math/Vector4.hh +++ b/include/ignition/math/Vector4.hh @@ -133,14 +133,10 @@ namespace ignition /// \param[in] _v the maximum clamping vector public: void Max(const Vector4 &_v) { - if (_v[0] > this->data[0]) - this->data[0] = _v[0]; - if (_v[1] > this->data[1]) - this->data[1] = _v[1]; - if (_v[2] > this->data[2]) - this->data[2] = _v[2]; - if (_v[3] > this->data[3]) - this->data[3] = _v[3]; + this->data[0] = std::max(_v[0], this->data[0]); + this->data[1] = std::max(_v[1], this->data[1]); + this->data[2] = std::max(_v[2], this->data[2]); + this->data[3] = std::max(_v[3], this->data[3]); } /// \brief Set this vector's components to the minimum of itself and the @@ -148,34 +144,24 @@ namespace ignition /// \param[in] _v the minimum clamping vector public: void Min(const Vector4 &_v) { - if (_v[0] < this->data[0]) - this->data[0] = _v[0]; - if (_v[1] < this->data[1]) - this->data[1] = _v[1]; - if (_v[2] < this->data[2]) - this->data[2] = _v[2]; - if (_v[3] < this->data[3]) - this->data[3] = _v[3]; + this->data[0] = std::min(_v[0], this->data[0]); + this->data[1] = std::min(_v[1], this->data[1]); + this->data[2] = std::min(_v[2], this->data[2]); + this->data[3] = std::min(_v[3], this->data[3]); } /// \brief Get the maximum value in the vector /// \return the maximum element public: T Max() const { - return std::max(std::max(std::max(this->data[0], - this->data[1]), - this->data[2]), - this->data[3]); + return *std::max_element(this->data, this->data+4); } /// \brief Get the minimum value in the vector /// \return the minimum element public: T Min() const { - return std::min(std::min(std::min(this->data[0], - this->data[1]), - this->data[2]), - this->data[3]); + return *std::min_element(this->data, this->data+4); } /// \brief Assignment operator From 7079d7e313d2bc7f434f7a51e695013cf54e23b0 Mon Sep 17 00:00:00 2001 From: Lucas Fernando Date: Tue, 14 Jul 2020 22:40:57 -0300 Subject: [PATCH 4/5] Resolve vestigal changes Signed-off-by: Lucas Fernando --- src/CMakeLists.txt | 3 +-- src/Vector4_TEST.cc | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a6d2885eb..657d6ca2f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,8 +26,7 @@ if (SWIG_FOUND) GaussMarkovProcess Rand Vector2 - Vector3 - Vector4) + Vector3) endif() ################################################# diff --git a/src/Vector4_TEST.cc b/src/Vector4_TEST.cc index e8ab4e8e2..b14cb8515 100644 --- a/src/Vector4_TEST.cc +++ b/src/Vector4_TEST.cc @@ -20,7 +20,6 @@ #include "ignition/math/Helpers.hh" #include "ignition/math/Matrix4.hh" #include "ignition/math/Vector4.hh" -#include "../include/ignition/math/Vector4.hh" using namespace ignition; From 5d39745e9ecd578d07d7a98eecde06a3bbaf577a Mon Sep 17 00:00:00 2001 From: Lucas Fernando Date: Tue, 14 Jul 2020 22:51:08 -0300 Subject: [PATCH 5/5] Remove Rolling Mean include Signed-off-by: Lucas Fernando --- include/ignition/math/RollingMean.hh | 1 - 1 file changed, 1 deletion(-) diff --git a/include/ignition/math/RollingMean.hh b/include/ignition/math/RollingMean.hh index 50aee00a1..ff49dbdec 100644 --- a/include/ignition/math/RollingMean.hh +++ b/include/ignition/math/RollingMean.hh @@ -18,7 +18,6 @@ #define IGNITION_MATH_ROLLINGMEAN_HH_ #include -#include #include #include