Browse Source

Math: converted Geometry::Distance and Intersection to namespaces.

No need for them to be classes, less indentation, less keywords and
boilerplate, more space for documentation, better `using` usage. Also
revised and fixed various issues in the documentation.
pull/190/head
Vladimír Vondruš 10 years ago
parent
commit
7f89105a51
  1. 16
      doc/namespaces.dox
  2. 284
      src/Magnum/Math/Geometry/Distance.h
  3. 197
      src/Magnum/Math/Geometry/Intersection.h

16
doc/namespaces.dox

@ -94,7 +94,21 @@ and @ref cmake for more information.
/** @namespace Magnum::Math::Geometry /** @namespace Magnum::Math::Geometry
@brief Geometry library @brief Geometry library
Functions for computing intersections, distances, areas and volumes. This library is built as part of Magnum by default. To use it, you need to
find `Magnum` package and link to `Magnum::Magnum` target. See @ref building
and @ref cmake for more information.
*/
/** @namespace Magnum::Math::Geometry::Distance
@brief Functions for calculating distances
This library is built as part of Magnum by default. To use it, you need to
find `Magnum` package and link to `Magnum::Magnum` target. See @ref building
and @ref cmake for more information.
*/
/** @namespace Magnum::Math::Geometry::Intersection
@brief Function for calculating intersections
This library is built as part of Magnum by default. To use it, you need to This library is built as part of Magnum by default. To use it, you need to
find `Magnum` package and link to `Magnum::Magnum` target. See @ref building find `Magnum` package and link to `Magnum::Magnum` target. See @ref building

284
src/Magnum/Math/Geometry/Distance.h

@ -27,202 +27,192 @@
*/ */
/** @file /** @file
* @brief Class @ref Magnum::Math::Geometry::Distance * @brief Namespace @ref Magnum::Math::Geometry::Distance
*/ */
#include "Magnum/Math/Functions.h" #include "Magnum/Math/Functions.h"
#include "Magnum/Math/Vector3.h" #include "Magnum/Math/Vector3.h"
#include "Magnum/Math/Vector4.h" #include "Magnum/Math/Vector4.h"
namespace Magnum { namespace Math { namespace Geometry { namespace Magnum { namespace Math { namespace Geometry { namespace Distance {
/** @brief Functions for computing distances */
class Distance {
public:
Distance() = delete;
/** /**
* @brief Distance of line and point in 2D @brief Distance of line and point in 2D, squared
* @param a First point of the line @param a First point of the line
* @param b Second point of the line @param b Second point of the line
* @param point Point @param point Point
*
* The distance *d* is computed from point **p** and line defined by **a** More efficient than @ref linePoint(const Vector2<T>&, const Vector2<T>&, const Vector2<T>&)
* and **b** using @ref cross(const Vector2<T>&, const Vector2<T>&) "perp-dot product": @f[ for comparing distance with other values, because it doesn't calculate the
* d = \frac{|(\boldsymbol b - \boldsymbol a)_\bot \cdot (\boldsymbol a - \boldsymbol p)|} {|\boldsymbol b - \boldsymbol a|} square root.
* @f]
* Source: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
* @see @ref linePointSquared(const Vector2<T>&, const Vector2<T>&, const Vector2<T>&)
*/ */
template<class T> static T linePoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) { template<class T> inline T linePointSquared(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) {
const Vector2<T> bMinusA = b - a; const Vector2<T> bMinusA = b - a;
return std::abs(cross(bMinusA, a - point))/bMinusA.length(); return Math::pow<2>(cross(bMinusA, a - point))/bMinusA.dot();
} }
/** /**
* @brief Distance of line and point in 2D, squared @brief Distance of line and point in 2D
* @param a First point of the line @param a First point of the line
* @param b Second point of the line @param b Second point of the line
* @param point Point @param point Point
*
* More efficient than The distance @f$ d @f$ is calculated from point @f$ \boldsymbol{p} @f$ and line
* @ref linePoint(const Vector2<T>&, const Vector2<T>&, const Vector2<T>&) defined by @f$ \boldsymbol{a} @f$ and @f$ \boldsymbol{b} @f$ using
* for comparing distance with other values, because it doesn't @ref cross(const Vector2<T>&, const Vector2<T>&) "perp-dot product": @f[
* compute the square root. 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 @ref linePointSquared(const Vector2<T>&, const Vector2<T>&, const Vector2<T>&)
*/ */
template<class T> static T linePointSquared(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) { template<class T> inline T linePoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) {
const Vector2<T> bMinusA = b - a; const Vector2<T> bMinusA = b - a;
return Math::pow<2>(cross(bMinusA, a - point))/bMinusA.dot(); return std::abs(cross(bMinusA, a - point))/bMinusA.length();
} }
/** /**
* @brief Distance of line and point in 3D @brief Distance of line and point in 3D, squared
* @param a First point of the line
* @param b Second point of the line More efficient than @ref linePoint(const Vector3<T>&, const Vector3<T>&, const Vector3<T>&)
* @param point Point for comparing distance with other values, because it doesn't calculate the
* square root.
* The distance *d* is computed from point **p** and line defined by **a**
* and **b** using @ref cross(const Vector3<T>&, const Vector3<T>&) "cross product": @f[
* d = \frac{|(\boldsymbol p - \boldsymbol a) \times (\boldsymbol p - \boldsymbol b)|}
* {|\boldsymbol b - \boldsymbol a|}
* @f]
* Source: http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
* @see @ref linePointSquared(const Vector3<T>&, const Vector3<T>&, const Vector3<T>&)
*/ */
template<class T> static T linePoint(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) { template<class T> inline T linePointSquared(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) {
return std::sqrt(linePointSquared(a, b, point)); return cross(point - a, point - b).dot()/(b - a).dot();
} }
/** /**
* @brief Distance of line and point in 3D, squared @brief Distance of line and point in 3D
* @param a First point of the line
* More efficient than @ref linePoint(const Vector3<T>&, const Vector3<T>&, const Vector3<T>&) @param b Second point of the line
* for comparing distance with other values, because it doesn't @param point Point
* compute the square root.
The distance @f$ d @f$ is calculated from point @f$ \boldsymbol{p} @f$ and line
defined by @f$ \boldsymbol{a} @f$ and @f$ \boldsymbol{b} @f$ using
@ref cross(const Vector3<T>&, const Vector3<T>&) "cross product": @f[
d = \frac{|(\boldsymbol p - \boldsymbol a) \times (\boldsymbol p - \boldsymbol b)|}{|\boldsymbol b - \boldsymbol a|}
@f]
Source: http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
@see @ref linePointSquared(const Vector3<T>&, const Vector3<T>&, const Vector3<T>&)
*/ */
template<class T> static T linePointSquared(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) { template<class T> inline T linePoint(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) {
return cross(point - a, point - b).dot()/(b - a).dot(); return std::sqrt(linePointSquared(a, b, point));
} }
/** /**
* @brief Dístance of point from line segment in 2D @brief Distance of point from line segment in 2D, squared
* @param a Starting point of the line
* @param b Ending point of the line More efficient than @ref lineSegmentPoint() for comparing distance with other
* @param point Point values, because it doesn't calculate the square root.
*
* Returns distance of point from line segment or from its
* starting/ending point, depending on where the point lies.
*
* Determining whether the point lies next to line segment or outside
* is done using Pythagorean theorem. If the following equation
* applies, the point **p** lies outside line segment closer to **a**: @f[
* |\boldsymbol p - \boldsymbol b|^2 > |\boldsymbol b - \boldsymbol a|^2 + |\boldsymbol p - \boldsymbol a|^2
* @f]
* On the other hand, if the following equation applies, the point
* lies outside line segment closer to **b**: @f[
* |\boldsymbol p - \boldsymbol a|^2 > |\boldsymbol b - \boldsymbol a|^2 + |\boldsymbol p - \boldsymbol b|^2
* @f]
* The last alternative is when the following equation applies. The
* point then lies between **a** and **b** and the distance is
* computed the same way as in @ref linePoint(). @f[
* |\boldsymbol b - \boldsymbol a|^2 > |\boldsymbol p - \boldsymbol a|^2 + |\boldsymbol p - \boldsymbol b|^2
* @f]
*
* @see @ref lineSegmentPointSquared()
*/ */
template<class T> static T lineSegmentPoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point); template<class T> T lineSegmentPointSquared(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point);
/** /**
* @brief Distance of point from line segment in 2D, squared @brief Dístance of point from line segment in 2D
* @param a Starting point of the line
* More efficient than @ref lineSegmentPoint() for comparing distance @param b Ending point of the line
* with other values, because it doesn't compute the square root. @param point Point
Returns distance of point from line segment or from its starting/ending point,
depending on where the point lies.
Determining whether the point lies next to line segment or outside is done
using Pythagorean theorem. If the following equation applies, the point
@f$ \boldsymbol{p} @f$ lies outside line segment closer to @f$ \boldsymbol{a} @f$: @f[
|\boldsymbol p - \boldsymbol b|^2 > |\boldsymbol b - \boldsymbol a|^2 + |\boldsymbol p - \boldsymbol a|^2
@f]
On the other hand, if the following equation applies, the point lies outside
line segment closer to @f$ \boldsymbol{b} @f$: @f[
|\boldsymbol p - \boldsymbol a|^2 > |\boldsymbol b - \boldsymbol a|^2 + |\boldsymbol p - \boldsymbol b|^2
@f]
The last alternative is when the following equation applies. The point then
lies between @f$ \boldsymbol{a} @f$ and @f$ \boldsymbol{b} @f$ and the distance
is calculated the same way as in @ref linePoint(). @f[
|\boldsymbol b - \boldsymbol a|^2 > |\boldsymbol p - \boldsymbol a|^2 + |\boldsymbol p - \boldsymbol b|^2
@f]
@see @ref lineSegmentPointSquared()
*/ */
template<class T> static T lineSegmentPointSquared(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point); template<class T> T lineSegmentPoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point);
/** /**
* @brief Dístance of point from line segment in 3D @brief Distance of point from line segment in 3D, squared
* @param a Starting point of the line
* @param b Ending point of the line More efficient than @ref lineSegmentPoint(const Vector3<T>&, const Vector3<T>&, const Vector3<T>&)
* @param point Point for comparing distance with other values, because it doesn't calculate the
* square root.
* Similar to 2D implementation
* @ref lineSegmentPoint(const Vector2<T>&, const Vector2<T>&, const Vector2<T>&).
*
* @see @ref lineSegmentPointSquared(const Vector3<T>&, const Vector3<T>&, const Vector3<T>&)
*/ */
template<class T> static T lineSegmentPoint(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) { template<class T> T lineSegmentPointSquared(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point);
return std::sqrt(lineSegmentPointSquared(a, b, point));
}
/** /**
* @brief Distance of point from line segment in 3D, squared @brief Dístance of point from line segment in 3D
* @param a Starting point of the line
* More efficient than @param b Ending point of the line
* @ref lineSegmentPoint(const Vector3<T>&, const Vector3<T>&, const Vector3<T>&) @param point Point
* for comparing distance with other values, because it doesn't compute
* the square root. Similar to 2D implementation @ref lineSegmentPoint(const Vector2<T>&, const Vector2<T>&, const Vector2<T>&).
@see @ref lineSegmentPointSquared(const Vector3<T>&, const Vector3<T>&, const Vector3<T>&)
*/ */
template<class T> static T lineSegmentPointSquared(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point); template<class T> inline T lineSegmentPoint(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) {
return std::sqrt(lineSegmentPointSquared(a, b, point));
}
/** /**
* @brief Distance of point from plane @brief Distance of point from plane, scaled by the length of the planes normal
*
* The distance **d** is computed from point **p** and plane with The distance @f$ d @f$ is calculated from point @f$ \boldsymbol{p} @f$ and
* normal **n** and **w** using: @f[ plane with normal @f$ \boldsymbol{n} @f$ and @f$ w @f$ using: @f[
* d = \frac{p \cdot n + w}{\left| n \right|} d = \boldsymbol{p} \cdot \boldsymbol{n} + w
* @f] @f]
* The distance is negative if the point lies behind the plane. The distance is negative if the point lies behind the plane.
*
* In cases where the planes normal is a unit vector, More efficient than @ref pointPlane() when merely the sign of the distance is
* @ref pointPlaneUnnormalized() is more efficient. If merely the sign of interest, for example when testing on which half space of the plane the
* of the distance is of interest, @ref pointPlaneScaled() is more point lies.
* efficient. @see @ref pointPlaneNormalized()
*/ */
template<class T> static T pointPlane(const Vector3<T>& point, const Vector4<T>& plane) { template<class T> inline T pointPlaneScaled(const Vector3<T>& point, const Vector4<T>& plane) {
return pointPlaneScaled<T>(point, plane)/plane.xyz().length(); return dot(plane.xyz(), point) + plane.w();
} }
/** /**
* @brief Distance of point from plane, scaled by the length of the planes normal @brief Distance of point from plane
*
* The distance **d** is computed from point **p** and plane with The distance @f$ d @f$ is calculated from point @f$ \boldsymbol{p} @f$ and
* normal **n** and **w** using: @f[ plane with normal @f$ \boldsymbol{n} @f$ and @f$ w @f$ using: @f[
* d = p \cdot n + w d = \frac{\boldsymbol{p} \cdot \boldsymbol{n} + w}{\left| \boldsymbol{n} \right|}
* @f] @f]
* The distance is negative if the point lies behind the plane. The distance is negative if the point lies behind the plane.
*
* More efficient than @ref pointPlane() when merely the sign of the In cases where the planes normal is a unit vector, @ref pointPlaneNormalized()
* distance is of interest, for example when testing on which half is more efficient. If merely the sign of the distance is of interest,
* space of the plane the point lies. @ref pointPlaneScaled() is more efficient.
* @see @ref pointPlaneNormalized()
*/ */
template<class T> static T pointPlaneScaled(const Vector3<T>& point, const Vector4<T>& plane) { template<class T> inline T pointPlane(const Vector3<T>& point, const Vector4<T>& plane) {
return Math::dot(plane.xyz(), point) + plane.w(); return pointPlaneScaled<T>(point, plane)/plane.xyz().length();
} }
/** /**
* @brief Distance of point from plane with normalized normal @brief Distance of point from plane with normalized normal
*
* The distance **d** is computed from point **p** and plane with The distance @f$ d @f$ is calculated from point @f$ \boldsymbol{p} @f$ and plane
* normal **n** and **w** using: @f[ with normal @f$ \boldsymbol{n} @f$ and @f$ w @f$ using: @f[
* d = p \cdot n + w d = \boldsymbol{p} \cdot \boldsymbol{n} + w
* @f] @f]
* The distance is negative if the point lies behind the plane. Expects The distance is negative if the point lies behind the plane. Expects that
* that @p plane normal is normalized. @p plane normal is normalized.
*
* More efficient than @ref pointPlane() in cases where the planes More efficient than @ref pointPlane() in cases where the plane's normal is
* normal is normalized. Equivalent to @ref pointPlaneScaled() but with normalized. Equivalent to @ref pointPlaneScaled() but with assertion added on
* assertion added on top. top.
*/ */
template<class T> static T pointPlaneNormalized(const Vector3<T>& point, const Vector4<T>& plane) { template<class T> inline T pointPlaneNormalized(const Vector3<T>& point, const Vector4<T>& plane) {
CORRADE_ASSERT(plane.xyz().isNormalized(), CORRADE_ASSERT(plane.xyz().isNormalized(),
"Math::Geometry::Distance::pointPlaneNormalized(): plane normal is not an unit vector", {}); "Math::Geometry::Distance::pointPlaneNormalized(): plane normal is not an unit vector", {});
return pointPlaneScaled<T>(point, plane); return pointPlaneScaled<T>(point, plane);
} }
};
template<class T> T Distance::lineSegmentPoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) { template<class T> T lineSegmentPoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) {
const Vector2<T> pointMinusA = point - a; const Vector2<T> pointMinusA = point - a;
const Vector2<T> pointMinusB = point - b; const Vector2<T> pointMinusB = point - b;
const Vector2<T> bMinusA = b - a; const Vector2<T> bMinusA = b - a;
@ -242,7 +232,7 @@ template<class T> T Distance::lineSegmentPoint(const Vector2<T>& a, const Vector
return std::abs(cross(bMinusA, -pointMinusA))/std::sqrt(bDistanceA); return std::abs(cross(bMinusA, -pointMinusA))/std::sqrt(bDistanceA);
} }
template<class T> T Distance::lineSegmentPointSquared(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) { template<class T> T lineSegmentPointSquared(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) {
const Vector2<T> pointMinusA = point - a; const Vector2<T> pointMinusA = point - a;
const Vector2<T> pointMinusB = point - b; const Vector2<T> pointMinusB = point - b;
const Vector2<T> bMinusA = b - a; const Vector2<T> bMinusA = b - a;
@ -262,7 +252,7 @@ template<class T> T Distance::lineSegmentPointSquared(const Vector2<T>& a, const
return Math::pow<2>(cross(bMinusA, -pointMinusA))/bDistanceA; return Math::pow<2>(cross(bMinusA, -pointMinusA))/bDistanceA;
} }
template<class T> T Distance::lineSegmentPointSquared(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) { template<class T> T lineSegmentPointSquared(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) {
const Vector3<T> pointMinusA = point - a; const Vector3<T> pointMinusA = point - a;
const Vector3<T> pointMinusB = point - b; const Vector3<T> pointMinusB = point - b;
const T pointDistanceA = pointMinusA.dot(); const T pointDistanceA = pointMinusA.dot();
@ -281,6 +271,6 @@ template<class T> T Distance::lineSegmentPointSquared(const Vector3<T>& a, const
return cross(pointMinusA, pointMinusB).dot()/bDistanceA; return cross(pointMinusA, pointMinusB).dot()/bDistanceA;
} }
}}} }}}}
#endif #endif

