pycram.utils

Contents

pycram.utils#

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#

bcolors

Color codes which can be used to highlight Text in the Terminal. For example,

GeneratorList

Implementation of generator list wrappers.

suppress_stdout_stderr

A context manager for doing a "deep suppression" of stdout and stderr in

RayTestUtils

ClassPropertyDescriptor

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

Functions#

get_rays_from_min_max(→ numpy.ndarray)

Get rays from min and max bounds as an array of start and end 3D points.

chunks(→ typing_extensions.List)

Yield successive n-sized chunks from lst.

_apply_ik(→ None)

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

axis_angle_to_quaternion(→ typing_extensions.Tuple)

Convert axis-angle to quaternion.

adjust_camera_pose_based_on_target(...)

Adjust the given cam_pose orientation such that it is facing the target_pose, which partly depends on the

get_quaternion_between_camera_and_target(→ numpy.ndarray)

Get the quaternion between the camera and the target.

transform_vector_using_pose(→ numpy.ndarray)

Transform a vector using a pose.

apply_quaternion_to_pose(...)

Apply a quaternion to a pose.

get_quaternion_between_two_vectors(→ numpy.ndarray)

Get the quaternion between two vectors.

get_axis_angle_between_two_vectors(...)

Get the axis and angle between two vectors.

wxyz_to_xyzw(→ typing_extensions.List[float])

Convert a quaternion from WXYZ to XYZW format.

xyzw_to_wxyz(→ typing_extensions.List[float])

Convert a quaternion from XYZW to WXYZ format.

wxyz_to_xyzw_arr(→ numpy.ndarray)

Convert a quaternion from WXYZ to XYZW format.

xyzw_to_wxyz_arr(→ numpy.ndarray)

Convert a quaternion from XYZW to WXYZ format.

classproperty(func)

is_iterable(→ bool)

Checks if the given object is iterable.

lazy_product(...)

Lazily generate the cartesian product of the iterables.

Module Contents#

pycram.utils.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. ]] ])

Parameters:
  • min_bound – The minimum bound of the rays, a sequence of 3 floats.

  • max_bound – The maximum bound of the rays, a sequence of 3 floats.

  • step_size_in_meters – The step size in meters between the rays.

Returns:

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.

pycram.utils.chunks(lst: typing_extensions.List, n: int) typing_extensions.List#

Yield successive n-sized chunks from lst.

Parameters:
  • lst – The list from which chunks should be yielded

  • n – Size of the chunks

Returns:

A list of size n from lst

class pycram.utils.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}’)

HEADER = '\x1b[95m'#
OKBLUE = '\x1b[94m'#
OKCYAN = '\x1b[96m'#
OKGREEN = '\x1b[92m'#
WARNING = '\x1b[93m'#
FAIL = '\x1b[91m'#
ENDC = '\x1b[0m'#
BOLD = '\x1b[1m'#
UNDERLINE = '\x1b[4m'#
pycram.utils._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

Parameters:
  • robot – The robot the joint poses should be applied on

  • pose_and_joint_poses – The base pose and joint states as returned by the ik solver

Returns:

None

class pycram.utils.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.

_generated = []#
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.

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.

pycram.utils.axis_angle_to_quaternion(axis: typing_extensions.List, angle: float) typing_extensions.Tuple#

Convert axis-angle to quaternion.

Parameters:
  • axis – (x, y, z) tuple representing rotation axis.

  • angle – rotation angle in degree

Returns:

The quaternion representing the axis angle

class pycram.utils.suppress_stdout_stderr#

Bases: 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

null_fds#
save_fds#
__enter__()#
__exit__(*_)#
pycram.utils.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.

Parameters:
  • cam_pose – The camera pose.

  • target_pose – The target pose.

  • camera_description – The camera description.

Returns:

The adjusted camera pose.

pycram.utils.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.

Parameters:
  • cam_pose – The camera pose.

  • target_pose – The target pose.

  • camera_description – The camera description.

Returns:

The quaternion between the camera and the target.

pycram.utils.transform_vector_using_pose(vector: typing_extensions.Sequence, pose) numpy.ndarray#

Transform a vector using a pose.

Parameters:
  • vector – The vector.

  • pose – The pose.

Returns:

The transformed vector.

pycram.utils.apply_quaternion_to_pose(pose: pycram.datastructures.pose.PoseStamped, quaternion: numpy.ndarray) pycram.datastructures.pose.PoseStamped#

Apply a quaternion to a pose.

Parameters:
  • pose – The pose.

  • quaternion – The quaternion.

Returns:

The new pose.

pycram.utils.get_quaternion_between_two_vectors(v1: numpy.ndarray, v2: numpy.ndarray) numpy.ndarray#

