code: def r_mat_to_euler_angles(R):
      """
      Extract Euler angles from a 3x3 rotation matrix using the ZYX sequence.
      Returns the angles in radians.
      """
  
      # Check for gimbal lock: singular cases
      if abs(R[2, 0]) == 1:
          # Gimbal lock exists
          yaw = 0  # Set yaw to 0 and compute the others
          if R[2, 0] == -1:  # Directly looking up
              pitch = np.pi / 2
              roll = yaw + np.arctan2(R[0, 1], R[0, 2])
          else:  # Directly looking down
              pitch = -np.pi / 2
              roll = -yaw + np.arctan2(-R[0, 1], -R[0, 2])
      else:
          # Not at the singularities
          pitch = -np.arcsin(R[2, 0])
          roll = np.arctan2(
              R[2, 1] / np.cos(pitch), R[2, 2] / np.cos(pitch)
          )
          yaw = np.arctan2(
              R[1, 0] / np.cos(pitch), R[0, 0] / np.cos(pitch)
          )
  
      if np.isclose(roll, np.pi):
          roll = 0
      if np.isclose(pitch, np.pi):
          pitch = 0
      if np.isclose(yaw, np.pi):
          yaw = 0
  
      return roll, pitch, yaw
  
imports: import numpy
  import pytest
  import vtk
  import pyvista
  from pyvista import examples
  from pyvista.core.errors import VTKVersionError
  from pyvista.plotting.affine_widget import DARK_YELLOW, get_angle, ray_plane_intersection
label_desc: Use image node_id 1 for calling a global function with example usage: r_mat_to_euler_angles(R) and returns: roll,  pitch,  yaw