Exemple #1
0
def color(ray, world):
    if world.hit(ray, 0, 1e9):
        return 0.5 * (world.register_hit.normal + Vec3D(1, 1, 1))
    else:
        unit_direction = ray.direction.unit_vector()
        t = 0.5 * (unit_direction.y + 1.0)
        return (1 - t) * Vec3D(1, 1, 1) + t * Vec3D(0.5, 0.7, 1)
Exemple #2
0
def hello_world_vec():
    # Second example from tutorial
    array_rgb = np.zeros((ny, nx, 3), dtype=np.uint8)
    for id, j in enumerate(reversed(range(ny))):
        for i in range(nx):
            array_rgb[id, i, :] = 255.99 * np.array(
                Vec3D(i / nx, j / ny, 0.2).vector())
    return array_rgb
Exemple #3
0
 def __mul__(self, other):
     '''Should handle multiplication by scalar'''
     if isinstance(other, Vec3D):
         # Normal dot product
         return Vec3D_base.__mul__(self, other)
     elif isinstance(other, (int, float)):
         # Multiply each component by the scalar
         x, y, z = self.coordinates
         return Vec3D(x*other, y*other, z*other)
Exemple #4
0
 def __mul__(self, other):
     '''Should handle multiplication by scalar'''
     if isinstance(other, Vec3D):
         # Normal dot product
         return Vec3D_base.__mul__(self, other)
     elif isinstance(other, (int, float)):
         # Multiply each component by the scalar
         x, y, z = self.coordinates
         return Vec3D(x * other, y * other, z * other)
Exemple #5
0
 def __sub__(self, other):
     '''Sub should now also handle scalars'''
     if isinstance(other, Vec3D):
         # Normal vector subtraction
         return Vec3D_base.__sub__(self, other)
     elif isinstance(other, (int, float)):
         # Subtract the scalar from each component
         x, y, z = self.coordinates
         return Vec3D(x - other, y - other, z - other)
Exemple #6
0
 def __sub__(self, other):
     '''Sub should now also handle scalars'''
     if isinstance(other, Vec3D):
         # Normal vector subtraction
         return Vec3D_base.__sub__(self, other)
     elif isinstance(other, (int, float)):
         # Subtract the scalar from each component
         x, y, z = self.coordinates
         return Vec3D(x-other, y-other, z-other)
Exemple #7
0
    def __add__(self, other):
        '''Add should now also handle scalars'''   
        if isinstance(other, Vec3D):
            # Normal vector addition
            return Vec3D_base.__add__(self, other)

        elif isinstance(other, (int, float)):
            # Add the scalar to each component
            x, y, z = self.coordinates
            return Vec3D(x+other, y+other, z+other)
Exemple #8
0
    def __add__(self, other):
        '''Add should now also handle scalars'''
        if isinstance(other, Vec3D):
            # Normal vector addition
            return Vec3D_base.__add__(self, other)

        elif isinstance(other, (int, float)):
            # Add the scalar to each component
            x, y, z = self.coordinates
            return Vec3D(x + other, y + other, z + other)
 def points(self):
     """
     The unique corner points of box (front + back order)
     """
     cpoints = [] 
     cpoints.append( Vec3D(-self.xd * 0.5 + self.xx, -self.yd * 0.5 + self.yy, -self.zd * 0.5 + self.zz) )  
     cpoints.append( Vec3D(+self.xd * 0.5 + self.xx, -self.yd * 0.5 + self.yy, -self.zd * 0.5 + self.zz) )
     cpoints.append( Vec3D(-self.xd * 0.5 + self.xx, +self.yd * 0.5 + self.yy, -self.zd * 0.5 + self.zz) )
     cpoints.append( Vec3D(+self.xd * 0.5 + self.xx, +self.yd * 0.5 + self.yy, -self.zd * 0.5 + self.zz) )
     cpoints.append( Vec3D(+self.xd * 0.5 + self.xx, -self.yd * 0.5 + self.yy, +self.zd * 0.5 + self.zz) )
     cpoints.append( Vec3D(-self.xd * 0.5 + self.xx, -self.yd * 0.5 + self.yy, +self.zd * 0.5 + self.zz) )
     cpoints.append( Vec3D(+self.xd * 0.5 + self.xx, +self.yd * 0.5 + self.yy, +self.zd * 0.5 + self.zz) )
     cpoints.append( Vec3D(-self.xd * 0.5 + self.xx, +self.yd * 0.5 + self.yy, +self.zd * 0.5 + self.zz) )
     return cpoints  
