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)
Пример #2
0
 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)
Пример #4
0
 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
Пример #5
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))
Пример #6
0
    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)
Пример #7
0
    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)
Пример #8
0
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)),
    ))
Пример #9
0
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)
Пример #10
0
    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
Пример #11
0
    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)]
Пример #13
0
 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)]
Пример #15
0
 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)
Пример #16
0
 def test_normalized(self):
     self.assertTrue(allclose(normalized(vec3(0, 0, 4)), vec3(0, 0, 1)))
Пример #17
0
 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)
Пример #18
0
 def test_normalized(self):
     self.assertTrue(allclose(normalized(vec3(0, 0, 4)), vec3(0, 0, 1)))
Пример #19
0
 def test_dot(self):
     self.assertEqual(dot(vec3(3, 5, 7), vec3(2, 4, 6)), 68)
Пример #20
0
 def view_matrix(self):
     # TODO, cache?
     return mat4_look_at(self.transform.loc,
                         self._target,
                         vec3(0, 1, 0))
Пример #21
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))
Пример #22
0
 def test_create(self):
     self.assertTrue(allclose(vec3(), (0, 0, 0)))
     self.assertTrue(allclose(vec3(1, 2, 3), (1, 2, 3)))
Пример #23
0
 def view_matrix(self):
     # TODO, cache?
     return mat4_look_at(self.transform.loc, self._target, vec3(0, 1, 0))
Пример #24
0
 def test_dot(self):
     self.assertEqual(dot(vec3(3, 5, 7), vec3(2, 4, 6)), 68)
Пример #25
0
 def test_magnitude(self):
     vec = vec3(2, 4, 6)
     self.assertEqual(magnitude_squared(vec), 56)
     self.assertAlmostEqual(magnitude(vec), 7.483315)
Пример #26
0
 def test_magnitude(self):
     vec = vec3(2, 4, 6)
     self.assertEqual(magnitude_squared(vec), 56)
     self.assertAlmostEqual(magnitude(vec), 7.483315)
Пример #27
0
 def test_create(self):
     self.assertTrue(allclose(vec3(), (0, 0, 0)))
     self.assertTrue(allclose(vec3(1, 2, 3), (1, 2, 3)))
Пример #28
0
 def __init__(self):
     self._loc = vec3()
     self._rot = quat4f()
     self._scale = vec3_from_scalar(1)