Curve-mesh proximity: I want the closest point from a curve to a mesh

How can I get the closest point from a curve to a mesh?

Thank you for your response. :slight_smile:

problem curve mesh proximity (12.2 KB)

This would be a possibility:

Depending on your curve length, you need to crank up the resolution!

problem curve mesh proximity (12.0 KB)

1 Like

Few lines of code will do it:

problem curve mesh proximity (14.4 KB)

1 Like

Is this also possible in Python you think? I tried it in Python but it did not work.

What I am trying to do is instead of pulling a point towards a mesh finding out ‘on which height’ the closest point to the mesh is.
Doing it between a curve and a brep seems also not possible in Python.

Almost 8 years ago, is it possible to do the same of what RadovanG did in C# but than in Python?

Damn, you revived an old thread here, @ForestOwl! :slight_smile:

Have you tried something akin to this:

# Inputs: Mesh (mesh), Lines ([line, ...])
import Rhino.Geometry as rg

def divide_line(line, step_size):
    for i in xrange(int(ln.Length/step_size)):
        yield ln.PointAtLength(step_size * i)

resolution = 0.01

closest_mpts = []
closest_lpts = []

for ln in Lines:
    div_pts = divide_line(ln, resolution)
    closest_mpt = None
    closest_mdist = float("inf")
    for div_pt in div_pts:
        pt = Mesh.ClosestPoint(div_pt)
        dist = pt.DistanceTo(div_pt)
        if dist < closest_mdist:
            closest_mdist = dist
            closest_mpt = pt
    closest_lpt = ln.ClosestPoint(closest_mpt, True)
a = closest_mpts
b = closest_lpts

This is much faster than the C# script from above and the result seems to be far more correct (if I’m not mistaken)! For instance, the closest point between a mesh and a line that intersects the latter in one point, must be this intersection point.

Or are you looking for something else than “the closesest point from a (line) curve to a mesh”?

1 Like
for i,p in enumerate(mainMsPnts):
    for j,m in enumerate(lockMshs):
        for k,lp in enumerate(linePnts):

“generator object is not subscriptable”
Do you might know why it says that? It is about the last rule.

I guess you’ve renamed my function and are using it to do the line division?
It yields (returns) a generator object, not a list!
Generators are very fast by nature, but not subscriptable!
You can only iterate through them item by item, like I do in my example, and only once.

For your example, you can only do this…:

for pt in linePnts:

… and only once! After this the generator is expired.