code: def compute_triangle_matrices(
      clip_space_vertices: type_alias.TensorLike,
      triangles: type_alias.TensorLike,
  ) -> tf.Tensor:
      """Computes per-triangle matrices used in barycentric coordinate calculation.
  
      The result corresponds to the inverse matrix from equation (4) in the paper
      "Triangle Scan Conversion using 2D Homogeneous Coordinates". Our matrix
      inverses are not divided by the determinant, only multiplied by its sign. The
      division happens in compute_barycentric_coordinates.
  
      Args:
        clip_space_vertices: float32 tensor with shape [vertex_count, 4] containing
          vertex positions in clip space (x, y, z, w).
        triangles: 2-D int32 tensor with shape [triangle_count, 3]. Each triplet
          contains a triangle's vertex indices in counter-clockwise order.
  
      Returns:
        3-D float32 tensor with shape [3, 3, triangle_count] containing per-triangle
        matrices.
      """
      # First make a vertex tensor of size [triangle_count, 3, 3], where the last
      # dimension contains x, y, w coordinates of the corresponding vertex in each
      # triangle
      xyw = tf.stack(
          [
              clip_space_vertices[:, 0],
              clip_space_vertices[:, 1],
              clip_space_vertices[:, 3],
          ],
          axis=1,
      )
      xyw = tf.gather(xyw, triangles)
      xyw = tf.transpose(xyw, perm=[0, 2, 1])
      # Compute the sub-determinants.
      d11 = xyw[:, 1, 1] * xyw[:, 2, 2] - xyw[:, 1, 2] * xyw[:, 2, 1]
      d21 = xyw[:, 1, 2] * xyw[:, 2, 0] - xyw[:, 1, 0] * xyw[:, 2, 2]
      d31 = xyw[:, 1, 0] * xyw[:, 2, 1] - xyw[:, 1, 1] * xyw[:, 2, 0]
      d12 = xyw[:, 2, 1] * xyw[:, 0, 2] - xyw[:, 2, 2] * xyw[:, 0, 1]
      d22 = xyw[:, 2, 2] * xyw[:, 0, 0] - xyw[:, 2, 0] * xyw[:, 0, 2]
      d32 = xyw[:, 2, 0] * xyw[:, 0, 1] - xyw[:, 2, 1] * xyw[:, 0, 0]
      d13 = xyw[:, 0, 1] * xyw[:, 1, 2] - xyw[:, 0, 2] * xyw[:, 1, 1]
      d23 = xyw[:, 0, 2] * xyw[:, 1, 0] - xyw[:, 0, 0] * xyw[:, 1, 2]
      d33 = xyw[:, 0, 0] * xyw[:, 1, 1] - xyw[:, 0, 1] * xyw[:, 1, 0]
      matrices = tf.stack(
          [[d11, d12, d13], [d21, d22, d23], [d31, d32, d33]]
      )
      # Multiply by the sign of the determinant, avoiding divide by zero.
      determinant = (
          xyw[:, 0, 0] * d11 + xyw[:, 1, 0] * d12 + xyw[:, 2, 0] * d13
      )
      sign = tf.sign(determinant) + tf.cast(
          determinant == 0, tf.float32
      )
      matrices = sign * matrices
      return matrices
  
file_path: graphics/tensorflow_graphics/rendering/barycentrics.py
imports: from typing import Tuple
  import tensorflow
  from tensorflow_graphics.rendering import framebuffer
  from tensorflow_graphics.util import shape
  from tensorflow_graphics.util import type_alias
label_desc: Use image node_id 3 for calling a global function with example usage: compute_triangle_matrices(clip_space_vertices, triangles) and returns: matrices