HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathLine.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 line class template
8 //
9 
10 #ifndef INCLUDED_IMATHLINE_H
11 #define INCLUDED_IMATHLINE_H
12 
13 #include "ImathMatrix.h"
14 #include "ImathNamespace.h"
15 #include "ImathVec.h"
16 
17 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
18 
19 ///
20 /// The `Line3` class represents a 3D line, defined by a point and a
21 /// direction vector.
22 ///
23 
24 template <class T> class Line3
25 {
26  public:
27 
28  /// @{
29  /// @name Direct access to member fields
30 
31  /// A point on the line
33 
34  /// The direction of the line
36 
37  /// @}
38 
39  /// @{
40  /// @name Constructors
41 
42  /// Uninitialized by default
44 
45  /// Initialize with two points. The direction is the difference
46  /// between the points.
47  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Line3 (const Vec3<T>& point1, const Vec3<T>& point2) IMATH_NOEXCEPT;
48 
49  /// @}
50 
51  /// @{
52  /// @name Manipulation
53 
54  /// Set the line defined by two points. The direction is the difference
55  /// between the points.
56  IMATH_HOSTDEVICE void set (const Vec3<T>& point1, const Vec3<T>& point2) IMATH_NOEXCEPT;
57 
58  /// @}
59 
60  /// @{
61  /// @name Utility Methods
62 
63  /// Return the point on the line at the given parameter value,
64  /// e.g. L(t)
65  IMATH_HOSTDEVICE constexpr Vec3<T> operator() (T parameter) const IMATH_NOEXCEPT;
66 
67  /// Return the distance to the given point
68  IMATH_HOSTDEVICE constexpr T distanceTo (const Vec3<T>& point) const IMATH_NOEXCEPT;
69  /// Return the distance to the given line
70  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T distanceTo (const Line3<T>& line) const IMATH_NOEXCEPT;
71 
72  /// Return the point on the line closest to the given point
73  IMATH_HOSTDEVICE constexpr Vec3<T> closestPointTo (const Vec3<T>& point) const IMATH_NOEXCEPT;
74 
75  /// Return the point on the line closest to the given line
76  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3<T> closestPointTo (const Line3<T>& line) const IMATH_NOEXCEPT;
77 
78  /// @}
79 };
80 
81 /// Line of type float
83 
84 /// Line of type double
86 
87 template <class T>
88 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Line3<T>::Line3 (const Vec3<T>& p0, const Vec3<T>& p1) IMATH_NOEXCEPT
89 {
90  set (p0, p1);
91 }
92 
93 template <class T>
94 IMATH_HOSTDEVICE inline void
96 {
97  pos = p0;
98  dir = p1 - p0;
99  dir.normalize();
100 }
101 
102 template <class T>
103 IMATH_HOSTDEVICE constexpr inline Vec3<T>
105 {
106  return pos + dir * parameter;
107 }
108 
109 template <class T>
110 IMATH_HOSTDEVICE constexpr inline T
112 {
113  return (closestPointTo (point) - point).length();
114 }
115 
116 template <class T>
117 IMATH_HOSTDEVICE constexpr inline Vec3<T>
119 {
120  return ((point - pos) ^ dir) * dir + pos;
121 }
122 
123 template <class T>
124 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
126 {
127  T d = (dir % line.dir) ^ (line.pos - pos);
128  return (d >= 0) ? d : -d;
129 }
130 
131 template <class T>
132 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Vec3<T>
134 {
135  // Assumes the lines are normalized
136 
137  Vec3<T> posLpos = pos - line.pos;
138  T c = dir ^ posLpos;
139  T a = line.dir ^ dir;
140  T f = line.dir ^ posLpos;
141  T num = c - a * f;
142 
143  T denom = a * a - 1;
144 
145  T absDenom = ((denom >= 0) ? denom : -denom);
146 
147  if (absDenom < 1)
148  {
149  T absNum = ((num >= 0) ? num : -num);
150 
151  if (absNum >= absDenom * std::numeric_limits<T>::max())
152  return pos;
153  }
154 
155  return pos + dir * (num / denom);
156 }
157 
158 /// Stream output, as "(pos dir)"
159 template <class T>
160 std::ostream&
161 operator<< (std::ostream& o, const Line3<T>& line)
162 {
163  return o << "(" << line.pos << ", " << line.dir << ")";
164 }
165 
166 /// Transform a line by a matrix
167 template <class S, class T>
168 IMATH_HOSTDEVICE constexpr inline Line3<S>
170 {
171  return Line3<S> (line.pos * M, (line.pos + line.dir) * M);
172 }
173 
174 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
175 
176 #endif // INCLUDED_IMATHLINE_H
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:575
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:72
Vec3< T > pos
A point on the line.
Definition: ImathLine.h:32
Definition: ImathVec.h:32
const GLfloat * c
Definition: glew.h:16631
IMATH_HOSTDEVICE constexpr Vec3< T > operator()(T parameter) const IMATH_NOEXCEPT
Definition: ImathLine.h:104
#define IMATH_HOSTDEVICE
Definition: ImathConfig.h:102
IMATH_HOSTDEVICE constexpr Line3() IMATH_NOEXCEPT
Uninitialized by default.
Definition: ImathLine.h:43
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLuint num
Definition: glew.h:2695
Vec3< T > dir
The direction of the line.
Definition: ImathLine.h:35
Line3< double > Line3d
Line of type double.
Definition: ImathLine.h:85
IMATH_HOSTDEVICE void set(const Vec3< T > &point1, const Vec3< T > &point2) IMATH_NOEXCEPT
Definition: ImathLine.h:95
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLfloat f
Definition: glcorearb.h:1926
IMATH_HOSTDEVICE constexpr T distanceTo(const Vec3< T > &point) const IMATH_NOEXCEPT
Return the distance to the given point.
Definition: ImathLine.h:111
Line3< float > Line3f
Line of type float.
Definition: ImathLine.h:82
IMATH_HOSTDEVICE constexpr Vec3< T > closestPointTo(const Vec3< T > &point) const IMATH_NOEXCEPT
Return the point on the line closest to the given point.
Definition: ImathLine.h:118