コード例 #1
0
 def test_cross(self):
     v1 = Vec3(1, 0, 0)
     v2 = Vec3(0, 1, 0)
     v3 = Vec3.cross(v1, v2)
     self.assertEqual(v3[0], 0)
     self.assertEqual(v3[1], 0)
     self.assertEqual(v3[2], 1)
コード例 #2
0
    def test_construct(self):
        v = Vec3()
        self.assertEqual(v._e[0], 0)
        self.assertEqual(v._e[1], 0)
        self.assertEqual(v._e[2], 0)

        v = Vec3(1, 2, 3)
        self.assertEqual(v._e[0], 1)
        self.assertEqual(v._e[1], 2)
        self.assertEqual(v._e[2], 3)
コード例 #3
0
    def test_getter(self):
        v = Vec3()
        self.assertEqual(v[0], 0)
        self.assertEqual(v[1], 0)
        self.assertEqual(v[2], 0)

        v = Vec3(1, 2, 3)
        self.assertEqual(v[0], 1)
        self.assertEqual(v[1], 2)
        self.assertEqual(v[2], 3)
コード例 #4
0
 def test_unitvector(self):
     v1 = Vec3(1, 2, 3)
     v2 = Vec3.unitVector(v1)
     self.assertEqual(v2.Length, 1)
     self.assertEqual(v2.SquaredLength, 1)
     self.assertEqual(v2[0], 0.2672612419124244)
     self.assertEqual(v2[1], 0.5345224838248488)
     self.assertEqual(v2[2], 0.8017837257372732)
     self.assertEqual(v1[0], 1)
     self.assertEqual(v1[1], 2)
     self.assertEqual(v1[2], 3)
コード例 #5
0
    def test_add_equals(self):
        v1 = Vec3(1, 2, 3)
        v2 = Vec3(4, 5, 6)
        v1 += v2
        self.assertEqual(v1[0], 5)
        self.assertEqual(v1[1], 7)
        self.assertEqual(v1[2], 9)

        self.assertEqual(v2[0], 4)
        self.assertEqual(v2[1], 5)
        self.assertEqual(v2[2], 6)
コード例 #6
0
    def test_div_vector_equals(self):
        v1 = Vec3(1, 2, 3)
        v2 = Vec3(4, 6, 6)
        v2 /= v1
        self.assertEqual(v1[0], 1)
        self.assertEqual(v1[1], 2)
        self.assertEqual(v1[2], 3)

        self.assertEqual(v2[0], 4)
        self.assertEqual(v2[1], 3)
        self.assertEqual(v2[2], 2)
コード例 #7
0
    def test_mul_vector_equals(self):
        v1 = Vec3(1, 2, 3)
        v2 = Vec3(4, 5, 6)
        v1 *= v2
        self.assertEqual(v1[0], 4)
        self.assertEqual(v1[1], 10)
        self.assertEqual(v1[2], 18)

        self.assertEqual(v2[0], 4)
        self.assertEqual(v2[1], 5)
        self.assertEqual(v2[2], 6)
コード例 #8
0
    def test_sub_equals(self):
        v1 = Vec3(1, 2, 3)
        v2 = Vec3(4, 5, 6)
        v2 -= v1
        self.assertEqual(v1[0], 1)
        self.assertEqual(v1[1], 2)
        self.assertEqual(v1[2], 3)

        self.assertEqual(v2[0], 3)
        self.assertEqual(v2[1], 3)
        self.assertEqual(v2[2], 3)
コード例 #9
0
    def test_add(self):
        v1 = Vec3(1, 2, 3)
        v2 = Vec3(4, 5, 6)
        v3 = v1 + v2
        self.assertEqual(v3[0], 5)
        self.assertEqual(v3[1], 7)
        self.assertEqual(v3[2], 9)

        self.assertEqual(v1[0], 1)
        self.assertEqual(v1[1], 2)
        self.assertEqual(v1[2], 3)

        self.assertEqual(v2[0], 4)
        self.assertEqual(v2[1], 5)
        self.assertEqual(v2[2], 6)
コード例 #10
0
    def test_sub(self):
        v1 = Vec3(1, 2, 3)
        v2 = Vec3(4, 5, 6)
        v3 = v2 - v1
        self.assertEqual(v3[0], 3)
        self.assertEqual(v3[1], 3)
        self.assertEqual(v3[2], 3)

        self.assertEqual(v1[0], 1)
        self.assertEqual(v1[1], 2)
        self.assertEqual(v1[2], 3)

        self.assertEqual(v2[0], 4)
        self.assertEqual(v2[1], 5)
        self.assertEqual(v2[2], 6)
