From 0b099a362a814866a7c2b449f37fd8607745dcde Mon Sep 17 00:00:00 2001 From: NickyMateev Date: Wed, 21 Nov 2018 13:30:05 +0200 Subject: [PATCH] Tweaks --- Homework3/solution.go | 6 +++--- Homework3/solution_test.go | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Homework3/solution.go b/Homework3/solution.go index 8236bae..d326956 100644 --- a/Homework3/solution.go +++ b/Homework3/solution.go @@ -5,7 +5,7 @@ import ( "sync" ) -const Episilon = 1e-7 +const epsilon = 1e-7 type Triangle struct { a, b, c geom.Vector @@ -53,7 +53,7 @@ func (triangle Triangle) Intersect(ray geom.Ray) bool { h := geom.Cross(ray.Direction, edge2) det := geom.Dot(edge1, h) - if det > -Episilon && det < Episilon { + if det > -epsilon && det < epsilon { return false // The ray is parallel to triangle plane, impossible that they intersect } @@ -78,7 +78,7 @@ func (triangle Triangle) Intersect(ray geom.Ray) bool { // Calculating t - final check to see if ray intersects triangle t := f * geom.Dot(edge2, q) - if t > Episilon { + if t > epsilon { return true } diff --git a/Homework3/solution_test.go b/Homework3/solution_test.go index bffefc4..9ff28d1 100644 --- a/Homework3/solution_test.go +++ b/Homework3/solution_test.go @@ -18,7 +18,19 @@ func TestSampleSimpleTriangle(t *testing.T) { } } -func TestSampleSimpleSphere(t *testing.T) { +func TestSampleSimpleTriangleBoundaryIntersectionShouldIntersect(t *testing.T) { + var prim geom.Intersectable + + a, b, c := geom.NewVector(0, -2, 0), geom.NewVector(3, 0, 0), geom.NewVector(0, 1, 0) + prim = NewTriangle(a, b, c) + ray := geom.NewRay(geom.NewVector(0, 0, 3), geom.NewVector(0, 0, -2)) + + if !prim.Intersect(ray) { + t.Errorf("Expected ray %#v to intersect triangle %#v but it did not.", ray, prim) + } +} + +func TestSampleSimpleSphereShouldIntersect(t *testing.T) { var prim geom.Intersectable origin, r := geom.NewVector(0, 0, 0), 2.0 @@ -30,10 +42,10 @@ func TestSampleSimpleSphere(t *testing.T) { } } -func TestSampleSimpleQuadConvex(t *testing.T) { +func TestSampleSimpleQuadConvexShouldIntersect(t *testing.T) { var prim geom.Intersectable - a, b, c, d := geom.NewVector(0, -2, 0), geom.NewVector(3, 0, 0), geom.NewVector(0, 1, 0), geom.Vector{-1, 0, 0} + a, b, c, d := geom.NewVector(0, -2, 0), geom.NewVector(3, 0, 0), geom.NewVector(0, 1, 0), geom.NewVector(-1, 0, 0) prim = NewQuad(a, b, c, d) ray := geom.NewRay(geom.NewVector(0, 0, 2), geom.NewVector(0, 0, -1)) @@ -45,12 +57,12 @@ func TestSampleSimpleQuadConvex(t *testing.T) { func TestSampleSimpleQuadConcaveShouldNotIntersect(t *testing.T) { var prim geom.Intersectable - a, b, c, d := geom.NewVector(0, -2, 0), geom.NewVector(3, 0, 0), geom.NewVector(0, 1, 0), geom.Vector{1, 0, 0} + a, b, c, d := geom.NewVector(0, -2, 0), geom.NewVector(3, 0, 0), geom.NewVector(0, 1, 0), geom.NewVector(1, 0, 0) prim = NewQuad(a, b, c, d) ray := geom.NewRay(geom.NewVector(0, 0, 2), geom.NewVector(0, 0, -1)) if prim.Intersect(ray) { - t.Errorf("Expected ray %#v to intersect quad %#v but it did not.", ray, prim) + t.Errorf("Expected ray %#v to not intersect quad %#v but it did.", ray, prim) } }