MeshLib
 
Loading...
Searching...
No Matches
MRTriPoint.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVector3.h"
4#include "MRSegmPoint.h"
5
6namespace MR
7{
8
12template <typename T>
14{
17 T a;
18 T b;
19
20 static constexpr auto eps = SegmPoint<T>::eps;
21
22 constexpr TriPoint() noexcept : a( 0 ), b( 0 ) { }
23 explicit TriPoint( NoInit ) noexcept { }
24 constexpr TriPoint( T a, T b ) noexcept : a( a ), b( b ) { }
25 template <typename U>
26 constexpr TriPoint( const TriPoint<U> & s ) : a( T( s.a ) ), b( T( s.b ) ) { }
27
29 TriPoint( const Vector3<T> & p, const Vector3<T> & v0, const Vector3<T> & v1, const Vector3<T> & v2 ) : TriPoint( p - v0, v1 - v0, v2 - v0 ) { }
31 TriPoint( const Vector3<T> & p, const Vector3<T> & v1, const Vector3<T> & v2 );
32
34 template <typename U>
35 U interpolate( const U & v0, const U & v1, const U & v2 ) const
36 { return ( 1 - a - b ) * v0 + a * v1 + b * v2; }
37
39 [[nodiscard]] TriPoint lnext() const { return { b, 1 - a - b }; }
40
41 // requirements:
42 // 1) inVertex() == onEdge() && toEdge()->inVertex()
43 // 2) invariance to lnext() application
44
46 constexpr int inVertex() const;
49 constexpr int onEdge() const;
50
52 [[nodiscard]] constexpr bool operator==( const TriPoint& rhs ) const = default;
53};
54
57
58template <typename T>
59TriPoint<T>::TriPoint( const Vector3<T> & p, const Vector3<T> & v1, const Vector3<T> & v2 )
60{
61 const T v11 = dot( v1, v1 );
62 const T v12 = dot( v1, v2 );
63 const T v22 = dot( v2, v2 );
64 const T det = v11 * v22 - v12 * v12;
65 if ( det <= 0 )
66 {
67 // degenerate triangle
68 a = b = 1 / T(3);
69 return;
70 }
71 const T pv1 = dot( p, v1 );
72 const T pv2 = dot( p, v2 );
73 a = std::clamp( ( 1 / det ) * ( v22 * pv1 - v12 * pv2 ), T(0), T(1) );
74 b = std::clamp( ( 1 / det ) * (-v12 * pv1 + v11 * pv2 ), T(0), T(1) - a );
75}
76
77template <typename T>
78constexpr int TriPoint<T>::inVertex() const
79{
80 if ( a <= eps && b <= eps )
81 return 0;
82 if ( 1 - a - b <= eps )
83 {
84 if ( b <= eps )
85 return 1;
86 if ( a <= eps )
87 return 2;
88 }
89 return -1;
90}
91
92template <typename T>
93constexpr int TriPoint<T>::onEdge() const
94{
95 if ( 1 - a - b <= eps )
96 return 0;
97 if ( a <= eps )
98 return 1;
99 if ( b <= eps )
100 return 2;
101 return -1;
102}
103
105
106} // namespace MR
Definition MRCameraOrientationPlugin.h:7
MRMESH_CLASS Vector3
Definition MRMesh/MRMeshFwd.h:136
Definition MRMesh/MRMeshFwd.h:56
encodes a point inside a line segment using relative distance in [0,1]
Definition MRSegmPoint.h:12
encodes a point inside a triangle using barycentric coordinates
Definition MRTriPoint.h:14
TriPoint lnext() const
represents the same point relative to next edge in the same triangle
Definition MRTriPoint.h:39
constexpr TriPoint() noexcept
Definition MRTriPoint.h:22
constexpr TriPoint(T a, T b) noexcept
Definition MRTriPoint.h:24
TriPoint(const Vector3< T > &p, const Vector3< T > &v1, const Vector3< T > &v2)
given a point coordinates and triangle (0,v1,v2) computes barycentric coordinates of the point
U interpolate(const U &v0, const U &v1, const U &v2) const
given three values in three vertices, computes interpolated value at this barycentric coordinates
Definition MRTriPoint.h:35
constexpr int inVertex() const
returns [0,2] if the point is in a vertex or -1 otherwise
static constexpr auto eps
Definition MRTriPoint.h:20
T b
b in [0,1], b=0 => point is on [v0,v1] edge, b=1 => point is in v2
Definition MRTriPoint.h:18
T a
a in [0,1], a=0 => point is on [v2,v0] edge, a=1 => point is in v1
Definition MRTriPoint.h:17
constexpr int onEdge() const
constexpr bool operator==(const TriPoint &rhs) const =default
returns true if two points have equal (a,b) representation
TriPoint(NoInit) noexcept
Definition MRTriPoint.h:23
TriPoint(const Vector3< T > &p, const Vector3< T > &v0, const Vector3< T > &v1, const Vector3< T > &v2)
given a point coordinates and triangle (v0,v1,v2) computes barycentric coordinates of the point
Definition MRTriPoint.h:29
constexpr TriPoint(const TriPoint< U > &s)
Definition MRTriPoint.h:26
Definition MRMesh/MRVector3.h:19