From fdb3e8cb8d53449c107388e143345beae162f95e Mon Sep 17 00:00:00 2001 From: Julian T Date: Fri, 29 Jan 2021 00:58:32 +0100 Subject: Finish get_tile on Film --- src/bound.rs | 42 +++++++++++++++++++++++++++++--------- src/camera/film.rs | 44 +++++++++++++++++++++++++--------------- src/camera/filter/mod.rs | 4 ++-- src/camera/mod.rs | 4 ++-- src/lib.rs | 9 ++++---- src/main.rs | 15 +++++++++++++- src/vector.rs | 53 ++++++++++++++++++++++++++++++++++++++++-------- 7 files changed, 127 insertions(+), 44 deletions(-) diff --git a/src/bound.rs b/src/bound.rs index b7ce05d..ab0e703 100644 --- a/src/bound.rs +++ b/src/bound.rs @@ -24,18 +24,25 @@ fn max (a: T, b: T) -> T { } impl Bound2 { - fn new(p0: &Vector2, p1: &Vector2) -> Bound2 { - let min = Vector2::from_xy(min(p0.x, p1.x), min(p0.y, p1.y)); - let max = Vector2::from_xy(max(p0.x, p1.x), max(p0.y, p1.y)); + pub fn new(p0: &Vector2, p1: &Vector2) -> Self { + let min = Vector2::new_xy(min(p0.x, p1.x), min(p0.y, p1.y)); + let max = Vector2::new_xy(max(p0.x, p1.x), max(p0.y, p1.y)); - Bound2 { min, max } + Self { min, max } } - fn diagonal(&self) -> Vector2 { + pub fn new_xyxy(x1: T, y1: T, x2: T, y2: T) -> Self { + Self::new( + &Vector2::new_xy(x1, y1), + &Vector2::new_xy(x2, y2), + ) + } + + pub fn diagonal(&self) -> Vector2 { self.max - self.min } - fn area(&self) -> T { + pub fn area(&self) -> T { let diag = self.diagonal(); return diag.x * diag.y; } @@ -61,8 +68,8 @@ impl From<&Bound2f> for Bound2i { pub fn intersect(a: &Bound2, b: &Bound2) -> Bound2 { Bound2::new( - &Vector2::from_xy(max(a.min.x, b.min.x), max(a.min.y, b.min.y)), - &Vector2::from_xy(min(a.max.x, b.max.x), min(a.max.y, b.max.y)), + &Vector2::new_xy(max(a.min.x, b.min.x), max(a.min.y, b.min.y)), + &Vector2::new_xy(min(a.max.x, b.max.x), min(a.max.y, b.max.y)), ) } @@ -72,8 +79,8 @@ mod tests { fn create_test() -> Bound2 { Bound2::new( - &Vector2::from_xy(1, 2), - &Vector2::from_xy(10, 3) + &Vector2::new_xy(1, 2), + &Vector2::new_xy(10, 3) ) } @@ -83,4 +90,19 @@ mod tests { assert!(b.area() == 9); } + + #[test] + fn intersect_test() { + let b1 = Bound2i::new_xyxy(10, 10, 20, 20); + let b2 = Bound2i::new_xyxy(2, 11, 22, 17); + + let b = intersect(&b1, &b2); + + assert!( + b.min.x == 10 && + b.min.y == 11 && + b.max.x == 20 && + b.max.y == 17 + ) + } } diff --git a/src/camera/film.rs b/src/camera/film.rs index e31b760..6661529 100644 --- a/src/camera/film.rs +++ b/src/camera/film.rs @@ -1,23 +1,28 @@ -use crate::vector::{Vector2, Vector2f}; +use crate::vector::*; use crate::Float; use crate::bound; +use crate::spectrum::Spectrum; + use bound::{Bound2i, Bound2f}; use super::filter::Filter; +use std::rc::Rc; #[derive(Clone)] pub struct Pixel { pub rgb: [Float; 3], } -pub struct Film<'a> { - pub resolution: Vector2, +pub struct Film { + pub resolution: Vector2i, + drawingBound: Bound2i, pixels: Vec, - filter: &'a Filter, + filter: Rc, } pub struct FilmTile { - + pub bounds: Bound2i, + filter: Rc, } impl Pixel { @@ -26,30 +31,37 @@ impl Pixel { } } -impl Film<'_> { - fn new(resolution: Vector2, filter: & Filter) -> Film { +impl Film { + pub fn new(resolution: Vector2i, filter: Rc) -> Film { let area = resolution.x * resolution.y; Film { resolution, - pixels: vec![Pixel::new(); area], + drawingBound: Bound2i::new(&Vector2::new(0), &resolution), + pixels: vec![Pixel::new(); area as usize], filter, } } - fn get_tile(bound: &Bound2i) { + pub fn get_tile(&self, bound: &Bound2i) -> FilmTile { // Used to calculate descrete coordinates into continues - let halfpixel = Vector2f::from_xy(0.5, 0.5); + let halfpixel = Vector2f::new_xy(0.5, 0.5); let fbound = Bound2f::from(bound); + let p0 = Vector2i::from((fbound.min - halfpixel - self.filter.radius).ceil()); + let p1 = Vector2i::from((fbound.min - halfpixel + self.filter.radius).floor()); + let tilebound = bound::intersect(&Bound2i::new(&p0, &p1), &self.drawingBound); - //let tilebound = bound::intersect(bound, - } - - fn splat(&mut self, point: &Vector2, pixel: &Pixel) { - let index = point.x + point.y * self.resolution.x; + FilmTile { + bounds: tilebound, + filter: self.filter.clone(), + } - self.pixels[index] = pixel.clone(); } } +impl FilmTile { + fn add_sample(point: &Vector2f, c: Spectrum) { + + } +} diff --git a/src/camera/filter/mod.rs b/src/camera/filter/mod.rs index bfa17bb..01d094c 100644 --- a/src/camera/filter/mod.rs +++ b/src/camera/filter/mod.rs @@ -26,7 +26,7 @@ impl Eval for Filter { } impl Filter { - fn new_box(radius: &Vector2f) -> Filter { - Filter { radius: radius.clone(), eval: Box::new(BoxFilter {}) } + pub fn new_box(radius: Vector2f) -> Filter { + Filter { radius: radius, eval: Box::new(BoxFilter {}) } } } diff --git a/src/camera/mod.rs b/src/camera/mod.rs index bba95f7..8aed4bf 100644 --- a/src/camera/mod.rs +++ b/src/camera/mod.rs @@ -1,2 +1,2 @@ -mod film; -mod filter; +pub mod film; +pub mod filter; diff --git a/src/lib.rs b/src/lib.rs index 7066a80..96c9212 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,15 +1,16 @@ -mod vector; -mod bound; -mod camera; +pub mod vector; +pub mod bound; +pub mod camera; mod spectrum; -use std::ops::{Sub, Mul}; +use std::ops::{Add, Sub, Mul}; use std::cmp; pub trait Number: Copy + cmp::PartialOrd + Sub + + Add + Mul {} diff --git a/src/main.rs b/src/main.rs index e7a11a9..0ac71df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,16 @@ +use pathtrace::camera::filter::Filter; +use pathtrace::camera::film::Film; +use pathtrace::vector::{Vector2i, Vector2f}; +use pathtrace::bound::Bound2i; + +use std::rc::Rc; + fn main() { - println!("Hello, world!"); + + let filter = Rc::new(Filter::new_box(Vector2f::new(2.5))); + + let film = Film::new(Vector2i::new(100), filter.clone()); + let tile = film.get_tile(&Bound2i::new_xyxy(10, 10, 100, 100)); + + println!("Yo {}", tile.bounds.min.x); } diff --git a/src/vector.rs b/src/vector.rs index ff443f5..ac70947 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -1,5 +1,5 @@ use crate::{Float, Number}; -use std::ops::{Sub}; +use std::ops::{Sub, Add}; #[derive(Clone, Copy)] pub struct Vector2 { @@ -15,7 +15,7 @@ impl Vector2 { Vector2 { x: initial, y: initial } } - pub fn from_xy(x: T, y: T) -> Vector2 { + pub fn new_xy(x: T, y: T) -> Vector2 { Vector2 { x, y } } } @@ -23,10 +23,36 @@ impl Vector2 { impl Sub for Vector2 { type Output = Self; fn sub(self, op: Self) -> Self::Output { - Vector2 { - x: self.x - op.x, - y: self.y - op.y, - } + Self::new_xy( + self.x - op.x, + self.y - op.y, + ) + } +} + +impl Add for Vector2 { + type Output = Self; + fn add(self, op: Self) -> Self::Output { + Self::new_xy( + self.x + op.x, + self.y + op.y, + ) + } +} + +impl Vector2f { + pub fn ceil(&self) -> Self { + Self::new_xy( + self.x.ceil(), + self.y.ceil() + ) + } + + pub fn floor(&self) -> Self { + Self::new_xy( + self.x.ceil(), + self.y.ceil() + ) } } @@ -55,7 +81,7 @@ mod tests { #[test] fn new_vec2() { - let v = Vector2::from_xy(2.0, 10.0); + let v = Vector2::new_xy(2.0, 10.0); assert!(v.x == 2.0 && v.y == 10.0); @@ -66,10 +92,19 @@ mod tests { #[test] fn sub_vec2() { - let v1 = Vector2::from_xy(10, 11); - let v2 = Vector2::from_xy(2, 3); + let v1 = Vector2::new_xy(10, 11); + let v2 = Vector2::new_xy(2, 3); let v3 = v1-v2; assert!(v3.x == 8 && v3.y == 8); } + + #[test] + fn add_vec2() { + let v1 = Vector2::new_xy(10, 11); + let v2 = Vector2::new_xy(2, 3); + + let v3 = v1+v2; + assert!(v3.x == 12 && v3.y == 14); + } } -- cgit v1.2.3