6#include "MRPch/MRBindingMacros.h"
24 const auto ab = ( b - a ).lengthSq();
25 const auto ca = ( a - c ).lengthSq();
26 const auto bc = ( c - b ).lengthSq();
33 const auto f =
cross( b - a, c - a ).lengthSq();
35 return std::numeric_limits<T>::infinity();
36 return ab * ca * bc / f;
59 const auto ab = ( b - a ).lengthSq();
60 const auto ca = ( a - c ).lengthSq();
61 const auto bc = ( c - b ).lengthSq();
68 const auto f =
cross( b - a, c - a ).lengthSq();
70 return ab * ca * bc / f;
86 const auto ac = c - a;
88 const auto p =
dot( ac, d );
91 auto sqDistToNearest = [&]( T t )
93 t = std::clamp( t, T( 0 ), T( 1 ) );
94 return std::max( T( 0 ), std::min( { t * t * lSq, ( 1 - t ) * ( 1 - t ) * lSq,
95 acSq - 2 * t * p + t * t * lSq } ) );
98 auto res = sqDistToNearest( T( 0.5 ) );
100 res = std::max( res, sqDistToNearest( acSq / ( 2 * p ) ) );
102 res = std::max( res, sqDistToNearest( ( lSq - acSq ) / ( 2 * ( lSq - p ) ) ) );
117 const auto ab = ( b - a ).lengthSq();
118 const auto ca = ( a - c ).lengthSq();
119 const auto bc = ( c - b ).lengthSq();
123 if ( ca < bc + ab && bc < ab + ca && ab < ca + bc )
124 return ab * ca * bc / ( 4 *
cross( b - a, c - a ).lengthSq() );
128 if ( ab >= bc && ab >= ca )
152 const auto ab =
dot( a, b );
153 return ( bb * ( aa - ab ) * a + aa * ( bb - ab ) * b ) / ( 2 * xabSq );
176 const auto rr =
sqr( radius );
178 if ( rr < circRadSq )
181 const auto x = std::sqrt( rr - circRadSq );
182 const auto xn = x *
normal( a, b, c );
184 centerPos = circCenter + xn;
185 centerNeg = circCenter - xn;
198 const auto ab = ( b - a ).
length();
199 const auto ca = ( a - c ).
length();
200 const auto bc = ( c - b ).
length();
201 if ( ab <= 0 || ca <= 0 || bc <= 0 )
203 const auto f =
cross( b - a, c - a ).length();
204 return f * std::min( { ab, ca, bc } ) / ( ab * ca * bc );
224 const auto bc = ( c - b ).
length();
225 const auto ca = ( a - c ).
length();
226 const auto ab = ( b - a ).
length();
227 auto halfPerimeter = ( bc + ca + ab ) / 2;
228 auto den = 8 * ( halfPerimeter - bc ) * ( halfPerimeter - ca ) * ( halfPerimeter - ab );
230 return std::numeric_limits<T>::max();
232 return bc * ca * ab / den;
242 return cross( t[1] - t[0], t[2] - t[0] );
252 return cross( q, r );
262 return cross( q - p, r - p );
282 return normal( q - p, r - p );
292 return normal( t[1] - t[0], t[2] - t[0] );
310[[nodiscard]]
inline T
dblArea(
const Triangle3<T> & t )
342 return std::abs(
cross( q - p, r - p ) );
362 const auto c = ( t[0] + t[1] + t[2] ) / T(3);
365 for (
int i = 0; i < 3; ++i )
367 const auto sq = ( t[i] - c ).lengthSq();
368 if ( longestSq >= sq )
373 const auto d = ( t[longest] - c ).normalized();
377 for (
int i = 0; i < 3; ++i )
378 res[i] = c + d *
dot( d, t[i] - c );
390 const auto c = ( t[0] + t[1] + t[2] ) / T(3);
392 for (
int i = 0; i < 3; ++i )
393 res[i] = t[i] - n *
dot( n, t[i] - c );
412 return dot( edgeDir,
cross( leftNorm, rightNorm ) );
426 return dot( leftNorm, rightNorm );
443 return std::atan2( sin, cos );
462 const auto aa =
sqr( a );
463 const auto y = ( aa -
sqr( b ) +
sqr( c ) ) / ( 2 * c );
464 const auto yy =
sqr( y );
467 const auto x = std::sqrt( aa - yy );
488 auto y = ( p->x * p1->y - p1->x * p->y ) / ( p->x - p1->x );
489 if ( y < 0 || y > c )
491 return ( *p - *p1 ).length();
503 const T den = ( a + b + c ) * ( b + c - a );
505 return std::numeric_limits<T>::infinity();
506 const T num = ( a + c - b ) * ( a + b - c );
518[[nodiscard]]
inline T
cotan(
const Triangle3<T> & t, T absMaxVal = std::numeric_limits<T>::max() )
520 auto a = t[0] - t[2];
521 auto b = t[1] - t[2];
522 auto nom =
dot( a, b );
523 auto den =
cross( a, b ).length();
524 if ( fabs( nom ) >= absMaxVal * den )
525 return absMaxVal *
sgn( nom );
536[[nodiscard]]
inline T
cotan( T a, T b, T c )
538 const T den = ( a + b + c ) * ( b + c - a );
540 return -std::numeric_limits<T>::infinity();
541 const T num = ( a + c - b ) * ( a + b - c );
543 return std::numeric_limits<T>::infinity();
544 const auto tanSq = num / den;
545 return ( 1 - tanSq ) / ( 2 * std::sqrt( tanSq ) );
556 const auto bb =
dot( b, b );
557 const auto bc =
dot( b, c );
558 const auto cc =
dot( c, c );
559 const auto det = bb * cc - bc * bc;
562 const auto kb = ( 1 / det ) * ( cc * vb - bc * vc );
563 const auto kc = ( 1 / det ) * (-bc * vb + bb * vc );
564 return kb * b + kc * c;
586 const auto gradSq = grad.
lengthSq();
589 const auto d = c - b;
591 const auto gort = d - (
dot( d, grad ) / gradSq ) * grad;
592 const auto god =
dot( gort, d );
595 const auto gob = -
dot( gort, b );
596 if ( gob <= 0 || gob >= god )
598 const auto a = gob / god;
599 assert( a < std::numeric_limits<T>::max() );
600 const auto ip = a * c + ( 1 - a ) * b;
601 if (
dot( grad, ip ) >= 0 )
619 if ( !grad.has_value() )
621 auto gradSq = grad->lengthSq();
624 return sqrt( 1 - gradSq ) *
normal( b, c ) - *grad;
Plane3
Definition MRMeshFwd.h:390
float area(const MeshTopology &topology, const VertCoords &points, FaceId f)
returns the area of given face
Definition MRMeshMath.h:168
float dihedralAngleSin(const MeshTopology &topology, const VertCoords &points, UndirectedEdgeId e)
constexpr auto sqr(T x) noexcept -> decltype(x *x)
squared value; the result type is the type of x*x, which is int for small integer types (char,...
Definition MRMeshFwd.h:769
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
Vector3< T > circumcircleCenter(const Vector3< T > &a, const Vector3< T > &b)
Computes the center of the the triangle's 0AB circumcircle.
Definition MRTriMath.h:140
auto lengthSq() const -> decltype(std::declval< T >() *std::declval< T >())
Definition MRVector3.h:68
std::optional< T > findTriExitPos(const Vector3< T > &b, const Vector3< T > &c, const Vector3< T > &grad)
Definition MRTriMath.h:584
std::optional< T > quadrangleOtherDiagonal(T a, T b, T c, T a1, T b1)
Definition MRTriMath.h:478
Triangle3< T > makeDegenerate(const Triangle3< T > &t)
make degenerate triangle (all 3 points on a line) that maximally resembles the input one and has the ...
Definition MRTriMath.h:360
float triangleAspectRatio(const MeshTopology &topology, const VertCoords &points, FaceId f)
returns aspect ratio of given mesh triangle equal to the ratio of the circum-radius to twice its in-r...
T tanSqOfHalfAngle(T a, T b, T c)
Definition MRTriMath.h:501
Triangle3< T > triangleWithNormal(const Triangle3< T > &t, const Vector3< T > &n)
Definition MRTriMath.h:388
Vector3f dirDblArea(const MeshTopology &topology, const VertCoords &points, FaceId f)
computes directed double area for a triangular face from its vertices
Definition MRMeshMath.h:153
float dihedralAngleCos(const MeshTopology &topology, const VertCoords &points, UndirectedEdgeId e)
constexpr int sgn(T x) noexcept
sign of given value in { -1, 0, 1 }
Definition MRMeshFwd.h:773
std::optional< Vector3< T > > gradientInTri(const Vector3< T > &b, const Vector3< T > &c, T vb, T vc)
Definition MRTriMath.h:554
Triangle3< double > Triangle3d
Definition MRMeshFwd.h:467
float circumcircleDiameter(const MeshTopology &topology, const VertCoords &points, FaceId f)
returns circumcircle diameter of given mesh triangle
std::optional< Plane3< T > > tangentPlaneToSpheres(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c, T ra, T rb, T rc)
Definition MRTriMath.h:637
bool circumballCenters(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c, T radius, Vector3< T > ¢erPos, Vector3< T > ¢erNeg)
ball's center from the negative side of triangle
Definition MRTriMath.h:172
std::optional< Vector3< T > > tangentPlaneNormalToSpheres(const Vector3< T > &b, const Vector3< T > &c, T rb, T rc)
Definition MRTriMath.h:616
Vector3 normalized() const MR_REQUIRES_IF_SUPPORTED(std
Definition MRVector3.h:77
float dblArea(const MeshTopology &topology, const VertCoords &points, FaceId f)
returns twice the area of given face
Definition MRMeshMath.h:162
MR_BIND_TEMPLATE(std::pair< Vector3f, TriPointf > closestPointInTriangle(const Vector3f &p, const Vector3f &a, const Vector3f &b, const Vector3f &c))
T minTriangleAngle(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c)
Definition MRTriMath.h:211
Triangle3< float > Triangle3f
Definition MRMeshFwd.h:466
std::optional< Vector2< T > > posFromTriEdgeLengths(T a, T b, T c)
Definition MRTriMath.h:453
float circumcircleDiameterSq(const MeshTopology &topology, const VertCoords &points, FaceId f)
returns squared circumcircle diameter of given mesh triangle
float cotan(const MeshTopology &topology, const VertCoords &points, UndirectedEdgeId ue)
Definition MRMeshMath.h:315
float dihedralAngle(const MeshTopology &topology, const VertCoords &points, UndirectedEdgeId e)
T dblAreaSq(const Vector3< T > &p, const Vector3< T > &q, const Vector3< T > &r)
computes the square of double area of given triangle
Definition MRTriMath.h:300
length
Definition MRObjectDimensionsEnum.h:17
@ normal
Definition MRUnits.h:45
T mincircleDiameterSq(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c)
Definition MRTriMath.h:57
T edgeCoveringRadiusSq(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c)
Definition MRTriMath.h:80
T coveringRadiusSq(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c)
Definition MRTriMath.h:115
T minTriangleAngleSin(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c)
Definition MRTriMath.h:196
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRVector2.h:29
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