示例#1
0
    def __init__(self, file):
        self.ignored = 0
        self.vertices = [Point(0, 0,
                               0)]  # Add a point to start actual vertices at 1
        self.named_groups = {}
        self.default_group = Group()
        self.current_group = self.default_group
        for line in file:
            if line[0] == "v":
                point_values = line.split(" ")
                p = Point(
                    float(point_values[1]),
                    float(point_values[2]),
                    float(point_values[3]),
                )
                self.vertices.append(p)
            elif line[0] == "f":
                point_i = line.split(" ")
                print(list(map(lambda x: int(x), point_i[1:])))
                ts = self.fan_triangulation(
                    list(map(lambda x: int(x), point_i[1:])))
                for t in ts:
                    self.current_group.add_child(t)
            elif line[0] == "g":
                group_name = line.split(" ")[1].strip()
                if self.get_named_group(group_name) is None:
                    self.create_named_group(group_name)
                self.current_group = self.named_groups[group_name]
                print(self.named_groups)

            else:
                self.ignored += 1
示例#2
0
def test_transformed_group():
    g = Group()
    g.set_transform(Scaling(2, 2, 2))
    s = Sphere()
    s.set_transform(Translation(5, 0, 0))
    g.add_child(s)
    r = Ray(Point(10, 0, -10), Vector(0, 0, 1))
    xs = g.intersect(r)
    assert len(xs) == 2
示例#3
0
def test_nonempty_group():
    g = Group()
    s1 = Sphere()
    s2 = Sphere()
    s2.set_transform(Translation(0, 0, -3))
    s3 = Sphere()
    s3.set_transform(Translation(5, 0, 0))
    g.add_child(s1)
    g.add_child(s2)
    g.add_child(s3)
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    xs = g.local_intersect(r)
    assert len(xs) == 4
    assert xs[0].object == s2
    assert xs[1].object == s2
    assert xs[2].object == s1
    assert xs[3].object == s1
示例#4
0
 def create_named_group(self, group_name):
     self.named_groups[group_name] = Group()
示例#5
0
 def obj_to_group(self):
     g = Group()
     g.objects.append(self.default_group)
     for group in self.named_groups:
         g.objects.append(group)
     return g
示例#6
0
def test_group_creation():
    g = Group()
    assert g.transform == Identity()
    assert len(g.objects) == 0
示例#7
0
def test_empty_group():
    g = Group()
    r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
    xs = g.local_intersect(r)
    assert len(xs) == 0
示例#8
0
def test_add_child():
    g = Group()
    s = _TestShape()
    g.add_child(s)
    assert len(g.objects) > 0
    assert s.parent == g
示例#9
0
def test_normal_object_to_world():
    g1 = Group()
    g1.set_transform(RotationY(math.pi / 2))
    g2 = Group()
    g2.set_transform(Scaling(1, 2, 3))
    g1.add_child(g2)
    s = Sphere()
    s.set_transform(Translation(5, 0, 0))
    g2.add_child(s)
    n = s.normal_to_world(
        Vector(math.sqrt(3) / 3,
               math.sqrt(3) / 3,
               math.sqrt(3) / 3))
    assert n == Vector(0.2857, 0.4286, -0.8571)
示例#10
0
def test_world_to_object():
    g1 = Group()
    g1.set_transform(RotationY(math.pi / 2))
    g2 = Group()
    g2.set_transform(Scaling(2, 2, 2))
    g1.add_child(g2)
    s = Sphere()
    s.set_transform(Translation(5, 0, 0))
    g2.add_child(s)
    p = s.world_to_object(Point(-2, 0, -10))
    assert p == Point(0, 0, -1)
示例#11
0
def test_child_normal():
    g1 = Group()
    g1.set_transform(RotationY(math.pi / 2))
    g2 = Group()
    g2.set_transform(Scaling(1, 2, 3))
    g1.add_child(g2)
    s = Sphere()
    s.set_transform(Translation(5, 0, 0))
    g2.add_child(s)
    n = s.normal_at(Point(1.7321, 1.1547, -5.5774))
    assert n == Vector(0.2857, 0.4286, -0.8571)