From ea39f3735ee14447fe6df71cb90e098c771e8923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 16 Apr 2012 23:51:17 +0200 Subject: [PATCH] Vector dot product is now static function instead of operator*(). operator* is used in GLSL for element-wise multiplication, be compatible with that. --- src/Math/GeometryUtils.h | 4 ++-- src/Math/Test/VectorTest.cpp | 2 +- src/Math/Vector.h | 24 ++++++++++++------------ src/Math/Vector2.h | 3 --- src/Math/Vector3.h | 3 --- src/Math/Vector4.h | 3 --- 6 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/Math/GeometryUtils.h b/src/Math/GeometryUtils.h index 4a5ffb94a..eecf9b77d 100644 --- a/src/Math/GeometryUtils.h +++ b/src/Math/GeometryUtils.h @@ -69,10 +69,10 @@ template class GeometryUtils { /* Compute f with cross product and one of the points defining the plane */ - T f = crossProduct*plane[0]; + T f = Vector3::dot(crossProduct, plane[0]); /* Compute t */ - return (f-crossProduct*a)/(crossProduct*(b-a)); + return (f-Vector3::dot(crossProduct, a)/Vector3::dot(crossProduct, b-a)); } }; diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index 59820d698..38f13c919 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -69,7 +69,7 @@ void VectorTest::copy() { } void VectorTest::dot() { - QVERIFY(Vector4(1.0f, 0.5f, 0.75f, 1.5f)*Vector4(2.0f, 4.0f, 1.0f, 7.0f) == 15.25f); + QVERIFY(Vector4::dot({1.0f, 0.5f, 0.75f, 1.5f}, {2.0f, 4.0f, 1.0f, 7.0f}) == 15.25f); } void VectorTest::multiplyDivide() { diff --git a/src/Math/Vector.h b/src/Math/Vector.h index b8673a058..addf06f59 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -53,9 +53,19 @@ template class Vector { return *reinterpret_cast*>(data); } + /** @brief Dot product */ + static T dot(const Vector& a, const Vector& b) { + T out(0); + + for(size_t i = 0; i != size; ++i) + out += a[i]*b[i]; + + return out; + } + /** @brief Angle between vectors */ inline static T angle(const Vector& a, const Vector& b) { - return acos((a*b)/(a.length()*b.length())); + return acos(dot(a, b)/(a.length()*b.length())); } /** @brief Default constructor */ @@ -111,16 +121,6 @@ template class Vector { return !operator==(other); } - /** @brief Dot product */ - T operator*(const Vector& other) const { - T out(0); - - for(size_t i = 0; i != size; ++i) - out += (*this)[i]*other[i]; - - return out; - } - /** @brief Multiply vector */ inline Vector operator*(T number) const { return Vector(*this)*=number; @@ -205,7 +205,7 @@ template class Vector { /** @brief %Vector length */ inline T length() const { - return sqrt(operator*(*this)); + return sqrt(dot(*this, *this)); } /** @brief Normalized vector (of length 1) */ diff --git a/src/Math/Vector2.h b/src/Math/Vector2.h index de7dc7b04..e30f4b307 100644 --- a/src/Math/Vector2.h +++ b/src/Math/Vector2.h @@ -58,9 +58,6 @@ template class Vector2: public Vector { /** @copydoc Vector::operator=() */ inline Vector2& operator=(const Vector& other) { return Vector::operator=(other); } - /** @copydoc Vector::operator*(const Vector&) const */ - inline T operator*(const Vector& other) const { return Vector::operator*(other); } - /** @copydoc Vector::operator*(T) const */ inline Vector2 operator*(T number) const { return Vector::operator*(number); } diff --git a/src/Math/Vector3.h b/src/Math/Vector3.h index 9926fa8ba..7b7631548 100644 --- a/src/Math/Vector3.h +++ b/src/Math/Vector3.h @@ -92,9 +92,6 @@ template class Vector3: public Vector { /** @copydoc Vector::operator=() */ inline Vector3& operator=(const Vector& other) { return Vector::operator=(other); } - /** @copydoc Vector::operator*(const Vector&) const */ - inline T operator*(const Vector& other) const { return Vector::operator*(other); } - /** @copydoc Vector::operator*(T) const */ inline Vector3 operator*(T number) const { return Vector::operator*(number); } diff --git a/src/Math/Vector4.h b/src/Math/Vector4.h index 2b4505539..ee02dbf2e 100644 --- a/src/Math/Vector4.h +++ b/src/Math/Vector4.h @@ -100,9 +100,6 @@ template class Vector4: public Vector { /** @copydoc Vector::operator=() */ inline Vector4& operator=(const Vector& other) { return Vector::operator=(other); } - /** @copydoc Vector::operator*(const Vector&) const */ - inline T operator*(const Vector& other) const { return Vector::operator*(other); } - /** @copydoc Vector::operator*(T) const */ inline Vector4 operator*(T number) const { return Vector::operator*(number); }