def test_intersect(self): # Ray pointing in the +Z direction ray = Ray(origin=vec3(1, 1, 0), direction=vec3(0, 0, 1)) triangle = xy_triangle() result = ray_triangle_intersect(ray, triangle) self.assertEqual(result, 2)
def __init__(self): super().__init__() self._field_of_view_y = 90 self._near = 0.001 self._far = 100.0 self._transform.loc = vec3(0, 0, 2) self._target = vec3(0, 0, 0) self._aspect_ratio = None self._projection_matrix = None
def test_intersect(self): # Ray pointing in the +Z direction ray = Ray(origin=vec3(1, 1, 0), direction=vec3(0, 0, 1)) triangle = xy_triangle() result = ray_triangle_intersect(ray, triangle) self.assertEqual(result, 2)
def __init__(self): super().__init__() self._field_of_view_y = 90 self._near = 0.001 self._far = 100.0 self._transform.loc = vec3(0, 0, 2) self._target = vec3(0, 0, 0) self._aspect_ratio = None self._projection_matrix = None
def test_look_at(self): actual = mat4_look_at(eye=vec3(0, 0, 2), target=vec3(0, 0, 0), up=vec3(0, 1, 0)) expected = mat4( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, -2, 0, 0, 0, 1 ) self.assertTrue(allclose(actual, expected))
def load_obj(cls, path): """Create a |Mesh| from an obj file.""" with open(path) as rfile: verts = [] faces = [] for line in rfile.readlines(): line = _obj_remove_comment(line) parts = line.split() if len(parts) == 0: continue tok = parts[0] if tok == 'v': vec = vec3() if len(parts) > 1: vec[0] = float(parts[1]) if len(parts) > 2: vec[1] = float(parts[2]) if len(parts) > 3: vec[2] = float(parts[3]) verts.append(Vert(vec)) elif tok == 'f': indices = [int(ind) - 1 for ind in parts[1:]] faces.append(Face(indices)) return Mesh(verts, faces, path)
def load_obj(cls, path): """Create a |Mesh| from an obj file.""" with open(path) as rfile: verts = [] faces = [] for line in rfile.readlines(): line = _obj_remove_comment(line) parts = line.split() if len(parts) == 0: continue tok = parts[0] if tok == 'v': vec = vec3() if len(parts) > 1: vec[0] = float(parts[1]) if len(parts) > 2: vec[1] = float(parts[2]) if len(parts) > 3: vec[2] = float(parts[3]) verts.append(Vert(vec)) elif tok == 'f': indices = [int(ind) - 1 for ind in parts[1:]] faces.append(Face(indices)) return Mesh(verts, faces, path)
def mesh_two_adj_triangles(): r""" v0______v3 |\ / | \ / |__\/ v1 v2 """ return Mesh(( Vert(vec3(0, 0, 0)), Vert(vec3(0, 1, 0)), Vert(vec3(1, 1, 0)), Vert(vec3(2, 0, 0)), ), ( Face((0, 1, 2)), Face((0, 2, 3)), ))
def cube_mesh(): verts = [ Vert(vec3(-1, -1, -1)), Vert(vec3(-1, +1, -1)), Vert(vec3(+1, +1, -1)), Vert(vec3(+1, -1, -1)), Vert(vec3(-1, -1, +1)), Vert(vec3(-1, +1, +1)), Vert(vec3(+1, +1, +1)), Vert(vec3(+1, -1, +1)), ] faces = [ Face([0, 1, 2, 3]), Face([7, 6, 5, 4]), Face([0, 4, 5, 1]), Face([2, 3, 7, 6]), Face([1, 5, 6, 2]), Face([0, 3, 7, 4]), ] return Mesh(verts, faces)
def update_ray(self, ray): up = vec3(0, 1, 0) cr1 = cross(ray.direction, up) cr2 = cross(cr1, ray.direction) # Visual length vlen = 0.01 self._v0.loc = ray.origin self._v1.loc = ray.origin + ray.direction * vlen # Push cross away from actual origin push = 0.1 org = ray.origin + ray.direction * push self._v2.loc = org self._v3.loc = org + cr1 * vlen self._v4.loc = org self._v5.loc = org + cr2 * vlen self._edge_buf.dirty = True
def update_ray(self, ray): up = vec3(0, 1, 0) cr1 = cross(ray.direction, up) cr2 = cross(cr1, ray.direction) # Visual length vlen = 0.01 self._v0.loc = ray.origin self._v1.loc = ray.origin + ray.direction * vlen # Push cross away from actual origin push = 0.1 org = ray.origin + ray.direction * push self._v2.loc = org self._v3.loc = org + cr1 * vlen self._v4.loc = org self._v5.loc = org + cr2 * vlen self._edge_buf.dirty = True
def xy_triangle(): """Triangle in the XY plane.""" return [vec3(0, 0, 2), vec3(2, 0, 2), vec3(1, 2, 2)]
def __init__(self): self._loc = vec3() self._rot = quat4f() self._scale = vec3_from_scalar(1)
def xy_triangle(): """Triangle in the XY plane.""" return [vec3(0, 0, 2), vec3(2, 0, 2), vec3(1, 2, 2)]
def __init__(self, loc=None): self.loc = vec3() if loc is None else loc self.edge_indices = [] self.col = vec4(0.9, 0.8, 0.8, 1.0)
def test_normalized(self): self.assertTrue(allclose(normalized(vec3(0, 0, 4)), vec3(0, 0, 1)))
def __init__(self, loc=None): self.loc = vec3() if loc is None else loc self.edge_indices = [] self.col = vec4(0.9, 0.8, 0.8, 1.0)
def test_normalized(self): self.assertTrue(allclose(normalized(vec3(0, 0, 4)), vec3(0, 0, 1)))
def test_dot(self): self.assertEqual(dot(vec3(3, 5, 7), vec3(2, 4, 6)), 68)
def view_matrix(self): # TODO, cache? return mat4_look_at(self.transform.loc, self._target, vec3(0, 1, 0))
def test_look_at(self): actual = mat4_look_at(eye=vec3(0, 0, 2), target=vec3(0, 0, 0), up=vec3(0, 1, 0)) expected = mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, -2, 0, 0, 0, 1) self.assertTrue(allclose(actual, expected))
def test_create(self): self.assertTrue(allclose(vec3(), (0, 0, 0))) self.assertTrue(allclose(vec3(1, 2, 3), (1, 2, 3)))
def view_matrix(self): # TODO, cache? return mat4_look_at(self.transform.loc, self._target, vec3(0, 1, 0))
def test_dot(self): self.assertEqual(dot(vec3(3, 5, 7), vec3(2, 4, 6)), 68)
def test_magnitude(self): vec = vec3(2, 4, 6) self.assertEqual(magnitude_squared(vec), 56) self.assertAlmostEqual(magnitude(vec), 7.483315)
def test_magnitude(self): vec = vec3(2, 4, 6) self.assertEqual(magnitude_squared(vec), 56) self.assertAlmostEqual(magnitude(vec), 7.483315)
def test_create(self): self.assertTrue(allclose(vec3(), (0, 0, 0))) self.assertTrue(allclose(vec3(1, 2, 3), (1, 2, 3)))
def __init__(self): self._loc = vec3() self._rot = quat4f() self._scale = vec3_from_scalar(1)