Exemple #10
0
 def randRay(self,sphere,poi=(0,0,0)):
     # 'create' a cube whose diagonal corners are at (0,0,0) and (radius,radius,radius)
     radius = sphere.getRad()
     r = radius/2
     while True: 
         x = random.random()*radius  #random x, y,and z the cube
         y = random.random()*radius
         z = random.random()*radius
         if ((r-x)*(r-x)+(r-y)*(r-y)+(r-z)*(r-z))<=(radius*radius):  #checks if that random point is also in the sphere
             break
     direction = sphere.getCent()
     direction = (direction[0]-r,direction[1]-r,direction[2]-r)
     
     return Vec3D((direction[0]+x)-poi[0],(direction[1]+y)-poi[1],(direction[2]+z)-poi[2])
Exemple #11
0
def something(param):
    '''Spheres! They are ordered such that in the first position
    they hold the radius, second is the center, and third is the color'''
    s3 = Sphere(1, (-2, 0, -3), (1.0, 0.1, 0.1))  # Left Sphere
    s1 = Sphere(1, (0, 0, -3), (.1, 1.0, 1.0), "sparkle")  # Center Sphere
    s2 = Sphere(1, (2, 0, -4), (0.0, 1.0, 0.1), None, 8)  # Right Sphere
    s4 = Sphere(98.5, (0, -100, 0), (1.0, 1.0, 1.0))  # Bottom Sphere
    s5 = Sphere(0.5, (-1, 1.2, -2.8), (1, 1, 1), "mirror")
    s6 = Sphere(0.5, (1, 1.2, -2.8), (0.0, 1.0, 1.0), "mirror")

    light = Sphere(1, (0, 10, 0), (1.0, 1.0, 1.0), "light")
    l2 = Sphere(1, (5, 10, 5), (1.0, 1.0, 1.0), "light")
    l3 = Sphere(1, (10, 10, 10), (1.0, 1.0, 1.0), "light")
    sArray = [s4, s3, s1, s2, s5, s6]
    lArray = [l3, l2, light]
    '''image stuffs'''

    checkHit = None
    i = param[0]
    j = param[1]

    closeSphere = None
    poi = None
    if (i % (width / 10) == 0 and j == i) or i == j == width - 1:
        print i / (width * 1.0)
    ray = Vec3D((-1 + 2 * (i / (width - 1.0))),
                (1.0 - 2.0 * (j / (height - 1.0))), -1.0)
    closest = float("inf")
    for sphere in sArray:
        checkHit = ray.sphereInt(sphere)
        if checkHit != None:
            if checkHit < closest:
                closeSphere = sphere
                closest = checkHit
                poi = ray.poi(checkHit)
    '''Background'''
    if closeSphere == None:
        return (0, int(256 * ((0.2 * (1 - ray.gety())) - 0.0000001)),
                int(256 * (0.1 * 0.0000001)))
    else:
        color = closeSphere.getColor(0, ray, poi, sArray, lArray)
        return (int(256 * (color[0] - 0.0000001)),
                int(256 * (color[1] - 0.0000001)),
                int(256 * (color[2] - 0.0000001)))
