2015-02-19 13:28:56 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* 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
|
2015-02-19 13:28:56 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-02-19 13:28:56 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* 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.
|
2015-02-19 13:28:56 +11:00
|
|
|
*
|
|
|
|
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2015-10-13 18:19:47 +11:00
|
|
|
#ifndef __UTIL_GEOM_RAY_HPP
|
|
|
|
#define __UTIL_GEOM_RAY_HPP
|
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"
|
|
|
|
#include "sphere.hpp"
|
2015-10-14 15:32:53 +11:00
|
|
|
#include "../vector.hpp"
|
|
|
|
#include "../point.hpp"
|
2015-02-19 13:28:56 +11:00
|
|
|
|
2017-01-05 15:06:49 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace util::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 {
|
2017-08-22 17:23:36 +10:00
|
|
|
constexpr ray () = default;
|
|
|
|
|
2017-08-22 15:46:49 +10:00
|
|
|
constexpr ray (point<S,T> _origin, vector<S,T> _direction) noexcept:
|
|
|
|
origin (_origin),
|
|
|
|
direction (_direction)
|
2017-08-24 12:27:18 +10:00
|
|
|
{
|
|
|
|
CHECK (is_normalised (direction));
|
|
|
|
}
|
2015-02-19 13:28:56 +11:00
|
|
|
|
2017-08-22 15:46:49 +10:00
|
|
|
constexpr ray (point<S,T> _origin, point <S,T> _distant) noexcept:
|
|
|
|
ray (_origin, _origin.to (_distant ))
|
2017-08-24 12:27:18 +10:00
|
|
|
{
|
|
|
|
CHECK (is_normalised (direction));
|
|
|
|
}
|
2015-04-15 18:59:28 +10:00
|
|
|
|
2017-08-22 15:46:49 +10:00
|
|
|
|
2015-04-15 14:25:35 +10:00
|
|
|
// queries
|
2015-02-19 14:20:09 +11:00
|
|
|
T closest (point<S,T>) const;
|
2015-02-19 13:28:56 +11:00
|
|
|
|
|
|
|
util::point<S,T> at (T) const;
|
|
|
|
|
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-03-13 23:27:37 +11: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)
|
2017-08-22 15:46:49 +10:00
|
|
|
{
|
2018-03-13 23:27:37 +11:00
|
|
|
return dot (p.coefficients, r.origin. template redim<S+1> (1)) /
|
|
|
|
dot (p.coefficients, r.direction.template redim<S+1> (0));
|
|
|
|
|
2017-08-22 15:46:49 +10:00
|
|
|
}
|
|
|
|
|
2018-03-13 23:27:37 +11:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <size_t S, typename T>
|
|
|
|
constexpr bool
|
|
|
|
intersects (const ray<S,T> r, const plane<S,T> p)
|
2017-08-22 15:46:49 +10:00
|
|
|
{
|
2018-03-13 23:27:37 +11:00
|
|
|
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.
|
2018-03-27 16:06:34 +11:00
|
|
|
// the return value may be infinity in the case the ray and plane are parallel
|
2018-03-13 23:27:37 +11:00
|
|
|
template <size_t S, typename T>
|
|
|
|
constexpr T
|
|
|
|
distance (const ray<S,T> r, const aabb<S,T> b)
|
|
|
|
{
|
|
|
|
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)
|
2018-03-27 16:06:34 +11:00
|
|
|
return std::numeric_limits<T>::infinity ();
|
2018-03-13 23:27:37 +11:00
|
|
|
|
|
|
|
// closest is behind us
|
|
|
|
if (tmax < 0)
|
|
|
|
return tmax;
|
|
|
|
|
|
|
|
// closest is in front of us
|
|
|
|
return tmin;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// returns the smallest distance from a ray to a sphere intersection
|
|
|
|
///
|
|
|
|
/// returns NaN on miss
|
|
|
|
/// returns NaN if behind
|
|
|
|
template <size_t S, typename T>
|
|
|
|
constexpr T
|
|
|
|
distance (const ray<S,T> r, const sphere<S,T> s)
|
|
|
|
{
|
|
|
|
const T b = dot (r.direction, r.origin - s.centre);
|
|
|
|
const T c = dot (r.origin - s.centre, r.origin - s.centre) - s.radius * s.radius;
|
|
|
|
|
|
|
|
const T D = b * b - c;
|
|
|
|
|
|
|
|
// no intersection
|
|
|
|
if (D < 0)
|
|
|
|
return std::numeric_limits<T>::quiet_NaN ();
|
|
|
|
|
|
|
|
auto t_ = std::sqrt (D);
|
|
|
|
auto t0 = -b + t_;
|
|
|
|
auto t1 = -b - t_;
|
|
|
|
|
|
|
|
return t1 >= 0 ? t1 :
|
|
|
|
t0 >= 0 ? t0 :
|
|
|
|
std::numeric_limits<T>::quiet_NaN ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2017-08-22 15:46:49 +10:00
|
|
|
|
2018-03-13 23:27:37 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-03-11 22:31:35 +11:00
|
|
|
typedef ray<2,float> ray2f;
|
|
|
|
typedef ray<3,float> ray3f;
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2015-02-19 13:28:56 +11:00
|
|
|
|
|
|
|
#endif
|