//  [python]
def fit_objects_to_instance_id_points(mesh_inputs,
                                      object_inputs,
                                      epsilon=0.001):
  """Fits objects to points based on their instance ids.

  Args:
    mesh_inputs: A dictionary that contains point properties.
    object_inputs: A dictionary that contains object information.
    epsilon: A very small value.
  """
  if (standard_fields.InputDataFields.object_instance_id_points not in
      mesh_inputs):
    raise ValueError('object_instance_id_points not in mesh_inputs.')
  if standard_fields.InputDataFields.object_class_points not in mesh_inputs:
    raise ValueError('object_class_points not in mesh_inputs.')
  if standard_fields.InputDataFields.point_positions not in mesh_inputs:
    raise ValueError('point_positions not in mesh_inputs.')
  unique_instance_ids, point_instance_idx = tf.unique(
      tf.reshape(
          mesh_inputs[
              standard_fields.InputDataFields.object_instance_id_points], [-1]),
      out_idx=tf.int32)

  unique_instance_ids = tf.cast(unique_instance_ids, dtype=tf.int32)
  num_objects = tf.shape(unique_instance_ids)[0]

  def get_object_i(i):
    """Function that fits a box to each set of instance points."""
    object_instance_id_i = unique_instance_ids[i]
    points_i_mask = tf.equal(point_instance_idx, i)
    point_positions_i = tf.boolean_mask(
        mesh_inputs[standard_fields.InputDataFields.point_positions],
        points_i_mask)
    point_classes_i = tf.boolean_mask(
        mesh_inputs[standard_fields.InputDataFields.object_class_points],
        points_i_mask)
    min_object_xyz_i = tf.reduce_min(point_positions_i, axis=0)
    max_object_xyz_i = tf.reduce_max(point_positions_i, axis=0)
    object_center_i = (min_object_xyz_i + max_object_xyz_i) / 2.0
    object_size_i = max_object_xyz_i - min_object_xyz_i
    object_length_i = tf.expand_dims(object_size_i[0], axis=0)
    object_width_i = tf.expand_dims(object_size_i[1], axis=0)
    object_height_i = tf.expand_dims(object_size_i[2], axis=0)
    object_class_i = point_classes_i[0, :]
    return (object_center_i, object_length_i, object_width_i, object_height_i,
            object_class_i, object_instance_id_i)

  (object_inputs[standard_fields.InputDataFields.objects_center],
   object_inputs[standard_fields.InputDataFields.objects_length],
   object_inputs[standard_fields.InputDataFields.objects_width],
   object_inputs[standard_fields.InputDataFields.objects_height],
   object_inputs[standard_fields.InputDataFields.objects_class],
   unique_instance_ids) = tf.map_fn(
       fn=get_object_i,
       elems=tf.range(num_objects),
       dtype=(tf.float32, tf.float32, tf.float32, tf.float32, mesh_inputs[
           standard_fields.InputDataFields.object_class_points].dtype,
              unique_instance_ids.dtype))
  object_inputs[standard_fields.InputDataFields.objects_length] = tf.maximum(
      object_inputs[standard_fields.InputDataFields.objects_length], epsilon)
  object_inputs[standard_fields.InputDataFields.objects_height] = tf.maximum(
      object_inputs[standard_fields.InputDataFields.objects_height], epsilon)
  object_inputs[standard_fields.InputDataFields.objects_width] = tf.maximum(
      object_inputs[standard_fields.InputDataFields.objects_width], epsilon)
  object_inputs[
      standard_fields.InputDataFields.objects_rotation_matrix] = tf.tile(
          tf.expand_dims(tf.eye(3, dtype=tf.float32), axis=0),
          [num_objects, 1, 1])

  foreground_object_mask = tf.greater(
      tf.reshape(object_inputs[standard_fields.InputDataFields.objects_class],
                 [-1]), 0)
  for key in object_inputs:
    object_inputs[key] = tf.boolean_mask(object_inputs[key],
                                         foreground_object_mask)
  unique_instance_ids = tf.boolean_mask(unique_instance_ids,
                                        foreground_object_mask)

  def add_instance_id_i(instance_id_points_i, i):
    """Adds the mapped values for the instance id i."""
    current_instance_id = unique_instance_ids[i]
    instance_id_addition_i = tf.cast(
        tf.equal(
            mesh_inputs[standard_fields.InputDataFields
                        .object_instance_id_points], current_instance_id),
        dtype=tf.int32) * (
            i + 1)
    instance_id_points_i = tf.maximum(instance_id_points_i,
                                      instance_id_addition_i)
    i += 1
    return instance_id_points_i, i

  num_objects = tf.shape(unique_instance_ids)[0]
  cond = lambda instance_id_points_i, i: tf.less(i, num_objects)
  init_inputs = [
      tf.zeros_like(mesh_inputs[
          standard_fields.InputDataFields.object_instance_id_points]),
      tf.constant(0, dtype=tf.int32)
  ]
  mesh_inputs[standard_fields.InputDataFields
              .object_instance_id_points], _ = tf.while_loop(
                  cond=cond, body=add_instance_id_i, loop_vars=init_inputs)