コード例 #1
0
ファイル: things.py プロジェクト: CarolinaMisa/Ray_tracing
def test_sphere_hits():
    sphere_pars = [['location', VEC3(0, -1, 3)], ['radius', 1]]
    sphere = Sphere(sphere_pars)
    for raydir in (
            VEC3(0, 0.2, 1),  # Justo arriba de la esfera
            VEC3(0, 0, 1),  # Tangencial
            VEC3(0, -0.2, 1)):  # Impacta a la esfera
        ray = Ray(VEC3(0, 0, 0), raydir.normalized())
        print("Ray: {}".format(ray))
        for hit in sphere.intersection(ray):
            print("  {}".format(ray.at(hit.impact)))
コード例 #2
0
ファイル: things.py プロジェクト: CarolinaMisa/Ray_tracing
def test_camera_rays():
    pars = [['orthographic', None], ['up', VEC3(0.0, 1.0, 0.0)],
            ['look_at', VEC3(0.0, 0.0, 0.0)], ['angle', 60.0],
            ['location', VEC3(5.0, 0.0, 0.0)]]
    cam = Camera(pars)
    ray_gen = cam.ray_generator(32, 24)
    x0 = []
    y0 = []
    for ray, x, y in ray_gen:
        x0.append(ray.dir.x)
        y0.append(ray.dir.y)
    plt.plot(x0, y0, '*')
    plt.grid()
    plt.show()
コード例 #3
0
ファイル: things.py プロジェクト: CarolinaMisa/Ray_tracing
    def ray_generator(self, width, height):
        bg_color = RGB_colors.Black.as_tuple()
        self.image = Image.new("RGB", (width, height), bg_color)

        nr_pixels = width * height
        h_size = 2 * tan(radians(self.param('angle')) / 2)
        v_size = h_size * height / width
        scale = h_size / (width - 1)

        for pix in range(nr_pixels):
            x0 = pix % width
            y0 = pix // width
            x = x0 - (width - 1) / 2
            y = y0 - (height - 1) / 2
            ray = Ray(self.param("location"),
                      VEC3(x * scale, y * scale, 1).normalized())
            yield ray, x0, y0
