Atoms Crowd  7.0.0
ImathLine.h
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_ATOMSMATHLINE_H
11 #define INCLUDED_ATOMSMATHLINE_H
12 
13 #include <AtomsMath/ImathMatrix.h>
14 #include <AtomsMath/ImathNamespace.h>
15 #include <AtomsMath/ImathVec.h>
16 
17 ATOMSMATH_INTERNAL_NAMESPACE_HEADER_ENTER
18 
23 
24 template <class T> class Line3
25 {
26  public:
27 
30 
33 
36 
38 
41 
43  ATOMSMATH_HOSTDEVICE constexpr Line3() noexcept {}
44 
47  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Line3 (const Vec3<T>& point1, const Vec3<T>& point2) noexcept;
48 
50 
53 
56  ATOMSMATH_HOSTDEVICE void set (const Vec3<T>& point1, const Vec3<T>& point2) noexcept;
57 
59 
62 
65  ATOMSMATH_HOSTDEVICE constexpr Vec3<T> operator() (T parameter) const noexcept;
66 
68  ATOMSMATH_HOSTDEVICE constexpr T distanceTo (const Vec3<T>& point) const noexcept;
70  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T distanceTo (const Line3<T>& line) const noexcept;
71 
73  ATOMSMATH_HOSTDEVICE constexpr Vec3<T> closestPointTo (const Vec3<T>& point) const noexcept;
74 
76  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Vec3<T> closestPointTo (const Line3<T>& line) const noexcept;
77 
79 };
80 
82 typedef Line3<float> Line3f;
83 
85 typedef Line3<double> Line3d;
86 
87 template <class T> ATOMSMATH_CONSTEXPR14 inline Line3<T>::Line3 (const Vec3<T>& p0, const Vec3<T>& p1) noexcept
88 {
89  set (p0, p1);
90 }
91 
92 template <class T>
93 inline void
94 Line3<T>::set (const Vec3<T>& p0, const Vec3<T>& p1) noexcept
95 {
96  pos = p0;
97  dir = p1 - p0;
98  dir.normalize();
99 }
100 
101 template <class T>
102 constexpr inline Vec3<T>
103 Line3<T>::operator() (T parameter) const noexcept
104 {
105  return pos + dir * parameter;
106 }
107 
108 template <class T>
109 constexpr inline T
110 Line3<T>::distanceTo (const Vec3<T>& point) const noexcept
111 {
112  return (closestPointTo (point) - point).length();
113 }
114 
115 template <class T>
116 constexpr inline Vec3<T>
117 Line3<T>::closestPointTo (const Vec3<T>& point) const noexcept
118 {
119  return ((point - pos) ^ dir) * dir + pos;
120 }
121 
122 template <class T>
123 ATOMSMATH_CONSTEXPR14 inline T
124 Line3<T>::distanceTo (const Line3<T>& line) const noexcept
125 {
126  T d = (dir % line.dir) ^ (line.pos - pos);
127  return (d >= 0) ? d : -d;
128 }
129 
130 template <class T>
131 ATOMSMATH_CONSTEXPR14 inline Vec3<T>
132 Line3<T>::closestPointTo (const Line3<T>& line) const noexcept
133 {
134  // Assumes the lines are normalized
135 
136  Vec3<T> posLpos = pos - line.pos;
137  T c = dir ^ posLpos;
138  T a = line.dir ^ dir;
139  T f = line.dir ^ posLpos;
140  T num = c - a * f;
141 
142  T denom = a * a - 1;
143 
144  T absDenom = ((denom >= 0) ? denom : -denom);
145 
146  if (absDenom < 1)
147  {
148  T absNum = ((num >= 0) ? num : -num);
149 
150  if (absNum >= absDenom * std::numeric_limits<T>::max())
151  return pos;
152  }
153 
154  return pos + dir * (num / denom);
155 }
156 
158 template <class T>
159 std::ostream&
160 operator<< (std::ostream& o, const Line3<T>& line)
161 {
162  return o << "(" << line.pos << ", " << line.dir << ")";
163 }
164 
166 template <class S, class T>
167 constexpr inline Line3<S>
168 operator* (const Line3<S>& line, const Matrix44<T>& M) noexcept
169 {
170  return Line3<S> (line.pos * M, (line.pos + line.dir) * M);
171 }
172 
173 
178 
179 template <class T> class Line2
180 {
181 public:
182 
185 
188 
191 
192 
195 
197  ATOMSMATH_HOSTDEVICE constexpr Line2() noexcept {}
198 
201  ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Line2(const Vec2<T>& point1, const Vec2<T>& point2) noexcept;
202 
204 
207 
210  ATOMSMATH_HOSTDEVICE void set(const Vec2<T>& point1, const Vec2<T>& point2) noexcept;
211 
213 
216  ATOMSMATH_HOSTDEVICE constexpr Vec2<T> operator() (T parameter) const noexcept;
217 
219  ATOMSMATH_HOSTDEVICE constexpr Vec2<T> closestPointTo(const Vec2<T>& point) const noexcept;
220 };
221 
222 
224 typedef Line2<float> Line2f;
225 
227 typedef Line2<double> Line2d;
228 
229 template <class T> ATOMSMATH_CONSTEXPR14 inline Line2<T>::Line2(const Vec2<T>& p0, const Vec2<T>& p1) noexcept
230 {
231  set(p0, p1);
232 }
233 
234 template <class T>
235 inline void
236 Line2<T>::set(const Vec2<T>& p0, const Vec2<T>& p1) noexcept
237 {
238  pos = p0;
239  dir = p1 - p0;
240  dir.normalize();
241 }
242 
243 template <class T>
244 constexpr inline Vec2<T>
245 Line2<T>::operator() (T parameter) const noexcept
246 {
247  return pos + dir * parameter;
248 }
249 
250 template <class T>
251 constexpr inline Vec2<T>
252 Line2<T>::closestPointTo(const Vec2<T>& point) const noexcept
253 {
254  T t = dir.dot(point - pos);
255  return operator()(t);
256 }
257 
258 ATOMSMATH_INTERNAL_NAMESPACE_HEADER_EXIT
259 
260 #endif // INCLUDED_ATOMSMATHLINE_H
Definition: ImathLine.h:180
constexpr ATOMSMATH_HOSTDEVICE Vec2< T > closestPointTo(const Vec2< T > &point) const noexcept
Return the point on the line closest to the given point.
Definition: ImathLine.h:252
Vec2< T > pos
A point on the line.
Definition: ImathLine.h:187
ATOMSMATH_HOSTDEVICE void set(const Vec2< T > &point1, const Vec2< T > &point2) noexcept
Definition: ImathLine.h:236
constexpr ATOMSMATH_HOSTDEVICE Line2() noexcept
Uninitialized by default.
Definition: ImathLine.h:197
Vec2< T > dir
The direction of the line.
Definition: ImathLine.h:190
constexpr ATOMSMATH_HOSTDEVICE Vec2< T > operator()(T parameter) const noexcept
Definition: ImathLine.h:245
Definition: ImathLine.h:25
constexpr ATOMSMATH_HOSTDEVICE Vec3< T > operator()(T parameter) const noexcept
Definition: ImathLine.h:103
constexpr ATOMSMATH_HOSTDEVICE T distanceTo(const Vec3< T > &point) const noexcept
Return the distance to the given point.
Definition: ImathLine.h:110
ATOMSMATH_HOSTDEVICE void set(const Vec3< T > &point1, const Vec3< T > &point2) noexcept
Definition: ImathLine.h:94
constexpr ATOMSMATH_HOSTDEVICE Line3() noexcept
Uninitialized by default.
Definition: ImathLine.h:43
Vec3< T > pos
A point on the line.
Definition: ImathLine.h:32
constexpr ATOMSMATH_HOSTDEVICE Vec3< T > closestPointTo(const Vec3< T > &point) const noexcept
Return the point on the line closest to the given point.
Definition: ImathLine.h:117
Vec3< T > dir
The direction of the line.
Definition: ImathLine.h:35
Definition: ImathMatrix.h:631
Definition: ImathVec.h:43
Definition: ImathVec.h:260