8 #ifndef META_OCEAN_MATH_FINITE_LINE_2_H
9 #define META_OCEAN_MATH_FINITE_LINE_2_H
21 template <
typename T>
class FiniteLineT2;
109 template <
typename U>
315 explicit inline operator bool()
const;
329 template <
typename T>
333 direction_((point1 - point0).normalizedOrZero())
338 template <
typename T>
339 template <
typename U>
347 template <
typename T>
353 template <
typename T>
359 template <
typename T>
362 ocean_assert(index <= 1u);
374 template <
typename T>
377 return (point0_ + point1_) * T(0.5);
380 template <
typename T>
386 template <
typename T>
389 ocean_assert(isValid());
392 ocean_assert(result.
cross(direction_) > 0);
396 template <
typename T>
399 return (point1_ - point0_).sqr();
402 template <
typename T>
405 return (point1_ - point0_).length();
408 template <
typename T>
411 ocean_assert(isValid());
416 template <
typename T>
419 ocean_assert(isValid());
424 template <
typename T>
427 ocean_assert(isValid());
432 template <
typename T>
435 ocean_assert(isValid());
440 template <
typename T>
443 ocean_assert(isValid());
445 return nearestPoint(point).sqrDistance(point);
448 template <
typename T>
451 ocean_assert(isValid());
456 const T dotProduct = lineOffset * pointOffset;
465 if (dotProduct >= lineOffset.
sqr())
471 return point0_ + direction_ * (pointOffset * direction_);
474 template <
typename T>
477 ocean_assert(isValid());
482 const T dotProduct = lineOffset * pointOffset;
488 if (outOfBoundaryDistance)
492 *outOfBoundaryDistance = std::min(pointOffset * direction_, T(0));
493 ocean_assert(*outOfBoundaryDistance <= T(0));
496 else if (dotProduct >= lineOffset.
sqr())
500 if (outOfBoundaryDistance)
504 *outOfBoundaryDistance = std::max(T(0), pointOffset * direction_ - length());
505 ocean_assert(*outOfBoundaryDistance >= T(0));
512 if (outOfBoundaryDistance)
514 *outOfBoundaryDistance = T(0);
518 const T length = pointOffset * direction_;
520 if (finiteLineLocation)
525 *finiteLineLocation = length;
529 return point0_ + direction_ * length;
532 template <
typename T>
535 ocean_assert(isValid() && second.
isValid());
544 const T lengthOnThisLine = direction_ * (point - point0_);
555 template <
typename T>
558 ocean_assert(isValid() && second.
isValid());
559 if (!
LineT2<T>(point0_, direction_).intersection(second, point))
567 const T lengthOnThisLine = direction_ * (point - point0_);
569 if (lengthOnThisLine < T(0) ||
NumericT<T>::sqr(lengthOnThisLine) > sqrLength())
577 template <
typename T>
580 ocean_assert(isValid() && second.
isValid());
583 return intersection(second, dummyPoint);
586 template <
typename T>
589 ocean_assert(isValid() && second.
isValid());
592 return intersection(second, dummyPoint);
595 template <
typename T>
598 ocean_assert(isValid() && right.
isValid());
603 template <
typename T>
606 ocean_assert(isValid() && right.
isValid());
610 const T squareDistanceEpsilon = distanceEpsilon * distanceEpsilon;
611 const bool validDistanceRightToThis = (nearestPointOnInfiniteLine(right.
point0()) - right.
point0()).
sqr() <= squareDistanceEpsilon && (nearestPointOnInfiniteLine(right.
point1()) - right.
point1()).
sqr() <= squareDistanceEpsilon;
615 const T cosAngle = normal() * right.
normal();
616 const bool validNormalOrientations =
NumericT<T>::abs(cosAngle) >= cosAngleEpsilon;
618 return validDistanceRightToThis && validDistanceThisToRight && validNormalOrientations;
621 template <
typename T>
624 return !direction_.
isNull();
627 template <
typename T>
630 ocean_assert(isValid() && line.
isValid());
638 template <
typename T>
645 template <
typename T>
648 return !(*
this == right);
651 template <
typename T>
bool isLeftOfLine(const VectorT2< T > &point) const
Check if a point is in the left half-plane of the direction vector of a line A point p is located on ...
Definition: FiniteLine2.h:425
const VectorT2< T > & point(const unsigned int index) const
Returns the first or second end point of the line.
Definition: FiniteLine2.h:360
FiniteLineT2(const FiniteLineT2< U > &line)
Copies a line with different data type than T.
Definition: FiniteLine2.h:340
bool isValid() const
Returns whether this line has valid parameters.
Definition: FiniteLine2.h:622
bool isEqual(const FiniteLineT2< T > &line, const T &epsilon) const
Returns whether two lines are equal up to a specified epsilon.
Definition: FiniteLine2.h:628
VectorT2< T > direction_
Direction of the line with unit length, if the object holds valid parameters.
Definition: FiniteLine2.h:326
bool intersects(const LineT2< T > &second) const
Returns whether two finite lies have an intersection.
Definition: FiniteLine2.h:587
bool intersection(const FiniteLineT2< T > &second, VectorT2< T > &point) const
Returns the unique intersection point of two finite lines.
Definition: FiniteLine2.h:533
FiniteLineT2()=default
Creates a finite line with default parameters.
T length() const
Returns the length of the finite line.
Definition: FiniteLine2.h:403
bool operator!=(const FiniteLineT2< T > &right) const
Returns whether two line are identical up to a small epsilon.
Definition: FiniteLine2.h:646
VectorT2< T > point1_
Second end point of the line.
Definition: FiniteLine2.h:323
bool isOnLine(const VectorT2< T > &point) const
Returns whether a given point is part of the finite line.
Definition: FiniteLine2.h:409
T Type
Definition of the used data type.
Definition: FiniteLine2.h:88
bool isOnInfiniteLine(const VectorT2< T > &point) const
Returns whether a given point lies on the infinite line defined by this finite line.
Definition: FiniteLine2.h:417
bool isParallel(const FiniteLineT2< T > &right) const
Returns whether two lines are parallel up to a small epsilon.
Definition: FiniteLine2.h:596
const VectorT2< T > & direction() const
Returns the direction of the line: normalized(point1() - point0())
Definition: FiniteLine2.h:381
FiniteLineT2(const VectorT2< T > &point0, const VectorT2< T > &point1)
Creates a finite line defined by two end points of the line.
Definition: FiniteLine2.h:330
bool intersects(const FiniteLineT2< T > &second) const
Returns whether two finite lines have a unique intersection point.
Definition: FiniteLine2.h:578
bool isCollinear(const FiniteLineT2< T > &right, const T &distanceEpsilon=NumericT< T >::weakEps(), const T &cosAngleEpsilon=NumericT< T >::cos(NumericT< T >::weakEps())) const
Check for collinearity with other line segment.
Definition: FiniteLine2.h:604
T distance(const VectorT2< T > &point) const
Returns the distance between the line and a given point.
Definition: FiniteLine2.h:433
const VectorT2< T > & point0() const
Returns the first end point of the line.
Definition: FiniteLine2.h:348
bool operator==(const FiniteLineT2< T > &right) const
Returns whether two line are identical up to a small epsilon.
Definition: FiniteLine2.h:639
T sqrDistance(const VectorT2< T > &point) const
Returns the square distance between the line and a given point.
Definition: FiniteLine2.h:441
const VectorT2< T > normal() const
Returns the normal of the line: -direction().perpendicular() The 2D cross product between the resulti...
Definition: FiniteLine2.h:387
VectorT2< T > nearestPoint(const VectorT2< T > &point) const
Returns the point lying on this finite line nearest to an arbitrary given point.
Definition: FiniteLine2.h:449
VectorT2< T > nearestPointOnInfiniteLine(const VectorT2< T > &point, T *outOfBoundaryDistance=nullptr, T *finiteLineLocation=nullptr) const
Returns the point on the infinite line (defined by this finite line) to an arbitrary given point.
Definition: FiniteLine2.h:475
VectorT2< T > point0_
First end point of the line.
Definition: FiniteLine2.h:320
const VectorT2< T > & point1() const
Returns the second end point of the line.
Definition: FiniteLine2.h:354
bool intersection(const LineT2< T > &second, VectorT2< T > &point) const
Returns the unique intersection point of this finite line with an infinite line.
Definition: FiniteLine2.h:556
VectorT2< T > midpoint() const
Returns the midpoint of the line.
Definition: FiniteLine2.h:375
T sqrLength() const
Returns the squared length of the finite line.
Definition: FiniteLine2.h:397
This class implements an infinite line in 2D space.
Definition: Line2.h:83
bool isValid() const
Returns whether this line has valid parameters.
Definition: Line2.h:405
bool isLeftOfLine(const VectorT2< T > &point) const
Check if a point is in the left half-plane of the direction vector of a line A point p is located on ...
Definition: Line2.h:455
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 T sqrt(const T value)
Returns the square root of a given value.
Definition: Numeric.h:1533
static constexpr T sqr(const T value)
Returns the square of a given value.
Definition: Numeric.h:1495
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 two elements.
Definition: Vector2.h:96
T sqr() const
Returns the square of the vector length.
Definition: Vector2.h:621
bool isNull() const
Returns whether this vector is a null vector up to a small epsilon.
Definition: Vector2.h:734
T sqrDistance(const VectorT2< T > &right) const
Returns the square distance between this 2D position and a second 2D position.
Definition: Vector2.h:633
T length() const
Returns the length of the vector.
Definition: Vector2.h:615
T cross(const VectorT2< T > &vector) const
Returns the cross product of two 2D vectors.
Definition: Vector2.h:544
VectorT2< T > perpendicular() const
Returns a vector perpendicular to this vectors.
Definition: Vector2.h:550
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition: base/Utilities.h:1089
unsigned int sqr(const char value)
Returns the square value of a given value.
Definition: base/Utilities.h:1029
FiniteLineT2< Scalar > FiniteLine2
Definition of the FiniteLine2 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with si...
Definition: FiniteLine2.h:21
std::vector< FiniteLineD2 > FiniteLinesD2
Definition of a vector holding FiniteLineD2 objects.
Definition: FiniteLine2.h:64
FiniteLineT2< float > FiniteLineF2
Instantiation of the LineT2 template class using a single precision float data type.
Definition: FiniteLine2.h:42
std::vector< FiniteLine2 > FiniteLines2
Definition of a vector holding FiniteLine2 objects.
Definition: FiniteLine2.h:57
std::vector< FiniteLineF2 > FiniteLinesF2
Definition of a vector holding FiniteLineF2 objects.
Definition: FiniteLine2.h:71
std::vector< FiniteLineT2< T > > FiniteLinesT2
Definition of a typename alias for vectors with FiniteLineT2 objects.
Definition: FiniteLine2.h:50
FiniteLineT2< double > FiniteLineD2
Instantiation of the LineT2 template class using a double precision float data type.
Definition: FiniteLine2.h:35
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15