diff --git a/geom/ray.cpp b/geom/ray.cpp index 41fa0d76..3650ad3f 100644 --- a/geom/ray.cpp +++ b/geom/ray.cpp @@ -24,30 +24,6 @@ using util::geom::ray; -/////////////////////////////////////////////////////////////////////////////// -template -ray::ray (util::point _origin, - util::vector _direction): - origin (_origin), - direction (_direction) -{ - CHECK (is_normalised (direction)); -} - - -//----------------------------------------------------------------------------- -template -ray -ray::make (util::point origin, - util::point target) -{ - return { - origin, - normalised (target - origin) - }; -} - - /////////////////////////////////////////////////////////////////////////////// /// returns the distance along the ray in a ray-plane intersection /// diff --git a/geom/ray.hpp b/geom/ray.hpp index 2f63abed..b3381281 100644 --- a/geom/ray.hpp +++ b/geom/ray.hpp @@ -28,13 +28,17 @@ namespace util::geom { template struct ray { - ray (point origin, - vector direction); + constexpr ray (point _origin, vector _direction) noexcept: + origin (_origin), + direction (_direction) + { ; } - static - ray make (point origin, - point target); + constexpr ray (point _origin, point _distant) noexcept: + ray (_origin, _origin.to (_distant )) + { ; } + + //--------------------------------------------------------------------- // intersection tests T intersect (plane) const; T intersect (AABB) const; @@ -50,6 +54,20 @@ namespace util::geom { vector direction; }; + template + constexpr auto + make_ray (point p, vector d) noexcept + { + return ray { p, d }; + } + + template + constexpr auto + make_ray (point p, point q) + { + return ray { p, q }; + }; + typedef ray<2,float> ray2f; typedef ray<3,float> ray3f; } diff --git a/test/geom/ray.cpp b/test/geom/ray.cpp index 45d989e4..2cef97e1 100644 --- a/test/geom/ray.cpp +++ b/test/geom/ray.cpp @@ -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"); }