Ocean
FiniteLine3.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MATH_FINITE_LINE_3_H
9 #define META_OCEAN_MATH_FINITE_LINE_3_H
10 
11 #include "ocean/math/Math.h"
12 #include "ocean/math/Line3.h"
13 #include "ocean/math/Vector3.h"
14 
15 #include <vector>
16 
17 namespace Ocean
18 {
19 
20 // Forward declaration.
21 template <typename T> class FiniteLineT3;
22 
23 /**
24  * Definition of the FiniteLine3 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single or double precision float data type.
25  * @see FiniteLineT3
26  * @ingroup math
27  */
29 
30 /**
31  * Instantiation of the LineT3 template class using a double precision float data type.
32  * @see FiniteLineT3
33  * @ingroup math
34  */
36 
37 /**
38  * Instantiation of the LineT3 template class using a single precision float data type.
39  * @see FiniteLineT3
40  * @ingroup math
41  */
43 
44 /**
45  * Definition of a typename alias for vectors with FiniteLineT3 objects.
46  * @see FiniteLineT3
47  * @ingroup math
48  */
49 template <typename T>
50 using FiniteLinesT3 = std::vector<FiniteLineT3<T>>;
51 
52 /**
53  * Definition of a vector holding FiniteLine3 objects.
54  * @see Line3
55  * @ingroup math
56  */
57 typedef std::vector<FiniteLine3> FiniteLines3;
58 
59 /**
60  * This class implements an finite line in 3D space.
61  * The finite line object is invalid if both end points of the line object are identical.<br>
62  * @tparam T Data type used to represent lines
63  * @see FiniteLine3, FiniteLineF3, FiniteLineD3, Line3, Line3
64  * @ingroup math
65  */
66 template <typename T>
68 {
69  public:
70 
71  /**
72  * Definition of the used data type.
73  */
74  typedef T Type;
75 
76  public:
77 
78  /**
79  * Creates a finite line with default parameters.
80  */
81  FiniteLineT3();
82 
83  /**
84  * Creates a finite line defined by two end points of the line.
85  * @param point0 First end point of the line
86  * @param point1 Second end point of the line, must be different from point0 to create a valid line, otherwise the line is invalid
87  */
89 
90  /**
91  * Copies a line with different data type than T.
92  * @param line The line to copy
93  * @tparam U The data type of the second line
94  */
95  template <typename U>
96  inline explicit FiniteLineT3(const FiniteLineT3<U>& line);
97 
98  /**
99  * Returns the first end point of the line.
100  * @return First end point of the line
101  */
102  inline const VectorT3<T>& point0() const;
103 
104  /**
105  * Returns the second end point of the line.
106  * @return Second end point of the line
107  */
108  inline const VectorT3<T>& point1() const;
109 
110  /**
111  * Returns the first or second end point of the line.
112  * @param index The index of the point to be returned, with range [0, 1]
113  * @return First or second end point of the line
114  */
115  inline const VectorT3<T>& point(const unsigned int index) const;
116 
117  /**
118  * Returns the midpoint of the line.
119  * @return Midpoint point of the line
120  */
121  inline VectorT3<T> midpoint() const;
122 
123  /**
124  * Returns the direction of the line: normalize(point1() - point0())
125  * @return Direction vector with unit length, a zero vector if the line is invalid
126  * @see isValid().
127  */
128  inline const VectorT3<T>& direction() const;
129 
130  /**
131  * Returns whether a given point is part of the finite line.
132  * @param point Point to check
133  * @return True, if so
134  */
135  inline bool isOnLine(const VectorT3<T>& point) const;
136 
137  /**
138  * Returns the distance between the line and a given point.
139  * This function needs a unit vector as direction!
140  * @param point Point to return the distance for
141  * @return Distance between point and line
142  */
143  inline T distance(const VectorT3<T>& point) const;
144 
145  /**
146  * Returns the square distance between the line and a given point.
147  * @param point Point to return the distance for
148  * @return Square distance between point and line
149  */
150  inline T sqrDistance(const VectorT3<T>& point) const;
151 
152  /**
153  * Returns the point on this line nearest to an arbitrary given point.
154  * This function needs a unit vector as direction!
155  * @param point Arbitrary point outside the line
156  * @return Nearest point on the line
157  */
159 
160  /**
161  * Returns the intersection pointer of two finite lines.
162  * @param right Right line for intersection calculation
163  * @param point Resulting intersection pointer
164  * @return True, if both lines have a common intersection point
165  */
166  inline bool intersection(const FiniteLineT3<T>& right, VectorT3<T>& point) const;
167 
168  /**
169  * Returns whether two lines are parallel up to a small epsilon.
170  * @param right Second line
171  * @return True, if so
172  */
173  inline bool isParallel(const FiniteLineT3<T>& right) const;
174 
175  /**
176  * Returns whether this line has valid parameters.
177  * @return True, if so
178  */
179  inline bool isValid() const;
180 
181  /**
182  * Returns whether two line are identical up to a small epsilon.
183  * Two finite lines are identical if both lines have the same endpoint (independent of the order of the end points).<br>
184  * @param right Right line
185  * @return True, if so
186  */
187  inline bool operator==(const FiniteLineT3<T>& right) const;
188 
189  /**
190  * Returns whether two line are identical up to a small epsilon.
191  * @param right Right line
192  * @return True, if so
193  */
194  inline bool operator!=(const FiniteLineT3<T>& right) const;
195 
196  /**
197  * Returns whether this line is valid.
198  * @return True, if so
199  */
200  explicit inline operator bool() const;
201 
202  protected:
203 
204  /// First end point of the line.
206 
207  /// Second end point of the line.
209 
210  /// Direction of the line with unit length, if the object holds valid parameters.
212 };
213 
214 template <typename T>
216  linePoint0(T(0), T(0), T(0)),
217  linePoint1(T(0), T(0), T(0)),
218  lineDirection(T(0), T(0), T(0))
219 {
220  // nothing to do here
221 }
222 
223 template <typename T>
225  linePoint0(point0),
226  linePoint1(point1),
227  lineDirection((point1 - point0).normalizedOrZero())
228 {
229  // nothing to do here
230 }
231 
232 template <typename T>
233 template <typename U>
235 {
236  linePoint0 = VectorT3<T>(line.linePoint0);
237  linePoint1 = VectorT3<T>(line.linePoint1);
238  lineDirection = VectorT3<T>(line.lineDirection);
239 }
240 
241 template <typename T>
243 {
244  return linePoint0;
245 }
246 
247 template <typename T>
249 {
250  return linePoint1;
251 }
252 
253 template <typename T>
254 inline const VectorT3<T>& FiniteLineT3<T>::point(const unsigned int index) const
255 {
256  ocean_assert(index <= 1u);
257 
258  if (index == 0u)
259  {
260  return linePoint0;
261  }
262  else
263  {
264  return linePoint1;
265  }
266 }
267 
268 template <typename T>
270 {
271  return (linePoint0 + linePoint1) * T(0.5);
272 }
273 
274 template <typename T>
276 {
277  return lineDirection;
278 }
279 
280 template <typename T>
281 inline bool FiniteLineT3<T>::isOnLine(const VectorT3<T>& point) const
282 {
283  ocean_assert(isValid());
284 
285  return NumericT<T>::isEqualEps(sqrDistance(point));
286 }
287 
288 template <typename T>
289 inline T FiniteLineT3<T>::distance(const VectorT3<T>& point) const
290 {
291  ocean_assert(isValid());
292 
293  return NumericT<T>::sqrt(sqrDistance(point));
294 }
295 
296 template <typename T>
297 inline T FiniteLineT3<T>::sqrDistance(const VectorT3<T>& point) const
298 {
299  ocean_assert(isValid());
300 
301  return nearestPoint(point).sqrDistance(point);
302 }
303 
304 template <typename T>
306 {
307  ocean_assert(isValid());
308 
309  const VectorT3<T> lineOffset(linePoint1 - linePoint0);
310  const VectorT3<T> pointOffset(point - linePoint0);
311 
312  const Scalar dotProduct = lineOffset * pointOffset;
313 
314  // the projected point does not lie on the finite line (before the first end point)
315  if (dotProduct <= 0)
316  {
317  return linePoint0;
318  }
319 
320  // the projected point does not lie on the finite line (behind the second end point)
321  if (dotProduct >= lineOffset.sqr())
322  {
323  return linePoint1;
324  }
325 
326  // the projected point lies on the finite line
327  return linePoint0 + lineDirection * (pointOffset * lineDirection);
328 }
329 
330 template <typename T>
331 inline bool FiniteLineT3<T>::intersection(const FiniteLineT3<T>& right, VectorT3<T>& point) const
332 {
333  return LineT3<T>(linePoint0, lineDirection).intersection(LineT3<T>(right.linePoint0, right.lineDirection), point) && isOnLine(point);
334 }
335 
336 template <typename T>
337 inline bool FiniteLineT3<T>::isParallel(const FiniteLineT3<T>& right) const
338 {
339  ocean_assert(isValid() && right.isValid());
340 
341  return lineDirection == right.lineDirection || lineDirection == -right.lineDirection;
342 }
343 
344 template <typename T>
345 inline bool FiniteLineT3<T>::isValid() const
346 {
347  return !lineDirection.isNull();
348 }
349 
350 template <typename T>
352 {
353  return (linePoint0 == right.linePoint0 && linePoint1 == right.linePoint1)
354  || (linePoint0 == right.linePoint1 && linePoint1 == right.linePoint0);
355 }
356 
357 template <typename T>
358 inline bool FiniteLineT3<T>::operator!=(const FiniteLineT3<T>& right) const
359 {
360  return !(*this == right);
361 }
362 
363 template <typename T>
364 inline FiniteLineT3<T>::operator bool() const
365 {
366  return isValid();
367 }
368 
369 }
370 
371 #endif // META_OCEAN_MATH_FINITE_LINE_3_H
This class implements an finite line in 3D space.
Definition: FiniteLine3.h:68
bool operator!=(const FiniteLineT3< T > &right) const
Returns whether two line are identical up to a small epsilon.
Definition: FiniteLine3.h:358
VectorT3< T > midpoint() const
Returns the midpoint of the line.
Definition: FiniteLine3.h:269
VectorT3< T > linePoint0
First end point of the line.
Definition: FiniteLine3.h:205
const VectorT3< T > & point1() const
Returns the second end point of the line.
Definition: FiniteLine3.h:248
bool intersection(const FiniteLineT3< T > &right, VectorT3< T > &point) const
Returns the intersection pointer of two finite lines.
Definition: FiniteLine3.h:331
bool isParallel(const FiniteLineT3< T > &right) const
Returns whether two lines are parallel up to a small epsilon.
Definition: FiniteLine3.h:337
bool operator==(const FiniteLineT3< T > &right) const
Returns whether two line are identical up to a small epsilon.
Definition: FiniteLine3.h:351
bool isValid() const
Returns whether this line has valid parameters.
Definition: FiniteLine3.h:345
VectorT3< T > nearestPoint(const VectorT3< T > &point) const
Returns the point on this line nearest to an arbitrary given point.
Definition: FiniteLine3.h:305
T sqrDistance(const VectorT3< T > &point) const
Returns the square distance between the line and a given point.
Definition: FiniteLine3.h:297
bool isOnLine(const VectorT3< T > &point) const
Returns whether a given point is part of the finite line.
Definition: FiniteLine3.h:281
const VectorT3< T > & point(const unsigned int index) const
Returns the first or second end point of the line.
Definition: FiniteLine3.h:254
const VectorT3< T > & direction() const
Returns the direction of the line: normalize(point1() - point0())
Definition: FiniteLine3.h:275
FiniteLineT3()
Creates a finite line with default parameters.
Definition: FiniteLine3.h:215
VectorT3< T > linePoint1
Second end point of the line.
Definition: FiniteLine3.h:208
const VectorT3< T > & point0() const
Returns the first end point of the line.
Definition: FiniteLine3.h:242
VectorT3< T > lineDirection
Direction of the line with unit length, if the object holds valid parameters.
Definition: FiniteLine3.h:211
T Type
Definition of the used data type.
Definition: FiniteLine3.h:74
T distance(const VectorT3< T > &point) const
Returns the distance between the line and a given point.
Definition: FiniteLine3.h:289
This class implements an infinite line in 3D space.
Definition: Line3.h:70
static T sqrt(const T value)
Returns the square root of a given value.
Definition: Numeric.h:1533
static constexpr bool isEqualEps(const T value)
Returns whether a value is smaller than or equal to a small epsilon.
Definition: Numeric.h:2087
This class implements a vector with three elements.
Definition: Vector3.h:97
T sqr() const
Returns the square of the vector length.
Definition: Vector3.h:670
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition: base/Utilities.h:1089
FiniteLineT3< Scalar > FiniteLine3
Definition of the FiniteLine3 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with si...
Definition: FiniteLine3.h:21
std::vector< FiniteLineT3< T > > FiniteLinesT3
Definition of a typename alias for vectors with FiniteLineT3 objects.
Definition: FiniteLine3.h:50
float Scalar
Definition of a scalar type.
Definition: Math.h:128
FiniteLineT3< float > FiniteLineF3
Instantiation of the LineT3 template class using a single precision float data type.
Definition: FiniteLine3.h:42
FiniteLineT3< double > FiniteLineD3
Instantiation of the LineT3 template class using a double precision float data type.
Definition: FiniteLine3.h:35
std::vector< FiniteLine3 > FiniteLines3
Definition of a vector holding FiniteLine3 objects.
Definition: FiniteLine3.h:57
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15