def vsumq(): x = rv.vector3() y = rv.vector3() a = x + y q = "%s + %s" % (numpy.array_str(x), numpy.array_str(y)) ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "sum")
def cross_productq(): x = rv.vector3() y = rv.vector3() a = numpy.cross(x, y) q = "%s x %s" % (numpy.array_str(x), numpy.array_str(y)) ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "cross_product")
def dot_productq(): x = rv.vector3() y = rv.vector3() a = x.dot(y) q = "%s dot %s\n" % (numpy.array_str(x), numpy.array_str(y)) ua = rv.expect_float(q) rv.check_answer(a, ua, q, "dot product")
def angleq(): x = rv.vector3() y = rv.vector3() a = numpy.arccos(gf.normalize(x).dot(gf.normalize(y))) q = "What is the angle between the following two vectors (in radians)?\n %s, %s\n" % ( numpy.array_str(x), numpy.array_str(y)) ua = rv.expect_float(q) rv.check_answer(a, ua, q, "angle")
def point_to_pointq(): x = rv.vector3() y = rv.vector3() a = y - x q = "What is the vector from %s to %s?\n" % (numpy.array_str(x), numpy.array_str(y)) ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "point to point")
def cross_productq(ask=True): x = rv.vector3() y = rv.vector3() a = numpy.cross(x, y) q = "%s x %s" % (numpy.array_str(x), numpy.array_str(y)) if ask: ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "cross_product") else: return q, a, ()
def dot_productq(ask=True): x = rv.vector3() y = rv.vector3() a = x.dot(y) q = "%s dot %s\n" % (numpy.array_str(x), numpy.array_str(y)) if ask: ua = rv.expect_float(q) rv.check_answer(a, ua, q, "dot product") else: return q, a, ()
def vsumq(ask=True): x = rv.vector3() y = rv.vector3() a = x + y q = "%s + %s" % (numpy.array_str(x), numpy.array_str(y)) if ask: ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "sum") else: return q, a, ()
def ldirq(ask=True): ppos = rv.vector3() lpos = rv.vector3() q = "Given a point location of %s and a light location of %s, what is the light direction? (Remember to normalize.)" % (numpy.array_str(ppos), numpy.array_str(lpos)) a = gf.normalize(lpos - ppos) if ask: ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "light direction") else: return q, a, ()
def cameraq(ask=True): eye = rv.vector3() gaze = rv.vector3() up = rv.vector3() q = "Given a camera position of %s, a gaze vector of %s, and an up vector of %s, what is the resulting camera transformation matrix?" % ( numpy.array_str(eye), numpy.array_str(gaze), numpy.array_str(up)) # derive answer w = -gf.normalize(gaze) u = gf.normalize(numpy.cross(up, w)) v = gf.normalize(numpy.cross(w, u)) a = numpy.matrix([[u[0], u[1], u[2], eye[0]], [v[0], v[1], v[2], eye[1]], [w[0], w[1], w[2], eye[2]], [0, 0, 0, 1]]) if ask: ua = rv.expect_matrix(q) # nested function for breaking down camera question def camera_help(ua, a): if rv.lax_equal(a, ua): return True b = input( "Incorrect. Enter 'b' to break down the problem into subproblems, or anything else to abandon this question.\n" ) if b != 'b': return False q = "w is the normalized and negated gaze vector. Enter w." ua = rv.expect_vector(q) rv.check_answer(w, ua, q, "camera.w") q = "u is the normalized cross product of the up vector and w. Enter u." ua = rv.expect_vector(q) rv.check_answer(u, ua, q, "camera.u") q = "v is the normalized cross product of u and w. Enter v." ua = rv.expect_vector(q) rv.check_answer(v, ua, q, "camera.v") f = [['ux', 'uy', 'uz', 'px'], ['vx', 'vy', 'vz', 'py'], ['wx', 'wy', 'wz', 'pz'], ['0', '0', '0', '1']] print(numpy.array_str(numpy.matrix(f))) print(rv.mxstr(f)) q = "The camera transformation matrix is composed of the three basis vectors and camera position in the following order:\n %s\n Enter the camera transformation matrix." % rv.mxstr( f) ua = rv.expect_matrix(q) return rv.check_answer(a, ua, q, "camera.final") rv.check_answer(a, ua, q, "camera", camera_help) else: return q, a, (eye, gaze, up)
def point_to_pointq(ask=True): x = rv.vector3() y = rv.vector3() a = y - x q = "What is the vector from %s to %s?\n" % (numpy.array_str(x), numpy.array_str(y)) if ask: ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "point to point") else: return q, a, ()
def lineq(ask=True): p0 = rv.vector3() p1 = rv.vector3() q = "What are the A, B, and C components of the line passing through %s and %s, where Ax + By + C = 0" % (numpy.array_str(p0), numpy.array_str(p1)) a = gf.lineEq(p0, p1)[:-1] rv.writeModule(dict(zip(('p0','p1','a'), (p0, p1,a)))) if ask: ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "line equation", rv.vector_check) else: return q, rv.combine(a), ()
def directionq(): x = rv.vector3() y = rv.vector3() a = 'a' if (x.dot(y) < 0): a = 'b' elif x.dot(y) == 0: a = 'c' q = "What is the relationship between the following two vectors?\n %s, %s\n a) They point in the same direction\n b) they point in opposite directions\n c) they are perpendicular\n" % ( numpy.array_str(x), numpy.array_str(y)) ua = rv.expect_categorical(q, ('a', 'b', 'c')) rv.check_answer(a, ua, q, "direction")
def diffuseq(ask=True): cl = rv.color() cr = rv.color() ld = gf.normalize(rv.vector3()) normal = gf.normalize(rv.vector3()) q = "Point p has a surface color of %s and a surface normal of %s. Given a light of color %s and direction %s, what will be the diffuse component of p's final color?" % ( rv.tostring(cr), rv.tostring(normal), rv.tostring(cl), rv.tostring(ld)) a = cl * cr * max((0, ld.dot(normal))) if ask: ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "diffuse") else: return q, a
def rayq(ask=True): # camera frame x = gf.normalize(rv.vector3()) y = gf.normalize(rv.vector3()) z = gf.normalize(rv.vector3()) e = rv.vector3() # view volume l, r, b, t = numpy.random.randint(-5, 5, 4) l, r = rv.strict_order(l, r) b, t = rv.strict_order(b, t) # u, v, coordinates i, j = numpy.random.randint(0, 5, 2) nx, ny = numpy.random.randint(250, 750, 2) u = l + (r-l)*(i+0.5)/nx v = b + (t-b)*(j+0.5)/ny # ray if rv.coinflip(0.5): vt = 'orthographic' d = -z o = e + u*x + v*y ip = None else: vt = 'perspective' ip = numpy.random.randint(0, 5) o = e d = -ip*z + u*x + v*y q = """What are the origin and direction of a ray cast from the viewpoint to pixel (%d, %d) in a %d x %d image with the following parameters? l=%d, r=%d, b=%d, t=%d view type = %s camera origin = %s camera u axis = %s camera v axis = %s camera w axis = %s """ % (i, j, nx, ny, l, r, b, t, vt, numpy.array_str(e), numpy.array_str(x), numpy.array_str(y), numpy.array_str(z)) if v == 'perspective': q = q + "image plane at distance %d in front of viewpoint\n" % ip rv.writeModule(dict(zip(('i', 'j', 'nx', 'ny', 'l', 'r', 'b', 't', 'vt', 'e', 'x', 'y', 'z', 'ip', 'u', 'v', 'o', 'd'), (i, j, nx, ny, l, r, b, t, vt, e, x, y, z, ip, u, v, o, d)))) if ask: print(q) ua = rv.expect_vector("origin:") rv.check_answer(o, ua, q, "ray casting: origin", rv.vector_check) ua = rv.expect_vector("direction:") rv.check_answer(d, ua, q, "ray casting: direction", rv.vector_check) else: return q, rv.combine((o, d)), ()
def normalizeq(ask=True): x = rv.vector3() a = gf.normalize(x) q = "normalize %s" % numpy.array_str(x) if ask: ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "normalize") else: return q, a, ()
def magnitudeq(ask=True): x = rv.vector3() a = gf.magnitude(x) q = "||%s||\n" % numpy.array_str(x) if ask: ua = rv.expect_float(q) rv.check_answer(a, ua, q, "magnitude") else: return q, a, ()
def normalq(ask=True): vertices = [rv.vector3() for _ in range(3)] q = "What is the normal to a triangle defined by vertices %s, %s, and %s (listed in the order of positive rotation)?" % tuple(numpy.array_str(p) for p in vertices) a = gf.getNormal(vertices) if ask: ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "normal") else: return q, a, ()
def specularq(ask=True): cl = rv.color() ld = gf.normalize(rv.vector3()) cr = rv.color() normal = gf.normalize(rv.vector3()) r = gf.reflect(ld, normal) e = gf.normalize(rv.vector3()) p = 2 q = "Point p has a surface color of %s and a surface normal of %s. Given a light of color %s and direction %s, and a view direction %s, what will be the specular component of p's final color, with a Phong exponent of %d?" % ( numpy.array_str(cr), numpy.array_str(normal), numpy.array_str(cl), numpy.array_str(ld), numpy.array_str(e), p) a = cl * max((r.dot(e), 0))**p if ask: ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "specular") else: return q, a
def pointOnPlane(p, n): # find point whose vector to p is perpendicular to n # take another random vector and take the cross product of that and n # now we have a vector perpendicular to n # add that to p to get another point on the plane. v = rv.vector3() v2 = numpy.cross(v, n) p2 = p + v2 if not (p - p2).dot(n) < 0.000001: print("ERROR") return (v, v2, p2) return p2
def perspectiveq(ask=True): p = rv.vector3() p = numpy.append(p, 1) n = rv.vector3()[0] if n == 0: n = 1 q = "Project point %s onto the plane n=%d. " % (numpy.array_str(p), n) q1 = "What will px, py, pw be after the perspective transformation is applied? (before the scaling and translation of the orthographic transformation) " q2 = "What will px, py be after perspective division?" a1 = numpy.array((p[0] * n, p[1] * n, p[2])) a2 = numpy.array((a1[0] / a1[2], a1[1] / a1[2])) if ask: print(q) ua1 = rv.expect_vector(q1) rv.check_answer(a1, ua1, q1, "perspective.a") ua2 = rv.expect_vector(q2, 2) rv.check_answer(a2, ua2, q2, "perspective.b") else: finalq = r"%s\\a) %s\\b) %s" % (q, q1, q2) finala = r"a) %s \\ b) %s" % (a1, a2) return finalq, finala, ()
def triangle(): return [rv.vector3() for _ in range(3)]
def ray(): return rv.vector3(), normalize(rv.vector3())
def rayToPoint(p): d = numpy.random.random() * rv.vector3() e = p + d return e, -normalize(d)
def magnitudeq(): x = rv.vector3() a = gf.magnitude(x) q = "||%s||\n" % numpy.array_str(x) ua = rv.expect_float(q) rv.check_answer(a, ua, q, "magnitude")
def normalizeq(): x = rv.vector3() a = gf.normalize(x) q = "normalize %s" % numpy.array_str(x) ua = rv.expect_vector(q) rv.check_answer(a, ua, q, "normalize")
def plane(): return rv.vector3(), normalize(rv.vector3())