8#ifndef META_OCEAN_MATH_LINE_3_H
9#define META_OCEAN_MATH_LINE_3_H
20template <
typename T>
class LineT3;
70 template <
typename U>
friend class LineT3;
242 explicit inline operator bool()
const;
266 direction_(direction)
288 ocean_assert(isValid());
289 return point_ + direction_ * distance;
308 direction_ = direction;
314 return !direction_.
isNull();
326 ocean_assert(isValid() && right.
isValid());
329 const T scalarProduct = direction_ * right.
direction_;
337 ocean_assert(isValid());
340 const T scalarProduct = direction_ * right;
348 ocean_assert(isValid() && right.
isValid());
350 return isParallel(right) && isOnLine(right.
point());
356 return !(*
this == right);
368 ocean_assert(isValid());
369 ocean_assert(hasUnitDirection());
372 const T length = offset.
length();
380 if (!std::is_same<T, float>::value)
394 ocean_assert(isValid());
395 ocean_assert(hasUnitDirection());
398 const float length = offset.
length();
417 ocean_assert(isValid());
418 ocean_assert(hasUnitDirection());
420 const VectorT3<T> pointOnLine(nearestPoint(point));
422 return (pointOnLine - point).length();
431 ocean_assert(isValid() && line.
isValid());
442 if (isParallel(line))
444 return (line.
point_ - point_ + direction_ * (direction_ * offset)).length();
457 ocean_assert(isValid());
458 ocean_assert(hasUnitDirection());
460 const VectorT3<T> pointOnLine(nearestPoint(point));
462 return (pointOnLine - point).sqr();
468 ocean_assert(isValid());
469 ocean_assert(hasUnitDirection());
473 return point_ + direction_ * (direction_ * offset);
479 ocean_assert(isValid() && line.
isValid());
483 if (nearestPoints(line, first, second))
485 middle = (first + second) * T(0.5);
495 ocean_assert(isValid() && line.
isValid());
498 if (isParallel(line))
506 const T denominator = d.
sqr();
513 const T factor = - (p * d) / denominator;
516 first = nearestPoint(second);
524 static_assert(std::is_same<float, T>::value || std::is_same<double, T>::value,
"Type T can be either float or double.");
525 ocean_assert(points !=
nullptr && size >= 2);
530 for (
size_t i = 0; i < size; ++i)
532 centroid += points[i];
535 ocean_assert(size != 0);
536 const T invSize = T(1) / T(size);
547 for (
size_t i = 0; i < size; ++i)
551 cxx += diff.
x() * diff.
x();
552 cxy += diff.
x() * diff.
y();
553 cxz += diff.
x() * diff.
z();
554 cyy += diff.
y() * diff.
y();
555 cyz += diff.
y() * diff.
z();
556 czz += diff.
z() * diff.
z();
560 const SquareMatrixT3<T> covarianceMatrix(cxx, cxy, cxz, cxy, cyy, cyz, cxz, cyz, czz);
576 constexpr unsigned int maxIterations = 100u;
578 for (
unsigned int iteration = 0u; iteration < maxIterations; ++iteration)
580 const VectorT3<T> newDirection = covarianceMatrix * direction;
581 const T length = newDirection.
length();
588 const VectorT3<T> normalizedDirection = newDirection / length;
593 direction = normalizedDirection;
597 direction = normalizedDirection;
This class implements an infinite line in 3D space.
Definition Line3.h:69
bool isParallel(const LineT3< T > &right) const
Returns whether two lines are parallel up to a small epsilon.
Definition Line3.h:324
static bool fitLineLeastSquare(const VectorT3< T > *points, const size_t size, LineT3< T > &line)
Fits a line to a set of given 3D points by application of the least square measure.
Definition Line3.h:522
bool operator==(const LineT3< T > &right) const
Returns whether two line are identical up to a small epsilon.
Definition Line3.h:346
void setPoint(const VectorT3< T > &point)
Sets a point of this line.
Definition Line3.h:299
VectorT3< T > direction_
Direction of the line.
Definition Line3.h:260
T Type
Definition of the used data type.
Definition Line3.h:77
T sqrDistance(const VectorT3< T > &point) const
Returns the square distance between the line and a given point.
Definition Line3.h:455
bool isOnLine(const VectorT3< T > &point) const
Returns whether a given point is part of the line.
Definition Line3.h:366
void setDirection(const VectorT3< T > &direction)
Sets the direction of this line.
Definition Line3.h:305
bool isValid() const
Returns whether this line has valid parameters.
Definition Line3.h:312
const VectorT3< T > & direction() const
Returns the direction of the line.
Definition Line3.h:293
bool nearestPoints(const LineT3< T > &line, VectorT3< T > &first, VectorT3< T > &second) const
Returns the two nearest points for two crossing lines.
Definition Line3.h:493
VectorT3< T > nearestPoint(const VectorT3< T > &point) const
Returns the point on this line nearest to an arbitrary given point.
Definition Line3.h:466
friend class LineT3
Definition Line3.h:70
const VectorT3< T > & point() const
Returns a point on the line.
Definition Line3.h:280
LineT3()=default
Creates an invalid line.
VectorT3< T > point_
Point on the line.
Definition Line3.h:257
T distance(const VectorT3< T > &point) const
Returns the distance between the line and a given point.
Definition Line3.h:415
bool hasUnitDirection() const
Returns whether this line has a unit vector as direction.
Definition Line3.h:318
bool operator!=(const LineT3< T > &right) const
Returns whether two line are identical up to a small epsilon.
Definition Line3.h:354
This class provides basic numeric functionalities.
Definition Numeric.h:57
static T abs(const T value)
Returns the absolute value of a given value.
Definition Numeric.h:1220
static bool isEqual(const T first, const T second)
Returns whether two values are equal up to a small epsilon.
Definition Numeric.h:2395
This class implements a 3x3 square matrix.
Definition SquareMatrix3.h:89
This class implements a vector with three elements.
Definition Vector3.h:97
const T & y() const noexcept
Returns the y value.
Definition Vector3.h:824
const T & x() const noexcept
Returns the x value.
Definition Vector3.h:812
VectorT3< T > normalized() const
Returns the normalized vector.
Definition Vector3.h:617
const T & z() const noexcept
Returns the z value.
Definition Vector3.h:836
bool isNull() const
Returns whether this vector is a null vector up to a small epsilon.
Definition Vector3.h:866
T length() const
Returns the length of the vector.
Definition Vector3.h:676
T sqr() const
Returns the square of the vector length.
Definition Vector3.h:682
std::vector< LineT3< T > > LinesT3
Definition of a typename alias for vectors with LineT3 objects.
Definition Line3.h:49
std::vector< Line3 > Lines3
Definition of a vector holding Line3 objects.
Definition Line3.h:56
The namespace covering the entire Ocean framework.
Definition Accessor.h:15