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_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_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_add(self): v = Vector(3, 5, 1) b = Vector(1, 0, 2) r = v.add(b) self.assertEqual(r.x, 4) self.assertEqual(r.y, 5) self.assertEqual(r.z, 3)
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_sub(self): v = Vector(3, 5, 1) b = Vector(1, 0, 2) r = v.sub(b) self.assertEqual(r.x, 2) self.assertEqual(r.y, 5) self.assertEqual(r.z, -1)
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_crossProduct(self): v = Vector(3, 2, 5) b = Vector(4, 1, 2) r = v.crossProduct(b) self.assertEqual(r.x, -1) self.assertEqual(r.y, 14) self.assertEqual(r.z, -5)
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_getInverse(self): v = Vector(0, 0, 0.5) invV = v.getInverse() self.assertEqual(invV.x, sys.float_info.max) self.assertEqual(invV.y, sys.float_info.max) self.assertEqual(invV.z, 2)
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 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_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_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 __init__(self, position=Vector(0, 0, 0), pointOfView=Vector(0, 0, 1), upGuide=Vector(1, 0, 0), fov=40): self.position = position self.pointOfView = pointOfView self.forward = self.pointOfView.sub(self.position).normalize() self.fov = fov self.right = self.forward.crossProduct(upGuide).normalize() self.up = self.right.crossProduct(self.forward) self.h = math.tan(fov) self.w = self.h * 1 self.angle = self.calculateAngle()
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 __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 test_getLightVector(self): point = Vector(7, 3, 1) light = Light(2, 2, 2, 1) lightray = light.getLightVector(point) self.assertEqual(lightray.x, -5) self.assertEqual(lightray.y, -1) self.assertEqual(lightray.z, 1)
def __init__(self, startp=Vector(0, 0, 0), directionPoint=Vector(0, 0, 0)): self.startPoint = startp self.direction = directionPoint #self.direction = directionPoint.sub(startp) self.inverseDirection = self.direction.getInverse() self.inv = list() self.inv.append(int(self.inverseDirection.x < 0)) self.inv.append(int(self.inverseDirection.y < 0)) self.inv.append(int(self.inverseDirection.z < 0)) self.inverseDirection = self.direction.getInverse() self.inv = list() self.inv.append(int(self.inverseDirection.x < 0)) self.inv.append(int(self.inverseDirection.y < 0)) self.inv.append(int(self.inverseDirection.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_constructor(self): v = Vector() v.setX(3) v.setY(2) self.assertEqual(v.x, 3) self.assertEqual(v.y, 2) self.assertEqual(v.z, 0) v.setZ(1) self.assertEqual(v.z, 1)
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 __init__(self, point=Vector(), object3d=Object3D(), ray=Ray(), distance=0): self.point = point self.object = object3d self.ray = ray self.distance = distance
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 intersection(self, ray, tMin, tMax): deltaP = ray.startPoint.sub(self.center) partA = ray.direction.sub( self.va.multiply(ray.direction.dotProduct(self.va))) a = partA.dotProduct(partA) dotProd = deltaP.dotProduct(self.va) partB = deltaP.sub(self.va.multiply(dotProd)) b = 2 * partA.dotProduct(partB) c = partB.dotProduct(partB) - self.radius * self.radius t = MathUtil().solveQuadraticFormula(a, b, c) intersect = None if t is not None: tSmallest = math.inf if tMin < t.getx1() < tMax and t.getx1() < tSmallest: tSmallest = t.getx1() if tMin < t.getx2() < tMax and t.getx2() < tSmallest: tSmallest = t.getx2() if tSmallest == math.inf: return None point = ray.getPointOfRay(tSmallest) intersect = Intersection(point, self, ray, tSmallest) if not self.inCylinder(point): tmp = None if self.isAbove(point): tmp = self.intersect_base(ray, self.top, Vector(0, 1, 0)) else: tmp = self.intersect_base(ray, self.bottom, Vector(0, -1, 0)) if tmp: intersect.point = tmp.point else: intersect = None return intersect
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 test_equals(self): v = Vector(3, 2, 5) b = Vector(4, 1, 2) v2 = Vector(3, 2, 5) vb = v.equals(b) vv = v.equals(v2) self.assertFalse(vb) self.assertTrue(vv)
def getSurfaceNormal(self,point): surfaceNormal = Vector() epsilon = 0.00001 if point.y > self.center.y - epsilon: surfaceNormal.y = 1 return Vector(0.0, 1.0, 0.0) elif self.bottom.y - epsilon < point.y < self.bottom.y + epsilon: surfaceNormal.y = -1 return Vector(0.0, -1.0, 0.0) x = point.x - self.center.x z = point.z - self.center.z r = math.sqrt(math.pow(x, 2) + math.pow(z, 2)) surfaceNormal = Vector(x, r * (self.radius / self.height), z) return surfaceNormal.normalize()
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