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

Sum normalized functions #140

Merged
merged 6 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions include/ignition/math/Vector4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ namespace ignition
}
}

/// \brief Return a normalized vector
/// \return unit length vector
public: Vector4 Normalized() const
{
Vector4<T> result = *this;
result.Normalize();
return result;
}

/// \brief Return the dot product of this vector and another vector
/// \param[in] _v the vector
/// \return the dot product
Expand Down Expand Up @@ -201,6 +210,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
Expand Down
15 changes: 15 additions & 0 deletions src/Vector4_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ 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));
Expand Down Expand Up @@ -210,6 +215,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)
{
Expand Down