def test_vertex_normals_random(self): """Tests the computation of vertex normals for a regular octahedral.""" tensor_vertex_size = np.random.randint(1, 3) tensor_out_shape = np.random.randint(1, 5, size=tensor_vertex_size) tensor_out_shape = tensor_out_shape.tolist() with self.subTest(name="triangular_faces"): vertex_on_axes = np.array( ((0., 0., 1), (1., 0., 0.), (0., 1., 0.), (0., 0., -1.), (-1., 0., 0.), (0., -1., 0.)), dtype=np.float32) vertex_on_axes = vertex_on_axes.reshape([1] * tensor_vertex_size + [6, 3]) index_init = np.array(((0, 1, 2), (0, 2, 4), (0, 4, 5), (0, 5, 1), (3, 2, 1), (3, 4, 2), (3, 5, 4), (3, 1, 5)), dtype=np.int32) index_init = index_init.reshape([1] * tensor_vertex_size + [8, 3]) index_init = np.tile(index_init, tensor_out_shape + [1, 1]) vertex_scale = np.random.uniform(0.5, 5., tensor_out_shape + [1] * 2) vertex_init = vertex_on_axes * vertex_scale expected = vertex_on_axes * (vertex_scale * 0. + 1.) vertex_tensor = tf.convert_to_tensor(value=vertex_init) index_tensor = tf.convert_to_tensor(value=index_init) self.assertAllClose( normals.vertex_normals(vertex_tensor, index_tensor), expected) with self.subTest(name="polygon_faces"): num_vertices = np.random.randint(4, 8) poly_vertices = [] rad_step = np.pi * 2. / num_vertices for i in range(num_vertices): poly_vertices.append( [np.cos(i * rad_step), np.sin(i * rad_step), 0]) vertex_init = np.array(poly_vertices, dtype=np.float32) vertex_init = vertex_init.reshape([1] * tensor_vertex_size + [-1, 3]) vertex_init = vertex_init * vertex_scale index_init = np.arange(num_vertices, dtype=np.int32) index_init = index_init.reshape([1] * tensor_vertex_size + [1, -1]) index_init = np.tile(index_init, tensor_out_shape + [1, 1]) expected = np.array((0., 0., 1.), dtype=np.float32) expected = expected.reshape([1] * tensor_vertex_size + [1, 3]) expected = np.tile(expected, tensor_out_shape + [num_vertices, 1]) vertex_tensor = tf.convert_to_tensor(value=vertex_init) index_tensor = tf.convert_to_tensor(value=index_init) self.assertAllClose( normals.vertex_normals(vertex_tensor, index_tensor), expected)
def test_vertex_normals_jacobian_random(self): """Test the Jacobian of the vertex normals function.""" tensor_vertex_size = np.random.randint(1, 3) tensor_out_shape = np.random.randint(1, 5, size=tensor_vertex_size) tensor_out_shape = tensor_out_shape.tolist() vertex_axis = np.array(((0., 0., 1), (1., 0., 0.), (0., 1., 0.), (0., 0., -1.), (-1., 0., 0.), (0., -1., 0.)), dtype=np.float32) vertex_axis = vertex_axis.reshape([1] * tensor_vertex_size + [6, 3]) faces = np.array(((0, 1, 2), (0, 2, 4), (0, 4, 5), (0, 5, 1), (3, 2, 1), (3, 4, 2), (3, 5, 4), (3, 1, 5)), dtype=np.int32) faces = faces.reshape([1] * tensor_vertex_size + [8, 3]) index_init = np.tile(faces, tensor_out_shape + [1, 1]) vertex_scale = np.random.uniform(0.5, 5., tensor_out_shape + [1] * 2) vertex_init = vertex_axis * vertex_scale vertex_tensor = tf.convert_to_tensor(value=vertex_init) index_tensor = tf.convert_to_tensor(value=index_init) y = normals.vertex_normals(vertex_tensor, index_tensor) self.assert_jacobian_is_correct(vertex_tensor, vertex_init, y)
def vertex_normals(vertex_tensor): return normals.vertex_normals(vertex_tensor, index_tensor)