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. * 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) { 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) { 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> pointMinusA = point - a;
Vector3<T> pointMinusB = point - b; Vector3<T> pointMinusB = point - b;
T pointDistanceA = pointMinusA.lengthSquared(); T pointDistanceA = pointMinusA.dot();
T pointDistanceB = pointMinusB.lengthSquared(); T pointDistanceB = pointMinusB.dot();
T bDistanceA = (b - a).lengthSquared(); T bDistanceA = (b - a).dot();
/* Point is before A */ /* Point is before A */
if(pointDistanceB > bDistanceA + pointDistanceA) if(pointDistanceB > bDistanceA + pointDistanceA)
@ -110,7 +110,7 @@ class Distance {
return pointDistanceB; return pointDistanceB;
/* Between A and B */ /* 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::dot,
&VectorTest::multiplyDivide, &VectorTest::multiplyDivide,
&VectorTest::addSubstract, &VectorTest::addSubstract,
&VectorTest::dotSelf,
&VectorTest::length, &VectorTest::length,
&VectorTest::lengthSquared,
&VectorTest::normalized, &VectorTest::normalized,
&VectorTest::sum, &VectorTest::sum,
&VectorTest::product, &VectorTest::product,
@ -110,12 +110,12 @@ void VectorTest::addSubstract() {
CORRADE_COMPARE(expected - b, a); CORRADE_COMPARE(expected - b, a);
} }
void VectorTest::length() { void VectorTest::dotSelf() {
CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).length(), 5.4772256f); CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).dot(), 30.0f);
} }
void VectorTest::lengthSquared() { void VectorTest::length() {
CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).lengthSquared(), 30.0f); CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).length(), 5.4772256f);
} }
void VectorTest::normalized() { void VectorTest::normalized() {

2
src/Math/Test/VectorTest.h

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

25
src/Math/Vector.h

@ -70,6 +70,7 @@ template<size_t size, class T> class Vector {
* @f[ * @f[
* a \cdot b = \sum_{i=0}^{n-1} a_ib_i * a \cdot b = \sum_{i=0}^{n-1} a_ib_i
* @f] * @f]
* @see dot() const
*/ */
static T dot(const Vector<size, T>& a, const Vector<size, T>& b) { static T dot(const Vector<size, T>& a, const Vector<size, T>& b) {
T out(0); 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 { inline T dot() const {
return std::sqrt(dot(*this, *this)); return dot(*this, *this);
} }
/** /**
* @brief %Vector length squared * @brief %Vector length
* *
* More efficient than length() for comparing vector length with * @see dot() const
* 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$.
*/ */
inline T lengthSquared() const { inline T length() const {
return dot(*this, *this); return std::sqrt(dot());
} }
/** @brief Normalized vector (of length 1) */ /** @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 { bool Sphere::operator%(const Point& other) const {
return (other.transformedPosition()-transformedPosition()).lengthSquared() < return (other.transformedPosition()-transformedPosition()).dot() <
Math::pow<2>(transformedRadius()); Math::pow<2>(transformedRadius());
} }
@ -56,7 +56,7 @@ bool Sphere::operator%(const LineSegment& other) const {
} }
bool Sphere::operator%(const Sphere& 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()); Math::pow<2>(transformedRadius()+other.transformedRadius());
} }

Loading…
Cancel
Save