1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::f32;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut};

use linalg::{self, Vector};

/// Normal is a standard 3 component normal but transforms as a normal
/// normal when transformations are applied
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Normal {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

impl Normal {
    /// Initialize the normal and set values for x, y, z
    pub fn new(x: f32, y: f32, z: f32) -> Normal {
        Normal { x: x, y: y, z: z }
    }
    /// Initialize the normal with the same value of x, y, z
    pub fn broadcast(x: f32) -> Normal {
        Normal { x: x, y: x, z: x }
    }
    /// Compute the squared length of the normal
    pub fn length_sqr(&self) -> f32 {
        self.x * self.x + self.y * self.y + self.z * self.z
    }
    /// Compute the length of the normal
    pub fn length(&self) -> f32 {
        f32::sqrt(self.length_sqr())
    }
    /// Get a normalized copy of this normal
    pub fn normalized(&self) -> Normal {
        let len = self.length();
        Normal { x: self.x / len, y: self.y / len, z: self.z / len }
    }
    /// Return a normal facing along the same direction as v
    pub fn face_forward(&self, v: &Vector) -> Normal {
        if linalg::dot(self, v) < 0f32 { -*self } else { *self }
    }
}

impl Add for Normal {
    type Output = Normal;
    /// Add two normals together
    fn add(self, rhs: Normal) -> Normal {
        Normal { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z }
    }
}

impl Sub for Normal {
    type Output = Normal;
    /// Subtract two normals
    fn sub(self, rhs: Normal) -> Normal {
        Normal { x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z }
    }
}

impl Mul for Normal {
    type Output = Normal;
    /// Multiply two normals
    fn mul(self, rhs: Normal) -> Normal {
        Normal { x: self.x * rhs.x, y: self.y * rhs.y, z: self.z * rhs.z }
    }
}

impl Mul<f32> for Normal {
    type Output = Normal;
    /// Scale the normal by some value
    fn mul(self, rhs: f32) -> Normal {
        Normal { x: self.x * rhs, y: self.y * rhs, z: self.z * rhs }
    }
}

impl Mul<Normal> for f32 {
    type Output = Normal;
    /// Scale the normal by some value
    fn mul(self, rhs: Normal) -> Normal {
        Normal { x: self * rhs.x, y: self * rhs.y, z: self * rhs.z }
    }
}

impl Div for Normal {
    type Output = Normal;
    /// Divide the normals components by the right hand side's components
    fn div(self, rhs: Normal) -> Normal {
        Normal { x: self.x / rhs.x, y: self.y / rhs.y, z: self.z / rhs.z }
    }
}

impl Div<f32> for Normal {
    type Output = Normal;
    /// Divide the normals components by scalar
    fn div(self, rhs: f32) -> Normal {
        Normal { x: self.x / rhs, y: self.y / rhs, z: self.z / rhs }
    }
}

impl Neg for Normal {
    type Output = Normal;
    /// Negate the normal
    fn neg(self) -> Normal {
        Normal { x: -self.x, y: -self.y, z: -self.z }
    }
}

impl Index<usize> for Normal {
    type Output = f32;
    /// Access the normal by index
    ///
    /// - 0 = x
    /// - 1 = y
    /// - 2 = z
    fn index(&self, i: usize) -> &f32 {
        match i {
            0 => &self.x,
            1 => &self.y,
            2 => &self.z,
            _ => panic!("Invalid index into normal"),
        }
    }
}

impl IndexMut<usize> for Normal {
    /// Access the normal by index
    ///
    /// - 0 = x
    /// - 1 = y
    /// - 2 = z
    fn index_mut(&mut self, i: usize) -> &mut f32 {
        match i {
            0 => &mut self.x,
            1 => &mut self.y,
            2 => &mut self.z,
            _ => panic!("Invalid index into normal"),
        }
    }
}

#[test]
fn test_face_fwd() {
    let n = Normal::new(1f32, 0f32, 0f32);
    let v = Vector::new(-1f32, 0f32, 0f32);
    let n_fwd = n.face_forward(&v);
    assert!(n_fwd == Normal::new(-1f32, 0f32, 0f32));
}