def test_a_ray_strikes_a_cylinder1(self): cyl = Cylinder() direction = Vector(0, 0, 1).normalize() r = Ray(Point(1, 0, -5), direction) xs = cyl.local_intersect(r) self.assertEqual(2, xs.count) self.assertTrue(equals(5.0, xs[0].t)) self.assertTrue(equals(5.0, xs[1].t))
def test_a_ray_strikes_a_cylinder3(self): cyl = Cylinder() direction = Vector(0.1, 1, 1).normalize() r = Ray(Point(0.5, 0, -5), direction) xs = cyl.local_intersect(r) self.assertEqual(2, xs.count) self.assertTrue(equals(6.80798, xs[0].t)) self.assertTrue(equals(7.08872, xs[1].t))
def test_ray_misses_cylinder(self): cyl = Cylinder() rays = [ Ray(Point(1, 0, 0), Vector(0, 1, 0)), Ray(Point(0, 0, 0), Vector(0, 1, 0)), Ray(Point(0, 0, -5), Vector(1, 1, 1)) ] for ray in rays: direction = Vector.normalize(ray.direction) r = Ray(ray.origin, direction) xs = cyl.local_intersect(r) self.assertEqual(len(xs), 0)
def test_intersect_constrained_cylinder(self): cyl = Cylinder() cyl.minimum = 1 cyl.maximum = 2 CylinderIntersection = namedtuple("CyliderIntersection", ["point", "direction", "count"]) cylinder_intersections = [ CylinderIntersection(Point(0, 1.5, 0), Vector(0.1, 1, 0), 0), CylinderIntersection(Point(0, 3, -5), Vector(0, 0, 1), 0), CylinderIntersection(Point(0, 0, -5), Vector(0, 0, 1), 0), CylinderIntersection(Point(0, 2, -5), Vector(0, 0, 1), 0), CylinderIntersection(Point(0, 1, -5), Vector(0, 0, 1), 0), CylinderIntersection(Point(0, 1.5, -2), Vector(0, 0, 1), 2) ] for cylinder_intersection in cylinder_intersections: direction = Vector.normalize(cylinder_intersection.direction) r = Ray(cylinder_intersection.point, direction) xs = cyl.local_intersect(r) self.assertEqual(len(xs), cylinder_intersection.count)
def test_ray_strikes_cylinder(self): cyl = Cylinder() RayIntersection = namedtuple("RayIntersection", ["origin", "direction", "t0", "t1"]) ray_intersections = [ RayIntersection(Point(1, 0, -5), Vector(0, 0, 1), 5, 5), RayIntersection(Point(0, 0, -5), Vector(0, 0, 1), 4, 6), RayIntersection(Point(0.5, 0, -5), Vector(0.1, 1, 1), 6.80798, 7.08872) ] for ray_intersection in ray_intersections: direction = Vector.normalize(ray_intersection.direction) r = Ray(ray_intersection.origin, direction) xs = cyl.local_intersect(r) self.assertEqual(len(xs), 2) self.assertAlmostEqual(xs[0].t, ray_intersection.t0, delta=Constants.epsilon) self.assertAlmostEqual(xs[1].t, ray_intersection.t1, delta=Constants.epsilon)
def test_interserct_caps_of_closed_cylinder(self): cyl = Cylinder() cyl.minimum = 1 cyl.maximum = 2 cyl.closed = True cyl.maximum = 2 CylinderIntersection = namedtuple("CyliderIntersection", ["point", "direction", "count"]) cylinder_intersections = [ CylinderIntersection(Point(0, 3, 0), Vector(0, -1, 0), 2), CylinderIntersection(Point(0, 3, -2), Vector(0, -1, 2), 2), CylinderIntersection(Point(0, 4, -2), Vector(0, -1, 1), 2), # corner case CylinderIntersection(Point(0, 0, -2), Vector(0, 1, 2), 2), CylinderIntersection(Point(0, -1, -2), Vector(0, 1, 1), 2) # corner case ] for cylinder_intersection in cylinder_intersections: direction = Vector.normalize(cylinder_intersection.direction) r = Ray(cylinder_intersection.point, direction) xs = cyl.local_intersect(r) self.assertEqual(len(xs), cylinder_intersection.count)
def test_intersecting_a_constrained_cylinder(self): cyl = Cylinder(1, 2) direction = Vector(0.1, 1, 0) r = Ray(Point(0, 1.5, 0), direction) xs = cyl.local_intersect(r) self.assertEqual(xs.count, 0)
def test_a_ray_misses_a_cylinder3(self): cyl = Cylinder() direction = Vector(1, 1, 1).normalize() r = Ray(Point(0, 0, -5), direction) xs = cyl.local_intersect(r) self.assertEqual(0, xs.count)