vsg  1.0.4
VulkanSceneGraph library
sphere.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission 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 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE 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 disabled 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 
29 namespace 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{0.0, 0.0, 0.0, -1.0} {}
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 >= 0.0; }
110 
111  T* data() { return value; }
112  const T* data() const { return value; }
113  };
114 
115  using sphere = t_sphere<float>;
116  using dsphere = t_sphere<double>;
117 
118  VSG_type_name(vsg::sphere);
119  VSG_type_name(vsg::dsphere);
120 } // namespace vsg
121 
122 #if defined(__clang__)
123 # pragma clang diagnostic pop
124 #endif
125 #if defined(__GNUC__)
126 # pragma GCC diagnostic pop
127 #endif
template sphere class
Definition: sphere.h:34
t_vec3 template class that a represents a 3D vector
Definition: vec3.h:34
t_vec4 template class that a represents a 4D vector
Definition: vec4.h:34