Skip to main content

Sphere

Struct Sphere 

Source
pub struct Sphere { /* private fields */ }
Expand description

A sphere in 3D: a Frame3D (origin plus local x/y/z directions) and a radius, evaluated as Rcv = r*cos(v), origin + Rcv*cos(u)*x_dir + Rcv*sin(u)*y_dir + r*sin(v)*z_dir.

§Examples

use geomcore::{Sphere, Point3D};
let sphere = Sphere::new(Point3D::ORIGIN, 3.0).unwrap();
assert_eq!(sphere.eval_point(0.0, 0.0), Point3D::new(3.0, 0.0, 0.0));

Implementations§

Source§

impl Sphere

Source

pub fn new( center: Point3D, radius: f64, ) -> Result<Sphere, SphereConstructionError>

Creates a sphere from a center and a radius, using a world-aligned frame (x/y/z directions matching Vector3D::X/Vector3D::Y/ Vector3D::Z) at center.

§Errors

Returns SphereConstructionError::NegativeRadius if radius < 0.

§Examples
use geomcore::{Point3D, Sphere};
let sphere = Sphere::new(Point3D::new(1.0, 2.0, 3.0), 2.0).unwrap();
assert_eq!(sphere.center(), Point3D::new(1.0, 2.0, 3.0));
Source

pub fn from_frame( frame: Frame3D, radius: f64, ) -> Result<Sphere, SphereConstructionError>

Creates a sphere from a frame and a radius directly.

§Errors

Returns SphereConstructionError::NegativeRadius if radius < 0.

§Examples
use geomcore::{Frame3D, Sphere};
let sphere = Sphere::from_frame(Frame3D::WORLD, 2.0).unwrap();
assert_eq!(sphere.frame(), Frame3D::WORLD);
Source

pub fn center(&self) -> Point3D

Returns the center of the sphere.

§Examples
use geomcore::{Point3D, Sphere};
let sphere = Sphere::new(Point3D::new(1.0, 2.0, 3.0), 2.0).unwrap();
assert_eq!(sphere.center(), Point3D::new(1.0, 2.0, 3.0));
Source

pub fn frame(&self) -> Frame3D

Returns the sphere’s frame.

§Examples
use geomcore::{Frame3D, Sphere};
let sphere = Sphere::from_frame(Frame3D::WORLD, 2.0).unwrap();
assert_eq!(sphere.frame(), Frame3D::WORLD);
Source

pub fn radius(&self) -> f64

Returns the sphere’s radius.

§Examples
use geomcore::{Point3D, Sphere};
let sphere = Sphere::new(Point3D::ORIGIN, 2.0).unwrap();
assert_eq!(sphere.radius(), 2.0);
Source

pub fn eval_point(&self, u: f64, v: f64) -> Point3D

Evaluates the point on the sphere at (u, v). See the type-level docs for the formula.

§Examples
use geomcore::{Point3D, Sphere};
let sphere = Sphere::new(Point3D::ORIGIN, 3.0).unwrap();
assert_eq!(sphere.eval_point(0.0, 0.0), Point3D::new(3.0, 0.0, 0.0));
Source

pub fn eval_points(&self, uvs: &[(f64, f64)]) -> Vec<Point3D>

Evaluates the points on the sphere at each (u, v) in uvs.

§Examples
use geomcore::{Point3D, Sphere};
let sphere = Sphere::new(Point3D::ORIGIN, 3.0).unwrap();
let points = sphere.eval_points(&[(0.0, 0.0)]);
assert_eq!(points[0], Point3D::new(3.0, 0.0, 0.0));
Source

pub fn eval_derivative(&self, u: f64, v: f64, du: u32, dv: u32) -> Vector3D

Evaluates the derivative of order (du, dv) at (u, v). See surface_math::analytic::sphere_derivative for the formulas.

§Panics

Panics if du + dv == 0 (use Sphere::eval_point for the position itself) or if du + dv > 2.

§Examples
use geomcore::{Point3D, Sphere, Vector3D};
let sphere = Sphere::new(Point3D::ORIGIN, 3.0).unwrap();
assert_eq!(sphere.eval_derivative(0.0, 0.0, 0, 1), Vector3D::Z * 3.0);
Source

pub fn parameters_of(&self, point: Point3D) -> (f64, f64)

Recovers (u, v) of a point on (or near) the sphere. See surface_math::analytic::sphere_parameters for the formula, including the pole handling.

§Examples
use geomcore::{Point3D, Sphere};
let sphere = Sphere::new(Point3D::ORIGIN, 3.0).unwrap();
let (u, v) = sphere.parameters_of(Point3D::new(3.0, 0.0, 0.0));
assert_eq!((u, v), (0.0, 0.0));
Source

