raytracing/shapes/nd/
hypercube.rs

1use core::ops::AddAssign;
2
3use num::Float;
4
5use crate::{shapes::Shape, Ray, Raytrace, RaytraceWithNorm};
6
7use super::HyperRectangle;
8
9#[derive(Debug, Clone, Copy)]
10pub struct HyperCube<F, const D: usize>
11where
12    F: Float
13{
14    pub center: [F; D],
15    pub radius: F
16}
17
18impl<F, const D: usize> From<HyperCube<F, D>> for HyperRectangle<F, D>
19where
20    F: Float
21{
22    fn from(cube: HyperCube<F, D>) -> Self
23    {
24        let c1 = cube.center.map(|c| c - cube.radius);
25        let c2 = cube.center.map(|c| c + cube.radius);
26        Self {
27            c1,
28            c2
29        }
30    }
31}
32
33impl<F, const D: usize> Shape<F, D> for HyperCube<F, D>
34where
35    F: Float + AddAssign
36{
37    fn raytrace(&self, ray: &Ray<F, D>) -> Raytrace<F, D>
38    {
39        HyperRectangle::from(*self).raytrace(ray)
40    }
41
42    fn raytrace_with_norm(&self, ray: &Ray<F, D>) -> RaytraceWithNorm<F, D>
43    {
44        HyperRectangle::from(*self).raytrace_with_norm(ray)
45    }
46}