14#include <lagrange/common.h>
43template <
typename Po
intType>
45 const Eigen::MatrixBase<PointType>& U0,
46 const Eigen::MatrixBase<PointType>& U1,
47 const Eigen::MatrixBase<PointType>& V0,
48 const Eigen::MatrixBase<PointType>& V1,
49 Eigen::PlainObjectBase<PointType>& closest_pointU,
50 Eigen::PlainObjectBase<PointType>& closest_pointV,
51 ScalarOf<PointType>& lambdaU,
52 ScalarOf<PointType>& lambdaV) -> ScalarOf<PointType>
54 using Scalar = ScalarOf<PointType>;
55 using Vector =
typename PointType::PlainObject;
57 constexpr Scalar ZERO =
static_cast<Scalar
>(0);
58 constexpr Scalar ONE =
static_cast<Scalar
>(1);
59 constexpr Scalar EPS = std::numeric_limits<Scalar>::epsilon();
62 constexpr bool is_lineU =
false;
63 constexpr bool is_lineV =
false;
68 Scalar a = d1.squaredNorm();
69 Scalar e = d2.squaredNorm();
73 if (std::abs(a) < EPS) {
74 if (std::abs(e) < EPS) {
76 lambdaU = lambdaV = 0;
79 return (closest_pointU - closest_pointV).dot(closest_pointU - closest_pointV);
85 if (!is_lineV) lambdaV = std::clamp(lambdaV, ZERO, ONE);
89 if (std::abs(e) < EPS) {
95 lambdaU = std::clamp(lambdaU, ZERO, ONE);
98 Scalar b = d1.dot(d2);
99 Scalar denom = a * e - b * b;
103 if (std::abs(denom) >= EPS) {
104 lambdaU = (b * f - c * e) / denom;
106 if (!is_lineU) lambdaU = std::clamp(lambdaU, ZERO, ONE);
112 lambdaV = (b * lambdaU + f) / e;
123 if (!is_lineU) lambdaU = std::clamp(lambdaU, ZERO, ONE);
124 }
else if (lambdaV > 1) {
126 lambdaU = (b - c) / a;
128 if (!is_lineU) lambdaU = std::clamp(lambdaU, ZERO, ONE);
134 closest_pointU = U0 + lambdaU * d1;
135 closest_pointV = V0 + lambdaV * d2;
136 return (closest_pointU - closest_pointV).squaredNorm();
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Type alias for one-dimensional column Eigen vectors.
Definition: views.h:79
Main namespace for Lagrange.
Definition: AABBIGL.h:30
auto segment_segment_squared_distance(const Eigen::MatrixBase< PointType > &U0, const Eigen::MatrixBase< PointType > &U1, const Eigen::MatrixBase< PointType > &V0, const Eigen::MatrixBase< PointType > &V1, Eigen::PlainObjectBase< PointType > &closest_pointU, Eigen::PlainObjectBase< PointType > &closest_pointV, ScalarOf< PointType > &lambdaU, ScalarOf< PointType > &lambdaV) -> ScalarOf< PointType >
Computes the squared distance between two N-d line segments, and the closest pair of points whose sep...
Definition: segment_segment_squared_distance.h:44