MeshLib icon indicating copy to clipboard operation
MeshLib copied to clipboard

How can I calculate the minimum distance between two meshes and return the nearest point in C#?

Open 6ther opened this issue 6 months ago • 19 comments

6ther avatar Aug 02 '25 17:08 6ther

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.

Fedr avatar Aug 03 '25 06:08 Fedr

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.

6ther avatar Aug 03 '25 10:08 6ther

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.

Fedr avatar Aug 03 '25 13:08 Fedr

I can search for the closest distance in the list, but I do not know whitch corresponding vertex of mesh calcualte the closest distance.

6ther avatar Aug 04 '25 06:08 6ther

If you find that the minimum distance is in ith element of the list, then the corresponding vertex has ID i.

Fedr avatar Aug 04 '25 10:08 Fedr

I see. There is a problem, that is, the minimum distance between two meshes may not necessarily be at the vertex, isn't it?

6ther avatar Aug 04 '25 11:08 6ther

Indeed, in general the minimal distance between two triangular surfaces can be achieved:

  1. 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),
  2. 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#.

Fedr avatar Aug 04 '25 13:08 Fedr

thanks a lot.

6ther avatar Aug 06 '25 00:08 6ther

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.

Fedr avatar Aug 07 '25 09:08 Fedr

Image I find that new version is released in nuget and I update, but I do not find Lib folder and related dll.

6ther avatar Aug 13 '25 09:08 6ther

It seems like a bug, thanks for reporting we will fix it ASAP

Grantim avatar Aug 13 '25 10:08 Grantim

I find another bug in v3.0.7.237:

Image

6ther avatar Aug 13 '25 12:08 6ther

v3.0.7.237 was corrupted, we have just uploaded v3.0.7.238, it should be ok, could you please verify?

Grantim avatar Aug 13 '25 12:08 Grantim

yes, thanks.

6ther avatar Aug 14 '25 00:08 6ther

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]|)

6ther avatar Aug 18 '25 12:08 6ther

Could you please say:

  1. What is your Linux distribution?
  2. What is your platform: x86 or Arm?

Fedr avatar Aug 18 '25 14:08 Fedr

[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 /]#

6ther avatar Aug 19 '25 00:08 6ther

Many dependency libraries are missing: Image

Image What should I do?

6ther avatar Aug 19 '25 06:08 6ther

Hello, unfortunately we don't support CentOS 7 due to outdated system libraries. CentOS 8 support might be added in the nearest future.

oitel avatar Aug 19 '25 07:08 oitel