pycram.utils
============

.. py:module:: pycram.utils

.. autoapi-nested-parse::

   Implementation of helper functions and classes for internal usage only.

   Functions:
   _block -- wrap multiple statements into a single block.

   Classes:
   GeneratorList -- implementation of generator list wrappers.



Classes
-------

.. autoapisummary::

   pycram.utils.bcolors
   pycram.utils.GeneratorList
   pycram.utils.suppress_stdout_stderr
   pycram.utils.RayTestUtils
   pycram.utils.ClassPropertyDescriptor


Functions
---------

.. autoapisummary::

   pycram.utils.get_rays_from_min_max
   pycram.utils.chunks
   pycram.utils._apply_ik
   pycram.utils.axis_angle_to_quaternion
   pycram.utils.adjust_camera_pose_based_on_target
   pycram.utils.get_quaternion_between_camera_and_target
   pycram.utils.transform_vector_using_pose
   pycram.utils.apply_quaternion_to_pose
   pycram.utils.get_quaternion_between_two_vectors
   pycram.utils.get_axis_angle_between_two_vectors
   pycram.utils.wxyz_to_xyzw
   pycram.utils.xyzw_to_wxyz
   pycram.utils.wxyz_to_xyzw_arr
   pycram.utils.xyzw_to_wxyz_arr
   pycram.utils.classproperty
   pycram.utils.is_iterable
   pycram.utils.lazy_product


Module Contents
---------------

.. py:function:: get_rays_from_min_max(min_bound: typing_extensions.Sequence[float], max_bound: typing_extensions.Sequence[float], step_size_in_meters: float = 0.01) -> numpy.ndarray

   Get rays from min and max bounds as an array of start and end 3D points.
   Note: The rays are not steped in the x direction as the rays are cast parallel to the x-axis.

   Example:
   >>> min_bound = [0, 0, 0]
   >>> max_bound = [1, 2, 3]
   >>> rays = get_rays_from_min_max(min_bound, max_bound, 1)
   >>> rays.shape
   (6, 3, 2)
   >>> rays
   array([
   [[0. , 1. ],
    [0. , 0. ],
    [0. , 0. ]],
   [[0. , 1. ],
    [0. , 0. ],
    [1.5, 1.5]],
   [[0. , 1. ],
    [0. , 0. ],
    [3. , 3. ]],
   [[0. , 1. ],
    [2. , 2. ],
    [0. , 0. ]],
   [[0. , 1. ],
    [2. , 2. ],
    [1.5, 1.5]],
   [[0. , 1. ],
    [2. , 2. ],
    [3. , 3. ]]
    ])

   :param min_bound: The minimum bound of the rays, a sequence of 3 floats.
   :param max_bound: The maximum bound of the rays, a sequence of 3 floats.
   :param step_size_in_meters: The step size in meters between the rays.
   :return: The rays as an array of shape (n, 3, 2) where n is number of rays, 3 is because each point has x, y, and z,
   and 2 is for the start and end points of the rays.


.. py:function:: chunks(lst: typing_extensions.List, n: int) -> typing_extensions.List

   Yield successive n-sized chunks from lst.

   :param lst: The list from which chunks should be yielded
   :param n: Size of the chunks
   :return: A list of size n from lst


.. py:class:: bcolors

   Color codes which can be used to highlight Text in the Terminal. For example,
   for warnings.
   Usage:
   Firstly import the class into the file.
   print(f'{bcolors.WARNING} Some Text {bcolors.ENDC}')


   .. py:attribute:: HEADER
      :value: '\x1b[95m'



   .. py:attribute:: OKBLUE
      :value: '\x1b[94m'



   .. py:attribute:: OKCYAN
      :value: '\x1b[96m'



   .. py:attribute:: OKGREEN
      :value: '\x1b[92m'



   .. py:attribute:: WARNING
      :value: '\x1b[93m'



   .. py:attribute:: FAIL
      :value: '\x1b[91m'



   .. py:attribute:: ENDC
      :value: '\x1b[0m'



   .. py:attribute:: BOLD
      :value: '\x1b[1m'



   .. py:attribute:: UNDERLINE
      :value: '\x1b[4m'



.. py:function:: _apply_ik(robot: pycram.world_concepts.world_object.Object, pose_and_joint_poses: typing_extensions.Tuple[pycram.datastructures.pose.PoseStamped, typing_extensions.Dict[str, float]]) -> None

   Apllies a list of joint poses calculated by an inverse kinematics solver to a robot

   :param robot: The robot the joint poses should be applied on
   :param pose_and_joint_poses: The base pose and joint states as returned by the ik solver
   :return: None


.. py:class:: GeneratorList(generator: typing_extensions.Callable)

   Implementation of generator list wrappers.

   Generator lists store the elements of a generator, so these can be fetched multiple times.

   Methods:
   get -- get the element at a specific index.
   has -- check if an element at a specific index exists.


   .. py:attribute:: _generated
      :value: []



   .. py:method:: get(index: int = 0)

      Get the element at a specific index or raise StopIteration if it doesn't exist.

      Arguments:
      index -- the index to get the element of.



   .. py:method:: has(index: int) -> bool

      Check if an element at a specific index exists and return True or False.

      Arguments:
      index -- the index to check for.