コード例 #11
0
    def test_div_vector(self):
        v1 = Vec3(1, 2, 3)
        v2 = Vec3(4, 6, 6)
        v3 = v2 / v1
        self.assertEqual(v3[0], 4)
        self.assertEqual(v3[1], 3)
        self.assertEqual(v3[2], 2)

        self.assertEqual(v1[0], 1)
        self.assertEqual(v1[1], 2)
        self.assertEqual(v1[2], 3)

        self.assertEqual(v2[0], 4)
        self.assertEqual(v2[1], 6)
        self.assertEqual(v2[2], 6)
コード例 #12
0
def color(r, world, depth):
    bHit, rec = world.hit(r, 0.001, sys.float_info.max)
    if bHit:
        if depth < 50:
            bScattered, attenuation, scattered = rec.Material.scatter(r, rec)
            if bScattered:
                return attenuation * color(scattered, world, depth + 1)
            else:
                return Vec3(0, 0, 0)
        else:
            return Vec3(0, 0, 0)
    else:
        unitDirection = Vec3.unitVector(r.Direction)
        t = 0.5 * (unitDirection.Y + 1.0)
        return (1.0 - t) * Vec3(1, 1, 1) + t * Vec3(0.5, 0.7, 1.0)
コード例 #13
0
    def test_mul_vector(self):
        v1 = Vec3(1, 2, 3)
        v2 = Vec3(4, 5, 6)
        v3 = v1 * v2
        self.assertEqual(v3[0], 4)
        self.assertEqual(v3[1], 10)
        self.assertEqual(v3[2], 18)

        self.assertEqual(v1[0], 1)
        self.assertEqual(v1[1], 2)
        self.assertEqual(v1[2], 3)

        self.assertEqual(v2[0], 4)
        self.assertEqual(v2[1], 5)
        self.assertEqual(v2[2], 6)
コード例 #14
0
def createTestScene1():
    world = HitableList()
    world.append(Sphere(Vec3(0, 0, -1), 0.5, Lambertian(Vec3(0.1, 0.2, 0.5))))
    world.append(
        Sphere(Vec3(0, -100.5, -1), 100, Lambertian(Vec3(0.8, 0.8, 0.0))))
    world.append(Sphere(Vec3(1, 0, -1), 0.5, Metal(Vec3(0.8, 0.6, 0.2))))
    world.append(Sphere(Vec3(-1, 0, -1), 0.5, Dielectric(1.5)))
    world.append(Sphere(Vec3(-1, 0, -1), -0.45, Dielectric(1.5)))
    return world
コード例 #15
0
 def test_makeunitvector(self):
     v = Vec3(1, 2, 3)
     v.makeUnitVector()
     self.assertEqual(v.Length, 1)
     self.assertEqual(v.SquaredLength, 1)
     self.assertEqual(v[0], 0.2672612419124244)
     self.assertEqual(v[1], 0.5345224838248488)
     self.assertEqual(v[2], 0.8017837257372732)
コード例 #16
0
 def test_properties(self):
     v = Vec3(1, 2, 3)
     self.assertEqual(v.X, 1)
     self.assertEqual(v.Y, 2)
     self.assertEqual(v.Z, 3)
     self.assertEqual(v.R, 1)
     self.assertEqual(v.G, 2)
     self.assertEqual(v.B, 3)
コード例 #17
0
def main():
    nx = 200
    ny = 100
    ns = 10
    print("P3\n%d %d\n255" % (nx, ny))

    lookFrom = Vec3(13, 2, 3)
    lookAt = Vec3(0, 0, 0)
    distToFocus = 10.0
    aperature = 0.1

    cam = Camera(lookFrom, lookAt, Vec3(0, 1, 0), 20.0,
                 float(nx) / float(ny), aperature, distToFocus)

    world = createRandomScene()

    numRays = 0
    t0 = time.time()

    for j in reversed(range(ny)):
        for i in range(nx):
            col = Vec3(0.0, 0.0, 0.0)
            for s in range(ns):
                u = (i + random()) / float(nx)
                v = (j + random()) / float(ny)
                r = cam.getRay(u, v)
                numRays += 1
                col += color(r, world, 0)

            col /= float(ns)
            col = Vec3(math.sqrt(col.R), math.sqrt(col.G), math.sqrt(col.B))
            ir = int(255.99 * col.R)
            ig = int(255.99 * col.G)
            ib = int(255.99 * col.B)
            print("%d %d %d" % (ir, ig, ib))
            print("row: %d, col: %d" % (j, i), file=sys.stderr)

    t1 = time.time()
    seconds = t1 - t0
    print("Rays = %d" % numRays, file=sys.stderr)
    print("Seconds = %f" % seconds, file=sys.stderr)
    raysPerSecond = numRays / seconds
    print("Rays/Second = %f" % raysPerSecond, file=sys.stderr)
