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

vsg 1.1.3 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{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, -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 != 0.0 && n.y != 0.0 && n.z != 0.0; }
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
114 VSG_type_name(vsg::plane);
115 VSG_type_name(vsg::dplane);
116
117 template<typename T>
118 constexpr bool operator==(const t_plane<T>& lhs, const t_plane<T>& rhs)
119 {
120 return lhs[0] == rhs[0] && lhs[1] == rhs[1] && lhs[2] == rhs[2] && lhs[3] == rhs[3];
121 }
122
123 template<typename T>
124 constexpr bool operator!=(const t_plane<T>& lhs, const t_plane<T>& rhs)
125 {
126 return lhs[0] != rhs[0] || lhs[1] != rhs[1] || lhs[2] != rhs[2] || lhs[3] != rhs[3];
127 }
128
129 template<typename T>
130 constexpr bool operator<(const t_plane<T>& lhs, const t_plane<T>& rhs)
131 {
132 if (lhs[0] < rhs[0]) return true;
133 if (lhs[0] > rhs[0]) return false;
134 if (lhs[1] < rhs[1]) return true;
135 if (lhs[1] > rhs[1]) return false;
136 if (lhs[2] < rhs[2]) return true;
137 if (lhs[2] > rhs[2]) return false;
138 return lhs[3] < rhs[3];
139 }
140
141 template<typename T>
142 constexpr T distance(const t_plane<T>& pl, const t_vec3<T>& v)
143 {
144 return dot(pl.n, v) + pl.p;
145 }
146
147 template<typename T, typename R>
148 constexpr T distance(const t_plane<T>& pl, const t_vec3<R>& v)
149 {
150 using normal_type = typename t_plane<T>::normal_type;
151 return dot(pl.n, normal_type(v)) + pl.p;
152 }
153
155 template<class PlaneItr, typename T>
156 constexpr bool intersect(PlaneItr first, PlaneItr last, const t_sphere<T>& s)
157 {
158 auto negative_radius = -s.radius;
159 for (auto itr = first; itr != last; ++itr)
160 {
161 if (distance(*itr, s.center) < negative_radius) return false;
162 }
163 return true;
164 }
165
166 template<class Polytope, typename T>
167 constexpr bool intersect(const Polytope& polytope, const t_sphere<T>& s)
168 {
169 return intersect(polytope.begin(), polytope.end(), s);
170 }
171} // namespace vsg
172
173#if defined(__clang__)
174# pragma clang diagnostic pop
175#endif
176#if defined(__GNUC__)
177# pragma GCC diagnostic pop
178#endif
vsg::allocator_affinity_nodes
std container adapter for allocating with MEMORY_AFFINITY_NODES
Definition Allocator.h:138
vsg::t_plane
Definition plane.h:33