def test_camera_is_picklable(): for _ in range(2): phys = Camera() phys.position.xyz = np.random.uniform(-5, 5, 3) phys.rotation.xyz = np.random.uniform(-5, 5, 3) with NamedTemporaryFile('wb', delete=False) as f: phys.to_pickle(f.name) with open(f.name, 'rb') as f: phys2 = Camera.from_pickle(f.name) assert phys.position.xyz == phys2.position.xyz assert phys.rotation.xyz == phys2.rotation.xyz assert np.isclose(phys.model_matrix[:3, -1], phys2.model_matrix[:3, -1]).all() phys.position.xyz = 5, 5, 5 assert not np.isclose(phys.position.xyz, phys2.position.xyz).all() assert not np.isclose(phys.model_matrix[:3, -1], phys2.model_matrix[:3, -1]).all() assert np.isclose(phys.model_matrix, phys.uniforms['model_matrix']).all() assert np.isclose(phys.view_matrix, phys.uniforms['view_matrix']).all() phys2.position.xyz = 5, 5, 5 assert np.isclose(phys.position.xyz, phys2.position.xyz).all() assert np.isclose(phys2.position.xyz, phys2.model_matrix[:3, -1]).all() assert np.isclose(phys2.model_matrix, phys2.uniforms['model_matrix']).all() assert np.isclose(phys2.view_matrix, phys2.uniforms['view_matrix']).all() assert np.isclose(phys.view_matrix, phys2.view_matrix).all() assert np.isclose(phys.model_matrix[:3, -1], phys2.model_matrix[:3, -1]).all() phys2.position.xyz = 10, 10, 10 assert np.isclose(phys2.position.xyz, phys2.uniforms['model_matrix'][:3, -1]).all() assert np.isclose(phys.projection.projection_matrix, phys2.projection.projection_matrix).all() assert np.isclose(phys2.projection.projection_matrix, phys2.uniforms['projection_matrix']).all() phys2.projection.fov_y = 30 assert not np.isclose(phys.projection.projection_matrix, phys2.projection.projection_matrix).all() assert np.isclose(phys2.projection.projection_matrix, phys2.uniforms['projection_matrix']).all()
def test_projection_matrix_updates_when_assigning_new_projection(): cam = Camera() assert (cam.projection_matrix == cam.projection.projection_matrix).all() old_projmat = cam.projection_matrix.copy() cam.projection = OrthoProjection() assert (cam.projection_matrix == cam.projection.projection_matrix).all() assert not (cam.projection_matrix == old_projmat).all() assert not (cam.projection.projection_matrix == old_projmat).all() cam.projection = PerspectiveProjection() assert (cam.projection_matrix == old_projmat).all()
def test_camera_has_all_uniforms(): cam = Camera() for name in [ 'projection_matrix', 'model_matrix', 'view_matrix', 'camera_position' ]: assert name in cam.uniforms
def test_cameras_are_cameragroup_childrens(): cameras_n = np.random.randint(1, 11) cameras = [Camera() for _ in range(cameras_n)] cam = CameraGroup(cameras=cameras) for camera in cam.cameras: assert camera in cam.children
def test_projection_shift(): for _ in range(50): x, y = np.random.uniform(-5, 5, size=2) proj = PerspectiveProjection(x_shift=x, y_shift=y) smat = proj._get_shift_matrix() assert np.isclose(smat[0, 2], x) assert np.isclose(smat[1, 2], y) proj = PerspectiveProjection() old_pmat = proj.projection_matrix.copy() proj.x_shift = x proj.y_shift = y smat = proj._get_shift_matrix() assert np.isclose(smat[0, 2], x) assert np.isclose(smat[1, 2], y) assert not np.isclose(old_pmat, proj.projection_matrix).all() cam = Camera() old_pmat = cam.projection_matrix.copy() assert np.isclose(old_pmat, cam.projection.projection_matrix).all() assert np.isclose(old_pmat, cam.projection_matrix).all() cam.projection.x_shift = x cam.projection.y_shift = y assert not np.isclose(old_pmat, cam.projection_matrix).all() proj = PerspectiveProjection(x_shift=x) cam = Camera(projection=proj) old_pmat = cam.projection_matrix.copy() assert np.isclose(old_pmat, cam.projection.projection_matrix).all() assert np.isclose(old_pmat, cam.projection_matrix).all() assert np.isclose(old_pmat, cam.uniforms['projection_matrix']).all() cam.projection.y_shift = y assert not np.isclose(old_pmat, cam.projection_matrix).all() assert not np.isclose(old_pmat, cam.uniforms['projection_matrix']).all() proj = PerspectiveProjection() old_smat = proj._get_shift_matrix() proj.fov_y = 10. assert np.isclose(old_smat, proj._get_shift_matrix()).all()
def test_camera_physical_attributes(): cam = Camera() assert np.isclose(cam.position.xyz, (0, 0, 0)).all() assert np.isclose(cam.rotation.xyz, (0, 0, 0)).all() cam.position.x = 1 assert np.isclose(cam.position.xyz, (1, 0, 0)).all() assert np.all(cam.view_matrix[:3, -1] == tuple(-el for el in cam.position.xyz)) assert np.all(cam.model_matrix[:3, -1] == cam.position.xyz) cam.rotation.y = 30 assert np.isclose(cam.rotation.xyz, (0, 30, 0)).all() assert cam.rotation.y == 30
def test_projection_attributes_change_cameras_projection_matrix_uniform(): cam = Camera() for proj in [(), PerspectiveProjection(), PerspectiveProjection()]: if isinstance(proj, int): cam, proj = Camera(name='Early'), PerspectiveProjection() cam.projection = proj elif proj: cam = Camera(projection=proj) old_projmat = cam.projection_matrix.copy() old_pm_uni = cam.uniforms['projection_matrix'].copy() assert np.all(old_projmat == old_pm_uni) cam.projection.aspect = np.random.random() assert np.any(cam.projection_matrix != old_projmat) assert np.any(cam.uniforms['projection_matrix'] != old_pm_uni) assert np.all( cam.projection_matrix == cam.uniforms['projection_matrix']) assert np.all( cam.projection.projection_matrix == cam.projection_matrix) assert np.all(cam.uniforms['projection_matrix'] == cam.projection.projection_matrix)
def test_default_projection_is_perspective(): cam = Camera() assert isinstance(cam.projection, PerspectiveProjection)
def test_cameraggroups_has_all_cameras(): cameras_n = np.random.randint(1, 11) cameras = [Camera() for _ in range(cameras_n)] cam = CameraGroup(cameras=cameras) assert len(cam.cameras) == cameras_n