diff --git a/include/ignition/math/Vector4.hh b/include/ignition/math/Vector4.hh index 7091ce8fa..8223f5745 100644 --- a/include/ignition/math/Vector4.hh +++ b/include/ignition/math/Vector4.hh @@ -115,6 +115,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 &_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 &_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 diff --git a/src/Vector4_TEST.cc b/src/Vector4_TEST.cc index b14cb8515..e988d356f 100644 --- a/src/Vector4_TEST.cc +++ b/src/Vector4_TEST.cc @@ -61,6 +61,17 @@ 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)); + EXPECT_TRUE(v.Abs() == math::Vector4d(2, 4, 6, 8)); + // ::operator= vector4 v = v1; EXPECT_EQ(v, v1);