diff --git a/include/ignition/math/Vector2.hh b/include/ignition/math/Vector2.hh index c7d7bb199..74621d945 100644 --- a/include/ignition/math/Vector2.hh +++ b/include/ignition/math/Vector2.hh @@ -17,6 +17,8 @@ #ifndef IGNITION_MATH_VECTOR2_HH_ #define IGNITION_MATH_VECTOR2_HH_ +#include + #include #include @@ -167,6 +169,38 @@ namespace ignition this->data[1] = 0; } + /// \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 Vector2 &_v) + { + this->data[0] = std::max(_v[0], this->data[0]); + this->data[1] = std::max(_v[1], this->data[1]); + } + + /// \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 Vector2 &_v) + { + this->data[0] = std::min(_v[0], this->data[0]); + this->data[1] = std::min(_v[1], this->data[1]); + } + + /// \brief Get the maximum value in the vector + /// \return the maximum element + public: T Max() const + { + return std::max(this->data[0], this->data[1]); + } + + /// \brief Get the minimum value in the vector + /// \return the minimum element + public: T Min() const + { + return std::min(this->data[0], this->data[1]); + } + /// \brief Assignment operator /// \param[in] _v a value for x and y element /// \return this diff --git a/src/Vector2.i b/src/Vector2.i index 589c3f155..af1393f39 100644 --- a/src/Vector2.i +++ b/src/Vector2.i @@ -46,6 +46,10 @@ namespace ignition public: void Normalize(); public: void Set(T _x, T _y); public: T Dot(const Vector2 &_v) const; + public: void Max(const Vector2 &_v); + public: void Min(const Vector2 &_v); + public: T Max() const; + public: T Min() const; public: Vector2 operator+(const Vector2 &_v) const; public: inline Vector2 operator+(const T _s) const; public: inline Vector2 operator-() const; diff --git a/src/Vector2_TEST.cc b/src/Vector2_TEST.cc index f6957bfc8..b5cd52694 100644 --- a/src/Vector2_TEST.cc +++ b/src/Vector2_TEST.cc @@ -160,6 +160,40 @@ TEST(Vector2Test, TestNormalized) EXPECT_EQ(vec3, math::Vector2d(0.447213, 0.894427)); } +///////////////////////////////////////////////// +TEST(Vector2Test, Max) +{ + math::Vector2d vec1(0.1, 0.2); + math::Vector2d vec2(0.3, 0.5); + math::Vector2d vec3(0.4, 0.2); + + EXPECT_DOUBLE_EQ(vec1.Max(), 0.2); + EXPECT_DOUBLE_EQ(vec3.Max(), 0.4); + + vec1.Max(vec2); + EXPECT_EQ(vec1, math::Vector2d(0.3, 0.5)); + + vec1.Max(vec3); + EXPECT_EQ(vec1, math::Vector2d(0.4, 0.5)); +} + +///////////////////////////////////////////////// +TEST(Vector2Test, Min) +{ + math::Vector2d vec1(0.3, 0.5); + math::Vector2d vec2(0.1, 0.2); + math::Vector2d vec3(0.05, 0.1); + + EXPECT_DOUBLE_EQ(vec1.Min(), 0.3); + EXPECT_DOUBLE_EQ(vec3.Min(), 0.05); + + vec1.Min(vec2); + EXPECT_EQ(vec1, math::Vector2d(0.1, 0.2)); + + vec1.Min(vec3); + EXPECT_EQ(vec1, math::Vector2d(0.05, 0.1)); +} + ///////////////////////////////////////////////// TEST(Vector2Test, NoException) { diff --git a/src/Vector2_TEST.rb b/src/Vector2_TEST.rb index 58bbd8ee5..a53b32d18 100644 --- a/src/Vector2_TEST.rb +++ b/src/Vector2_TEST.rb @@ -108,6 +108,40 @@ def test_equal_tolerance "Zero should equal 1 with 1.1 tolerance") end + def test_max + vec1 = Ignition::Math::Vector2d.new(0.1, 0.2) + vec2 = Ignition::Math::Vector2d.new(0.3, 0.5) + vec3 = Ignition::Math::Vector2d.new(0.4, 0.2) + + assert((vec1.Max() - 0.2).abs() < 1e-10, + "Vector2 vec1.Max should equal 0.2") + + vec1.Max(vec2) + assert(vec1 == Ignition::Math::Vector2d.new(0.3, 0.5), + "Vector2 vec1 should equal [0.3, 0.5]") + + vec1.Max(vec3) + assert(vec1 == Ignition::Math::Vector2d.new(0.4, 0.5), + "Vector2 vec1 should equal [0.4, 0.5]") + end + + def test_min + vec1 = Ignition::Math::Vector2d.new(0.3, 0.5) + vec2 = Ignition::Math::Vector2d.new(0.1, 0.2) + vec3 = Ignition::Math::Vector2d.new(0.05, 0.1) + + assert((vec1.Min() - 0.3).abs() < 1e-10, + "Vector2 vec1.Min should equal 0.3") + + vec1.Min(vec2) + assert(vec1 == Ignition::Math::Vector2d.new(0.1, 0.2), + "Vector2 vec1 should equal [0.1, 0.2]") + + vec1.Max(vec3) + assert(vec1 == Ignition::Math::Vector2d.new(0.05, 0.1), + "Vector2 vec1 should equal [0.05, 0.1]") + end + def test_dot v = Ignition::Math::Vector2d.new(1, 2)