geom/plane: use a vector for coefficients
this simplifies usage of the coefficients in other routines
This commit is contained in:
parent
563ffaf894
commit
47efd293c9
@ -24,11 +24,12 @@ using util::geom::plane;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
plane<S,T>::plane (point<S,T> _p, vector<S,T> _n):
|
||||
p (_p),
|
||||
n (_n)
|
||||
plane<S,T>::plane (point<S,T> base, vector<S,T> normal)
|
||||
{
|
||||
CHECK (is_normalised (n));
|
||||
CHECK (is_normalised (normal));
|
||||
|
||||
std::copy (std::begin (normal), std::end (normal), std::begin (coefficients));
|
||||
coefficients[S] = dot (base, normal);
|
||||
}
|
||||
|
||||
|
||||
|
@ -19,17 +19,49 @@
|
||||
|
||||
#include "../point.hpp"
|
||||
#include "../vector.hpp"
|
||||
#include "../matrix.hpp"
|
||||
|
||||
namespace util::geom {
|
||||
/// represents an S dimensional plane in parametric form
|
||||
template <size_t S, typename T>
|
||||
struct plane {
|
||||
plane (util::point<S,T>, util::vector<S,T>);
|
||||
plane () = default;
|
||||
plane (util::point<S,T> base, util::vector<S,T> normal);
|
||||
|
||||
::util::point<S,T> p;
|
||||
::util::vector<S,T> n;
|
||||
explicit plane (util::vector<S+1,T> _coefficients):
|
||||
coefficients (_coefficients)
|
||||
{ ; }
|
||||
|
||||
util::vector<S+1,T> coefficients;
|
||||
};
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/// returns the normal for a plane
|
||||
template <size_t S, typename T>
|
||||
util::vector<S,T>
|
||||
normal (plane<S,T> p)
|
||||
{
|
||||
return p.coefficients.template redim<S> ();
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/// normalises a plane's parametric form
|
||||
///
|
||||
/// useful only after manually modifying the coefficients (as in default
|
||||
/// construction and piecewise initialisation). a plane will otherwise be
|
||||
/// assumed to be in normal form.
|
||||
template <size_t S, typename T>
|
||||
plane<S,T>
|
||||
normalised (plane<S,T> p)
|
||||
{
|
||||
const auto mag = norm (normal (p));
|
||||
CHECK_NEZ (mag);
|
||||
return plane<S,T> (p.coefficients / mag);
|
||||
}
|
||||
|
||||
|
||||
typedef plane<2,float> plane2f;
|
||||
typedef plane<3,float> plane3f;
|
||||
}
|
||||
|
@ -33,7 +33,8 @@ template <size_t S, typename T>
|
||||
T
|
||||
ray<S,T>::intersect (plane<S,T> q) const
|
||||
{
|
||||
return dot (q.p - origin, q.n) / dot (direction, q.n);
|
||||
return dot (q.coefficients, origin. template redim<S+1> (1)) /
|
||||
dot (q.coefficients, direction.template redim<S+1> (0));
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user