def test_from_axis_angle_normalized_preset(self): """Tests that axis-angles can be converted to rotation matrices.""" euler_angles = test_helpers.generate_preset_test_euler_angles() axis, angle = axis_angle.from_euler(euler_angles) matrix_axis_angle = rotation_matrix_3d.from_axis_angle(axis, angle) self.assertAllEqual(rotation_matrix_3d.is_valid(matrix_axis_angle), np.ones(euler_angles.shape[0:-1] + (1, )))
def test_from_euler_random(self): """Tests that Euler angles produce the same result as axis-angle.""" angles = test_helpers.generate_random_test_euler_angles() matrix = rotation_matrix_3d.from_euler(angles) tensor_tile = angles.shape[:-1] x_axis = np.tile(td.AXIS_3D_X, tensor_tile + (1,)) y_axis = np.tile(td.AXIS_3D_Y, tensor_tile + (1,)) z_axis = np.tile(td.AXIS_3D_Z, tensor_tile + (1,)) x_angle = np.expand_dims(angles[..., 0], axis=-1) y_angle = np.expand_dims(angles[..., 1], axis=-1) z_angle = np.expand_dims(angles[..., 2], axis=-1) x_rotation = rotation_matrix_3d.from_axis_angle(x_axis, x_angle) y_rotation = rotation_matrix_3d.from_axis_angle(y_axis, y_angle) z_rotation = rotation_matrix_3d.from_axis_angle(z_axis, z_angle) expected_matrix = tf.matmul(z_rotation, tf.matmul(y_rotation, x_rotation)) self.assertAllClose(expected_matrix, matrix, rtol=1e-3)
def test_from_axis_angle_preset(self): """Checks that Euler angles can be retrieved from axis-angle.""" preset_euler_angles = test_helpers.generate_preset_test_euler_angles() random_matrix = rotation_matrix_3d.from_euler(preset_euler_angles) random_axis, random_angle = axis_angle.from_euler(preset_euler_angles) predicted_matrix = rotation_matrix_3d.from_axis_angle( random_axis, random_angle) self.assertAllClose(random_matrix, predicted_matrix, atol=1e-3)
def place_frustum_sampling_points_at_blender_camera(sampling_points, camera_rotation_matrix, camera_translation_vector): """Transform the TF-Graphics frustum points to the blender camera position.""" blender_rotation_matrix = rotation_matrix_3d.from_axis_angle(X_AXIS, -PI) camera_translation_vector = tf.matmul(blender_rotation_matrix, camera_translation_vector) # The original frustum points are mirrored in the x axis and rotated 270 mirror_x_matrix = tf.convert_to_tensor(MIRROR_X_MATRIX) up_rotation_matrix = rotation_matrix_3d.from_axis_angle(Z_AXIS, 3 * PI_2) local_camera_transform = tf.matmul(mirror_x_matrix, up_rotation_matrix) sampling_points = tf.matmul(local_camera_transform, sampling_points, transpose_b=True) sampling_points = \ tf.matmul(tf.linalg.matrix_transpose(camera_rotation_matrix), sampling_points+camera_translation_vector) return tf.linalg.matrix_transpose(sampling_points) # [h*w*dstep, 3]
def half_rotation(rotation): """Return half of the input rotation. Args: rotation: [BATCH, 3, 3] rotation matrices. Returns: [BATCH, 3, 3] rotation matrices. """ axes, angles = axis_angle.from_rotation_matrix(rotation) return rotation_matrix_3d.from_axis_angle(axes, angles / 2)
def test_from_axis_angle_normalized_random(self): """Tests that axis-angles can be converted to rotation matrices.""" tensor_shape = np.random.randint(1, 10, size=np.random.randint(3)).tolist() random_axis = np.random.normal(size=tensor_shape + [3]) random_axis /= np.linalg.norm(random_axis, axis=-1, keepdims=True) random_angle = np.random.normal(size=tensor_shape + [1]) matrix_axis_angle = rotation_matrix_3d.from_axis_angle( random_axis, random_angle) self.assertAllEqual(rotation_matrix_3d.is_valid(matrix_axis_angle), np.ones(tensor_shape + [1]))
def test_from_axis_angle_random(self): """Tests conversion to matrix.""" tensor_shape = np.random.randint(1, 10, size=np.random.randint(3)).tolist() random_axis = np.random.normal(size=tensor_shape + [3]) random_axis /= np.linalg.norm(random_axis, axis=-1, keepdims=True) random_angle = np.random.normal(size=tensor_shape + [1]) matrix_axis_angle = rotation_matrix_3d.from_axis_angle( random_axis, random_angle) random_quaternion = quaternion.from_axis_angle(random_axis, random_angle) matrix_quaternion = rotation_matrix_3d.from_quaternion(random_quaternion) self.assertAllClose(matrix_axis_angle, matrix_quaternion, rtol=1e-3) # Checks that resulting rotation matrices are normalized. self.assertAllEqual( rotation_matrix_3d.is_valid(matrix_axis_angle), np.ones(tensor_shape + [1]))
def func(axis, angle, point): matrix = rotation_matrix_3d.from_axis_angle(axis, angle) return rotation_matrix_3d.rotate(point, matrix)
def rotate(points): """Randomly rotates a point cloud around the Y axis (UP).""" axis = tf.constant([[0, 1, 0]], dtype=tf.float32) # [1, 3] angle = tf.random.uniform((1, 1), minval=0., maxval=2 * np.pi) # [1, 1] matrix = rotation_matrix_3d.from_axis_angle(axis, angle) # [3, 3] return rotation_matrix_3d.rotate(points, matrix) # [N, 3]