コード例 #18
0
    def test_mul_scalar_reflected(self):
        v1 = Vec3(1, 2, 3)
        v2 = 2.0 * v1
        self.assertEqual(v2[0], 2)
        self.assertEqual(v2[1], 4)
        self.assertEqual(v2[2], 6)

        self.assertEqual(v1[0], 1)
        self.assertEqual(v1[1], 2)
        self.assertEqual(v1[2], 3)
コード例 #19
0
    def test_mul_scalar(self):
        v1 = Vec3(1, 2, 3)
        v2 = v1 * 2.0
        self.assertEqual(v2[0], 2)
        self.assertEqual(v2[1], 4)
        self.assertEqual(v2[2], 6)

        self.assertEqual(v1[0], 1)
        self.assertEqual(v1[1], 2)
        self.assertEqual(v1[2], 3)
コード例 #20
0
    def test_div_scalar(self):
        v1 = Vec3(1, 2, 3)
        v2 = v1 / 0.5
        self.assertEqual(v2[0], 2)
        self.assertEqual(v2[1], 4)
        self.assertEqual(v2[2], 6)

        self.assertEqual(v1[0], 1)
        self.assertEqual(v1[1], 2)
        self.assertEqual(v1[2], 3)
コード例 #21
0
    def test_neg(self):
        v = Vec3(1, 2, 3)
        self.assertEqual(v[0], 1)
        self.assertEqual(v[1], 2)
        self.assertEqual(v[2], 3)

        w = -v
        self.assertEqual(w[0], -1)
        self.assertEqual(w[1], -2)
        self.assertEqual(w[2], -3)
コード例 #22
0
def testImage():
    nx = 200
    ny = 100
    print("P3\n%d %d\n255" % (nx, ny))
    for j in reversed(range(ny)):
        for i in range(nx):
            col = Vec3(i / float(nx), j / float(ny), 0.2)
            ir = int(255.99 * col.R)
            ig = int(255.99 * col.G)
            ib = int(255.99 * col.B)
            print("%d %d %d" % (ir, ig, ib))
コード例 #23
0
    def test_setter(self):
        v = Vec3()
        self.assertEqual(v._e[0], 0)
        self.assertEqual(v._e[1], 0)
        self.assertEqual(v._e[2], 0)

        v[0] = 1
        v[1] = 2
        v[2] = 3

        self.assertEqual(v._e[0], 1)
        self.assertEqual(v._e[1], 2)
        self.assertEqual(v._e[2], 3)
コード例 #24
0
def createRandomScene():
    world = HitableList()
    world.append(
        Sphere(Vec3(0, -1000, 0), 1000.0, Lambertian(Vec3(0.5, 0.5, 0.5))))
    for a in range(-11, 11):
        for b in range(-11, 11):
            chooseMat = random()
            center = Vec3(a + 0.9 * random(), 0.2, b + 0.9 * random())
            if (center - Vec3(4, 0.2, 0)).Length > 0.9:
                if chooseMat < 0.8:  # dffuse
                    world.append(
                        Sphere(
                            center, 0.2,
                            Lambertian(
                                Vec3(random() * random(),
                                     random() * random(),
                                     random() * random()))))
                elif chooseMat < 0.95:  #metal
                    world.append(
                        Sphere(
                            center, 0.2,
                            Metal(
                                Vec3(0.5 * (1.0 + random()),
                                     0.5 * (1.0 + random()),
                                     0.5 * (1.0 + random())), 0.5 * random())))
                else:  #glass
                    world.append(Sphere(center, 0.2, Dielectric(1.5)))

    world.append(Sphere(Vec3(0, 1, 0), 1.0, Dielectric(1.5)))
    world.append(Sphere(Vec3(-4, 1, 0), 1.0, Lambertian(Vec3(0.4, 0.2, 0.1))))
    world.append(Sphere(Vec3(4, 1, 0), 1.0, Metal(Vec3(0.7, 0.6, 0.5), 0.0)))
    return world
