Repository navigation
Intersection line between 2 triangles #7235
|
Hi! I'm new to this library, so, still not familiar with the API. I'm trying to get the intersection line between 2 3D triangles (2x Triangle3f), how can this be achieved with meshlib? Thanks in advance! |
Replies: 2 comments 1 reply
|
for the full intersection segment one MeshLib approach is to build a single face i checked this through the Python bindings in MeshLib 3.1.4.297 with two noncoplanar triangles and got one contour with two endpoints a disjoint pair returned no contours for that noncoplanar segment case the two contour points are your segment endpoints coplanar overlap needs separate handling since it can be an area
|
|
Hi! If two triangles are not coplanar, their intersection is a segment, and each endpoint of it is where an edge of one triangle crosses the other triangle. So you can test all 6 edges with the functions from #include "MRMesh/MRTriangleIntersection.h"
using namespace MR;
// returns 2 points (the ends of the intersection segment) or nothing if the triangles do not intersect
std::vector<Vector3f> intersectTriangles( const Triangle3f& t1, const Triangle3f& t2 )
{
std::vector<Vector3f> res;
auto test = [&] ( const Triangle3f& t, const Vector3f& p, const Vector3f& q )
{
if ( doTriangleSegmentIntersect( t[0], t[1], t[2], p, q ) )
res.push_back( findTriangleSegmentIntersection( t[0], t[1], t[2], p, q ) );
};
for ( int i = 0; i < 3; ++i )
{
test( t2, t1[i], t1[( i + 1 ) % 3] );
test( t1, t2[i], t2[( i + 1 ) % 3] );
}
return res;
}It is fast and works in general position, but these are plain floating-point tests: if a vertex of one triangle lies exactly in the plane of the other, or an edge exactly hits an edge, you may get only 1 point (or 0), and coplanar triangles always give 0 points. If you need a result that is robust in such degenerate cases, use the approach suggested above: make a one-triangle mesh from each triangle and call #include "MRMesh/MRMesh.h"
#include "MRMesh/MRMeshBoolean.h"
auto meshA = Mesh::fromPointTriples( { t1 }, false );
auto meshB = Mesh::fromPointTriples( { t2 }, false );
Contours3f conts = findIntersectionContours( meshA, meshB );
// conts is empty if no intersection, otherwise conts[0] holds the segment's pointsThis is slower because of mesh construction, so for millions of triangle pairs the first variant is preferable. |
for the full intersection segment one MeshLib approach is to build a single face
MR::Meshfrom eachTriangle3fusingMR::Mesh::fromTrianglesthen callMR::findIntersectionContours(meshA, meshB)from<MRMesh/MRMeshBoolean.h>i checked this through the Python bindings in MeshLib 3.1.4.297 with two noncoplanar triangles and got one contour with two endpoints a disjoint pair returned no contours
for that noncoplanar segment case the two contour points are your segment endpoints coplanar overlap needs separate handling since it can be an area
API docs
findTriangleTriangleIntersectioninMRTriangleIntersection.honly returns one intersection point so it does not directly give both endpoints