Sphere Packing

Hello
I wanted to do the Sphere Packing method with Python but I couldn’t.
Sphere Packing.gh (45.1 KB)

import Rhino.Geometry as rg
if 'pts' not in globals() or reset:
    pts = iPts
counts = [0]*len(pts)
vecs = [rg.Vector3d.Zero]*len(pts)
for i in range(len(pts)):
    for j in range(i+1,len(pts)):
        d = pts[i].DistanceTo(pts[j])
        if d < 2:
            counts[i] += 1
            counts[j] += 1
            vec= pts[i] - pts[j]
            vec.Unitize()
            vec *= (2-d) * 0.5
            vecs[i] += vec
            vecs[j] -= vec
for i in range(len(pts)):
    if counts[i] > 0:
        pts[i] += vecs[i] / counts[i]
    pts[i] = brep.ClosestPoint(pts[i])
a = pts

Fatemeh.gh (29.6 KB)

5 Likes