Sphere

Struct Sphere 

Source
pub struct Sphere {
    pub origin: Vec3,
    pub radius: Float,
    /* private fields */
}
Expand description

Sphere specified by its radius and origin

Fields§

§origin: Vec3

Origin of the sphere

§radius: Float

Radius of the sphere

Implementations§

Source§

impl Sphere

Source

pub fn new(origin: Vec3, radius: Float, material: Material) -> Box<Self>

§Arguments
  • origin - Origin of the sphere
  • radius - Radius of the sphere
  • material - Material of the sphere
Examples found in repository?
examples/fireplace.rs (lines 21-25)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let camera = Camera::perspective(
9        Vec3::new(4.0, 1.0, -3.0),
10        0.5 * Vec3::Y,
11        Vec3::Y,
12        90.0,
13        0.0,
14        1.0,
15        1024,
16        768,
17    );
18
19    let mut scene = parser::scene_from_url(SCENE_URL, SCENE_NAME)?;
20
21    scene.add_light(Sphere::new(
22        Vec3::new(2.0, 1.5, -2.0),
23        0.3,
24        Material::Light(Texture::Solid(Color::WHITE)),
25    ));
26
27    let renderer = Renderer::new(scene, camera);
28    renderer.render().save("fireplace.png")?;
29
30    Ok(())
31}
More examples
Hide additional examples
examples/conference.rs (lines 21-25)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let camera = Camera::perspective(
9        Vec3::new(-50.0, 400.0, -350.0),
10        500.0 * Vec3::X + 250.0 * Vec3::Z,
11        Vec3::Y,
12        90.0,
13        0.0,
14        1.0,
15        1024,
16        768,
17    );
18
19    let mut scene = parser::scene_from_url(SCENE_URL, SCENE_NAME)?;
20
21    scene.add_light(Sphere::new(
22        Vec3::new(-200.0, 40.0, -400.0),
23        100.0,
24        Material::Light(Texture::Solid(Color::WHITE)),
25    ));
26
27    scene.add_light(Sphere::new(
28        Vec3::new(900.0, 300.0, -600.0),
29        100.0,
30        Material::Light(Texture::Solid(Color::WHITE)),
31    ));
32
33    let renderer = Renderer::new(scene, camera);
34    renderer.render().save("conference.png")?;
35
36    Ok(())
37}
examples/hello_sphere.rs (lines 14-18)
4fn main() -> Result<(), png::EncodingError> {
5    let camera = Camera::default(1280, 720);
6    let mut scene = Scene::default();
7
8    scene.add(Plane::new(
9        Vec3::NEG_Y,
10        Vec3::Y,
11        Material::diffuse(Texture::Solid(Color::new(190, 200, 210))),
12    ));
13
14    scene.add_light(Sphere::new(
15        8.0 * Vec3::Y + 1.5 * Vec3::NEG_Z,
16        4.0,
17        Material::Light(Texture::Solid(Color::WHITE)),
18    ));
19
20    scene.add(
21        Sphere::new(
22            Vec3::ZERO,
23            1.0,
24            Material::diffuse(Texture::Solid(Color::new(0, 0, 255))),
25        )
26        .scale(0.3, 0.3, 0.3)
27        .translate(0.0, -0.7, -1.5),
28    );
29
30    let mut renderer = Renderer::new(scene, camera);
31    renderer.set_samples(36);
32    renderer.render().save("hello.png")
33}
examples/box.rs (lines 15-22)
4fn main() -> Result<(), std::io::Error> {
5    let camera = Camera::default(1024, 768);
6    let def_color = Color::new(242, 242, 242);
7    let mut scene = Scene::empty_box(
8        def_color,
9        // left
10        Material::diffuse(Texture::Solid(Color::new(255, 0, 255))),
11        // right
12        Material::diffuse(Texture::Solid(Color::new(0, 255, 255))),
13    );
14
15    scene.add(Sphere::new(
16        Vec3::new(-0.45, -0.5, -1.5),
17        0.25,
18        Material::metallic(
19            Texture::Solid(Color::WHITE),
20            0.0,
21        )
22    ));
23
24    scene.add(Sphere::new(
25        Vec3::new(0.45, -0.5, -1.3),
26        0.25,
27        Material::transparent(
28            Texture::Solid(Color::WHITE),
29            0.0,
30            2.5,
31        ),
32    ));
33
34    let renderer = Renderer::new(scene, camera);
35    renderer.render().save("box.png")?;
36    Ok(())
37}
examples/circle.rs (lines 39-43)
16fn main() -> Result<(), std::io::Error> {
17    let camera = Camera::perspective(
18        Vec3::new(0.0, 1.5, 1.5),
19        Vec3::ZERO,
20        Vec3::new(0.0, 1.0, -1.0),
21        90.0,
22        0.0,
23        0.0,
24        1024,
25        768,
26    );
27
28    let mut scene = Scene::default();
29    let ground = -0.2;
30
31    // ground
32    scene.add(Plane::new(
33        ground * Vec3::Y,
34        Vec3::Y,
35        Material::metallic(Texture::Solid(Color::new(150, 40, 39)), 0.009999),
36    ));
37
38    let r = 0.2;
39    scene.add_light(Sphere::new(
40        Vec3::new(0.0, ground + r + 0.1, 0.0),
41        r,
42        Material::Light(Texture::Solid(Color::WHITE)),
43    ));
44
45    let circle_s = 8;
46    let offset = PI / circle_s as Float;
47
48    for i in 0..circle_s {
49        let theta = (i as Float / circle_s as Float) * 2.0 * PI + offset;
50        let x = theta.cos();
51        let y = ground + r;
52        let z = theta.sin();
53
54        scene.add(Sphere::new(
55            Vec3::new(x, y, z),
56            r,
57            Material::specular(Texture::Solid(hsv_to_rgb(theta - offset)), 0.2),
58        ));
59    }
60
61    scene.set_medium(
62        Medium::new(
63            Vec3::new(0.002, 0.003, 0.0001),
64            Vec3::new(0.175, 0.125, 0.11),
65            0.9,
66        )
67    );
68
69    let renderer = Renderer::new(scene, camera);
70    renderer.render().save("circle.png")?;
71
72    Ok(())
73}

