Exemplo n.º 1
0
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")
Exemplo n.º 2
0
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")
Exemplo n.º 3
0
def ioq(ask=True):
    pipeline = [(('object', 'model'), ('model', ), ('world', )),
                (('world', ), ('view', 'camera'), ('camera', 'eye')),
                (('camera', 'eye'), ('projection', ), ('canonical view volume',
                                                       'clip')),
                (('canonical view volume', 'clip'), ('perspective division', ),
                 ('2d', )), (('2d', ), ('viewport', ), ('screen', ))]
    stage = rv.choose_random_from(pipeline)
    space_syn = rv.choose_random_from(("space", "coordinates"))
    qp = (
        rv.choose_random_from(stage[0]),
        rv.choose_random_from(stage[1]),
        rv.choose_random_from(stage[2]),
    )
    qidx = rv.choose_random_from(range(len(qp)))
    q = 'The %s transformation transforms %s %s into %s %s.\n' % (
        qp[1], qp[0], space_syn, qp[2], space_syn)
    q = q.replace(qp[qidx], rv.blank())

    # hacky: fix phrasing for some terms that don't fit templates
    q = q.replace("The perspective division transformation",
                  "Perspective division")
    q = q.replace("canonical view volume %s" % space_syn,
                  "the canonical view volume")

    a = stage[qidx]
    categories = [y for x in pipeline for y in x[qidx]]

    if ask:
        ua = rv.expect_categorical(q, categories)
        rv.check_answer(a, ua, q, "input/output",
                        part_of_str_answer_is_in_choices)
    else:
        return q, a, (categories)
Exemplo n.º 4
0
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")
Exemplo n.º 5
0
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")
Exemplo n.º 6
0
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")
Exemplo n.º 7
0
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, ()
Exemplo n.º 8
0
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, ()
Exemplo n.º 9
0
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, ()
Exemplo n.º 10
0
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, ()
Exemplo n.º 11
0
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, ()
Exemplo n.º 12
0
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, ()
Exemplo n.º 13
0
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, ()
Exemplo n.º 14
0
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)
Exemplo n.º 15
0
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, ()
Exemplo n.º 16
0
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), ()
Exemplo n.º 17
0
def totalq(ask=True):
	(cr, normal, cl, ld, ed, ca) = (rv.color(), rv.direction(), rv.color(), rv.direction(), rv.direction(), rv.color())
	q = "Point p has a surface color of %s and a surface normal of %s. Given a light of color %s and direction %s, a view direction %s, and an ambient color %s, what will be p's final color, with a Phong exponent of 2?" % tuple(numpy.array_str(s) for s in (cr, normal, cl, ld, ed, ca))

	a = diffuseColor(cl, cr, ld, normal) + specularColor(cl, ld, normal, ed, 2) + ca * cr

	if ask:
		ua = rv.expect_vector(q)
		rv.check_answer(a, ua, q, "total")
	else:
		return q, a, ()
Exemplo n.º 18
0
def diffuseq(ask=True):
	cl = rv.color()
	cr = rv.color()
	ld = rv.direction()
	normal = rv.direction()
	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, ()
Exemplo n.º 19
0
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")
Exemplo n.º 20
0
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)
Exemplo n.º 21
0
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)), ()
Exemplo n.º 22
0
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)
Exemplo n.º 23
0
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
Exemplo n.º 24
0
def nearestq(ask=True):
    u, v = np.round(np.random.random(2), 2)
    rs, rt = np.random.randint(16, 2048, 2)
    q = "Given (u, v) coordinates of (%.2f, %.2f) and a texture of size (%d, %d), what texel will be chosen by nearest neighbor sampling?" % (
        u, v, rs, rt)

    a = np.round(u * rs), np.round(v * rt)

    rv.writeModule(dict(zip("u,v,rs,rt,a".split(','), (u, v, rs, rt, a))))

    if ask:
        ua = rv.expect_vector(q, 2)
        rv.check_answer(a, ua, q, "nearest neighbor")
    else:
        return q, a
Exemplo n.º 25
0
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
Exemplo n.º 26
0
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, ()
Exemplo n.º 27
0
def specularq(ask=True):
	cl = rv.color()
	ld = rv.direction()
	cr = rv.color()
	normal = rv.direction()
	r = gf.reflect(ld, normal)
	e = rv.direction()
	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, ()
Exemplo n.º 28
0
        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")
Exemplo n.º 29
0
def linearq(ask=True):
    smin, smax, tmin, tmax = rv.vector(4)
    smin, smax = rv.strict_order(smin, smax)
    tmin, tmax = rv.strict_order(tmin, tmax)
    s = np.random.randint(smin, smax + 1)
    t = np.random.randint(tmin, tmax + 1)

    u = (s - smin) / (smax - smin)
    v = (t - tmin) / (tmax - tmin)

    q = "Given a 2D image texture with coordinates ranging from 0 to 1, and a rectangular surface with x ranging from %d to %d and y ranging from %d to %d, what are the (u, v) texture coordinates of surface point (%d, %d) by simple linear interpolation?" % (
        smin, smax, tmin, tmax, s, t)

    a = (u, v)

    if ask:
        ua = rv.expect_vector(q, 2)
        rv.check_answer(a, ua, q, 'linear interpolation')
    else:
        return q, a
Exemplo n.º 30
0
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)