ray: extract make member function as free function

for consistency with other make_foo functions. to be replaced with
deduction guides in the future.
This commit is contained in:
Danny Robson 2017-08-22 15:46:49 +10:00
parent 8b089d412c
commit 2576061b26
3 changed files with 32 additions and 38 deletions

View File

@ -24,30 +24,6 @@
using util::geom::ray; using util::geom::ray;
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
ray<S,T>::ray (util::point<S,T> _origin,
util::vector<S,T> _direction):
origin (_origin),
direction (_direction)
{
CHECK (is_normalised (direction));
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
ray<S,T>
ray<S,T>::make (util::point<S,T> origin,
util::point<S,T> target)
{
return {
origin,
normalised (target - origin)
};
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/// returns the distance along the ray in a ray-plane intersection /// returns the distance along the ray in a ray-plane intersection
/// ///

View File

@ -28,13 +28,17 @@
namespace util::geom { namespace util::geom {
template <size_t S, typename T> template <size_t S, typename T>
struct ray { struct ray {
ray (point<S,T> origin, constexpr ray (point<S,T> _origin, vector<S,T> _direction) noexcept:
vector<S,T> direction); origin (_origin),
direction (_direction)
{ ; }
static constexpr ray (point<S,T> _origin, point <S,T> _distant) noexcept:
ray<S,T> make (point<S,T> origin, ray (_origin, _origin.to (_distant ))
point<S,T> target); { ; }
//---------------------------------------------------------------------
// intersection tests // 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;
@ -50,6 +54,20 @@ namespace util::geom {
vector<S,T> direction; vector<S,T> direction;
}; };
template <std::size_t S, typename T>
constexpr auto
make_ray (point<S,T> p, vector<S,T> d) noexcept
{
return ray<S,T> { p, d };
}
template <std::size_t S, typename T>
constexpr auto
make_ray (point<S,T> p, point<S,T> q)
{
return ray<S,T> { p, q };
};
typedef ray<2,float> ray2f; typedef ray<2,float> ray2f;
typedef ray<3,float> ray3f; typedef ray<3,float> ray3f;
} }

View File

@ -12,8 +12,8 @@ void
test_intersect_plane (util::TAP::logger &tap) test_intersect_plane (util::TAP::logger &tap)
{ {
// trivial case: origin ray facing z, plane at unit z facing -z. // trivial case: origin ray facing z, plane at unit z facing -z.
const util::geom::ray3f l ({0,0,0}, {0,0, 1}); const util::geom::ray3f l (util::point3f {0,0,0}, util::vector3f {0,0, 1});
const util::geom::plane3f p ({0,0,1}, {0,0,-1}); const util::geom::plane3f p (util::point3f {0,0,1}, util::vector3f {0,0,-1});
tap.expect_eq (l.intersect (p), 1.f, "ray-plane intersect"); tap.expect_eq (l.intersect (p), 1.f, "ray-plane intersect");
} }
@ -32,15 +32,15 @@ test_intersect_aabb (util::TAP::logger &tap)
}; };
const ray2f forward { const ray2f forward {
{ 0.5f, -0.5f }, util::point2f { 0.5f, -0.5f },
{ 0.f, 1.f } util::vector2f { 0.f, 1.f }
}; };
tap.expect_eq (forward.intersect (box), 0.5f, "ray-aabb intersect"); tap.expect_eq (forward.intersect (box), 0.5f, "ray-aabb intersect");
const ray2f behind { const ray2f behind {
{ 0.5f, 2.f }, util::point2f { 0.5f, 2.f },
{ 0.f, 1.f } util::vector2f { 0.f, 1.f }
}; };
tap.expect_nan (behind.intersect (box), "ray-aabb intersect behind"); tap.expect_nan (behind.intersect (box), "ray-aabb intersect behind");
@ -55,13 +55,13 @@ test_intersect_sphere (util::TAP::logger &tap)
const sphere3f s = {{0.f, 0.f, 0.f}, 1.f}; const sphere3f s = {{0.f, 0.f, 0.f}, 1.f};
const ray3f r0 {{0.f, 2.f, 0.f}, {0.f, -1.f, 0.f}}; const ray3f r0 {util::point3f {0.f, 2.f, 0.f}, util::vector3f {0.f, -1.f, 0.f}};
tap.expect_eq (r0.intersect (s), 1.f, "ray-sphere simple"); tap.expect_eq (r0.intersect (s), 1.f, "ray-sphere simple");
const ray3f r1 {{0.f, 1.f, 0.f}, {0.f, 1.f, 0.f}}; const ray3f r1 {util::point3f {0.f, 1.f, 0.f}, util::vector3f {0.f, 1.f, 0.f}};
tap.expect_eq (r1.intersect (s), 0.f, "ray-sphere adjacent"); tap.expect_eq (r1.intersect (s), 0.f, "ray-sphere adjacent");
const ray3f r2 {{0.f, 2.f, 0.f}, {0.f, 1.f, 0.f}}; const ray3f r2 {util::point3f {0.f, 2.f, 0.f}, util::vector3f {0.f, 1.f, 0.f}};
tap.expect_nan (r2.intersect (s), "ray-sphere no-intersect"); tap.expect_nan (r2.intersect (s), "ray-sphere no-intersect");
} }