Debian Codegraph @

src/Imath/ImathLine.h

raw
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
32 Vec3<T> pos;
33
34 /// The direction of the line
35 Vec3<T> dir;
36
37 /// @}
38
39 /// @{
40 /// @name Constructors
41
42 /// Uninitialized by default
43 IMATH_HOSTDEVICE constexpr Line3() IMATH_NOEXCEPT {}
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
82 typedef Line3<float> Line3f;
83
84 /// Line of type double
85 typedef Line3<double> Line3d;
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
95 Line3<T>::set (const Vec3<T>& p0, const Vec3<T>& p1) IMATH_NOEXCEPT
96 {
97 pos = p0;
98 dir = p1 - p0;
99 dir.normalize();
100 }
101
102 template <class T>
103 IMATH_HOSTDEVICE constexpr inline Vec3<T>
104 Line3<T>::operator() (T parameter) const IMATH_NOEXCEPT
105 {
106 return pos + dir * parameter;
107 }
108
109 template <class T>
110 IMATH_HOSTDEVICE constexpr inline T
111 Line3<T>::distanceTo (const Vec3<T>& point) const IMATH_NOEXCEPT
112 {
113 return (closestPointTo (point) - point).length();
114 }
115
116 template <class T>
117 IMATH_HOSTDEVICE constexpr inline Vec3<T>
118 Line3<T>::closestPointTo (const Vec3<T>& point) const IMATH_NOEXCEPT
119 {
120 return ((point - pos) ^ dir) * dir + pos;
121 }
122
123 template <class T>
124 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
125 Line3<T>::distanceTo (const Line3<T>& line) const IMATH_NOEXCEPT
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>
133 Line3<T>::closestPointTo (const Line3<T>& line) const IMATH_NOEXCEPT
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>
169 operator* (const Line3<S>& line, const Matrix44<T>& M) IMATH_NOEXCEPT
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