MeshLib C++ Docs
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#include "MRMesh/MRMacros.h"
6#include <iosfwd>
7
8namespace MR
9{
10
14template <typename T>
16{
19 T a;
20 T b;
21
22 static constexpr auto eps = SegmPoint<T>::eps;
23
24 constexpr TriPoint() noexcept : a( 0 ), b( 0 ) { }
25 explicit TriPoint( NoInit ) noexcept { }
26 constexpr TriPoint( T a, T b ) noexcept : a( a ), b( b ) { }
27 template <typename U> MR_REQUIRES_IF_SUPPORTED(!std::is_same_v<T, U>) // The condition is needed for the bindings to not duplicate this ctor against the copy ctor.
28 constexpr TriPoint( const TriPoint<U> & s ) : a( T( s.a ) ), b( T( s.b ) ) { }
29
31 TriPoint( const Vector3<T> & p, const Vector3<T> & v0, const Vector3<T> & v1, const Vector3<T> & v2 ) : TriPoint( p - v0, v1 - v0, v2 - v0 ) { }
33 TriPoint( const Vector3<T> & p, const Vector3<T> & v1, const Vector3<T> & v2 );
34
36 template <typename U>
37 U interpolate( const U & v0, const U & v1, const U & v2 ) const
38 { return ( 1 - a - b ) * v0 + a * v1 + b * v2; }
39
41 [[nodiscard]] TriPoint lnext() const { return { b, 1 - a - b }; }
42
43 // requirements:
44 // 1) inVertex() == onEdge() && toEdge()->inVertex()
45 // 2) invariance to lnext() application
46
48 constexpr int inVertex() const;
51 constexpr int onEdge() const;
52
54 [[nodiscard]] constexpr bool operator==( const TriPoint& rhs ) const = default;
55
56 friend std::ostream& operator<<( std::ostream& s, const TriPoint& tp )
57 {
58 return s << tp.a << ' ' << tp.b;
59 }
60
61 friend std::istream& operator>>( std::istream& s, TriPoint& tp )
62 {
63 s >> tp.a >> tp.b;
64 return s;
65 }
66};
67
70
71template <typename T>
72TriPoint<T>::TriPoint( const Vector3<T> & p, const Vector3<T> & v1, const Vector3<T> & v2 )
73{
74 const T v11 = dot( v1, v1 );
75 const T v12 = dot( v1, v2 );
76 const T v22 = dot( v2, v2 );
77 const T det = v11 * v22 - v12 * v12;
78 if ( det <= 0 )
79 {
80 // degenerate triangle
81 a = b = 1 / T(3);
82 return;
83 }
84 const T pv1 = dot( p, v1 );
85 const T pv2 = dot( p, v2 );
86 a = std::clamp( ( 1 / det ) * ( v22 * pv1 - v12 * pv2 ), T(0), T(1) );
87 b = std::clamp( ( 1 / det ) * (-v12 * pv1 + v11 * pv2 ), T(0), T(1) - a );
88}
89
90template <typename T>
91constexpr int TriPoint<T>::inVertex() const
92{
93 if ( a <= eps && b <= eps )
94 return 0;
95 if ( 1 - a - b <= eps )
96 {
97 if ( b <= eps )
98 return 1;
99 if ( a <= eps )
100 return 2;
101 }
102 return -1;
103}
104
105template <typename T>
106constexpr int TriPoint<T>::onEdge() const
107{
108 if ( 1 - a - b <= eps )
109 return 0;
110 if ( a <= eps )
111 return 1;
112 if ( b <= eps )
113 return 2;
114 return -1;
115}
116
118
119} // namespace MR
Definition MRCameraOrientationPlugin.h:8
float dot(Vector2f a, Vector2f b)
Definition MRMeshFwd.h:94
encodes a point inside a line segment using relative distance in [0,1]
Definition MRSegmPoint.h:14
encodes a point inside a triangle using barycentric coordinates
Definition MRTriPoint.h:16
TriPoint lnext() const
represents the same point relative to next edge in the same triangle
Definition MRTriPoint.h:41
constexpr TriPoint() noexcept
Definition MRTriPoint.h:24
constexpr TriPoint(T a, T b) noexcept
Definition MRTriPoint.h:26
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:37
friend std::ostream & operator<<(std::ostream &s, const TriPoint &tp)
Definition MRTriPoint.h:56
constexpr int inVertex() const
returns [0,2] if the point is in a vertex or -1 otherwise
static constexpr auto eps
Definition MRTriPoint.h:22
MR_REQUIRES_IF_SUPPORTED(!std::is_same_v< T, U >) const expr TriPoint(const TriPoint< U > &s)
Definition MRTriPoint.h:27
T b
b in [0,1], b=0 => point is on [v0,v1] edge, b=1 => point is in v2
Definition MRTriPoint.h:20
T a
a in [0,1], a=0 => point is on [v2,v0] edge, a=1 => point is in v1
Definition MRTriPoint.h:19
constexpr int onEdge() const
friend std::istream & operator>>(std::istream &s, TriPoint &tp)
Definition MRTriPoint.h:61
constexpr bool operator==(const TriPoint &rhs) const =default
returns true if two points have equal (a,b) representation
TriPoint(NoInit) noexcept
Definition MRTriPoint.h:25
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:31
Definition MRMesh/MRVector3.h:30