Spatial objects and computations based on NumPy arrays for 2D, 3D, and higher-dimensional geometric calculations.
—
Circles, spheres, and cylinders with specialized methods for curved surface computations, intersections, volume calculations, and fitting operations for point clouds.
Represents a circle in 2D space with center point and radius. Provides area calculations, intersections, and fitting capabilities.
class Circle:
"""
A circle in 2D space.
Parameters:
- point: array-like, center point (2D)
- radius: float, radius of the circle
"""
def __init__(self, point, radius): ...
@classmethod
def from_points(cls, point_a, point_b, point_c, **kwargs) -> 'Circle':
"""
Create circle from three points.
Parameters:
- point_a: array-like, first point (2D)
- point_b: array-like, second point (2D)
- point_c: array-like, third point (2D)
- **kwargs: additional keywords passed to numpy functions
Returns:
Circle passing through all three points
"""
@classmethod
def best_fit(cls, points) -> 'Circle':
"""
Find circle of best fit for 2D points.
Parameters:
- points: array-like, 2D points to fit circle to
Returns:
Best fit circle
"""
@property
def point(self) -> Point:
"""Center point of the circle (2D)."""
@property
def radius(self) -> float:
"""Radius of the circle."""
@property
def dimension(self) -> int:
"""Dimension of the circle (always 2)."""
def circumference(self) -> float:
"""
Calculate circumference of the circle.
Returns:
Circumference (2πr)
"""
def area(self) -> float:
"""
Calculate area of the circle.
Returns:
Area (πr²)
"""
def intersect_circle(self, other) -> Tuple[Point, Point]:
"""
Find intersection points with another circle.
Parameters:
- other: Circle, another circle
Returns:
Tuple of two intersection points
Raises:
ValueError if circles don't intersect
"""
def intersect_line(self, line) -> Tuple[Point, Point]:
"""
Find intersection points with a line.
Parameters:
- line: Line, line to intersect
Returns:
Tuple of intersection points
Raises:
ValueError if line doesn't intersect circle
"""
def distance_point(self, point) -> np.float64:
"""
Calculate distance from point to circle edge.
Parameters:
- point: array-like, point
Returns:
Distance to closest point on circle circumference
"""
def contains_point(self, point, **kwargs) -> bool:
"""
Check if circle contains point.
Parameters:
- point: array-like, point to check
- **kwargs: additional keywords passed to math.isclose
Returns:
True if point is on circle circumference
"""
def project_point(self, point) -> Point:
"""
Project point onto circle circumference.
Parameters:
- point: array-like, point to project
Returns:
Closest point on circle circumference
"""
def plot_2d(self, ax_2d, **kwargs):
"""
Plot 2D circle.
Parameters:
- ax_2d: matplotlib Axes, 2D plotting axes
- **kwargs: additional keywords passed to plotting functions
"""Usage Example:
from skspatial.objects import Circle, Line, Point
# Create circle from center and radius
circle = Circle([0, 0], radius=5)
# Create circle from three points
circle2 = Circle.from_points([0, 0], [1, 0], [0, 1])
# Calculate properties
area = circle.area() # 25π
circumference = circle.circumference() # 10π
# Find intersections
line = Line([0, -10], [0, 1])
intersections = circle.intersect_line(line) # Two points where line crosses circle
# Circle of best fit
import numpy as np
points_2d = np.random.randn(20, 2) * 3 + [2, 3] # Noisy circle data
best_fit_circle = Circle.best_fit(points_2d)Represents a sphere in 3D space with center point and radius. Provides volume calculations, surface area, intersections, and mesh generation.
class Sphere:
"""
A sphere in 3D space.
Parameters:
- point: array-like, center point (3D)
- radius: float, radius of the sphere
"""
def __init__(self, point, radius): ...
@classmethod
def best_fit(cls, points) -> 'Sphere':
"""
Find sphere of best fit for 3D points.
Parameters:
- points: array-like, 3D points to fit sphere to
Returns:
Best fit sphere
"""
@property
def point(self) -> Point:
"""Center point of the sphere (3D)."""
@property
def radius(self) -> float:
"""Radius of the sphere."""
@property
def dimension(self) -> int:
"""Dimension of the sphere (always 3)."""
def surface_area(self) -> float:
"""
Calculate surface area of the sphere.
Returns:
Surface area (4πr²)
"""
def volume(self) -> float:
"""
Calculate volume of the sphere.
Returns:
Volume (4/3 πr³)
"""
def intersect_line(self, line) -> Tuple[Point, Point]:
"""
Find intersection points with a line.
Parameters:
- line: Line, line to intersect
Returns:
Tuple of intersection points
Raises:
ValueError if line doesn't intersect sphere
"""
def to_mesh(self, n_angles=30):
"""
Generate mesh coordinates for plotting.
Parameters:
- n_angles: int, number of angles for discretization (default: 30)
Returns:
X, Y, Z mesh arrays for plotting
"""
def to_points(self, **kwargs) -> Points:
"""
Return points on the sphere surface.
Parameters:
- **kwargs: additional keywords for mesh generation
Returns:
Points sampled on sphere surface
"""
def distance_point(self, point) -> np.float64:
"""
Calculate distance from point to sphere surface.
Parameters:
- point: array-like, point
Returns:
Distance to closest point on sphere surface
"""
def contains_point(self, point, **kwargs) -> bool:
"""
Check if sphere contains point.
Parameters:
- point: array-like, point to check
- **kwargs: additional keywords passed to math.isclose
Returns:
True if point is on sphere surface
"""
def project_point(self, point) -> Point:
"""
Project point onto sphere surface.
Parameters:
- point: array-like, point to project
Returns:
Closest point on sphere surface
"""
def plot_3d(self, ax_3d, n_angles=30, **kwargs):
"""
Plot 3D sphere surface.
Parameters:
- ax_3d: matplotlib Axes3D, 3D plotting axes
- n_angles: int, number of angles for discretization (default: 30)
- **kwargs: additional keywords passed to plot_surface
"""Usage Example:
from skspatial.objects import Sphere, Line, Point
# Create sphere from center and radius
sphere = Sphere([0, 0, 0], radius=3)
# Calculate properties
volume = sphere.volume() # 36π
surface_area = sphere.surface_area() # 36π
# Find intersections with line
line = Line([0, 0, -5], [0, 0, 1])
intersections = sphere.intersect_line(line) # Entry and exit points
# Sphere of best fit
import numpy as np
# Generate noisy sphere data
theta = np.random.uniform(0, 2*np.pi, 100)
phi = np.random.uniform(0, np.pi, 100)
points_3d = np.column_stack([
3 * np.sin(phi) * np.cos(theta),
3 * np.sin(phi) * np.sin(theta),
3 * np.cos(phi)
]) + np.random.normal(0, 0.1, (100, 3))
best_fit_sphere = Sphere.best_fit(points_3d)Represents a cylinder in 3D space defined by base center, axis vector, and radius. Provides volume calculations, surface areas, and intersection operations.
class Cylinder:
"""
A cylinder in 3D space.
Parameters:
- point: array-like, center of cylinder base (3D)
- vector: array-like, axis vector (3D)
- radius: float, radius of the cylinder
"""
def __init__(self, point, vector, radius): ...
@classmethod
def from_points(cls, point_a, point_b, radius) -> 'Cylinder':
"""
Create cylinder from two points and radius.
Parameters:
- point_a: array-like, first point on axis (3D)
- point_b: array-like, second point on axis (3D)
- radius: float, radius of the cylinder
Returns:
Cylinder with axis from point_a to point_b
"""
@classmethod
def best_fit(cls, points) -> 'Cylinder':
"""
Find cylinder of best fit for 3D points.
Parameters:
- points: array-like, 3D points to fit cylinder to
Returns:
Best fit cylinder
"""
@property
def point(self) -> Point:
"""Center of cylinder base (3D)."""
@property
def vector(self) -> Vector:
"""Axis vector of the cylinder (3D)."""
@property
def radius(self) -> float:
"""Radius of the cylinder."""
@property
def dimension(self) -> int:
"""Dimension of the cylinder (always 3)."""
def length(self) -> np.float64:
"""
Calculate length of the cylinder.
Returns:
Length along axis vector
"""
def lateral_surface_area(self) -> np.float64:
"""
Calculate lateral surface area of the cylinder.
Returns:
Lateral surface area (2πrh)
"""
def surface_area(self) -> np.float64:
"""
Calculate total surface area of the cylinder.
Returns:
Total surface area (2πr² + 2πrh)
"""
def volume(self) -> np.float64:
"""
Calculate volume of the cylinder.
Returns:
Volume (πr²h)
"""
def is_point_within(self, point) -> bool:
"""
Check if point is inside the cylinder.
Parameters:
- point: array-like, point to check (3D)
Returns:
True if point is within cylinder volume
"""
def intersect_line(self, line, n_digits=None, infinite=True):
"""
Find intersection points with a line.
Parameters:
- line: Line, line to intersect
- n_digits: int, optional precision for calculations
- infinite: bool, treat cylinder as infinite (default: True)
Returns:
Intersection points with cylinder surface
Raises:
ValueError if line doesn't intersect cylinder
"""
def to_mesh(self, n_along_axis=100, n_angles=30):
"""
Generate mesh coordinates for plotting.
Parameters:
- n_along_axis: int, points along axis (default: 100)
- n_angles: int, angular discretization (default: 30)
Returns:
X, Y, Z mesh arrays for plotting
"""
def to_points(self, **kwargs) -> Points:
"""
Return points on the cylinder surface.
Parameters:
- **kwargs: additional keywords for mesh generation
Returns:
Points sampled on cylinder surface
"""
def plot_3d(self, ax_3d, n_along_axis=100, n_angles=30, **kwargs):
"""
Plot 3D cylinder surface.
Parameters:
- ax_3d: matplotlib Axes3D, 3D plotting axes
- n_along_axis: int, points along axis (default: 100)
- n_angles: int, angular discretization (default: 30)
- **kwargs: additional keywords passed to plot_surface
"""Usage Example:
from skspatial.objects import Cylinder, Line, Point
# Create cylinder from base center, axis, and radius
cylinder = Cylinder([0, 0, 0], [0, 0, 5], radius=2)
# Create cylinder from two points
cylinder2 = Cylinder.from_points([0, 0, 0], [0, 0, 10], radius=1.5)
# Calculate properties
volume = cylinder.volume() # 20π
surface_area = cylinder.surface_area() # 28π
length = cylinder.length() # 5
# Check if point is inside
point = Point([1, 1, 2.5])
is_inside = cylinder.is_point_within(point) # True (within radius and height)
# Find intersections with line
line = Line([-3, 0, 2.5], [1, 0, 0]) # Horizontal line through cylinder
intersections = cylinder.intersect_line(line) # Entry and exit points
# Cylinder of best fit
import numpy as np
# Generate cylindrical point cloud
theta = np.random.uniform(0, 2*np.pi, 200)
z = np.random.uniform(0, 5, 200)
points_3d = np.column_stack([
2 * np.cos(theta) + np.random.normal(0, 0.1, 200),
2 * np.sin(theta) + np.random.normal(0, 0.1, 200),
z + np.random.normal(0, 0.1, 200)
])
best_fit_cylinder = Cylinder.best_fit(points_3d)Install with Tessl CLI
npx tessl i tessl/pypi-scikit-spatial