Permalink
Browse files

made the code faster by ditching numpy.

  • Loading branch information...
1 parent 8d59ddd commit 2cb6e2b31bc75a21a121ed9c7a46b1a3113fcab0 @stefanoborini committed Jul 2, 2011
Showing with 65 additions and 40 deletions.
  1. +2 −4 raytrace/Ray.py
  2. +0 −1 raytrace/ViewPlane.py
  3. +17 −10 raytrace/World.py
  4. +3 −0 raytrace/math.py
  5. +13 −13 raytrace/objects/Plane.py
  6. +28 −11 raytrace/objects/Sphere.py
  7. +1 −1 raytrace/objects/__init__.py
  8. +1 −0 raytrace/samplers.py
View
@@ -1,7 +1,5 @@
-import numpy
-
class Ray(object):
def __init__(self, origin, direction):
- self.origin = numpy.array(origin)
- self.direction = numpy.array(direction)
+ self.origin = origin
+ self.direction = direction
View
@@ -1,4 +1,3 @@
-import numpy
from . import Ray
class ViewPlane(object):
View
@@ -6,7 +6,6 @@
import pygame
import itertools
import math
-import numpy
import sys
@@ -28,20 +27,27 @@ def render(self):
pxarray = pygame.PixelArray(window)
im = Image.new("RGB", self.viewplane.resolution)
tracer = Tracer.Tracer(self)
+
+ num_samples = self.sampler.num_samples_per_set()
for row in self.viewplane:
for pixel in row:
- print pixel
imagePxPos = (pixel[0], self.viewplane.resolution[1]-pixel[1]-1)
- color = numpy.array([0.0, 0.0, 0.0])
+ color = (0.0, 0.0, 0.0)
for subsample in itertools.islice(self.sampler,self.sampler.num_samples_per_set()):
- origin = numpy.zeros(3)
- origin[0] = self.viewplane.pixel_size*(pixel[0] - self.viewplane.resolution[0] / 2 + subsample[0])
- origin[1] = self.viewplane.pixel_size*(pixel[1] - self.viewplane.resolution[1] / 2 + subsample[1])
- origin[2] = 100.0
+ origin = ( self.viewplane.pixel_size*(pixel[0] - self.viewplane.resolution[0] / 2 + subsample[0]),
+ self.viewplane.pixel_size*(pixel[1] - self.viewplane.resolution[1] / 2 + subsample[1]),
+ 100.0)
+
ray = Ray.Ray(origin = origin, direction = (0.0,0.0,-1.0))
- color += numpy.array(tracer.trace_ray(ray))
- color /= self.sampler.num_samples_per_set()
+ c = tracer.trace_ray(ray)
+ color = ( color[0] + c[0],
+ color[1] + c[1],
+ color[2] + c[2])
+
+ color = ( color[0]/num_samples,
+ color[1]/num_samples,
+ color[2]/num_samples)
im.putpixel(imagePxPos, (int(color[0]*255), int(color[1]*255), int(color[2]*255)))
pxarray[imagePxPos[0]][imagePxPos[1]] = (int(color[0]*255), int(color[1]*255), int(color[2]*255))
@@ -62,11 +68,12 @@ def add_object(self, o):
def hit_bare_bones_object(self,ray):
def f(o):
shadeRec = o.hit(ray)
+
if shadeRec:
return (shadeRec.parameter, o)
else:
return None
-
+
try:
foremost=sorted(filter(lambda x: x is not None, map(f, self.objects)), key=lambda x: x[0])[0][1]
except IndexError:
View
@@ -0,0 +1,3 @@
+def dot(a,b):
+ return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]
+
View
@@ -1,13 +1,13 @@
-import numpy
-from .. import ShadeRecord
-class Plane(object):
- def __init__(self, point, normal, color):
- self.point = numpy.array(point)
- self.normal = numpy.array(normal)
- self.color = color
- def hit(self, ray):
- t = numpy.dot((self.point - ray.origin), self.normal) / numpy.dot(ray.direction, self.normal)
- if (t > 1.0e-7):
- return ShadeRecord.ShadeRecord(normal=self.normal, hit_point=(ray.origin + t * ray.direction), parameter=t, color=self.color)
-
- return None
+#from .. import ShadeRecord
+#from .. import math
+#class Plane(object):
+# def __init__(self, point, normal, color):
+# self.point = point
+# self.normal = normal
+# self.color = color
+# def hit(self, ray):
+# t = math.dot((self.point - ray.origin), self.normal) / math.dot(ray.direction, self.normal)
+# if (t > 1.0e-7):
+# return ShadeRecord.ShadeRecord(normal=self.normal, hit_point=(ray.origin + t * ray.direction), parameter=t, color=self.color)
+#
+# return None
View
@@ -1,19 +1,23 @@
import math
-import numpy
import sys
from .. import ShadeRecord
+from .. import math as rtmath
class Sphere(object):
def __init__(self, center, radius, color):
- self.center = numpy.array(center)
- self.radius = numpy.array(radius)
+ self.center = center
+ self.radius = radius
self.color = color
+ #@profile
def hit(self, ray):
- temp = ray.origin - self.center
- a = numpy.dot(ray.direction, ray.direction)
- b = 2.0 * numpy.dot(temp, ray.direction)
- c = numpy.dot(temp, temp) - self.radius * self.radius
+ temp = (ray.origin[0] - self.center[0],
+ ray.origin[1] - self.center[1],
+ ray.origin[2] - self.center[2])
+
+ a = rtmath.dot(ray.direction, ray.direction)
+ b = 2.0 * rtmath.dot(temp, ray.direction)
+ c = rtmath.dot(temp, temp) - self.radius * self.radius
disc = b * b - 4.0 * a * c
if (disc < 0.0):
@@ -23,15 +27,28 @@ def hit(self, ray):
denom = 2.0 * a
t = (-b - e) / denom
if (t > 1.0e-7):
- normal = (temp + t * ray.direction) / self.radius
- hit_point = ray.origin + t * ray.direction
+ normal = ( (temp[0] + t * ray.direction[0]) / self.radius,
+ (temp[1] + t * ray.direction[1]) / self.radius,
+ (temp[2] + t * ray.direction[2]) / self.radius,
+ )
+ hit_point = ( ray.origin[0] + t * ray.direction[0],
+ ray.origin[1] + t * ray.direction[1],
+ ray.origin[2] + t * ray.direction[2]
+ )
+
return ShadeRecord.ShadeRecord(normal=normal, hit_point=hit_point, parameter=t, color=self.color)
t = (-b + e) / denom
if (t > 1.0e-7):
- normal = (temp + t * ray.direction) / self.radius
- hit_point = ray.origin + t * ray.direction
+ normal = ( (temp[0] + t * ray.direction[0]) / self.radius,
+ (temp[1] + t * ray.direction[1]) / self.radius,
+ (temp[2] + t * ray.direction[2]) / self.radius,
+ )
+ hit_point = ( ray.origin[0] + t * ray.direction[0],
+ ray.origin[1] + t * ray.direction[1],
+ ray.origin[2] + t * ray.direction[2]
+ )
return ShadeRecord.ShadeRecord(normal=normal, hit_point=hit_point, parameter=t, color=self.color)
return None
@@ -1,2 +1,2 @@
from .Sphere import Sphere
-from .Plane import Plane
+#from .Plane import Plane
View
@@ -1,3 +1,4 @@
+from __future__ import absolute_import
import math
import random

0 comments on commit 2cb6e2b

Please sign in to comment.