[][src]Struct raytracer::Vec3

pub struct Vec3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

This struct represents a mathematical vector.

Fields

x: f32y: f32z: f32

Methods

impl Vec3[src]

pub fn new(x: f32, y: f32, z: f32) -> Vec3[src]

Vector constructor. Consider using a shorthand vec3!

Examples

let v = Vec3::new(1.0, 2.0, 3.0);

assert_eq!(v.x, 1.0);
assert_eq!(v.y, 2.0);
assert_eq!(v.z, 3.0);

pub fn dot(self, other: Vec3) -> f32[src]

Returns dot product between current and other vector.

Examples

let a = vec3!(1.0, 2.0, 3.0);
let b = vec3!(3.0, 2.0, 1.0);

assert_eq!(a.dot(b), 10.0);

pub fn cross(self, other: Vec3) -> Vec3[src]

Returns cross product between current and other vector.

Examples

let i = vec3!(1.0, 0.0, 0.0);
let j = vec3!(0.0, 1.0, 0.0);
let k = vec3!(0.0, 0.0, 1.0);

assert_eq!(i.cross(j),  k);
assert_eq!(j.cross(i), -k);

pub fn len(self) -> f32[src]

Returns magnitude of this vector.

Examples

let v = vec3!(3.0, 4.0, 0.0);

assert_eq!(v.len(), 5.0);

pub fn len_sqr(self) -> f32[src]

Returns squared magnitude of this vector.

Examples

let v = vec3!(3.0, 4.0, 0.0);

assert_eq!(v.len_sqr(), 25.0);

pub fn normalize(self) -> Vec3[src]

Returns normalized version of this vector.

Important: it does not alter original vector.

Examples

let a = vec3!(10.0, 0.0, 0.0);
let b = a.normalize();

assert_ne!(a, b);

assert_eq!(a, vec3!(10.0, 0.0, 0.0));
assert_eq!(b, vec3!(1.0, 0.0, 0.0));

pub fn reflect(self, normal: Vec3) -> Vec3[src]

Returns reflected vector.

  • normal - Normal of the surface.

Examples

// reflective surface
let normal = vec3!(0.0, 1.0, 0.0);

// Reflection when ray is perpendicular
let incident = vec3!(0.0, -1.0, 0.0);
assert_eq!(incident.reflect(normal), vec3!(0.0, 1.0, 0.0));

// Incident ray coming at 45 degree angle
let incident = vec3!(-1.0, -1.0, 0.0);
assert_eq!(incident.reflect(normal), vec3!(-1.0, 1.0, 0.0));

pub fn refract(self, normal: Vec3, eta: f32) -> Option<Vec3>[src]

Returns refracted vector.

At critical angles (typically nearing 0), refraction is impossible. In such case, this function returns None.

  • normal - Normal of the surface.
  • eta - η₁ / η₂ of refraction indexes, where η₁ is the material that is being entered by the ray.

Examples

// Glass surface
let n = 1.5; // η₁ / η₂, where η₁ = 1.5 (glass) and η₂ = 1.0 (air)
let normal = vec3!(0.0, 1.0, 0.0);

// Rays perpendicular to the surface aren't affected by refraction
let incident = vec3!(0.0, -1.0, 0.0);
assert_eq!(incident.refract(normal, n), Some(incident));

// Rays coming from an angle are refracted
let incident = vec3!(-0.7, -1.0, 0.0);
assert_eq!(incident.refract(normal, n), Some(vec3!(-0.86019355, -0.5099678, 0.0)));

// At critical angles (nearing 0), rays aren't refracted, only reflected
let incident = vec3!(-1.0, -0.05, 0.0);
assert_eq!(incident.refract(normal, n), None); // from outside of the surface

let incident = vec3!(1.0, 0.05, 0.0);
assert_eq!(incident.refract(normal, n), None); // from inside of the surface

Trait Implementations

