arctk/rt/
side.rs

1//! Hit side.
2
3use nalgebra::{Unit, Vector3};
4
5/// Differentiates the side of a surface.
6#[allow(clippy::exhaustive_enums)]
7#[derive(Clone)]
8pub enum Side {
9    /// Inside surface.
10    Inside(Unit<Vector3<f64>>),
11    /// Outside surface.
12    Outside(Unit<Vector3<f64>>),
13}
14
15impl Side {
16    /// Construct a new instance.
17    #[inline]
18    #[must_use]
19    pub fn new(dir: &Unit<Vector3<f64>>, norm: Unit<Vector3<f64>>) -> Self {
20        if dir.dot(&norm) < 0.0 {
21            Self::Outside(norm)
22        } else {
23            Self::Inside(-norm)
24        }
25    }
26
27    /// Check if the side is an inside.
28    #[inline]
29    #[must_use]
30    pub const fn is_inside(&self) -> bool {
31        match *self {
32            Self::Inside(..) => true,
33            Self::Outside(..) => false,
34        }
35    }
36
37    /// Reference the surface-normal vector.
38    /// This points away from the constructing direction normal.
39    #[inline]
40    #[must_use]
41    pub const fn norm(&self) -> &Unit<Vector3<f64>> {
42        match *self {
43            Self::Inside(ref norm) | Self::Outside(ref norm) => norm,
44        }
45    }
46}