sandbox
Loading...
Searching...
No Matches
volume.hpp
1#ifndef LIBSBX_MATH_VOLUME_HPP_
2#define LIBSBX_MATH_VOLUME_HPP_
3
4#include <libsbx/math/concepts.hpp>
5#include <libsbx/math/vector3.hpp>
6#include <libsbx/math/matrix4x4.hpp>
7
8namespace sbx::math {
9
10template<scalar Type>
12
13public:
14
15 using value_type = Type;
17
18 basic_volume() noexcept = default;
19
20 basic_volume(const vector_type& min, const vector_type& max) noexcept
21 : _min{min},
22 _max{max} { }
23
24 static auto transformed(const basic_volume& volume, const math::matrix4x4& matrix) -> basic_volume {
25 auto min = math::vector3{std::numeric_limits<std::float_t>::max()};
26 auto max = math::vector3{std::numeric_limits<std::float_t>::lowest()};
27
28 for (const auto& corner : volume.corners()) {
29 const auto transformed = math::vector3{matrix * math::vector4{corner, 1.0f}};
30 min = math::vector3::min(min, transformed);
31 max = math::vector3::max(max, transformed);
32 }
33
34 return basic_volume{min, max};
35 }
36
37 auto min() const noexcept -> const vector_type& {
38 return _min;
39 }
40
41 auto max() const noexcept -> const vector_type& {
42 return _max;
43 }
44
45 auto center() const noexcept -> vector_type {
46 return (_min + _max) / 2.0f;
47 }
48
49 auto corners() const noexcept -> std::array<math::vector3, 8u> {
50 return std::array<math::vector3, 8u>{
51 math::vector3{_min.x(), _min.y(), _min.z()},
52 math::vector3{_min.x(), _min.y(), _max.z()},
53 math::vector3{_min.x(), _max.y(), _min.z()},
54 math::vector3{_min.x(), _max.y(), _max.z()},
55 math::vector3{_max.x(), _min.y(), _min.z()},
56 math::vector3{_max.x(), _min.y(), _max.z()},
57 math::vector3{_max.x(), _max.y(), _min.z()},
58 math::vector3{_max.x(), _max.y(), _max.z()}
59 };
60 }
61
62 auto contains(const vector_type& point) const noexcept -> bool {
63 return point.x() >= _min.x() && point.x() <= _max.x() && point.y() >= _min.y() && point.y() <= _max.y() && point.z() >= _min.z() && point.z() <= _max.z();
64 }
65
66 auto contains(const basic_volume& other) const noexcept -> bool {
67 return _min.x() <= other.min().x() && _min.y() <= other.min().y() && _min.z() <= other.min().z() && _max.x() >= other.max().x() && _max.y() >= other.max().y() && _max.z() >= other.max().z();
68 }
69
70 auto intersects(const basic_volume& other) const noexcept -> bool {
71 return _min.x() <= other.max().x() && _max.x() >= other.min().x() && _min.y() <= other.max().y() && _max.y() >= other.min().y() && _min.z() <= other.max().z() && _max.z() >= other.min().z();
72 }
73
74 auto diagonal_length() const noexcept -> value_type {
75 return (_max - _min).length();
76 }
77
78private:
79
80 vector_type _min;
81 vector_type _max;
82
83}; // class basic_volume
84
86
87using volume = volumef;
88
89} // namespace sbx::math
90
91#endif // LIBSBX_MATH_VOLUME_HPP_
Definition: matrix4x4.hpp:25
Definition: vector4.hpp:22
Definition: volume.hpp:11