Skip to content

Commit

Permalink
Matix Collision Math!
Browse files Browse the repository at this point in the history
  • Loading branch information
IGBC committed May 19, 2019
1 parent 27ee8bb commit 2ad0c89
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 69 deletions.
10 changes: 1 addition & 9 deletions examples/dawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,8 @@ fn main() {
wavelength: Sample::Blackbody(4500.0),
};

let viewport = Rect::from_points(
&Point { x: 0.0, y: 0.0 },
&Point {
x: width,
y: height,
},
);

println!("Tracing Rays!");
let r = Scene::new(width as usize, height as usize, viewport)
let r = Scene::new(width as usize, height as usize)
.with_object(o)
.with_light(l);
let image = r.render(rays);
Expand Down
10 changes: 1 addition & 9 deletions examples/laser-rainbow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,8 @@ fn main() {
material: Box::new(floor_m.clone()),
};

let viewport = Rect::from_points(
&Point { x: 0.0, y: 0.0 },
&Point {
x: width + 1.0,
y: height + 1.0,
},
);

println!("Tracing Rays!");
let r = Scene::new(width as usize, height as usize, viewport)
let r = Scene::new(width as usize, height as usize)
.with_light(laser(30.0, 694.0))
.with_light(laser(31.0, 676.0))
.with_light(laser(32.0, 647.0))
Expand Down
7 changes: 7 additions & 0 deletions rustic.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"folders": [
{
"path": "."
}
]
}
92 changes: 91 additions & 1 deletion src/geom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(missing_docs)]

use std::ops::{Add, Neg, Sub};
use std::ops::{Add, Neg, Sub, Mul};
use std::option::Option;

