def test_bounded_cylinder_bounding_box(self): shape = Cylinder() shape.minimum = -5 shape.maximum = 3 box = shape.bounds_of() self.assertEqual(box.min, Point(-1, -5, -1)) self.assertEqual(box.max, Point(1, 3, 1))
def test_group_bounding_box_contains_children(self): s = Sphere() s.transform = Transformations.translation(2, 5, -3).dot( Transformations.scaling(2, 2, 2)) c = Cylinder() c.minimum = -2 c.maximum = 2 c.transform = Transformations.translation(-4, -1, 4).dot( Transformations.scaling(0.5, 1, 0.5)) shape = Group() shape.add_child(s) shape.add_child(c) box = shape.bounds_of() self.assertEqual(box.min, Point(-4.5, -3, -5)) self.assertEqual(box.max, Point(4, 7, 4.5))
def test_normal_vector_cylinder_end_caps(self): cyl = Cylinder() cyl.minimum = 1 cyl.maximum = 2 cyl.closed = True PointNormal = namedtuple("PointNormal", ["point", "normal"]) point_normals = [ PointNormal(Point(0, 1, 0), Vector(0, -1, 0)), PointNormal(Point(0.5, 1, 0), Vector(0, -1, 0)), PointNormal(Point(0, 1, 0.5), Vector(0, -1, 0)), PointNormal(Point(0, 2, 0), Vector(0, 1, 0)), PointNormal(Point(0.5, 2, 0), Vector(0, 1, 0)), PointNormal(Point(0, 2, 0.5), Vector(0, 1, 0)) ] for point_normal in point_normals: n = cyl.local_normal_at(point_normal.point) self.assertEqual(n, point_normal.normal)
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_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)