libcruft-util/cruft/util/geom/ray.hpp

137 lines
3.7 KiB
C++
Raw Normal View History

2015-02-19 13:28:56 +11:00
/*
2018-08-04 15:14:06 +10:00
* 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/.
2015-02-19 13:28:56 +11:00
*
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
2015-02-19 13:28:56 +11:00
*/
#pragma once
2015-02-19 13:28:56 +11:00
2015-03-07 03:19:48 +11:00
#include "aabb.hpp"
2015-04-13 21:47:51 +10:00
#include "plane.hpp"
2015-10-14 15:32:53 +11:00
#include "../vector.hpp"
#include "../point.hpp"
#include "../matrix.hpp"
2015-02-19 13:28:56 +11:00
#include <iosfwd>
2017-01-05 15:06:49 +11:00
///////////////////////////////////////////////////////////////////////////////
namespace cruft::geom {
2015-02-19 13:28:56 +11:00
template <size_t S, typename T>
2015-03-11 22:31:35 +11:00
struct ray {
2015-04-15 14:25:35 +10:00
// queries
T closest (point<S,T>) const;
2015-02-19 13:28:56 +11:00
cruft::point<S,T> at (T) const;
2015-02-19 13:28:56 +11:00
2015-04-15 14:25:35 +10:00
// data members
point<S,T> origin;
vector<S,T> direction;
2015-02-19 13:28:56 +11:00
};
2018-04-13 18:47:07 +10:00
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;
2018-04-16 14:36:39 +10:00
///////////////////////////////////////////////////////////////////////////
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)
};
}
2018-04-16 14:36:39 +10:00
///////////////////////////////////////////////////////////////////////////
/// 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 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 <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) {
if (tmax <= 0) {
return std::numeric_limits<T>::infinity ();
} else {
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>);
2017-01-05 15:06:49 +11:00
}