MeshLib C++ Docs
Loading...
Searching...
No Matches
MRClosestPointInTriangle.h
Go to the documentation of this file.
1
16#pragma once
17
18#include "MRVector3.h"
19#include "MRTriPoint.h"
20#include "MRMacros.h"
21#include "MRPch/MRBindingMacros.h"
22
23#include <cassert>
24
25namespace MR
26{
29
30
32template <typename T>
33MR_FORCE_INLINE std::pair<Vector3<T>, TriPoint<T>> closestPointInTriangle( const Vector3<T>& p, const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& c )
34{
36 const Vector3<T> ab = b - a;
37 const Vector3<T> ac = c - a;
38 const Vector3<T> ap = p - a;
39
40 const T d1 = dot( ab, ap );
41 const T d2 = dot( ac, ap );
42 if ( d1 <= 0 && d2 <= 0 )
43 return { a, { 0, 0 } }; //#1
44
45 const Vector3<T> bp = p - b;
46 const T d3 = dot( ab, bp );
47 const T d4 = dot( ac, bp );
48 if ( d3 >= 0 && d4 <= d3 )
49 return { b, { 1, 0 } }; //#2
50
51 const Vector3<T> cp = p - c;
52 const T d5 = dot( ab, cp );
53 const T d6 = dot( ac, cp );
54 if ( d6 >= 0 && d5 <= d6 )
55 return { c, { 0, 1 } }; //#3
56
57 const T vc = d1 * d4 - d3 * d2;
58 if ( vc <= 0 && d1 >= 0 && d3 <= 0 )
59 {
60 const T v = d1 / ( d1 - d3 );
61 return { a + v * ab, { v, 0 } }; //#4
62 }
63
64 const T vb = d5 * d2 - d1 * d6;
65 if ( vb <= 0 && d6 <= 0 && d2 >= 0 )
66 {
67 const T v = d2 / ( d2 - d6 );
68 return { a + v * ac, { 0, v } }; //#5
69 }
70
71 const T va = d3 * d6 - d5 * d4;
72 if ( va <= 0 )
73 {
75 if ( d4 < d3 )
76 return { b, { 1, 0 } }; //#2
77
79 if ( d5 < d6 )
80 return { c, { 0, 1 } }; //#3
81
83 const T v = ( d4 - d3 ) / ( ( d4 - d3 ) + ( d5 - d6 ) );
84 return { b + v * ( c - b ), { 1 - v, v } }; //#6
85 }
86
87 assert( va > 0 && vb > 0 && vc > 0 );
88 const T denom = 1 / ( va + vb + vc );
89 const T v = vb * denom;
90 const T w = vc * denom;
91 return { a + v * ab + w * ac, { v, w } }; //#0
92}
93
94MR_BIND_TEMPLATE( std::pair<Vector3f, TriPointf> closestPointInTriangle( const Vector3f& p, const Vector3f& a, const Vector3f& b, const Vector3f& c ) );
95MR_BIND_TEMPLATE( std::pair<Vector3d, TriPointd> closestPointInTriangle( const Vector3d& p, const Vector3d& a, const Vector3d& b, const Vector3d& c ) );
96
97}
#define MR_FORCE_INLINE
Definition MRMacros.h:47
auto dot(const Matrix2< T > &a, const Matrix2< T > &b) -> decltype(dot(a.x, b.x))
double-dot product: x = a : b
Definition MRMatrix2.h:142
MR_FORCE_INLINE std::pair< Vector3< T >, TriPoint< T > > closestPointInTriangle(const Vector3< T > &p, const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c)
forced inline: through a call, the vertices are stored in memory and reloaded, which can stall on sto...
Definition MRClosestPointInTriangle.h:33
MR_BIND_TEMPLATE(std::pair< Vector3f, TriPointf > closestPointInTriangle(const Vector3f &p, const Vector3f &a, const Vector3f &b, const Vector3f &c))
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
encodes a point inside a triangle using barycentric coordinates
Definition MRTriPoint.h:19
Definition MRVector3.h:33