52#include <lagrange/common.h>
53#include <lagrange/utils/point_segment_squared_distance.h>
76template <
typename Po
intType>
78 const Eigen::MatrixBase<PointType>& point,
79 const Eigen::MatrixBase<PointType>& V0,
80 const Eigen::MatrixBase<PointType>& V1,
81 const Eigen::MatrixBase<PointType>& V2,
82 Eigen::PlainObjectBase<PointType>& closest_point,
83 ScalarOf<PointType>& lambda0,
84 ScalarOf<PointType>& lambda1,
85 ScalarOf<PointType>& lambda2) -> ScalarOf<PointType>
87 using Scalar = ScalarOf<PointType>;
89 Eigen::Vector3d diff = V0.template cast<double>() - point.template cast<double>();
90 Eigen::Vector3d edge0 = V1.template cast<double>() - V0.template cast<double>();
91 Eigen::Vector3d edge1 = V2.template cast<double>() - V0.template cast<double>();
92 double a00 = edge0.squaredNorm();
93 double a01 = edge0.dot(edge1);
94 double a11 = edge1.squaredNorm();
95 double b0 = diff.dot(edge0);
96 double b1 = diff.dot(edge1);
97 double c = diff.squaredNorm();
98 double det = std::fabs(a00 * a11 - a01 * a01);
99 double s = a01 * b1 - a11 * b0;
100 double t = a01 * b0 - a00 * b1;
105 Scalar cur_l1, cur_l2;
106 PointType cur_closest;
111 closest_point = cur_closest;
116 if (cur_dist < result) {
118 closest_point = cur_closest;
124 if (cur_dist < result) {
126 closest_point = cur_closest;
141 sqr_distance = a00 + 2 * b0 + c;
144 sqr_distance = b0 * s + c;
151 }
else if (-b1 >= a11) {
153 sqr_distance = a11 + 2 * b1 + c;
156 sqr_distance = b1 * t + c;
164 }
else if (-b1 >= a11) {
166 sqr_distance = a11 + 2 * b1 + c;
169 sqr_distance = b1 * t + c;
177 }
else if (-b0 >= a00) {
179 sqr_distance = a00 + 2 * b0 + c;
182 sqr_distance = b0 * s + c;
186 double inv_det = double(1) / det;
189 sqr_distance = s * (a00 * s + a01 * t + 2 * b0) + t * (a01 * s + a11 * t + 2 * b1) + c;
192 double tmp0, tmp1, numer, denom;
199 denom = a00 - 2 * a01 + a11;
200 if (numer >= denom) {
203 sqr_distance = a00 + 2 * b0 + c;
208 s * (a00 * s + a01 * t + 2 * b0) + t * (a01 * s + a11 * t + 2 * b1) + c;
214 sqr_distance = a11 + 2 * b1 + c;
215 }
else if (b1 >= 0) {
220 sqr_distance = b1 * t + c;
228 denom = a00 - 2 * a01 + a11;
229 if (numer >= denom) {
232 sqr_distance = a11 + 2 * b1 + c;
237 s * (a00 * s + a01 * t + 2 * b0) + t * (a01 * s + a11 * t + 2 * b1) + c;
243 sqr_distance = a00 + 2 * b0 + c;
244 }
else if (b0 >= 0) {
249 sqr_distance = b0 * s + c;
253 numer = a11 + b1 - a01 - b0;
257 sqr_distance = a11 + 2 * b1 + c;
259 denom = a00 - 2 * a01 + a11;
260 if (numer >= denom) {
263 sqr_distance = a00 + 2 * b0 + c;
268 s * (a00 * s + a01 * t + 2 * b0) + t * (a01 * s + a11 * t + 2 * b1) + c;
275 if (sqr_distance < 0) {
279 closest_point = (V0.template cast<double>() + s * edge0 + t * edge1).
template cast<Scalar>();
280 lambda0 = Scalar(1 - s - t);
283 return Scalar(sqr_distance);
299template <
typename Po
intType>
301 const Eigen::MatrixBase<PointType>& point,
302 const Eigen::MatrixBase<PointType>& V0,
303 const Eigen::MatrixBase<PointType>& V1,
304 const Eigen::MatrixBase<PointType>& V2) -> ScalarOf<PointType>
306 PointType closest_point;
307 ScalarOf<PointType> lambda1, lambda2, lambda3;
Main namespace for Lagrange.
Definition: AABBIGL.h:30
auto point_segment_squared_distance(const Eigen::MatrixBase< PointType > &point, const Eigen::MatrixBase< PointType > &V0, const Eigen::MatrixBase< PointType > &V1, Eigen::PlainObjectBase< PointType > &closest_point, ScalarOf< PointType > &lambda0, ScalarOf< PointType > &lambda1) -> ScalarOf< PointType >
Computes the point closest to a given point in a nd segment.
Definition: point_segment_squared_distance.h:73
auto point_triangle_squared_distance(const Eigen::MatrixBase< PointType > &point, const Eigen::MatrixBase< PointType > &V0, const Eigen::MatrixBase< PointType > &V1, const Eigen::MatrixBase< PointType > &V2, Eigen::PlainObjectBase< PointType > &closest_point, ScalarOf< PointType > &lambda0, ScalarOf< PointType > &lambda1, ScalarOf< PointType > &lambda2) -> ScalarOf< PointType >
Computes the point closest to a given point in a nd triangle.
Definition: point_triangle_squared_distance.h:77