예제 #1
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")
예제 #2
0
def barycentricq(ask=True):
	triangle = gf.triangle()
	if rv.coinflip(.5):
		p = gf.pointProbablyInPolygon(triangle)
	else:
		p = gf.pointNotInPolygon(triangle)
	bary = gf.getBarycentricCoordinates(triangle, p)
	
	q1 = "What are the barycentric coordinates of point P=%s with respect to triangle T with vertices\n %s?" % (numpy.array_str(p), '\n '.join(' '.join("%.2f" % v for v in vx) for vx in triangle))
	a1 = bary

	q2 = "Is point P inside or outside T?"
	a2 = (bary >= 0).all() and (bary <= 1).all()

	colors = numpy.array([rv.color() for _ in range(3)])
	q3 = "If vertex 0 has color %s, and vertex 1 has color %s, and vertex 2 has color %s, what is P's color?" % tuple(numpy.array_str(c) for c in colors)
	a3 = sum(b*c for b,c in zip(bary, colors))

	rv.writeModule(dict(zip(('triangle','p','bary','colors','a1','a2','a3'), (triangle, p, bary, colors, a1, a2, a3))))

	if ask:
		ua1 = rv.expect_vector(q1)
		rv.check_answer(a1, ua1, q1, "barycentric coordinates", rv.vector_check)
		ua2 = rv.expect_boolish(q2, {'inside':True, 'outside':False} )
		rv.check_answer(a2, ua2, q2, "barycentric inside", rv.bool_check)
		if a2:
			ua3 = rv.expect_vector(q3)
			rv.check_answer(a3, ua3, q3, "barycentric mixing", rv.vector_check)
	else:
		return rv.combine((q1, q2, q3)), rv.combine((a1, a2, a3)), ()
예제 #3
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)), ()
예제 #4
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")
예제 #5
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")
예제 #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")
예제 #7
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, ()
예제 #8
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, ()
예제 #9
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, ()
예제 #10
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, ()
예제 #11
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, ()
예제 #12
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, ()
예제 #13
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, ()
예제 #14
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), ()
예제 #15
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, ()
예제 #16
0
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), ()
예제 #17
0
def mipmapq(ask=True):
    ir = np.random.randint(2**4, 2**7)
    q = "Suppose a texture is applied to an area of size %d x %d." % (ir, ir)
    q1 = "What two levels of detail (powers of two) should be used for trilinear interpolation?"
    q2 = "How should each one be weighted?"

    d = np.log2(ir)
    a1 = 2**np.floor(d), 2**np.ceil(d)
    dd = (ir - a1[0]) / (a1[1] - a1[0])
    a2 = 1 - dd, dd

    rv.writeModule(
        dict(zip(("ir", "d", "dd", "a1", "a2"), (ir, d, dd, a1, a2))))

    if ask:
        print(q)
        ua1 = rv.expect_vector(q1, 2)
        rv.check_answer(a1, ua1, q1, "level of detail")
        ua2 = rv.expect_vector(q2, 2)
        rv.check_answer(a2, ua2, q2, "trilinear interpolation")
    else:
        return rv.combine((q, q1, q2)), rv.combine((a1, a2), False)
예제 #18
0
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, ()
예제 #19
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
예제 #20
0
def triangleq(ask=True):
	p, n = gf.plane()
	vertices = gf.polygon(p, n, 3)
	if rv.coinflip(0.5):
		px = gf.pointProbablyInPolygon(vertices)
	else:
		px = gf.pointNotInPolygon(vertices)

	e, d = gf.rayToPoint(px)

	q = "Triangle T has vertices p0=%s, p1=%s, p2=%s. Ray R has starting point e=%s and direction d=%s." % tuple(numpy.array_str(s) for s in tuple(vertices) + (e, d))

	q1 = "What are the beta and gamma barycentric coordinates and the t distance along the ray of the intersection between R and the plane defined by T?"

	# answer calculations
	e1 = vertices[1]-vertices[0]
	e2 = vertices[2]-vertices[0]
	x = numpy.cross(d, e2)
	m = e1.dot(x)		# determinant of original matrix
	s = e - vertices[0] # solution
	beta = s.dot(x)/m		# |-d s p2-p0| = -(-d x (p2-p0)) dot s
	r = numpy.cross(s, e1)
	gamma = d.dot(r)/m
	t = e2.dot(r)/m
	a1 = (beta, gamma, t)

	q2 = "Is the intersection point inside the triangle?"
	a2 = beta >= 0 and beta <= 1 and gamma >= 0 and gamma <= 1

	q3 = "Is the intersection point in front of the viewpoint e?"
	a3 = t > 0

	rv.writeModule(dict(zip(('p','n','vertices','e','d','e1','e2','x','m','s','beta','r','gamma','t', 'px'), (p, n, vertices, e, d, e1, e2, x, m, s, beta, r, gamma, t, px))))

	if ask:
		print(q)
		ua = rv.expect_vector(q1)
		rv.check_answer(numpy.array(a1), ua, q1, "triangle intersection", rv.vector_check)
		ua = rv.expect_boolish(q2, {'y':True, 'n':False} )
		rv.check_answer(a2, ua, q2, "triangle inside", rv.bool_check)
		ua = rv.expect_boolish(q3, {'y':True, 'n':False} )
		rv.check_answer(a3, ua, q3, "ray distance", rv.bool_check)
	else:
		finalq = rv.combine((q, q1, q2, q3), True)
		finala = rv.combine((a1, a2, a3))
		return finalq, finala, ()
예제 #21
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, ()
예제 #22
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
예제 #23
0
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")