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")
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 rotationq(ask=True, twod=True): r = numpy.round(numpy.random.random() + numpy.random.random(), 2) if twod: ax = 'z' else: ax = numpy.random.permutation(('x', 'y', 'z'))[0] q = "Create a matrix to %s." % qtext(("rotation", r, ax)) a = rotation_matrix(r, ax) if ask: ua = rv.expect_matrix(q) rv.check_answer(a, ua, q, "rotation") else: return q, a, (ax, r)
def translationq(ask=True, twod=False): (x, y, z) = numpy.random.randint(-5, 5, 3) if twod: z = 0 q = "Create a matrix to %s." % qtext(("translation", { 'x': x, 'y': y, 'z': z })) a = translation_matrix(x, y, z) if ask: ua = rv.expect_matrix(q) rv.check_answer(a, ua, q, "translation") else: return q, a, (x, y, z)
def pictureq(ask=True): transformation = numpy.random.permutation( ((translationq, "translation"), (rotationq, "rotation"), (scaleq, "scale")))[0] (qt, a, params) = transformation[0](False, True) q = "Create a matrix to transform the green triangle into the yellow triangle." call(["Rscript", "Rcode/generate_figs.R", transformation[1]] + [str(p) for p in params]) if ask: with Image.open('tmp.png') as img: img.show() ua = rv.expect_matrix(q) rv.check_answer(a, ua, q, "picture") else: return q, a, params
def comboq(ask=True): transformations = numpy.random.permutation( (translationq, rotationq, scaleq))[:numpy.random.randint(2, 4)] transformations = [t(False) for t in transformations] q = "Create a matrix to %s." % ", and then ".join([ qt.replace("Create a matrix to ", '')[:-1] for (qt, a, params) in transformations ]) a = numpy.eye(4) for q, m, p in reversed(transformations): a = a * m if ask: ua = rv.expect_matrix(q) rv.check_answer(a, ua, q, "combo") else: return q, a, transformations
def orthoq(ask=True): (t, b, r, l, n, f) = rv.vector(6) if t == b: t = t + 1 if r == l: r = r + 1 if n == f: n = n + 1 a = orthomatrix(t, b, r, l, n, f) q = "Create a matrix to transform a paralleliped defined by t=%d, b=%d, r=%d, l=%d, n=%d, and f=%d into the canonical view volume (an orthographic projection matrix)." % ( t, b, r, l, n, f) if ask: ua = rv.expect_matrix(q) rv.check_answer(a, ua, q, "orthographic") else: return q, a, ()
def scaleq(ask=True, twod=False): axes = ('x', 'y', 'z') target_axes = numpy.random.permutation( ('x', 'y', 'z'))[:(numpy.random.randint(0, 3) + 1)] # randomly permute (x, y, z), then choose a random slice if twod: target_axes = [a for a in target_axes if a != 'z'] params = { a: round(numpy.random.random() * 5, 2) if a in target_axes else 1 for a in axes } q = "Create a matrix to scale a point %s." % " and ".join([ "%.2f along the %s-axis" % (factor, axis) for axis, factor in sorted(params.items()) if axis in target_axes ]) a = scale_matrix(params['x'], params['y'], params['z']) if ask: ua = rv.expect_matrix(q) rv.check_answer(a, ua, q, "scale") else: return q, a, (params['x'], params['y'], params['z'])