alloy/geometry/
plane.rs

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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use std::fmt;
use std::ops::{Deref, DerefMut};

use crate::cmp::AlmostEq;
use crate::math::vec::{Vec2, Vec3, Vec4, Vector3, Vector4};
use crate::ops::{Cross, Dot};

use super::{Encloses, Intersects, Point3};

/// A value type that represents a plane in 3D space with the equation:
/// `a*x + b*y + c*z + d = 0`.
///
/// Points can implicitly deref into [`Vec4`] to allow for easily using points
/// in places where vectors are expected.
#[repr(C)]
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
pub struct Plane {
  data: Vector4,
}

impl fmt::Debug for Plane {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("Plane")
      .field("a", &self.a())
      .field("b", &self.b())
      .field("c", &self.c())
      .field("d", &self.d())
      .finish()
  }
}

// Construction
impl Plane {
  /// Creates a new plane with the given coefficients.
  ///
  /// # Parameters
  ///
  /// * `a` - The first component of the plane
  /// * `b` - The second component of the plane
  /// * `c` - The third component of the plane
  /// * `d` - The fourth component of the plane
  #[inline(always)]
  pub const fn new(a: f32, b: f32, c: f32, d: f32) -> Self {
    Self {
      data: Vector4::new(a, b, c, d),
    }
  }

  /// Create a new plane from a normal and distance
  ///
  /// # Parameters
  ///
  /// * `normal` - The normal of the plane
  /// * `distance` - The distance of the plane
  #[inline]
  pub const fn from_normal_distance(normal: &Vec3, distance: f32) -> Self {
    Self {
      data: Vector4::new(normal.x(), normal.y(), normal.z(), distance),
    }
  }

  /// Create a new plane from a point and normal
  ///
  /// # Parameters
  ///
  /// * `point` - The point on the plane
  /// * `normal` - The normal of the plane
  pub fn from_point_and_normal(point: &Point3, normal: &Vec3) -> Self {
    let d = -point.dot(normal);
    Self {
      data: Vector4::new(normal.x(), normal.y(), normal.z(), d),
    }
  }

  /// Create a new plane from three points in counter-clockwise order.
  ///
  /// # Parameters
  ///
  /// * `p0` - The first point
  /// * `p1` - The second point
  /// * `p2` - The third point
  pub fn from_points_clockwise(p0: &Point3, p1: &Point3, p2: &Point3) -> Self {
    let normal = (p2 - p0).cross(&(p1 - p0)).normalized();

    Self::from_point_and_normal(p0, &normal)
  }

  /// Create a new plane from three points in counter-clockwise order.
  /// This is the same as `from_points_clockwise` but with the points in a different order.
  ///
  /// # Parameters
  ///
  /// * `p0` - The first point
  /// * `p1` - The second point
  /// * `p2` - The third point
  pub fn from_points_counter_clockwise(p0: &Point3, p1: &Point3, p2: &Point3) -> Self {
    let normal = (p1 - p0).cross(&(p2 - p0)).normalized();

    Self::from_point_and_normal(p0, &normal)
  }
}

// Getters / Setters
impl Plane {
  /// Gets the 'a' component of the plane
  #[inline(always)]
  pub const fn a(&self) -> f32 {
    self.data.as_vec4().x()
  }

  /// Gets the 'b' component of the plane
  #[inline(always)]
  pub const fn b(&self) -> f32 {
    self.data.as_vec4().y()
  }

  /// Gets the 'c' component of the plane
  #[inline(always)]
  pub const fn c(&self) -> f32 {
    self.data.as_vec4().z()
  }

  /// Gets the 'd' component of the plane
  #[inline(always)]
  pub const fn d(&self) -> f32 {
    self.data.as_vec4().w()
  }

  /// Gets the 'a' and 'b' components of the plane
  /// as a 2D vector
  #[inline(always)]
  pub const fn ab(&self) -> &Vec2 {
    self.data.as_vec4().xy()
  }

  /// Gets the 'b' and 'c' components of the plane
  /// as a 2D vector
  #[inline(always)]
  pub const fn bc(&self) -> &Vec2 {
    self.data.as_vec4().yz()
  }

  /// Gets the 'c' and 'd' components of the plane
  /// as a 2D vector
  #[inline(always)]
  pub const fn cd(&self) -> &Vec2 {
    self.data.as_vec4().zw()
  }

  /// Gets the 'a', 'b', and 'c' components of the plane
  /// as a 3D vector
  #[inline(always)]
  pub const fn abc(&self) -> &Vec3 {
    self.data.as_vec4().xyz()
  }

