def test_addition(self): black = Color(0.3, 0.2, 0.16) small = Color(0.2, 0.8, 0.62) additionResult = black.add(small) additionResult2 = small.add(black) self.assertEqual(additionResult.getArray(), [0.5, 1.0, 0.78]) self.assertEqual(additionResult2.getArray(), [0.5, 1.0, 0.78])
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_compareBrightness(self): red = Color() red.red() white = Color() white.white() test1 = red.isBrighterOrEqualTo(white) test2 = white.isBrighterOrEqualTo(red) self.assertFalse(test1) self.assertTrue(test2)
def test_checkReflectionAndTransparency(self): object = Object3D(Vector(0, 0, 5), Color(1, 0, 0), 0, 0.4, 0.3) objectMore = Object3D(Vector(0, 0, 5), Color(1, 0, 0), 0, 1.0, 1.0) self.assertEqual(object.getReflection(), 0.4) self.assertEqual(object.getTransparency(), 0.3) self.assertEqual(objectMore.getReflection(), 0.5) self.assertEqual(objectMore.getTransparency(), 0.5)
def test_multiply(self): black = Color(0.0, 0.0, 0.0) small = Color(0.1, 0.1, 0.1) multiplier = 0.3 multiplicationResultblack = black.multiply(multiplier) multiplicationResultsmall = small.multiply(multiplier) self.assertEqual(multiplicationResultblack.getArray(), black.getArray()) self.assertEqual(multiplicationResultsmall.getArray(), [0.03, 0.03, 0.03])
def test_getSurfaceNormal2(self): cylinder = Cylinder(Vector(0, 0, 5), 10, 1.5, Color().red(), 10, 0.1) point = Vector(-1.5, 2.5, 5) surfaceNormal = cylinder.getSurfaceNormal(point) self.assertEqual(surfaceNormal.x, -1) self.assertEqual(surfaceNormal.y, 0) self.assertEqual(surfaceNormal.z, 0)
def test_constructor(self): cylinder = Cylinder(Vector(0, 0, 5), 2, 1, Color().red(), 10, 0.1) self.assertEqual(cylinder.center.x, 0) self.assertEqual(cylinder.center.y, 0) self.assertEqual(cylinder.center.z, 5) self.assertEqual(cylinder.radius, 1) self.assertEqual(cylinder.height, 2)
def test_intersection6(self): cylinder = Cylinder(Vector(0, 8, 14), 10, 1.5, Color().red(), 10, 0.1) ray = Ray(Vector(0, 0, 0), Vector(0.0, 0.2, 1)) intersection = cylinder.intersection(ray, 0, 1000) self.assertAlmostEqual(intersection.point.x, 0.0) self.assertAlmostEqual(intersection.point.y, 3) self.assertAlmostEqual(intersection.point.z, 15)
def test_intersection5(self): cylinder = Cylinder(Vector(0, 0, 5), 2, 1, Color().red(), 10, 0.1) ray = Ray(Vector(0, 0, 0), Vector(0.1, -0.2, 1)) intersection = cylinder.intersection(ray, 0, 1000) self.assertAlmostEqual(intersection.point.x, 0.4087346744) self.assertAlmostEqual(intersection.point.y, -0.8174693488) self.assertAlmostEqual(intersection.point.z, 4.0873467439)
def test_intersection2(self): cylinder = Cylinder(Vector(0, 0, 5), 2, 1, Color().red(), 10, 0.1) ray = Ray(Vector(0, 0, 0), Vector(0, 0.1, 1)) intersection = cylinder.intersection(ray, 0, 1000) self.assertEqual(intersection.point.x, 0.0) self.assertEqual(intersection.point.y, 0.4) self.assertEqual(intersection.point.z, 4.0)
def test_constructor(self): cube = Cube(Vector(1, 1, 1), 2, Color().red(), 10) self.assertEqual(cube.minPoint.x, 0) self.assertEqual(cube.minPoint.y, 0) self.assertEqual(cube.minPoint.z, 0) self.assertEqual(cube.maxPoint.x, 2) self.assertEqual(cube.maxPoint.y, 2) self.assertEqual(cube.maxPoint.z, 2)
def test_intersection5(self): cone = Cone(Vector(0, 2.5, 5), 3, 1, Color(1, 0, 0)) ray = Ray(Vector(0, 0, 0), Vector(0.05, 0.2, 1)) intersection = cone.intersection(ray, 0.00001, 1000) self.assertAlmostEqual(intersection.point.x, 0.25, 3) self.assertAlmostEqual(intersection.point.y, 1.0, 3) self.assertAlmostEqual(intersection.point.z, 5, 3)
def test_intersection4(self): cone = Cone(Vector(0, 0, 5), 3, 1, Color(1, 0, 0)) ray = Ray(Vector(0, 0, 0), Vector(0.05, -0.3, 1)) intersection = cone.intersection(ray, 0.00001, 1000) self.assertAlmostEqual(intersection.point.x, 0.205613676) self.assertAlmostEqual(intersection.point.y, -1.233682056) self.assertAlmostEqual(intersection.point.z, 4.11227352)
def test_intersection2(self): cone = Cone(Vector(0, 0, 5), 3, 1, Color(1, 0, 0)) ray = Ray(Vector(0, 0, 0), Vector(0.0, 0.05, 1)) intersection = cone.intersection(ray, 0.00001, 1000) self.assertAlmostEqual(intersection.point.x, 0.0) self.assertAlmostEqual(intersection.point.y, 0.2288135593) self.assertAlmostEqual(intersection.point.z, 4.5762711864)
def __init__(self, v=Vector(0, 0, 0), l=0, color=Color(), specular=100, reflection=0.1, transparency=0, refractiveIndex=1.0): super().__init__(v, l, l, l, color, specular, reflection, transparency, refractiveIndex)
def __init__(self, point, normal, color=Color(), specular=100, reflection=0.1, transparency=0.0, refractiveIndex=1.0): super().__init__(point, color, specular, reflection, transparency, refractiveIndex) self.direction = normal
def __init__(self, v=Vector(0, 0, 0), radius=0, color=Color(), specular=50, reflection=0.1, transparency=0, refractiveIndex=1.0): super().__init__(v, color, specular, reflection, transparency, refractiveIndex) self.radius = radius
def __init__(self, x=0, y=0, z=0, l=0, color=Color(), specular=100, reflection=0.1, transparency=0, refractiveIndex=1.0): super().__init__(x, y, z, l, l, l, color, specular, reflection, transparency, refractiveIndex)
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 __init__(self, x=0, y=0, z=0, radius=0, color=Color(), specular=50, reflection=0.1, transparency=0, refractiveIndex=1.0): super().__init__(x, y, z, color, specular, reflection, transparency, refractiveIndex) self.radius = radius
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 test_constructor(self): cone = Cone(Vector(0, 0, 5), 3, 1, Color(1, 0, 0)) self.assertEqual(cone.bottom.x, 0) self.assertEqual(cone.bottom.y, -1.5) self.assertEqual(cone.bottom.z, 5) self.assertEqual(cone.top.x, 0) self.assertEqual(cone.top.y, 4.5) self.assertEqual(cone.top.z, 5) self.assertEqual(cone.radius, 1) self.assertEqual(cone.height, 3) self.assertAlmostEqual(cone.alpha, 18.43494882)
def __init__(self, center=Vector(0, 0, 0), color=Color(), specular=100, reflection=0.1, transparency=0, refractiveIndex=1.0): self.center = center self.color = color self.specular = specular self.reflection = reflection self.transparency = transparency self.checkReflectionAndTransparencyLimit() self.refractiveIndex = refractiveIndex
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 __init__(self, v=Vector(0, 0, 0), height=0, radius=0, color=Color(), specular=50, reflection=0.1, transparency=0, refractiveIndex=1.0): super().__init__(v, color, specular, reflection, transparency, refractiveIndex) self.radius = radius self.height = height self.top = Vector(v.x, v.y + height / 2, v.z) self.bottom = Vector(v.x, v.y - height / 2, v.z) self.va = self.top.sub(self.center).normalize()
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
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 __init__(self, v=Vector(0, 0, 0), height=0, radius=0, color=Color(), specular=50, reflection=0.1, transparency=0, refractiveIndex=1.0): super().__init__(v, height, radius, color, specular, reflection, transparency, refractiveIndex) self.alpha = self.calculateAlpha() self.center = self.top self.bottom = Vector(self.center.x, self.center.y - self.height, self.center.z) self.top = Vector(self.center.x, self.center.y + self.height, self.center.z)
def deserializeColor(self, colorJson): r = float(colorJson["r"]) g = float(colorJson["g"]) b = float(colorJson["b"]) return Color(r, g, b)
from RayTracing.Classes.Models.Cone import Cone from RayTracing.Classes.Models.Cube import Cube from RayTracing.Classes.Models.Imageplane import Imageplane from RayTracing.Classes.Models.Light import Light from RayTracing.Classes.Models.Scene import Scene from RayTracing.Classes.Models.Sphere import Sphere from RayTracing.Classes.Models.Vector import Vector from RayTracing.Classes.Models.Plane import Plane from RayTracing.Classes.RayTracer import RayTracer if __name__ == '__main__': sCenter1 = Vector(2, 1, 10) sCenter2 = Vector(-2, 1, 13) p1 = Plane(Vector(0, -3, 0), Vector(0, 1, 0), Color(1, 0, 0), 0, 0) p2 = Plane(Vector(0, 10, 0), Vector(0, -1, 0), Color(0.1, 1.0, 1.0), 0, 0) p3 = Plane(Vector(0, 0, 30), Vector(0, 0, -1), Color(0.7, 0.1, 0.2), 0, 0) p4 = Plane(Vector(0, 0, -1), Vector(0, 0, 1), Color(0.1, 0.4, 0.8), 0, 0) p5 = Plane(Vector(-10, 0, 0), Vector(1, 0, 0), Color(0.2, 0.1, 0.6), 0, 0) 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)