Example #1
0
 def __init__(self, in_stream):
     for line in in_stream:
         if not line.isspace():
             p, d, a = SEARCH.search(line).groups()
             self.view_position = Vector3f_str(p)
             self.view_direction = Vector3f_str(d).unitize()
             if self.view_direction.is_zero():
                 self.view_direction = Vector3f(0.0, 0.0, 1.0)
             self.view_angle = min(max(10.0, float(a)), 160.0) * (pi / 180.0)
             self.right = Vector3f(0.0, 1.0, 0.0).cross(self.view_direction).unitize()
             if self.right.is_zero():
                 self.up = Vector3f(0.0, 0.0, 1.0 if self.view_direction.y else -1.0)
                 self.right = self.up.cross(self.view_direction).unitize()
             else:
                 self.up = self.view_direction.cross(self.right).unitize()
             break
 def __init__(self, arg, items, level=0):
     if type(arg) == Vector3f:
         items = [(item.get_bound(), item) for item in items]
         bound = list(arg) * 2
         for item in items:
             for j in range(6):
                 if (bound[j] > item[0][j]) ^ (j > 2):
                     bound[j] = item[0][j]
         size = max(list(Vector3f(bound[3:6]) - Vector3f(bound[0:3])))
         self.bound = bound[0:3] + list(
             Vector3f(bound[3:6]).clamped(
                 Vector3f(bound[0:3]) + Vector3f(size), MAX))
     else:
         self.bound = arg
     self.is_branch = len(items) > MAX_ITEMS and level < MAX_LEVELS - 1
     if self.is_branch:
         q1 = 0
         self.vector = [None] * 8
         for s in range(8):
             sub_bound = []
             for j in range(6):
                 m = j % 3
                 if (((s >> m) & 1) != 0) ^ (j > 2):
                     sub_bound.append(
                         (self.bound[m] + self.bound[m + 3]) * 0.5)
                 else:
                     sub_bound.append(self.bound[j])
             sub_items = []
             for item in items:
                 item_bound = item[0]
                 if item_bound[3] >= sub_bound[0] and \
                    item_bound[0] < sub_bound[3] and \
                    item_bound[4] >= sub_bound[1] and \
                    item_bound[1] < sub_bound[4] and \
                    item_bound[5] >= sub_bound[2] and \
                    item_bound[2] < sub_bound[5]:
                     sub_items.append(item)
             q1 += 1 if len(sub_items) == len(items) else 0
             q2 = (sub_bound[3] - sub_bound[0]) < (TOLERANCE * 4.0)
             if len(sub_items) > 0:
                 self.vector[s] = SpatialIndex(
                     sub_bound, sub_items,
                     MAX_LEVELS if q1 > 1 or q2 else level + 1)
     else:
         self.vector = [item[1] for item in items]
Example #3
0
 def calculate_tone_mapping(self, pixels, divider):
     sum_of_logs = 0.0
     for i in range(len(pixels) / 3):
         y = Vector3f(pixels[i * 3:i * 3 + 3]).dot(RGB_LUMINANCE) * divider
         sum_of_logs += log10(y if y > 1e-4 else 1e-4)
     adapt_luminance = 10.0**(sum_of_logs / (len(pixels) / 3))
     a = 1.219 + (DISPLAY_LUMINANCE_MAX * 0.25)**0.4
     b = 1.219 + adapt_luminance**0.4
     return ((a / b)**2.5) / DISPLAY_LUMINANCE_MAX
Example #4
0
 def __init__(self, x, y, z):
     self.x = x
     self.y = y
     self.z = z
     self.position = Vector3f(x + 0.5, y + 0.5, z + 0.5)
     self.__init_vertices()
     self.__init_indices()
     self.__init_normals()
     self.__init_colors()
     self.on = False
Example #5
0
 def __init__(self, in_stream, eye_position):
     for l in in_stream:
         if type(l) == type(u""):
             line = l.encode('ascii', 'ignore')
         else:
             line = l
         if not line.isspace():
             s, g = SEARCH(line).groups()
             self.sky_emission = Vector3f(s).clamped(ZERO, MAX)
             self.ground_reflection = Vector3f(g).clamped(ZERO, ONE)
             self.triangles = []
             try:
                 for i in range(MAX_TRIANGLES):
                     self.triangles.append(Triangle(in_stream))
             except StopIteration:
                 pass
             self.emitters = [
                 triangle for triangle in self.triangles
                 if not triangle.emitivity.is_zero() and triangle.area > 0.0
             ]
             self.index = SpatialIndex(eye_position, self.triangles)
             self.get_intersection = self.index.get_intersection
             break
