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

Add Abs, Dot and AbsDot and respective tests to Vector4 #135

Merged
merged 3 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions include/ignition/math/Vector4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,43 @@ namespace ignition
}
}

/// \brief Return the dot product of this vector and another vector
/// \param[in] _v the vector
/// \return the dot product
public: T Dot(const Vector4<T> &_v) const
{
return this->data[0] * _v[0] +
this->data[1] * _v[1] +
this->data[2] * _v[2] +
this->data[3] * _v[3];
}

/// \brief Return the absolute dot product of this vector and
/// another vector. This is similar to the Dot function, except the
/// absolute value of each component of the vector is used.
///
/// result = abs(x1 * x2) + abs(y1 * y2) + abs(z1 * z2) + abs(w1 * w2)
///
/// \param[in] _v the vector
/// \return The absolute dot product
public: T AbsDot(const Vector4<T> &_v) const
{
return std::abs(this->data[0] * _v[0]) +
std::abs(this->data[1] * _v[1]) +
std::abs(this->data[2] * _v[2]) +
std::abs(this->data[3] * _v[3]);
}

/// \brief Get the absolute value of the vector
/// \return a vector with positive elements
public: Vector4 Abs() const
{
return Vector4(std::abs(this->data[0]),
std::abs(this->data[1]),
std::abs(this->data[2]),
std::abs(this->data[3]));
}

/// \brief Set the contents of the vector
/// \param[in] _x value along x axis
/// \param[in] _y value along y axis
Expand Down
10 changes: 10 additions & 0 deletions src/Vector4_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ TEST(Vector4dTest, Vector4d)
v.Set(2, 4, 6, 8);
EXPECT_EQ(v, math::Vector4d(2, 4, 6, 8));

// ::DotProd
EXPECT_TRUE(math::equal(60.0, v.Dot(math::Vector4d(1, 2, 3, 4)), 1e-2));

// ::AbsDotProd
v1.Set(-1, -2, -3, -4);
EXPECT_TRUE(math::equal(60.0, v.AbsDot(v1), 1e-2));

// ::GetAbs
EXPECT_TRUE(v1.Abs() == math::Vector4d(1, 2, 3, 4));
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved

// ::operator= vector4
v = v1;
EXPECT_EQ(v, v1);
Expand Down