#[derive(PartialOrd, PartialEq, Copy, Clone, Debug)]
pub struct Point {
Expand All @@ -14,6 +15,13 @@ pub struct Vector {
pub y: f64,
}

#[derive(Copy, Clone, Debug)]
pub struct Matrix {
pub a1: f64, pub b1: f64,
pub a2: f64, pub b2: f64,
}


#[derive(PartialOrd, PartialEq, Copy, Clone, Debug)]
pub struct Rect {
pub top_left: Point,
Expand All @@ -30,6 +38,88 @@ impl Neg for Vector {
}
}

impl Sub<Matrix> for Matrix {
type Output = Matrix;
fn sub(self, rhs: Matrix) -> Matrix {
Matrix {
a1: self.a1 - rhs.a1,
a2: self.a2 - rhs.a2,
b1: self.b1 - rhs.b1,
b2: self.b2 - rhs.b2,
}
}
}

impl Add<Matrix> for Matrix {
type Output = Matrix;
fn add(self, rhs: Matrix) -> Matrix {
Matrix {
a1: self.a1 + rhs.a1,
a2: self.a2 + rhs.a2,
b1: self.b1 + rhs.b1,
b2: self.b2 + rhs.b2,
}
}
}

impl Mul<Matrix> for Matrix {
type Output = Matrix;
fn mul(self, rhs: Matrix) -> Matrix {
Matrix {
a1: (self.a1 * rhs.a1) + (self.b1 * rhs.a2),
a2: (self.a2 * rhs.a1) + (self.b2 * rhs.a2),
b1: (self.a1 * rhs.b1) + (self.b1 * rhs.b2),
b2: (self.a2 * rhs.b1) + (self.b2 * rhs.b2),
}
}
}

impl Mul<f64> for Matrix {
type Output = Matrix;
fn mul(self, rhs: f64) -> Matrix {
Matrix {
a1: self.a1 * rhs,
a2: self.a2 * rhs,
b1: self.b1 * rhs,
b2: self.b2 * rhs,
}
}
}

impl Mul<Point> for Matrix {
type Output = Point;
fn mul(self, rhs: Point) -> Point {
Point {
x: (self.a1 * rhs.x) + (self.b1 * rhs.y),
y: (self.a2 * rhs.x) + (self.b2 * rhs.y),
}
}
}

impl Mul<Vector> for Matrix {
type Output = Vector;
fn mul(self, rhs: Vector) -> Vector {
Vector {
x: (self.a1 * rhs.x) + (self.b1 * rhs.y),
y: (self.a2 * rhs.x) + (self.b2 * rhs.y),
}
}
}

impl Matrix {
pub fn inverse(self) -> Option<Matrix> {
let det = (self.a1 * self.b2) - (self.b1 * self.a2);
if det == 0.0 {
None
} else {
Some(Matrix {
a1: self.b2, b1: -self.b1,
a2: -self.a2, b2: self.a1,
} * (1.0/det))
}
}
}

impl Sub<Vector> for Point {
type Output = Point;
fn sub(self, rhs: Vector) -> Point {
Expand Down
33 changes: 4 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mod scene;

/// This prelude contains everything to quickstart using Rustic Zen.
pub mod prelude {
pub use geom::{Point, Rect};
pub use geom::{Point};
pub use material::{HQZLegacy, Material};
pub use object::Object;
pub use sampler::Sample;
Expand Down Expand Up @@ -115,8 +115,6 @@ mod tests {
use scene::Light;
use scene::Scene;

use geom::{Point, Rect};

use material::HQZLegacy;

#[test]
Expand All @@ -134,14 +132,7 @@ mod tests {
wavelength: Sample::Blackbody(6900.0),
};

let viewport = Rect::from_points(
&Point { x: 0.0, y: 0.0 },
&Point {
x: width,
y: height,
},
);
let r = Scene::new(width as usize, height as usize, viewport).with_light(l);
let r = Scene::new(width as usize, height as usize).with_light(l);
let image = r.render(10_000);

let data = image.to_rgb8(0.5, 0.5);
Expand Down Expand Up @@ -183,15 +174,7 @@ mod tests {
wavelength: Sample::Blackbody(6900.0),
};

let viewport = Rect::from_points(
&Point { x: 0.0, y: 0.0 },
&Point {
x: width,
y: height,
},
);

let r = Scene::new(width as usize, height as usize, viewport)
let r = Scene::new(width as usize, height as usize)
.with_light(l)
.with_object(o);
let image = r.render(rays);
Expand Down Expand Up @@ -235,15 +218,7 @@ mod tests {
wavelength: Sample::Blackbody(6900.0),
};

let viewport = Rect::from_points(
&Point { x: 0.0, y: 0.0 },
&Point {
x: width,
y: height,
},
);

let r = Scene::new(width as usize, height as usize, viewport)
let r = Scene::new(width as usize, height as usize)
.with_light(l)
.with_object(o);
let image = r.render(rays);
Expand Down
42 changes: 28 additions & 14 deletions src/ray.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use geom::{Point, Rect, Vector};
use geom::{Point, Rect, Vector, Matrix};
use image::Image;
use object::Object;
use prng::PRNG;
Expand Down Expand Up @@ -128,18 +128,33 @@ impl Ray {
}

fn intersect_edge(&self, s1: Point, sd: Point) -> Option<f64> {
let slope = self.direction.y / self.direction.x;
let alpha =
((s1.x - self.origin.x) * slope + (self.origin.y - s1.y)) / (sd.y - sd.x * slope);
if alpha < 0.0 || alpha > 1.0 {
return None;
}
let mat_a = Matrix {
a1: sd.x - s1.x, b1: self.origin.x - self.direction.x,
a2: sd.y - s1.y, b2: self.origin.y - self.direction.y,
};

let distance = (s1.x + sd.x * alpha - self.origin.x) / self.direction.x;
if distance < 0.0 {
return None;
let omega = self.origin - s1;

let result = mat_a.inverse().unwrap() * omega;
if (result.x >= 0.0) && (result.x <= 1.0) && (result.y > 0.0) {
Some(result.y)
} else {
None
}
return Some(distance);


// let slope = self.direction.y / self.direction.x;
// let alpha =
// ((s1.x - self.origin.x) * slope + (self.origin.y - s1.y)) / (sd.y - sd.x * slope);
// if alpha < 0.0 || alpha > 1.0 {
// return None;
// }

// let distance = (s1.x + sd.x * alpha - self.origin.x) / self.direction.x;
// if distance < 0.0 {
// return None;
// }
// return Some(distance);
}

pub fn furthest_aabb(&self, aabb: Rect) -> Option<Point> {
Expand Down Expand Up @@ -294,7 +309,6 @@ mod test {
}

#[test]
#[ignore]
fn furthest_aabb_hits_vertical() {
let mut rng = PRNG::seed(0);

Expand Down Expand Up @@ -323,8 +337,8 @@ mod test {
assert!(result.is_some());
let result = result.unwrap();
println!("result: ({},{})", result.x, result.y);
assert_eq!(result.x, 0.0);
assert_eq!(result.y, 11.0);
assert_eq!(result.x.round(), 0.0);
assert_eq!(result.y.round(), 11.0);
}

#[test]
Expand Down
11 changes: 4 additions & 7 deletions src/scene.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use geom::Rect;
use geom::{Point, Rect};
use image::Image;
use object::Object;
use prng::PRNG;
Expand Down Expand Up @@ -53,12 +53,12 @@ pub struct Scene {

impl Scene {
/// Creates new Renderer ready for defining a scene.
pub fn new(resolution_x: usize, resolution_y: usize, viewport: Rect) -> Self {
pub fn new(resolution_x: usize, resolution_y: usize) -> Self {
Self {
seed: 0,
lights: vec![],
objects: vec![],
viewport,
viewport: Rect::from_points(&Point{ x: 0.0, y: 0.0 }, &Point { x: resolution_x as f64, y: resolution_y as f64 }),
resolution_x,
resolution_y,
total_light_power: 0.0,
Expand Down Expand Up @@ -126,7 +126,6 @@ impl Scene {
#[cfg(test)]
mod tests {
use super::Scene;
use geom::{Point, Rect};
use material::HQZLegacy;
use object::Object;
use sampler::Sample;
Expand Down Expand Up @@ -154,9 +153,7 @@ mod tests {
wavelength: Sample::Blackbody(5800.0),
};

let viewport = Rect::from_points(&Point { x: 0.0, y: 0.0 }, &Point { x: 160.0, y: 90.0 });

let r = Scene::new(1920, 1080, viewport)
let r = Scene::new(1920, 1080)
.with_light(l)
.with_object(obj);
r.render(100);
Expand Down

0 comments on commit 2ad0c89

Please sign in to comment.