HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathPlane.h
Go to the documentation of this file.
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_IMATHPLANE_H
11 #define INCLUDED_IMATHPLANE_H
12 
13 #include "ImathExport.h"
14 #include "ImathNamespace.h"
15 
16 #include "ImathLine.h"
17 #include "ImathVec.h"
18 
19 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
20 
21 ///
22 /// The `Plane3` class represents a half space in 3D, so the normal
23 /// may point either towards or away from origin. The plane `P` can
24 /// be represented by Plane3 as either `p` or `-p` corresponding to
25 /// the two half-spaces on either side of the plane. Any function
26 /// which computes a distance will return either negative or positive
27 /// values for the distance indicating which half-space the point is
28 /// in. Note that reflection, and intersection functions will operate
29 /// as expected.
30 
31 template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Plane3
32 {
33 public:
34  /// @{
35  /// @name Direct access to member fields
36 
37  /// The normal to the plane
39 
40  /// The distance from the origin to the plane
42 
43  /// @}
44 
45  /// @{
46  /// @name Constructors
47 
48  /// Uninitialized by default
50 
51  /// Initialize with a normal and distance
52  IMATH_HOSTDEVICE IMATH_CONSTEXPR14
53  Plane3 (const Vec3<T>& normal, T distance) IMATH_NOEXCEPT;
54 
55  /// Initialize with a point and a normal
56  IMATH_HOSTDEVICE IMATH_CONSTEXPR14
57  Plane3 (const Vec3<T>& point, const Vec3<T>& normal) IMATH_NOEXCEPT;
58 
59  /// Initialize with three points
60  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Plane3 (
61  const Vec3<T>& point1,
62  const Vec3<T>& point2,
63  const Vec3<T>& point3) IMATH_NOEXCEPT;
64 
65  /// @}
66 
67  /// @{
68  /// @name Manipulation
69 
70  /// Set via a given normal and distance
71  IMATH_HOSTDEVICE void
72  set (const Vec3<T>& normal, T distance) IMATH_NOEXCEPT;
73 
74  /// Set via a given point and normal
75  IMATH_HOSTDEVICE void
76  set (const Vec3<T>& point, const Vec3<T>& normal) IMATH_NOEXCEPT;
77 
78  /// Set via three points
79  IMATH_HOSTDEVICE void
80  set (const Vec3<T>& point1, const Vec3<T>& point2, const Vec3<T>& point3)
82 
83  /// @}
84 
85  /// @{
86  /// @name Utility Methods
87 
88  /// Determine if a line intersects the plane.
89  /// @param line The line
90  /// @param[out] intersection The point of intersection
91  /// @return True if the line intersects the plane.
92  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersect (
93  const Line3<T>& line, Vec3<T>& intersection) const IMATH_NOEXCEPT;
94 
95  /// Determine if a line intersects the plane.
96  /// @param line The line
97  /// @param[out] parameter The parametric value of the point of intersection
98  /// @return True if the line intersects the plane.
99  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool
100  intersectT (const Line3<T>& line, T& parameter) const IMATH_NOEXCEPT;
101 
102  /// Return the distance from a point to the plane.
103  IMATH_HOSTDEVICE constexpr T
104  distanceTo (const Vec3<T>& point) const IMATH_NOEXCEPT;
105 
106  /// Reflect the given point around the plane.
107  IMATH_HOSTDEVICE constexpr Vec3<T>
108  reflectPoint (const Vec3<T>& point) const IMATH_NOEXCEPT;
109 
110  /// Reflect the direction vector around the plane
111  IMATH_HOSTDEVICE constexpr Vec3<T>
112  reflectVector (const Vec3<T>& vec) const IMATH_NOEXCEPT;
113 
114  /// @}
115  typedef T BaseType;
116  typedef T value_type;
117 };
118 
119 /// Plane of type float
121 
122 /// Plane of type double
124 
125 //---------------
126 // Implementation
127 //---------------
128 
129 template <class T>
130 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Plane3<T>::Plane3 (
131  const Vec3<T>& p0, const Vec3<T>& p1, const Vec3<T>& p2) IMATH_NOEXCEPT
132 {
133  set (p0, p1, p2);
134 }
135 
136 template <class T>
137 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Plane3<T>::Plane3 (
138  const Vec3<T>& n, T d) IMATH_NOEXCEPT
139 {
140  set (n, d);
141 }
142 
143 template <class T>
144 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Plane3<T>::Plane3 (
145  const Vec3<T>& p, const Vec3<T>& n) IMATH_NOEXCEPT
146 {
147  set (p, n);
148 }
149 
150 template <class T>
151 IMATH_HOSTDEVICE inline void
153  const Vec3<T>& point1,
154  const Vec3<T>& point2,
155  const Vec3<T>& point3) IMATH_NOEXCEPT
156 {
157  normal = (point2 - point1) % (point3 - point1);
158  normal.normalize ();
159  distance = normal ^ point1;
160 }
161 
162 template <class T>
163 IMATH_HOSTDEVICE inline void
165 {
166  normal = n;
167  normal.normalize ();
168  distance = normal ^ point;
169 }
170 
171 template <class T>
172 IMATH_HOSTDEVICE inline void
174 {
175  normal = n;
176  normal.normalize ();
177  distance = d;
178 }
179 
180 template <class T>
181 IMATH_HOSTDEVICE constexpr inline T
183 {
184  return (point ^ normal) - distance;
185 }
186 
187 template <class T>
188 IMATH_HOSTDEVICE constexpr inline Vec3<T>
190 {
191  return normal * distanceTo (point) * -2.0 + point;
192 }
193 
194 template <class T>
195 IMATH_HOSTDEVICE constexpr inline Vec3<T>
197 {
198  return normal * (normal ^ v) * 2.0 - v;
199 }
200 
201 template <class T>
202 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
204 {
205  T d = normal ^ line.dir;
206  if (d == 0.0) return false;
207  T t = -((normal ^ line.pos) - distance) / d;
208  point = line (t);
209  return true;
210 }
211 
212 template <class T>
213 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
215 {
216  T d = normal ^ line.dir;
217  if (d == 0.0) return false;
218  t = -((normal ^ line.pos) - distance) / d;
219  return true;
220 }
221 
222 /// Stream output, as "(normal distance)"
223 template <class T>
224 std::ostream&
225 operator<< (std::ostream& o, const Plane3<T>& plane)
226 {
227  return o << "(" << plane.normal << ", " << plane.distance << ")";
228 }
229 
230 /// Transform a plane by a matrix
231 template <class T>
232 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Plane3<T>
234 {
235  // T
236  // -1
237  // Could also compute M but that would suck.
238  //
239 
240  Vec3<T> dir1 = Vec3<T> (1, 0, 0) % plane.normal;
241  T dir1Len = dir1 ^ dir1;
242 
243  Vec3<T> tmp = Vec3<T> (0, 1, 0) % plane.normal;
244  T tmpLen = tmp ^ tmp;
245 
246  if (tmpLen > dir1Len)
247  {
248  dir1 = tmp;
249  dir1Len = tmpLen;
250  }
251 
252  tmp = Vec3<T> (0, 0, 1) % plane.normal;
253  tmpLen = tmp ^ tmp;
254 
255  if (tmpLen > dir1Len) { dir1 = tmp; }
256 
257  Vec3<T> dir2 = dir1 % plane.normal;
258  Vec3<T> point = plane.distance * plane.normal;
259 
260  return Plane3<T> (point * M, (point + dir2) * M, (point + dir1) * M);
261 }
262 
263 /// Reflect the pla
264 template <class T>
265 IMATH_HOSTDEVICE constexpr inline Plane3<T>
267 {
268  return Plane3<T> (-plane.normal, -plane.distance);
269 }
270 
271 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
272 
273 #endif // INCLUDED_IMATHPLANE_H
IMATH_HOSTDEVICE constexpr Vec3< T > reflectPoint(const Vec3< T > &point) const IMATH_NOEXCEPT
Reflect the given point around the plane.
Definition: ImathPlane.h:189
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Multiply each element of the given matrix by scalar and return the result.
Definition: Mat3.h:561
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:78
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersect(const Line3< T > &line, Vec3< T > &intersection) const IMATH_NOEXCEPT
Definition: ImathPlane.h:203
Definition: ImathVec.h:40
const GLdouble * v
Definition: glcorearb.h:837
IMATH_HOSTDEVICE constexpr Vec3< T > reflectVector(const Vec3< T > &vec) const IMATH_NOEXCEPT
Reflect the direction vector around the plane.
Definition: ImathPlane.h:196
T BaseType
Definition: ImathPlane.h:115
#define IMATH_HOSTDEVICE
Definition: ImathConfig.h:108
T distance
The distance from the origin to the plane.
Definition: ImathPlane.h:41
GLdouble n
Definition: glcorearb.h:2008
Mat3< typename promote< T0, T1 >::type > operator-(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Subtract corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:587
constexpr auto set(type rhs) -> int
Definition: core.h:610
IMATH_HOSTDEVICE void set(const Vec3< T > &normal, T distance) IMATH_NOEXCEPT
Set via a given normal and distance.
Definition: ImathPlane.h:173
Plane3< double > Plane3d
Plane of type double.
Definition: ImathPlane.h:123
GLdouble t
Definition: glad.h:2397
Plane3< float > Plane3f
Plane of type float.
Definition: ImathPlane.h:120
IMATH_HOSTDEVICE constexpr T distanceTo(const Vec3< T > &point) const IMATH_NOEXCEPT
Return the distance from a point to the plane.
Definition: ImathPlane.h:182
#define IMATH_EXPORT_TEMPLATE_TYPE
Definition: ImathExport.h:61
IMATH_CONSTEXPR14 bool intersect(const Line3< T > &line, const Vec3< T > &v0, const Vec3< T > &v1, const Vec3< T > &v2, Vec3< T > &pt, Vec3< T > &barycentric, bool &front) IMATH_NOEXCEPT
Definition: ImathLineAlgo.h:85
if(num_boxed_items<=0)
Definition: UT_RTreeImpl.h:697
SIM_API const UT_StringHolder distance
IMATH_HOSTDEVICE Plane3() IMATH_NOEXCEPT
Uninitialized by default.
Definition: ImathPlane.h:49
Vec3< T > normal
The normal to the plane.
Definition: ImathPlane.h:38
T value_type
Definition: ImathPlane.h:116
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersectT(const Line3< T > &line, T &parameter) const IMATH_NOEXCEPT
Definition: ImathPlane.h:214