ray: give data members proper names

This commit is contained in:
Danny Robson 2015-04-15 14:25:35 +10:00
parent 13abad0d43
commit fb97584ad6
2 changed files with 23 additions and 20 deletions

32
ray.cpp
View File

@ -19,18 +19,18 @@
#include "debug.hpp" #include "debug.hpp"
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
util::ray<S,T>::ray (util::point<S,T> _p, util::ray<S,T>::ray (util::point<S,T> _origin,
util::vector<S,T> _d): util::vector<S,T> _direction):
p (_p), origin (_origin),
d (_d) direction (_direction)
{ {
CHECK_EQ (d.magnitude2 (), 1); CHECK (direction.is_normalised ());
} }
///---------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
/// returns the distance along the ray in a ray-plane intersection /// returns the distance along the ray in a ray-plane intersection
/// ///
/// returns inf if parallel /// returns inf if parallel
@ -39,7 +39,7 @@ template <size_t S, typename T>
T T
util::ray<S,T>::intersect (plane<S,T> q) const util::ray<S,T>::intersect (plane<S,T> q) const
{ {
return dot (q.p - p, q.n) / dot (d, q.n); return dot (q.p - origin, q.n) / dot (direction, q.n);
} }
@ -52,8 +52,8 @@ template <size_t S, typename T>
T T
util::ray<S,T>::intersect (AABB<S,T> r) const util::ray<S,T>::intersect (AABB<S,T> r) const
{ {
auto t1 = (r.p0 - p) / d; auto t1 = (r.p0 - origin) / direction;
auto t2 = (r.p1 - p) / d; auto t2 = (r.p1 - origin) / direction;
auto vmin = min (t1, t2); auto vmin = min (t1, t2);
auto vmax = max (t1, t2); auto vmax = max (t1, t2);
@ -82,8 +82,8 @@ template <size_t S, typename T>
T T
util::ray<S,T>::intersect (sphere<S,T> s) const util::ray<S,T>::intersect (sphere<S,T> s) const
{ {
T b = dot (d, p - s.c); T b = dot (direction, origin - s.c);
T c = dot (p - s.c, p - s.c) - s.r * s.r; T c = dot (origin - s.c, origin - s.c) - s.r * s.r;
T D = b * b - c; T D = b * b - c;
if (D < 0) if (D < 0)
@ -99,14 +99,14 @@ util::ray<S,T>::intersect (sphere<S,T> s) const
} }
///---------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
/// returns the closest parameter along the ray to a given point /// returns the closest parameter along the ray to a given point
template <size_t S, typename T> template <size_t S, typename T>
T T
util::ray<S,T>::closest (point<S,T> q) const util::ray<S,T>::closest (point<S,T> q) const
{ {
// project the origin-point difference onto the direction // project the origin-point difference onto the direction
return dot (p - q, d); return dot (origin - q, direction);
} }
@ -115,10 +115,10 @@ template <size_t S, typename T>
util::point<S,T> util::point<S,T>
util::ray<S,T>::at (T t) const util::ray<S,T>::at (T t) const
{ {
return p + d * t; return origin + direction * t;
} }
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
template struct util::ray<2,float>; template struct util::ray<2,float>;
template struct util::ray<3,float>; template struct util::ray<3,float>;

11
ray.hpp
View File

@ -27,19 +27,22 @@
namespace util { namespace util {
template <size_t S, typename T> template <size_t S, typename T>
struct ray { struct ray {
ray (util::point<S,T> origin, ray (point<S,T> origin,
util::vector<S,T> direction); vector<S,T> direction);
// intersection tests
T intersect (plane<S,T>) const; T intersect (plane<S,T>) const;
T intersect (AABB<S,T>) const; T intersect (AABB<S,T>) const;
T intersect (sphere<S,T>) const; T intersect (sphere<S,T>) const;
// queries
T closest (point<S,T>) const; T closest (point<S,T>) const;
util::point<S,T> at (T) const; util::point<S,T> at (T) const;
util::point<S,T> p; // data members
util::vector<S,T> d; point<S,T> origin;
vector<S,T> direction;
}; };