-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
194 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
use bvh::aabb::AABB; | ||
use nalgebra::{Matrix4, Point3, Vector3}; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct Triangle { | ||
pub a: Point3<f32>, | ||
pub b: Point3<f32>, | ||
pub c: Point3<f32>, | ||
pub a_normal: Vector3<f32>, | ||
pub b_normal: Vector3<f32>, | ||
pub c_normal: Vector3<f32>, | ||
pub material_index: usize, | ||
bh_node_index: usize, | ||
} | ||
|
||
impl Triangle { | ||
pub fn new( | ||
a: Point3<f32>, | ||
b: Point3<f32>, | ||
c: Point3<f32>, | ||
a_normal: Vector3<f32>, | ||
b_normal: Vector3<f32>, | ||
c_normal: Vector3<f32>, | ||
material_index: usize, | ||
) -> Self { | ||
Self { | ||
a, | ||
b, | ||
c, | ||
a_normal, | ||
b_normal, | ||
c_normal, | ||
material_index, | ||
bh_node_index: 0, | ||
} | ||
} | ||
|
||
pub fn transform(&mut self, matrix: &Matrix4<f32>) -> &mut Triangle { | ||
self.a = matrix.transform_point(&self.a); | ||
self.b = matrix.transform_point(&self.b); | ||
self.c = matrix.transform_point(&self.c); | ||
|
||
self.a_normal = matrix.transform_vector(&self.a_normal); | ||
self.b_normal = matrix.transform_vector(&self.b_normal); | ||
self.c_normal = matrix.transform_vector(&self.c_normal); | ||
|
||
self | ||
} | ||
|
||
pub fn intersect( | ||
&self, | ||
ray_dir: &Vector3<f32>, | ||
ray_origin: &Point3<f32>, | ||
) -> Option<Point3<f32>> { | ||
let edge1 = self.b - self.a; | ||
let edge2 = self.c - self.a; | ||
|
||
let h = ray_dir.cross(&edge2); | ||
let a = edge1.dot(&h); | ||
|
||
if a.abs() < f32::EPSILON { | ||
return None; | ||
} | ||
|
||
let f = 1.0 / a; | ||
|
||
let s = ray_origin - self.a; | ||
let u = f * s.dot(&h); | ||
|
||
if u < 0.0 || u > 1.0 { | ||
return None; | ||
} | ||
|
||
let q = s.cross(&edge1); | ||
let v = f * ray_dir.dot(&q); | ||
|
||
if v < 0.0 || u + v > 1.0 { | ||
return None; | ||
} | ||
|
||
let t = f * edge2.dot(&q); | ||
|
||
if t > f32::EPSILON { | ||
Some(ray_origin + ray_dir * t) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn barycentric(&self, point: Point3<f32>) -> (f32, f32, f32) { | ||
let v0 = self.b - self.a; | ||
let v1 = self.c - self.a; | ||
let v2 = point - self.a; | ||
|
||
let d00 = v0.dot(&v0); | ||
let d01 = v0.dot(&v1); | ||
let d11 = v1.dot(&v1); | ||
let d20 = v2.dot(&v0); | ||
let d21 = v2.dot(&v1); | ||
|
||
let denom = d00 * d11 - d01 * d01; | ||
|
||
let v = (d11 * d20 - d01 * d21) / denom; | ||
let w = (d00 * d21 - d01 * d20) / denom; | ||
let u = 1.0 - v - w; | ||
|
||
(u, v, w) | ||
} | ||
} | ||
|
||
impl bvh::aabb::Bounded for Triangle { | ||
fn aabb(&self) -> bvh::aabb::AABB { | ||
AABB::default() | ||
.grow(&bvh::Point3::new(self.a.x, self.a.y, self.a.z)) | ||
.grow(&bvh::Point3::new(self.b.x, self.b.y, self.b.z)) | ||
.grow(&bvh::Point3::new(self.c.x, self.c.y, self.c.z)) | ||
} | ||
} | ||
|
||
impl bvh::bounding_hierarchy::BHShape for Triangle { | ||
fn set_bh_node_index(&mut self, index: usize) { | ||
self.bh_node_index = index; | ||
} | ||
|
||
fn bh_node_index(&self) -> usize { | ||
self.bh_node_index | ||
} | ||
} | ||
|
||
impl PartialEq for Triangle { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.a == other.a | ||
&& self.b == other.b | ||
&& self.c == other.c | ||
&& self.a_normal == other.a_normal | ||
&& self.b_normal == other.b_normal | ||
&& self.c_normal == other.c_normal | ||
&& self.material_index == other.material_index | ||
&& self.bh_node_index == other.bh_node_index | ||
} | ||
} |