Trait Implementations§

Source§

impl Bounded for Sphere

Source§

fn bounding_box(&self) -> AaBoundingBox

Axis aligned box that contains the object
Source§

impl Object for Sphere

Source§

fn hit(&self, r: &Ray, t_min: Float, t_max: Float) -> Option<Hit<'_>>

Solve the quadratic

Source§

impl Sampleable for Sphere

Source§

fn sample_on(&self, rand_sq: DVec2) -> Hit<'_>

Sample on unit sphere and scale

Source§

fn sample_towards(&self, xo: Vec3, rand_sq: DVec2) -> Vec3

Visible area from xo forms a cone. Sample a random point on the spherical cap that the visible area forms. Return a ray with direction towards the sampled point.

Source§

fn sample_towards_pdf(&self, ri: &Ray) -> (Float, Option<Hit<'_>>)

PDF (w.r.t area) for sampling area of the sphere that is visible from xo (a spherical cap formed by a cone)

Source§

fn area(&self) -> Float

Returns surface area of the object
Source§

fn sample_leaving(&self, rand_sq0: DVec2, rand_sq1: DVec2) -> (Ray, Hit<'_>)

Samples a ray leaving at random point on the surface of the object. Direction cos weighed on the hemisphere. Returns also normal at ray origin
Source§

fn sample_leaving_pdf(&self, r: &Ray, ng: Vec3) -> (Float, Float)

Returns PDF for sampled ray (i) origin and (ii) direction

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instanceable<T> for T
where T: Object,

Source§

fn translate(self, x: f64, y: f64, z: f64) -> Box<Instance<T>>

Translate object by xyz
Source§

fn scale(self, x: f64, y: f64, z: f64) -> Box<Instance<T>>

Apply scale xyz
Source§

fn rotate_x(self, r: f64) -> Box<Instance<T>>

Rotate around x-axis by r radians
Source§

fn rotate_y(self, r: f64) -> Box<Instance<T>>

Rotate around y-axis by r radians
Source§

fn rotate_z(self, r: f64) -> Box<Instance<T>>

Rotate around z-axis by r radians
Source§

fn rotate_axis(self, axis: DVec3, r: f64) -> Box<Instance<T>>

Rotate around axis by r radisn
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,