sandbox
Loading...
Searching...
No Matches
plane.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_MATH_PLANE_HPP_
3#define LIBSBX_MATH_PLANE_HPP_
4
6#include <libsbx/math/vector3.hpp>
7#include <libsbx/math/ray.hpp>
8
9namespace sbx::math {
10
11template<scalar Type>
13
14public:
15
16 using value_type = Type;
18
19 basic_plane() noexcept = default;
20
21 basic_plane(const vector_type& normal, const value_type distance) noexcept
22 : _normal{normal},
23 _distance{distance} { }
24
26 : _normal{plane.x(), plane.y(), plane.z()},
27 _distance{plane.w()} { }
28
29 auto normal() const noexcept -> const vector_type& {
30 return _normal;
31 }
32
33 auto distance() const noexcept -> value_type {
34 return _distance;
35 }
36
37 auto distance_to_point(const vector_type& point) const noexcept -> value_type {
38 return math::vector3::dot(_normal, point) + _distance;
39 }
40
41 auto normalize() noexcept -> basic_plane& {
42 const auto length = _normal.length();
43
44 _normal /= length;
45 _distance /= length;
46
47 return *this;
48 }
49
50 static auto normalized(const basic_plane& plane) noexcept -> basic_plane {
51 const auto length = plane._normal.length();
52
53 return basic_plane{plane._normal / length, plane._distance / length};
54 }
55
56 auto ray_intersect(const sbx::math::ray& ray) -> std::optional<sbx::math::vector3> {
57 const auto denominator = sbx::math::vector3::dot(_normal, ray.direction());
58
60 return std::nullopt;
61 }
62
63 const auto t = -distance_to_point(ray.origin()) / denominator;
64
65 if (t < 0.0f) {
66 return std::nullopt;
67 }
68
69 return ray.point_at(t);
70 }
71
72private:
73
74 vector_type _normal;
75 value_type _distance;
76
77}; // class basic_plane
78
80
81using plane = planef;
82
83} // namespace sbx::math
84
85#endif // LIBSBX_MATH_PLANE_HPP_
Definition: plane.hpp:12
Definition: vector4.hpp:24
constexpr auto length() const noexcept -> length_type
Returns the length of the vector.
Definition: vector.ipp:202
3D ray with normalized direction.
Definition: ray.hpp:37
auto direction() const -> const vector3 &
Returns the ray direction.
Definition: ray.cpp:18
auto point_at(const std::float_t t) const -> vector3
Computes a point along the ray at parameter t.
Definition: ray.cpp:22
auto origin() const -> const vector3 &
Returns the ray origin.
Definition: ray.cpp:14
Core numeric concepts and type traits.
Ray type for geometric queries.
Definition: traits.hpp:11