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  /// @name Direct access to member fields
29 
30  /// A point on the line
32 
33  /// The direction of the line
35 
36  /// @}
37 
38  /// @{
39  /// @name Constructors
40 
41  /// Uninitialized by default
43 
44  /// Initialize with two points. The direction is the difference
45  /// between the points.
46  IMATH_HOSTDEVICE IMATH_CONSTEXPR14
47  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
57  set (const Vec3<T>& point1, const Vec3<T>& point2) IMATH_NOEXCEPT;
58 
59  /// @}
60 
61  /// @{
62  /// @name Utility Methods
63 
64  /// Return the point on the line at the given parameter value,
65  /// e.g. L(t)
66  IMATH_HOSTDEVICE constexpr Vec3<T>
67  operator() (T parameter) const IMATH_NOEXCEPT;
68 
69  /// Return the distance to the given point
70  IMATH_HOSTDEVICE constexpr T
71  distanceTo (const Vec3<T>& point) const IMATH_NOEXCEPT;
72  /// Return the distance to the given line
73  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T
74  distanceTo (const Line3<T>& line) const IMATH_NOEXCEPT;
75 
76  /// Return the point on the line closest to the given point
77  IMATH_HOSTDEVICE constexpr Vec3<T>
78  closestPointTo (const Vec3<T>& point) const IMATH_NOEXCEPT;
79 
80  /// Return the point on the line closest to the given line
81  IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3<T>
82  closestPointTo (const Line3<T>& line) const IMATH_NOEXCEPT;
83 
84  /// @}
85 
86  typedef T BaseType;
87  typedef T value_type;
88 };
89 
90 /// Line of type float
92 
93 /// Line of type double
95 
96 template <class T>
97 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Line3<T>::Line3 (
98  const Vec3<T>& p0, const Vec3<T>& p1) IMATH_NOEXCEPT
99 {
100  set (p0, p1);
101 }
102 
103 template <class T>
104 IMATH_HOSTDEVICE inline void
106 {
107  pos = p0;
108  dir = p1 - p0;
109  dir.normalize ();
110 }
111 
112 template <class T>
113 IMATH_HOSTDEVICE constexpr inline Vec3<T>
115 {
116  return pos + dir * parameter;
117 }
118 
119 template <class T>
120 IMATH_HOSTDEVICE constexpr inline T
122 {
123  return (closestPointTo (point) - point).length ();
124 }
125 
126 template <class T>
127 IMATH_HOSTDEVICE constexpr inline Vec3<T>
129 {
130  return ((point - pos) ^ dir) * dir + pos;
131 }
132 
133 template <class T>
134 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
136 {
137  T d = (dir % line.dir) ^ (line.pos - pos);
138  return (d >= 0) ? d : -d;
139 }
140 
141 template <class T>
142 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Vec3<T>
144 {
145  // Assumes the lines are normalized
146 
147  Vec3<T> posLpos = pos - line.pos;
148  T c = dir ^ posLpos;
149  T a = line.dir ^ dir;
150  T f = line.dir ^ posLpos;
151  T num = c - a * f;
152 
153  T denom = a * a - 1;
154 
155  T absDenom = ((denom >= 0) ? denom : -denom);
156 
157  if (absDenom < 1)
158  {
159  T absNum = ((num >= 0) ? num : -num);
160 
161  if (absNum >= absDenom * std::numeric_limits<T>::max ()) return pos;
162  }
163 
164  return pos + dir * (num / denom);
165 }
166 
167 /// Stream output, as "(pos dir)"
168 template <class T>
169 std::ostream&
170 operator<< (std::ostream& o, const Line3<T>& line)
171 {
172  return o << "(" << line.pos << ", " << line.dir << ")";
173 }
174 
175 /// Transform a line by a matrix
176 template <class S, class T>
177 IMATH_HOSTDEVICE constexpr inline Line3<S>
179 {
180  return Line3<S> (line.pos * M, (line.pos + line.dir) * M);
181 }
182 
183 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
184 
185 #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:561
T BaseType
Definition: ImathLine.h:86
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:78
Vec3< T > pos
A point on the line.
Definition: ImathLine.h:31
Definition: ImathVec.h:40
T value_type
Definition: ImathLine.h:87
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
IMATH_HOSTDEVICE constexpr Vec3< T > operator()(T parameter) const IMATH_NOEXCEPT
Definition: ImathLine.h:114
#define IMATH_HOSTDEVICE
Definition: ImathConfig.h:108
IMATH_HOSTDEVICE constexpr Line3() IMATH_NOEXCEPT
Uninitialized by default.
Definition: ImathLine.h:42
GLfloat f
Definition: glcorearb.h:1926
constexpr auto set(type rhs) -> int
Definition: core.h:610
Vec3< T > dir
The direction of the line.
Definition: ImathLine.h:34
Line3< double > Line3d
Line of type double.
Definition: ImathLine.h:94
IMATH_HOSTDEVICE void set(const Vec3< T > &point1, const Vec3< T > &point2) IMATH_NOEXCEPT
Definition: ImathLine.h:105
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
IMATH_HOSTDEVICE constexpr T distanceTo(const Vec3< T > &point) const IMATH_NOEXCEPT
Return the distance to the given point.
Definition: ImathLine.h:121
Line3< float > Line3f
Line of type float.
Definition: ImathLine.h:91
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:128