Browse Source

Vector dot product is now static function instead of operator*().

operator* is used in GLSL for element-wise multiplication, be compatible
with that.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
ea39f3735e
  1. 4
      src/Math/GeometryUtils.h
  2. 2
      src/Math/Test/VectorTest.cpp
  3. 24
      src/Math/Vector.h
  4. 3
      src/Math/Vector2.h
  5. 3
      src/Math/Vector3.h
  6. 3
      src/Math/Vector4.h

4
src/Math/GeometryUtils.h

@ -69,10 +69,10 @@ template<class T> class GeometryUtils {
/* Compute f with cross product and one of the points defining the
plane */
T f = crossProduct*plane[0];
T f = Vector3<T>::dot(crossProduct, plane[0]);
/* Compute t */
return (f-crossProduct*a)/(crossProduct*(b-a));
return (f-Vector3<T>::dot(crossProduct, a)/Vector3<T>::dot(crossProduct, b-a));
}
};

2
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() {

24
src/Math/Vector.h

@ -53,9 +53,19 @@ template<class T, size_t size> class Vector {
return *reinterpret_cast<const Vector<T, size>*>(data);
}
/** @brief Dot product */
static T dot(const Vector<T, size>& a, const Vector<T, size>& 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<T, size>& a, const Vector<T, size>& 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 T, size_t size> class Vector {
return !operator==(other);
}
/** @brief Dot product */
T operator*(const Vector<T, size>& 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<T, size> operator*(T number) const {
return Vector<T, size>(*this)*=number;
@ -205,7 +205,7 @@ template<class T, size_t size> class Vector {
/** @brief %Vector length */
inline T length() const {
return sqrt(operator*(*this));
return sqrt(dot(*this, *this));
}
/** @brief Normalized vector (of length 1) */

3
src/Math/Vector2.h

@ -58,9 +58,6 @@ template<class T> class Vector2: public Vector<T, 2> {
/** @copydoc Vector::operator=() */
inline Vector2<T>& operator=(const Vector<T, 2>& other) { return Vector<T, 2>::operator=(other); }
/** @copydoc Vector::operator*(const Vector<T, size>&) const */
inline T operator*(const Vector<T, 2>& other) const { return Vector<T, 2>::operator*(other); }
/** @copydoc Vector::operator*(T) const */
inline Vector2<T> operator*(T number) const { return Vector<T, 2>::operator*(number); }

3
src/Math/Vector3.h

@ -92,9 +92,6 @@ template<class T> class Vector3: public Vector<T, 3> {
/** @copydoc Vector::operator=() */
inline Vector3<T>& operator=(const Vector<T, 3>& other) { return Vector<T, 3>::operator=(other); }
/** @copydoc Vector::operator*(const Vector<T, size>&) const */
inline T operator*(const Vector<T, 3>& other) const { return Vector<T, 3>::operator*(other); }
/** @copydoc Vector::operator*(T) const */
inline Vector3<T> operator*(T number) const { return Vector<T, 3>::operator*(number); }

3
src/Math/Vector4.h

@ -100,9 +100,6 @@ template<class T> class Vector4: public Vector<T, 4> {
/** @copydoc Vector::operator=() */
inline Vector4<T>& operator=(const Vector<T, 4>& other) { return Vector<T, 4>::operator=(other); }
/** @copydoc Vector::operator*(const Vector<T, size>&) const */
inline T operator*(const Vector<T, 4>& other) const { return Vector<T, 4>::operator*(other); }
/** @copydoc Vector::operator*(T) const */
inline Vector4<T> operator*(T number) const { return Vector<T, 4>::operator*(number); }

Loading…
Cancel
Save