raytracing-rs

https://raytracing.github.io in Rust

git clone https://code.pdelong.com/raytracing-rs.git

 1mod ray;
 2mod vec3;
 3
 4use core::f64;
 5
 6pub use ray::*;
 7pub use vec3::*;
 8
 9#[derive(Debug, Copy, Clone, PartialEq)]
10pub struct NotNanF64(f64);
11
12// We can implement these two because we know NotNanF64 can't contain a NaN
13impl Eq for NotNanF64 {}
14impl PartialOrd for NotNanF64 {
15    fn partial_cmp(&self, other: &NotNanF64) -> Option<std::cmp::Ordering> {
16        Some(self.cmp(other))
17    }
18}
19impl Ord for NotNanF64 {
20    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
21        self.0.partial_cmp(&other.0).unwrap()
22    }
23}
24
25impl NotNanF64 {
26    pub fn new(v: f64) -> Option<NotNanF64> {
27        if v.is_nan() { None } else { Some(NotNanF64(v)) }
28    }
29
30    pub fn get(&self) -> f64 {
31        self.0
32    }
33}
34
35#[derive(Debug, Clone)]
36pub struct Interval {
37    pub min: f64,
38    pub max: f64,
39}
40
41impl Interval {
42    pub fn new(min: f64, max: f64) -> Option<Interval> {
43        if min > max {
44            None
45        } else {
46            Some(Interval { min, max })
47        }
48    }
49
50    pub fn contains(&self, t: f64) -> bool {
51        self.min <= t && t <= self.max
52    }
53
54    pub fn surrounds(&self, t: f64) -> bool {
55        self.min < t && t < self.max
56    }
57}
58
59impl Default for Interval {
60    fn default() -> Self {
61        Interval {
62            min: f64::NEG_INFINITY,
63            max: f64::INFINITY,
64        }
65    }
66}