impl Clone for Vec3[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl Copy for Vec3[src]

impl PartialEq<Vec3> for Vec3[src]

impl Debug for Vec3[src]

impl Add<Vec3> for Vec3[src]

type Output = Vec3

The resulting type after applying the + operator.

fn add(self, other: Vec3) -> Self::Output[src]

Sums vector components.

Examples

let a = vec3!(1.0, 2.0, 5.0);
let b = vec3!(2.0, 4.0, 5.0);

assert_eq!(a + b, vec3!(3.0, 6.0, 10.0));

impl Sub<Vec3> for Vec3[src]

type Output = Vec3

The resulting type after applying the - operator.

fn sub(self, other: Vec3) -> Self::Output[src]

Subtracts vector components.

Examples

let a = vec3!(1.0, 2.0, 3.0);
let b = vec3!(0.5);

assert_eq!(a - b, vec3!(0.5, 1.5, 2.5));

impl Mul<Vec3> for Vec3[src]

type Output = Vec3

The resulting type after applying the * operator.

fn mul(self, other: Vec3) -> Self::Output[src]

Multiplies vector components. This is NOT a dot product.

Examples

let a = vec3!(1.0, 2.0, 3.0);
let b = vec3!(2.0);

assert_eq!(a * b, vec3!(2.0, 4.0, 6.0));

impl Mul<f32> for Vec3[src]

type Output = Vec3

The resulting type after applying the * operator.

fn mul(self, other: f32) -> Self::Output[src]

Multiplies each vector component by scalar.

Examples

let v = vec3!(1.0, 2.0, 3.0);
let s = 3.0;

assert_eq!(v * s, vec3!(3.0, 6.0, 9.0))

impl Mul<Vec3> for f32[src]

type Output = Vec3

The resulting type after applying the * operator.

fn mul(self, other: Vec3) -> Self::Output[src]

Multiplies each vector component by scalar.

Examples

let v = vec3!(1.0, 2.0, 3.0);
let s = 3.0;

assert_eq!(s * v, vec3!(3.0, 6.0, 9.0))

impl Div<Vec3> for Vec3[src]

type Output = Vec3

The resulting type after applying the / operator.

fn div(self, other: Vec3) -> Self::Output[src]

Divides vector components.

Examples

let a = vec3!(4.0, 2.0, 10.0);
let b = vec3!(2.0);

assert_eq!(a / b, vec3!(2.0, 1.0, 5.0));

impl Div<f32> for Vec3[src]

type Output = Vec3

The resulting type after applying the / operator.

fn div(self, other: f32) -> Self::Output[src]

Divides each vector component by scalar.

Examples

let a = vec3!(4.0, 2.0, 10.0);
let s = 2.0;

assert_eq!(a / s, vec3!(2.0, 1.0, 5.0));

impl Neg for Vec3[src]

type Output = Vec3

The resulting type after applying the - operator.

fn neg(self) -> Self::Output[src]

Negates each vector component.

Examples

let a = vec3!(1.0, 2.5, 7.0);
let b = -a;

assert_eq!(b.x, -a.x);
assert_eq!(b.y, -a.y);
assert_eq!(b.z, -a.z);

impl AddAssign<Vec3> for Vec3[src]

fn add_assign(&mut self, other: Vec3)[src]

Sums vector components and assigns the result to the vector on the left.

Examples

let mut a = vec3!(5.0);
let b = vec3!(1.0, 2.0, 3.0);
a += b;

assert_eq!(a, vec3!(6.0, 7.0, 8.0));

impl SubAssign<Vec3> for Vec3[src]

fn sub_assign(&mut self, other: Vec3)[src]

Subtracts vector components and assigns the result to the vector on the left.

Examples

let mut a = vec3!(5.0);
let b = vec3!(1.0, 2.0, 3.0);
a -= b;

assert_eq!(a, vec3!(4.0, 3.0, 2.0));

impl MulAssign<Vec3> for Vec3[src]

fn mul_assign(&mut self, other: Vec3)[src]

Multiplies vector components and assigns the result to the vector on the left.

Examples

let mut a = vec3!(1.0, 2.0, 3.0);
let b = vec3!(2.0);
a *= b;

assert_eq!(a, vec3!(2.0, 4.0, 6.0));

impl MulAssign<f32> for Vec3[src]

fn mul_assign(&mut self, other: f32)[src]

Multiplies each vector component by scalar and assigns the result to the vector.

Examples

let mut a = vec3!(1.0, 2.0, 3.0);
let s = 2.0;
a *= s;

assert_eq!(a, vec3!(2.0, 4.0, 6.0));

impl DivAssign<Vec3> for Vec3[src]

fn div_assign(&mut self, other: Vec3)[src]

Divides vector components and assigns the result to the vector on the left.

Examples

let mut a = vec3!(2.0, 4.0, 6.0);
let b = vec3!(2.0);
a /= b;

assert_eq!(a, vec3!(1.0, 2.0, 3.0));

impl DivAssign<f32> for Vec3[src]

fn div_assign(&mut self, other: f32)[src]

Divides each vector component by scalar and assigns the result to the vector.

Examples

let mut a = vec3!(2.0, 4.0, 6.0);
let s = 2.0;
a /= s;

assert_eq!(a, vec3!(1.0, 2.0, 3.0));

Auto Trait Implementations

impl Send for Vec3

impl Sync for Vec3

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.