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