Python points distances to breps

I am looking for the code that measures the distance to the closest brep, or at least something with very little codelines because of the speculated calculation processes from between hundred points and hundred breps.

Do you might know if there is a better ‘pull code’ or something similar like the component version, but than for python? Rhinocommon?

Thank you for your response. :slight_smile:

problem points distances to breps 00.gh (10.1 KB)

something like this should work.
modify component:

  • set points to list access/type hint Point3D
  • set brps to item acess/type hint brep.
  • add two more outputs to your component, (lines and closestPts)
import Rhino

dist = []
closestPts = []
lines = [] #for example only

for pt in pnts:
    closestPt = Rhino.Geometry.Brep.ClosestPoint(brps, pt)
    dist.append(closestPt.DistanceTo(pt))
    lines.append(Rhino.Geometry.Line(closestPt, pt))
    closestPts.append(closestPt)

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Brep_ClosestPoint.htm
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Point3d_DistanceTo.htm

1 Like

But then it has to check every brep, am I right?
That is what I am worried about.

With hundred breps and hundred points, it would take 10.000 calculations to get the 100 closest distances.

I don’t think your going to get too much faster than native components. Yes, the example above would needt to check each point against each Brep. Have you tested it? Where does it fail? 500 points/breps, a million? I don’t think a few hundred breps/points would be a total catastrophe for native component, or the example above?

Do you might know what I am missing here?

Hi @ForestOwl,

My guess would be that line 12 should look something like this:
closestdistvals.append(distvals[0])

1 Like

When going for 500 points/breps, it is a catastrofe: a total catastrophe. It takes more than a minute. Or is this a case of affectation?

Below an image of 200 points/breps.

There is not another Rhinocommon code? [This is running from explosions (see gif).]

problem points distances to breps 01.gh (19.7 KB)

I want to check if the calculations can be done faster.

I want to try the statement any to see if a sphere around a point does intersect with any brep.
Maybe it is faster, maybe not.

The only problem is that I cannot let it work in the current setting. The sphere does not intersect any brep, and the statement says True.

Do you might know what I am doing wrong here? :slight_smile:

problem points distances to breps 02.gh (24.5 KB)

You can primarily speed up your code by refactoring it and implementing faster algorithms.

Instead of performing intersection tests on every box, you could for instance first find the immediate neighbourhood of your sphere and only perform the intersections on these boxes, instead of all the boxes.
In order to find the closest boxes to the sphere, you could evaluate distances. You know the radius R of your sphere and the radius D of the outer sphere of each box (box diagonal / 2). A box and the sphere can thus only intersect, if the distance between the sphere center point and the box center point is smaller or equal to the maximum distance R + D. The boxes found within that distance form the immediate neighbourhood and are evaluated for intersections.

If you want to optimise even further, you can implement the R-tree algorithm, available in rhinocommon, which does a similar thing to what I’ve described above, but much more efficiently.
You could also implement parallel computing. However, multithreading is oftentimes inferior to R-tree. When the bruteforce method, checking each box, needs n time to execute, parallel computing only speeds that up by a factor of n / p + log2p (p = processor cores), whereas R-tree for instance offers a speed up factor of n/log2n .

A very simple and straightforward way to test execution speeds of code portions is using time.

import time

start_time = time.time()
# Your test code
elapsed_time = time.time() - start_time

print elapsed_time

problem points distances to breps 03.gh (15.4 KB)

Your script works for me without issues! When the sphere intersects a box, it print True. If not, it outputs False. It seems to be totally fine!

2 Likes

Thank you :smiley: