def test_brdf_exceptions_raised(self): """Tests that the exceptions are raised correctly.""" direction_incoming_light = np.random.uniform(-1.0, 1.0, size=(3, )) direction_outgoing_light = np.random.uniform(-1.0, 1.0, size=(3, )) surface_normal = np.random.uniform(-1.0, 1.0, size=(3, )) shininess = np.random.uniform(0.0, 1.0, size=(1, )) albedo = np.random.uniform(0.0, 1.0, (3, )) with self.subTest( name="assert_on_direction_incoming_light_not_normalized"): with self.assertRaises(tf.errors.InvalidArgumentError): self.evaluate( phong.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, shininess, albedo)) direction_incoming_light /= np.linalg.norm(direction_incoming_light, axis=-1) with self.subTest( name="assert_on_direction_outgoing_light_not_normalized"): with self.assertRaises(tf.errors.InvalidArgumentError): self.evaluate( phong.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, shininess, albedo)) direction_outgoing_light /= np.linalg.norm(direction_outgoing_light, axis=-1) with self.subTest(name="assert_on_surface_normal_not_normalized"): with self.assertRaises(tf.errors.InvalidArgumentError): self.evaluate( phong.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, shininess, albedo)) surface_normal /= np.linalg.norm(surface_normal, axis=-1) with self.subTest(name="assert_on_albedo_not_normalized"): albedo = np.random.uniform(-10.0, -sys.float_info.epsilon, (3, )) with self.assertRaises(tf.errors.InvalidArgumentError): self.evaluate( phong.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, shininess, albedo)) albedo = np.random.uniform(sys.float_info.epsilon, 10.0, (3, )) with self.assertRaises(tf.errors.InvalidArgumentError): self.evaluate( phong.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, shininess, albedo))
def test_brdf_random(self, incoming_yz, outgoing_yz, ratio): tensor_size = np.random.randint(3) tensor_shape = np.random.randint(1, 10, size=(tensor_size)).tolist() shininess = np.zeros(shape=tensor_shape + [1]) albedo = np.random.uniform(low=0.0, high=1.0, size=tensor_shape + [3]) direction_incoming_light = np.random.uniform(low=-1.0, high=1.0, size=tensor_shape + [3]) direction_outgoing_light = np.random.uniform(low=-1.0, high=1.0, size=tensor_shape + [3]) surface_normal = np.array((0.0, 1.0, 1.0)) direction_incoming_light[..., 1:3] = incoming_yz direction_outgoing_light[..., 1:3] = outgoing_yz direction_incoming_light = direction_incoming_light / np.linalg.norm( direction_incoming_light, axis=-1, keepdims=True) direction_outgoing_light = direction_outgoing_light / np.linalg.norm( direction_outgoing_light, axis=-1, keepdims=True) surface_normal = surface_normal / np.linalg.norm( surface_normal, axis=-1, keepdims=True) gt = albedo * ratio pred = phong.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, shininess, albedo) self.assertAllClose(gt, pred)
def test_brdf_jacobian_random(self): """Tests the Jacobian of brdf.""" tensor_size = np.random.randint(3) tensor_shape = np.random.randint(1, 10, size=(tensor_size)).tolist() direction_incoming_light_init = np.random.uniform(-1.0, 1.0, size=tensor_shape + [3]) direction_outgoing_light_init = np.random.uniform(-1.0, 1.0, size=tensor_shape + [3]) surface_normal_init = np.random.uniform(-1.0, 1.0, size=tensor_shape + [3]) shininess_init = np.random.uniform(size=tensor_shape + [1]) albedo_init = np.random.random(tensor_shape + [3]) direction_incoming_light = tf.convert_to_tensor( value=direction_incoming_light_init) direction_outgoing_light = tf.convert_to_tensor( value=direction_outgoing_light_init) surface_normal = tf.convert_to_tensor(value=surface_normal_init) shininess = tf.convert_to_tensor(value=shininess_init) albedo = tf.convert_to_tensor(value=albedo_init) y = phong.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, shininess, albedo) self.assert_jacobian_is_correct(direction_incoming_light, direction_incoming_light_init, y, atol=1e-5, delta=1e-9) self.assert_jacobian_is_correct(direction_outgoing_light, direction_outgoing_light_init, y, atol=1e-5, delta=1e-9) self.assert_jacobian_is_correct(surface_normal, surface_normal_init, y, atol=1e-5, delta=1e-9) self.assert_jacobian_is_correct(shininess, shininess_init, y, atol=1e-5, delta=1e-9) self.assert_jacobian_is_correct(albedo, albedo_init, y, atol=1e-5, delta=1e-9)
def test_brdf_jacobian_preset(self): delta = 1e-5 direction_incoming_light_init = np.array((delta, -1.0, 0.0)) direction_outgoing_light_init = np.array((delta, 1.0, 0.0)) surface_normal_init = np.array((1.0, 0.0, 0.0)) shininess_init = np.array((1.0, )) albedo_init = np.array((1.0, 1.0, 1.0)) direction_incoming_light = tf.convert_to_tensor( value=direction_incoming_light_init) direction_outgoing_light = tf.convert_to_tensor( value=direction_outgoing_light_init) surface_normal = tf.convert_to_tensor(value=surface_normal_init) shininess = tf.convert_to_tensor(value=shininess_init) albedo = tf.convert_to_tensor(value=albedo_init) y = phong.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, shininess, albedo) self.assert_jacobian_is_correct(direction_incoming_light, direction_incoming_light_init, y, delta=delta / 10.0) self.assert_jacobian_is_correct(direction_outgoing_light, direction_outgoing_light_init, y, delta=delta / 10.0) self.assert_jacobian_is_correct(surface_normal, surface_normal_init, y, delta=delta / 10.0) self.assert_jacobian_is_correct(shininess, shininess_init, y, delta=delta / 10.0) self.assert_jacobian_is_correct(albedo, albedo_init, y, delta=delta / 10.0)