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
35 TriPoint( const Vector3<T> & p, const Vector3<T> & v0, const Vector3<T> & v1, const Vector3<T> & v2, int edge = -1 ) : TriPoint( p - v0, v1 - v0, v2 - v0, edge ) { }
38 TriPoint( const Vector3<T> & p, const Vector3<T> & v1, const Vector3<T> & v2, int edge = -1 );
39
41 template <typename U>
42 U interpolate( const U & v0, const U & v1, const U & v2 ) const
43 { return ( 1 - a - b ) * v0 + a * v1 + b * v2; }
44
46 [[nodiscard]] TriPoint lnext() const { return { b, 1 - a - b }; }
47
51
53 constexpr int inVertex() const;
56 constexpr int onEdge() const;
57
59 [[nodiscard]] constexpr bool operator==( const TriPoint& rhs ) const = default;
60
61 friend std::ostream& operator<<( std::ostream& s, const TriPoint& tp )
62 {
63 return s << tp.a << ' ' << tp.b;
64 }
65
66 friend std::istream& operator>>( std::istream& s, TriPoint& tp )
67 {
68 s >> tp.a >> tp.b;
69 return s;
70 }
71};
72
75
76template <typename T>
77TriPoint<T>::TriPoint( const Vector3<T> & p, const Vector3<T> & v1, const Vector3<T> & v2, int edge )
78{
79 if ( edge >= 0 )
80 {
82 const Vector3<T> v0;
83 const Vector3<T> * v[3] = { &v0, &v1, &v2 };
84 const auto & org = *v[( edge + 1 ) % 3];
85 const auto d = *v[( edge + 2 ) % 3] - org;
86 const T dSq = d.lengthSq();
87 const T s = dSq > 0 ? std::clamp( dot( p - org, d ) / dSq, T(0), T(1) ) : T(0);
88 switch ( edge )
89 {
90 case 0:
91 a = 1 - s;
92 b = s;
93 break;
94 case 1:
95 a = 0;
96 b = 1 - s;
97 break;
98 default:
99 a = s;
100 b = 0;
101 }
102 return;
103 }
105 const auto n = cross( v1, v2 );
106 const T nSq = n.lengthSq();
107 if ( nSq <= 0 )
108 {
110 a = b = 1 / T(3);
111 return;
112 }
113 const T inv = 1 / nSq;
114 a = std::clamp( inv * dot( n, cross( p, v2 ) ), T(0), T(1) );
115 b = std::clamp( inv * dot( n, cross( v1, p ) ), T(0), T(1) - a );
116}
117
118template <typename T>
119constexpr int TriPoint<T>::inVertex() const
120{
121 if ( a <= eps && b <= eps )
122 return 0;
123 if ( 1 - a - b <= eps )
124 {
125 if ( b <= eps )
126 return 1;
127 if ( a <= eps )
128 return 2;
129 }
130 return -1;
131}
132
133template <typename T>
134constexpr int TriPoint<T>::onEdge() const
135{
136 if ( 1 - a - b <= eps )
137 return 0;
138 if ( a <= eps )
139 return 1;
140 if ( b <= eps )
141 return 2;
142 return -1;
143}
144
146
147}
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:33
TriPoint lnext() const
represents the same point relative to next edge in the same triangle
Definition MRTriPoint.h:46
constexpr TriPoint() noexcept
Definition MRTriPoint.h:27
static constexpr auto eps
Definition MRSegmPoint.h:20
constexpr TriPoint(T a, T b) noexcept
Definition MRTriPoint.h:29
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
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:42
friend std::ostream & operator<<(std::ostream &s, const TriPoint &tp)
Definition MRTriPoint.h:61
constexpr int inVertex() const
returns [0,2] if the point is in a vertex or -1 otherwise
TriPoint(const Vector3< T > &p, const Vector3< T > &v1, const Vector3< T > &v2, int edge=-1)
TriPoint(const Vector3< T > &p, const Vector3< T > &v0, const Vector3< T > &v1, const Vector3< T > &v2, int edge=-1)
Definition MRTriPoint.h:35
static constexpr auto eps
Definition MRTriPoint.h:25
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:66
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
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
Definition MRMeshFwd.h:102
auto cross(const Vector2< T > &a, const Vector2< T > &b) -> decltype(a.x *b.x)
cross product
Definition MRVector2.h:160
Definition MRVector3.h:33