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, )) 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( lambertian.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, 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( lambertian.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, 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( lambertian.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, 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( lambertian.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, albedo)) albedo = np.random.uniform(sys.float_info.epsilon, 10.0, (3, )) with self.assertRaises(tf.errors.InvalidArgumentError): self.evaluate( lambertian.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, 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() 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 = lambertian.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, 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]) 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) albedo = tf.convert_to_tensor(value=albedo_init) y = lambertian.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, albedo) self.assert_jacobian_is_correct(albedo, albedo_init, y)
def test_brdf_jacobian_preset(self): direction_incoming_light_init = np.array((0.0, 1.0, 0.0)) direction_outgoing_light_init = np.array((0.0, 1.0, 0.0)) surface_normal_init = np.array((1.0, 0.0, 0.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) albedo = tf.convert_to_tensor(value=albedo_init) y = lambertian.brdf(direction_incoming_light, direction_outgoing_light, surface_normal, albedo) self.assert_jacobian_is_correct(albedo, albedo_init, y)
def lamertian_brdf_fn(albedo): return lambertian.brdf(direction_incoming_light_init, direction_outgoing_light_init, surface_normal_init, albedo)