diff --git a/src/surface/triangle.rs b/src/surface/triangle.rs index f19b6e2..1f785a4 100644 --- a/src/surface/triangle.rs +++ b/src/surface/triangle.rs @@ -128,6 +128,22 @@ pub fn intersect_ray(ray: &Ray, tri: (Point, Point, Point)) -> Option<(f32, f32, Some((t, b0, b1, b2)) } +/// Calculates a point on a triangle's surface at the given barycentric +/// coordinates. +/// +/// Returns the point and the error magnitude of the point. +pub fn surface_point(tri: (Point, Point, Point), bary: (f32, f32, f32)) -> (Point, f32) { + let pos = ((tri.0.into_vector() * bary.0) + (tri.1.into_vector() * bary.1) + + (tri.2.into_vector() * bary.2)) + .into_point(); + + let pos_err = (((tri.0.into_vector().abs() * bary.0) + (tri.1.into_vector().abs() * bary.1) + + (tri.2.into_vector().abs() * bary.2)) * fp_gamma(7)).co + .h_max(); + + (pos, pos_err) +} + fn max_abs_3(a: f32, b: f32, c: f32) -> f32 { let a = a.abs(); let b = b.abs(); diff --git a/src/surface/triangle_mesh.rs b/src/surface/triangle_mesh.rs index 3f46e60..29c8ca5 100644 --- a/src/surface/triangle_mesh.rs +++ b/src/surface/triangle_mesh.rs @@ -5,7 +5,6 @@ use mem_arena::MemArena; use accel::BVH4; use bbox::BBox; use boundable::Boundable; -use fp_utils::fp_gamma; use lerp::lerp_slice; use math::{Point, Normal, Matrix4x4, dot, cross}; use ray::{Ray, AccelRay}; @@ -193,15 +192,7 @@ impl<'a> Surface for TriangleMesh<'a> { r.mark_done(); } else { // Calculate intersection point and error magnitudes - let pos = ((tri.0.into_vector() * b0) + (tri.1.into_vector() * b1) + - (tri.2.into_vector() * b2)) - .into_point(); - - let pos_err = (((tri.0.into_vector().abs() * b0) + - (tri.1.into_vector().abs() * b1) + - (tri.2.into_vector().abs() * b2)) * - fp_gamma(7)).co - .h_max(); + let (pos, pos_err) = triangle::surface_point(tri, (b0, b1, b2)); // Calculate geometric surface normal let geo_normal = cross(tri.0 - tri.1, tri.0 - tri.2).into_normal();