Add Primitive trait

This commit is contained in:
Matthew Gordon 2020-01-30 17:01:23 -05:00
parent a0de9c18ba
commit 8c527d34fc
6 changed files with 22 additions and 14 deletions

View File

@ -19,7 +19,7 @@ use vanrijn::colour::{ColourRgbF, NamedColour};
use vanrijn::image::{ClampingToneMapper, ImageRgbF, ImageRgbU8, ToneMapper};
use vanrijn::materials::{LambertianMaterial, PhongMaterial, ReflectiveMaterial};
use vanrijn::mesh::load_obj;
use vanrijn::raycasting::{Intersect, Plane, Sphere};
use vanrijn::raycasting::{Primitive, Plane, Sphere};
use vanrijn::scene::Scene;
fn update_texture(image: &ImageRgbU8, texture: &mut Texture) {
@ -75,17 +75,17 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
)
.unwrap()
.into_iter()
.map(|triangle| Box::new(triangle) as Box<dyn Intersect<f64>>)
.map(|triangle| Arc::new(triangle) as Arc<dyn Primitive<f64>>)
.chain(vec![
Box::new(Plane::new(
Arc::new(Plane::new(
Vector3::new(0.0, 1.0, 0.0),
-2.0,
Arc::new(LambertianMaterial {
colour: ColourRgbF::new(0.55, 0.27, 0.04),
diffuse_strength: 0.1,
}),
)) as Box<dyn Intersect<f64>>,
Box::new(Sphere::new(
)) as Arc<dyn Primitive<f64>>,
Arc::new(Sphere::new(
Point3::new(-6.25, -0.5, 1.0),
1.0,
Arc::new(LambertianMaterial {
@ -93,7 +93,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
diffuse_strength: 0.1,
}),
)),
Box::new(Sphere::new(
Arc::new(Sphere::new(
Point3::new(-4.25, -0.5, 2.0),
1.0,
Arc::new(ReflectiveMaterial {
@ -102,7 +102,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
reflection_strength: 0.99,
}),
)),
Box::new(Sphere::new(
Arc::new(Sphere::new(
Point3::new(-5.0, 1.5, 1.0),
1.0,
Arc::new(PhongMaterial {

View File

@ -64,6 +64,8 @@ pub trait HasBoundingBox<T: RealField>: Send + Sync {
fn bounding_box(&self) -> BoundingBox<T>;
}
pub trait Primitive<T: RealField>: Intersect<T> + HasBoundingBox<T> {}
#[cfg(test)]
mod tests {
use quickcheck_macros::quickcheck;

View File

@ -2,7 +2,7 @@ use nalgebra::{convert, Point3, RealField, Vector3};
use crate::materials::Material;
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Ray};
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Primitive, Ray};
use std::sync::Arc;
@ -87,6 +87,8 @@ impl<T: RealField> HasBoundingBox<T> for Plane<T> {
}
}
impl<T: RealField> Primitive<T> for Plane<T> {}
#[cfg(test)]
mod tests {
use nalgebra::Point3;
@ -274,6 +276,4 @@ mod tests {
assert!(bb.contains_point(Point3::new(-2000.0, 2.0, 2.0)));
assert!(bb.contains_point(Point3::new(3.0, 2.0, 3.0)));
}
}

View File

@ -2,7 +2,7 @@ use nalgebra::{convert, Point3, RealField, Vector3};
use crate::materials::Material;
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Ray};
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Primitive, Ray};
use std::sync::Arc;
@ -84,6 +84,8 @@ impl<T: RealField> HasBoundingBox<T> for Sphere<T> {
}
}
impl<T: RealField> Primitive<T> for Sphere<T> {}
#[cfg(test)]
mod tests {
use quickcheck::TestResult;

View File

@ -1,6 +1,6 @@
use crate::materials::Material;
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Ray};
use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Primitive, Ray};
use nalgebra::{Point3, RealField, Vector2, Vector3};
use std::sync::Arc;
@ -83,6 +83,8 @@ impl<T: RealField> HasBoundingBox<T> for Triangle<T> {
}
}
impl<T: RealField> Primitive<T> for Triangle<T> {}
fn indices_with_index_of_largest_element_last<T: RealField>(v: &Vector3<T>) -> [usize; 3] {
if v.x > v.y {
if v.z > v.x {

View File

@ -1,8 +1,10 @@
use nalgebra::{Point3, RealField};
use crate::raycasting::Intersect;
use crate::raycasting::Primitive;
use std::sync::Arc;
pub struct Scene<T: RealField> {
pub camera_location: Point3<T>,
pub objects: Vec<Box<dyn Intersect<T>>>,
pub objects: Vec<Arc<dyn Primitive<T>>>,
}