Skip to content

Commit

Permalink
Add Vector4 functions to match Vector3 (#132)
Browse files Browse the repository at this point in the history
This implements the functions Min(), Min(Vector4), Max() and Max(Vector4) in Vector4 API, similar to the existing same functions in Vector3.

Signed-off-by: Lucas Fernando <[email protected]>
  • Loading branch information
luccosta authored Jul 20, 2020
1 parent 11864fd commit 0270a51
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
38 changes: 38 additions & 0 deletions include/ignition/math/Vector4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef IGNITION_MATH_VECTOR4_HH_
#define IGNITION_MATH_VECTOR4_HH_

#include <algorithm>

#include <ignition/math/Matrix4.hh>
#include <ignition/math/Helpers.hh>
#include <ignition/math/config.hh>
Expand Down Expand Up @@ -126,6 +128,42 @@ 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<T> &_v)
{
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
/// passed in vector
/// \param[in] _v the minimum clamping vector
public: void Min(const Vector4<T> &_v)
{
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_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_element(this->data, this->data+4);
}

/// \brief Assignment operator
/// \param[in] _v the vector
/// \return a reference to this vector
Expand Down
32 changes: 32 additions & 0 deletions src/Vector4_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,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)
Expand Down

0 comments on commit 0270a51

Please sign in to comment.