/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright 2015-2018 Danny Robson */ #ifndef __UTIL_GEOM_RAY_HPP #define __UTIL_GEOM_RAY_HPP #include "aabb.hpp" #include "plane.hpp" #include "../vector.hpp" #include "../point.hpp" #include /////////////////////////////////////////////////////////////////////////////// namespace util::geom { template struct ray { // queries T closest (point) const; util::point at (T) const; // data members point origin; vector direction; }; template ray (point,vector) -> ray; /////////////////////////////////////////////////////////////////////////// typedef ray<2,float> ray2f; typedef ray<3,float> ray3f; /////////////////////////////////////////////////////////////////////////// template ray operator* (matrix lhs, ray rhs) { return { lhs * rhs.origin, normalised (lhs * rhs.direction) }; } /////////////////////////////////////////////////////////////////////////// /// returns the distance along the ray in a ray-plane intersection /// /// returns inf if parallel /// returns 0 if coplanar template constexpr T distance (const ray r, const plane p) { CHECK_SANITY (r); return -dot (p.coefficients, r.origin. template redim (1)) / dot (p.coefficients, r.direction.template redim (0)); } //------------------------------------------------------------------------- template constexpr bool intersects (const ray r, const plane p) { const auto d = distance (r, p); return d >= 0 && std::isfinite (d); } /////////////////////////////////////////////////////////////////////////// // returns the distance along a ray to an aabb // // the distance will never be negative // // the origin point of the ray does not provoke an intersection, so the // returned distances _should_ always be strictly positive. // // if a forward intersection cannot be found then 'infinity' is returned // instead. template constexpr T distance (const ray r, const aabb b) { CHECK_SANITY (r); const auto t1 = (b.lo - r.origin) / r.direction; const auto t2 = (b.hi - r.origin) / r.direction; const auto tmin = max (min (t1, t2)); const auto tmax = min (max (t1, t2)); // did not intersect if (tmin >= tmax) return std::numeric_limits::infinity (); // closest is behind us if (tmin <= 0) { if (tmax <= 0) { return std::numeric_limits::infinity (); } else { return tmax; } } // closest is in front of us return tmin; } //------------------------------------------------------------------------- // convenience method to test a ray intersects an aabb template constexpr bool intersects (const ray r, const aabb b) { return distance (r, b) >= 0; } /////////////////////////////////////////////////////////////////////////// template std::ostream& operator<< (std::ostream&, ray); } #endif