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
148
149
150
use std::fmt;
use rust_num::{one, Zero, zero};
use approx::ApproxEq;
use intersect::Intersect;
use num::{BaseFloat};
use point::{Point, Point3};
use ray::Ray3;
use vector::{Vector3, Vector4};
use vector::{Vector, EuclideanVector};
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Plane<S> {
pub n: Vector3<S>,
pub d: S,
}
impl<S: BaseFloat> Plane<S> {
pub fn new(n: Vector3<S>, d: S) -> Plane<S> {
Plane { n: n, d: d }
}
pub fn from_abcd(a: S, b: S, c: S, d: S) -> Plane<S> {
Plane { n: Vector3::new(a, b, c), d: d }
}
pub fn from_vector4(v: Vector4<S>) -> Plane<S> {
Plane { n: Vector3::new(v.x, v.y, v.z), d: v.w }
}
pub fn from_vector4_alt(v: Vector4<S>) -> Plane<S> {
Plane { n: Vector3::new(v.x, v.y, v.z), d: -v.w }
}
pub fn from_points(a: Point3<S>, b: Point3<S>, c: Point3<S>) -> Option<Plane<S>> {
let v0 = b.sub_p(&a);
let v1 = c.sub_p(&a);
let mut n = v0.cross(&v1);
if n.approx_eq(&zero()) { None }
else {
n.normalize_self();
let d = -a.dot(&n);
Some(Plane::new(n, d))
}
}
pub fn from_point_normal(p: Point3<S>, n: Vector3<S>) -> Plane<S> {
Plane { n: n, d: p.dot(&n) }
}
pub fn normalize(&self) -> Option<Plane<S>> {
if self.n.approx_eq(&zero()) { None }
else {
let denom = one::<S>() / self.n.length();
Some(Plane::new(self.n.mul_s(denom), self.d*denom))
}
}
}
impl<S: BaseFloat> Intersect<Option<Point3<S>>> for (Plane<S>, Ray3<S>) {
fn intersection(&self) -> Option<Point3<S>> {
let (ref p, ref r) = *self;
let t = -(p.d + r.origin.dot(&p.n)) / r.direction.dot(&p.n);
if t < Zero::zero() { None }
else { Some(r.origin.add_v(&r.direction.mul_s(t))) }
}
}
impl<S: BaseFloat> Intersect<Option<Ray3<S>>> for (Plane<S>, Plane<S>) {
fn intersection(&self) -> Option<Ray3<S>> {
panic!("Not yet implemented");
}
}
impl<S: BaseFloat> Intersect<Option<Point3<S>>> for (Plane<S>, Plane<S>, Plane<S>) {
fn intersection(&self) -> Option<Point3<S>> {
panic!("Not yet implemented");
}
}
impl<S: BaseFloat + ApproxEq<S>>
ApproxEq<S> for Plane<S> {
#[inline]
fn approx_eq_eps(&self, other: &Plane<S>, epsilon: &S) -> bool {
self.n.approx_eq_eps(&other.n, epsilon) &&
self.d.approx_eq_eps(&other.d, epsilon)
}
}
impl<S: BaseFloat> fmt::Debug for Plane<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}x + {:?}y + {:?}z - {:?} = 0",
self.n.x, self.n.y, self.n.z, self.d)
}
}