Struct Sphere

Source
#[repr(C)]
pub struct Sphere { pub center: Vec3, pub radius: f32, }
Expand description

Represents a sphere in 3D space! Composed of a center point and a radius, can be used for raycasting, collision, visibility, and other things! https://stereokit.net/Pages/StereoKit/Sphere.html

§Examples

use stereokit_rust::{maths::{Vec3, Matrix, Sphere, Ray}, system::Lines,
    mesh::Mesh, material::Material, util::named_colors};

let sphere = Sphere::new(Vec3::ZERO, 0.5);
let sphere_mesh = Mesh::generate_sphere(sphere.radius * 2.0, Some(12));
let mut material_sphere = Material::pbr().copy();
material_sphere.color_tint(named_colors::GOLD)
               .border_size(0.05);

let scale = 0.1;
let transform = Matrix::t(sphere.center);
let ray_x = Ray::new(Vec3::X, Vec3::NEG_X);
let ray_y = Ray::new(Vec3::Y, Vec3::NEG_Y);
let contact_x = sphere.intersect(ray_x).expect("X Should be there");
let contact_y = sphere.intersect(ray_y).expect("Y Should be there");

assert_eq!(contact_x, Vec3::X * sphere.radius);
assert_eq!(contact_y, Vec3::Y * sphere.radius);

filename_scr = "screenshots/sphere.jpeg";
test_screenshot!( // !!!! Get a proper main loop !!!!
    sphere_mesh.draw(token, &material_sphere, transform, None, None);
    Lines::add_ray(token, ray_x, 0.30, named_colors::RED, None, 0.04);
    Lines::add_ray(token, ray_y, 0.30, named_colors::GREEN, None, 0.04);
);
screenshot

Fields§

§center: Vec3

Center of the sphere

§radius: f32

Distance from the center, to the surface of the sphere, in meters. Half the diameter.

Implementations§

Source§

impl Sphere

Source

pub fn new<V: Into<Vec3>>(center: V, radius: f32) -> Sphere

Creates a Sphere directly from the ax + by + cz + d = 0 formula! https://stereokit.net/Pages/StereoKit/Sphere.html

§Examples
use stereokit_rust::maths::{Vec3, Sphere};

let sphere = Sphere::new(Vec3::ZERO, 0.5);
let sphere_b = Sphere {center : Vec3::ZERO, radius : 0.5};

assert_eq!(sphere, sphere_b);
Source

pub fn contains<V: Into<Vec3>>(&self, point: V) -> bool

A fast check to see if the given point is contained in or on a sphere! https://stereokit.net/Pages/StereoKit/Sphere/Contains.html

see also sphere_point_contains

§Examples
use stereokit_rust::maths::{Vec3, Sphere};

let sphere = Sphere::new(Vec3::ZERO, 0.5);

assert!(sphere.contains(Vec3::ONE * 0.28), "Should be contained");
assert!(!sphere.contains(Vec3::ONE * 0.29), "Should not be contained");
Source

pub fn intersect(&self, ray: Ray) -> Option<Vec3>

Intersects a ray with this sphere, and finds if they intersect, and if so, where that intersection is! This only finds the closest intersection point to the origin of the ray. https://stereokit.net/Pages/StereoKit/Sphere/Intersect.html

  • ray - A ray to intersect with

Returns the closest intersection point to the ray’s origin. Or None it there is no intersection. see also sphere_ray_intersect same as Ray::intersect_sphere

§Examples
use stereokit_rust::maths::{Vec3, Sphere, Ray};

let sphere = Sphere::new(Vec3::ZERO, 0.5);

assert_eq!(sphere.intersect(Ray::new(Vec3::Z, Vec3::NEG_Z)), Some(Vec3::Z * 0.5));
assert_ne!(sphere.intersect(Ray::new(Vec3::Z, Vec3::Z)),     None);

Trait Implementations§

Source§

impl AsRef<Sphere> for Sphere

AsRef

Source§

fn as_ref(&self) -> &Sphere

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Sphere

Source§

fn clone(&self) -> Sphere

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Sphere

Source§

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

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

impl Default for Sphere

Source§

fn default() -> Sphere

Returns the “default value” for a type. Read more
Source§

impl Display for Sphere

Source§

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

Creates a text description of the Sphere, in the format of “[center:X radius:X]”

§Examples
use stereokit_rust::maths::{Vec3, Sphere};

let sphere = Sphere::new([1.1, 2.0, 3.0], 4.0);
assert_eq!(format!("{}", sphere),
           "[center:[x:1.1, y:2, z:3] radius:4]");
Source§

impl PartialEq for Sphere

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Sphere

Source§

impl StructuralPartialEq for Sphere

Auto Trait Implementations§

§

impl Freeze for Sphere

§

impl RefUnwindSafe for Sphere

§

impl Send for Sphere

§

impl Sync for Sphere

§

impl Unpin for Sphere

§

impl UnwindSafe for Sphere

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more