class TestModelViewNormalMatrices(unittest.TestCase): def setUp(self): self.phys = Physical(position=[10, 20, 30], rotation=(90, 0, 0), scale=3) def test_correct_modelmatrix(self): correct_modelmat = np.array([[3., 0., 0., 10.], [0., 0., -3., 20.], [0., 3., 0., 30.], [0., 0., 0., 1.]], dtype=np.float32) self.assertTrue( np.isclose(self.phys.model_matrix, correct_modelmat, rtol=1e-3, atol=1e-3).all()) def test_correct_normalmatrix(self): correct_normalmat = np.linalg.inv(self.phys.model_matrix.T) self.assertTrue( np.isclose(self.phys.normal_matrix, correct_normalmat, rtol=1e-3, atol=1e-3).all()) def test_correct_viewmatrix(self): correct_modelmat_without_scale = np.array( [[1., 0., 0., 10.], [0., 0., -1., 20.], [0., 1., 0., 30.], [0., 0., 0., 1.]], dtype=np.float32) correct_viewmat = np.linalg.inv(correct_modelmat_without_scale) self.assertTrue( np.isclose(self.phys.view_matrix, correct_viewmat, rtol=1e-3, atol=1e-3).all()) def test_update_doesnt_alter_mats(self): modelmat, normalmat, viewmat = self.phys.model_matrix.copy( ), self.phys.normal_matrix.copy(), self.phys.view_matrix.copy() self.phys.update() self.assertTrue((modelmat == self.phys.model_matrix).all()) self.assertTrue((normalmat == self.phys.normal_matrix).all()) self.assertTrue((viewmat == self.phys.view_matrix).all())
class TestModelViewNormalMatrices(unittest.TestCase): def setUp(self): self.phys = Physical(position=[10, 20, 30], rotation=(90, 0, 0), scale=3) def test_correct_modelmatrix(self): correct_modelmat = np.array([[3., 0., 0., 10.], [0., 0., -3., 20.], [0., 3., 0., 30.], [0., 0., 0., 1.]], dtype=np.float32) self.assertTrue( np.isclose(self.phys.model_matrix, correct_modelmat, rtol=1e-3, atol=1e-3).all()) def test_correct_normalmatrix(self): correct_normalmat = np.linalg.inv(self.phys.model_matrix.T) self.assertTrue( np.isclose(self.phys.normal_matrix, correct_normalmat, rtol=1e-3, atol=1e-3).all()) def test_correct_viewmatrix(self): correct_modelmat_without_scale = np.array( [[1., 0., 0., 10.], [0., 0., -1., 20.], [0., 1., 0., 30.], [0., 0., 0., 1.]], dtype=np.float32) correct_viewmat = np.linalg.inv(correct_modelmat_without_scale) self.assertTrue( np.isclose(self.phys.view_matrix, correct_viewmat, rtol=1e-3, atol=1e-3).all()) def test_update_doesnt_alter_mats(self): modelmat, normalmat, viewmat = self.phys.model_matrix.copy( ), self.phys.normal_matrix.copy(), self.phys.view_matrix.copy() self.phys.update() self.assertTrue((modelmat == self.phys.model_matrix).all()) self.assertTrue((normalmat == self.phys.normal_matrix).all()) self.assertTrue((viewmat == self.phys.view_matrix).all()) def test_look_at_makes_correct_viewmatrix(self): """Makes sure that the projection of a looked-at point is centered onscreen.""" for _ in range(200): phys = Physical(position=np.random.uniform(-3, 3, size=3), rotation=np.random.uniform(-3, 3, size=3), scale=np.random.uniform(-5, 5, size=3)) x, y, z = np.random.uniform(-5, 5, size=3) phys.look_at(x, y, z) view_projection = np.dot(phys.view_matrix, np.matrix([x, y, z, 1]).T) self.assertTrue( np.isclose(view_projection[:2], 0, atol=1e-4).all())