Exemple #12
0
 def getColor(self, iterations, ray, poi=None, sphereSet=None):
     #         sColor = Vec3D(self.color[0],self.color[1],self.color[2])
     normal = ray.normU(poi, self.center)
     if iterations > 10:
         return (1, 1, 1)
     elif self.classification == None:
         shadowV = Vec3D(0, 1, 0)
         color = None
         for subS in sphereSet:
             if subS != self:
                 if not subS.isLight():
                     shadow = shadowV.sphereInt(subS, poi)
                     if shadow != None:
                         color = (0, 0, 0)
         if color == None:
             color = (self.color[0] * normal.gety(),
                      self.color[1] * normal.gety(),
                      self.color[2] * normal.gety())
         return color
     elif self.classification == "mirror":
         reflect = normal.multiply(2 * ray.dot(normal))
         reflect = ray.subtract(reflect)
         closest = float("inf")
         for other in sphereSet:
             if other != self:
                 checkHit = reflect.sphereInt(other, poi)
                 if checkHit == None and closest == float("inf"):
                     color = (0, ((0.2 * (1 - reflect.gety())) - 0.0000001),
                              (0.1 * 0.0000001))
                 elif checkHit != None:
                     if checkHit < closest:
                         closest = checkHit
                         newPOI = reflect.poi(checkHit, poi)
                         color = other.getColor(iterations + 1, reflect,
                                                newPOI, sphereSet)
         return color
     else:
         #Assumes sphere is a light sphere
         return (1.0, 1.0, 1.0)
Exemple #13
0
def first_render():
    # Third example from tutorial
    array_rgb = np.zeros((ny, nx, 3), dtype=np.uint8)
    lower_left_corner = Vec3D(-2, -1, -1)
    horizontal = Vec3D(4, 0, 0)
    vertical = Vec3D(0, 2, 0)
    origin = Vec3D(0, 0, 0)
    world = EntityList(
        [Sphere(Vec3D(0, 0, -1), 0.5),
         Sphere(Vec3D(0, -100.5, -1), 100)])
    for id, j in enumerate(reversed(range(ny))):
        for i in range(nx):
            u = i / nx
            v = j / ny
            r = Ray(origin, lower_left_corner + u * horizontal + v * vertical)
            array_rgb[id, i, :] = 255.99 * np.array(color(r, world).vector())
    return np.array(array_rgb)
"""
box.py contains use lighweight Vec3D class to store indexed vector points 
by having public attributes (as does python default)
"""
from vec3d import Vec3D
from mesh import Mesh

normals = [ Vec3D(0, 0, 1),   
    Vec3D(0, 0, -1), 
    Vec3D(1, 0, 0), 
    Vec3D(-1, 0, 0), 
    Vec3D(0, 1, 0),  
    Vec3D(0, -1, 0)]

class Box(object):
    """
    A lightweight convenience class to store box point data
    With a function to return indexed vector points 
    """

    
    def __init__(self, x = 0, y = 0,  z = 0,  xd = 1,  yd = 1,  zd = 1):
        """
        Initialise the new Box object
        """
        self.xx= x
        self.yy = y 
        self.zz = z 
        self.xd = xd
        self.yd = yd
        self.zd = zd
