Path: blob/master/thirdparty/manifold/src/tri_dist.h
10278 views
// Copyright 2024 The Manifold Authors.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#pragma once1516#include <array>17#include <cstdint>1819#include "manifold/common.h"2021namespace manifold {2223// From NVIDIA-Omniverse PhysX - BSD 3-Clause "New" or "Revised" License24// https://github.com/NVIDIA-Omniverse/PhysX/blob/main/LICENSE.md25// https://github.com/NVIDIA-Omniverse/PhysX/blob/main/physx/source/geomutils/src/sweep/GuSweepCapsuleCapsule.cpp26// With minor modifications2728/**29* Returns the distance between two line segments.30*31* @param[out] x Closest point on line segment pa.32* @param[out] y Closest point on line segment qb.33* @param[in] p One endpoint of the first line segment.34* @param[in] a Other endpoint of the first line segment.35* @param[in] p One endpoint of the second line segment.36* @param[in] b Other endpoint of the second line segment.37*/38inline void EdgeEdgeDist(vec3& x, vec3& y, // closest points39const vec3& p,40const vec3& a, // seg 1 origin, vector41const vec3& q,42const vec3& b) // seg 2 origin, vector43{44const vec3 T = q - p;45const auto ADotA = la::dot(a, a);46const auto BDotB = la::dot(b, b);47const auto ADotB = la::dot(a, b);48const auto ADotT = la::dot(a, T);49const auto BDotT = la::dot(b, T);5051// t parameterizes ray (p, a)52// u parameterizes ray (q, b)5354// Compute t for the closest point on ray (p, a) to ray (q, b)55const auto Denom = ADotA * BDotB - ADotB * ADotB;5657double t; // We will clamp result so t is on the segment (p, a)58t = Denom != 0.059? la::clamp((ADotT * BDotB - BDotT * ADotB) / Denom, 0.0, 1.0)60: 0.0;6162// find u for point on ray (q, b) closest to point at t63double u;64if (BDotB != 0.0) {65u = (t * ADotB - BDotT) / BDotB;6667// if u is on segment (q, b), t and u correspond to closest points,68// otherwise, clamp u, recompute and clamp t69if (u < 0.0) {70u = 0.0;71t = ADotA != 0.0 ? la::clamp(ADotT / ADotA, 0.0, 1.0) : 0.0;72} else if (u > 1.0) {73u = 1.0;74t = ADotA != 0.0 ? la::clamp((ADotB + ADotT) / ADotA, 0.0, 1.0) : 0.0;75}76} else {77u = 0.0;78t = ADotA != 0.0 ? la::clamp(ADotT / ADotA, 0.0, 1.0) : 0.0;79}80x = p + a * t;81y = q + b * u;82}8384// From NVIDIA-Omniverse PhysX - BSD 3-Clause "New" or "Revised" License85// https://github.com/NVIDIA-Omniverse/PhysX/blob/main/LICENSE.md86// https://github.com/NVIDIA-Omniverse/PhysX/blob/main/physx/source/geomutils/src/distance/GuDistanceTriangleTriangle.cpp87// With minor modifications8889/**90* Returns the minimum squared distance between two triangles.91*92* @param p First triangle.93* @param q Second triangle.94*/95inline auto DistanceTriangleTriangleSquared(const std::array<vec3, 3>& p,96const std::array<vec3, 3>& q) {97std::array<vec3, 3> Sv;98Sv[0] = p[1] - p[0];99Sv[1] = p[2] - p[1];100Sv[2] = p[0] - p[2];101102std::array<vec3, 3> Tv;103Tv[0] = q[1] - q[0];104Tv[1] = q[2] - q[1];105Tv[2] = q[0] - q[2];106107bool shown_disjoint = false;108109auto mindd = std::numeric_limits<double>::max();110111for (uint32_t i = 0; i < 3; i++) {112for (uint32_t j = 0; j < 3; j++) {113vec3 cp;114vec3 cq;115EdgeEdgeDist(cp, cq, p[i], Sv[i], q[j], Tv[j]);116const vec3 V = cq - cp;117const auto dd = la::dot(V, V);118119if (dd <= mindd) {120mindd = dd;121122uint32_t id = i + 2;123if (id >= 3) id -= 3;124vec3 Z = p[id] - cp;125auto a = la::dot(Z, V);126id = j + 2;127if (id >= 3) id -= 3;128Z = q[id] - cq;129auto b = la::dot(Z, V);130131if ((a <= 0.0) && (b >= 0.0)) {132return la::dot(V, V);133};134135if (a <= 0.0)136a = 0.0;137else if (b > 0.0)138b = 0.0;139140if ((mindd - a + b) > 0.0) shown_disjoint = true;141}142}143}144145vec3 Sn = la::cross(Sv[0], Sv[1]);146auto Snl = la::dot(Sn, Sn);147148if (Snl > 1e-15) {149const vec3 Tp(la::dot(p[0] - q[0], Sn), la::dot(p[0] - q[1], Sn),150la::dot(p[0] - q[2], Sn));151152int index = -1;153if ((Tp[0] > 0.0) && (Tp[1] > 0.0) && (Tp[2] > 0.0)) {154index = Tp[0] < Tp[1] ? 0 : 1;155if (Tp[2] < Tp[index]) index = 2;156} else if ((Tp[0] < 0.0) && (Tp[1] < 0.0) && (Tp[2] < 0.0)) {157index = Tp[0] > Tp[1] ? 0 : 1;158if (Tp[2] > Tp[index]) index = 2;159}160161if (index >= 0) {162shown_disjoint = true;163164const vec3& qIndex = q[index];165166vec3 V = qIndex - p[0];167vec3 Z = la::cross(Sn, Sv[0]);168if (la::dot(V, Z) > 0.0) {169V = qIndex - p[1];170Z = la::cross(Sn, Sv[1]);171if (la::dot(V, Z) > 0.0) {172V = qIndex - p[2];173Z = la::cross(Sn, Sv[2]);174if (la::dot(V, Z) > 0.0) {175vec3 cp = qIndex + Sn * Tp[index] / Snl;176vec3 cq = qIndex;177return la::dot(cp - cq, cp - cq);178}179}180}181}182}183184vec3 Tn = la::cross(Tv[0], Tv[1]);185auto Tnl = la::dot(Tn, Tn);186187if (Tnl > 1e-15) {188const vec3 Sp(la::dot(q[0] - p[0], Tn), la::dot(q[0] - p[1], Tn),189la::dot(q[0] - p[2], Tn));190191int index = -1;192if ((Sp[0] > 0.0) && (Sp[1] > 0.0) && (Sp[2] > 0.0)) {193index = Sp[0] < Sp[1] ? 0 : 1;194if (Sp[2] < Sp[index]) index = 2;195} else if ((Sp[0] < 0.0) && (Sp[1] < 0.0) && (Sp[2] < 0.0)) {196index = Sp[0] > Sp[1] ? 0 : 1;197if (Sp[2] > Sp[index]) index = 2;198}199200if (index >= 0) {201shown_disjoint = true;202203const vec3& pIndex = p[index];204205vec3 V = pIndex - q[0];206vec3 Z = la::cross(Tn, Tv[0]);207if (la::dot(V, Z) > 0.0) {208V = pIndex - q[1];209Z = la::cross(Tn, Tv[1]);210if (la::dot(V, Z) > 0.0) {211V = pIndex - q[2];212Z = la::cross(Tn, Tv[2]);213if (la::dot(V, Z) > 0.0) {214vec3 cp = pIndex;215vec3 cq = pIndex + Tn * Sp[index] / Tnl;216return la::dot(cp - cq, cp - cq);217}218}219}220}221}222223return shown_disjoint ? mindd : 0.0;224};225} // namespace manifold226227228