def overlap_mask(depth1,
                 pose1_c2w,
                 depth2,
                 pose2_c2w,
                 intrinsics):
  """Compute the overlap masks of two views using triangulation.

  The masks have the same shape of the input images. A pixel value is true if it
  can be seen by both cameras.

  Args:
    depth1: [HEIGHT, WIDTH, 1] the depth map of the first view.
    pose1_c2w: [3, 4] camera pose matrix (camera to world) of the first view.
      pose1_c2w[:, :3] is the rotation and pose1_c2w[:, -1] is the translation.
    depth2: [HEIGHT, WIDTH, 1] the depth map of the second view.
    pose2_c2w: [3, 4] camera pose matrix (camera to world) of the second view.
      pose1_c2w[:, :3] is the rotation and pose1_c2w[:, -1] is the translation.
    intrinsics: [3, 3] camera's intrinsic matrix.

  Returns:
    [HEIGHT, WIDTH] two overlap masks of the two inputs respectively.
  """

  pose1_w2c = tf.matrix_inverse(
      tf.concat([pose1_c2w, tf.constant([[0., 0., 0., 1.]])], 0))[:3]
  pose2_w2c = tf.matrix_inverse(
      tf.concat([pose2_c2w, tf.constant([[0., 0., 0., 1.]])], 0))[:3]

  p_world1 = image_to_world_projection(depth1, intrinsics, pose1_c2w)
  p_image1_in_2, z1_c2 = world_to_image_projection(
      p_world1, intrinsics, pose2_w2c)

  p_world2 = image_to_world_projection(depth2, intrinsics, pose2_c2w)
  p_image2_in_1, z2_c1 = world_to_image_projection(
      p_world2, intrinsics, pose1_w2c)

  shape = depth1.shape.as_list()
  height, width = shape[0], shape[1]
  height = tf.cast(height, tf.float32)
  width = tf.cast(width, tf.float32)
  # Error tolerance.
  eps = 1e-4
  # check the object seen by camera 2 is also projected to camera 1's image
  # plane and in front of the camera 1.
  mask_h2_in_1 = tf.logical_and(
      tf.less_equal(p_image2_in_1[:, :, 1], height+eps),
      tf.greater_equal(p_image2_in_1[:, :, 1], 0.-eps))
  mask_w2_in_1 = tf.logical_and(
      tf.less_equal(p_image2_in_1[:, :, 0], width+eps),
      tf.greater_equal(p_image2_in_1[:, :, 0], 0.-eps))
  # check the projected points are within the image boundaries and in front of
  # the camera.
  mask2_in_1 = tf.logical_and(
      tf.logical_and(mask_h2_in_1, mask_w2_in_1), tf.squeeze(z2_c1, -1) > 0)

  # check the object seen by camera 1 is also projected to camera 2's image
  # plane and in front of the camera 2.
  mask_h1_in_2 = tf.logical_and(
      tf.less_equal(p_image1_in_2[:, :, 1], height+eps),
      tf.greater_equal(p_image1_in_2[:, :, 1], 0.-eps))
  mask_w1_in_2 = tf.logical_and(
      tf.less_equal(p_image1_in_2[:, :, 0], width+eps),
      tf.greater_equal(p_image1_in_2[:, :, 0], 0.-eps))
  # check the projected points are within the image boundaries and in front of
  # the camera.
  mask1_in_2 = tf.logical_and(
      tf.logical_and(mask_h1_in_2, mask_w1_in_2), tf.squeeze(z1_c2, -1) > 0)

  return mask1_in_2, mask2_in_1