92 lines
3.7 KiB
C++
92 lines
3.7 KiB
C++
#pragma once
|
|
|
|
#include <J3ML/LinearAlgebra/Vector3.h>
|
|
#include "Plane.h"
|
|
#include <J3ML/Geometry/Common.h>
|
|
|
|
namespace J3ML::Geometry
|
|
{
|
|
|
|
/// Clamps the given input value to the range [min, max].
|
|
/** @see Clamp01(), Min(), Max(). */
|
|
template<typename T>
|
|
T Clamp(const T &val, const T &floor, const T &ceil)
|
|
{
|
|
assert(floor <= ceil);
|
|
return val <= ceil ? (val >= floor ? val : floor) : ceil;
|
|
}
|
|
|
|
/// Clamps the given input value to the range [0, 1].
|
|
/** @see Clamp(), Min(), Max(). */
|
|
template<typename T>
|
|
T Clamp01(const T &val) { return Clamp(val, T(0), T(1)); }
|
|
|
|
using LinearAlgebra::Vector3;
|
|
class LineSegment
|
|
{
|
|
public:
|
|
Vector3 A;
|
|
Vector3 B;
|
|
public:
|
|
LineSegment();
|
|
LineSegment(const Vector3& a, const Vector3& b);
|
|
|
|
/// Computes the closest point on this line segment to the given object.
|
|
/** @param d [out] If specified, this parameter receives the normalized distance along
|
|
this line segment which specifies the closest point on this line segment to
|
|
the specified point.
|
|
@return The closest point on this line segment to the given object.
|
|
@see Contains(), Distance(), Intersects(). */
|
|
Vector3 ClosestPoint(const Vector3 &point) const;
|
|
bool Contains(const Vector3& point, float distanceThreshold = 1e-3f) const;
|
|
|
|
|
|
/// Quickly returns an arbitrary point inside this LineSegment. Used in GJK intersection test.
|
|
inline Vector3 AnyPointFast() const { return A; }
|
|
|
|
/// Computes an extreme point of this LineSegment in the given direction.
|
|
/** An extreme point is a farthest point along this LineSegment in the given direction. Given a direction,
|
|
this point is not necessarily unique.
|
|
@param direction The direction vector of the direction to find the extreme point. This vector may
|
|
be unnormalized, but may not be null.
|
|
@return An extreme point of this LineSegment in the given direction. The returned point is always
|
|
either a or b.
|
|
@see a, b.*/
|
|
Vector3 ExtremePoint(const Vector3 &direction) const;
|
|
Vector3 ExtremePoint(const Vector3 &direction, float &projectionDistance) const;
|
|
|
|
void ProjectToAxis(const Vector3 &direction, float &outMin, float &outMax) const;
|
|
Vector3 GetPoint(float d) const;
|
|
Vector3 ClosestPoint(const Vector3 &point, float &d) const;
|
|
Vector3 ClosestPoint(const Ray &other, float &d, float &d2) const;
|
|
float Distance(const Vector3 &point) const;
|
|
float DistanceSq(const Vector3& point) const;
|
|
float Distance(const Vector3 &point, float &d) const;
|
|
float Distance(const Ray &other) const;
|
|
float Distance(const Ray &other, float &d) const;
|
|
float Distance(const Ray &other, float &d, float &d2) const;
|
|
float Distance(const Line &other) const;
|
|
float Distance(const Line &other, float &d) const;
|
|
float Distance(const Line &other, float &d, float &d2) const;
|
|
float Distance(const LineSegment &other) const;
|
|
float Distance(const LineSegment &other, float &d) const;
|
|
float Distance(const LineSegment &other, float &d, float &d2) const;
|
|
float Distance(const Plane& other) const;
|
|
|
|
Vector3 Dir() const;
|
|
|
|
float Length() const;
|
|
|
|
float LengthSq() const;
|
|
|
|
float DistanceSq(const LineSegment &other) const;
|
|
|
|
Vector3 ClosestPoint(const LineSegment &other, float &d, float &d2) const;
|
|
|
|
Vector3 ClosestPoint(const Line &other, float &d, float &d2) const;
|
|
|
|
bool Intersects(const LineSegment &segment) const;
|
|
};
|
|
|
|
LineSegment operator *(const Matrix4x4 &transform, const LineSegment &l);
|
|
} |