Skip to content

Commit

Permalink
debugged AABBTree.FindNearestTriangles(AABBTree). Tested it, and Test…
Browse files Browse the repository at this point in the history
…Intersection(AABBTree). Simplified some of the aabb/aabb code. Added documentation. Added to MeshQueries to support these functions (linear-search versions, fast query-constructors)
  • Loading branch information
rms80 committed Nov 23, 2017
1 parent 8776397 commit f9b48b4
Show file tree
Hide file tree
Showing 2 changed files with 306 additions and 123 deletions.
91 changes: 91 additions & 0 deletions queries/MeshQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,50 @@ public static IntrRay3Triangle3 TriangleIntersection(DMesh3 mesh, int ti, Ray3d
}


/// <summary>
/// convenience function to construct a IntrTriangle3Triangle3 object for two mesh triangles
/// </summary>
public static IntrTriangle3Triangle3 TrianglesIntersection(DMesh3 mesh1, int ti, DMesh3 mesh2, int tj, Func<Vector3d, Vector3d> TransformF = null)
{
if (mesh1.IsTriangle(ti) == false || mesh2.IsTriangle(tj) == false)
return null;
Triangle3d tri1 = new Triangle3d(), tri2 = new Triangle3d();
mesh1.GetTriVertices(ti, ref tri1.V0, ref tri1.V1, ref tri1.V2);
mesh2.GetTriVertices(tj, ref tri2.V0, ref tri2.V1, ref tri2.V2);
if (TransformF != null) {
tri2.V0 = TransformF(tri2.V0);
tri2.V1 = TransformF(tri2.V1);
tri2.V2 = TransformF(tri2.V2);
}
IntrTriangle3Triangle3 intr = new IntrTriangle3Triangle3(tri1, tri2);
intr.Find();
return intr;
}


/// <summary>
/// convenience function to construct a DistTriangle3Triangle3 object for two mesh triangles
/// </summary>
public static DistTriangle3Triangle3 TrianglesDistance(DMesh3 mesh1, int ti, DMesh3 mesh2, int tj, Func<Vector3d,Vector3d> TransformF = null)
{
if (mesh1.IsTriangle(ti) == false || mesh2.IsTriangle(tj) == false)
return null;
Triangle3d tri1 = new Triangle3d(), tri2 = new Triangle3d();
mesh1.GetTriVertices(ti, ref tri1.V0, ref tri1.V1, ref tri1.V2);
mesh2.GetTriVertices(tj, ref tri2.V0, ref tri2.V1, ref tri2.V2);
if (TransformF != null) {
tri2.V0 = TransformF(tri2.V0);
tri2.V1 = TransformF(tri2.V1);
tri2.V2 = TransformF(tri2.V2);
}
DistTriangle3Triangle3 dist = new DistTriangle3Triangle3(tri1, tri2);
dist.GetSquared();
return dist;
}




/// <summary>
/// Find point-normal frame at ray-intersection point on mesh, or return false if no hit.
/// Returns interpolated vertex-normal frame if available, otherwise tri-normal frame.
Expand Down Expand Up @@ -322,6 +366,53 @@ public static int FindHitTriangle_LinearSearch(DMesh3 mesh, Ray3d ray)



/// <summary>
/// brute force search for any intersecting triangles on two meshes
/// </summary>
public static Index2i FindIntersectingTriangles_LinearSearch(DMesh3 mesh1, DMesh3 mesh2)
{
foreach (int ti in mesh1.TriangleIndices()) {
Vector3d a = Vector3d.Zero, b = Vector3d.Zero, c = Vector3d.Zero;
mesh1.GetTriVertices(ti, ref a, ref b, ref c);
foreach (int tj in mesh2.TriangleIndices()) {
Vector3d e = Vector3d.Zero, f = Vector3d.Zero, g = Vector3d.Zero;
mesh2.GetTriVertices(tj, ref e, ref f, ref g);
IntrTriangle3Triangle3 intr = new IntrTriangle3Triangle3(new Triangle3d(a, b, c), new Triangle3d(e, f, g));
if (intr.Test()) {
return new Index2i(ti, tj);
}
}
}
return Index2i.Max;
}


/// <summary>
/// Brute-force search for closest pair of triangles on two meshes
/// </summary>
public static Index2i FindNearestTriangles_LinearSearch(DMesh3 mesh1, DMesh3 mesh2, out double fNearestSqr)
{
Index2i nearPair = Index2i.Max;
fNearestSqr = double.MaxValue;
foreach (int ti in mesh1.TriangleIndices()) {
Vector3d a = Vector3d.Zero, b = Vector3d.Zero, c = Vector3d.Zero;
mesh1.GetTriVertices(ti, ref a, ref b, ref c);
foreach ( int tj in mesh2.TriangleIndices() ) {
Vector3d e = Vector3d.Zero, f = Vector3d.Zero, g = Vector3d.Zero;
mesh2.GetTriVertices(tj, ref e, ref f, ref g);

DistTriangle3Triangle3 dist = new DistTriangle3Triangle3(new Triangle3d(a, b, c), new Triangle3d(e, f, g));
if ( dist.GetSquared() < fNearestSqr ) {
fNearestSqr = dist.GetSquared();
nearPair = new Index2i(ti, tj);
}
}
}
fNearestSqr = Math.Sqrt(fNearestSqr);
return nearPair;
}



public static void EdgeLengthStats(DMesh3 mesh, out double minEdgeLen, out double maxEdgeLen, out double avgEdgeLen, int samples = 0)
{
Expand Down
Loading

0 comments on commit f9b48b4

Please sign in to comment.