pub fn contains(&self, point: Point3D, tol: Tolerance) -> bool

Returns whether point lies on the sphere: the inverse parameters are recovered with Sphere::parameters_of and re-evaluated, and the point counts as contained when the re-evaluated point is within tol.confusion of it.

§Examples
use geomcore::{Point3D, Sphere, Tolerance};
let sphere = Sphere::new(Point3D::ORIGIN, 3.0).unwrap();
let tol = Tolerance::DEFAULT;
assert!(sphere.contains(Point3D::new(3.0, 0.0, 0.0), tol));
assert!(!sphere.contains(Point3D::ORIGIN, tol));
Source

pub fn project_point(&self, point: Point3D, tol: Tolerance) -> SurfaceProjection

Projects point onto the sphere, returning the (u, v) parameters of the closest point and its distance. Distances within tol.confusion snap to 0.0, matching Sphere::contains.

§Examples
use geomcore::{Point3D, Sphere, Tolerance};
let sphere = Sphere::new(Point3D::ORIGIN, 3.0).unwrap();
let proj = sphere.project_point(Point3D::new(4.0, 0.0, 0.0), Tolerance::DEFAULT);
assert_eq!((proj.u, proj.v), (0.0, 0.0));
assert_eq!(proj.distance, 1.0);
Source

pub fn project_points( &self, points: &[Point3D], tol: Tolerance, ) -> Vec<SurfaceProjection>

Projects each point in points onto the sphere.

Default-style batch wrapper over Sphere::project_point: one native call per batch, mirroring Sphere::eval_points.

Source

pub fn intersect_sphere( &self, other: &Sphere, tol: Tolerance, ) -> SphereSphereIntersection

Intersects this sphere with another sphere.

Concentric spheres (center distance within tol.confusion) are SphereSphereIntersection::Coincident when the radii agree within tolerance, else SphereSphereIntersection::Empty. Otherwise the radical plane cuts the center line at distance a = (r1^2 - r2^2 + d^2) / (2d) from this center; a negative r1^2 - a^2 (beyond tolerance) misses (SphereSphereIntersection::Empty), a near-zero one grazes in a single SphereSphereIntersection::TangentPoint, and the rest meet in a Circle3D whose axis points from this center to the other.

§Examples
use geomcore::{Point3D, Sphere, SphereSphereIntersection, Tolerance};
let s1 = Sphere::new(Point3D::ORIGIN, 2.0).unwrap();
let s2 = Sphere::new(Point3D::new(3.0, 0.0, 0.0), 2.0).unwrap();
let tol = Tolerance::DEFAULT;
match s1.intersect_sphere(&s2, tol) {
    SphereSphereIntersection::Circle(circle) => {
        assert_eq!(circle.center(), Point3D::new(1.5, 0.0, 0.0));
    }
    _ => panic!("expected a circle"),
}

Trait Implementations§

Source§

impl Clone for Sphere

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Sphere

Source§

impl Debug for Sphere

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&Sphere> for Surface

Source§

fn from(surface: &Sphere) -> Surface

Copies a Sphere reference into a Surface::Sphere.

Source§

impl From<Sphere> for Surface

Source§

fn from(surface: Sphere) -> Surface

Wraps a Sphere as a Surface::Sphere.

Source§

impl ParametricSurface for Sphere

Source§

fn eval_point(&self, u: f64, v: f64) -> Point3D

Evaluates the point on the surface at parameters (u, v).
Source§

fn eval_derivative(&self, u: f64, v: f64, du: u32, dv: u32) -> Vector3D

Evaluates the derivative of order (du, dv) at parameters (u, v).
Source§

fn u_bounds(&self) -> (f64, f64)

Returns the surface’s u parameter bounds as (first, last). Unbounded surfaces use (f64::NEG_INFINITY, f64::INFINITY).
Source§

fn v_bounds(&self) -> (f64, f64)

Returns the surface’s v parameter bounds as (first, last). Unbounded surfaces use (f64::NEG_INFINITY, f64::INFINITY).
Source§

fn u_period(&self) -> Option<f64>

Returns the surface’s period along u, or None if the surface is not periodic in u.
Source§

fn v_period(&self) -> Option<f64>

Returns the surface’s period along v, or None if the surface is not periodic in v.
Source§

fn eval_points(&self, uvs: &[(f64, f64)]) -> Vec<Point3D>

Evaluates the points on the surface at each (u, v) in uvs. Read more
Source§

impl PartialEq for Sphere

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Sphere

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.