diff --git a/src/Math/Geometry/Distance.h b/src/Math/Geometry/Distance.h index e3e710f03..143ab8784 100644 --- a/src/Math/Geometry/Distance.h +++ b/src/Math/Geometry/Distance.h @@ -52,7 +52,7 @@ class Distance { * values, because it doesn't compute the square root. */ template static T linePointSquared(const Vector3& a, const Vector3& b, const Vector3& point) { - return Vector3::cross(point - a, point - b).lengthSquared()/(b - a).lengthSquared(); + return Vector3::cross(point - a, point - b).dot()/(b - a).dot(); } /** @@ -97,9 +97,9 @@ class Distance { template static T lineSegmentPointSquared(const Vector3& a, const Vector3& b, const Vector3& point) { Vector3 pointMinusA = point - a; Vector3 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::cross(pointMinusA, pointMinusB).lengthSquared()/bDistanceA; + return Vector3::cross(pointMinusA, pointMinusB).dot()/bDistanceA; } }; diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index 2d409ceed..459084f44 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/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() { diff --git a/src/Math/Test/VectorTest.h b/src/Math/Test/VectorTest.h index a3795d551..e00481d59 100644 --- a/src/Math/Test/VectorTest.h +++ b/src/Math/Test/VectorTest.h @@ -29,8 +29,8 @@ class VectorTest: public Corrade::TestSuite::Tester { void dot(); void multiplyDivide(); void addSubstract(); + void dotSelf(); void length(); - void lengthSquared(); void normalized(); void sum(); void product(); diff --git a/src/Math/Vector.h b/src/Math/Vector.h index fb3232947..5e089d81f 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -70,6 +70,7 @@ template class Vector { * @f[ * a \cdot b = \sum_{i=0}^{n-1} a_ib_i * @f] + * @see dot() const */ static T dot(const Vector& a, const Vector& b) { T out(0); @@ -243,24 +244,26 @@ template 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&, const Vector&) */ - 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) */ diff --git a/src/Physics/Sphere.cpp b/src/Physics/Sphere.cpp index ac9ed700d..334982b07 100644 --- a/src/Physics/Sphere.cpp +++ b/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()); }