How can I calculate the minimum distance between two meshes and return the nearest point in C#?
In C++ we have a function for this in MeshLib/source/MRMesh/MRMeshMeshDistance.h:
struct MeshMeshDistanceResult
{
/// two closest points: from meshes A and B respectively
PointOnFace a, b;
/// squared distance between a and b
float distSq = 0;
};
/**
* \brief computes minimal distance between two meshes or two mesh regions
* \param rigidB2A rigid transformation from B-mesh space to A mesh space, nullptr considered as identity transformation
* \param upDistLimitSq upper limit on the distance in question, if the real distance is larger than the function exists returning upDistLimitSq and no valid points
*/
MRMESH_API MeshMeshDistanceResult findDistance( const MeshPart & a, const MeshPart & b,
const AffineXf3f * rigidB2A = nullptr, float upDistLimitSq = FLT_MAX );
but it is not exposed in C# yet. We will try to add it in the nearest future.
I find that "FindSignedDistances" function is exposed in C#, but I do not know if it is used to calculate minimum distance because it's return result is a float list.
Yes, in C# we have
/// Computes signed distances from valid vertices of test mesh to the closest point on the reference mesh:
/// positive value - outside reference mesh, negative - inside reference mesh;
/// this method can return wrong sign if the closest point is located on self-intersecting part of the mesh
unsafe public static VertScalars FindSignedDistances(Mesh refMesh, Mesh mesh, MeshProjectionParameters parameters)
but it computes not one closest distance, but the distance from each vertex of mesh to refMesh. That is why the return type is float list. You can manually then search for the closest distance in the list. And then project again the corresponding vertex of mesh to refMesh to find the closest point on the later.
I can search for the closest distance in the list, but I do not know whitch corresponding vertex of mesh calcualte the closest distance.
If you find that the minimum distance is in ith element of the list, then the corresponding vertex has ID i.
I see. There is a problem, that is, the minimum distance between two meshes may not necessarily be at the vertex, isn't it?
Indeed, in general the minimal distance between two triangular surfaces can be achieved:
- if one of the closest points is located in a vertex (and the other in any place of the closest triangle on the other mesh),
- if both of the closest points are located on the edges of both meshes.
The current solution present in C# covers only 1). But it will give you a good approximation from above (the actual closest distance can be only smaller).
The other function that is present in C++ for now, will cover the general case after exposing in C#.
thanks a lot.
We have just added requested function in master branch of MeshLib:
/**
* \brief computes minimal distance between two meshes or two mesh regions
* \param rigidB2A rigid transformation from B-mesh space to A mesh space, nullptr considered as identity transformation
* \param upDistLimitSq upper limit on the distance in question, if the real distance is larger than the function exists returning upDistLimitSq and no valid points
*/
static public MeshMeshDistanceResult FindDistance(MeshPart a, MeshPart b, AffineXf3f? rigidB2A = null, float upDistLimitSq = float.MaxValue)
It will be available in Nuget after the nearest release, hopefully this week.
It seems like a bug, thanks for reporting we will fix it ASAP
I find another bug in v3.0.7.237:
v3.0.7.237 was corrupted, we have just uploaded v3.0.7.238, it should be ok, could you please verify?
yes, thanks.
I found that when running on a Linux system, the error is as follows: hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|报错信息:One or more errors occurred. (Unable to load shared library 'MRMeshC' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable: hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|/root/.net/GraphdbApp/kqzLzZDUuyYvAEWPGc_RhiE7GR8FDH4=/MRMeshC.so: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|/.dotnet/shared/Microsoft.NETCore.App/7.0.11/MRMeshC.so: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|/home/MRMeshC.so: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|libGL.so.1: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|/.dotnet/shared/Microsoft.NETCore.App/7.0.11/libMRMeshC.so: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|/home/libMRMeshC.so: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|/root/.net/GraphdbApp/kqzLzZDUuyYvAEWPGc_RhiE7GR8FDH4=/MRMeshC: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|/.dotnet/shared/Microsoft.NETCore.App/7.0.11/MRMeshC: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|/home/MRMeshC: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|/root/.net/GraphdbApp/kqzLzZDUuyYvAEWPGc_RhiE7GR8FDH4=/libMRMeshC: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|/.dotnet/shared/Microsoft.NETCore.App/7.0.11/libMRMeshC: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|/home/libMRMeshC: cannot open shared object file: No such file or directory hdec-test-bps-graphdb-app|2025-08-18 20:38:41.405|INFO | N/A|Thread-27|com.hdec.bps.cmd.proxy.infrastructure.utils.CmdUtil[57]|)
Could you please say:
- What is your Linux distribution?
- What is your platform: x86 or Arm?
[root@bps-graphdb-app-58b4964c84-ngwxx /]# uname -a Linux bps-graphdb-app-58b4964c84-ngwxx 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux [root@bps-graphdb-app-58b4964c84-ngwxx /]# uname -r 3.10.0-1160.76.1.el7.x86_64 [root@bps-graphdb-app-58b4964c84-ngwxx /]# cat /etc/centos-release CentOS Linux release 7.9.2009 (Core) [root@bps-graphdb-app-58b4964c84-ngwxx /]#
Many dependency libraries are missing:
Hello, unfortunately we don't support CentOS 7 due to outdated system libraries. CentOS 8 support might be added in the nearest future.