Lagrange
point_segment_squared_distance.h
1// Source: https://github.com/alicevision/geogram/blob/master/src/lib/geogram/basic/geometry_nd.h
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// Copyright (c) 2012-2014, Bruno Levy
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are met:
9//
10// * Redistributions of source code must retain the above copyright notice,
11// this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above copyright notice,
13// this list of conditions and the following disclaimer in the documentation
14// and/or other materials provided with the distribution.
15// * Neither the name of the ALICE Project-Team nor the names of its
16// contributors may be used to endorse or promote products derived from this
17// software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29// POSSIBILITY OF SUCH DAMAGE.
30//
31// If you modify this software, you should include a notice giving the
32// name of the person performing the modification, the date of modification,
33// and the reason for such modification.
34//
35// Contact: Bruno Levy
36//
37// Bruno.Levy@inria.fr
38// http://www.loria.fr/~levy
39//
40// ALICE Project
41// LORIA, INRIA Lorraine,
42// Campus Scientifique, BP 239
43// 54506 VANDOEUVRE LES NANCY CEDEX
44// FRANCE
45//
46// This file has been modified by Adobe.
47//
48// All modifications are Copyright 2020 Adobe.
49//
50#pragma once
51
52#include <lagrange/common.h>
53
54#include <Eigen/Dense>
55
56namespace lagrange {
57
72template <typename PointType>
74 const Eigen::MatrixBase<PointType>& point,
75 const Eigen::MatrixBase<PointType>& V0,
76 const Eigen::MatrixBase<PointType>& V1,
77 Eigen::PlainObjectBase<PointType>& closest_point,
78 ScalarOf<PointType>& lambda0,
79 ScalarOf<PointType>& lambda1) -> ScalarOf<PointType>
80{
81 using Scalar = ScalarOf<PointType>;
82
83 auto l2 = (V0 - V1).squaredNorm();
84 auto t = (point - V0).dot(V1 - V0);
85 if (t <= Scalar(0) || l2 == Scalar(0)) {
86 closest_point = V0;
87 lambda0 = Scalar(1);
88 lambda1 = Scalar(0);
89 return (point - V0).squaredNorm();
90 } else if (t > l2) {
91 closest_point = V1;
92 lambda0 = Scalar(0);
93 lambda1 = Scalar(1);
94 return (point - V1).squaredNorm();
95 }
96 lambda1 = t / l2;
97 lambda0 = Scalar(1) - lambda1;
98 closest_point = lambda0 * V0 + lambda1 * V1;
99 return (point - closest_point).squaredNorm();
100}
101
113template <typename PointType>
115 const Eigen::MatrixBase<PointType>& point,
116 const Eigen::MatrixBase<PointType>& V0,
117 const Eigen::MatrixBase<PointType>& V1) -> ScalarOf<PointType>
118{
119 PointType closest_point;
120 ScalarOf<PointType> lambda0;
121 ScalarOf<PointType> lambda1;
122 return point_segment_squared_distance(point, V0, V1, closest_point, lambda0, lambda1);
123}
124
125} // namespace lagrange
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