Exemple #15
0
def something(param):
    n = param[2]
    n = n + start
    yDic = {
    }  # s1  s2  s3  s5        R       O     `  Y       G       B       I       V
    yDic[0] = [-.4, .75, -.1, 1, -3, 20, 20, 20, 20, 20, 20]
    yDic[1] = [-0.9, 0.3, .65, 1.5, -2.5, 20, 20, 20, 20, 20, 20]
    yDic[2] = [-.45, -.45, 1.3, 2, -2, -3, 20, 20, 20, 20, 20]
    yDic[3] = [0, -1, .95, 2.5, -1.5, -2.5, 20, 20, 20, 20, 20]
    yDic[4] = [.45, -.45, .6, 3, -1, -2, -3, 20, 20, 20, 20]
    yDic[5] = [0.9, 0, 0.5, 3.5, -.5, -1.5, -2.5, 20, 20, 20, 20]
    yDic[6] = [.6, .55, -.1, 20, 0, -1, -2, -3, 20, 20, 20]
    yDic[7] = [.3, 1.1, -.8, 20, .5, -.5, -1.5, -2.5, 20, 20, 20]
    yDic[8] = [-.2, .85, -.45, 20, 1, 0, -1, -2, -3, 20, 20]
    yDic[9] = [-.8, 0.6, 0.1, 20, 1.5, .5, -.5, -1.5, -2.5, 20, 20]
    yDic[10] = [-.35, -.15, .55, 20, 2, 1, 0, -1, -2, -3, 20]
    yDic[11] = [0.1, -0.95, 1.2, 20, 2.5, 1.5, .5, -.5, -1.5, -2.5, 20]
    yDic[12] = [.5, -.45, .85, 20, 3, 2, 1, 0, -1, -2, -3]
    yDic[13] = [0.9, 0.0, 0.5, 20, 20, 2.5, 1.5, .5, -.5, -1.5, -2.5]
    yDic[14] = [.5, .6, -.25, 20, 20, 3, 2, 1, 0, -1, -2]
    yDic[15] = [0.1, 1.2, -0.85, 20, 20, 20, 2.5, 1.5, .5, -.5, -1.5]
    yDic[16] = [-.4, .75, -.35, 20, 20, 20, 3, 2, 1, 0, -1]
    yDic[17] = [-0.9, 0.3, 0.1, 20, 20, 20, 20, 2.5, 1.5, .5, -.5]
    yDic[18] = [-.45, -.45, .65, 20, 20, 20, 20, 3, 2, 1, 0]
    yDic[19] = [0, -1, 1.2, 20, 20, 20, 20, 20, 2.5, 1.5, .5]
    yDic[20] = [-.4, .75, -.1, 1, 20, 20, 20, 20, 3, 2, 1]
    yDic[21] = [-0.9, 0.3, .65, 1.5, 20, 20, 20, 20, 20, 2.5, 1.5]
    yDic[22] = [-.45, -.45, 1.3, 2, 20, 20, 20, 20, 20, 3, 2]
    yDic[23] = [0, -1, .95, 2.5, 20, 20, 20, 20, 20, 20, 2.5]
    yDic[24] = [.45, -.45, .6, 3, 20, 20, 20, 20, 20, 20, 3]
    yDic[25] = [0.9, 0, 0.5, 3.5, 20, 20, 20, 20, 20, 20, 20]

    coords = yDic[n]
    '''Spheres! They are ordered such that in the first position
    they hold the radius, second is the center, and third is the color'''
    s3 = Sphere(1, (-2, coords[2], -3), (1, 1, 1), "mirror")  # Left Sphere
    s1 = Sphere(1, (0, coords[0], -3), (1, 1, 1), "mirror")  # Center Sphere
    s2 = Sphere(1, (2, coords[1], -4), (0.5, 1.0, .5))  # Right Sphere
    s4 = Sphere(98.5, (0, -100, 0), (1.0, 1.0, 1.0))  # Bottom Sphere
    s5 = Sphere(.4, (coords[3], -1.1, -2), (0.0, 1.0, 1.0))

    red = Sphere(.4, (coords[4], -1.1, -2), (1.0, 0, 0))
    orange = Sphere(.4, (coords[5], -1.1, -2), (1.0, 0.6, 0))
    yellow = Sphere(.4, (coords[6], -1.1, -2), (1.0, 1.0, 0.0))
    green = Sphere(.4, (coords[7], -1.1, -2), (0.0, 1.0, 0.0))
    blue = Sphere(.4, (coords[8], -1.1, -2), (0.0, 0.0, 1.0))
    indigo = Sphere(.4, (coords[9], -1.1, -2), (0.44, 0.0, 1.0))
    violet = Sphere(.4, (coords[10], -1.1, -2), (0.93, .5, 0.93))

    light = Sphere(1, (0, 10, 0), (1.0, 1.0, 1.0), "light")
    l2 = Sphere(1, (5, 10, 5), (1.0, 1.0, 1.0), "light")
    l3 = Sphere(1, (10, 10, 10), (1.0, 1.0, 1.0), "light")

    # if param[3] <.25:
    #     which = param[4]
    #     if which == 0:
    #         red = Sphere(.4,(coords[4],-.9,-2),(1.0,0,0))
    #     if which == 1:
    #         orange = Sphere(.4,(coords[5],-.9,-2),(1.0,0.6,0))
    #     if which == 2:
    #         yellow =Sphere(.4,(coords[6],-.9,-2),(1.0,1.0,0.0))
    #     if which == 3:
    #         green = Sphere(.4,(coords[7],-.9,-2),(0.0,1.0,0.0))
    #     if which == 4:
    #         blue = Sphere(.4,(coords[8],-.9,-2),(0.0,0.0,1.0))
    #     if which == 5:
    #         indigo = Sphere(.4,(coords[9],-.9,-2),(0.44,0.0,1.0))
    #     if which == 6:
    #         violet = Sphere(.4,(coords[10],-.9,-2),(0.93,.5,0.93))
    sArray = [
        s4, s3, s1, s2, s5, red, orange, yellow, green, blue, indigo, violet
    ]
    lArray = [light]

    if n > 5:
        lArray = [light, l2]
        if n > 10:
            lArray = [light, l2, l3]

    checkHit = None
    i = param[0]
    j = param[1]

    closeSphere = None
    poi = None
    if (i % (width / 10) == 0 and j == i) or i == j == width - 1:
        print i / (width * 1.0)
    ray = Vec3D((-1 + 2 * (i / (width - 1.0))),
                (1.0 - 2.0 * (j / (height - 1.0))), -1.0)
    closest = float("inf")
    for sphere in sArray:
        checkHit = ray.sphereInt(sphere)
        if checkHit != None:
            if checkHit < closest:
                closeSphere = sphere
                closest = checkHit
                poi = ray.poi(checkHit)
    '''Background'''
    if closeSphere == None:
        return (0, int(256 * ((0.2 * (1 - ray.gety())) - 0.0000001)),
                int(256 * (0.1 * 0.0000001)))
    else:
        color = closeSphere.getColor(0, ray, poi, sArray, lArray)
        return (int(256 * (color[0] - 0.0000001)),
                int(256 * (color[1] - 0.0000001)),
                int(256 * (color[2] - 0.0000001)))
