Connecting spheres at smaller minimum distance with cylinder structure

Hi guys,

For my project, I am trying to make a bed of spheres, which are connected to other(s) if the gap between them is smaller than a certain distance.

gap = rs.Distance(point1, point2) - radius1 - radius2;
tolerance = 0.050
val = gap/radius
if val <= tolerance, the connection shall be formed

The connecting structure between two spheres is a cylinder, whose radius is 0.08 times the radius of the sphere. Sphere radii are all uniform and a sphere could have more than one cylinder connection if the criteria are met. The illustration of the connection is given in the following figure.

In order to do that, I made the following Python code:

import rhinoscriptsyntax as rs
import math as math

def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
    return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

# determination of particle bridge is given as follows
def isConnected(point1, point2, radius1, radius2):
    gap = rs.Distance(point1, point2) - radius1 - radius2;
    tolerance = 0.050
    val = gap/radius
    return (isclose(val, tolerance) or (val < tolerance))


radius = 10.0
points = []
points.append([3.8004, -10.7703, -14.7366])
points.append([6.89814, 9.90892, -14.7361])
points.append([-14.1991, -1.3038, -14.7362])
points.append([-2.50297, -1.28871, 1.70578])
points.append([12.3037, -17.4367, 2.09313])
points.append([21.1603, -2.79959, -8.81348])
points.append([-11.5506, -18.1149, -4.23054])
points.append([-11.1571, 18.5256, -14.736])
points.append([4.63671, 21.0576, 1.71328])
points.append([-19.803, 8.74576, 1.62201])
points.append([19.4772, 8.88409, 7.33124])
points.append([19.7547, -8.05903, 18.1097])
points.append([8.40554, -19.6233, 29.834])
points.append([19.879, 7.92851, 30.1251])
points.append([4.80376, 21.0154, 28.916])
points.append([-14.0158, -14.7737, 29.5774])
points.append([-19.7362, -8.70016, 11.4009])
points.append([-3.47392, -21.1316, 13.8154])
points.append([-20.8642, 5.75207, 25.1795])
points.append([3.10321, 2.98932, 20.421])
points.append([-9.83524, 19.2703, 15.4012])
points.append([-21.4772, -2.41992, 43.4234])
points.append([19.6639, -8.27222, 41.8506])
points.append([-12.3412, 17.7824, 38.6932])
points.append([-2.90253, 21.4069, 55.9492])
points.append([-0.923667, 0.666779, 39.873])
points.append([-3.82574, -21.0765, 45.5909])
points.append([14.4364, 15.8948, 47.644])
points.append([-7.73744, -5.57666, 57.6097])

for i in range(len(points)):
    rs.AddSphere(points[i], radius)
    rs.AddPoint(points[i], radius)

#forming connections 

for i in range(len(points)):
    for j in range(i+1, len(points)):
        if (isConnected(points[i], points[j], radius, radius)):
            rs.AddCylinder(points[i], rs.Distance(points[i], points[j]), 0.08 * radius, cap=True)

However, the structure generated by Rhino is not what I hope for:

Instead of connecting two center points of spheres, the cylinders are just formed vertically out of the core in rather random manner. I dont know whats wrong with my coding, could somebody help me to fix it? Thanks in advance.

Hi Anthony,

To get the cylinder oriented correctly you need to pass the ‘base’ argument as a plane.
For else the worldXY plane is assumed as the cylinder direction

for i in range(len(points)):
    for j in range(i+1, len(points)):
        if (isConnected(points[i], points[j], radius, radius)):            
            direction_vector = rs.VectorCreate(points[j], points[i])
            cylinder_plane = rs.PlaneFromNormal(points[i],direction_vector)
            
            rs.AddCylinder(cylinder_plane, rs.Distance(points[i], points[j]), 0.08 * radius, cap=True)

-Willem

1 Like