Antkeeper  0.0.1
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 primitives {
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 T sqr_distance(const vector_type& point) const noexcept
68  {
70  }
71 
79  [[nodiscard]] inline T distance(const vector_type& point) const noexcept
80  {
81  const T d = sqr_distance(point);
82  return (d) ? std::sqrt(d) : d;
83  }
84 };
85 
86 } // namespace primitives
87 
88 using namespace primitives;
89 
90 } // namespace geom
91 
92 #endif // ANTKEEPER_GEOM_PRIMITIVES_RAY_HPP
Geometric algorithms.
constexpr point< T, N > closest_point(const ray< T, N > &a, const point< T, N > &b) noexcept
Calculates the closest point on a ray to a point.
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:1514
vector< T, N > sqrt(const vector< T, N > &x)
Takes the square root of each element.
Half of a line proceeding from an initial point.
Definition: ray.hpp:38
T distance(const vector_type &point) const noexcept
Calculates the distance from the ray to a point.
Definition: ray.hpp:79
constexpr vector_type extrapolate(T distance) const noexcept
Extrapolates from the ray origin along the ray direction vector.
Definition: ray.hpp:55
constexpr T sqr_distance(const vector_type &point) const noexcept
Calculates the square distance from the ray to a point.
Definition: ray.hpp:67
vector_type direction
Ray direction vector.
Definition: ray.hpp:46
vector_type origin
Ray origin position.
Definition: ray.hpp:43
n-dimensional vector.
Definition: vector.hpp:44