vsg  1.0.4
VulkanSceneGraph library
plane.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/sphere.h>
27 
28 namespace vsg
29 {
31  template<typename T>
32  struct t_plane
33  {
34  using value_type = T;
35  using vec_type = t_vec4<T>;
36  using normal_type = t_vec3<T>;
37 
38  union
39  {
40  value_type value[4];
41 
42  vec_type vec;
43 
44  // Hessian Normal Form
45  struct
46  {
47  normal_type n;
48  value_type p;
49  };
50  };
51 
52  constexpr t_plane() :
53  value{0.0, 0.0, 0.0, 0.0} {}
54 
55  constexpr t_plane(const t_plane& pl) :
56  value{pl[0], pl[1], pl[2], pl[3]} {}
57 
58  constexpr t_plane& operator=(const t_plane&) = default;
59 
60  constexpr explicit t_plane(const t_vec4<T>& v) :
61  value{v[0], v[1], v[2], v[3]} {}
62 
63  constexpr t_plane(value_type nx, value_type ny, value_type nz, value_type in_p) :
64  value{nx, ny, nz, in_p} {}
65 
66  constexpr t_plane(const normal_type& normal, value_type in_p) :
67  value{normal.x, normal.y, normal.z, in_p} {}
68 
69  constexpr t_plane(const normal_type& position, const normal_type& normal) :
70  value{normal.x, normal.y, normal.z, -(position * normal)} {}
71 
72  template<typename R>
73  constexpr explicit t_plane(const t_plane<R>& v) :
74  value{v[0], v[1], v[2], v[3]} {}
75 
76  template<typename R>
77  constexpr explicit t_plane(const t_vec4<T>& v) :
78  value{v[0], v[1], v[2], v[3]} {}
79 
80  constexpr std::size_t size() const { return 4; }
81 
82  value_type& operator[](std::size_t i) { return value[i]; }
83  value_type operator[](std::size_t i) const { return value[i]; }
84 
85  template<typename R>
86  t_plane& operator=(const t_plane<R>& rhs)
87  {
88  value[0] = static_cast<value_type>(rhs[0]);
89  value[1] = static_cast<value_type>(rhs[1]);
90  value[2] = static_cast<value_type>(rhs[2]);
91  value[3] = static_cast<value_type>(rhs[3]);
92  return *this;
93  }
94 
95  void set(value_type in_x, value_type in_y, value_type in_z, value_type in_d)
96  {
97  value[0] = in_x;
98  value[1] = in_y;
99  value[2] = in_z;
100  value[3] = in_d;
101  }
102 
103  bool valid() const { return n.x != 0.0 && n.y != 0.0 && n.z != 0.0; }
104 
105  T* data() { return value; }
106  const T* data() const { return value; }
107  };
108 
109  using plane = t_plane<float>;
110  using dplane = t_plane<double>;
111 
112  VSG_type_name(vsg::plane);
113  VSG_type_name(vsg::dplane);
114 
115  template<typename T>
116  constexpr T distance(const t_plane<T>& pl, const t_vec3<T>& v)
117  {
118  return dot(pl.n, v) + pl.p;
119  }
120 
121  template<typename T, typename R>
122  constexpr T distance(const t_plane<T>& pl, const t_vec3<R>& v)
123  {
124  using normal_type = typename t_plane<T>::normal_type;
125  return dot(pl.n, normal_type(v)) + pl.p;
126  }
127 
129  template<class PlaneItr, typename T>
130  constexpr bool intersect(PlaneItr first, PlaneItr last, const t_sphere<T>& s)
131  {
132  auto negative_radius = -s.radius;
133  for (auto itr = first; itr != last; ++itr)
134  {
135  if (distance(*itr, s.center) < negative_radius) return false;
136  }
137  return true;
138  }
139 
140  template<class Polytope, typename T>
141  constexpr bool intersect(const Polytope& polytope, const t_sphere<T>& s)
142  {
143  return intersect(polytope.begin(), polytope.end(), s);
144  }
145 } // namespace vsg
146 
147 #if defined(__clang__)
148 # pragma clang diagnostic pop
149 #endif
150 #if defined(__GNUC__)
151 # pragma GCC diagnostic pop
152 #endif
Definition: plane.h:33
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