2

For my project, I need to measure the distance between two STL files. I wrote a script that allows reading the files, positioning them in relation to each other in the desired position. Now, in the next step I need to check the distance from one object to the other. Is there a function or script available on a library that allows me to carry out this process? Because then I’m going to want to define metrics like interpenetration area, maximum negative distance etc etc so I need to check first the distance between those objects and see if there is like mesh intersection and mesure that distance. I put the url for the combination of the 2 objects that I want to mesure the distance:

https://i.stack.imgur.com/y5hFT.jpg

273K
  • 29,503
  • 10
  • 41
  • 64
Pedro
  • 23
  • 4
  • Which library are you using to work with STL? `stl`, `numpy-stl`, `pymesh`, `OpenMesh`? – Joe Apr 11 '20 at 17:43
  • https://stackoverflow.com/questions/51476722/intersection-3d-meshes-python – Joe Apr 11 '20 at 17:49
  • How professional do you need your solution to be? How do you define the distance? Closest nodes? Centroid to centroid? – Joe Apr 11 '20 at 17:51
  • Hello Joe. I wrote a script to read ASCII files, separated the information into arrays (normal, vertex 1, vertex 2 and vertex 3 of each triangle) so that I could translate and rotate all points. What I did was "indicate where the sphere should be", such as an assembly process in 3D CAD software (solidworks for example) – Pedro Apr 11 '20 at 21:24
  • Now I need to find a way to check if there is an intersection between meshes (in the position where the sphere is in relation to the other object). This is a job for my thesis in mechanical engineering and I believe it will certainly be through the closest nodes. I have an image (in this url: https://imgur.com/8RBfWaL) that I was sent to the way I should measure .. what I need to determine is that distance between the red dots. Im "new" to coding and im trying to improve day by day. Do you think im capable of making this with some of those libraries? – Pedro Apr 11 '20 at 21:31
  • The orange and blue is a representation of 2 different objects that intersect . This is what they advise me to do (for keep the in the correct way of what I already made): "now start thinking how you determine the distances between the two objects, always from the second in relation to the first (to be objective in the analysis). This way, you will then be able to define various metrics, for example, interpenetration area, maximum negative distance, areas at a given distance". And thank you so much for the attention ! – Pedro Apr 11 '20 at 21:34
  • Perhaps this help: https://stackoverflow.com/a/66717145/7127519 – Rafael Valero Mar 20 '21 at 00:27

2 Answers2

3

Pyvista offers a really easy way of calculating just that:

import pyvista as pv
import numpy as np

mesh_1 = pv.read(**path to mesh 1**)
mesh_2 = pv.read(**path to mesh 2**)

closest_cells, closest_points = mesh_2.find_closest_cell(mesh_1.points, return_closest_point=True)
d_exact = np.linalg.norm(mesh_1 .points - closest_points, axis=1)
print(f'mean distance is: {np.mean(d_exact)}')

For more methods and examples, have a look at:

https://docs.pyvista.org/examples/01-filter/distance-between-surfaces.html#using-pyvista-filter

Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31
Sash
  • 31
  • 3
2

To calculate the distance between two meshes, first one needs to check whether these meshes intersect. If not, then the resulting distance can be computed as the distance between two closest points, one from each mesh (as on the picture below).

Distance between cube and torus

If the meshes do intersect, then it is necessary to find the part of each mesh, which is inside the other mesh, then find two most distant points, one from each inner part. The distance between these points will be the maximum deepness of the meshes interpenetration. It can be returned with negative sign to distinguish it from the distance between separated meshes.

In Python, one can use MeshLib library and findSignedDistance function from it as follows:

import meshlib.mrmeshpy as mr
mesh1 = mr.loadMesh("Cube.stl")
mesh2 = mr.loadMesh("Torus.stl"))
z = mr.findSignedDistance(mesh1, mesh2)
print(z.signedDist) // 0.3624192774295807
Fedor
  • 17,146
  • 13
  • 40
  • 131