diff options
author | Julian T <julian@jtle.dk> | 2021-01-29 00:58:32 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-01-29 00:58:32 +0100 |
commit | fdb3e8cb8d53449c107388e143345beae162f95e (patch) | |
tree | 7a04102eb2722d2fddeefd0b2e1fa0e8a4e995f3 | |
parent | f467334b26f31b19ebbd222de2b4167b1538ccee (diff) |
Finish get_tile on Film
-rw-r--r-- | src/bound.rs | 42 | ||||
-rw-r--r-- | src/camera/film.rs | 44 | ||||
-rw-r--r-- | src/camera/filter/mod.rs | 4 | ||||
-rw-r--r-- | src/camera/mod.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 9 | ||||
-rw-r--r-- | src/main.rs | 15 | ||||
-rw-r--r-- | 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<T: Number> (a: T, b: T) -> T { } impl<T: Number> Bound2<T> { - fn new(p0: &Vector2<T>, p1: &Vector2<T>) -> Bound2<T> { - 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<T>, p1: &Vector2<T>) -> 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<T> { + 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<T> { 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<T: Number>(a: &Bound2<T>, b: &Bound2<T>) -> Bound2<T> { 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<i32> { 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<usize>, +pub struct Film { + pub resolution: Vector2i, + drawingBound: Bound2i, pixels: Vec<Pixel>, - filter: &'a Filter, + filter: Rc<Filter>, } pub struct FilmTile { - + pub bounds: Bound2i, + filter: Rc<Filter>, } impl Pixel { @@ -26,30 +31,37 @@ impl Pixel { } } -impl Film<'_> { - fn new(resolution: Vector2<usize>, filter: & Filter) -> Film { +impl Film { + pub fn new(resolution: Vector2i, filter: Rc<Filter>) -> 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<usize>, 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; @@ -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<Output = Self> + + Add<Output = Self> + Mul<Output = Self> {} 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<T: Number> { @@ -15,7 +15,7 @@ impl<T: Number> Vector2<T> { Vector2 { x: initial, y: initial } } - pub fn from_xy(x: T, y: T) -> Vector2<T> { + pub fn new_xy(x: T, y: T) -> Vector2<T> { Vector2 { x, y } } } @@ -23,10 +23,36 @@ impl<T: Number> Vector2<T> { impl<T: Number> Sub for Vector2<T> { 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<T: Number> Add for Vector2<T> { + 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); + } } |