Exemple #16
0
 l2 = Sphere(1, (5, 10, 5), (1.0, 1.0, 1.0), "light")
 sArray = [s4, s3, s1, s2]
 lArray = [light, l2]
 '''image stuffs'''
 image = Image.new('RGB', (width, height))
 image.putpixel((0, 0), (255, 255, 255))
 checkHit = None
 '''actual creating of the image'''
 for i in range(width):
     for j in range(height):
         #             print "pixel: ", i, ", ", j
         closeSphere = None
         poi = None
         if i % 10 == 0 and j == i:
             print i / (width * 1.0)
         ray = Vec3D((-1 + 2 * (i / (width - 1.0))),
                     (1.0 - 2.0 * (j / (height - 1.0))), -1.0)
         closest = float("inf")
         for sphere in sArray:
             checkHit = ray.sphereInt(sphere)
             if checkHit != None:
                 if checkHit < closest:
                     closeSphere = sphere
                     closest = checkHit
                     poi = ray.poi(checkHit)
         '''Background'''
         if closeSphere == None:
             image.putpixel(
                 (i, j),
                 (0, int(256 * ((0.2 * (1 - ray.gety())) - 0.0000001)),
                  int(256 * (0.1 * 0.0000001))))
         else:
Exemple #17
0
from vec3d import Vec3D
u = Vec3D(1, 0, 0)
v = Vec3D(0, 1, 0)

print 'str(u): ', str(u)
print 'repr(v): ', repr(v)
print 'u[1] = ', u[1]
v[2] = 2.5
print 'set v[2] = :', v[2]
d = u + v
print 'u+v = ', d
e = u - v
print 'u-v = ', e
f = u**v
print 'cross product u x v = ', f
print 'scalar product u*v = ', u * v

