Go to the documentation of this file. 1 #ifndef EAGINE_MATH_INTERSECTION_HPP
9 #define EAGINE_MATH_INTERSECTION_HPP
11 #include "../valid_if/nonnegative.hpp"
12 #include "../valid_if/positive.hpp"
20 EAGINE_DIAG_OFF(
double-promotion)
27 template <
typename T,
typename P,
typename L>
29 nearest_ray_param(
const std::pair<valid_if<T, P, L>, valid_if<T, P, L>>& params)
30 -> valid_if<T, P, L> {
32 const auto& t0 = std::get<0>(params);
33 const auto& t1 = std::get<1>(params);
55 template <
typename T,
bool V>
57 _line_sphere_intersection_a(vector<T, 3, V> ld, vector<T, 3, V> oc) noexcept
59 return -T(2) *
dot(ld, oc);
62 template <
typename T,
bool V>
63 static constexpr
auto _line_sphere_intersection_d(vector<T, 3, V> ld) noexcept
64 -> valid_if_positive<T> {
65 return T(2) *
dot(ld, ld);
68 template <
typename T,
bool V>
69 static constexpr
auto _line_sphere_intersection_b(
72 T sr) noexcept -> valid_if_nonnegative<T> {
75 pow(2 *
dot(ld, oc), 2) - 4 *
dot(ld, ld) * (
dot(oc, oc) - pow(sr, 2)));
79 static constexpr
auto _line_sphere_intersection_t(
81 valid_if_nonnegative<T> b,
82 valid_if_positive<T> d) noexcept {
84 using R = std::pair<optionally_valid<T>, optionally_valid<T>>;
85 return (b && d) ? (
extract(b) > T(0))
92 template <
typename T,
bool V>
93 static constexpr
auto _line_sphere_intersection_p(
94 const line<T, V>& ray,
95 const std::pair<optionally_valid<T>, optionally_valid<T>>& ts) {
96 using E = optionally_valid<vector<T, 3, V>>;
97 using R = std::pair<E, E>;
99 std::get<0>(ts) ? E{ray.point_at(
extract(std::get<0>(ts))),
true} : E{},
100 std::get<1>(ts) ? E{ray.point_at(
extract(std::get<1>(ts))),
true} : E{},
104 template <
typename T,
bool V>
105 auto line_sphere_intersection_params(
106 const line<T, V>& ray,
107 const sphere<T, V>& sph) noexcept
108 -> std::pair<optionally_valid<T>, optionally_valid<T>> {
109 return _line_sphere_intersection_t(
110 _line_sphere_intersection_a(ray.direction(), ray.origin() - sph.center()),
111 _line_sphere_intersection_b(
112 ray.direction(), ray.origin() - sph.center(), sph.radius()),
113 _line_sphere_intersection_d(ray.direction()));
119 template <
typename T,
bool V>
124 return _line_sphere_intersection_p(
125 ray, line_sphere_intersection_params(ray, sph));
128 template <
typename T,
bool V>
129 static constexpr
auto _line_sphere_intersection_n_p(
135 return std::get<0>(ts)
147 template <
typename T,
bool V>
151 return _line_sphere_intersection_n_p(
152 ray, line_sphere_intersection_params(ray, sph));
157 template <
typename T,
bool V>
158 static inline auto line_triangle_intersection_param(
163 const T a =
dot(tri.ab(), h);
168 const T u = f *
dot(s, h);
170 if((u >= T(0)) && (u <= T(1))) {
174 if((v >= T(0)) && (u + v <= T(1))) {
175 const T t = f *
dot(tri.ac(), q);
176 return {t, t >= T(0)};
185 template <
typename T,
bool V>
189 if(
const auto t = line_triangle_intersection_param(ray, tri)) {
201 #endif // EAGINE_MATH_INTERSECTION_HPP
constexpr auto direction() const noexcept -> vector< T, N, V >
Returns the line direction.
Definition: primitives.hpp:35
Basic template for spheres in N-dimensional space.
Definition: primitives.hpp:122
Basic template for lines in N-dimensional space.
Definition: primitives.hpp:19
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
Primary template for conditionally valid values.
Definition: decl.hpp:49
Basic template for triangles in N-dimensional space.
Definition: primitives.hpp:57
static constexpr auto reciprocal(T x) noexcept -> optionally_valid< T >
Returns the reciprocal of x if x is not zero.
Definition: functions.hpp:85
static constexpr auto nearest_line_sphere_intersection(const line< T, V > &ray, const sphere< T, V > &sph) noexcept -> optionally_valid< vector< T, 3, V >>
Finds nearest line-sphere intersection point.
Definition: intersection.hpp:148
Basic N-dimensional vector implementation template.
Definition: fwd.hpp:19
constexpr auto point_at(T t) const noexcept -> vector< T, N, V >
Returns a point on the line at the specified parameter t.
Definition: primitives.hpp:40
Math-related code is placed in this namespace.
Definition: eagine.hpp:48
static constexpr auto dot(const vector< T, N, V > &a, const vector< T, N, V > &b) noexcept
Vector dot product.
Definition: vector.hpp:311
static constexpr auto line_sphere_intersection(const line< T, V > &ray, const sphere< T, V > &sph) noexcept -> std::pair< optionally_valid< vector< T, 3, V >>, optionally_valid< vector< T, 3, V >>>
Finds line-sphere intersection points.
Definition: intersection.hpp:120
constexpr auto origin() const noexcept -> vector< T, N, V >
Returns the line origin.
Definition: primitives.hpp:30
static auto cross(const vector< T, 3, V > &a, const vector< T, 3, V > &b) noexcept
3D vector cross product.
Definition: vector.hpp:326
static auto line_triangle_intersection(const line< T, V > &ray, const triangle< T, V > &tri) noexcept -> optionally_valid< vector< T, 3, V >>
Finds line-triangle intersection point.
Definition: intersection.hpp:186