JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
ClosestPoint.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright 2024 Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
23
24#include <limits>
25
26namespace JPL
27{
31 template<CVec3 Vec3>
32 inline bool GetBaryCentricCoordinates(const Vec3& inA, const Vec3& inB, float& outU, float& outV)
33 {
34 static constexpr float epsSqr = std::numeric_limits<float>::epsilon() * std::numeric_limits<float>::epsilon();
35
36 const Vec3 ab = inB - inA;
37 const float denominator = LengthSquared(ab);
38 if (denominator < (epsSqr))
39 {
40 // Degenerate line segment, fallback to points
41 if (LengthSquared(inA) < LengthSquared(inB))
42 {
43 // A closest
44 outU = 1.0f;
45 outV = 0.0f;
46 }
47 else
48 {
49 // B closest
50 outU = 0.0f;
51 outV = 1.0f;
52 }
53 return false;
54 }
55 else
56 {
57 outV = -DotProduct(inA, ab) / denominator;
58 outU = 1.0f - outV;
59 }
60 return true;
61 }
62
65 template<CVec3 Vec3>
66 inline Vec3 GetClosestPointOnLine(const Vec3& inA, const Vec3& inB, uint32_t& outSet)
67 {
68 float u, v;
70 if (v <= 0.0f)
71 {
72 // inA is closest point
73 outSet = 0b0001;
74 return inA;
75 }
76 else if (u <= 0.0f)
77 {
78 // inB is closest point
79 outSet = 0b0010;
80 return inB;
81 }
82 else
83 {
84 // Closest point lies on line inA inB
85 outSet = 0b0011;
86 return u * inA + v * inB;
87 }
88 }
89} // namespace JPL
Definition AcousticMaterial.h:36
bool GetBaryCentricCoordinates(const Vec3 &inA, const Vec3 &inB, float &outU, float &outV)
Definition ClosestPoint.h:32
Vec3 GetClosestPointOnLine(const Vec3 &inA, const Vec3 &inB, uint32_t &outSet)
Definition ClosestPoint.h:66
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813