.. py:function:: axis_angle_to_quaternion(axis: typing_extensions.List, angle: float) -> typing_extensions.Tuple

   Convert axis-angle to quaternion.

   :param axis: (x, y, z) tuple representing rotation axis.
   :param angle: rotation angle in degree
   :return: The quaternion representing the axis angle


.. py:class:: suppress_stdout_stderr

   Bases: :py:obj:`object`


   A context manager for doing a "deep suppression" of stdout and stderr in
   Python, i.e. will suppress all prints, even if the print originates in a
   compiled C/Fortran sub-function.

   This will not suppress raised exceptions, since exceptions are printed
   to stderr just before a script exits, and after the context manager has
   exited (at least, I think that is why it lets exceptions through).
   Copied from https://stackoverflow.com/questions/11130156/suppress-stdout-stderr-print-from-python-functions


   .. py:attribute:: null_fds


   .. py:attribute:: save_fds


   .. py:method:: __enter__()


   .. py:method:: __exit__(*_)


.. py:function:: adjust_camera_pose_based_on_target(cam_pose: pycram.datastructures.pose.PoseStamped, target_pose: pycram.datastructures.pose.PoseStamped, camera_description: pycram.robot_description.CameraDescription) -> pycram.datastructures.pose.PoseStamped

   Adjust the given cam_pose orientation such that it is facing the target_pose, which partly depends on the
    front_facing_axis of the that is defined in the camera_description.

   :param cam_pose: The camera pose.
   :param target_pose: The target pose.
   :param camera_description: The camera description.
   :return: The adjusted camera pose.


.. py:function:: get_quaternion_between_camera_and_target(cam_pose: pycram.datastructures.pose.PoseStamped, target_pose: pycram.datastructures.pose.PoseStamped, camera_description: pycram.robot_description.CameraDescription) -> numpy.ndarray

   Get the quaternion between the camera and the target.

   :param cam_pose: The camera pose.
   :param target_pose: The target pose.
   :param camera_description: The camera description.
   :return: The quaternion between the camera and the target.


.. py:function:: transform_vector_using_pose(vector: typing_extensions.Sequence, pose) -> numpy.ndarray

   Transform a vector using a pose.

   :param vector: The vector.
   :param pose: The pose.
   :return: The transformed vector.


.. py:function:: apply_quaternion_to_pose(pose: pycram.datastructures.pose.PoseStamped, quaternion: numpy.ndarray) -> pycram.datastructures.pose.PoseStamped

   Apply a quaternion to a pose.

   :param pose: The pose.
   :param quaternion: The quaternion.
   :return: The new pose.


.. py:function:: get_quaternion_between_two_vectors(v1: numpy.ndarray, v2: numpy.ndarray) -> numpy.ndarray

   Get the quaternion between two vectors.

   :param v1: The first vector.
   :param v2: The second vector.
   :return: The quaternion between the two vectors.


.. py:function:: get_axis_angle_between_two_vectors(v1: numpy.ndarray, v2: numpy.ndarray) -> typing_extensions.Tuple[numpy.ndarray, float]

   Get the axis and angle between two vectors.

   :param v1: The first vector.
   :param v2: The second vector.
   :return: The axis and angle between the two vectors.


