Fibonacci Sphere on a Capsule
The Fibonacci sphere algorithm is a method for distributing points evenly on the surface of a sphere. Here, we will apply this algorithm to a capsule (a cylinder with hemispherical ends) and modify it to only include points on the side of a given point.
Code
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def fibonacci_sphere_on_capsule(num_points, radius, height, point):
"""
Generate points on a capsule using the Fibonacci sphere algorithm,
only including points on the side of a given point.
Parameters:
num_points (int): Number of points to generate.
radius (float): Radius of the capsule.
height (float): Height of the cylindrical part of the capsule.
point (numpy array): Point to determine the side of the capsule.
Returns:
points (numpy array): Points on the capsule.
"""
# Calculate the number of points on the spherical ends
num_sphere_points = int(num_points * (2 * radius) / (2 * radius + height))
# Generate points on the spherical ends
sphere_points = fibonacci_sphere(num_sphere_points, radius)
# Shift points to the correct position on the capsule
top_sphere_points = sphere_points + np.array([0, 0, height/2 + radius])
bottom_sphere_points = sphere_points + np.array([0, 0, -height/2 - radius])
# Generate points on the cylindrical part
num_cylinder_points = num_points - 2 * num_sphere_points
cylinder_points = fibonacci_cylinder(num_cylinder_points, radius, height)
# Combine points
points = np.vstack((top_sphere_points, cylinder_points, bottom_sphere_points))
# Calculate the normal vector of the plane that separates the capsule into two sides
normal_vector = point / np.linalg.norm(point)
# Calculate the angle between the normal vector and the z-axis
theta = np.arccos(normal_vector[2])
# Calculate the azimuthal angle of the normal vector
phi = np.arctan2(normal_vector[1], normal_vector[0])
# Generate points only on the side of the point
points = generate_points_on_side(num_points, radius, height, theta, phi)
return points
def fibonacci_sphere(num_points, radius):
"""
Generate points on a sphere using the Fibonacci sphere algorithm.
Parameters:
num_points (int): Number of points to generate.
radius (float): Radius of the sphere.
Returns:
points (numpy array): Points on the sphere.
"""
indices = np.arange(0, num_points, dtype=float) + 0.5
phi = np.arccos(1 - 2*indices/num_points)
theta = np.pi * (1 + 5**0.5) * indices
x, y, z = np.cos(theta) * np.sin(phi), np.sin(theta) * np.sin(phi), np.cos(phi)
points = np.column_stack((x, y, z)) * radius
return points
def fibonacci_cylinder(num_points, radius, height):
"""
Generate points on a cylinder using a modified Fibonacci algorithm.
Parameters:
num_points (int): Number of points to generate.
radius (float): Radius of the cylinder.
height (float): Height of the cylinder.
Returns:
points (numpy array): Points on the cylinder.
"""
indices = np.arange(0, num_points, dtype=float) + 0.5
theta = np.pi * (1 + 5**0.5) * indices
z = height * (indices / num_points - 0.5)
x, y = np.cos(theta), np.sin(theta)
points = np.column_stack((x, y, z)) * np.array([radius, radius, 1])
return points
def generate_points_on_side(num_points, radius, height, theta, phi):
"""
Generate points on a capsule, only on the side of a given point.
Parameters:
num_points (int): Number of points to generate.
radius (float): Radius of the capsule.
height (float): Height of the cylindrical part of the capsule.
theta (float): Angle between the normal vector and the z-axis.
phi (float): Azimuthal angle of the normal vector.
Returns:
points (numpy array): Points on the capsule.
"""
# Calculate the number of points on the spherical ends
num_sphere_points = int(num_points * (2 * radius) / (2 * radius + height))
# Generate points on the spherical ends
top_sphere_points = fibonacci_sphere_on_side(num_sphere_points, radius, theta, phi)
top_sphere_points += np.array([0, 0, height/2 + radius])
bottom_sphere_points = fibonacci_sphere_on_side(num_sphere_points, radius, np.pi - theta, phi + np.pi)
bottom_sphere_points += np.array([0, 0, -height/2 - radius])
# Generate points on the cylindrical part
num_cylinder_points = num_points - 2 * num_sphere_points
cylinder_points = fibonacci_cylinder_on_side(num_cylinder_points, radius, height, phi)
# Combine points
points = np.vstack((top_sphere_points, cylinder_points, bottom_sphere_points))
return points
def fibonacci_sphere_on_side(num_points, radius, theta, phi):
"""
Generate points on a sphere, only on one side.
Parameters:
num_points (int): Number of points to generate.
radius (float): Radius of the sphere.
theta (float): Angle between the normal vector and the z-axis.
phi (float): Azimuthal angle of the normal vector.
Returns:
points (numpy array): Points on the sphere.
"""
indices = np.arange(0, num_points, dtype=float) + 0.5
# Only generate points with a polar angle less than theta
max_phi = np.arccos(1 - 2*(indices/num_points) / (1 + np.cos(theta)))
theta_points = np.pi * (1 + 5**0.5) * indices + phi
x = np.cos(theta_points) * np.sin(max_phi)
y = np.sin(theta_points) * np.sin(max_phi)
z = np.cos(max_phi)
# Rotate the points to the correct orientation
rotation_matrix = np.array([
[np.cos(phi)*np.cos(theta), -np.sin(phi), np.cos(phi)*np.sin(theta)],
[np.sin(phi)*np.cos(theta), np.cos(phi), np.sin(phi)*np.sin(theta)],
[-np.sin(theta), 0, np.cos(theta)]
])
points = np.dot(np.column_stack((x, y, z)), rotation_matrix) * radius
return points
def fibonacci_cylinder_on_side(num_points, radius, height, phi):
"""
Generate points on a cylinder, only on one side.
Parameters:
num_points (int): Number of points to generate.
radius (float): Radius of the cylinder.
height (float): Height of the cylinder.
phi (float): Azimuthal angle of the normal vector.
Returns:
points (numpy array): Points on the cylinder.
"""
indices = np.arange(0, num_points, dtype=float) + 0.5
# Only generate points with an azimuthal angle between phi - pi/2 and phi + pi/2
theta_points = np.pi * (1 + 5**0.5) * (indices / num_points) + phi - np.pi/2
z = height * (indices / num_points - 0.5)
x = np.cos(theta_points)
y = np.sin(theta_points)
# Select points on the correct side
mask = (x > 0)
x, y, z = x[mask], y[mask], z[mask]
num_points = len(x)
# If not enough points, repeat the process
while num_points < num_points:
indices = np.arange(num_points, 2*num_points, dtype=float) + 0.5
theta_points = np.pi * (1 + 5**0.5) * (indices / num_points) + phi - np.pi/2
z_points = height * (indices / num_points - 0.5)
x_points = np.cos(theta_points)
y_points = np.sin(theta_points)
mask = (x_points > 0)
x_points, y_points, z_points = x_points[mask], y_points[mask], z_points[mask]
x = np.concatenate((x, x_points))
y = np.concatenate((y, y_points))
z = np.concatenate((z, z_points))
num_points = len(x)
points = np.column_stack((x[:num_points], y[:num_points], z[:num_points])) * np.array([radius, radius, 1])
return points
# Example usage
num_points = 1000
radius = 1
height = 2
point = np.array([1, 0, 0])
points = fibonacci_sphere_on_capsule(num_points, radius, height, point)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(points[:, 0], points[:, 1], points[:, 2], s=1)
plt.show