diff --git a/src/Math/Geometry/Distance.h b/src/Math/Geometry/Distance.h new file mode 100644 index 000000000..3abb9b78f --- /dev/null +++ b/src/Math/Geometry/Distance.h @@ -0,0 +1,61 @@ +#ifndef Magnum_Math_Geometry_Distance_h +#define Magnum_Math_Geometry_Distance_h +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +/** @file + * @brief Class Magnum::Math::Geometry::Distance + */ + +#include "Math/Vector3.h" + +namespace Magnum { namespace Math { namespace Geometry { + +/** @brief Functions for computing distances */ +class Distance { + public: + /** + * @brief %Distance of line and point + * @param a First point of the line + * @param b Second point of the line + * @param point Point + * + * The distance *d* is computed from point **p** and line defined by **a** + * and **b** using @ref Vector3::cross() "cross product": + * @f[ + * d = \frac{|(\boldsymbol p - \boldsymbol a) \times (\boldsymbol p - \boldsymbol b)|} + * {|\boldsymbol b - \boldsymbol a|} + * @f] + * + * @see linePointSquared() + */ + template inline static T linePoint(const Vector3& a, const Vector3& b, const Vector3& point) { + return sqrt(linePointSquared(a, b, point)); + } + + /** + * @brief %Distance of line and point, squared + * + * More efficient than linePoint() for comparing distance with other + * 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(); + } +}; + +}}} + +#endif diff --git a/src/Math/Geometry/Test/CMakeLists.txt b/src/Math/Geometry/Test/CMakeLists.txt index 1cff67c30..ae56b3a13 100644 --- a/src/Math/Geometry/Test/CMakeLists.txt +++ b/src/Math/Geometry/Test/CMakeLists.txt @@ -1 +1,2 @@ +corrade_add_test(MathGeometryDistanceTest DistanceTest.h DistanceTest.cpp) corrade_add_test(MathGeometryIntersectionTest IntersectionTest.h IntersectionTest.cpp) diff --git a/src/Math/Geometry/Test/DistanceTest.cpp b/src/Math/Geometry/Test/DistanceTest.cpp new file mode 100644 index 000000000..6311aa0d2 --- /dev/null +++ b/src/Math/Geometry/Test/DistanceTest.cpp @@ -0,0 +1,46 @@ +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include "DistanceTest.h" + +#include +#include + +#include "Math.h" +#include "Distance.h" + +QTEST_APPLESS_MAIN(Magnum::Math::Geometry::Test::DistanceTest) + +using namespace std; + +namespace Magnum { namespace Math { namespace Geometry { namespace Test { + +typedef Magnum::Math::Vector3 Vector3; + +void DistanceTest::linePoint() { + Vector3 a(0.0f); + Vector3 b(1.0f); + + /* Point on the line */ + QCOMPARE((Distance::linePoint(a, b, Vector3(0.25f))), 0.0f); + + /* The distance should be the same for all equidistant points */ + QCOMPARE((Distance::linePoint(a, b, Vector3(1.0f, 0.0f, 1.0f))), + Constants::Sqrt2/Constants::Sqrt3); + QCOMPARE((Distance::linePoint(a, b, Vector3(1.0f, 0.0f, 1.0f)+Vector3(100.0f))), + Constants::Sqrt2/Constants::Sqrt3); +} + +}}}} diff --git a/src/Math/Geometry/Test/DistanceTest.h b/src/Math/Geometry/Test/DistanceTest.h new file mode 100644 index 000000000..13809007f --- /dev/null +++ b/src/Math/Geometry/Test/DistanceTest.h @@ -0,0 +1,31 @@ +#ifndef Magnum_Math_Geometry_Test_DistanceTest_h +#define Magnum_Math_Geometry_Test_DistanceTest_h +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include + +namespace Magnum { namespace Math { namespace Geometry { namespace Test { + +class DistanceTest: public QObject { + Q_OBJECT + + private slots: + void linePoint(); +}; + +}}}} + +#endif