コード例 #4
0
ファイル: parse.py プロジェクト: CarolinaMisa/Ray_tracing
    def make_parser(self, which="parser"):
        open_par = pp.Literal("{").suppress()
        close_par = pp.Literal("}").suppress()

        unsigned = pp.Word(pp.nums)
        signed = pp.Combine(pp.Optional(pp.oneOf("- +")) + unsigned)

        floatn = pp.Combine(signed + pp.Optional(pp.Literal(".") + unsigned) +
                            pp.Optional(pp.oneOf("e E") + signed))
        floatn = floatn.setParseAction(lambda tkn: float(tkn[0]))

        vector = (
            (pp.Literal("<").suppress() + floatn + pp.Literal(",").suppress() +
             floatn + pp.Literal(",").suppress() + floatn +
             pp.Literal(">").suppress()
             ).setParseAction(lambda t: VEC3(t[0], t[1], t[2]))
            | pp.Literal("x").setParseAction(lambda t: VEC3(1.0, 0.0, 0.0))
            | pp.Literal("y").setParseAction(lambda t: VEC3(0.0, 1.0, 0.0))
            | pp.Literal("z").setParseAction(lambda t: VEC3(0.0, 0.0, 1.0)))

        color_vector = ((pp.Literal("<").suppress() + floatn +
                         pp.Literal(",").suppress() + floatn +
                         pp.Literal(",").suppress() + floatn +
                         pp.Literal(">").suppress()
                         ).setParseAction(lambda t: RGB(t[0], t[1], t[2])))

        color = (pp.Keyword("rgb") +
                 color_vector).setParseAction(lambda t: ["rgb", t[1]])

        pigment_item = color

        pigment_items = pp.Group(pp.OneOrMore(pigment_item))

        pigment = (pp.Keyword("pigment").suppress() + open_par +
                   pigment_items +
                   close_par).setParseAction(lambda t: ["pigment", t[0]])

        reflection = pp.Group(pp.Keyword('reflection') + floatn)("reflection")

        finish_item = reflection

        finish_items = pp.Group(pp.OneOrMore(finish_item))

        finish = (pp.Keyword("finish").suppress() + open_par + finish_items +
                  close_par).setParseAction(lambda t: ["finish", t[0]])

        texture_item = pp.Group(pigment | finish)

        texture_items = pp.Group(pp.OneOrMore(texture_item))

        texture = (pp.Keyword("texture") + open_par + texture_items +
                   close_par).setParseAction(lambda t: ["texture", t[1]])

        object_modifier = pp.Group(texture)
        object_modifiers = pp.ZeroOrMore(object_modifier)

        cam_types = pp.oneOf("orthographic").setParseAction(
            lambda t: [[t[0], None]])
        cam_loc = (pp.Keyword("location") +
                   vector).setParseAction(lambda t: [["location", t[1]]])
        cam_angle = (pp.Keyword("angle") +
                     floatn).setParseAction(lambda t: [["angle", t[1]]])
        cam_lookat = (pp.Keyword("look_at") +
                      vector).setParseAction(lambda t: [["look_at", t[1]]])
        cam_up = (pp.Keyword("up") +
                  vector).setParseAction(lambda t: [["up", t[1]]])
        cam_items = pp.Group(cam_loc & cam_angle & cam_lookat
                             & pp.Optional(cam_types) & pp.Optional(cam_up))

        camera = pp.Group(
            pp.Keyword("camera") + pp.Literal("{").suppress() + cam_items +
            pp.Literal("}").suppress())

        light_modifiers = pp.Optional(
            pp.Keyword("parallel").setParseAction(lambda t: [[t[0], None]]))

        light = pp.Group(
            pp.Keyword("light_source") + open_par +
            pp.Group(vector.copy().setParseAction(
                lambda t: [["location", t[0]]]) + pp.Literal(",").suppress() +
                     color.setParseAction(lambda t: [["rgb", t[1]]]) +
                     light_modifiers) + close_par)

        sphere = pp.Group(
            pp.Keyword("sphere") + open_par + pp.Group(
                vector.copy().setParseAction(lambda t: [["location", t[0]]]) +
                pp.Literal(",").suppress() +
                floatn("radius").copy().setParseAction(
                    lambda t: [["radius", float(t[0])]]) + object_modifiers) +
            close_par)

        plane = pp.Group(
            pp.Keyword("plane") + open_par + pp.Group(
                vector.copy().setParseAction(lambda t: [["normal", t[0]]]) +
                pp.Literal(",").suppress() + floatn("distance").copy().
                setParseAction(lambda t: [["distance", float(t[0])]]) +
                object_modifiers) + close_par)

        triangle = pp.Group(
            pp.Keyword("triangle") + open_par +
            pp.Group(vector.copy().setParseAction(lambda t: [["v0", t[0]]]) +
                     pp.Literal(",").suppress() +
                     vector.copy().setParseAction(lambda t: [["v1", t[0]]]) +
                     pp.Literal(",").suppress() +
                     vector.copy().setParseAction(lambda t: [["v2", t[0]]]) +
                     object_modifiers) + close_par)

        graph_els = camera | light | sphere | plane | triangle

        parser = pp.OneOrMore(graph_els)
        return eval(which)
コード例 #5
0
ファイル: things.py プロジェクト: CarolinaMisa/Ray_tracing
def test_triangle():
    pars = [('v0', VEC3(3, 2, 1)), ('v1', VEC3(3, 2, 1)), ('v2', VEC3(3, 2,
                                                                      1))]
    triangle = Triangle(pars)
    print(triangle)
コード例 #6
0
ファイル: things.py プロジェクト: CarolinaMisa/Ray_tracing
def test_plane():
    pars = [('normal', VEC3(3, 2, 1)), ('distance', -2)]
    plane = Plane(pars)
    print(plane)
コード例 #7
0
ファイル: things.py プロジェクト: CarolinaMisa/Ray_tracing
def test_sphere():
    pars = [['location', VEC3(1, 2, 3)], ['radius', 1.3]]
    sphere = Sphere(pars)
    print(sphere)
コード例 #8
0
ファイル: things.py プロジェクト: CarolinaMisa/Ray_tracing
def test_light():
    pars = [('location', VEC3(1, 2, 3)), ('rgb', RGB(0.1, 0.3, 0.5)),
            ('parallel', None)]
    light = Light(pars)
    print(light)
コード例 #9
0
ファイル: things.py プロジェクト: CarolinaMisa/Ray_tracing
def test_camera():
    pars = [['orthographic', None], ['up', VEC3(0.0, 1.0, 0.0)],
            ['look_at', VEC3(0.0, 0.0, 0.0)], ['angle', 60.0],
            ['location', VEC3(5.0, 0.0, 0.0)]]
    cam = Camera(pars)
    print(cam)