import numpy as np
def fibonacci_capsule(N, height=2.0, radius=1.0, ref_point=None):
"""
Generates points on a capsule aligned along the Z-axis,
only generating points on the side opposite to ref_point.
If ref_point is None, returns full capsule.
"""
points = []
# Normalize inputs
N = int(N)
h = height
r = radius
top_hemi_N = int(N * r / (h + 2*r))
bottom_hemi_N = top_hemi_N
cylinder_N = N - top_hemi_N - bottom_hemi_N
def generate_hemisphere(num_points, z_offset, flip=False):
"""Generates a hemisphere either top or bottom"""
hemisphere = []
for i in range(num_points):
s = (i + 0.5) / num_points
phi = np.pi * (3 - np.sqrt(5)) * i # golden angle increment
# Map to hemisphere
cos_theta = 1 - s if not flip else s
sin_theta = np.sqrt(1 - cos_theta**2)
x = np.cos(phi) * sin_theta * r
y = np.sin(phi) * sin_theta * r
z = cos_theta * r + z_offset
hemisphere.append((x, y, z))
return hemisphere
def generate_cylinder(num_points):
"""Generates points along the cylindrical body"""
cylinder = []
golden_ratio = (1 + np.sqrt(5)) / 2
for i in range(num_points):
z = -r + (i / num_points) * h
theta = 2 * np.pi * (i / golden_ratio) % (2 * np.pi)
x = np.cos(theta) * r
y = np.sin(theta) * r
cylinder.append((x, y, z))
return cylinder
# Generate all parts
top_hemi = generate_hemisphere(top_hemi_N, z_offset=r)
bottom_hemi = generate_hemisphere(bottom_hemi_N, z_offset=-r, flip=True)
cylinder = generate_cylinder(cylinder_N)
full_capsule = top_hemi + bottom_hemi + cylinder
if ref_point is None:
return full_capsule
# If ref_point is provided, project it onto the capsule and find its normal
# Normalize ref_point to lie on the capsule
ref_vec = np.array(ref_point)
norm = np.linalg.norm(ref_vec[:2]) # radial component
# Find closest point on capsule
if ref_vec[2] > r:
closest = ref_vec.copy()
closest[2] = r
closest[:2] = closest[:2] / np.linalg.norm(closest[:2]) * r
closest[2] += r
normal = (closest - [0, 0, r])
elif ref_vec[2] < -r:
closest = ref_vec.copy()
closest[2] = -r
closest[:2] = closest[:2] / np.linalg.norm(closest[:2]) * r
closest[2] -= r
normal = (closest - [0, 0, -r])
else:
closest = np.array([ref_vec[0], ref_vec[1], ref_vec[2]])
closest[:2] = closest[:2] / np.linalg.norm(closest[:2]) * r
normal = closest[:2].tolist() + [0]
normal = normal / np.linalg.norm(normal)
# Now, we compute the direction we want: opposite to the normal
desired_dir = -normal
# Rotate coordinate system so that desired_dir aligns with +Z axis
# This allows us to restrict spherical coordinates to a hemisphere
up = np.array([0, 0, 1])
rot_axis = np.cross(desired_dir, up)
if np.linalg.norm(rot_axis) > 1e-6:
rot_axis /= np.linalg.norm(rot_axis)
angle = np.arccos(np.dot(desired_dir, up))
from scipy.spatial.transform import Rotation as R
rotation = R.from_rotvec(angle * rot_axis).as_matrix()
else:
rotation = np.eye(3)
# Recompute all parts with directional constraint
constrained_points = []
def generate_constrained_fibonacci(num_points, callback):
new_points = []
for i in range(num_points):
s = (i + 0.5) / num_points
phi = np.pi * (3 - np.sqrt(5)) * i
cos_theta = 1 - 2 * s
sin_theta = np.sqrt(1 - cos_theta**2)
x = np.cos(phi) * sin_theta
y = np.sin(phi) * sin_theta
z = cos_theta
p = np.array([x, y, z])
# Only keep points where dot(p, up) >= 0 => upper hemisphere
if np.dot(p, up) < 0:
continue
p = np.dot(rotation.T, p) # rotate back to original space
scaled_p = p * r
if callable(callback):
scaled_p = callback(scaled_p)
new_points.append(tuple(scaled_p))
return new_points
def hemi_top_callback(p):
p[2] += r
return p
def hemi_bottom_callback(p):
p[2] = -p[2] - r
return p
def cylinder_from_spherical(p):
# Convert spherical to cylindrical coordinates
x = p[0]
y = p[1]
z = p[2]
# Clamp z to cylinder range [-r, h - r]
z_clamped = np.clip(z, -r, h - r)
return [x, y, z_clamped]
constrained_top = generate_constrained_fibonacci(top_hemi_N, hemi_top_callback)
constrained_bottom = generate_constrained_fibonacci(bottom_hemi_N, hemi_bottom_callback)
constrained_cylinder = generate_constrained_fibonacci(cylinder_N, cylinder_from_spherical)
constrained_points = constrained_top + constrained_bottom + constrained_cylinder
return constrained_points
# Example usage
if __name__ == "__main__":
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
ref_point = [0, 0, 2] # On top of the capsule
points = fibonacci_capsule(1000, height=2.0, radius=1.0, ref_point=ref_point)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xs, ys, zs = zip(*points)
ax.scatter(xs, ys, zs, s=10)
ax.set_box_aspect([1,1,1])
plt.show()