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_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_intersection(self): cube = Cube(Vector(0, 0, 5), 2, 10, 10) ray = Ray(Vector(0, 0, 0), Vector(0, 0.25, 1)) intersection = cube.intersection(ray, 1, 10000) self.assertEqual(intersection.point.x, 0) self.assertEqual(intersection.point.y, 1) self.assertEqual(intersection.point.z, 4)
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_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_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_NoIntersection(self): startPoint = Vector(1, 1, 4) direction = Vector(2, 3, 1) line = Ray(startPoint, direction) center = Vector(5.0, 1, 3) sphere = Sphere(center, 2) intersection = sphere.intersection(line, 0, math.inf) self.assertIsNone(intersection)
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, point=Vector(), object3d=Object3D(), ray=Ray(), distance=0): self.point = point self.object = object3d self.ray = ray self.distance = distance
def test_intersection(self): startPoint = Vector(0, 0, 0) direction = Vector(2.0, 2.0, 0) line = Ray(startPoint, direction) center = Vector(5.0, 3.0, 0.0) sphere = Sphere(center, 2.0) intersection = sphere.intersection(line, 0, math.inf) self.assertEqual(intersection.point.x, 3) self.assertEqual(intersection.point.y, 3) self.assertEqual(intersection.point.z, 0)
def getRefraction(self, intersection, recursionDepth): """ Cast a new ray to calculate refraction from a point of an object """ if intersection.getObject().getTransparency() <= 0: return None refractedColor = self.backgroundColor if intersection.getObject().getTransparency( ) > 0 and recursionDepth < self.recursionLimit: refractedColor = self.backgroundColor originalDirection = intersection.getRay().getDirection() surfaceNormal = intersection.getObject().getSurfaceNormal( intersection.getPoint()) ior = intersection.getObject().getRefractiveIndex() cosi = originalDirection.dotProduct(surfaceNormal) etai = 1 etat = ior if cosi < -1: cosi = -1 elif cosi > 1: cosi = 1 if cosi >= 0: etai = ior etat = 1 surfaceNormal = surfaceNormal.getNegative() else: cosi = -cosi eta = etai / etat k = 1 - eta * eta * (1 - cosi * cosi) if k < 0: return refractedColor else: refractionDirection = originalDirection.multiply(eta).add( surfaceNormal.multiply(eta * cosi - math.sqrt(k))) refractionDirection = refractionDirection.normalize() refractionRay = Ray(intersection.getPoint(), refractionDirection) refractedColor = self.traceRay(refractionRay, recursionDepth + 1) return refractedColor
def getReflection(self, intersection, recursionDepth): """ Cast a new ray to calculate reflections from a point of an object""" if intersection.getObject().getReflection() <= 0: return None reflectedColor = self.backgroundColor if intersection.getObject().getReflection( ) > 0 and recursionDepth < self.recursionLimit: originalDirection = intersection.getRay().getDirection() surfaceNormal = intersection.getObject().getSurfaceNormal( intersection.getPoint()) surfNormDotNCD = surfaceNormal.dotProduct(originalDirection) reflectionDirection = originalDirection.sub( surfaceNormal.multiply(2 * surfNormDotNCD)) reflectionDirection = reflectionDirection.normalize() reflectionRay = Ray(intersection.getPoint(), reflectionDirection) reflectedColor = self.traceRay(reflectionRay, recursionDepth + 1) return reflectedColor
def getRay(self, x, y): part1 = self.up.multiply(self.w * x) part2 = self.right.multiply(self.h * y) return Ray(self.position, self.forward.add(part1).add(part2))
def test_constructor(self): ray = Ray(Vector(0, 0, 0), Vector(0, 0, 1))
def getLightRay(self, point): lightRay = Ray(self.position, point) return lightRay
def test_intersection(self): cube = Cube(Vector(0, 0, 5), 2, 10, 10) ray = Ray(Vector(0, 0, 0), Vector(0, 0.25, 1)) intersection = cube.intersection(ray)
def test_constructor(self): ray = Ray(Vector(0, 0, 0), Vector(0, 0, 1)) self.assertEqual(ray.direction.x, 0)
def test_intersection3(self): cylinder = Cylinder(Vector(0, 0, 5), 2, 1, Color().red(), 10, 0.1) ray = Ray(Vector(0, 0, 0), Vector(0, 1, 1)) intersection = cylinder.intersection(ray, 0, 1000) self.assertIsNone(intersection)
def test_intersection4(self): cylinder = Cylinder(Vector(0, 8, 14), 10, 1.5, Color().red(), 10, 0.1) ray = Ray(Vector(0, 0, 0), Vector(0.0, 1.8, 1)) intersection = cylinder.intersection(ray, 0, 1000) self.assertIsNone(intersection)
def test_intersection3(self): cone = Cone(Vector(0, 0, 5), 3, 1, Color(1, 0, 0)) ray = Ray(Vector(0, 0, 0), Vector(0.0, 1.0, 1)) intersection = cone.intersection(ray, 0.00001, 1000) self.assertIsNone(intersection)