Move load_obj() into submudule

This commit is contained in:
Matthew Gordon 2019-12-21 10:42:52 -05:00
parent 12e26887c1
commit 08484306fd
1 changed files with 82 additions and 70 deletions

View File

@ -1,19 +1,15 @@
use nalgebra::{convert, Point3, RealField, Vector2, Vector3}; use nalgebra::{Point3, RealField, Vector2, Vector3};
use obj::{IndexTuple, Obj, SimplePolygon};
use super::materials::Material; use super::materials::Material;
use super::raycasting::{Intersect, IntersectionInfo, Ray}; use super::raycasting::{Intersect, IntersectionInfo, Ray};
use alga::general::SupersetOf;
use std::io::Result;
use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
#[derive(Debug)] #[derive(Debug)]
pub struct Triangle<T: RealField> { pub struct Triangle<T: RealField> {
pub vertices: [Point3<T>; 3], pub vertices: [Point3<T>; 3],
pub normals: [Vector3<T>; 3], pub normals: [Vector3<T>; 3],
pub material: Arc<dyn Material<T> >, pub material: Arc<dyn Material<T>>,
} }
impl<T: RealField> Intersect<T> for Triangle<T> { impl<T: RealField> Intersect<T> for Triangle<T> {
@ -81,14 +77,27 @@ impl<T: RealField> Intersect<T> for Triangle<T> {
} }
} }
fn get_vertex_and_normal<T: RealField>( mod wavefront_obj {
use crate::materials::Material;
use super::Triangle;
use alga::general::SupersetOf;
use nalgebra::{convert, Point3, RealField, Vector3};
use obj::{IndexTuple, Obj, SimplePolygon};
use std::io::Result;
use std::path::Path;
use std::sync::Arc;
fn get_vertex_and_normal<T: RealField>(
index_tuple: &IndexTuple, index_tuple: &IndexTuple,
vertex_positions: &Vec<[f32; 3]>, vertex_positions: &Vec<[f32; 3]>,
normal_positions: &Vec<[f32; 3]>, normal_positions: &Vec<[f32; 3]>,
) -> (Point3<T>, Vector3<T>) ) -> (Point3<T>, Vector3<T>)
where where
T: SupersetOf<f32>, T: SupersetOf<f32>,
{ {
let &IndexTuple(vertex_index, _, maybe_normal_index) = index_tuple; let &IndexTuple(vertex_index, _, maybe_normal_index) = index_tuple;
let vertex: Point3<T> = convert(Point3::from_slice(&vertex_positions[vertex_index])); let vertex: Point3<T> = convert(Point3::from_slice(&vertex_positions[vertex_index]));
let normal = match maybe_normal_index { let normal = match maybe_normal_index {
@ -96,17 +105,17 @@ where
None => Vector3::zeros(), None => Vector3::zeros(),
}; };
(vertex, normal) (vertex, normal)
} }
fn get_triangles<T: RealField>( fn get_triangles<T: RealField>(
polygon: &SimplePolygon, polygon: &SimplePolygon,
vertex_positions: &Vec<[f32; 3]>, vertex_positions: &Vec<[f32; 3]>,
normal_positions: &Vec<[f32; 3]>, normal_positions: &Vec<[f32; 3]>,
material: Arc<dyn Material<T>>, material: Arc<dyn Material<T>>,
) -> Vec<Triangle<T>> ) -> Vec<Triangle<T>>
where where
T: SupersetOf<f32>, T: SupersetOf<f32>,
{ {
if let Some(v0_index) = polygon.iter().next() { if let Some(v0_index) = polygon.iter().next() {
let (v0_vertex, v0_normal) = let (v0_vertex, v0_normal) =
get_vertex_and_normal(v0_index, &vertex_positions, &normal_positions); get_vertex_and_normal(v0_index, &vertex_positions, &normal_positions);
@ -131,15 +140,15 @@ where
} else { } else {
vec![] vec![]
} }
} }
pub fn load_obj<T: RealField>( pub fn load_obj<T: RealField>(
filename: &Path, filename: &Path,
material: Arc<dyn Material<T>>, material: Arc<dyn Material<T>>,
) -> Result<Vec<Triangle<T>>> ) -> Result<Vec<Triangle<T>>>
where where
T: SupersetOf<f32>, T: SupersetOf<f32>,
{ {
let obj = Obj::<SimplePolygon>::load(filename)?; let obj = Obj::<SimplePolygon>::load(filename)?;
Ok(obj Ok(obj
@ -149,8 +158,11 @@ where
.flat_map(|group| group.polys.iter()) .flat_map(|group| group.polys.iter())
.flat_map(|poly| get_triangles(poly, &obj.position, &obj.normal, material.clone())) .flat_map(|poly| get_triangles(poly, &obj.position, &obj.normal, material.clone()))
.collect()) .collect())
}
} }
pub use wavefront_obj::load_obj;
fn indices_with_index_of_largest_element_last<T: RealField>(v: &Vector3<T>) -> [usize; 3] { fn indices_with_index_of_largest_element_last<T: RealField>(v: &Vector3<T>) -> [usize; 3] {
if v.x > v.y { if v.x > v.y {
if v.z > v.x { if v.z > v.x {