Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector3 to vector4 functions #132

Merged
merged 8 commits into from
Jul 20, 2020
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, but with std::min

{
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