print 'u.len() = ', u.len()
Exemple #18
0
    yDic[36] = [.6,.55,-.1,20,1,.5,0,-.5,-1,-1.5,-2]
    yDic[37] = [.3,1.1,-.8,20,1.5,1,.5,0,-.5,-1,-1.5]
    yDic[38] = [-.2,.85,-.45,20,2,1.5,1,.5,0,-.5,-1]
    yDic[39] = [-.8,0.6,0.1,20,2.5,2,1.5,1,.5,0,-.5]
    yDic[40] = [-.35,-.15,.55,20,3,2.5,2,1.5,1,.5,0]
    yDic[41] = [0.1,-0.95,1.2,20,20,3,2.5,2,1.5,1,.5]
    yDic[42] = [.5,-.45,.85,20,20,20,3,2.5,2,1.5,1]
    yDic[43] = [0.9,0.0,0.5,20,20,20,20,3,2.5,2,1.5]
    yDic[44] = [.5,.6,-.25,20,20,20,20,20,3,2.5,2]
    yDic[45] = [0.1,1.2,-0.85,20,20,20,20,20,20,3,2.5]
    yDic[46] = [-.4,.75,-.35,20,20,20,20,20,20,20,3]
    yDic[19] = [-0.9,0.3,0.1]
    yDic[20] = [-.45,-.45,.65]
    yDic[21] = [0,-1,1.2]

    shadowV = Vec3D(0,1,0)
    n = 41
    while n<47:
        coords = yDic[n]
        '''Spheres! They are ordered such that in the first position
        they hold the radius, second is the center, and third is the color'''
        s1 = Sphere(1,(0,coords[0],-3),(1.0,0.5,0.5)) #Red
        s2 = Sphere(1,(2,coords[1],-4),(0.5,1.0,0.5)) #Green
        s3 = Sphere(1,(-2,coords[2],-3),(0.5,.5,1.0)) #Blue
        s4 = Sphere(98.5,(0,-100,0),(1.0,1.0,1.0)) #White
        light = Sphere(1,(0,10,0),(1.0,1.0,1.0),"light")
        if n > 15:
            s3 = Sphere(1,(-2,coords[2],-3),(0.5,.5,1.0),"mirror") #Blue
            if n > 18:
                s1 = Sphere(1,(0,coords[0],-3),(1.0,0.5,0.5),"mirror") #Red
                s5 = Sphere(.4,(coords[3],-1.1,-2),(0.0,1.0,1.0))
Exemple #19
0
     s2[0], (s2[1][0] + s2randx, s2[1][1] + s2randy, s2[1][2]),
     (0, 1, 0)
 ]
 s3 = [
     s3[0], (s3[1][0] + s3randx, s3[1][1] + s3randy, s3[1][2]),
     (0, 0, 1)
 ]
 s4 = [
     s4[0], (s4[1][0] + s4randx, s4[1][1] + s4randy, s4[1][2]),
     (1, 1, 1)
 ]
 sArray = [s1, s2, s3, s4]
 '''actual creating of the image'''
 for i in range(width):
     for j in range(height):
         ray = Vec3D((-1 + 2 * (i / (width - 1.0))),
                     (-1.0 + 2.0 * (j / (height - 1.0))), -1.0)
         closest = float("inf")
         for sphere in sArray:
             checkHit = ray.sphereInt(sphere[0], sphere[1])
             if checkHit != None and checkHit < closest:
                 closest = checkHit
                 color = sphere[2]
         if closest == float("inf"):
             image.putpixel(
                 (i, j),
                 (0, int(256 * ((0.2 * (1 + ray.gety())) - 0.0000001)),
                  int(256 * (0.1 * 0.0000001))))
         else:
             nx = ray.getx() * closest
             ny = ray.gety() * closest
             nz = ray.getz(