libcruft-util/geom/ray.hpp

137 lines
3.8 KiB
C++

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_GEOM_RAY_HPP
#define __UTIL_GEOM_RAY_HPP
#include "aabb.hpp"
#include "plane.hpp"
#include "../vector.hpp"
#include "../point.hpp"
#include <iosfwd>
///////////////////////////////////////////////////////////////////////////////
namespace util::geom {
template <size_t S, typename T>
struct ray {
// queries
T closest (point<S,T>) const;
util::point<S,T> at (T) const;
// data members
point<S,T> origin;
vector<S,T> direction;
};
template <size_t S, typename T>
ray (point<S,T>,vector<S,T>) -> ray<S,T>;
///////////////////////////////////////////////////////////////////////////
typedef ray<2,float> ray2f;
typedef ray<3,float> ray3f;
///////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
ray<S,T>
operator* (matrix<S+1,S+1,T> lhs, ray<S,T> 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 <size_t S, typename T>
constexpr T
distance (const ray<S,T> r, const plane<S,T> p)
{
CHECK_SANITY (r);
return -dot (p.coefficients, r.origin. template redim<S+1> (1)) /
dot (p.coefficients, r.direction.template redim<S+1> (0));
}
//-------------------------------------------------------------------------
template <size_t S, typename T>
constexpr bool
intersects (const ray<S,T> r, const plane<S,T> p)
{
const auto d = distance (r, p);
return d >= 0 && std::isfinite (d);
}
///////////////////////////////////////////////////////////////////////////
// returns the distance along a ray to an aabb
//
// the return value may be negative if the plane lies behind the ray.
// the return value may be infinity in the case the ray and plane are parallel
template <size_t S, typename T>
constexpr T
distance (const ray<S,T> r, const aabb<S,T> 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<T>::infinity ();
// closest is behind us
if (tmin < 0)
return tmax;
// closest is in front of us
return tmin;
}
//-------------------------------------------------------------------------
// convenience method to test a ray intersects an aabb
template <size_t S, typename T>
constexpr bool
intersects (const ray<S,T> r, const aabb<S,T> b)
{
return distance (r, b) >= 0;
}
///////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
std::ostream&
operator<< (std::ostream&, ray<S,T>);
}
#endif