diff options
Diffstat (limited to 'src/hittable/model.rs')
-rw-r--r-- | src/hittable/model.rs | 118 |
1 files changed, 61 insertions, 57 deletions
diff --git a/src/hittable/model.rs b/src/hittable/model.rs index 1a652be..6357ab1 100644 --- a/src/hittable/model.rs +++ b/src/hittable/model.rs @@ -1,7 +1,12 @@ use std::sync::Arc; use std::vec::Vec; -use crate::{hittable::{HitRecord, Hittable, AABB, HittableList, Triangle}, material::Material, ray::Ray, vec3::{Point3, Vec3}}; +use crate::{ + hittable::{HitRecord, Hittable, HittableList, Triangle, AABB}, + material::Material, + ray::Ray, + vec3::Vec3, +}; pub struct Model { faces: HittableList, @@ -10,7 +15,15 @@ pub struct Model { impl Model { fn parse_face_triplet(triplet: &str) -> Option<(isize, Option<isize>, Option<isize>)> { let mut triplet_iter = triplet.split("/"); - Some((triplet_iter.next()?.parse::<isize>().ok()?, triplet_iter.next().and_then(|val| val.parse::<isize>().ok()) , triplet_iter.next().and_then(|val| val.parse::<isize>().ok()))) + Some(( + triplet_iter.next()?.parse::<isize>().ok()?, + triplet_iter + .next() + .and_then(|val| val.parse::<isize>().ok()), + triplet_iter + .next() + .and_then(|val| val.parse::<isize>().ok()), + )) } pub fn from_obj(obj_data: &str, material: Arc<dyn Material>) -> Self { let mut geometric_vertices: Vec<Vec3> = Vec::new(); @@ -34,95 +47,85 @@ impl Model { None => { eprintln!("Malformed {} entry in OBJ: Missing x!", operator); continue; - }, - Some(val) => { - match val.parse::<f64>() { - Err(_) => { - eprintln!("Malformed {} entry in OBJ: Malformed f64 x!", operator); - continue; - }, - Ok(val) => val, - } } + Some(val) => match val.parse::<f64>() { + Err(_) => { + eprintln!("Malformed {} entry in OBJ: Malformed f64 x!", operator); + continue; + } + Ok(val) => val, + }, }; let y = match entry_iter.next() { None => { eprintln!("Malformed {} entry in OBJ: Missing y!", operator); continue; - }, - Some(val) => { - match val.parse::<f64>() { - Err(_) => { - eprintln!("Malformed {} entry in OBJ: Malformed f64 y!", operator); - continue; - }, - Ok(val) => val, - } } + Some(val) => match val.parse::<f64>() { + Err(_) => { + eprintln!("Malformed {} entry in OBJ: Malformed f64 y!", operator); + continue; + } + Ok(val) => val, + }, }; let z = match entry_iter.next() { None => { eprintln!("Malformed {} entry in OBJ: Missing z!", operator); continue; - }, - Some(val) => { - match val.parse::<f64>() { - Err(_) => { - eprintln!("Malformed {} entry in OBJ: Malformed f64 z!", operator); - continue; - }, - Ok(val) => val, - } } + Some(val) => match val.parse::<f64>() { + Err(_) => { + eprintln!("Malformed {} entry in OBJ: Malformed f64 z!", operator); + continue; + } + Ok(val) => val, + }, }; // who cares about w match operator { - "v" => geometric_vertices.push(Vec3 {x, y, z}), - "vn" => vertex_normals.push(Vec3 {x, y, z}), + "v" => geometric_vertices.push(Vec3 { x, y, z }), + "vn" => vertex_normals.push(Vec3 { x, y, z }), _ => panic!(), } - }, + } "vt" => { let u = match entry_iter.next() { None => { eprintln!("Malformed vt entry in OBJ: Missing u!"); continue; - }, - Some(val) => { - match val.parse::<f64>() { - Err(_) => { - eprintln!("Malformed vt entry in OBJ: Malformed f64 u!"); - continue; - }, - Ok(val) => val, - } } + Some(val) => match val.parse::<f64>() { + Err(_) => { + eprintln!("Malformed vt entry in OBJ: Malformed f64 u!"); + continue; + } + Ok(val) => val, + }, }; let v = match entry_iter.next() { None => { eprintln!("Malformed vt entry in OBJ: Missing v!"); continue; - }, - Some(val) => { - match val.parse::<f64>() { - Err(_) => { - eprintln!("Malformed v entry in OBJ: Malformed f64 v!"); - continue; - }, - Ok(val) => val, - } } + Some(val) => match val.parse::<f64>() { + Err(_) => { + eprintln!("Malformed v entry in OBJ: Malformed f64 v!"); + continue; + } + Ok(val) => val, + }, }; // who cares about w texture_vertices.push((u, v)); - }, + } "f" => { - let mut triplets : Vec<(isize, Option<isize>, Option<isize>)> = Vec::new(); + let mut triplets: Vec<(isize, Option<isize>, Option<isize>)> = Vec::new(); for triplet in entry_iter { match Self::parse_face_triplet(triplet) { None => { eprintln!("Encountered malformed triplet in f operator!"); - }, + } Some(val) => { triplets.push(val); } @@ -163,7 +166,7 @@ impl Model { if let Some(vn2) = triplets.get(2).unwrap().2 { if vn0 != vn1 || vn1 != vn2 { eprintln!("Unsupported geometry in OBJ file: Multiple normals for face!"); - continue + continue; } let mut vn0 = vn0; if vn0 < 0 { @@ -171,16 +174,17 @@ impl Model { } else { vn0 = vn0 - 1; } - triangle.custom_normal = Some(vertex_normals.get(vn0 as usize).unwrap().unit_vector()); + triangle.custom_normal = + Some(vertex_normals.get(vn0 as usize).unwrap().unit_vector()); } } } faces.push(Arc::new(triangle)); - }, + } _ => { eprintln!("Ignoring unknown operator {} in OBJ!", operator); continue; - }, + } } } Self { |