Browse Source

Renamed Vector::lengthSquared() to parameterless Vector::dot().

It's now less confusing ("length squared" looks like it's even heavier
than length), but on the other hand it's not so obvious that these two
functions come together.
pull/279/head
Vladimír Vondruš 14 years ago
parent
commit
0a0860a9f2
  1. 10
      src/Math/Geometry/Distance.h
  2. 10
      src/Math/Test/VectorTest.cpp
  3. 2
      src/Math/Test/VectorTest.h
  4. 25
      src/Math/Vector.h
  5. 4
      src/Physics/Sphere.cpp

10
src/Math/Geometry/Distance.h

@ -52,7 +52,7 @@ class Distance {
* values, because it doesn't compute the square root.
*/
template<class T> static T linePointSquared(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) {
return Vector3<T>::cross(point - a, point - b).lengthSquared()/(b - a).lengthSquared();
return Vector3<T>::cross(point - a, point - b).dot()/(b - a).dot();
}
/**
@ -97,9 +97,9 @@ class Distance {
template<class T> static T lineSegmentPointSquared(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) {
Vector3<T> pointMinusA = point - a;
Vector3<T> pointMinusB = point - b;
T pointDistanceA = pointMinusA.lengthSquared();
T pointDistanceB = pointMinusB.lengthSquared();
T bDistanceA = (b - a).lengthSquared();
T pointDistanceA = pointMinusA.dot();
T pointDistanceB = pointMinusB.dot();
T bDistanceA = (b - a).dot();
/* Point is before A */
if(pointDistanceB > bDistanceA + pointDistanceA)
@ -110,7 +110,7 @@ class Distance {
return pointDistanceB;
/* Between A and B */
return Vector3<T>::cross(pointMinusA, pointMinusB).lengthSquared()/bDistanceA;
return Vector3<T>::cross(pointMinusA, pointMinusB).dot()/bDistanceA;
}
};

10
src/Math/Test/VectorTest.cpp

@ -37,8 +37,8 @@ VectorTest::VectorTest() {
&VectorTest::dot,
&VectorTest::multiplyDivide,
&VectorTest::addSubstract,
&VectorTest::dotSelf,
&VectorTest::length,
&VectorTest::lengthSquared,
&VectorTest::normalized,
&VectorTest::sum,
&VectorTest::product,
@ -110,12 +110,12 @@ void VectorTest::addSubstract() {
CORRADE_COMPARE(expected - b, a);
}
void VectorTest::length() {
CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).length(), 5.4772256f);
void VectorTest::dotSelf() {
CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).dot(), 30.0f);
}
void VectorTest::lengthSquared() {
CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).lengthSquared(), 30.0f);
void VectorTest::length() {
CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).length(), 5.4772256f);
}
void VectorTest::normalized() {

2
src/Math/Test/VectorTest.h

@ -29,8 +29,8 @@ class VectorTest: public Corrade::TestSuite::Tester<VectorTest> {
void dot();
void multiplyDivide();
void addSubstract();
void dotSelf();
void length();
void lengthSquared();
void normalized();
void sum();
void product();

25
src/Math/Vector.h

@ -70,6 +70,7 @@ template<size_t size, class T> class Vector {
* @f[
* a \cdot b = \sum_{i=0}^{n-1} a_ib_i
* @f]
* @see dot() const
*/
static T dot(const Vector<size, T>& a, const Vector<size, T>& b) {
T out(0);
@ -243,24 +244,26 @@ template<size_t size, class T> class Vector {
}
/**
* @brief %Vector length
* @brief Dot product of the vector
*
* @see lengthSquared()
* Should be used instead of length() for comparing vector length with
* other values, because it doesn't compute the square root, just the
* dot product: @f$ a \cdot a < length \cdot length @f$ is faster
* than @f$ \sqrt{a \cdot a} < length @f$.
*
* @see dot(const Vector<size, T>&, const Vector<size, T>&)
*/
inline T length() const {
return std::sqrt(dot(*this, *this));
inline T dot() const {
return dot(*this, *this);
}
/**
* @brief %Vector length squared
* @brief %Vector length
*
* More efficient than length() for comparing vector length with
* other values, because it doesn't compute the square root, just the
* dot product: @f$ a \cdot a < length \cdot length @f$ is faster
* than @f$ \sqrt{a \cdot a} < length @f$.
* @see dot() const
*/
inline T lengthSquared() const {
return dot(*this, *this);
inline T length() const {
return std::sqrt(dot());
}
/** @brief Normalized vector (of length 1) */

4
src/Physics/Sphere.cpp

@ -41,7 +41,7 @@ bool Sphere::collides(const AbstractShape* other) const {
}
bool Sphere::operator%(const Point& other) const {
return (other.transformedPosition()-transformedPosition()).lengthSquared() <
return (other.transformedPosition()-transformedPosition()).dot() <
Math::pow<2>(transformedRadius());
}
@ -56,7 +56,7 @@ bool Sphere::operator%(const LineSegment& other) const {
}
bool Sphere::operator%(const Sphere& other) const {
return (other.transformedPosition()-transformedPosition()).lengthSquared() <
return (other.transformedPosition()-transformedPosition()).dot() <
Math::pow<2>(transformedRadius()+other.transformedRadius());
}

Loading…
Cancel
Save