  /// Gets the 'b', 'c', and 'd' components of the plane
  /// as a 3D vector
  #[inline(always)]
  pub const fn bcd(&self) -> &Vec3 {
    self.data.as_vec4().yzw()
  }

  /// Gets the 'a', 'b', 'c', and 'd' components of the plane
  /// as a 4D vector
  #[inline(always)]
  pub const fn abcd(&self) -> &Vec4 {
    self.data.as_vec4()
  }

  /// Gets the normal of this plane
  #[inline(always)]
  pub const fn normal(&self) -> &Vec3 {
    self.data.as_vec4().xyz()
  }

  /// Gets the 'a' component of the plane
  #[inline(always)]
  pub fn a_mut(&mut self) -> &mut f32 {
    self.data.as_mut_vec4().x_mut()
  }

  /// Gets the 'b' component of the plane
  #[inline(always)]
  pub fn b_mut(&mut self) -> &mut f32 {
    self.data.as_mut_vec4().y_mut()
  }

  /// Gets the 'c' component of the plane
  #[inline(always)]
  pub fn c_mut(&mut self) -> &mut f32 {
    self.data.as_mut_vec4().z_mut()
  }

  /// Gets the 'd' component of the plane
  #[inline(always)]
  pub fn d_mut(&mut self) -> &mut f32 {
    self.data.as_mut_vec4().w_mut()
  }

  /// Gets the 'a' and 'b' components of the plane
  /// as a 2D vector
  #[inline(always)]
  pub fn ab_mut(&mut self) -> &mut Vec2 {
    self.data.as_mut_vec4().xy_mut()
  }

  /// Gets the 'b' and 'c' components of the plane
  /// as a 2D vector
  #[inline(always)]
  pub fn bc_mut(&mut self) -> &mut Vec2 {
    self.data.as_mut_vec4().yz_mut()
  }

  /// Gets the 'c' and 'd' components of the plane
  /// as a 2D vector
  #[inline(always)]
  pub fn cd_mut(&mut self) -> &mut Vec2 {
    self.data.as_mut_vec4().zw_mut()
  }

  /// Gets the 'a', 'b', and 'c' components of the plane
  /// as a 3D vector
  #[inline(always)]
  pub fn abc_mut(&mut self) -> &mut Vec3 {
    self.data.as_mut_vec4().xyz_mut()
  }

  /// Gets the 'b', 'c', and 'd' components of the plane
  /// as a 3D vector
  #[inline(always)]
  pub fn bcd_mut(&mut self) -> &mut Vec3 {
    self.data.as_mut_vec4().yzw_mut()
  }

  /// Gets the 'a', 'b', 'c', and 'd' components of the plane
  /// as a 4D vector
  #[inline(always)]
  pub fn abcd_mut(&mut self) -> &mut Vec4 {
    self.data.as_mut_vec4()
  }

  /// Sets the 'a' component of the plane
  ///
  /// # Parameters
  ///
  /// * `a` - The new value of the 'a' component
  #[inline(always)]
  pub fn set_a(&mut self, a: f32) {
    self.data.set_x(a)
  }

  /// Sets the 'b' component of the plane
  ///
  /// # Parameters
  ///
  /// * `b` - The new value of the 'b' component
  #[inline(always)]
  pub fn set_b(&mut self, b: f32) {
    self.data.set_y(b)
  }

  /// Sets the 'c' component of the plane
  ///
  /// # Parameters
  ///
  /// * `c` - The new value of the 'c' component
  #[inline(always)]
  pub fn set_c(&mut self, c: f32) {
    self.data.set_z(c)
  }

  /// Sets the 'd' component of the plane
  ///
  /// # Parameters
  ///
  /// * `d` - The new value of the 'd' component
  #[inline(always)]
  pub fn set_d(&mut self, d: f32) {
    self.data.set_w(d)
  }

  /// Gets the 'a' and 'b' components of the plane
  ///
  /// # Parameters
  ///
  /// * `ab` - The new value of the 'a' and 'b' components
  #[inline(always)]
  pub fn set_ab(&mut self, ab: &Vec2) {
    self.data.set_xy(ab)
  }

  /// Gets the 'b' and 'c' components of the plane
  ///
  /// # Parameters
  ///
  /// * `bc` - The new value of the 'b' and 'c' components
  #[inline(always)]
  pub fn set_bc(&mut self, bc: &Vec2) {
    self.data.set_yz(bc)
  }