コード例 #25
0
    def test_triangle(self):
        t = Triangle(Vec3(0.0, 0.0, 0.0),
                     Vec3(1.0, 0.0, 0.0),
                     Vec3(0.0, 1.0, 0.0))
        no_intersecting_rays = [
            Ray(Vec3(0, 0, 1), Vec3(1, 0, 0)),
            Ray(Vec3(0, 0, 1), Vec3(-1, 0, 0)),
            Ray(Vec3(1, 1, -1), Vec3(-1, 0, 0)),
            Ray(Vec3(-1, -1, -1), Vec3(0, 1, 0)),
        ]
        
        for r in no_intersecting_rays:
            self.assertEqual(r.intersect(t).success, False)

        intersecting_rays = [
            Ray(Vec3(0.1, 0.1, -1), Vec3(0, 0, 1)),
            Ray(Vec3(0.5, 0.5, 1), Vec3(0, 0, -1)),
            Ray(Vec3(0.5, 0.5, -1), Vec3(0, 0, 1)),
            Ray(Vec3(0.5, 0.5, 1), Vec3(0, 0, -1)),
        ]

        for r in intersecting_rays:
            self.assertEqual(r.intersect(t).success, True)
コード例 #26
0
    def test_sphere(self):
        s = Sphere(Vec3.zero, 1.0)

        no_intersecting_rays = [
            Ray(Vec3(0, 0, 2), Vec3(1, 0, 0)),
            Ray(Vec3(0, 0, 2), Vec3(-1, 0, 0)),
            Ray(Vec3(1, 1, -2), Vec3(-1, 0, 0)),
            Ray(Vec3(-2, -1, -1), Vec3(0, 1, 0)),
        ]
        
        for r in no_intersecting_rays:
            self.assertEqual(r.intersect(s).success, False)

        intersecting_rays = [
            Ray(Vec3(0, 0, -2), Vec3(0, 0, 1)),
            Ray(Vec3(0.5, -2, 0.5), Vec3(0, 1, 0)),
            Ray(Vec3(-2, 0.5, 0), Vec3(1, 0, 0)),
            Ray(Vec3(0.5, 0.5, 2), Vec3(0, 0, -1)),
        ]

        for r in intersecting_rays:
            self.assertEqual(r.intersect(s).success, True)
コード例 #27
0
def main():
    camera = Camera(
        position=Vec3(0, 2, 8.5),
        target=Vec3(0, 0, 0),
        up=Vec3.up,
        resolution=(300, 300),
    )

    mat = Material(0, 1 / pi)
    scene = (
        Sphere(Vec3(-4.0, 0, 0), 2.0, Material(0, 1.0 / pi)),
        Sphere(Vec3(0, -1.0, 3), 1.0, Material(0, 1.0 / pi)),
        Sphere(Vec3(4.0, 1, 0.0), 3.0, Material(0, 1.0 / pi)),
        Sphere(Vec3(0, 5, 0), 0.75, Material(19550, 0.0)),
        Triangle(Vec3(-10, -2, -10), Vec3(10, -2, 10), Vec3(10, -2, -10), mat),
        Triangle(Vec3(-10, -2, -10), Vec3(-10, -2, 10), Vec3(10, -2, 10), mat),
        Triangle(Vec3(-10, 6, -10), Vec3(10, 6, -10), Vec3(10, 6, 10), mat),
        Triangle(Vec3(-10, 6, -10), Vec3(10, 6, 10), Vec3(-10, 6, 10), mat),
        Triangle(Vec3(-10, -2, -10), Vec3(10, -2, -10), Vec3(10, 6, -10), mat),
        Triangle(Vec3(-10, -2, -10), Vec3(10, 6, -10), Vec3(-10, 6, -10), mat),
        Triangle(Vec3(-10, -2, -10), Vec3(-10, 6, 10), Vec3(-10, -2, 10), mat),
        Triangle(Vec3(-10, -2, -10), Vec3(-10, 6, -10), Vec3(-10, 6, 10), mat),
        Triangle(Vec3(10, -2, -10), Vec3(10, -2, 10), Vec3(10, 6, 10), mat),
        Triangle(Vec3(10, -2, -10), Vec3(10, 6, 10), Vec3(10, 6, -10), mat),
        Triangle(Vec3(-10, -2, 10), Vec3(10, 6, 10), Vec3(10, -2, 10), mat),
        Triangle(Vec3(-10, -2, 10), Vec3(-10, 6, 10), Vec3(10, 6, 10), mat),
    )

    ptracer = PathTracer()
    img = ptracer.render(camera, scene, 32)
    save_img("result.ppm", img)
コード例 #28
0
 def test_length(self):
     v = Vec3(1, 2, 3)
     self.assertEqual(v.Length, 3.7416573867739413)
     self.assertEqual(v.SquaredLength, 14)
コード例 #29
0
 def test_dot(self):
     v1 = Vec3(1, 2, 3)
     v2 = Vec3(4, 6, 6)
     self.assertEqual(Vec3.dot(v1, v2), 34)
コード例 #30
0
 def test_str(self):
     v = Vec3(1, 2, 3)
     self.assertEqual(str(v), "1.000000 2.000000 3.000000")