197
src/Magnum/Math/Geometry/Intersection.h

@ -27,7 +27,7 @@
*/ */
/** @file /** @file
* @brief Class @ref Magnum::Math::Geometry::Intersection * @brief Namespace @ref Magnum::Math::Geometry::Intersection
*/ */
#include "Magnum/Math/Frustum.h" #include "Magnum/Math/Frustum.h"
@ -35,126 +35,127 @@
#include "Magnum/Math/Range.h" #include "Magnum/Math/Range.h"
#include "Magnum/Math/Vector3.h" #include "Magnum/Math/Vector3.h"
namespace Magnum { namespace Math { namespace Geometry { namespace Magnum { namespace Math { namespace Geometry { namespace Intersection {
/** @brief Functions for computing intersections */
class Intersection {
public:
Intersection() = delete;
/** /**
* @brief Intersection of two line segments in 2D @brief Intersection of two line segments in 2D
* @param p Starting point of first line segment @param p Starting point of first line segment
* @param r Direction of first line segment @param r Direction of first line segment
* @param q Starting point of second line segment @param q Starting point of second line segment
* @param s Direction of second line segment @param s Direction of second line segment
* @return Intersection point positions `t`, `u` on both lines, NaN if
* the lines are collinear or infinity if they are parallel. Returns intersection point positions @f$ t @f$, @f$ u @f$ on both lines, NaN if
* Intersection point can be then computed with `p + t*r` or the lines are collinear or infinity if they are parallel. Intersection point
* `q + u*s`. If `t` is in range @f$ [ 0 ; 1 ] @f$, the can be then calculated with @f$ \boldsymbol{p} + t \boldsymbol{r} @f$ or
* intersection is inside the line segment defined by `p` and @f$ \boldsymbol{q} + u \boldsymbol{s} @f$. If @f$ t @f$ is in range
* `p + r`, if `u` is in range @f$ [ 0 ; 1 ] @f$, the intersection @f$ [ 0 ; 1 ] @f$, the intersection is inside the line segment defined by
* is inside the line segment defined by `q` and `q + s`. @f$ \boldsymbol{p} @f$ and @f$ \boldsymbol{p} + \boldsymbol{r} @f$, if @f$ u @f$
* is in range @f$ [ 0 ; 1 ] @f$, the intersection is inside the line segment
* The two lines intersect if **t** and **u** exist such that: @f[ defined by @f$ \boldsymbol{q} @f$ and @f$ \boldsymbol{q} + \boldsymbol{s} @f$.
* \boldsymbol p + t \boldsymbol r = \boldsymbol q + u \boldsymbol s
* @f] The two lines intersect if @f$ t @f$ and @f$ u @f$ exist such that: @f[
* Crossing both sides with **s**, distributing the cross product and \boldsymbol p + t \boldsymbol r = \boldsymbol q + u \boldsymbol s
* eliminating @f$ \boldsymbol s \times \boldsymbol s = 0 @f$, then @f]
* solving for **t** and similarly for **u**: @f[ Crossing both sides with @f$ \boldsymbol{s} @f$, distributing the cross product
* \begin{array}{rcl} and eliminating @f$ \boldsymbol s \times \boldsymbol s = 0 @f$, then solving
* (\boldsymbol p + t \boldsymbol r) \times s & = & (\boldsymbol q + u \boldsymbol s) \times s \\ for @f$ t @f$ and similarly for @f$ u @f$: @f[
* t (\boldsymbol r \times s) & = & (\boldsymbol q - \boldsymbol p) \times s \\ \begin{array}{rcl}
* t & = & \cfrac{(\boldsymbol q - \boldsymbol p) \times s}{\boldsymbol r \times \boldsymbol s} \\ (\boldsymbol p + t \boldsymbol r) \times s & = & (\boldsymbol q + u \boldsymbol s) \times s \\
* u & = & \cfrac{(\boldsymbol q - \boldsymbol p) \times r}{\boldsymbol r \times \boldsymbol s} t (\boldsymbol r \times s) & = & (\boldsymbol q - \boldsymbol p) \times s \\
* \end{array} t & = & \cfrac{(\boldsymbol q - \boldsymbol p) \times s}{\boldsymbol r \times \boldsymbol s} \\
* @f] u & = & \cfrac{(\boldsymbol q - \boldsymbol p) \times r}{\boldsymbol r \times \boldsymbol s}
* \end{array}
* See also @ref lineSegmentLine() which computes only **t**, which is @f]
* useful if you don't need to test that the intersection lies inside
* line segment defined by `q` and `q + s`. See also @ref lineSegmentLine() which calculates only @f$ t @f$, useful if you
don't need to test that the intersection lies inside line segment defined by
@f$ \boldsymbol{q} @f$ and @f$ \boldsymbol{q} + \boldsymbol{s} @f$.
*/ */
template<class T> static std::pair<T, T> lineSegmentLineSegment(const Vector2<T>& p, const Vector2<T>& r, const Vector2<T>& q, const Vector2<T>& s) { template<class T> inline std::pair<T, T> lineSegmentLineSegment(const Vector2<T>& p, const Vector2<T>& r, const Vector2<T>& q, const Vector2<T>& s) {
const Vector2<T> qp = q - p; const Vector2<T> qp = q - p;
const T rs = cross(r, s); const T rs = cross(r, s);
return {cross(qp, s)/rs, cross(qp, r)/rs}; return {cross(qp, s)/rs, cross(qp, r)/rs};
} }
/** /**
* @brief Intersection of line segment and line in 2D @brief Intersection of line segment and line in 2D
* @param p Starting point of first line segment @param p Starting point of first line segment
* @param r Direction of first line segment @param r Direction of first line segment
* @param q Starting point of second line @param q Starting point of second line
* @param s Direction of second line @param s Direction of second line
* @return Intersection point position `t` on first line, NaN if the
* lines are collinear or infinity if they are parallel. Returns intersection point position @f$ t @f$ on first line, NaN if the lines
* Intersection point can be then with `p + t*r`. If returned are collinear or infinity if they are parallel. Intersection point can be then
* value is in range @f$ [ 0 ; 1 ] @f$, the intersection is inside calculated with @f$ \boldsymbol{p} + t \boldsymbol{r} @f$. If returned value is
* the line segment defined by `p` and `p + r`. in range @f$ [ 0 ; 1 ] @f$, the intersection is inside the line segment defined
* by @f$ \boldsymbol{p} @f$ and @f$ \boldsymbol{p} + \boldsymbol{r} @f$.
* Unlike @ref lineSegmentLineSegment() computes only **t**.
Unlike @ref lineSegmentLineSegment() calculates only @f$ t @f$.
*/ */
template<class T> static T lineSegmentLine(const Vector2<T>& p, const Vector2<T>& r, const Vector2<T>& q, const Vector2<T>& s) { template<class T> inline T lineSegmentLine(const Vector2<T>& p, const Vector2<T>& r, const Vector2<T>& q, const Vector2<T>& s) {
return cross(q - p, s)/cross(r, s); return cross(q - p, s)/cross(r, s);
} }
/** /**
* @brief Intersection of a plane and line @brief Intersection of a plane and line
* @param planePosition Plane position @param planePosition Plane position
* @param planeNormal Plane normal @param planeNormal Plane normal
* @param p Starting point of the line @param p Starting point of the line
* @param r Direction of the line @param r Direction of the line
* @return Intersection point position `t` on the line, NaN if the
* line lies on the plane or infinity if the intersection doesn't Returns intersection point position @f$ t @f$ on the line, NaN if the line lies
* exist. Intersection point can be then computed from with on the plane or infinity if the intersection doesn't exist. Intersection point
* `p + t*r`. If returned value is in range @f$ [ 0 ; 1 ] @f$, the can be then calculated from with @f$ \boldsymbol{p} + t \boldsymbol{r} @f$. If
* intersection is inside the line segment defined by `p` and `r`. returned value is in range @f$ [ 0 ; 1 ] @f$, the intersection is inside the
* line segment defined by @f$ \boldsymbol{p} @f$ and @f$ \boldsymbol{r} @f$.
* First the parameter *f* of parametric equation of the plane
* is computed from plane normal **n** and plane position: @f[ First the parameter @f$ f @f$ of parametric equation of the plane is calculated
* \begin{pmatrix} n_0 \\ n_1 \\ n_2 \end{pmatrix} \cdot from plane normal @f$ \boldsymbol{n} @f$ and plane position: @f[
* \begin{pmatrix} x \\ y \\ z \end{pmatrix} - f = 0 \begin{pmatrix} n_0 \\ n_1 \\ n_2 \end{pmatrix} \cdot
* @f] \begin{pmatrix} x \\ y \\ z \end{pmatrix} - f = 0
* Using plane normal **n**, parameter *f* and line defined by **p** @f]
* and **r**, value of *t* is computed and returned. @f[ Using plane normal @f$ \boldsymbol{n} @f$, parameter @f$ f @f$ and line defined
* \begin{array}{rcl} by @f$ \boldsymbol{p} @f$ and @f$ \boldsymbol{r} @f$, value of @f$ t @f$ is
* f & = & \boldsymbol n \cdot (\boldsymbol p + t \boldsymbol r) \\ calculated and returned. @f[
* \Rightarrow t & = & \cfrac{f - \boldsymbol n \cdot \boldsymbol p}{\boldsymbol n \cdot \boldsymbol r} \begin{array}{rcl}
* \end{array} f & = & \boldsymbol n \cdot (\boldsymbol p + t \boldsymbol r) \\
* @f] \Rightarrow t & = & \cfrac{f - \boldsymbol n \cdot \boldsymbol p}{\boldsymbol n \cdot \boldsymbol r}
\end{array}
@f]
*/ */
template<class T> static T planeLine(const Vector3<T>& planePosition, const Vector3<T>& planeNormal, const Vector3<T>& p, const Vector3<T>& r) { template<class T> inline T planeLine(const Vector3<T>& planePosition, const Vector3<T>& planeNormal, const Vector3<T>& p, const Vector3<T>& r) {
const T f = dot(planePosition, planeNormal); const T f = dot(planePosition, planeNormal);
return (f - dot(planeNormal, p))/dot(planeNormal, r); return (f - dot(planeNormal, p))/dot(planeNormal, r);
} }
/** /**
* @brief Intersection of a point and a camera frustum @brief Intersection of a point and a camera frustum
* @param point Point @param point Point
* @param frustum Frustum planes with normals pointing outwards @param frustum Frustum planes with normals pointing outwards
* @return `true` if the point is on or inside the frustum.
* Returns `true` if the point is on or inside the frustum.
* Checks for each plane of the frustum whether the point is behind the
* plane (the points distance from the plane is negative) using Checks for each plane of the frustum whether the point is behind the plane (the
* @ref Distance::pointPlaneScaled(). points distance from the plane is negative) using @ref Distance::pointPlaneScaled().
*/ */
template<class T> static bool pointFrustum(const Vector3<T>& point, const Frustum<T>& frustum); template<class T> bool pointFrustum(const Vector3<T>& point, const Frustum<T>& frustum);
/** /**
* @brief Intersection of a range and a camera frustum @brief Intersection of an axis-aligned box and a camera frustum
* @return `true` if the box intersects with the camera frustum @param box Axis-aligned box
* @param frustum Frustum planes with normals pointing outwards
* Counts for each plane of the frustum how many points of the box lie
* in front of the plane (outside of the frustum). If none, the box Returns `true` if the box intersects with the camera frustum.
* must lie entirely outside of the frustum and there is no
* intersection. Else, the box is considered as intersecting, even if Counts for each plane of the frustum how many points of the box lie in front of
* it is merely corners of the box overlapping with corners of the the plane (outside of the frustum). If none, the box must lie entirely outside
* frustum, since checking the corners is less efficient. of the frustum and there is no intersection. Else, the box is considered as
intersecting, even if it is merely corners of the box overlapping with corners
of the frustum, since checking the corners is less efficient.
*/ */
template<class T> static bool boxFrustum(const Range3D<T>& box, const Frustum<T>& frustum); template<class T> bool boxFrustum(const Range3D<T>& box, const Frustum<T>& frustum);
};
template<class T> bool Intersection::pointFrustum(const Vector3<T>& point, const Frustum<T>& frustum) { template<class T> bool pointFrustum(const Vector3<T>& point, const Frustum<T>& frustum) {
for(const Vector4<T>& plane: frustum.planes()) { for(const Vector4<T>& plane: frustum.planes()) {
/* The point is in front of one of the frustum planes (normals point /* The point is in front of one of the frustum planes (normals point
outwards) */ outwards) */
@ -165,7 +166,7 @@ template<class T> bool Intersection::pointFrustum(const Vector3<T>& point, const
return true; return true;
} }
template<class T> bool Intersection::boxFrustum(const Range3D<T>& box, const Frustum<T>& frustum) { template<class T> bool boxFrustum(const Range3D<T>& box, const Frustum<T>& frustum) {
for(const Vector4<T>& plane: frustum.planes()) { for(const Vector4<T>& plane: frustum.planes()) {
bool cornerHit = 0; bool cornerHit = 0;
@ -187,6 +188,6 @@ template<class T> bool Intersection::boxFrustum(const Range3D<T>& box, const Fru
return true; return true;
} }
}}} }}}}
#endif #endif

Loading…
Cancel
Save