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:
parent
8b089d412c
commit
2576061b26
24
geom/ray.cpp
24
geom/ray.cpp
@ -24,30 +24,6 @@
|
||||
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
|
||||
///
|
||||
|
28
geom/ray.hpp
28
geom/ray.hpp
@ -28,13 +28,17 @@
|
||||
namespace util::geom {
|
||||
template <size_t S, typename T>
|
||||
struct ray {
|
||||
ray (point<S,T> origin,
|
||||
vector<S,T> direction);
|
||||
constexpr ray (point<S,T> _origin, vector<S,T> _direction) noexcept:
|
||||
origin (_origin),
|
||||
direction (_direction)
|
||||
{ ; }
|
||||
|
||||
static
|
||||
ray<S,T> make (point<S,T> origin,
|
||||
point<S,T> target);
|
||||
constexpr ray (point<S,T> _origin, point <S,T> _distant) noexcept:
|
||||
ray (_origin, _origin.to (_distant ))
|
||||
{ ; }
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// intersection tests
|
||||
T intersect (plane<S,T>) const;
|
||||
T intersect (AABB<S,T>) const;
|
||||
@ -50,6 +54,20 @@ namespace util::geom {
|
||||
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<3,float> ray3f;
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ void
|
||||
test_intersect_plane (util::TAP::logger &tap)
|
||||
{
|
||||
// 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::plane3f p ({0,0,1}, {0,0,-1});
|
||||
const util::geom::ray3f l (util::point3f {0,0,0}, util::vector3f {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");
|
||||
}
|
||||
@ -32,15 +32,15 @@ test_intersect_aabb (util::TAP::logger &tap)
|
||||
};
|
||||
|
||||
const ray2f forward {
|
||||
{ 0.5f, -0.5f },
|
||||
{ 0.f, 1.f }
|
||||
util::point2f { 0.5f, -0.5f },
|
||||
util::vector2f { 0.f, 1.f }
|
||||
};
|
||||
|
||||
tap.expect_eq (forward.intersect (box), 0.5f, "ray-aabb intersect");
|
||||
|
||||
const ray2f behind {
|
||||
{ 0.5f, 2.f },
|
||||
{ 0.f, 1.f }
|
||||
util::point2f { 0.5f, 2.f },
|
||||
util::vector2f { 0.f, 1.f }
|
||||
};
|
||||
|
||||
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 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");
|
||||
|
||||
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");
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user