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
use material::Material;
use nalgebra::Vector3;
use ray::Ray;
use Hit;
use PrimitiveType;
use Traceable;
type Vec3 = Vector3<f32>;

#[derive(Copy, Clone)]
pub struct Plane {
	pub position: Vec3,
	pub normal: Vec3,
	pub material: Material,
}

impl Plane {
	// Spawn a new plane
	pub fn new(position: Vec3, normal: Vec3, material: Material) -> Plane {
		Plane {
			position: position,
			normal: normal,
			material: material,
		}
	}
}

impl Traceable for Plane {
	// Ray-Plane Intersection
	fn intersect(&self, r: &Ray, result: &mut ::Hit) -> bool {
		let plane_normal = -self.normal;
		let denom = plane_normal.dot(&r.direction);

		if denom > 1e-6 {
			result.t = plane_normal.dot(&(self.position - r.origin)) / denom;
			result.p = r.origin + r.direction * result.t;
			result.n = if self.normal.dot(&r.direction) < 0.0 {
				self.normal
			} else {
				-self.normal
			};
			result.material = self.material;

			return true;
		} else {
			return false;
		}
	}

	fn get_primitive_type(&self) -> PrimitiveType {
		PrimitiveType::Plane
	}
}