Atoms Crowd  7.0.0
ImathPlane.h
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5 
6 //
7 // A 3D plane class template
8 //
9 
10 #ifndef INCLUDED_ATOMSMATHPLANE_H
11 #define INCLUDED_ATOMSMATHPLANE_H
12 
13 #include <AtomsMath/ImathLine.h>
14 #include <AtomsMath/ImathNamespace.h>
15 #include <AtomsMath/ImathVec.h>
16 
17 ATOMSMATH_INTERNAL_NAMESPACE_HEADER_ENTER
18 
28 
29 template <class T> class Plane3
30 {
31  public:
32 
35 
38 
41 
43 
46 
48  ATOMSMATH_HOSTDEVICE Plane3() noexcept {}
49 
51  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Plane3 (const Vec3<T>& normal, T distance) noexcept;
52 
54  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Plane3 (const Vec3<T>& point, const Vec3<T>& normal) noexcept;
55 
57  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Plane3 (const Vec3<T>& point1,
58  const Vec3<T>& point2,
59  const Vec3<T>& point3) noexcept;
60 
62 
65 
67  ATOMSMATH_HOSTDEVICE void set (const Vec3<T>& normal, T distance) noexcept;
68 
70  ATOMSMATH_HOSTDEVICE void set (const Vec3<T>& point, const Vec3<T>& normal) noexcept;
71 
73  ATOMSMATH_HOSTDEVICE void set (const Vec3<T>& point1, const Vec3<T>& point2, const Vec3<T>& point3) noexcept;
74 
76 
79 
84  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool
85  intersect (const Line3<T>& line, Vec3<T>& intersection) const noexcept;
86 
91  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool intersectT (const Line3<T>& line, T& parameter) const noexcept;
92 
94  ATOMSMATH_HOSTDEVICE constexpr T distanceTo (const Vec3<T>& point) const noexcept;
95 
97  ATOMSMATH_HOSTDEVICE constexpr Vec3<T> reflectPoint (const Vec3<T>& point) const noexcept;
98 
100  ATOMSMATH_HOSTDEVICE constexpr Vec3<T> reflectVector (const Vec3<T>& vec) const noexcept;
101 
103 };
104 
106 typedef Plane3<float> Plane3f;
107 
109 typedef Plane3<double> Plane3d;
110 
111 //---------------
112 // Implementation
113 //---------------
114 
115 template <class T>
116 ATOMSMATH_CONSTEXPR14 inline Plane3<T>::Plane3 (const Vec3<T>& p0, const Vec3<T>& p1, const Vec3<T>& p2) noexcept
117 {
118  set (p0, p1, p2);
119 }
120 
121 template <class T> ATOMSMATH_CONSTEXPR14 inline Plane3<T>::Plane3 (const Vec3<T>& n, T d) noexcept
122 {
123  set (n, d);
124 }
125 
126 template <class T> ATOMSMATH_CONSTEXPR14 inline Plane3<T>::Plane3 (const Vec3<T>& p, const Vec3<T>& n) noexcept
127 {
128  set (p, n);
129 }
130 
131 template <class T>
132 inline void
133 Plane3<T>::set (const Vec3<T>& point1, const Vec3<T>& point2, const Vec3<T>& point3) noexcept
134 {
135  normal = (point2 - point1) % (point3 - point1);
136  normal.normalize();
137  distance = normal ^ point1;
138 }
139 
140 template <class T>
141 inline void
142 Plane3<T>::set (const Vec3<T>& point, const Vec3<T>& n) noexcept
143 {
144  normal = n;
145  normal.normalize();
146  distance = normal ^ point;
147 }
148 
149 template <class T>
150 inline void
151 Plane3<T>::set (const Vec3<T>& n, T d) noexcept
152 {
153  normal = n;
154  normal.normalize();
155  distance = d;
156 }
157 
158 template <class T>
159 constexpr inline T
160 Plane3<T>::distanceTo (const Vec3<T>& point) const noexcept
161 {
162  return (point ^ normal) - distance;
163 }
164 
165 template <class T>
166 constexpr inline Vec3<T>
167 Plane3<T>::reflectPoint (const Vec3<T>& point) const noexcept
168 {
169  return normal * distanceTo (point) * -2.0 + point;
170 }
171 
172 template <class T>
173 constexpr inline Vec3<T>
174 Plane3<T>::reflectVector (const Vec3<T>& v) const noexcept
175 {
176  return normal * (normal ^ v) * 2.0 - v;
177 }
178 
179 template <class T>
180 ATOMSMATH_CONSTEXPR14 inline bool
181 Plane3<T>::intersect (const Line3<T>& line, Vec3<T>& point) const noexcept
182 {
183  T d = normal ^ line.dir;
184  if (d == 0.0)
185  return false;
186  T t = -((normal ^ line.pos) - distance) / d;
187  point = line (t);
188  return true;
189 }
190 
191 template <class T>
192 ATOMSMATH_CONSTEXPR14 inline bool
193 Plane3<T>::intersectT (const Line3<T>& line, T& t) const noexcept
194 {
195  T d = normal ^ line.dir;
196  if (d == 0.0)
197  return false;
198  t = -((normal ^ line.pos) - distance) / d;
199  return true;
200 }
201 
203 template <class T>
204 std::ostream&
205 operator<< (std::ostream& o, const Plane3<T>& plane)
206 {
207  return o << "(" << plane.normal << ", " << plane.distance << ")";
208 }
209 
211 template <class T>
212 ATOMSMATH_CONSTEXPR14 Plane3<T>
213 operator* (const Plane3<T>& plane, const Matrix44<T>& M) noexcept
214 {
215  // T
216  // -1
217  // Could also compute M but that would suck.
218  //
219 
220  Vec3<T> dir1 = Vec3<T> (1, 0, 0) % plane.normal;
221  T dir1Len = dir1 ^ dir1;
222 
223  Vec3<T> tmp = Vec3<T> (0, 1, 0) % plane.normal;
224  T tmpLen = tmp ^ tmp;
225 
226  if (tmpLen > dir1Len)
227  {
228  dir1 = tmp;
229  dir1Len = tmpLen;
230  }
231 
232  tmp = Vec3<T> (0, 0, 1) % plane.normal;
233  tmpLen = tmp ^ tmp;
234 
235  if (tmpLen > dir1Len)
236  {
237  dir1 = tmp;
238  }
239 
240  Vec3<T> dir2 = dir1 % plane.normal;
241  Vec3<T> point = plane.distance * plane.normal;
242 
243  return Plane3<T> (point * M, (point + dir2) * M, (point + dir1) * M);
244 }
245 
247 template <class T>
248 constexpr inline Plane3<T>
249 operator- (const Plane3<T>& plane) noexcept
250 {
251  return Plane3<T> (-plane.normal, -plane.distance);
252 }
253 
254 ATOMSMATH_INTERNAL_NAMESPACE_HEADER_EXIT
255 
256 #endif // INCLUDED_ATOMSMATHPLANE_H
Definition: ImathLine.h:25
Definition: ImathMatrix.h:631
Definition: ImathPlane.h:30
ATOMSMATH_HOSTDEVICE void set(const Vec3< T > &normal, T distance) noexcept
Set via a given normal and distance.
Definition: ImathPlane.h:151
ATOMSMATH_HOSTDEVICE Plane3() noexcept
Uninitialized by default.
Definition: ImathPlane.h:48
T distance
The distance from the origin to the plane.
Definition: ImathPlane.h:40
constexpr ATOMSMATH_HOSTDEVICE T distanceTo(const Vec3< T > &point) const noexcept
Return the distance from a point to the plane.
Definition: ImathPlane.h:160
constexpr ATOMSMATH_HOSTDEVICE Vec3< T > reflectPoint(const Vec3< T > &point) const noexcept
Reflect the given point around the plane.
Definition: ImathPlane.h:167
Vec3< T > normal
The normal to the plane.
Definition: ImathPlane.h:37
constexpr ATOMSMATH_HOSTDEVICE Vec3< T > reflectVector(const Vec3< T > &vec) const noexcept
Reflect the direction vector around the plane.
Definition: ImathPlane.h:174
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool intersectT(const Line3< T > &line, T &parameter) const noexcept
Definition: ImathPlane.h:193
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool intersect(const Line3< T > &line, Vec3< T > &intersection) const noexcept
Definition: ImathPlane.h:181
Definition: ImathVec.h:260