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 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 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 samplingq(ask=True): ir = np.random.randint(2**4, 2**7, 2) q1 = "Given a texture of size (%d, %d) and an image of size (%d, %d), how many texels must cover each pixel?" % ( ir[0], ir[0], ir[1], ir[1]) a1 = ir[0] / ir[1] q2 = "Is this a problem of magnification (mag) or minification (min)?" if ir[0] < ir[1]: a2 = "mag" else: a2 = "min" rv.writeModule(dict(zip("ir, a1, a2".split(','), (ir, a1, a2)))) if ask: ua1 = rv.expect_float(q1) rv.check_answer(a1, ua1, q1, "texel:pixel") ua2 = rv.expect_categorical(q2, ('mag', 'min')) rv.check_answer(a2, ua2, q2, "magnification") else: return rv.combine((q1, q2), False), rv.combine((a1, a2), False)
def bilinearq(ask=True): u, v = np.round(np.random.random(2), 2) rs, rt = (2**np.random.randint(7, 12) for _ in range(2)) r1, r2 = np.random.randint(1, 4, 2) q = "Given (u, v) coordinates of (%.2f, %.2f) and a texture with resolution %d x %d, where the value at each (s, t) texture location is (s+t+%d)/%d, what is the value retrieved by bilinear interpolation?" % ( u, v, rs, rt, r1, r2) fn = lambda u, v: (u + v + r1) / r2 a = gf.bilinearInterpolation(u, v, rs, rt, fn) rv.writeModule( dict( zip(("u", "v", "rs", "rt", "a", "fn", "r1", "r2"), (u, v, rs, rt, a, "lambda u, v: (u+v+r1)/r2", r1, r2)))) if ask: ua = rv.expect_float(q) rv.check_answer(a, ua, q, 'bilinear interpolation') else: return q, a
def polygonq(ask=True): p, n = gf.plane() vertices = gf.polygon(p, n) if rv.coinflip(0.5): px = gf.pointProbablyInPolygon(vertices) else: px = gf.pointNotInPolygon(vertices) e, d = gf.rayToPoint(px) q = "Ray R has starting point e=%s and direction d=%s.\n Polygon P has vertices \n%s." % (numpy.array_str(e), numpy.array_str(d), '\n'.join(numpy.array_str(v) for v in vertices)) print(q) q1 = "What is the normal to P?" a1 = gf.getNormal(vertices) q2 = "What is the t intersection point of R and P?" a2 = gf.rayPlane(e, d, v=vertices) q3 = "What is the (x, y, z) intersection point on R at t?" a3 = gf.pointOnRay(e, d, a2) q4 = "Is the intersection point inside the polygon?" a4 = gf.pointInPolygon(a3, vertices) q5 = "Is the intersection point in front of the viewpoint e?" a5 = a2 > 0 if ask: ua1 = rv.expect_vector(q1) rv.check_answer(a1, ua1, q1, "normal", rv.vector_check) ua2 = rv.expect_float(q2) rv.check_answer(a2, ua2, q2, "ray-plane", rv.float_check) ua3 = rv.expect_vector(q3) rv.check_answer(a3, ua3, q3, "point on ray", rv.vector_check) ua4 = rv.expect_yesno(q4) rv.check_answer(a4, ua4, q4, "point in polygon", rv.bool_check) ua5 = rv.expect_yesno(q5) rv.check_answer(a5, ua5, q5, "ray distance", rv.bool_check) else: return rv.combine((q, q1, q2, q3, q4, q5)), rv.combine((a1, a2, a3, a4,a5), False), ()
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")