18struct TriIntersectResult
26 bary.a = U; bary.b = V; t = dist;
38 const auto abcd = mixed( a - d, b - d, c - d );
39 const auto abce = mixed( a - e, b - e, c - e );
40 const auto abcf = mixed( a - f, b - f, c - f );
41 const auto abc_de = abcd * abce >= 0;
42 const auto abc_fd = abcf * abcd >= 0;
44 if ( abc_de && abc_fd && abce * abcf >= 0 )
47 const auto defa = mixed( d - a, e - a, f - a );
48 const auto defb = mixed( d - b, e - b, f - b );
49 const auto defc = mixed( d - c, e - c, f - c );
50 const auto def_ab = defa * defb >= 0;
51 const auto def_ca = defc * defa >= 0;
53 if ( def_ab && def_ca && defb * defc >= 0 )
68 const auto abde = mixed( a - e, b - e, d - e );
69 const auto abdf = mixed( a - f, b - f, d - f );
71 if ( abde * abdf < 0 )
74 const auto acde = mixed( a - e, c - e, d - e );
76 if ( abde * acde < 0 )
79 if ( abdf == 0 && acde == 0 )
82 const auto acdf = mixed( a - f, c - f, d - f );
84 if ( acde * acdf < 0 )
87 if ( abdf * acdf < 0 )
90 if ( abde == 0 && acdf == 0 )
100 return mixed( p - a, p - b, p - c ) == T( 0 );
103MR_BIND_TEMPLATE(
bool isPointInPlane(
const Vector3<float>& p,
const Vector3<float>& a,
const Vector3<float>& b,
const Vector3<float>& c ) )
104MR_BIND_TEMPLATE(
bool isPointInPlane(
const Vector3<double>& p,
const Vector3<double>& a,
const Vector3<double>& b,
const Vector3<double>& c ) )
110 return cross( p - a, p - b ).lengthSq() == T( 0 );
117 return cross( p - a, p - b ) == T( 0 );
127 return dot( p - a, b - a ) >= 0 && dot( p - b, a - b ) >= 0;
137 return dot( p - a, b - a ) >= 0 && dot( p - b, a - b ) >= 0;
140MR_BIND_TEMPLATE(
bool isPointInLine(
const Vector3<float>& p,
const Vector3<float>& a,
const Vector3<float>& b ) )
141MR_BIND_TEMPLATE(
bool isPointInLine(
const Vector3<double>& p,
const Vector3<double>& a,
const Vector3<double>& b ) )
142MR_BIND_TEMPLATE(
bool isPointInSegm(
const Vector3<float>& p,
const Vector3<float>& a,
const Vector3<float>& b ) )
143MR_BIND_TEMPLATE(
bool isPointInSegm(
const Vector3<double>& p,
const Vector3<double>& a,
const Vector3<double>& b ) )
151 const auto normDir = cross( b - a, c - a );
152 if ( dot( normDir, cross( b - a, p - a ) ) < 0 )
154 if ( dot( normDir, cross( c - b, p - b ) ) < 0 )
156 if ( dot( normDir, cross( a - c, p - c ) ) < 0 )
158 if ( normDir.lengthSq() == 0 )
162 if ( a == b && b == c && p != a )
164 if ( dot( b - a, c - a ) <= 0 )
166 else if ( ( b - a ).lengthSq() > ( c - a ).lengthSq() )
178 const auto normSign = cross( b - a, c - a );
179 if ( normSign * cross( b - a, p - a ) < 0 )
181 if ( normSign * cross( c - b, p - b ) < 0 )
183 if ( normSign * cross( a - c, p - c ) < 0 )
188 if ( a == b && b == c && p != a )
190 if ( dot( b - a, c - a ) <= 0 )
192 else if ( ( b - a ).lengthSq() > ( c - a ).lengthSq() )
201MR_BIND_TEMPLATE(
bool isPointInTriangle(
const Vector3<float>& p,
const Vector3<float>& a,
const Vector3<float>& b,
const Vector3<float>& c ) )
202MR_BIND_TEMPLATE(
bool isPointInTriangle(
const Vector3<double>& p,
const Vector3<double>& a,
const Vector3<double>& b,
const Vector3<double>& c ) )
203MR_BIND_TEMPLATE(
bool isPointInTriangle(
const Vector2<float>& p,
const Vector2<float>& a,
const Vector2<float>& b,
const Vector2<float>& c ) )
204MR_BIND_TEMPLATE(
bool isPointInTriangle(
const Vector2<double>& p,
const Vector2<double>& a,
const Vector2<double>& b,
const Vector2<double>& c ) )
214 const auto xy = ( y - x ).normalized();
215 d = ( d - xy * dot( xy, d ) ).normalized();
217 const auto dz = dot( d, z - x );
219 dz * dot( d, u - x ) < 0 &&
220 dz * dot( d, v - x ) < 0 &&
221 dz * dot( d, w - x ) < 0;
236 const auto dir = a + b + c - d - e - f;
254 const auto dabe = mixed( d - e, a - e, b - e );
255 const auto dbce = mixed( d - e, b - e, c - e );
256 if ( dabe * dbce <= 0 )
259 const auto dcae = mixed( d - e, c - e, a - e );
260 if ( dbce * dcae <= 0 )
263 if ( dcae * dabe <= 0 )
276 const auto abcd = mixed( a - d, b - d, c - d );
277 const auto abce = mixed( a - e, b - e, c - e );
278 if ( abcd * abce >= 0 )
290 const auto abcd = std::abs( mixed( a - d, b - d, c - d ) );
291 const auto abce = std::abs( mixed( a - e, b - e, c - e ) );
292 auto r = std::clamp( abcd / ( abcd + abce ), T( 0 ), T( 1 ) );
293 return r * e + ( 1 - r ) * d;
323 const T& Sx = prec.
Sx;
324 const T& Sy = prec.
Sy;
325 const T& Sz = prec.
Sz;
335 const T eps = std::numeric_limits<T>::epsilon() * std::max( { Ax, Bx, Cx, Ay, By, Cy } );
336 T U = Cx * By - Cy * Bx;
337 T V = Ax * Cy - Ay * Cx;
338 T W = Bx * Ay - By * Ax;
340 if( U < -eps || V < -eps || W < -eps )
342 if( U > eps || V > eps || W > eps )
355 const T t = U * Az + V * Bz + W * Cz;
357 auto invDet = T( 1 ) / det;
358 return TriIntersectResult(
float( V * invDet ),
float( W * invDet ),
float( t * invDet ) );
361MR_BIND_TEMPLATE( std::optional<TriIntersectResult>
rayTriangleIntersect(
const Vector3<float >& oriA,
const Vector3<float >& oriB,
const Vector3<float >& oriC,
const IntersectionPrecomputes<float >& prec ) )
362MR_BIND_TEMPLATE( std::optional<TriIntersectResult>
rayTriangleIntersect(
const Vector3<double>& oriA,
const Vector3<double>& oriB,
const Vector3<double>& oriC,
const IntersectionPrecomputes<double>& prec ) )
372MR_BIND_TEMPLATE( std::optional<TriIntersectResult>
rayTriangleIntersect(
const Vector3<float >& oriA,
const Vector3<float >& oriB,
const Vector3<float >& oriC,
const Vector3<float >& dir ) )
373MR_BIND_TEMPLATE( std::optional<TriIntersectResult>
rayTriangleIntersect(
const Vector3<double>& oriA,
const Vector3<double>& oriB,
const Vector3<double>& oriC,
const Vector3<double>& dir ) )
415MR_BIND_TEMPLATE(
bool doTrianglesOverlap(
const Vector2<float>& a,
const Vector2<float>& b,
const Vector2<float>& c,
const Vector2<float>& d,
const Vector2<float>& e,
const Vector2<float>& f ) )
416MR_BIND_TEMPLATE(
bool doTrianglesOverlap(
const Vector2<double>& a,
const Vector2<double>& b,
const Vector2<double>& c,
const Vector2<double>& d,
const Vector2<double>& e,
const Vector2<double>& f ) )
Definition MRTriangleIntersection.h:19
unsafe TriIntersectResult(MR.Const_TriIntersectResult _other)
int idxY
Definition MRIntersectionPrecomputes.h:124
T Sx
precomputed factors
Definition MRIntersectionPrecomputes.h:130
int idxX
Definition MRIntersectionPrecomputes.h:123
T Sz
Definition MRIntersectionPrecomputes.h:130
T Sy
Definition MRIntersectionPrecomputes.h:130
int maxDimIdxZ
Definition MRIntersectionPrecomputes.h:122
MRMESH_API TriangleSegmentIntersectResult doTriangleSegmentIntersect(const std::array< PreciseVertCoords, 5 > &vs)
std::optional< Vector3< T > > findTriangleTriangleIntersection(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c, const Vector3< T > &d, const Vector3< T > &e, const Vector3< T > &f)
Definition MRTriangleIntersection.h:299
bool isPointInPlane(const Vector3< T > &p, const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c)
returns true if ABC plane contains point P
Definition MRTriangleIntersection.h:98
bool doTrianglesIntersect(Vector3< T > a, Vector3< T > b, Vector3< T > c, Vector3< T > d, Vector3< T > e, Vector3< T > f)
Definition MRTriangleIntersection.h:33
std::optional< TriIntersectResult > rayTriangleIntersect(const Vector3< T > &oriA, const Vector3< T > &oriB, const Vector3< T > &oriC, const IntersectionPrecomputes< T > &prec)
Definition MRTriangleIntersection.h:320
bool doesEdgeXySeparate(const Vector3< T > &x, const Vector3< T > &y, const Vector3< T > &z, const Vector3< T > &u, const Vector3< T > &v, const Vector3< T > &w, Vector3< T > d)
returns true if a plane containing edge XY separates point Z from triangle UVW
Definition MRTriangleIntersection.h:208
bool isPointInTriangle(const Vector3< T > &p, const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c)
returns true if ABC triangle contains point P
Definition MRTriangleIntersection.h:147
bool doTriangleLineIntersect(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c, const Vector3< T > &d, const Vector3< T > &e)
checks whether triangle ABC and infinite line DE intersect
Definition MRTriangleIntersection.h:249
Vector3< T > findTriangleSegmentIntersection(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c, const Vector3< T > &d, const Vector3< T > &e)
this function input should have intersection
Definition MRTriangleIntersection.h:285
bool doTrianglesIntersectExt(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c, const Vector3< T > &d, const Vector3< T > &e, const Vector3< T > &f)
Definition MRTriangleIntersection.h:227
bool doTrianglesOverlap(const Vector2< T > &a, const Vector2< T > &b, const Vector2< T > &c, const Vector2< T > &d, const Vector2< T > &e, const Vector2< T > &f)
returns true if ABC and DEF overlaps or touches
Definition MRTriangleIntersection.h:377
bool isPointInLine(const Vector3< T > &p, const Vector3< T > &a, const Vector3< T > &b)
returns true if AB line contains point P
Definition MRTriangleIntersection.h:108
bool isPointInSegm(const Vector3< T > &p, const Vector3< T > &a, const Vector3< T > &b)
returns true if AB segment contains point P
Definition MRTriangleIntersection.h:122
Definition MRCameraOrientationPlugin.h:8
bool doSegmentsIntersect(const LineSegm< V > &x, const LineSegm< V > &y, typename V::ValueType *xPos=nullptr, typename V::ValueType *yPos=nullptr)
Definition MRLineSegm.h:53
Definition MRMeshFwd.h:503
Definition MRLineSegm.h:12
Definition MRVector2.h:29
Definition MRMesh/MRVector3.h:30