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

vsg 1.1.13 VulkanSceneGraph library

Loading...
Searching...
No Matches
plane.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/sphere.h>
27
28namespace 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{numbers<value_type>::zero(), numbers<value_type>::zero(), numbers<value_type>::zero(), numbers<value_type>::zero()} {}
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, -dot(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 != numbers<value_type>::zero() || n.y != numbers<value_type>::zero() || n.z != numbers<value_type>::zero(); }
104
105 explicit operator bool() const noexcept { return valid(); }
106
107 T* data() { return value; }
108 const T* data() const { return value; }
109 };
110
111 using plane = t_plane<float>;
112 using dplane = t_plane<double>;
113 using ldplane = t_plane<long double>;
114
115 VSG_type_name(vsg::plane);
116 VSG_type_name(vsg::dplane);
117
118 template<typename T>
119 constexpr bool operator==(const t_plane<T>& lhs, const t_plane<T>& rhs)
120 {
121 return lhs[0] == rhs[0] && lhs[1] == rhs[1] && lhs[2] == rhs[2] && lhs[3] == rhs[3];
122 }
123
124 template<typename T>
125 constexpr bool operator!=(const t_plane<T>& lhs, const t_plane<T>& rhs)
126 {
127 return lhs[0] != rhs[0] || lhs[1] != rhs[1] || lhs[2] != rhs[2] || lhs[3] != rhs[3];
128 }
129
130 template<typename T>
131 constexpr bool operator<(const t_plane<T>& lhs, const t_plane<T>& rhs)
132 {
133 if (lhs[0] < rhs[0]) return true;
134 if (lhs[0] > rhs[0]) return false;
135 if (lhs[1] < rhs[1]) return true;
136 if (lhs[1] > rhs[1]) return false;
137 if (lhs[2] < rhs[2]) return true;
138 if (lhs[2] > rhs[2]) return false;
139 return lhs[3] < rhs[3];
140 }
141
142 template<typename T>
143 constexpr T distance(const t_plane<T>& pl, const t_vec3<T>& v)
144 {
145 return dot(pl.n, v) + pl.p;
146 }
147
148 template<typename T, typename R>
149 constexpr T distance(const t_plane<T>& pl, const t_vec3<R>& v)
150 {
151 using normal_type = typename t_plane<T>::normal_type;
152 return dot(pl.n, normal_type(v)) + pl.p;
153 }
154
156 template<class PlaneItr, typename T>
157 constexpr bool intersect(PlaneItr first, PlaneItr last, const t_sphere<T>& s)
158 {
159 auto negative_radius = -s.radius;
160 for (auto itr = first; itr != last; ++itr)
161 {
162 if (distance(*itr, s.center) < negative_radius) return false;
163 }
164 return true;
165 }
166
167 template<class Polytope, typename T>
168 constexpr bool intersect(const Polytope& polytope, const t_sphere<T>& s)
169 {
170 return intersect(polytope.begin(), polytope.end(), s);
171 }
172
174 template<class PlaneItr, typename T>
175 constexpr bool inside(PlaneItr first, PlaneItr last, const t_vec3<T>& v, T epsilon = 1e-10)
176 {
177 const auto negative_epsilon = -epsilon;
178 for (auto itr = first; itr != last; ++itr)
179 {
180 if (distance(*itr, v) < negative_epsilon) return false;
181 }
182 return true;
183 }
184
186 template<class Polytope, typename T>
187 constexpr bool inside(const Polytope& polytope, const t_vec3<T>& v, T epsilon = 1e-10)
188 {
189 return inside(polytope.begin(), polytope.end(), v, epsilon);
190 }
191} // namespace vsg
192
193#if defined(__clang__)
194# pragma clang diagnostic pop
195#endif
196#if defined(__GNUC__)
197# pragma GCC diagnostic pop
198#endif
vsg::t_plane
Definition plane.h:33
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