.. py:class:: RayTestUtils(ray_test_batch: typing_extensions.Callable, object_id_to_name: typing_extensions.Dict = None)

   .. py:attribute:: local_transformer


   .. py:attribute:: ray_test_batch


   .. py:attribute:: object_id_to_name
      :value: None



   .. py:method:: get_images_for_target(cam_pose: pycram.datastructures.pose.PoseStamped, camera_description: pycram.robot_description.CameraDescription, camera_frame: str, size: int = 256, camera_min_distance: float = 0.1, camera_max_distance: int = 3, plot: bool = False) -> typing_extensions.List[numpy.ndarray]

      Note: The returned color image is a repeated depth image in 3 channels.



   .. py:method:: construct_segmentation_mask_from_ray_test_object_ids(object_ids: typing_extensions.List[int], size: int) -> numpy.ndarray
      :staticmethod:


      Construct a segmentation mask from the object ids returned by the ray test.

      :param object_ids: The object ids.
      :param size: The size of the grid.
      :return: The segmentation mask.



   .. py:method:: construct_depth_image_from_ray_test_distances(distances: typing_extensions.List[float], size: int) -> numpy.ndarray
      :staticmethod:


      Construct a depth image from the distances returned by the ray test.

      :param distances: The distances.
      :param size: The size of the grid.
      :return: The depth image.



   .. py:method:: construct_color_image_from_depth_image(depth_image: numpy.ndarray) -> numpy.ndarray
      :staticmethod:


      Construct a color image from the depth image.

      :param depth_image: The depth image.
      :return: The color image.



   .. py:method:: get_camera_rays_start_positions(camera_description: pycram.robot_description.CameraDescription, camera_frame: str, camera_pose: pycram.datastructures.pose.PoseStamped, size: int, camera_min_distance: float) -> numpy.ndarray


   .. py:method:: get_camera_rays_start_pose(camera_description: pycram.robot_description.CameraDescription, camera_frame: str, camera_pose: pycram.datastructures.pose.PoseStamped, camera_min_distance: float) -> pycram.datastructures.pose.PoseStamped

      Get the start position of the camera rays, which is the camera pose shifted by the minimum distance of the
      camera.

      :param camera_description: The camera description.
      :param camera_frame: The camera tf frame.
      :param camera_pose: The camera pose.
      :param camera_min_distance: The minimum distance from which the camera can see.



   .. py:method:: get_camera_rays_end_positions(camera_description: pycram.robot_description.CameraDescription, camera_frame: str, camera_pose: pycram.datastructures.pose.PoseStamped, size: int, camera_max_distance: float = 3.0) -> numpy.ndarray

      Get the end positions of the camera rays.

      :param camera_description: The camera description.
      :param camera_frame: The camera frame.
      :param camera_pose: The camera pose.
      :param size: The size of the grid.
      :param camera_max_distance: The maximum distance of the camera.
      :return: The end positions of the camera rays.



   .. py:method:: transform_points_from_camera_frame_to_world_frame(camera_pose: pycram.datastructures.pose.PoseStamped, camera_frame: str, points: numpy.ndarray) -> numpy.ndarray
      :staticmethod:


      Transform points from the camera frame to the world frame.

      :param camera_pose: The camera pose.
      :param camera_frame: The camera frame.
      :param points: The points to transform.
      :return: The transformed points.



   .. py:method:: get_end_positions_of_rays_from_angles_and_distance(vertical_angles: numpy.ndarray, horizontal_angles: numpy.ndarray, distance: float) -> numpy.ndarray
      :staticmethod:


      Get the end positions of the rays from the angles and the distance.

      :param vertical_angles: The vertical angles of the rays.
      :param horizontal_angles: The horizontal angles of the rays.
      :param distance: The distance of the rays.
      :return: The end positions of the rays.



   .. py:method:: construct_grid_of_camera_rays_angles(camera_description: pycram.robot_description.CameraDescription, size: int) -> typing_extensions.Tuple[numpy.ndarray, numpy.ndarray]
      :staticmethod:


      Construct a 2D grid of camera rays angles.

      :param camera_description: The camera description.
      :param size: The size of the grid.
      :return: The 2D grid of the horizontal and the vertical angles of the camera rays.



   .. py:method:: plot_segmentation_mask(segmentation_mask, object_id_to_name: typing_extensions.Dict[int, str] = None)
      :staticmethod:


      Plot the segmentation mask with different colors for each object.

      :param segmentation_mask: The segmentation mask.
      :param object_id_to_name: The mapping from object id to object name.



   .. py:method:: plot_depth_image(depth_image)
      :staticmethod:



.. py:function:: wxyz_to_xyzw(wxyz: typing_extensions.List[float]) -> typing_extensions.List[float]

   Convert a quaternion from WXYZ to XYZW format.


.. py:function:: xyzw_to_wxyz(xyzw: typing_extensions.List[float]) -> typing_extensions.List[float]

   Convert a quaternion from XYZW to WXYZ format.

   :param xyzw: The quaternion in XYZW format.


.. py:function:: wxyz_to_xyzw_arr(wxyz: numpy.ndarray) -> numpy.ndarray

   Convert a quaternion from WXYZ to XYZW format.

   :param wxyz: The quaternion in WXYZ format.


.. py:function:: xyzw_to_wxyz_arr(xyzw: numpy.ndarray) -> numpy.ndarray

   Convert a quaternion from XYZW to WXYZ format.

   :param xyzw: The quaternion in XYZW format.


.. py:class:: ClassPropertyDescriptor(fget, fset=None)

   A helper that can be used to define properties of a class like the built-in ones but does not require the class
   to be instantiated.


   .. py:attribute:: fget


   .. py:attribute:: fset
      :value: None



   .. py:method:: __get__(obj, klass=None)


   .. py:method:: __set__(obj, value)


   .. py:method:: setter(func)


.. py:function:: classproperty(func)

.. py:function:: is_iterable(obj: typing_extensions.Any) -> bool

   Checks if the given object is iterable.

   :param obj: The object that should be checked
   :return: True if the object is iterable, False otherwise


.. py:function:: lazy_product(*iterables: typing_extensions.Iterable) -> typing_extensions.Iterable[typing_extensions.Tuple]

   Lazily generate the cartesian product of the iterables.

   :param iterables: Iterable of iterables to construct product for.
   :return: Iterable of tuples in the cartesian product.