  /// Gets the 'c' and 'd' components of the plane
  ///
  /// # Parameters
  ///
  /// * `cd` - The new value of the 'c' and 'd' components
  #[inline(always)]
  pub fn set_cd(&mut self, cd: &Vec2) {
    self.data.set_yz(cd)
  }

  /// Gets the 'a', 'b', and 'c' components of the plane
  ///
  /// # Parameters
  ///
  /// * `abc` - The new value of the 'a', 'b', and 'c' components
  #[inline(always)]
  pub fn set_abc(&mut self, abc: &Vec3) {
    self.data.set_xyz(abc)
  }

  /// Gets the 'b', 'c', and 'd' components of the plane
  ///
  /// # Parameters
  ///
  /// * `bcd` - The new value of the 'b', 'c', and 'd' components
  #[inline(always)]
  pub fn set_bcd(&mut self, bcd: &Vec3) {
    self.data.set_yzw(bcd)
  }

  /// Gets the 'a', 'b', 'c', and 'd' components of the plane
  ///
  /// # Parameters
  ///
  /// * `abcd` - The new value of the 'a', 'b', 'c', and 'd' components
  #[inline(always)]
  pub fn set(&mut self, abcd: &Vec4) {
    self.data.set(abcd)
  }
}

// Conversion
impl Plane {
  /// Gets the normal of this plane
  #[inline(always)]
  pub const fn as_vec4(&self) -> &Vec4 {
    self.data.as_vec4()
  }

  /// Gets the normal of this plane
  #[inline(always)]
  pub fn as_mut_vec4(&mut self) -> &mut Vec4 {
    self.data.as_mut_vec4()
  }
}

impl AsRef<Vec4> for Plane {
  #[inline(always)]
  fn as_ref(&self) -> &Vec4 {
    self.as_vec4()
  }
}

impl AsMut<Vec4> for Plane {
  #[inline(always)]
  fn as_mut(&mut self) -> &mut Vec4 {
    self.as_mut_vec4()
  }
}

impl Deref for Plane {
  type Target = Vec4;

  #[inline(always)]
  fn deref(&self) -> &Self::Target {
    self.as_vec4()
  }
}

impl DerefMut for Plane {
  #[inline(always)]
  fn deref_mut(&mut self) -> &mut Self::Target {
    self.as_mut_vec4()
  }
}

// Queries
impl Plane {
  /// Gets the distance to the point from this plane
  ///
  /// # Parameters
  ///
  /// * `point` - The point to get the distance to
  pub fn distance_to_point(&self, point: &Point3) -> f32 {
    use crate::ops::Dot;
    let abc = self.data.as_vec4().xyz();

    abc.dot(point.as_vec3()) + self.d()
  }

  /// Checks if the plane contains the point within the default epsilon.
  ///
  /// # Parameters
  ///
  /// * `point` - The point to check
  pub fn contains_point(&self, point: &Point3) -> bool {
    self.distance_to_point(point).almost_eq(&0.0)
  }

  /// Checks if the point exists over the plane
  ///
  /// # Parameters
  ///
  /// * `point` - The point to check
  pub fn is_point_over_plane(&self, point: &Point3) -> bool {
    self.data.as_vec4().xyz().dot(point.as_vec3()) > self.d()
  }

  /// Checks if the point exists under the plane
  ///
  /// # Parameters
  ///
  /// * `point` - The point to check
  pub fn is_point_under_plane(&self, point: &Point3) -> bool {
    self.data.as_vec4().xyz().dot(point.as_vec3()) < self.d()
  }

  /// Gets the nearest point on the plane to the given point
  ///
  /// # Parameters
  ///
  /// * `point` - The point to get the nearest point to
  pub fn nearest_point(&self, point: &Point3) -> Point3 {
    let abc = self.data.as_vec4().xyz();

    let t = -(abc.dot(point.as_vec3()) + self.d());
    let scaled_normal = abc * t;

    *point + scaled_normal.as_vec3()
  }
}

impl Intersects for Plane {
  fn intersects(&self, other: &Self) -> bool {
    let normal = self.normal().cross(other.normal());
    !normal.almost_eq(&Vector3::ZERO)
  }
}

impl Encloses<Point3> for Plane {
  fn encloses(&self, point: &Point3) -> bool {
    self.contains_point(point)
  }
}

// Modifiers
impl Plane {
  /// Computes and returns the plane that is this plane's inverse.
  pub fn inverted(&self) -> Self {
    Self::new(-self.a(), -self.b(), -self.c(), -self.d())
  }
}

#[cfg(test)]
#[path = "plane.test.rs"]
mod test;