Example #6
0
 def __init__(self, in_stream):
     for l in in_stream:
         if type(l) == type(u""):
             line = l.encode('ascii', 'ignore')
         else:
             line = l
         if not line.isspace():
             p, d, a = SEARCH(line).groups()
             self.view_position = Vector3f(p)
             self.view_direction = Vector3f(d).unitize()
             if self.view_direction.is_zero():
                 self.view_direction = Vector3f(0.0, 0.0, 1.0)
             self.view_angle = min(max(VIEW_ANGLE_MIN, float(a)),
                                   VIEW_ANGLE_MAX) * (pi / 180.0)
             self.right = Vector3f(0.0, 1.0, 0.0).cross(
                 self.view_direction).unitize()
             if self.right.is_zero():
                 self.up = Vector3f(0.0, 0.0,
                                    1.0 if self.view_direction.y else -1.0)
                 self.right = self.up.cross(self.view_direction).unitize()
             else:
                 self.up = self.view_direction.cross(self.right).unitize()
             break
Example #7
0
 def __init__(self, in_stream):
     for l in in_stream:
         if type( l ) == type( u"" ):
             line = l.encode('ascii','ignore')
         else:
             line = l
         if not line.isspace():
             v0, v1, v2, r, e = SEARCH(line).groups()
             self.vertexs = map(Vector3f, [v0, v1, v2])
             self.edge0 = Vector3f(v1) - Vector3f(v0)
             self.edge3 = Vector3f(v2) - Vector3f(v0)
             self.reflectivity = Vector3f(r).clamped(ZERO, ONE)
             self.emitivity = Vector3f(e).clamped(ZERO, MAX)
             edge1 = Vector3f(v2) - Vector3f(v1)
             self.tangent = self.edge0.unitize()
             self.normal = self.tangent.cross(edge1).unitize()
             pa2 = self.edge0.cross(edge1)
             self.area = sqrt(pa2.dot(pa2)) * 0.5
             return
     raise StopIteration
Example #8
0
    def sample_radiance(self, x, y, w, h, aspect, camera, scene, num_samples):
        acc_radiance = [0.0, 0.0, 0.0]

        for i in range(num_samples):
            x_coefficient = ((x + self.random.real64()) * 2.0 / w) - 1.0
            y_coefficient = ((y + self.random.real64()) * 2.0 / h) - 1.0

            offset = camera.right * x_coefficient + camera.up * (
                y_coefficient * aspect)

            sample_direction = (
                camera.view_direction +
                (offset * tan(camera.view_angle * 0.5))).unitize()

            radiance = self.raytracer.get_radiance(camera.view_position,
                                                   sample_direction,
                                                   self.random)

            acc_radiance[0] += radiance[0]
            acc_radiance[1] += radiance[1]
            acc_radiance[2] += radiance[2]

        return Vector3f(acc_radiance[0], acc_radiance[1], acc_radiance[2])
Example #9
0
    def pixel_accumulated_radiance(self, scene, random, width, height, x, y,
                                   aspect, num_samples):
        raytracer = RayTracer(scene)
        acc_radiance = [0.0, 0.0, 0.0]

        for i in range(num_samples):
            x_coefficient = ((x + random.real64()) * 2.0 / width) - 1.0
            y_coefficient = ((y + random.real64()) * 2.0 / height) - 1.0

            offset = self.right * x_coefficient + self.up * (y_coefficient *
                                                             aspect)

            sample_direction = (
                self.view_direction +
                (offset * tan(self.view_angle * 0.5))).unitize()

            radiance = raytracer.get_radiance(self.view_position,
                                              sample_direction, random)

            acc_radiance[0] += radiance[0]
            acc_radiance[1] += radiance[1]
            acc_radiance[2] += radiance[2]

        return Vector3f(acc_radiance[0], acc_radiance[1], acc_radiance[2])
Example #10
0
#  MiniLight Python : minimal global illumination renderer
#
#  Harrison Ainsworth / HXA7241 and Juraj Sukop : 2007-2008, 2013.
#  http://www.hxa.name/minilight

from math import log10
from vector3f import Vector3f

IMAGE_DIM_MAX = 4000
PPM_ID = 'P6'
MINILIGHT_URI = 'http://www.hxa.name/minilight'
DISPLAY_LUMINANCE_MAX = 200.0
RGB_LUMINANCE = Vector3f(0.2126, 0.7152, 0.0722)
GAMMA_ENCODE = 0.45


class Img(object):
    def __init__(self, w, h):
        self.width = w
        self.height = h
        self.pixels = [0.0] * self.width * self.height * 3

    def copyPixels(self, data):
        length = len(self.pixels)
        if len(data) != length:
            raise AttributeError("Data length should be: {}".format(length))

        i = 0
        for y in range(self.height):
            offset = 3 * (self.width * (self.height - 1 - y))
            for x in range(3 * self.width):
Example #11
0
 def __init__(self, triangle, position):
     self.triangle_ref = triangle
     self.position = Vector3f(position)