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
impl Sphere
Sourcepub fn new(
center: Point3D,
radius: f64,
) -> Result<Sphere, SphereConstructionError>
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));Sourcepub fn from_frame(
frame: Frame3D,
radius: f64,
) -> Result<Sphere, SphereConstructionError>
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);Sourcepub fn center(&self) -> Point3D
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));Sourcepub fn frame(&self) -> Frame3D
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);Sourcepub fn radius(&self) -> f64
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);Sourcepub fn eval_point(&self, u: f64, v: f64) -> Point3D
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));Sourcepub fn eval_points(&self, uvs: &[(f64, f64)]) -> Vec<Point3D>
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));Sourcepub fn eval_derivative(&self, u: f64, v: f64, du: u32, dv: u32) -> Vector3D
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);Sourcepub fn parameters_of(&self, point: Point3D) -> (f64, f64)
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));Sourcepub fn contains(&self, point: Point3D, tol: Tolerance) -> bool
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));Sourcepub fn project_point(&self, point: Point3D, tol: Tolerance) -> SurfaceProjection
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);Sourcepub fn project_points(
&self,
points: &[Point3D],
tol: Tolerance,
) -> Vec<SurfaceProjection>
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.
Sourcepub fn intersect_sphere(
&self,
other: &Sphere,
tol: Tolerance,
) -> SphereSphereIntersection
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§
impl Copy for Sphere
Source§impl ParametricSurface for Sphere
impl ParametricSurface for Sphere
Source§fn eval_point(&self, u: f64, v: f64) -> Point3D
fn eval_point(&self, u: f64, v: f64) -> Point3D
(u, v).Source§fn eval_derivative(&self, u: f64, v: f64, du: u32, dv: u32) -> Vector3D
fn eval_derivative(&self, u: f64, v: f64, du: u32, dv: u32) -> Vector3D
(du, dv) at parameters (u, v).Source§fn u_bounds(&self) -> (f64, f64)
fn u_bounds(&self) -> (f64, f64)
u parameter bounds as (first, last).
Unbounded surfaces use (f64::NEG_INFINITY, f64::INFINITY).Source§fn v_bounds(&self) -> (f64, f64)
fn v_bounds(&self) -> (f64, f64)
v parameter bounds as (first, last).
Unbounded surfaces use (f64::NEG_INFINITY, f64::INFINITY).Source§fn u_period(&self) -> Option<f64>
fn u_period(&self) -> Option<f64>
u, or None if the surface is
not periodic in u.