def test_specularReflection2(self): #Light light = Light(0, 0, 0, 1) # Sphere 1 c1 = Vector(0, 0, 12) s1 = Sphere(c1, 1, Color(0, 1, 0), 0) scene = Scene() scene.addObject3D(s1) scene.addLight(light) camera = Camera(Vector(0, 0, 0), Vector(0, 0, 1), 30) imagepl = Imageplane(500, 500) raytracer = RayTracer(imagepl, scene, camera) pixelRay = Ray(camera.position, Vector(0.001, 0, 1)) sphereIntersection = s1.intersection(pixelRay, 1, math.inf) arrColor = raytracer.getColorForIntersection(sphereIntersection, 0) testColor = arrColor green = Color() green.green() testValue = testColor.isBrighterOrEqualTo(green) self.assertFalse(testValue)
def test_largeScene(self): light = Light(0, 2, 6, 1) ambientLight = AmbientLight(0.3) # Sphere 1 c1 = Vector(.8, .5, 1.5) s1 = Sphere(c1, .4, Color(0, 1, 0), 10) # Sphere 2 c2 = Vector(.7, .1, 1.) s2 = Sphere(c2, .7, Color(0, 0, 1), 10) # Sphere 3 c3 = Vector(.4, .4, .7) s3 = Sphere(c3, .2, Color(1, 0, 0), 10) # Create Scene scene = Scene() scene.addObject3D(s1) scene.addObject3D(s2) scene.addObject3D(s3) scene.addLight(light) scene.addLight(ambientLight) camera = Camera(Vector(0, 0, 0), Vector(0, 0, 1), Vector(1, 0, 0), math.pi / 4) imagepl = Imageplane(500, 500) raytracer = RayTracer(imagepl, scene, camera) startTime = time.time() raytracer.startRayTracing() endTime = time.time() self.assertLessEqual(endTime - startTime, 200)
def __init__(self, imageplane=Imageplane(), mainscene=Scene(), camera=Camera(), antialiasing=False): self.recursionLimit = 3 self.backgroundColor = Color(0, 0, 0) self.imageplane = imageplane self.scene = mainscene self.camera = camera self.imageAspectRatio = imageplane.width / imageplane.height self.img = numpy.zeros( (imageplane.getHeight(), imageplane.getWidth(), 3)) self.antialiasing = antialiasing
def test_diffusion1(self): #Light light = Light(0, 0, 0, 1) # Sphere 1 c1 = Vector(0, 0, 12) s1 = Sphere(c1, 1, Color(0, 1, 0), 1000) scene = Scene() scene.addObject3D(s1) scene.addLight(light) camera = Camera(Vector(0, 0, 0), Vector(0, 0, 1), 30) imagepl = Imageplane(500, 500) raytracer = RayTracer(imagepl, scene, camera) pixelRay1 = Ray(camera.position, Vector(0.001, 0, 1)) pixelRay2 = Ray(camera.position, Vector(0.01, 0, 1)) pixelRay3 = Ray(camera.position, Vector(0.02, 0, 1)) pixelRay4 = Ray(camera.position, Vector(0.03, 0, 1)) sphereIntersection1 = s1.intersection(pixelRay1, 0, 1000) sphereIntersection2 = s1.intersection(pixelRay2, 0, 1000) sphereIntersection3 = s1.intersection(pixelRay3, 0, 1000) sphereIntersection4 = s1.intersection(pixelRay4, 0, 1000) arrColor1 = raytracer.getColorForIntersection(sphereIntersection1, 0) arrColor2 = raytracer.getColorForIntersection(sphereIntersection2, 0) arrColor3 = raytracer.getColorForIntersection(sphereIntersection3, 0) arrColor4 = raytracer.getColorForIntersection(sphereIntersection4, 0) testColor1 = arrColor1 testColor2 = arrColor2 testColor3 = arrColor3 testColor4 = arrColor4 testValue3 = testColor3.isBrighterOrEqualTo(testColor4) self.assertTrue(testValue3) testValue2 = testColor2.isBrighterOrEqualTo(testColor3) self.assertTrue(testValue2) testValue1 = testColor1.isBrighterOrEqualTo(testColor2) self.assertTrue(testValue1)
def test_SimpleScene(self): light = Light(0, 2, 6, 1) center = Vector(.8, .1, 1.) sphere = Sphere(center, .4, Color(1, 0, 0), 0) scene = Scene() scene.addObject3D(sphere) scene.addLight(light) camera = Camera(Vector(0, 0, 0), Vector(0, 0, 1), Vector(1, 0, 0), math.pi / 4) imagepl = Imageplane(500, 500) raytracer = RayTracer(imagepl, scene, camera) startTime = time.time() raytracer.startRayTracing() endTime = time.time() self.assertLessEqual(endTime - startTime, 100)
def deserializeScene(self, json): scene = Scene() jsonScene = json["Scene"] for object in jsonScene["Object3D"]: type = list(object.keys())[0] if (type == "Sphere"): scene.addObject3D(self.deserializeSphere(object[type])) elif (type == "Cube"): scene.addObject3D(self.deserializeCube(object[type])) elif (type == "Cylinder"): scene.addObject3D(self.deserializeCylinder(object[type])) elif (type == "Cone"): scene.addObject3D(self.deserializeCone(object[type])) for light in jsonScene["Light"]: scene.addLight(self.deserializeLight(light)) scene.addLight(self.deserializeAmbientLight(jsonScene["AmbientLight"])) if self.deserializeFloor(jsonScene["Floor"]): scene.addObject3D( Plane(Vector(0, -3, 0), Vector(0, 1, 0), Color(0.75, 0.7, 0.75), 500, 0.2)) if self.deserializeRoom(jsonScene["Room"]): scene.addObject3D( Plane(Vector(0, -3, 0), Vector(0, 1, 0), Color(0.75, 0.7, 0.75), 500, 0.2)) scene.addObject3D( Plane(Vector(0, 10, 0), Vector(0, 1, 0), Color(0., 0.6, 0.2), 0, 0)) scene.addObject3D( Plane(Vector(0, 0, 30), Vector(0, 0, -1), Color(0.63, 0.73, 0.65), 0, 0)) scene.addObject3D( Plane(Vector(0, 0, -1), Vector(0, 0, 1), Color(0.9, 0.6, 0.4), 0, 0)) scene.addObject3D( Plane(Vector(-10, 0, 0), Vector(1, 0, 0), Color(1., 0.4, 0.4), 0, 0)) scene.addObject3D( Plane(Vector(10, 0, 0), Vector(-1, 0, 0), Color(0.6, 0.8, 1.0), 0, 0)) return scene
p6 = Plane(Vector(10, 0, 0), Vector(-1, 0, 0), Color(0.1, 0.7, 0.2), 0, 0) s1 = Sphere(sCenter1, 1, Color(1.0, 0, 0), 600, 0, 0) s2 = Sphere(sCenter2, 1.3, Color(0, 1.0, 0), 500, 0, 0) cube = Cube(Vector(0, 0, 25), 10, Color(0, 1, 0), 1000, 0, 0) cone = Cone(Vector(0, 0, 10), 1, 1, Color(1, 0, 0), 1000, 0, 0) cylinder = Cylinder(Vector(0, -3, 8), 1, 1, Color(1, 0, 0), 1000, 0.8, 0) light1 = Light(0, 3, 8, 0.5) light2 = Light(1, 3, 5, 0.3) light0 = AmbientLight(0.2) scene = Scene() scene.addLight(light0) scene.addLight(light1) scene.addLight(light2) scene.addObject3D(s1) scene.addObject3D(cylinder) scene.addObject3D(cone) scene.addObject3D(cube) scene.addObject3D(p1) #scene.addObject3D(p2) #scene.addObject3D(p3) #scene.addObject3D(p4)