Antkeeper  0.0.1
primitives/ray.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_GEOM_PRIMITIVES_RAY_HPP
21 #define ANTKEEPER_GEOM_PRIMITIVES_RAY_HPP
22 
23 #include <engine/math/vector.hpp>
24 #include <algorithm>
25 #include <cmath>
26 
27 namespace geom {
28 namespace primitive {
29 
36 template <class T, std::size_t N>
37 struct ray
38 {
41 
44 
47 
55  [[nodiscard]] inline constexpr vector_type extrapolate(T distance) const noexcept
56  {
57  return origin + direction * distance;
58  }
59 
67  [[nodiscard]] inline constexpr vector_type closest_point(const vector_type& point) const noexcept
68  {
69  return extrapolate(std::max<T>(T{0}, math::dot(point - origin, direction)));
70  }
71 
79  [[nodiscard]] inline constexpr T sqr_distance(const vector_type& point) const noexcept
80  {
81  return math::sqr_distance(point, closest_point(point));
82  }
83 
91  [[nodiscard]] inline T distance(const vector_type& point) const noexcept
92  {
93  const T d = sqr_distance(point);
94  return (d) ? std::sqrt(d) : d;
95  }
96 };
97 
98 } // namespace primitive
99 } // namespace geom
100 
101 #endif // ANTKEEPER_GEOM_PRIMITIVES_RAY_HPP
Geometry.
Definition: aabb.hpp:30
constexpr T sqr_distance(const vector< T, N > &p0, const vector< T, N > &p1) noexcept
Calculates the square distance between two points.
Definition: vector.hpp:1360
vector< T, N > sqrt(const vector< T, N > &x)
Takes the square root of each element.
constexpr T dot(const quaternion< T > &a, const quaternion< T > &b) noexcept
Calculates the dot product of two quaternions.
Definition: quaternion.hpp:500
Half of a line proceeding from an initial point.
T distance(const vector_type &point) const noexcept
Calculates the distance from the ray to a point.
constexpr vector_type closest_point(const vector_type &point) const noexcept
Calculates the closest point on the ray to a point.
vector_type direction
Ray direction vector.
constexpr T sqr_distance(const vector_type &point) const noexcept
Calculates the square distance from the ray to a point.
constexpr vector_type extrapolate(T distance) const noexcept
Extrapolates from the ray origin along the ray direction vector.
vector_type origin
Ray origin position.
n-dimensional vector.
Definition: vector.hpp:43