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
//! Triangle-mesh.

use itertools::izip;
use ndarray::parallel::prelude::{IntoParallelRefIterator, ParallelIterator};

use crate::{
    geom::{Cube, Triangle},
    rt::{Ray, Side},
};

/// Mesh of two-dimensional triangles embedded in space as a three-dimensional mesh.
#[derive(Clone)]
pub struct Mesh {
    /// Bounding box.
    pub boundary: Cube,
    /// List of component triangles.
    pub tris: Vec<Triangle>,
}

impl Mesh {
    /// Construct a new instance.
    #[inline]
    #[must_use]
    pub fn new(tris: Vec<Triangle>) -> Self {
        debug_assert!(!tris.is_empty());

        // Calculate bounding box.
        let mut mins = tris[0].centre();
        let mut maxs = mins;
        for tri in &tris {
            for vert in tri.verts {
                for (a, (min, max)) in izip!(vert.iter(), izip!(mins.iter_mut(), maxs.iter_mut())) {
                    if *min > *a {
                        *min = *a;
                    } else if *max < *a {
                        *max = *a;
                    }
                }
            }
        }
        let mut boundary = Cube::new(mins, maxs);
        boundary.expand(0.01); // TODO: Consider what value is best here.

        Self { boundary, tris }
    }

    /// Check for an intersection with a given bounding box.
    #[inline]
    #[must_use]
    pub fn collides(&self, cube: &Cube) -> bool {
        if !self.boundary.collides(cube) {
            return false;
        }

        self.tris.par_iter().any(|tri| tri.collides(cube))
    }

    /// Determine if a Ray-Mesh intersection occurs.
    #[inline]
    #[must_use]
    pub fn hit(&self, ray: &Ray) -> bool {
        if !self.boundary.hit(ray) {
            return false;
        }

        self.tris.par_iter().any(|t| t.hit(ray))
    }

    /// Determine the distance to a Ray-Mesh intersection.
    #[inline]
    #[must_use]
    pub fn dist(&self, ray: &Ray) -> Option<f64> {
        if !self.boundary.hit(ray) {
            return None;
        }

        self.tris
            .par_iter()
            .filter_map(|tri| tri.dist(ray))
            .min_by(|a, b| {
                a.partial_cmp(b)
                    .expect("Failed to perform Ray-Mesh intersection")
            })
    }

    /// Determine the distance and facing side of a Ray-Mesh intersection.
    #[inline]
    #[must_use]
    pub fn dist_side(&self, ray: &Ray) -> Option<(f64, Side)> {
        if !self.boundary.hit(ray) {
            return None;
        }

        self.tris
            .par_iter()
            .filter_map(|tri| tri.dist_side(ray))
            .min_by(|a, b| {
                a.0.partial_cmp(&b.0)
                    .expect("Failed to perform Ray-Mesh intersection")
            })
    }
}