rtiow: stub triangles shape created from STLs.

This commit is contained in:
2023-01-15 11:55:11 -08:00
parent 54e72cd81d
commit 39eeb79409
5 changed files with 172 additions and 6 deletions

View File

@@ -2,7 +2,7 @@ use std::fmt;
use crate::{ray::Ray, vec3::Vec3};
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct AABB {
bounds: [Vec3; 2],
}

View File

@@ -21,4 +21,5 @@ pub mod scenes;
pub mod sphere;
pub mod texture;
pub mod translate;
pub mod triangles;
pub mod vec3;

View File

@@ -0,0 +1,76 @@
use stl::STL;
use crate::{
aabb::AABB,
hitable::{Hit, HitRecord},
material::Material,
ray::Ray,
vec3::{dot, Vec3},
};
#[derive(Debug)]
pub struct Triangle {
pub normal: Vec3,
pub verts: [Vec3; 3],
}
#[derive(Debug)]
pub struct Triangles<M>
where
M: Material,
{
triangles: Vec<Triangle>,
bbox: Option<AABB>,
material: M,
}
impl<M> Triangles<M>
where
M: Material,
{
pub fn new<V>(stl: &STL, material: M) -> Triangles<M> {
let triangles: Vec<_> = stl
.triangles
.iter()
.map(|t| Triangle {
normal: t.normal,
verts: t.verts,
})
.collect();
let (min, max) = triangles.iter().fold(
(Vec3::from(f32::MAX), Vec3::from(f32::MIN)),
|(min, max), t| {
let t_min_x = t.verts[0].x.min(t.verts[1].x.min(t.verts[2].x));
let t_min_y = t.verts[0].y.min(t.verts[1].y.min(t.verts[2].y));
let t_min_z = t.verts[0].z.min(t.verts[1].z.min(t.verts[2].z));
let t_max_x = t.verts[0].x.max(t.verts[1].x.max(t.verts[2].x));
let t_max_y = t.verts[0].y.max(t.verts[1].y.max(t.verts[2].y));
let t_max_z = t.verts[0].z.max(t.verts[1].z.max(t.verts[2].z));
(
Vec3::from([min.x.min(t_min_x), min.y.min(t_min_y), min.z.min(t_min_z)]),
Vec3::from([max.x.max(t_max_x), max.y.max(t_max_y), max.z.max(t_max_z)]),
)
},
);
let bbox = Some(AABB::new(min, max));
Triangles {
triangles,
bbox,
material,
}
}
}
impl<M> Hit for Triangles<M>
where
M: Material,
{
fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord> {
None
}
fn bounding_box(&self, _t_min: f32, _t_max: f32) -> Option<AABB> {
self.bbox
}
}