raytracing/raytrace/
raytrace.rs

1use core::ops::BitOr;
2
3use num::Float;
4
5use super::RaytraceWithNorm;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
8pub struct Raytrace<F, const D: usize>
9where
10    F: Float
11{
12    pub t: F
13}
14
15impl<F, const D: usize> BitOr for Raytrace<F, D>
16where
17    F: Float
18{
19    type Output = Self;
20
21    fn bitor(self, rhs: Self) -> Self::Output
22    {
23        if self.t < rhs.t {self} else {rhs}
24    }
25}
26
27impl<F, const D: usize> Raytrace<F, D>
28where
29    F: Float
30{
31    pub fn miss() -> Self
32    {
33        Self {
34            t: F::infinity()
35        }
36    }
37
38    pub fn is_hit(&self) -> bool
39    {
40        self.t >= F::zero() && self.t.is_finite()
41    }
42
43    pub fn is_miss(&self) -> bool
44    {
45        !self.is_hit()
46    }
47
48    pub fn with_norm(self, norm: impl FnOnce() -> [F; D]) -> RaytraceWithNorm<F, D>
49    {
50        RaytraceWithNorm {
51            raytrace: self,
52            n: match self.is_hit()
53            {
54                true => Some(norm()),
55                false => None
56            }
57        }
58    }
59
60    pub fn min(self, rhs: Self) -> Self
61    {
62        if self > rhs
63        {
64            rhs
65        }
66        else
67        {
68            self
69        }
70    }
71
72    pub fn max(self, rhs: Self) -> Self
73    {
74        if self < rhs
75        {
76            rhs
77        }
78        else
79        {
80            self
81        }
82    }
83}