Browse Source

Math::Geometry: cleaned up Distance, removed use of Matrix.

The same functionality is now provided in Vector2::cross() which is
also more meaningful.
pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
1f675cae25
  1. 50
      src/Math/Geometry/Distance.h

50
src/Math/Geometry/Distance.h

@ -29,7 +29,6 @@
*/
#include "Math/Functions.h"
#include "Math/Matrix.h"
#include "Math/Vector3.h"
namespace Magnum { namespace Math { namespace Geometry {
@ -46,14 +45,15 @@ class Distance {
* @param point Point
*
* The distance *d* is computed from point **p** and line defined by **a**
* and **b** using @ref Matrix::determinant() "determinant": @f[
* d = \frac{|det(b - a a - point)|} {|b - a|}
* and **b** using @ref Vector2::cross() "perp-dot product": @f[
* d = \frac{|(\boldsymbol b - \boldsymbol a)_\bot \cdot (\boldsymbol a - \boldsymbol p)|} {|\boldsymbol b - \boldsymbol a|}
* @f]
* Source: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
* @see linePointSquared(const Vector2&, const Vector2&, const Vector2&)
*/
template<class T> inline static T linePoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) {
return std::abs(Matrix<2, T>(b - a, a - point).determinant())/(b - a).length();
const Vector2<T> bMinusA = b - a;
return std::abs(Vector2<T>::cross(bMinusA, a - point))/bMinusA.length();
}
/**
@ -67,8 +67,8 @@ class Distance {
* compute the square root.
*/
template<class T> inline static T linePointSquared(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) {
Vector2<T> bMinusA = b - a;
return Math::pow<2>(Matrix<2, T>(bMinusA, a - point).determinant())/bMinusA.dot();
const Vector2<T> bMinusA = b - a;
return Math::pow<2>(Vector2<T>::cross(bMinusA, a - point))/bMinusA.dot();
}
/**
@ -127,12 +127,12 @@ class Distance {
* @see lineSegmentPointSquared()
*/
template<class T> inline static T lineSegmentPoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) {
Vector2<T> pointMinusA = point - a;
Vector2<T> pointMinusB = point - b;
Vector2<T> bMinusA = b - a;
T pointDistanceA = pointMinusA.dot();
T pointDistanceB = pointMinusB.dot();
T bDistanceA = bMinusA.dot();
const Vector2<T> pointMinusA = point - a;
const Vector2<T> pointMinusB = point - b;
const Vector2<T> bMinusA = b - a;
const T pointDistanceA = pointMinusA.dot();
const T pointDistanceB = pointMinusB.dot();
const T bDistanceA = bMinusA.dot();
/* Point is before A */
if(pointDistanceB > bDistanceA + pointDistanceA)
@ -143,7 +143,7 @@ class Distance {
return std::sqrt(pointDistanceB);
/* Between A and B */
return std::abs(Matrix<2, T>(bMinusA, -pointMinusA).determinant())/std::sqrt(bDistanceA);
return std::abs(Vector2<T>::cross(bMinusA, -pointMinusA))/std::sqrt(bDistanceA);
}
/**
@ -153,12 +153,12 @@ class Distance {
* other values, because it doesn't compute the square root.
*/
template<class T> static T lineSegmentPointSquared(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) {
Vector2<T> pointMinusA = point - a;
Vector2<T> pointMinusB = point - b;
Vector2<T> bMinusA = b - a;
T pointDistanceA = pointMinusA.dot();
T pointDistanceB = pointMinusB.dot();
T bDistanceA = bMinusA.dot();
const Vector2<T> pointMinusA = point - a;
const Vector2<T> pointMinusB = point - b;
const Vector2<T> bMinusA = b - a;
const T pointDistanceA = pointMinusA.dot();
const T pointDistanceB = pointMinusB.dot();
const T bDistanceA = bMinusA.dot();
/* Point is before A */
if(pointDistanceB > bDistanceA + pointDistanceA)
@ -169,7 +169,7 @@ class Distance {
return pointDistanceB;
/* Between A and B */
return Math::pow<2>(Matrix<2, T>(bMinusA, -pointMinusA).determinant())/bDistanceA;
return Math::pow<2>(Vector2<T>::cross(bMinusA, -pointMinusA))/bDistanceA;
}
/**
@ -194,11 +194,11 @@ class Distance {
* other values, because it doesn't compute the square root.
*/
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.dot();
T pointDistanceB = pointMinusB.dot();
T bDistanceA = (b - a).dot();
const Vector3<T> pointMinusA = point - a;
const Vector3<T> pointMinusB = point - b;
const T pointDistanceA = pointMinusA.dot();
const T pointDistanceB = pointMinusB.dot();
const T bDistanceA = (b - a).dot();
/* Point is before A */
if(pointDistanceB > bDistanceA + pointDistanceA)

Loading…
Cancel
Save