def run(): d = 0.0001 zero = Vector(0.0,0.0,0.0) up = Vector(0.0,1.0,0.0) left = Vector(1.0,0.0,0.0) forward = Vector(0.0,0.0,1.0) # test __init__() method (and implicitly: length() method) u = Vector(3.0,4.0,1.0) dx,dy,dz = u.x - 3.0, u.y - 4.0, u.z - 1.0 if dx > d or dy > d or dz > d: print "Constructor: bad coordinates" if abs( u.length() - (26.0 ** 0.5) ) > d: print "Constructor: bad length" del u # test __sub__() method u = Vector(1.5, 2.5, 3.5) v = Vector(3.5, 2.5, 1.5) w = u - v x = Vector( -2.0, 0.0, 2.0 ) if abs((w - x).length()) > d: print "__sub__ method: incorrect coordinates" if abs( w.length() - (8 ** 0.5) ) > d: print "__sub__ method: incorrect length." del u,v,w,x # test __eq__() method u = Vector() v = u.dup() if not u == v: print "__eq__() method: not evaluating true correctly" v.trans(1.0,1.0,1.0) if u == v: print "__eq__() method: not evaluating false correctly" del u,v # test dup() method u = Vector(1.0,2.0,3.0) v = u.dup() if (u - v).length() > d: print "dup() method: bad coordinates" v.x = 4.0 if (u - v).length() < 1.0: print "dup() method: not a new instance" del u,v # test copy() method u = Vector(1.0,2.0,3.0) v = Vector() v.copy(u) if (u - v).length() > d: print "copy() method: bad coordinates" if abs(u.length() - v.length()) > d: print "copy() method: bad length" del u,v # test add() method u = Vector(-1.0,1.0,3.0) v = Vector(-2.0,2.0,3.0) w = Vector() for s in [-2.0, -1.0, 0.0, 1.0, 2.0]: w.copy(u) w.add(v, s) x = Vector(u.x + s * v.x, u.y + s * v.y, u.z + s * v.z) if (w - x).length() > d: print "add() method: bad coordinates with scalar", s if abs(w.length() - x.length()) > d: print "add() method: bad length with scalar", s del x del s,u,v,w # test scale() method u = Vector(1.0,2.0,3.0) for s in [-2.0, -1.0, 0.0, 1.0, 2.0]: v = Vector( s * 1.0, s * 2.0, s * 3.0 ) w = u.dup().scale(s) if (w - v).length() > d: print "scale() method: bad coordinates with scalar", s if abs(v.length() - w.length()) > d: print "scale() method: bad length with scalar", s del v del w del s,u # test trans() method u = Vector(1.0,2.0,3.0) v = Vector(-1.0,5.0,0.0) u.trans(-2.0,3.0,-3.0) if (u - v).length() > d: print "trans() method: bad coordinates" if abs(u.length() - v.length()) > d: print "trans() method: bad length" del u,v # test norm() method u = Vector(-3.0,0.0,4.0) v = u.dup().norm() if abs(v.length() - 1.0) > d: print "norm() method: didn't normalize to length 1.0" if (v.scale(u.length()) - u).length() > d: print "norm() method: didn't maintain direction" del u,v # test dot() method for v in [up, left,forward]: if abs(v.dot(zero)) > d: print "dot() method: handles zero vector poorly" for v in [left,forward]: if abs(up.dot(v)) > d: print "dot() method: handles orthogonal axis vectors poorly" if abs(left.dot(forward)) > d: print "dot() method: handles orthogonal axis vectors poorly" u = Vector(2.0,2.0,2.0).norm() v = Vector(1.0,1.0,-2.0).norm() if abs(u.dot(v)) > d: print "dot() method: handles orthogonal vectors poorly" del u,v u = Vector(3.0,-2.0,-5.0) v = Vector(7.0,-1.0,2.0) if abs(u.dot(v) - 13.0) > d: print "dot() method: bad non-orthogonal dot products" del u,v # test cross() method if (left.cross(up) - forward) > d: print "cross() method: fails on axis vectors" u = Vector(1.0,2.0,3.0) v = Vector(-1.0,3.0,2.0) w = u.cross(v) if abs(u.dot(w)) > d or abs(v.dot(w)) > d: print "cross() method: result not perpendicular" exit() del u,v,w # test delta() method u = Vector(-3.0,-2.0,-1.0) for translations in range(3): for delt in [0.001, 0.01, 0.1, 1.0, 10.0]: v = u.dup().delta(delt) if abs( (v - u).length() - delt ) > delt: print "delta() method: wrong delta distance" u.trans(1.0,1.0,1.0) del u,v
Vector(10.0, 1.0, 0.0)) p = CheckPlane() w = World() w.add_body(s) w.add_body(p) w.set_sky(Sky(Vector(1.0, 10.0, 1.0), Color(0.2, 0.2, 0.8))) camera = Vector(0.0, 0.8, 16.0) c_dir = Vector(0.0, 0.0, -1.0).norm() c_up = Vector(0.0, 1.0, 0.0).norm() c_origin = Vector(-1.5, 2.3, 0.0) - camera c_width = 3.0 c_height = 3.0 c_x = c_dir.cross(c_up).scale(c_width / width) c_y = c_up.dup().scale(-1 * c_height / height) ray = Ray() image = Image(width, height) color = Color() loops_per_pixel = 16 t_0 = time() for y in range(height): for x in range(width): color.set_rgb(0.0, 0.0, 0.0) for k in range(loops_per_pixel): ray.set_origin(camera.dup().delta(0.01)) ray.set_direction(c_origin.dup().add(c_x, x + rand()).add( c_y, y + rand())) color = color + w.sample(ray) color.dim(1.0 / loops_per_pixel)
def run(): d = 0.0001 zero = Vector(0.0, 0.0, 0.0) up = Vector(0.0, 1.0, 0.0) left = Vector(1.0, 0.0, 0.0) forward = Vector(0.0, 0.0, 1.0) # test __init__() method (and implicitly: length() method) u = Vector(3.0, 4.0, 1.0) dx, dy, dz = u.x - 3.0, u.y - 4.0, u.z - 1.0 if dx > d or dy > d or dz > d: print "Constructor: bad coordinates" if abs(u.length() - (26.0**0.5)) > d: print "Constructor: bad length" del u # test __sub__() method u = Vector(1.5, 2.5, 3.5) v = Vector(3.5, 2.5, 1.5) w = u - v x = Vector(-2.0, 0.0, 2.0) if abs((w - x).length()) > d: print "__sub__ method: incorrect coordinates" if abs(w.length() - (8**0.5)) > d: print "__sub__ method: incorrect length." del u, v, w, x # test __eq__() method u = Vector() v = u.dup() if not u == v: print "__eq__() method: not evaluating true correctly" v.trans(1.0, 1.0, 1.0) if u == v: print "__eq__() method: not evaluating false correctly" del u, v # test dup() method u = Vector(1.0, 2.0, 3.0) v = u.dup() if (u - v).length() > d: print "dup() method: bad coordinates" v.x = 4.0 if (u - v).length() < 1.0: print "dup() method: not a new instance" del u, v # test copy() method u = Vector(1.0, 2.0, 3.0) v = Vector() v.copy(u) if (u - v).length() > d: print "copy() method: bad coordinates" if abs(u.length() - v.length()) > d: print "copy() method: bad length" del u, v # test add() method u = Vector(-1.0, 1.0, 3.0) v = Vector(-2.0, 2.0, 3.0) w = Vector() for s in [-2.0, -1.0, 0.0, 1.0, 2.0]: w.copy(u) w.add(v, s) x = Vector(u.x + s * v.x, u.y + s * v.y, u.z + s * v.z) if (w - x).length() > d: print "add() method: bad coordinates with scalar", s if abs(w.length() - x.length()) > d: print "add() method: bad length with scalar", s del x del s, u, v, w # test scale() method u = Vector(1.0, 2.0, 3.0) for s in [-2.0, -1.0, 0.0, 1.0, 2.0]: v = Vector(s * 1.0, s * 2.0, s * 3.0) w = u.dup().scale(s) if (w - v).length() > d: print "scale() method: bad coordinates with scalar", s if abs(v.length() - w.length()) > d: print "scale() method: bad length with scalar", s del v del w del s, u # test trans() method u = Vector(1.0, 2.0, 3.0) v = Vector(-1.0, 5.0, 0.0) u.trans(-2.0, 3.0, -3.0) if (u - v).length() > d: print "trans() method: bad coordinates" if abs(u.length() - v.length()) > d: print "trans() method: bad length" del u, v # test norm() method u = Vector(-3.0, 0.0, 4.0) v = u.dup().norm() if abs(v.length() - 1.0) > d: print "norm() method: didn't normalize to length 1.0" if (v.scale(u.length()) - u).length() > d: print "norm() method: didn't maintain direction" del u, v # test dot() method for v in [up, left, forward]: if abs(v.dot(zero)) > d: print "dot() method: handles zero vector poorly" for v in [left, forward]: if abs(up.dot(v)) > d: print "dot() method: handles orthogonal axis vectors poorly" if abs(left.dot(forward)) > d: print "dot() method: handles orthogonal axis vectors poorly" u = Vector(2.0, 2.0, 2.0).norm() v = Vector(1.0, 1.0, -2.0).norm() if abs(u.dot(v)) > d: print "dot() method: handles orthogonal vectors poorly" del u, v u = Vector(3.0, -2.0, -5.0) v = Vector(7.0, -1.0, 2.0) if abs(u.dot(v) - 13.0) > d: print "dot() method: bad non-orthogonal dot products" del u, v # test cross() method if (left.cross(up) - forward) > d: print "cross() method: fails on axis vectors" u = Vector(1.0, 2.0, 3.0) v = Vector(-1.0, 3.0, 2.0) w = u.cross(v) if abs(u.dot(w)) > d or abs(v.dot(w)) > d: print "cross() method: result not perpendicular" exit() del u, v, w # test delta() method u = Vector(-3.0, -2.0, -1.0) for translations in range(3): for delt in [0.001, 0.01, 0.1, 1.0, 10.0]: v = u.dup().delta(delt) if abs((v - u).length() - delt) > delt: print "delta() method: wrong delta distance" u.trans(1.0, 1.0, 1.0) del u, v
def run(): small = 0.00001 zero = Vector(0.0,0.0,0.0) up = Vector(0.0,1.0,0.0) ray_origins = [ Vector(-3.0,-2.0,-1.0), Vector(-2.0,-1.0,0.0), Vector(-1.0,0.0,1.0), Vector(0.0,1.0,2.0), Vector(1.0,2.0,3.0)] ray_directions = [ Vector(-3.0,-2.0,-1.0).norm(), Vector(-2.0,-1.0,0.0).norm(), Vector(-1.0,0.0,1.0).norm(), Vector(0.0,1.0,2.0).norm(), Vector(1.0,2.0,3.0).norm()] # test initialization for o in ray_origins: for d in ray_directions: ray = Ray(o,d) if not ray.o == o: print "__init__() method: set origin incorrectly" if not ray.d == d: print "__init__() method: set direction incorrectly" del ray # test ray equality bad_ray = Ray(ray_origins[-1], ray_directions[-1]) for o in ray_origins: for d in ray_directions: ray = Ray(o.dup(),d.dup()) good_ray = Ray(o.dup(),d.dup()) if not ray == good_ray: print "__eq__() method: evaluates true wrong" if ray == bad_ray: print "__eq__() method: evaluates false wrong" bad_ray.d = d.dup() bad_ray.o = o.dup() # test set_origin() method ray = Ray(zero.dup(), up.dup()) for o in ray_origins: ray.set_origin(o) if not ray.o == o: print "set_origin() method: set bad origin" del ray # test set_direction() method ray = Ray(zero.dup(),up.dup()) for d in ray_directions: ray.set_direction(d.dup()) if not ray.d == d: print "set_direction() method: set bad direction" # test follow() method distances = [-3,-2,-1,0,1,2,3] for t in distances: for o in ray_origins: for d in ray_directions: p = Ray(o.dup(),d.dup()).follow(t) if not p == o.dup().add(d, t): print "follow() method: didn't follow right" # test reflect() method for poi in ray_origins: ray = Ray(zero.dup(),poi.dup()) for normal in ray_directions: bounce = ray.dup() bounce.reflect(poi.dup(), normal.dup()) o_sine = ray.d.dot(normal) r_sine = bounce.d.dot(normal) if abs( o_sine + r_sine ) > small: print "reflect() method: exit not at same angle to normal" print " poi: ", o.p() print " normal:", d.p() print " rayout:", bounce.d.p() print " o_sine:", o_sine print " r_sine:", r_sine