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  /// @{
36  /// @name Direct access to member fields
37 
38  /// The normal to the plane
40 
41  /// The distance from the origin to the plane
43 
44  /// @}
45 
46  /// @{
47  /// @name Constructors
48 
49  /// Uninitialized by default
51 
52  /// Initialize with a normal and distance
53  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Plane3 (const Vec3<T>& normal, T distance) IMATH_NOEXCEPT;
54 
55  /// Initialize with a point and a normal
56  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Plane3 (const Vec3<T>& point, const Vec3<T>& normal) IMATH_NOEXCEPT;
57 
58  /// Initialize with three points
59  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Plane3 (const Vec3<T>& point1,
60  const Vec3<T>& point2,
61  const Vec3<T>& point3) IMATH_NOEXCEPT;
62 
63  /// @}
64 
65  /// @{
66  /// @name Manipulation
67 
68  /// Set via a given normal and distance
69  IMATH_HOSTDEVICE void set (const Vec3<T>& normal, T distance) IMATH_NOEXCEPT;
70 
71  /// Set via a given point and normal
72  IMATH_HOSTDEVICE void set (const Vec3<T>& point, const Vec3<T>& normal) IMATH_NOEXCEPT;
73 
74  /// Set via three points
75  IMATH_HOSTDEVICE void set (const Vec3<T>& point1, const Vec3<T>& point2, const Vec3<T>& point3) IMATH_NOEXCEPT;
76 
77  /// @}
78 
79  /// @{
80  /// @name Utility Methods
81 
82  /// Determine if a line intersects the plane.
83  /// @param line The line
84  /// @param[out] intersection The point of intersection
85  /// @return True if the line intersects the plane.
86  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool
87  intersect (const Line3<T>& line, Vec3<T>& intersection) const IMATH_NOEXCEPT;
88 
89  /// Determine if a line intersects the plane.
90  /// @param line The line
91  /// @param[out] parameter The parametric value of the point of intersection
92  /// @return True if the line intersects the plane.
93  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersectT (const Line3<T>& line, T& parameter) const IMATH_NOEXCEPT;
94 
95  /// Return the distance from a point to the plane.
96  IMATH_HOSTDEVICE constexpr T distanceTo (const Vec3<T>& point) const IMATH_NOEXCEPT;
97 
98  /// Reflect the given point around the plane.
99  IMATH_HOSTDEVICE constexpr Vec3<T> reflectPoint (const Vec3<T>& point) const IMATH_NOEXCEPT;
100 
101  /// Reflect the direction vector around the plane
102  IMATH_HOSTDEVICE constexpr Vec3<T> reflectVector (const Vec3<T>& vec) const IMATH_NOEXCEPT;
103 
104  /// @}
105 };
106 
107 /// Plane of type float
109 
110 /// Plane of type double
112 
113 //---------------
114 // Implementation
115 //---------------
116 
117 template <class T>
118 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Plane3<T>::Plane3 (const Vec3<T>& p0, const Vec3<T>& p1, const Vec3<T>& p2) IMATH_NOEXCEPT
119 {
120  set (p0, p1, p2);
121 }
122 
123 template <class T>
124 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Plane3<T>::Plane3 (const Vec3<T>& n, T d) IMATH_NOEXCEPT
125 {
126  set (n, d);
127 }
128 
129 template <class T>
130 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Plane3<T>::Plane3 (const Vec3<T>& p, const Vec3<T>& n) IMATH_NOEXCEPT
131 {
132  set (p, n);
133 }
134 
135 template <class T>
136 IMATH_HOSTDEVICE inline void
137 Plane3<T>::set (const Vec3<T>& point1, const Vec3<T>& point2, const Vec3<T>& point3) IMATH_NOEXCEPT
138 {
139  normal = (point2 - point1) % (point3 - point1);
140  normal.normalize();
141  distance = normal ^ point1;
142 }
143 
144 template <class T>
145 IMATH_HOSTDEVICE inline void
147 {
148  normal = n;
149  normal.normalize();
150  distance = normal ^ point;
151 }
152 
153 template <class T>
154 IMATH_HOSTDEVICE inline void
156 {
157  normal = n;
158  normal.normalize();
159  distance = d;
160 }
161 
162 template <class T>
163 IMATH_HOSTDEVICE constexpr inline T
165 {
166  return (point ^ normal) - distance;
167 }
168 
169 template <class T>
170 IMATH_HOSTDEVICE constexpr inline Vec3<T>
172 {
173  return normal * distanceTo (point) * -2.0 + point;
174 }
175 
176 template <class T>
177 IMATH_HOSTDEVICE constexpr inline Vec3<T>
179 {
180  return normal * (normal ^ v) * 2.0 - v;
181 }
182 
183 template <class T>
184 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
186 {
187  T d = normal ^ line.dir;
188  if (d == 0.0)
189  return false;
190  T t = -((normal ^ line.pos) - distance) / d;
191  point = line (t);
192  return true;
193 }
194 
195 template <class T>
196 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
198 {
199  T d = normal ^ line.dir;
200  if (d == 0.0)
201  return false;
202  t = -((normal ^ line.pos) - distance) / d;
203  return true;
204 }
205 
206 /// Stream output, as "(normal distance)"
207 template <class T>
208 std::ostream&
209 operator<< (std::ostream& o, const Plane3<T>& plane)
210 {
211  return o << "(" << plane.normal << ", " << plane.distance << ")";
212 }
213 
214 /// Transform a plane by a matrix
215 template <class T>
216 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Plane3<T>
218 {
219  // T
220  // -1
221  // Could also compute M but that would suck.
222  //
223 
224  Vec3<T> dir1 = Vec3<T> (1, 0, 0) % plane.normal;
225  T dir1Len = dir1 ^ dir1;
226 
227  Vec3<T> tmp = Vec3<T> (0, 1, 0) % plane.normal;
228  T tmpLen = tmp ^ tmp;
229 
230  if (tmpLen > dir1Len)
231  {
232  dir1 = tmp;
233  dir1Len = tmpLen;
234  }
235 
236  tmp = Vec3<T> (0, 0, 1) % plane.normal;
237  tmpLen = tmp ^ tmp;
238 
239  if (tmpLen > dir1Len)
240  {
241  dir1 = tmp;
242  }
243 
244  Vec3<T> dir2 = dir1 % plane.normal;
245  Vec3<T> point = plane.distance * plane.normal;
246 
247  return Plane3<T> (point * M, (point + dir2) * M, (point + dir1) * M);
248 }
249 
250 /// Reflect the pla
251 template <class T>
252 IMATH_HOSTDEVICE constexpr inline Plane3<T>
254 {
255  return Plane3<T> (-plane.normal, -plane.distance);
256 }
257 
258 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
259 
260 #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:171
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:72
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersect(const Line3< T > &line, Vec3< T > &intersection) const IMATH_NOEXCEPT
Definition: ImathPlane.h:185
Definition: ImathVec.h:32
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:178
#define IMATH_HOSTDEVICE
Definition: ImathConfig.h:102
T distance
The distance from the origin to the plane.
Definition: ImathPlane.h:42
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
IMATH_HOSTDEVICE void set(const Vec3< T > &normal, T distance) IMATH_NOEXCEPT
Set via a given normal and distance.
Definition: ImathPlane.h:155
Plane3< double > Plane3d
Plane of type double.
Definition: ImathPlane.h:111
GLdouble t
Definition: glad.h:2397
Plane3< float > Plane3f
Plane of type float.
Definition: ImathPlane.h:108
IMATH_HOSTDEVICE constexpr T distanceTo(const Vec3< T > &point) const IMATH_NOEXCEPT
Return the distance from a point to the plane.
Definition: ImathPlane.h:164
#define IMATH_EXPORT_TEMPLATE_TYPE
Definition: ImathExport.h:60
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:80
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:50
Vec3< T > normal
The normal to the plane.
Definition: ImathPlane.h:39
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersectT(const Line3< T > &line, T &parameter) const IMATH_NOEXCEPT
Definition: ImathPlane.h:197