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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use core::ops::Neg;
use crate::Vector3;
#[cfg(not(feature = "no_rays"))]
use crate::{Ray3, Math, interfaces::IRaycast, collision::{RaycastInfo, RaycastInfoBuilder}};
/// A struct that represents a 3D plane
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy)]
pub struct Plane {
/// The normal perpendicular to the plane
normal: Vector3,
/// The distance from origin, up towards the normal where the plane lies
distance: f32,
}
/// Constructors
impl Plane {
/// Create a new 3D plane
/// - **normal**: The normal perpendicular to the plane
/// - **distance**: The distance from origin, up towards the normal where the plane lies
///
/// **Returns**: Returns a new 3D plane
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let plane = Plane::new(Vector3::one(), 1.0);
/// assert_eq!(0.57735026 * Vector3::one(), plane.normal());
/// assert_eq!(1.0, plane.distance());
/// ```
pub fn new(normal: Vector3, distance: f32) -> Self {
Plane {
normal: normal.normalize(),
distance,
}
}
/// Creates a new 3D plane from a normal and a given point
/// - **normal**: The normal perpendicular to the plane
/// - **point**: The point on the plane
///
/// **Returns**: Returns a new 3D plane from a normal and a given point
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane, Math, assert_range};
/// let plane = Plane::new_from_point(Vector3::one(), Vector3::new(-1.0, 0.5, 2.5));
/// assert_eq!(0.57735026 * Vector3::one(), plane.normal());
/// assert_range!(-1.1547005, plane.distance());
/// ```
pub fn new_from_point(normal: Vector3, point: Vector3) -> Self {
let normal = normal.normalize();
Plane {
normal,
distance: -(normal * point),
}
}
/// Creates a new 3D plane from 3 separate points
/// - **a**: The first point used to triangulate the plane
/// - **b**: The second point used to triangulate the plane
/// - **c**: The third point used to triangulate the plane
///
/// **Returns**: Returns a new 3D plane from 3 separate points
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane, Math, assert_range};
/// let plane = Plane::new_triangulated(
/// Vector3::new(0.0, 0.2, 0.4),
/// Vector3::new(0.6, 0.8, 1.0),
/// Vector3::new(0.3, 0.6, -0.9)
/// );
/// assert_eq!(Vector3::new(-0.7275328, 0.6847368, 0.04279606), plane.normal());
/// assert_range!(-0.1540658, plane.distance());
/// ```
pub fn new_triangulated(a: Vector3, b: Vector3, c: Vector3) -> Self {
let p = b - a;
let q = c - a;
let normal = p.cross(q).normalize();
Plane {
normal,
distance: -(normal * a),
}
}
/// Creates a plane that spans the X and Y axis
///
/// **Returns**: Returns a plane that spans the X and Y axis
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let plane = Plane::xy_plane();
/// assert!(plane.is_on_plane(Vector3::new(100.0, -100.0, 0.0)));
/// ```
pub fn xy_plane() -> Self { Plane::new(Vector3::forward(), 0.0) }
/// Creates a plane that spans the X and Z axis
///
/// **Returns**: Returns a plane that spans the X and Z axis
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let plane = Plane::xz_plane();
/// assert!(plane.is_on_plane(Vector3::new(1.0, 0.0, 2.0)));
/// ```
pub fn xz_plane() -> Self { Plane::new(Vector3::up(), 0.0) }
/// Creates a plane that spans the Y and Z axis
///
/// **Returns**: Returns a plane that spans the Y and Z axis
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let plane = Plane::yz_plane();
/// assert!(plane.is_on_plane(Vector3::new(0.0, -10.0, 10.0)));
/// ```
pub fn yz_plane() -> Self { Plane::new(Vector3::right(), 0.0) }
}
/// Properties
impl Plane {
/// Gets the normal of the plane
///
/// **Returns**: Returns the normal of the plane
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let plane = Plane::new(Vector3::one(), 1.0);
/// assert_eq!(0.57735026 * Vector3::one(), plane.normal());
/// ```
pub fn normal(&self) -> Vector3 { self.normal }
/// Sets the normal of the plane
/// - **value**: The normal to set the plane
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let mut plane = Plane::new(Vector3::down(), 1.0);
/// plane.set_normal(Vector3::one());
/// assert_eq!(0.57735026 * Vector3::one(), plane.normal());
/// ```
pub fn set_normal(&mut self, value: Vector3) { self.normal = value.normalize(); }
/// Gets the distance up the normal of the plane
///
/// **Returns**: Returns the distance up the normal of the plane
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let plane = Plane::new(Vector3::down(), 1.0);
/// assert_eq!(1.0, plane.distance());
/// ```
pub fn distance(&self) -> f32 { self.distance }
/// Sets the distance up the normal of the plane
/// - **value**: The value to set the distance for
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let mut plane = Plane::new(Vector3::down(), 1.0);
/// plane.set_distance(2.0);
/// assert_eq!(2.0, plane.distance());
/// ```
pub fn set_distance(&mut self, value: f32) { self.distance = value; }
/// Flips the plane to the opposite direction
///
/// **Returns**: Returns the flipped plane
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let plane = Plane::new(Vector3::one(), 1.0).flipped();
/// assert_eq!(-0.57735026 * Vector3::one(), plane.normal());
/// assert_eq!(-1.0, plane.distance());
/// ```
pub fn flipped(self) -> Self { Plane::new(-self.normal, -self.distance) }
}
/// Public Methods
impl Plane {
/// Finds if the point is on the plane
/// - **point**: The point to check with
///
/// **Returns**: Returns true if the point is on the plane
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let plane = Plane::yz_plane();
/// assert!(plane.is_on_plane(Vector3::new(0.0, -10.0, 10.0)));
/// ```
pub fn is_on_plane(&self, point: Vector3) -> bool { Math::approx((self.normal * point) + self.distance, 0.0) }
/// Gets the closest point on the plane from the given point
/// - **point**: The point to find the closest point on the plane with
///
/// **Returns**: Returns the closest point on the plane from the given point
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let plane = Plane::new(Vector3::new(1.0, -2.0, 3.0), 3.0);
/// let point = plane.closest_point(Vector3::one());
/// assert_eq!(Vector3::new(0.05535913, 2.889282, -1.833922), point);
/// ```
pub fn closest_point(self, point: Vector3) -> Vector3 {
point - self.normal * self.distance_to_point(point)
}
/// Gets the distance from the point to the plane
/// - **point**: The point to find the distance from the plane
///
/// **Returns**: Returns the distance from the point to the plane
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane, Math, assert_range};
/// let plane = Plane::new(Vector3::new(1.0, -2.0, 3.0), 3.0);
/// let distance = plane.distance_to_point(Vector3::one());
/// assert_range!(3.534523, distance)
/// ```
pub fn distance_to_point(self, point: Vector3) -> f32 { (self.normal * point) + self.distance }
/// Finds if the point is on the positive side of the plane
/// - **point**: The point to find the if it's on the positive side of the plane
///
/// **Returns**: Returns true if the point is on the positive side of the plane
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let plane = Plane::new(Vector3::new(1.0, -2.0, 3.0), 3.0);
/// assert!(plane.is_on_positive_side(Vector3::one()));
/// ```
pub fn is_on_positive_side(&self, point: Vector3) -> bool {
self.distance_to_point(point) > 0.0
}
/// Finds if the two given points are on the same side of the plane
/// - **a**: The first point to query with
/// - **b**: The second point to query with
///
/// **Returns**: Returns true if both points are on the same side of the plane
/// #### Examples
/// ```
/// # use mathx::{Vector3, Plane};
/// let plane = Plane::new(Vector3::new(1.0, -2.0, 3.0), 3.0);
/// let a = Vector3::one();
/// let b = Vector3::right();
/// let c = Vector3::new(-10.0, -20.0, -30.0);
/// assert!(plane.is_on_same_side(a, b));
/// assert!(!plane.is_on_same_side(a, c));
/// ```
pub fn is_on_same_side(&self, a: Vector3, b: Vector3) -> bool {
self.is_on_positive_side(a) == self.is_on_positive_side(b)
}
}
#[cfg(not(feature = "no_rays"))]
impl IRaycast for Plane {
/// Raycasts with the given ray
/// - **ray**: The ray to raycast with
///
/// **Returns**: Returns the information on the raycast
fn raycast(&self, ray: Ray3) -> RaycastInfo {
let diff = ray.direction().dot(self.normal);
let dist = -(ray.origin().dot(self.normal) + self.distance);
if Math::approx(diff, 0.0) {
return RaycastInfo::empty();
}
let distance = dist / diff;
return RaycastInfoBuilder::new()
.set_hit(distance > 0.0)
.set_distance(distance)
.set_normal(self.normal)
.set_point(ray.get_point(distance))
.build();
}
}
unsafe impl Send for Plane {}
unsafe impl Sync for Plane {}
impl Eq for Plane {}
impl PartialEq for Plane {
fn eq(&self, other: &Self) -> bool {
self.normal == other.normal
&& self.distance == other.distance
}
}
#[cfg(not(feature = "no_std"))]
impl std::fmt::Display for Plane {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!("normal: {}, distance: {}", self.normal, self.distance))
}
}
impl Neg for Plane {
type Output = Plane;
fn neg(self) -> Self::Output { self.flipped() }
}