Get the quaternion between two vectors.

Parameters:
  • v1 – The first vector.

  • v2 – The second vector.

Returns:

The quaternion between the two vectors.

pycram.utils.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.

Parameters:
  • v1 – The first vector.

  • v2 – The second vector.

Returns:

The axis and angle between the two vectors.

class pycram.utils.RayTestUtils(ray_test_batch: typing_extensions.Callable, object_id_to_name: typing_extensions.Dict = None)#
local_transformer#
ray_test_batch#
object_id_to_name = None#
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.

static construct_segmentation_mask_from_ray_test_object_ids(object_ids: typing_extensions.List[int], size: int) numpy.ndarray#

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

Parameters:
  • object_ids – The object ids.

  • size – The size of the grid.

Returns:

The segmentation mask.

static construct_depth_image_from_ray_test_distances(distances: typing_extensions.List[float], size: int) numpy.ndarray#

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

Parameters:
  • distances – The distances.

  • size – The size of the grid.

Returns:

The depth image.

static construct_color_image_from_depth_image(depth_image: numpy.ndarray) numpy.ndarray#

Construct a color image from the depth image.

Parameters:

depth_image – The depth image.

Returns:

The color image.

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#
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.

Parameters:
  • camera_description – The camera description.

  • camera_frame – The camera tf frame.

  • camera_pose – The camera pose.

  • camera_min_distance – The minimum distance from which the camera can see.

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.

Parameters:
  • camera_description – The camera description.

  • camera_frame – The camera frame.

  • camera_pose – The camera pose.

  • size – The size of the grid.

  • camera_max_distance – The maximum distance of the camera.

Returns:

The end positions of the camera rays.

static transform_points_from_camera_frame_to_world_frame(camera_pose: pycram.datastructures.pose.PoseStamped, camera_frame: str, points: numpy.ndarray) numpy.ndarray#

Transform points from the camera frame to the world frame.

Parameters:
  • camera_pose – The camera pose.

  • camera_frame – The camera frame.

  • points – The points to transform.

Returns:

The transformed points.

static get_end_positions_of_rays_from_angles_and_distance(vertical_angles: numpy.ndarray, horizontal_angles: numpy.ndarray, distance: float) numpy.ndarray#

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

Parameters:
  • vertical_angles – The vertical angles of the rays.

  • horizontal_angles – The horizontal angles of the rays.

  • distance – The distance of the rays.

Returns:

The end positions of the rays.

static construct_grid_of_camera_rays_angles(camera_description: pycram.robot_description.CameraDescription, size: int) typing_extensions.Tuple[numpy.ndarray, numpy.ndarray]#

Construct a 2D grid of camera rays angles.

Parameters:
  • camera_description – The camera description.

  • size – The size of the grid.

Returns:

The 2D grid of the horizontal and the vertical angles of the camera rays.

static plot_segmentation_mask(segmentation_mask, object_id_to_name: typing_extensions.Dict[int, str] = None)#

Plot the segmentation mask with different colors for each object.

Parameters:
  • segmentation_mask – The segmentation mask.

  • object_id_to_name – The mapping from object id to object name.

static plot_depth_image(depth_image)#
pycram.utils.wxyz_to_xyzw(wxyz: typing_extensions.List[float]) typing_extensions.List[float]#

Convert a quaternion from WXYZ to XYZW format.

pycram.utils.xyzw_to_wxyz(xyzw: typing_extensions.List[float]) typing_extensions.List[float]#

Convert a quaternion from XYZW to WXYZ format.

Parameters:

xyzw – The quaternion in XYZW format.

pycram.utils.wxyz_to_xyzw_arr(wxyz: numpy.ndarray) numpy.ndarray#

Convert a quaternion from WXYZ to XYZW format.

Parameters:

wxyz – The quaternion in WXYZ format.

pycram.utils.xyzw_to_wxyz_arr(xyzw: numpy.ndarray) numpy.ndarray#

Convert a quaternion from XYZW to WXYZ format.

Parameters:

xyzw – The quaternion in XYZW format.

class pycram.utils.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.

fget#
fset = None#
__get__(obj, klass=None)#
__set__(obj, value)#
setter(func)#
pycram.utils.classproperty(func)#
pycram.utils.is_iterable(obj: typing_extensions.Any) bool#

Checks if the given object is iterable.

Parameters:

obj – The object that should be checked

Returns:

True if the object is iterable, False otherwise

pycram.utils.lazy_product(*iterables: typing_extensions.Iterable) typing_extensions.Iterable[typing_extensions.Tuple]#

Lazily generate the cartesian product of the iterables.

Parameters:

iterables – Iterable of iterables to construct product for.

Returns:

Iterable of tuples in the cartesian product.