vsg: include/vsg/maths/box.h Source File

vsg 1.1.8 VulkanSceneGraph library

Loading...
Searching...
No Matches
box.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/maths/vec3.h>
16
17#include <limits>
18
19namespace vsg
20{
22 template<typename T>
23 struct t_box
24 {
25 using value_type = T;
26 using vec_type = t_vec3<T>;
27
28 vec_type min = vec_type(std::numeric_limits<value_type>::max(), std::numeric_limits<value_type>::max(), std::numeric_limits<value_type>::max());
29 vec_type max = vec_type(std::numeric_limits<value_type>::lowest(), std::numeric_limits<value_type>::lowest(), std::numeric_limits<value_type>::lowest());
30
31 constexpr t_box() = default;
32 constexpr t_box(const t_box& s) = default;
33
34 template<typename R>
35 constexpr explicit t_box(const t_box<R>& s) :
36 min(s.min),
37 max(s.max) {}
38
39 constexpr t_box(const vec_type& in_min, const vec_type& in_max) :
40 min(in_min),
41 max(in_max) {}
42
43 constexpr t_box& operator=(const t_box&) = default;
44
45 constexpr std::size_t size() const { return 6; }
46
47 value_type& operator[](std::size_t i) { return data()[i]; }
48 value_type operator[](std::size_t i) const { return data()[i]; }
49
50 bool valid() const { return min.x <= max.x; }
51
52 explicit operator bool() const noexcept { return valid(); }
53
54 T* data() { return min.data(); }
55 const T* data() const { return min.data(); }
56
57 void reset()
58 {
59 min = vec_type(std::numeric_limits<value_type>::max(), std::numeric_limits<value_type>::max(), std::numeric_limits<value_type>::max());
60 max = vec_type(std::numeric_limits<value_type>::lowest(), std::numeric_limits<value_type>::lowest(), std::numeric_limits<value_type>::lowest());
61 }
62
63 template<typename R>
64 void add(const t_vec3<R>& v)
65 {
66 add(v.x, v.y, v.z);
67 }
68
69 void add(value_type x, value_type y, value_type z)
70 {
71 if (x < min.x) min.x = x;
72 if (y < min.y) min.y = y;
73 if (z < min.z) min.z = z;
74 if (x > max.x) max.x = x;
75 if (y > max.y) max.y = y;
76 if (z > max.z) max.z = z;
77 }
78
79 template<typename R>
80 void add(const t_box<R>& bb)
81 {
82 if (bb.min.x < min.x) min.x = bb.min.x;
83 if (bb.min.y < min.y) min.y = bb.min.y;
84 if (bb.min.z < min.z) min.z = bb.min.z;
85 if (bb.max.x > max.x) max.x = bb.max.x;
86 if (bb.max.y > max.y) max.y = bb.max.y;
87 if (bb.max.z > max.z) max.z = bb.max.z;
88 }
89 };
90
91 using box = t_box<float>;
92 using dbox = t_box<double>;
93
94 VSG_type_name(vsg::box);
95 VSG_type_name(vsg::dbox);
96
97 template<typename T>
98 constexpr bool operator==(const t_box<T>& lhs, const t_box<T>& rhs)
99 {
100 return (lhs.min == rhs.min) && (lhs.max == rhs.max);
101 }
102
103 template<typename T>
104 constexpr bool operator!=(const t_box<T>& lhs, const t_box<T>& rhs)
105 {
106 return (lhs.min != rhs.min) || (lhs.max != rhs.max);
107 }
108
109 template<typename T>
110 constexpr bool operator<(const t_box<T>& lhs, const t_box<T>& rhs)
111 {
112 if (lhs.min < rhs.min) return true;
113 if (rhs.min < lhs.min) return false;
114 return lhs.max < rhs.max;
115 }
116
117} // namespace vsg
vsg::t_box
t_box template class that represents an axis aligned bounding box
Definition box.h:24
vsg::t_vec3
t_vec3 template class that represents a 3D vector
Definition vec3.h:34