Skip to content

Commit

Permalink
Sum normalized functions (gazebosim#140)
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Fernando <[email protected]>
Co-authored-by: Michael Carroll <[email protected]>
  • Loading branch information
luccosta and mjcarroll authored Aug 1, 2020
1 parent 8a73032 commit 68f1418
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
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

0 comments on commit 68f1418

Please sign in to comment.