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

vsg 1.1.13 VulkanSceneGraph library

Loading...
Searching...
No Matches
sphere.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// we can't implement the anonymous union/structs combination without causing warnings, so disable them for just this header
16#if defined(__GNUC__)
17# pragma GCC diagnostic push
18# pragma GCC diagnostic ignored "-Wpedantic"
19#endif
20#if defined(__clang__)
21# pragma clang diagnostic push
22# pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
23# pragma clang diagnostic ignored "-Wnested-anon-types"
24#endif
25
26#include <vsg/maths/vec3.h>
27#include <vsg/maths/vec4.h>
28
29namespace vsg
30{
32 template<typename T>
33 struct t_sphere
34 {
35 using value_type = T;
36 using vec_type = t_vec4<T>;
37 using center_type = t_vec3<T>;
38
39 union
40 {
41 value_type value[4];
42
43 vec_type vec;
44
45 struct
46 {
47 value_type x, y, z, r;
48 };
49
50 struct
51 {
52 center_type center;
53 value_type radius;
54 };
55 };
56
57 constexpr t_sphere() :
58 value{numbers<value_type>::zero(), numbers<value_type>::zero(), numbers<value_type>::zero(), numbers<value_type>::minus_one()} {}
59
60 constexpr t_sphere(const t_sphere& s) :
61 value{s[0], s[1], s[2], s[3]} {}
62
63 constexpr t_sphere& operator=(const t_sphere&) = default;
64
65 template<typename R>
66 constexpr explicit t_sphere(const t_sphere<R>& s) :
67 value{static_cast<value_type>(s[0]), static_cast<value_type>(s[1]), static_cast<value_type>(s[2]), static_cast<value_type>(s[3])} {}
68
69 template<typename R>
70 constexpr t_sphere(const t_vec3<R>& c, T rad) :
71 value{static_cast<value_type>(c.x), static_cast<value_type>(c.y), static_cast<value_type>(c.z), rad} {}
72
73 template<typename R>
74 constexpr t_sphere(R sx, R sy, R sz, R sd) :
75 value{sx, sy, sz, sd} {}
76
77 constexpr std::size_t size() const { return 4; }
78
79 value_type& operator[](std::size_t i) { return value[i]; }
80 value_type operator[](std::size_t i) const { return value[i]; }
81
82 template<typename R>
83 t_sphere& operator=(const t_sphere<R>& rhs)
84 {
85 value[0] = static_cast<value_type>(rhs[0]);
86 value[1] = static_cast<value_type>(rhs[1]);
87 value[2] = static_cast<value_type>(rhs[2]);
88 value[3] = static_cast<value_type>(rhs[3]);
89 return *this;
90 }
91
92 void set(value_type in_x, value_type in_y, value_type in_z, value_type in_r)
93 {
94 x = in_x;
95 y = in_y;
96 z = in_z;
97 r = in_r;
98 }
99
100 template<typename R>
101 void set(const t_vec3<R>& c, T rad)
102 {
103 x = static_cast<value_type>(c.x);
104 y = static_cast<value_type>(c.y);
105 z = static_cast<value_type>(c.z);
106 r = rad;
107 }
108
109 bool valid() const { return radius >= numbers<value_type>::zero(); }
110
111 explicit operator bool() const noexcept { return valid(); }
112
113 T* data() { return value; }
114 const T* data() const { return value; }
115
116 void reset()
117 {
118 center.set(numbers<value_type>::zero(), numbers<value_type>::zero(), numbers<value_type>::zero());
119 radius = numbers<value_type>::minus_one();
120 }
121 };
122
123 using sphere = t_sphere<float>;
124 using dsphere = t_sphere<double>;
125 using ldsphere = t_sphere<long double>;
126
127 VSG_type_name(vsg::sphere);
128 VSG_type_name(vsg::dsphere);
129 VSG_type_name(vsg::ldsphere);
130
131 template<typename T>
132 constexpr bool operator==(const t_sphere<T>& lhs, const t_sphere<T>& rhs)
133 {
134 return (lhs.center == rhs.center) && (lhs.radius == rhs.radius);
135 }
136
137 template<typename T>
138 constexpr bool operator!=(const t_sphere<T>& lhs, const t_sphere<T>& rhs)
139 {
140 return (lhs.center != rhs.center) || (lhs.radius != rhs.radius);
141 }
142
143 template<typename T>
144 constexpr bool operator<(const t_sphere<T>& lhs, const t_sphere<T>& rhs)
145 {
146 if (lhs.center < rhs.center) return true;
147 if (rhs.center < lhs.center) return false;
148 return lhs.radius < rhs.radius;
149 }
150
151} // namespace vsg
152
153#if defined(__clang__)
154# pragma clang diagnostic pop
155#endif
156#if defined(__GNUC__)
157# pragma GCC diagnostic pop
158#endif
vsg::t_sphere
template sphere class
Definition sphere.h:34
vsg::t_vec3
t_vec3 template class that represents a 3D vector
Definition vec3.h:34
vsg::t_vec4
t_vec4 template class that represents a 4D vector
Definition vec4.h:35